katequickopen.cpp 12.1 KB
Newer Older
Christoph Cullmann's avatar
Christoph Cullmann committed
1
2
/*  SPDX-License-Identifier: LGPL-2.0-or-later

3
    SPDX-FileCopyrightText: 2007, 2009 Joseph Wenninger <jowenn@kde.org>
Christoph Cullmann's avatar
Christoph Cullmann committed
4

5
    SPDX-License-Identifier: LGPL-2.0-or-later
6
7
8
*/

#include "katequickopen.h"
9
#include "katequickopenmodel.h"
Christoph Cullmann's avatar
Christoph Cullmann committed
10

11
#include "kateapp.h"
Dominik Haumann's avatar
Dominik Haumann committed
12
#include "katemainwindow.h"
13
#include "kateviewmanager.h"
14
15
16
17

#include <ktexteditor/document.h>
#include <ktexteditor/view.h>

Michal Humpula's avatar
Michal Humpula committed
18
19
#include <KAboutData>
#include <KActionCollection>
Waqar Ahmed's avatar
Waqar Ahmed committed
20
#include <KConfigGroup>
Michal Humpula's avatar
Michal Humpula committed
21
#include <KLocalizedString>
22
#include <KPluginFactory>
Waqar Ahmed's avatar
Waqar Ahmed committed
23
#include <KSharedConfig>
Michal Humpula's avatar
Michal Humpula committed
24

25
26
27
#include <QBoxLayout>
#include <QCoreApplication>
#include <QDesktopWidget>
Michal Humpula's avatar
Michal Humpula committed
28
29
#include <QEvent>
#include <QFileInfo>
30
31
#include <QHeaderView>
#include <QLabel>
32
#include <QPainter>
Michal Humpula's avatar
Michal Humpula committed
33
#include <QPointer>
34
#include <QSortFilterProxyModel>
Michal Humpula's avatar
Michal Humpula committed
35
#include <QStandardItemModel>
36
37
#include <QStyledItemDelegate>
#include <QTextDocument>
38
#include <QTreeView>
39

40
#include <kfts_fuzzy_match.h>
41
42
43

class QuickOpenFilterProxyModel : public QSortFilterProxyModel
{
44
public:
45
46
47
48
    QuickOpenFilterProxyModel(QObject *parent = nullptr)
        : QSortFilterProxyModel(parent)
    {
    }
49

50
51
52
53
54
    void changeMode(FilterModes m)
    {
        mode = m;
    }

55
protected:
56
    bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override
57
    {
58
59
        int l = sourceLeft.data(KateQuickOpenModel::Score).toInt();
        int r = sourceRight.data(KateQuickOpenModel::Score).toInt();
60
61
62
        return l < r;
    }

63
64
    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
    {
65
        if (pattern.isEmpty())
66
            return true;
67
        const QString fileName = sourceModel()->index(sourceRow, 0, sourceParent).data().toString();
Waqar Ahmed's avatar
Waqar Ahmed committed
68
        const auto nameAndPath = fileName.splitRef(QStringLiteral("{[split]}"));
69

70
71
        const auto &name = nameAndPath.at(0);
        const auto &path = nameAndPath.at(1);
72
73
74
75
76
77
78
79
        int score = 0;

        bool res = false;
        if (mode == FilterMode::FilterByName) {
            res = filterByName(name, score);
        } else if (mode == FilterMode::FilterByPath) {
            res = filterByPath(path, score);
        } else {
80
            int scorep = 0, scoren = 0;
81
82
83
84
85
86
87
            bool resp = filterByPath(path, scorep);
            bool resn = filterByName(name, scoren);

            // store the score for sorting later
            score = scoren + scorep;
            res = resp || resn;
        }
88

89
        auto idx = sourceModel()->index(sourceRow, 0, sourceParent);
90
        sourceModel()->setData(idx, score, KateQuickOpenModel::Score);
91

92
        return res;
93
94
95
    }

public Q_SLOTS:
96
    void setFilterText(const QString &text)
97
    {
98
        beginResetModel();
99
        pattern = text;
100
        endResetModel();
101
102
    }

103
private:
104
    inline bool filterByPath(const QStringRef &path, int &score) const
105
106
107
108
    {
        return kfts::fuzzy_match(pattern, path, score);
    }

109
    inline bool filterByName(const QStringRef &name, int &score) const
110
111
112
113
    {
        return kfts::fuzzy_match(pattern, name, score);
    }

114
private:
115
    QString pattern;
116
    FilterModes mode;
117
118
};

