1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qhelpsearchquerywidget.h" |
5 | |
6 | #include <QtCore/qabstractitemmodel.h> |
7 | #include <QtCore/qstringlist.h> |
8 | #include <QtGui/qevent.h> |
9 | #include <QtWidgets/qcompleter.h> |
10 | #include <QtWidgets/qlabel.h> |
11 | #include <QtWidgets/qlayout.h> |
12 | #include <QtWidgets/qlineedit.h> |
13 | #include <QtWidgets/qpushbutton.h> |
14 | #include <QtWidgets/qtoolbutton.h> |
15 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | class QHelpSearchQueryWidgetPrivate : public QObject |
19 | { |
20 | public: |
21 | struct QueryHistory { |
22 | explicit QueryHistory() : curQuery(-1) {} |
23 | QStringList queries; |
24 | int curQuery = 0; |
25 | }; |
26 | |
27 | class CompleterModel : public QAbstractListModel |
28 | { |
29 | public: |
30 | explicit CompleterModel(QObject *parent) : QAbstractListModel(parent) { } |
31 | |
32 | int rowCount(const QModelIndex &parent = QModelIndex()) const override |
33 | { |
34 | return parent.isValid() ? 0 : termList.size(); |
35 | } |
36 | |
37 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override |
38 | { |
39 | if (!index.isValid() || index.row() >= termList.size()|| |
40 | (role != Qt::EditRole && role != Qt::DisplayRole)) |
41 | return {}; |
42 | return termList.at(i: index.row()); |
43 | } |
44 | |
45 | void addTerm(const QString &term) |
46 | { |
47 | if (!termList.contains(str: term)) { |
48 | beginResetModel(); |
49 | termList.append(t: term); |
50 | endResetModel(); |
51 | } |
52 | } |
53 | |
54 | private: |
55 | QStringList termList; |
56 | }; |
57 | |
58 | QHelpSearchQueryWidgetPrivate() : m_searchCompleter(new CompleterModel(this), this) {} |
59 | |
60 | void retranslate() |
61 | { |
62 | m_searchLabel->setText(QHelpSearchQueryWidget::tr(s: "Search for:")); |
63 | m_searchButton->setText(QHelpSearchQueryWidget::tr(s: "Search")); |
64 | #if QT_CONFIG(tooltip) |
65 | m_prevQueryButton->setToolTip(QHelpSearchQueryWidget::tr(s: "Previous search")); |
66 | m_nextQueryButton->setToolTip(QHelpSearchQueryWidget::tr(s: "Next search")); |
67 | #endif |
68 | } |
69 | |
70 | void saveQuery(const QString &query) |
71 | { |
72 | // We only add the query to the list if it is different from the last one. |
73 | if (!m_queries.queries.isEmpty() && m_queries.queries.last() == query) |
74 | return; |
75 | |
76 | m_queries.queries.append(t: query); |
77 | static_cast<CompleterModel *>(m_searchCompleter.model())->addTerm(term: query); |
78 | } |
79 | |
80 | void nextOrPrevQuery(int maxOrMinIndex, int addend, QToolButton *thisButton, |
81 | QToolButton *otherButton) |
82 | { |
83 | m_lineEdit->clear(); |
84 | |
85 | // Otherwise, the respective button would be disabled. |
86 | Q_ASSERT(m_queries.curQuery != maxOrMinIndex); |
87 | |
88 | m_queries.curQuery = qBound(min: 0, val: m_queries.curQuery + addend, max: m_queries.queries.size() - 1); |
89 | const QString &query = m_queries.queries.at(i: m_queries.curQuery); |
90 | m_lineEdit->setText(query); |
91 | |
92 | if (m_queries.curQuery == maxOrMinIndex) |
93 | thisButton->setEnabled(false); |
94 | otherButton->setEnabled(true); |
95 | } |
96 | |
97 | void enableOrDisableToolButtons() |
98 | { |
99 | m_prevQueryButton->setEnabled(m_queries.curQuery > 0); |
100 | m_nextQueryButton->setEnabled(m_queries.curQuery < m_queries.queries.size() - 1); |
101 | } |
102 | |
103 | bool eventFilter(QObject *ob, QEvent *event) override |
104 | { |
105 | if (event->type() == QEvent::KeyPress) { |
106 | QKeyEvent *const keyEvent = static_cast<QKeyEvent *>(event); |
107 | if (keyEvent->key() == Qt::Key_Down) { |
108 | if (m_queries.curQuery + 1 < m_queries.queries.size()) |
109 | nextQuery(); |
110 | return true; |
111 | } |
112 | if (keyEvent->key() == Qt::Key_Up) { |
113 | if (m_queries.curQuery > 0) |
114 | prevQuery(); |
115 | return true; |
116 | } |
117 | } |
118 | return QObject::eventFilter(watched: ob, event); |
119 | } |
120 | |
121 | void searchRequested() |
122 | { |
123 | saveQuery(query: m_lineEdit->text()); |
124 | m_queries.curQuery = m_queries.queries.size() - 1; |
125 | if (m_queries.curQuery > 0) |
126 | m_prevQueryButton->setEnabled(true); |
127 | m_nextQueryButton->setEnabled(false); |
128 | } |
129 | |
130 | void nextQuery() |
131 | { |
132 | nextOrPrevQuery(maxOrMinIndex: m_queries.queries.size() - 1, addend: 1, thisButton: m_nextQueryButton, otherButton: m_prevQueryButton); |
133 | } |
134 | |
135 | void prevQuery() { nextOrPrevQuery(maxOrMinIndex: 0, addend: -1, thisButton: m_prevQueryButton, otherButton: m_nextQueryButton); } |
136 | |
137 | QLabel *m_searchLabel = nullptr; |
138 | QPushButton *m_searchButton = nullptr; |
139 | QLineEdit *m_lineEdit = nullptr; |
140 | QToolButton *m_nextQueryButton = nullptr; |
141 | QToolButton *m_prevQueryButton = nullptr; |
142 | QueryHistory m_queries; |
143 | QCompleter m_searchCompleter; |
144 | bool m_compactMode = false; |
145 | }; |
146 | |
147 | /*! |
148 | \class QHelpSearchQueryWidget |
149 | \since 4.4 |
150 | \inmodule QtHelp |
151 | \brief The QHelpSearchQueryWidget class provides a simple line edit or |
152 | an advanced widget to enable the user to input a search term in a |
153 | standardized input mask. |
154 | */ |
155 | |
156 | /*! |
157 | \fn void QHelpSearchQueryWidget::search() |
158 | |
159 | This signal is emitted when a the user has the search button invoked. |
160 | After receiving the signal you can ask the QHelpSearchQueryWidget for the |
161 | search input that you may pass to the QHelpSearchEngine::search() function. |
162 | */ |
163 | |
164 | /*! |
165 | Constructs a new search query widget with the given \a parent. |
166 | */ |
167 | QHelpSearchQueryWidget::QHelpSearchQueryWidget(QWidget *parent) |
168 | : QWidget(parent) |
169 | , d(new QHelpSearchQueryWidgetPrivate) |
170 | { |
171 | QVBoxLayout *vLayout = new QVBoxLayout(this); |
172 | vLayout->setContentsMargins({}); |
173 | |
174 | QHBoxLayout* hBoxLayout = new QHBoxLayout; |
175 | d->m_searchLabel = new QLabel(this); |
176 | d->m_lineEdit = new QLineEdit(this); |
177 | d->m_lineEdit->setClearButtonEnabled(true); |
178 | d->m_lineEdit->setCompleter(&d->m_searchCompleter); |
179 | d->m_lineEdit->installEventFilter(filterObj: d); |
180 | d->m_prevQueryButton = new QToolButton(this); |
181 | d->m_prevQueryButton->setArrowType(Qt::LeftArrow); |
182 | d->m_prevQueryButton->setEnabled(false); |
183 | d->m_nextQueryButton = new QToolButton(this); |
184 | d->m_nextQueryButton->setArrowType(Qt::RightArrow); |
185 | d->m_nextQueryButton->setEnabled(false); |
186 | d->m_searchButton = new QPushButton(this); |
187 | hBoxLayout->addWidget(d->m_searchLabel); |
188 | hBoxLayout->addWidget(d->m_lineEdit); |
189 | hBoxLayout->addWidget(d->m_prevQueryButton); |
190 | hBoxLayout->addWidget(d->m_nextQueryButton); |
191 | hBoxLayout->addWidget(d->m_searchButton); |
192 | |
193 | vLayout->addLayout(layout: hBoxLayout); |
194 | |
195 | connect(sender: d->m_prevQueryButton, signal: &QAbstractButton::clicked, context: this, slot: [this] { d->prevQuery(); }); |
196 | connect(sender: d->m_nextQueryButton, signal: &QAbstractButton::clicked, context: this, slot: [this] { d->nextQuery(); }); |
197 | connect(sender: d->m_searchButton, signal: &QAbstractButton::clicked, context: this, slot: &QHelpSearchQueryWidget::search); |
198 | connect(sender: d->m_lineEdit, signal: &QLineEdit::returnPressed, context: this, slot: &QHelpSearchQueryWidget::search); |
199 | |
200 | d->retranslate(); |
201 | connect(sender: this, signal: &QHelpSearchQueryWidget::search, context: this, slot: [this] { d->searchRequested(); }); |
202 | setCompactMode(true); |
203 | } |
204 | |
205 | /*! |
206 | Destroys the search query widget. |
207 | */ |
208 | QHelpSearchQueryWidget::~QHelpSearchQueryWidget() |
209 | { |
210 | delete d; |
211 | } |
212 | |
213 | /*! |
214 | Expands the search query widget so that the extended search fields are shown. |
215 | */ |
216 | void QHelpSearchQueryWidget::expandExtendedSearch() |
217 | { |
218 | // TODO: no extended search anymore, deprecate it? |
219 | } |
220 | |
221 | /*! |
222 | Collapses the search query widget so that only the default search field is |
223 | shown. |
224 | */ |
225 | void QHelpSearchQueryWidget::collapseExtendedSearch() |
226 | { |
227 | // TODO: no extended search anymore, deprecate it? |
228 | } |
229 | |
230 | #if QT_DEPRECATED_SINCE(5, 9) |
231 | QT_WARNING_PUSH |
232 | QT_WARNING_DISABLE_DEPRECATED |
233 | /*! |
234 | \deprecated |
235 | |
236 | Use searchInput() instead. |
237 | */ |
238 | QList<QHelpSearchQuery> QHelpSearchQueryWidget::query() const |
239 | { |
240 | return {{QHelpSearchQuery::DEFAULT, searchInput().split(sep: QChar::Space, behavior: Qt::SkipEmptyParts)}}; |
241 | } |
242 | |
243 | /*! |
244 | \deprecated |
245 | |
246 | Use setSearchInput() instead. |
247 | */ |
248 | void QHelpSearchQueryWidget::setQuery(const QList<QHelpSearchQuery> &queryList) |
249 | { |
250 | if (queryList.isEmpty()) |
251 | return; |
252 | |
253 | setSearchInput(queryList.first().wordList.join(sep: QChar::Space)); |
254 | } |
255 | QT_WARNING_POP |
256 | #endif // QT_DEPRECATED_SINCE(5, 9) |
257 | |
258 | /*! |
259 | \since 5.9 |
260 | |
261 | Returns a search phrase to use in combination with the |
262 | QHelpSearchEngine::search(const QString &searchInput) function. |
263 | */ |
264 | QString QHelpSearchQueryWidget::searchInput() const |
265 | { |
266 | if (d->m_queries.queries.isEmpty()) |
267 | return {}; |
268 | return d->m_queries.queries.last(); |
269 | } |
270 | |
271 | /*! |
272 | \since 5.9 |
273 | |
274 | Sets the QHelpSearchQueryWidget input field to the value specified by |
275 | \a searchInput. |
276 | |
277 | \note The QHelpSearchEngine::search(const QString &searchInput) function has |
278 | to be called to perform the actual search. |
279 | */ |
280 | void QHelpSearchQueryWidget::setSearchInput(const QString &searchInput) |
281 | { |
282 | d->m_lineEdit->clear(); |
283 | d->m_lineEdit->setText(searchInput); |
284 | d->searchRequested(); |
285 | } |
286 | |
287 | bool QHelpSearchQueryWidget::isCompactMode() const |
288 | { |
289 | return d->m_compactMode; |
290 | } |
291 | |
292 | void QHelpSearchQueryWidget::setCompactMode(bool on) |
293 | { |
294 | if (d->m_compactMode != on) { |
295 | d->m_compactMode = on; |
296 | d->m_prevQueryButton->setVisible(!on); |
297 | d->m_nextQueryButton->setVisible(!on); |
298 | d->m_searchLabel->setVisible(!on); |
299 | } |
300 | } |
301 | |
302 | /*! |
303 | \reimp |
304 | */ |
305 | void QHelpSearchQueryWidget::focusInEvent(QFocusEvent *focusEvent) |
306 | { |
307 | if (focusEvent->reason() != Qt::MouseFocusReason) { |
308 | d->m_lineEdit->selectAll(); |
309 | d->m_lineEdit->setFocus(); |
310 | } |
311 | } |
312 | |
313 | /*! |
314 | \reimp |
315 | */ |
316 | void QHelpSearchQueryWidget::changeEvent(QEvent *event) |
317 | { |
318 | if (event->type() == QEvent::LanguageChange) |
319 | d->retranslate(); |
320 | else |
321 | QWidget::changeEvent(event); |
322 | } |
323 | |
324 | QT_END_NAMESPACE |
325 |
Definitions
- QHelpSearchQueryWidgetPrivate
- QueryHistory
- QueryHistory
- CompleterModel
- CompleterModel
- rowCount
- data
- addTerm
- QHelpSearchQueryWidgetPrivate
- retranslate
- saveQuery
- nextOrPrevQuery
- enableOrDisableToolButtons
- eventFilter
- searchRequested
- nextQuery
- prevQuery
- QHelpSearchQueryWidget
- ~QHelpSearchQueryWidget
- expandExtendedSearch
- collapseExtendedSearch
- query
- setQuery
- searchInput
- setSearchInput
- isCompactMode
- setCompactMode
- focusInEvent
Learn Advanced QML with KDAB
Find out more