File: typeaheadfind.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (178 lines) | stat: -rw-r--r-- 5,988 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * typeaheadfind.cpp  - plugin
 * Copyright (C) 2009-2010  Evgeny Khryukin
 *
 * 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 2
 * 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 <https://www.gnu.org/licenses/>.
 *
 */

#include "typeaheadfind.h"

#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

using namespace ClientSwitcher;

class TypeAheadFindBar::Private {
public:
    void doFind(bool backward = false)
    {
        QTextDocument::FindFlags options;
        if (caseSensitive)
            options |= QTextDocument::FindCaseSensitively;
        if (backward) {
            options |= QTextDocument::FindBackward;
            QTextCursor cursor = te->textCursor();
            cursor.setPosition(cursor.selectionStart());
            cursor.movePosition(QTextCursor::Left);
            te->setTextCursor(cursor);
        }
        if (find(text, options)) {
            le_find->setStyleSheet("");
        } else {
            le_find->setStyleSheet("QLineEdit { background: #ff6666; color: #ffffff }");
        }
    }

    bool find(const QString &str, QTextDocument::FindFlags options,
              QTextCursor::MoveOperation start = QTextCursor::NoMove)
    {
        Q_UNUSED(str);
        if (start != QTextCursor::NoMove) {
            QTextCursor cursor = te->textCursor();
            cursor.movePosition(start);
            te->setTextCursor(cursor);
        }
        bool found = te->find(text, options);
        if (!found) {
            if (start == QTextCursor::NoMove)
                return find(text, options,
                            options & QTextDocument::FindBackward ? QTextCursor::End : QTextCursor::Start);
            return false;
        }
        return true;
    }

    QString      text;
    bool         caseSensitive;
    QTextEdit *  te;
    QLineEdit *  le_find;
    QPushButton *but_next;
    QPushButton *but_prev;
    QPushButton *first_page, *next_page, *last_page, *prev_page;
    QCheckBox *  cb_case;
};

TypeAheadFindBar::TypeAheadFindBar(IconFactoryAccessingHost *IcoHost, QTextEdit *textedit, const QString &title,
                                   QWidget *parent) :
    QToolBar(title, parent),
    icoHost_(IcoHost)
{
    d     = new Private();
    d->te = textedit;
    init();
}

void TypeAheadFindBar::init()
{
    d->caseSensitive = false;
    d->text          = "";
    addWidget(new QLabel(tr("Search: "), this));

    d->le_find = new QLineEdit(this);
    d->le_find->setMaximumWidth(128);
    connect(d->le_find, &QLineEdit::textEdited, this, &TypeAheadFindBar::textChanged);
    addWidget(d->le_find);

    d->but_prev = new QPushButton(this);
    d->but_prev->setFixedSize(25, 25);
    d->but_prev->setIcon(icoHost_->getIcon("psi/arrowUp"));
    d->but_prev->setEnabled(false);
    connect(d->but_prev, &QPushButton::released, this, &TypeAheadFindBar::findPrevious);
    addWidget(d->but_prev);

    d->but_next = new QPushButton(this);
    d->but_next->setFixedSize(25, 25);
    d->but_next->setIcon(icoHost_->getIcon("psi/arrowDown"));
    d->but_next->setEnabled(false);
    connect(d->but_next, &QPushButton::released, this, &TypeAheadFindBar::findNext);
    addWidget(d->but_next);

    d->cb_case = new QCheckBox(tr("&Case sensitive"), this);
    connect(d->cb_case, &QCheckBox::clicked, this, &TypeAheadFindBar::caseToggled);
    addWidget(d->cb_case);

    addSeparator();

    d->first_page = new QPushButton(this);
    d->first_page->setToolTip(tr("First page"));
    connect(d->first_page, &QPushButton::released, this, &TypeAheadFindBar::firstPage);
    d->first_page->setFixedSize(25, 25);
    d->first_page->setIcon(icoHost_->getIcon("psi/doubleBackArrow"));
    addWidget(d->first_page);

    d->prev_page = new QPushButton(this);
    d->prev_page->setToolTip(tr("Previous page"));
    connect(d->prev_page, &QPushButton::released, this, &TypeAheadFindBar::prevPage);
    d->prev_page->setFixedSize(25, 25);
    d->prev_page->setIcon(icoHost_->getIcon("psi/arrowLeft"));
    addWidget(d->prev_page);

    d->next_page = new QPushButton(this);
    d->next_page->setToolTip(tr("Next page"));
    connect(d->next_page, &QPushButton::released, this, &TypeAheadFindBar::nextPage);
    d->next_page->setFixedSize(25, 25);
    d->next_page->setIcon(icoHost_->getIcon("psi/arrowRight"));
    addWidget(d->next_page);

    d->last_page = new QPushButton(this);
    d->last_page->setToolTip(tr("Last page"));
    connect(d->last_page, &QPushButton::released, this, &TypeAheadFindBar::lastPage);
    d->last_page->setFixedSize(25, 25);
    d->last_page->setIcon(icoHost_->getIcon("psi/doubleNextArrow"));
    addWidget(d->last_page);
}

TypeAheadFindBar::~TypeAheadFindBar()
{
    delete d;
    d = nullptr;
}

void TypeAheadFindBar::textChanged(const QString &str)
{
    QTextCursor cursor = d->te->textCursor();
    if (str.isEmpty()) {
        d->but_next->setEnabled(false);
        d->but_prev->setEnabled(false);
        d->le_find->setStyleSheet("");
        cursor.clearSelection();
        d->te->setTextCursor(cursor);
    } else {
        d->but_next->setEnabled(true);
        d->but_prev->setEnabled(true);
        cursor.setPosition(cursor.selectionStart());
        d->te->setTextCursor(cursor);
        d->text = str;
        d->doFind();
    }
}

void TypeAheadFindBar::findNext() { d->doFind(); }

void TypeAheadFindBar::findPrevious() { d->doFind(true); }

void TypeAheadFindBar::caseToggled() { d->caseSensitive = d->cb_case->checkState(); }