119
120
class QuickOpenStyleDelegate : public QStyledItemDelegate
{
121
public:
122
    QuickOpenStyleDelegate(QObject *parent = nullptr)
123
        : QStyledItemDelegate(parent)
124
125
    {
    }
126
127
128
129
130
131
132
133
134

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QStyleOptionViewItem options = option;
        initStyleOption(&options, index);

        QTextDocument doc;

        QString str = index.data().toString();
135

Waqar Ahmed's avatar
Waqar Ahmed committed
136
        auto namePath = str.split(QStringLiteral("{[split]}"));
137
138
139
        QString name = namePath.at(0);
        QString path = namePath.at(1);

140
141
        const QString nameColor = option.palette.color(QPalette::Link).name();

142
        if (mode == FilterMode::FilterByName) {
143
            kfts::to_fuzzy_matched_display_string(m_filterString, name, QStringLiteral("<b style=\"color:%1;\">").arg(nameColor), QStringLiteral("</b>"));
144
145
146
        } else if (mode == FilterMode::FilterByPath) {
            kfts::to_fuzzy_matched_display_string(m_filterString, path, QStringLiteral("<b>"), QStringLiteral("</b>"));
        } else {
147
            kfts::to_fuzzy_matched_display_string(m_filterString, name, QStringLiteral("<b style=\"color:%1;\">").arg(nameColor), QStringLiteral("</b>"));
148
149
            kfts::to_fuzzy_matched_display_string(m_filterString, path, QStringLiteral("<b>"), QStringLiteral("</b>"));
        }
150

Waqar Ahmed's avatar
Waqar Ahmed committed
151
        const auto pathFontsize = option.font.pointSize();
152
        doc.setHtml(QStringLiteral("<span style=\"font-size: %1pt;\">").arg(pathFontsize) + name + QStringLiteral("</span>") + QStringLiteral(" &nbsp;") +
153
                    QStringLiteral("<span style=\"color:gray; font-size:%1pt;\">").arg(pathFontsize - 1) + path + QStringLiteral("</span>"));
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        doc.setDocumentMargin(2);

        painter->save();

        // paint background
        if (option.state & QStyle::State_Selected) {
            painter->fillRect(option.rect, option.palette.highlight());
        } else {
            painter->fillRect(option.rect, option.palette.base());
        }

        options.text = QString(); // clear old text
        options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);

        // draw text
169
170
171
172
        painter->translate(option.rect.x(), option.rect.y());
        if (index.column() == 0) {
            painter->translate(25, 0);
        }
173
174
175
176
177
        doc.drawContents(painter);

        painter->restore();
    }

178
179
180
181
182
    void changeMode(FilterModes m)
    {
        mode = m;
    }

183
public Q_SLOTS:
184
    void setFilterString(const QString &text)
185
186
187
188
189
190
    {
        m_filterString = text;
    }

private:
    QString m_filterString;
191
    FilterModes mode;
192
193
};

194
195
Q_DECLARE_METATYPE(QPointer<KTextEditor::Document>)

Waqar Ahmed's avatar
Waqar Ahmed committed
196
KateQuickOpen::KateQuickOpen(KateMainWindow *mainWindow)
197
    : QMenu(mainWindow)
198
    , m_mainWindow(mainWindow)
