mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-11 09:32:12 +01:00
LocationCompleterView: Change into QWidget
This commit is contained in:
parent
bb76281174
commit
11e2ff168e
|
@ -375,14 +375,11 @@ void LocationCompleter::showPopup()
|
||||||
|
|
||||||
void LocationCompleter::adjustPopupSize()
|
void LocationCompleter::adjustPopupSize()
|
||||||
{
|
{
|
||||||
const int maxItemsCount = 12;
|
|
||||||
const int popupHeight = s_view->sizeHintForRow(0) * qMin(maxItemsCount, s_model->rowCount()) + 2 * s_view->frameWidth();
|
|
||||||
|
|
||||||
if (s_view->currentIndex().row() == 0) {
|
if (s_view->currentIndex().row() == 0) {
|
||||||
m_originalText = m_locationBar->text();
|
m_originalText = m_locationBar->text();
|
||||||
s_view->setOriginalText(m_originalText);
|
s_view->setOriginalText(m_originalText);
|
||||||
}
|
}
|
||||||
|
|
||||||
s_view->resize(s_view->width(), popupHeight);
|
s_view->adjustSize();
|
||||||
s_view->show();
|
s_view->show();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* ============================================================
|
/* ============================================================
|
||||||
* Falkon - Qt web browser
|
* Falkon - Qt web browser
|
||||||
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -22,9 +22,10 @@
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QScrollBar>
|
#include <QScrollBar>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
|
||||||
LocationCompleterView::LocationCompleterView()
|
LocationCompleterView::LocationCompleterView()
|
||||||
: QListView(0)
|
: QWidget(nullptr)
|
||||||
, m_ignoreNextMouseMove(false)
|
, m_ignoreNextMouseMove(false)
|
||||||
{
|
{
|
||||||
setAttribute(Qt::WA_ShowWithoutActivating);
|
setAttribute(Qt::WA_ShowWithoutActivating);
|
||||||
|
@ -36,18 +37,51 @@ LocationCompleterView::LocationCompleterView()
|
||||||
setWindowFlags(Qt::Popup);
|
setWindowFlags(Qt::Popup);
|
||||||
}
|
}
|
||||||
|
|
||||||
setUniformItemSizes(true);
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
||||||
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
layout->setContentsMargins(0, 0, 0, 0);
|
||||||
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
layout->setSpacing(0);
|
||||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
setLayout(layout);
|
||||||
setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
||||||
setSelectionMode(QAbstractItemView::SingleSelection);
|
|
||||||
|
|
||||||
setMouseTracking(true);
|
m_view = new QListView(this);
|
||||||
|
layout->addWidget(m_view);
|
||||||
|
|
||||||
|
m_view->setUniformItemSizes(true);
|
||||||
|
m_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||||
|
m_view->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
||||||
|
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
|
m_view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||||
|
m_view->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||||
|
|
||||||
|
m_view->setMouseTracking(true);
|
||||||
qApp->installEventFilter(this);
|
qApp->installEventFilter(this);
|
||||||
|
|
||||||
m_delegate = new LocationCompleterDelegate(this);
|
m_delegate = new LocationCompleterDelegate(this);
|
||||||
setItemDelegate(m_delegate);
|
m_view->setItemDelegate(m_delegate);
|
||||||
|
}
|
||||||
|
|
||||||
|
QAbstractItemModel *LocationCompleterView::model() const
|
||||||
|
{
|
||||||
|
return m_view->model();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LocationCompleterView::setModel(QAbstractItemModel *model)
|
||||||
|
{
|
||||||
|
m_view->setModel(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex LocationCompleterView::currentIndex() const
|
||||||
|
{
|
||||||
|
return m_view->currentIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LocationCompleterView::setCurrentIndex(const QModelIndex &index)
|
||||||
|
{
|
||||||
|
m_view->setCurrentIndex(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
QItemSelectionModel *LocationCompleterView::selectionModel() const
|
||||||
|
{
|
||||||
|
return m_view->selectionModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocationCompleterView::setOriginalText(const QString &originalText)
|
void LocationCompleterView::setOriginalText(const QString &originalText)
|
||||||
|
@ -55,11 +89,47 @@ void LocationCompleterView::setOriginalText(const QString &originalText)
|
||||||
m_delegate->setOriginalText(originalText);
|
m_delegate->setOriginalText(originalText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LocationCompleterView::adjustSize()
|
||||||
|
{
|
||||||
|
const int maxItemsCount = 12;
|
||||||
|
m_view->setFixedHeight(m_view->sizeHintForRow(0) * qMin(maxItemsCount, model()->rowCount()) + 2 * m_view->frameWidth());
|
||||||
|
setFixedHeight(sizeHint().height());
|
||||||
|
}
|
||||||
|
|
||||||
bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
||||||
{
|
{
|
||||||
// Event filter based on QCompleter::eventFilter from qcompleter.cpp
|
// Event filter based on QCompleter::eventFilter from qcompleter.cpp
|
||||||
|
|
||||||
if (object == this || !isVisible()) {
|
if (object == this || object == m_view || !isVisible()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (object == m_view->viewport()) {
|
||||||
|
if (event->type() == QEvent::MouseButtonRelease) {
|
||||||
|
QMouseEvent *e = static_cast<QMouseEvent*>(event);
|
||||||
|
QModelIndex idx = m_view->indexAt(e->pos());
|
||||||
|
if (!idx.isValid()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Qt::MouseButton button = e->button();
|
||||||
|
Qt::KeyboardModifiers modifiers = e->modifiers();
|
||||||
|
|
||||||
|
if (button == Qt::LeftButton && modifiers == Qt::NoModifier) {
|
||||||
|
emit indexActivated(idx);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (button == Qt::MiddleButton || (button == Qt::LeftButton && modifiers == Qt::ControlModifier)) {
|
||||||
|
emit indexCtrlActivated(idx);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (button == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
|
||||||
|
emit indexShiftActivated(idx);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,11 +137,11 @@ bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
||||||
case QEvent::KeyPress: {
|
case QEvent::KeyPress: {
|
||||||
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
||||||
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
|
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
|
||||||
const QModelIndex idx = currentIndex();
|
const QModelIndex idx = m_view->currentIndex();
|
||||||
const QModelIndex visitSearchIdx = model()->index(0, 0).data(LocationCompleterModel::VisitSearchItemRole).toBool() ? model()->index(0, 0) : QModelIndex();
|
const QModelIndex visitSearchIdx = model()->index(0, 0).data(LocationCompleterModel::VisitSearchItemRole).toBool() ? model()->index(0, 0) : QModelIndex();
|
||||||
|
|
||||||
if ((keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down) && currentIndex() != idx) {
|
if ((keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down) && m_view->currentIndex() != idx) {
|
||||||
setCurrentIndex(idx);
|
m_view->setCurrentIndex(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (keyEvent->key()) {
|
switch (keyEvent->key()) {
|
||||||
|
@ -99,7 +169,7 @@ bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
||||||
|
|
||||||
case Qt::Key_End:
|
case Qt::Key_End:
|
||||||
if (modifiers & Qt::ControlModifier) {
|
if (modifiers & Qt::ControlModifier) {
|
||||||
setCurrentIndex(model()->index(model()->rowCount() - 1, 0));
|
m_view->setCurrentIndex(model()->index(model()->rowCount() - 1, 0));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
close();
|
close();
|
||||||
|
@ -108,8 +178,8 @@ bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
||||||
|
|
||||||
case Qt::Key_Home:
|
case Qt::Key_Home:
|
||||||
if (modifiers & Qt::ControlModifier) {
|
if (modifiers & Qt::ControlModifier) {
|
||||||
setCurrentIndex(model()->index(0, 0));
|
m_view->setCurrentIndex(model()->index(0, 0));
|
||||||
scrollToTop();
|
m_view->scrollToTop();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
close();
|
close();
|
||||||
|
@ -142,52 +212,46 @@ bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
||||||
if (!idx.isValid() || idx == visitSearchIdx) {
|
if (!idx.isValid() || idx == visitSearchIdx) {
|
||||||
int rowCount = model()->rowCount();
|
int rowCount = model()->rowCount();
|
||||||
QModelIndex lastIndex = model()->index(rowCount - 1, 0);
|
QModelIndex lastIndex = model()->index(rowCount - 1, 0);
|
||||||
setCurrentIndex(lastIndex);
|
m_view->setCurrentIndex(lastIndex);
|
||||||
} else if (idx.row() == 0) {
|
} else if (idx.row() == 0) {
|
||||||
setCurrentIndex(QModelIndex());
|
m_view->setCurrentIndex(QModelIndex());
|
||||||
} else {
|
} else {
|
||||||
setCurrentIndex(model()->index(idx.row() - 1, 0));
|
m_view->setCurrentIndex(model()->index(idx.row() - 1, 0));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case Qt::Key_Down:
|
case Qt::Key_Down:
|
||||||
if (!idx.isValid()) {
|
if (!idx.isValid()) {
|
||||||
QModelIndex firstIndex = model()->index(0, 0);
|
QModelIndex firstIndex = model()->index(0, 0);
|
||||||
setCurrentIndex(firstIndex);
|
m_view->setCurrentIndex(firstIndex);
|
||||||
} else if (idx != visitSearchIdx && idx.row() == model()->rowCount() - 1) {
|
} else if (idx != visitSearchIdx && idx.row() == model()->rowCount() - 1) {
|
||||||
setCurrentIndex(visitSearchIdx);
|
m_view->setCurrentIndex(visitSearchIdx);
|
||||||
scrollToTop();
|
m_view->scrollToTop();
|
||||||
} else {
|
} else {
|
||||||
setCurrentIndex(model()->index(idx.row() + 1, 0));
|
m_view->setCurrentIndex(model()->index(idx.row() + 1, 0));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case Qt::Key_Delete:
|
case Qt::Key_Delete:
|
||||||
if (idx != visitSearchIdx && viewport()->rect().contains(visualRect(idx))) {
|
if (idx != visitSearchIdx && m_view->viewport()->rect().contains(m_view->visualRect(idx))) {
|
||||||
emit indexDeleteRequested(idx);
|
emit indexDeleteRequested(idx);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Qt::Key_PageUp:
|
case Qt::Key_PageUp:
|
||||||
if (keyEvent->modifiers() != Qt::NoModifier) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
selectionModel()->setCurrentIndex(moveCursor(QAbstractItemView::MovePageUp, Qt::NoModifier), QItemSelectionModel::SelectCurrent);
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case Qt::Key_PageDown:
|
case Qt::Key_PageDown:
|
||||||
if (keyEvent->modifiers() != Qt::NoModifier) {
|
if (keyEvent->modifiers() != Qt::NoModifier) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
selectionModel()->setCurrentIndex(moveCursor(QAbstractItemView::MovePageDown, Qt::NoModifier), QItemSelectionModel::SelectCurrent);
|
QApplication::sendEvent(m_view, event);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case Qt::Key_Shift:
|
case Qt::Key_Shift:
|
||||||
// don't switch if there is no hovered or selected index to not disturb typing
|
// don't switch if there is no hovered or selected index to not disturb typing
|
||||||
if (idx != visitSearchIdx || underMouse()) {
|
if (idx != visitSearchIdx || underMouse()) {
|
||||||
m_delegate->setShowSwitchToTab(false);
|
m_delegate->setShowSwitchToTab(false);
|
||||||
viewport()->update();
|
m_view->viewport()->update();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -205,7 +269,7 @@ bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
||||||
switch (keyEvent->key()) {
|
switch (keyEvent->key()) {
|
||||||
case Qt::Key_Shift:
|
case Qt::Key_Shift:
|
||||||
m_delegate->setShowSwitchToTab(true);
|
m_delegate->setShowSwitchToTab(true);
|
||||||
viewport()->update();
|
m_view->viewport()->update();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -250,38 +314,10 @@ bool LocationCompleterView::eventFilter(QObject* object, QEvent* event)
|
||||||
|
|
||||||
void LocationCompleterView::close()
|
void LocationCompleterView::close()
|
||||||
{
|
{
|
||||||
QListView::hide();
|
hide();
|
||||||
verticalScrollBar()->setValue(0);
|
m_view->verticalScrollBar()->setValue(0);
|
||||||
|
|
||||||
m_delegate->setShowSwitchToTab(true);
|
m_delegate->setShowSwitchToTab(true);
|
||||||
|
|
||||||
emit closed();
|
emit closed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocationCompleterView::mouseReleaseEvent(QMouseEvent* event)
|
|
||||||
{
|
|
||||||
QModelIndex idx = indexAt(event->pos());
|
|
||||||
if (!idx.isValid()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Qt::MouseButton button = event->button();
|
|
||||||
Qt::KeyboardModifiers modifiers = event->modifiers();
|
|
||||||
|
|
||||||
if (button == Qt::LeftButton && modifiers == Qt::NoModifier) {
|
|
||||||
emit indexActivated(idx);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (button == Qt::MiddleButton || (button == Qt::LeftButton && modifiers == Qt::ControlModifier)) {
|
|
||||||
emit indexCtrlActivated(idx);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (button == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
|
|
||||||
emit indexShiftActivated(idx);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QListView::mouseReleaseEvent(event);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* ============================================================
|
/* ============================================================
|
||||||
* Falkon - Qt web browser
|
* Falkon - Qt web browser
|
||||||
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -24,16 +24,24 @@
|
||||||
|
|
||||||
class LocationCompleterDelegate;
|
class LocationCompleterDelegate;
|
||||||
|
|
||||||
class FALKON_EXPORT LocationCompleterView : public QListView
|
class FALKON_EXPORT LocationCompleterView : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit LocationCompleterView();
|
explicit LocationCompleterView();
|
||||||
|
|
||||||
QPersistentModelIndex hoveredIndex() const;
|
QAbstractItemModel *model() const;
|
||||||
|
void setModel(QAbstractItemModel *model);
|
||||||
|
|
||||||
|
QItemSelectionModel *selectionModel() const;
|
||||||
|
|
||||||
|
QModelIndex currentIndex() const;
|
||||||
|
void setCurrentIndex(const QModelIndex &index);
|
||||||
|
|
||||||
void setOriginalText(const QString &originalText);
|
void setOriginalText(const QString &originalText);
|
||||||
|
|
||||||
|
void adjustSize();
|
||||||
|
|
||||||
bool eventFilter(QObject* object, QEvent* event);
|
bool eventFilter(QObject* object, QEvent* event);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
@ -47,13 +55,11 @@ signals:
|
||||||
public slots:
|
public slots:
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
protected:
|
|
||||||
void mouseReleaseEvent(QMouseEvent* event);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_ignoreNextMouseMove;
|
bool m_ignoreNextMouseMove;
|
||||||
|
|
||||||
LocationCompleterDelegate* m_delegate;
|
QListView *m_view;
|
||||||
|
LocationCompleterDelegate *m_delegate;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LOCATIONCOMPLETERVIEW_H
|
#endif // LOCATIONCOMPLETERVIEW_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user