2012-04-22 17:09:43 +02:00
|
|
|
/* ============================================================
|
2017-02-13 20:56:16 +01:00
|
|
|
* QupZilla - Qt web browser
|
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2012-04-22 17:09:43 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "locationcompleterview.h"
|
2012-05-05 16:06:24 +02:00
|
|
|
#include "locationcompletermodel.h"
|
2012-12-04 14:29:27 +01:00
|
|
|
#include "locationcompleterdelegate.h"
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QApplication>
|
2012-09-08 22:54:53 +02:00
|
|
|
#include <QScrollBar>
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
LocationCompleterView::LocationCompleterView()
|
|
|
|
: QListView(0)
|
|
|
|
, m_ignoreNextMouseMove(false)
|
|
|
|
{
|
2017-02-13 20:56:16 +01:00
|
|
|
setAttribute(Qt::WA_ShowWithoutActivating);
|
|
|
|
setAttribute(Qt::WA_X11NetWmWindowTypeCombo);
|
2017-02-26 20:27:37 +01:00
|
|
|
|
2017-07-19 12:26:26 +02:00
|
|
|
if (qApp->platformName() == QL1S("xcb")) {
|
2017-02-26 20:27:37 +01:00
|
|
|
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::BypassWindowManagerHint);
|
2017-07-19 12:26:26 +02:00
|
|
|
} else {
|
|
|
|
setWindowFlags(Qt::Popup);
|
2017-02-26 20:27:37 +01:00
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
setUniformItemSizes(true);
|
|
|
|
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
|
|
|
|
setMouseTracking(true);
|
2017-02-13 20:56:16 +01:00
|
|
|
qApp->installEventFilter(this);
|
2014-03-01 13:02:57 +01:00
|
|
|
|
|
|
|
m_delegate = new LocationCompleterDelegate(this);
|
|
|
|
setItemDelegate(m_delegate);
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QPersistentModelIndex LocationCompleterView::hoveredIndex() const
|
|
|
|
{
|
|
|
|
return m_hoveredIndex;
|
|
|
|
}
|
|
|
|
|
2017-08-11 12:26:50 +02:00
|
|
|
void LocationCompleterView::setOriginalText(const QString &originalText)
|
|
|
|
{
|
|
|
|
m_delegate->setOriginalText(originalText);
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
|
|
|
{
|
|
|
|
// Event filter based on QCompleter::eventFilter from qcompleter.cpp
|
|
|
|
|
2017-02-13 20:56:16 +01:00
|
|
|
if (object == this || !isVisible()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::KeyPress: {
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
2014-03-01 13:02:57 +01:00
|
|
|
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
|
2017-08-11 12:26:50 +02:00
|
|
|
const QModelIndex idx = m_hoveredIndex;
|
|
|
|
const QModelIndex visitSearchIdx = model()->index(0, 0).data(LocationCompleterModel::VisitSearchItemRole).toBool() ? model()->index(0, 0) : QModelIndex();
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2014-03-01 13:02:57 +01:00
|
|
|
if ((keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down) && currentIndex() != idx) {
|
|
|
|
setCurrentIndex(idx);
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (keyEvent->key()) {
|
2012-12-04 14:29:27 +01:00
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Enter:
|
2014-03-01 13:02:57 +01:00
|
|
|
if (!idx.isValid()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-02 19:58:54 +01:00
|
|
|
if (modifiers == Qt::NoModifier || modifiers == Qt::KeypadModifier) {
|
2014-03-01 13:02:57 +01:00
|
|
|
emit indexActivated(idx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modifiers == Qt::ControlModifier) {
|
|
|
|
emit indexCtrlActivated(idx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modifiers == Qt::ShiftModifier) {
|
|
|
|
emit indexShiftActivated(idx);
|
|
|
|
return true;
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
case Qt::Key_End:
|
2017-02-13 20:56:16 +01:00
|
|
|
if (modifiers & Qt::ControlModifier) {
|
|
|
|
setCurrentIndex(model()->index(model()->rowCount() - 1, 0));
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
case Qt::Key_Home:
|
2014-03-01 13:02:57 +01:00
|
|
|
if (modifiers & Qt::ControlModifier) {
|
2017-02-13 20:56:16 +01:00
|
|
|
setCurrentIndex(model()->index(0, 0));
|
|
|
|
scrollToTop();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
close();
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::Key_Escape:
|
|
|
|
close();
|
2017-08-11 12:32:23 +02:00
|
|
|
return true;
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
case Qt::Key_F4:
|
2014-03-01 13:02:57 +01:00
|
|
|
if (modifiers == Qt::AltModifier) {
|
2012-04-22 17:09:43 +02:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::Key_Tab:
|
|
|
|
case Qt::Key_Backtab: {
|
2017-02-13 20:56:16 +01:00
|
|
|
if (keyEvent->modifiers() != Qt::NoModifier) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
Qt::Key k = keyEvent->key() == Qt::Key_Tab ? Qt::Key_Down : Qt::Key_Up;
|
|
|
|
QKeyEvent ev(QKeyEvent::KeyPress, k, Qt::NoModifier);
|
2017-02-13 20:56:16 +01:00
|
|
|
QApplication::sendEvent(focusProxy(), &ev);
|
|
|
|
return true;
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::Key_Up:
|
2017-08-11 12:26:50 +02:00
|
|
|
if (!idx.isValid() || idx == visitSearchIdx) {
|
2012-04-22 17:09:43 +02:00
|
|
|
int rowCount = model()->rowCount();
|
|
|
|
QModelIndex lastIndex = model()->index(rowCount - 1, 0);
|
|
|
|
setCurrentIndex(lastIndex);
|
2017-02-13 20:56:16 +01:00
|
|
|
} else if (idx.row() == 0) {
|
2012-04-22 17:09:43 +02:00
|
|
|
setCurrentIndex(QModelIndex());
|
2017-02-13 20:56:16 +01:00
|
|
|
} else {
|
|
|
|
setCurrentIndex(model()->index(idx.row() - 1, 0));
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
2017-02-13 20:56:16 +01:00
|
|
|
return true;
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
case Qt::Key_Down:
|
2014-03-01 13:02:57 +01:00
|
|
|
if (!idx.isValid()) {
|
2012-04-22 17:09:43 +02:00
|
|
|
QModelIndex firstIndex = model()->index(0, 0);
|
|
|
|
setCurrentIndex(firstIndex);
|
2017-08-11 12:26:50 +02:00
|
|
|
} else if (idx != visitSearchIdx && idx.row() == model()->rowCount() - 1) {
|
|
|
|
setCurrentIndex(visitSearchIdx);
|
2012-04-22 17:09:43 +02:00
|
|
|
scrollToTop();
|
2017-02-13 20:56:16 +01:00
|
|
|
} else {
|
|
|
|
setCurrentIndex(model()->index(idx.row() + 1, 0));
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
2017-02-13 20:56:16 +01:00
|
|
|
return true;
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2012-05-05 16:06:24 +02:00
|
|
|
case Qt::Key_Delete:
|
2014-03-01 13:02:57 +01:00
|
|
|
if (viewport()->rect().contains(visualRect(idx))) {
|
|
|
|
emit indexDeleteRequested(idx);
|
2012-05-05 16:06:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
case Qt::Key_PageUp:
|
2017-02-13 20:56:16 +01:00
|
|
|
if (keyEvent->modifiers() != Qt::NoModifier) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
selectionModel()->setCurrentIndex(moveCursor(QAbstractItemView::MovePageUp, Qt::NoModifier), QItemSelectionModel::SelectCurrent);
|
|
|
|
return true;
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
case Qt::Key_PageDown:
|
2017-02-13 20:56:16 +01:00
|
|
|
if (keyEvent->modifiers() != Qt::NoModifier) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
selectionModel()->setCurrentIndex(moveCursor(QAbstractItemView::MovePageDown, Qt::NoModifier), QItemSelectionModel::SelectCurrent);
|
|
|
|
return true;
|
2012-12-04 14:29:27 +01:00
|
|
|
|
|
|
|
case Qt::Key_Shift:
|
|
|
|
// don't switch if there is no hovered or selected index to not disturb typing
|
2014-03-01 13:02:57 +01:00
|
|
|
if (idx.isValid() || m_hoveredIndex.isValid()) {
|
|
|
|
m_delegate->setShowSwitchToTab(false);
|
2012-12-04 14:29:27 +01:00
|
|
|
viewport()->update();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
2012-04-22 17:09:43 +02:00
|
|
|
} // switch (keyEvent->key())
|
|
|
|
|
|
|
|
(static_cast<QObject*>(focusProxy()))->event(keyEvent);
|
2012-04-24 15:58:36 +02:00
|
|
|
return true;
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
2012-12-04 14:29:27 +01:00
|
|
|
case QEvent::KeyRelease: {
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
|
2012-12-10 14:59:12 +01:00
|
|
|
switch (keyEvent->key()) {
|
|
|
|
case Qt::Key_Shift:
|
2014-03-01 13:02:57 +01:00
|
|
|
m_delegate->setShowSwitchToTab(true);
|
|
|
|
viewport()->update();
|
|
|
|
return true;
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
2014-02-01 19:21:49 +01:00
|
|
|
break;
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
case QEvent::Show:
|
2014-03-01 13:02:57 +01:00
|
|
|
// Don't hover item when showing completer and mouse is currently in rect
|
2012-04-22 17:09:43 +02:00
|
|
|
m_ignoreNextMouseMove = true;
|
2012-04-24 15:58:36 +02:00
|
|
|
break;
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2017-02-13 20:56:16 +01:00
|
|
|
case QEvent::Wheel:
|
2012-04-22 17:09:43 +02:00
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
if (!underMouse()) {
|
|
|
|
close();
|
2017-02-13 20:56:16 +01:00
|
|
|
return false;
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-02-13 20:56:16 +01:00
|
|
|
case QEvent::FocusOut: {
|
|
|
|
QFocusEvent *focusEvent = static_cast<QFocusEvent*>(event);
|
2017-07-19 12:39:36 +02:00
|
|
|
if (focusEvent->reason() != Qt::PopupFocusReason && focusEvent->reason() != Qt::MouseFocusReason) {
|
2017-02-13 20:56:16 +01:00
|
|
|
close();
|
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
break;
|
2017-02-13 20:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::Move:
|
|
|
|
case QEvent::Resize: {
|
|
|
|
QWidget *w = qobject_cast<QWidget*>(object);
|
|
|
|
if (w && w->isWindow() && w == focusProxy()->window()) {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
default:
|
2012-04-24 15:58:36 +02:00
|
|
|
break;
|
2012-04-22 17:09:43 +02:00
|
|
|
} // switch (event->type())
|
|
|
|
|
2012-04-24 15:58:36 +02:00
|
|
|
return false;
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleterView::close()
|
|
|
|
{
|
|
|
|
QListView::hide();
|
2012-09-08 22:54:53 +02:00
|
|
|
verticalScrollBar()->setValue(0);
|
2014-03-01 13:02:57 +01:00
|
|
|
|
|
|
|
m_hoveredIndex = QPersistentModelIndex();
|
|
|
|
m_delegate->setShowSwitchToTab(true);
|
|
|
|
|
|
|
|
emit closed();
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleterView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
|
|
|
{
|
|
|
|
m_hoveredIndex = current;
|
|
|
|
|
|
|
|
QListView::currentChanged(current, previous);
|
|
|
|
|
|
|
|
viewport()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleterView::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (m_ignoreNextMouseMove || !isVisible()) {
|
|
|
|
m_ignoreNextMouseMove = false;
|
|
|
|
QListView::mouseMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex last = m_hoveredIndex;
|
|
|
|
QModelIndex atCursor = indexAt(mapFromGlobal(QCursor::pos()));
|
|
|
|
|
|
|
|
if (atCursor.isValid()) {
|
|
|
|
m_hoveredIndex = atCursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last != atCursor) {
|
|
|
|
viewport()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QListView::mouseMoveEvent(event);
|
|
|
|
}
|
2012-12-04 14:29:27 +01:00
|
|
|
|
|
|
|
void LocationCompleterView::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
2014-03-01 13:02:57 +01:00
|
|
|
if (m_hoveredIndex.isValid()) {
|
2014-03-01 14:12:50 +01:00
|
|
|
Qt::MouseButton button = event->button();
|
2014-03-01 13:02:57 +01:00
|
|
|
Qt::KeyboardModifiers modifiers = event->modifiers();
|
|
|
|
|
2014-03-01 14:12:50 +01:00
|
|
|
if (button == Qt::LeftButton && modifiers == Qt::NoModifier) {
|
2014-03-01 13:02:57 +01:00
|
|
|
emit indexActivated(m_hoveredIndex);
|
|
|
|
return;
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
2014-03-01 13:02:57 +01:00
|
|
|
|
2014-03-01 14:12:50 +01:00
|
|
|
if (button == Qt::MiddleButton || (button == Qt::LeftButton && modifiers == Qt::ControlModifier)) {
|
2014-03-01 13:02:57 +01:00
|
|
|
emit indexCtrlActivated(m_hoveredIndex);
|
|
|
|
return;
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 14:12:50 +01:00
|
|
|
if (button == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
|
2014-03-01 13:02:57 +01:00
|
|
|
emit indexShiftActivated(m_hoveredIndex);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-07 23:34:38 +01:00
|
|
|
}
|
2014-03-01 13:02:57 +01:00
|
|
|
|
|
|
|
QListView::mouseReleaseEvent(event);
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|