Christoph Cullmann's avatar
Christoph Cullmann committed
199
{
200
    // ensure the components have some proper frame
201
    QVBoxLayout *layout = new QVBoxLayout();
202
    layout->setSpacing(0);
203
    layout->setContentsMargins(4, 4, 4, 4);
204
    setLayout(layout);
205

206
    m_inputLine = new QuickOpenLineEdit(this);
207
    setFocusProxy(m_inputLine);
208

209
    layout->addWidget(m_inputLine);
210

211
212
    m_listView = new QTreeView();
    layout->addWidget(m_listView, 1);
213
214
    m_listView->setTextElideMode(Qt::ElideLeft);

215
    m_base_model = new KateQuickOpenModel(m_mainWindow, this);
216

217
    m_model = new QuickOpenFilterProxyModel(this);
218
    m_model->setFilterRole(Qt::DisplayRole);
219
    m_model->setSortRole(KateQuickOpenModel::Score);
220
221
    m_model->setFilterCaseSensitivity(Qt::CaseInsensitive);
    m_model->setSortCaseSensitivity(Qt::CaseInsensitive);
222
    m_model->setFilterKeyColumn(Qt::DisplayRole);
223

224
225
    m_styleDelegate = new QuickOpenStyleDelegate(this);
    m_listView->setItemDelegate(m_styleDelegate);
226

227
228
    connect(m_inputLine, &QuickOpenLineEdit::textChanged, m_model, &QuickOpenFilterProxyModel::setFilterText);
    connect(m_inputLine, &QuickOpenLineEdit::textChanged, m_styleDelegate, &QuickOpenStyleDelegate::setFilterString);
229
230
231
    connect(m_inputLine, &QuickOpenLineEdit::textChanged, this, [this]() {
        m_listView->viewport()->update();
    });
232
233
    connect(m_inputLine, &QuickOpenLineEdit::returnPressed, this, &KateQuickOpen::slotReturnPressed);
    connect(m_inputLine, &QuickOpenLineEdit::filterModeChanged, this, &KateQuickOpen::slotfilterModeChanged);
234
    connect(m_inputLine, &QuickOpenLineEdit::listModeChanged, this, &KateQuickOpen::slotListModeChanged);
Laurent Montel's avatar
Laurent Montel committed
235
236
    connect(m_model, &QSortFilterProxyModel::rowsInserted, this, &KateQuickOpen::reselectFirst);
    connect(m_model, &QSortFilterProxyModel::rowsRemoved, this, &KateQuickOpen::reselectFirst);
237

Laurent Montel's avatar
Laurent Montel committed
238
    connect(m_listView, &QTreeView::activated, this, &KateQuickOpen::slotReturnPressed);
Waqar Ahmed's avatar
Waqar Ahmed committed
239
    connect(m_listView, &QTreeView::clicked, this, &KateQuickOpen::slotReturnPressed); // for single click
240
241

    m_listView->setModel(m_model);
242
    m_listView->setSortingEnabled(true);
243
    m_model->setSourceModel(m_base_model);
244

245
246
    m_inputLine->installEventFilter(this);
    m_listView->installEventFilter(this);
247
248
    m_listView->setHeaderHidden(true);
    m_listView->setRootIsDecorated(false);
Waqar Ahmed's avatar
Waqar Ahmed committed
249
250
251
    m_listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    setHidden(true);
252
253

    m_filterMode = m_inputLine->filterMode();
254
255
}

Waqar Ahmed's avatar
Waqar Ahmed committed
256
257
258
259
260
261
262
263
264
KateQuickOpen::~KateQuickOpen()
{
    KSharedConfig::Ptr cfg = KSharedConfig::openConfig();
    KConfigGroup cg(cfg, "General");

    cg.writeEntry("Quickopen Filter Mode", static_cast<int>(m_filterMode));
    cg.writeEntry("Quickopen List Mode", m_base_model->listMode() == KateQuickOpenModelList::CurrentProject);
}

265
266
bool KateQuickOpen::eventFilter(QObject *obj, QEvent *event)
{
267
268
    // catch key presses + shortcut overrides to allow to have ESC as application wide shortcut, too, see bug 409856
    if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
269
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
270
        if (obj == m_inputLine) {
271
            const bool forward2list = (keyEvent->key() == Qt::Key_Up) || (keyEvent->key() == Qt::Key_Down) || (keyEvent->key() == Qt::Key_PageUp) || (keyEvent->key() == Qt::Key_PageDown);
272
            if (forward2list) {
273
                QCoreApplication::sendEvent(m_listView, event);
274
275
                return true;
            }
276

277
            if (keyEvent->key() == Qt::Key_Escape) {
278
279
                m_mainWindow->slotWindowActivated();
                m_inputLine->clear();
280
                keyEvent->accept();
Waqar Ahmed's avatar
Waqar Ahmed committed
281
                hide();
282
                return true;
283
            }
284
        } else {
285
286
            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);
287
            if (forward2input) {
288
                QCoreApplication::sendEvent(m_inputLine, event);
289
290
291
292
                return true;
            }
        }
    }
