diff --git a/kate/katequickopen.cpp b/kate/katequickopen.cpp index cf82e852b..93b33dd28 100644 --- a/kate/katequickopen.cpp +++ b/kate/katequickopen.cpp @@ -1,168 +1,178 @@ /* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2007,2009 Joseph Wenninger */ #include "katequickopen.h" #include "katequickopenmodel.h" #include "katemainwindow.h" #include "kateviewmanager.h" #include "kateapp.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include Q_DECLARE_METATYPE(QPointer) KateQuickOpen::KateQuickOpen(QWidget *parent, KateMainWindow *mainWindow) : QWidget(parent) , m_mainWindow(mainWindow) - , m_matchMode(0) { QVBoxLayout *layout = new QVBoxLayout(); layout->setSpacing(0); layout->setContentsMargins(0, 0, 0, 0); setLayout(layout); m_inputLine = new KLineEdit(); setFocusProxy(m_inputLine); m_inputLine->setPlaceholderText(i18n("Quick Open Search")); layout->addWidget(m_inputLine); m_listView = new QTreeView(); layout->addWidget(m_listView, 1); m_listView->setTextElideMode(Qt::ElideLeft); m_base_model = new KateQuickOpenModel(m_mainWindow, this); m_model = new QSortFilterProxyModel(this); m_model->setFilterRole(Qt::DisplayRole); m_model->setSortRole(Qt::DisplayRole); m_model->setFilterCaseSensitivity(Qt::CaseInsensitive); m_model->setSortCaseSensitivity(Qt::CaseInsensitive); - m_model->setFilterKeyColumn(m_matchMode); + m_model->setFilterKeyColumn(0); connect(m_inputLine, &KLineEdit::textChanged, m_model, &QSortFilterProxyModel::setFilterWildcard); connect(m_inputLine, &KLineEdit::returnPressed, this, &KateQuickOpen::slotReturnPressed); connect(m_model, &QSortFilterProxyModel::rowsInserted, this, &KateQuickOpen::reselectFirst); connect(m_model, &QSortFilterProxyModel::rowsRemoved, this, &KateQuickOpen::reselectFirst); connect(m_listView, &QTreeView::activated, this, &KateQuickOpen::slotReturnPressed); m_listView->setModel(m_model); m_model->setSourceModel(m_base_model); m_inputLine->installEventFilter(this); m_listView->installEventFilter(this); m_listView->setHeaderHidden(true); m_listView->setRootIsDecorated(false); } bool KateQuickOpen::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); if (obj == m_inputLine) { const bool forward2list = (keyEvent->key() == Qt::Key_Up) || (keyEvent->key() == Qt::Key_Down) || (keyEvent->key() == Qt::Key_PageUp) || (keyEvent->key() == Qt::Key_PageDown); if (forward2list) { QCoreApplication::sendEvent(m_listView, event); return true; } if (keyEvent->key() == Qt::Key_Escape) { m_mainWindow->slotWindowActivated(); m_inputLine->clear(); return true; } } else { const bool forward2input = (keyEvent->key() != Qt::Key_Up) && (keyEvent->key() != Qt::Key_Down) && (keyEvent->key() != Qt::Key_PageUp) && (keyEvent->key() != Qt::Key_PageDown) && (keyEvent->key() != Qt::Key_Tab) && (keyEvent->key() != Qt::Key_Backtab); if (forward2input) { QCoreApplication::sendEvent(m_inputLine, event); return true; } } } // hide on focus out, if neither input field nor list have focus! else if (event->type() == QEvent::FocusOut && !(m_inputLine->hasFocus() || m_listView->hasFocus())) { m_mainWindow->slotWindowActivated(); m_inputLine->clear(); return true; } return QWidget::eventFilter(obj, event); } void KateQuickOpen::reselectFirst() { QModelIndex index = m_model->index(0, 0); m_listView->setCurrentIndex(index); } void KateQuickOpen::update() { m_base_model->refresh(); m_listView->resizeColumnToContents(0); // If we have a very long file name we restrict the size of the first column // to take at most half of the space. Otherwise it would look odd. int colw0 = m_listView->header()->sectionSize(0); // file name int colw1 = m_listView->header()->sectionSize(1); // file path if (colw0 > colw1) { m_listView->setColumnWidth(0, (colw0 + colw1) / 2); } } void KateQuickOpen::slotReturnPressed() { const auto index = m_listView->model()->index(m_listView->currentIndex().row(), KateQuickOpenModel::Columns::FilePath); auto url = index.data(Qt::UserRole).toUrl(); m_mainWindow->wrapper()->openUrl(url); m_mainWindow->slotWindowActivated(); m_inputLine->clear(); } + + +void KateQuickOpen::setMatchMode(int mode) +{ + m_model->setFilterKeyColumn(mode); +} + +int KateQuickOpen::matchMode() +{ + return m_model->filterKeyColumn(); +} diff --git a/kate/katequickopen.h b/kate/katequickopen.h index 08c40146b..bc0dd556f 100644 --- a/kate/katequickopen.h +++ b/kate/katequickopen.h @@ -1,83 +1,76 @@ /* Copyright (C) 2007,2009 Joseph Wenninger This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KATE_QUICK_OPEN_H #define KATE_QUICK_OPEN_H #include class KateMainWindow; class KLineEdit; class QModelIndex; class QStandardItemModel; class QSortFilterProxyModel; class QTreeView; class KateQuickOpenModel; class KateQuickOpen : public QWidget { Q_OBJECT public: KateQuickOpen(QWidget *parent, KateMainWindow *mainWindow); /** * update state * will fill model with current open documents, project documents, ... */ void update(); - void setMatchMode(int mode) { - m_matchMode = mode; - } - - int matchMode() { - return m_matchMode; - } + int matchMode(); + void setMatchMode(int mode); protected: bool eventFilter(QObject *obj, QEvent *event) override; private Q_SLOTS: void reselectFirst(); /** * Return pressed, activate the selected document * and go back to background */ void slotReturnPressed(); private: KateMainWindow *m_mainWindow; QTreeView *m_listView; KLineEdit *m_inputLine; /** * our model we search in */ KateQuickOpenModel *m_base_model; /** * filtered model we search in */ QSortFilterProxyModel *m_model; - - int m_matchMode; }; #endif