293
294
295
296
297

    // 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();
Waqar Ahmed's avatar
Waqar Ahmed committed
298
        hide();
299
300
301
        return true;
    }

302
    return QWidget::eventFilter(obj, event);
303
304
}

305
306
void KateQuickOpen::reselectFirst()
{
307
    int first = 0;
308
    if (m_mainWindow->viewManager()->sortedViews().size() > 1 && m_model->rowCount() > 1)
309
310
311
        first = 1;

    QModelIndex index = m_model->index(first, 0);
312
313
314
    m_listView->setCurrentIndex(index);
}

315
void KateQuickOpen::update()
316
{
317
    m_base_model->refresh();
318
    reselectFirst();
Waqar Ahmed's avatar
Waqar Ahmed committed
319

320
    updateViewGeometry();
Waqar Ahmed's avatar
Waqar Ahmed committed
321
322
    show();
    setFocus();
323
}
Christoph Cullmann's avatar
Christoph Cullmann committed
324

325
void KateQuickOpen::slotReturnPressed()
Christoph Cullmann's avatar
Christoph Cullmann committed
326
{
Waqar Ahmed's avatar
Waqar Ahmed committed
327
    const auto index = m_listView->model()->index(m_listView->currentIndex().row(), 0);
328
    auto url = index.data(Qt::UserRole).toUrl();
329
    m_mainWindow->wrapper()->openUrl(url);
Waqar Ahmed's avatar
Waqar Ahmed committed
330
    hide();
331
332
    m_mainWindow->slotWindowActivated();
    m_inputLine->clear();
333
}
334

335
336
337
338
339
340
341
342
void KateQuickOpen::slotfilterModeChanged(FilterModes mode)
{
    m_filterMode = mode;
    m_model->changeMode(mode);
    m_styleDelegate->changeMode(mode);
    m_model->invalidate();
}

343
void KateQuickOpen::slotListModeChanged(KateQuickOpenModel::List mode)
344
345
346
347
{
    m_base_model->setListMode(mode);
}

Waqar Ahmed's avatar
Waqar Ahmed committed
348
349
void KateQuickOpen::updateViewGeometry()
{
350
    const QSize centralSize = m_mainWindow->size();
Waqar Ahmed's avatar
Waqar Ahmed committed
351

352
353
    // width: 2.4 of editor, height: 1/2 of editor
    const QSize viewMaxSize(centralSize.width() / 2.4, centralSize.height() / 2);
Waqar Ahmed's avatar
Waqar Ahmed committed
354
355
356
357
358

    const int rowHeight = m_listView->sizeHintForRow(0) == -1 ? 0 : m_listView->sizeHintForRow(0);

    const int width = viewMaxSize.width();

359
360
    const QSize viewSize(std::max(300, width), // never go below this
                         std::min(std::max(rowHeight * m_base_model->rowCount() + 2, rowHeight * 6), viewMaxSize.height()));
Waqar Ahmed's avatar
Waqar Ahmed committed
361

362
    // Position should be central over window
363
364
    const int xPos = std::max(0, (centralSize.width() - viewSize.width()) / 2);
    const int yPos = std::max(0, (centralSize.height() - viewSize.height()) * 1 / 4);
Waqar Ahmed's avatar
Waqar Ahmed committed
365

366
367
    const QPoint p(xPos, yPos);
    move(p + m_mainWindow->pos());
Waqar Ahmed's avatar
Waqar Ahmed committed
368
369
370
371
372
373
374
375

    QPointer<QPropertyAnimation> animation = new QPropertyAnimation(this, "size");
    animation->setDuration(150);
    animation->setStartValue(this->size());
    animation->setEndValue(viewSize);

    animation->start();
}