1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "chatviewsearchcontroller.h"
23 #include <QAbstractItemModel>
28 #include "chatlinemodel.h"
29 #include "chatscene.h"
30 #include "messagemodel.h"
32 ChatViewSearchController::ChatViewSearchController(QObject *parent)
38 void ChatViewSearchController::setSearchString(const QString &searchString)
40 QString oldSearchString = _searchString;
41 _searchString = searchString;
43 if (!searchString.startsWith(oldSearchString) || oldSearchString.isEmpty()) {
44 // we can't reuse our all findings... cler the scene and do it all over
49 updateHighlights(true);
55 void ChatViewSearchController::setScene(ChatScene *scene)
62 disconnect(_scene, nullptr, this, nullptr);
63 disconnect(Client::messageModel(), nullptr, this, nullptr);
64 qDeleteAll(_highlightItems);
65 _highlightItems.clear();
72 connect(_scene, &QObject::destroyed, this, &ChatViewSearchController::sceneDestroyed);
73 connect(_scene, &ChatScene::layoutChanged, this, [this]() { repositionHighlights(); });
74 connect(Client::messageModel(), &MessageModel::finishedBacklogFetch, this, [this]() { updateHighlights(); });
79 void ChatViewSearchController::highlightNext()
81 if (_highlightItems.isEmpty())
84 if (_currentHighlight < _highlightItems.count()) {
85 _highlightItems.at(_currentHighlight)->setHighlighted(false);
89 if (_currentHighlight >= _highlightItems.count())
90 _currentHighlight = 0;
91 _highlightItems.at(_currentHighlight)->setHighlighted(true);
92 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
96 void ChatViewSearchController::highlightPrev()
98 if (_highlightItems.isEmpty())
101 if (_currentHighlight < _highlightItems.count()) {
102 _highlightItems.at(_currentHighlight)->setHighlighted(false);
106 if (_currentHighlight < 0)
107 _currentHighlight = _highlightItems.count() - 1;
108 _highlightItems.at(_currentHighlight)->setHighlighted(true);
109 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
113 void ChatViewSearchController::updateHighlights(bool reuse)
119 QSet<ChatLine *> chatLines;
120 foreach(SearchHighlightItem *highlightItem, _highlightItems) {
121 auto *line = qgraphicsitem_cast<ChatLine *>(highlightItem->parentItem());
125 foreach(ChatLine *line, QList<ChatLine *>(chatLines.toList())) {
126 updateHighlights(line);
130 QPointF oldHighlightPos;
131 if (!_highlightItems.isEmpty() && _currentHighlight < _highlightItems.count()) {
132 oldHighlightPos = _highlightItems[_currentHighlight]->scenePos();
134 qDeleteAll(_highlightItems);
135 _highlightItems.clear();
136 Q_ASSERT(_highlightItems.isEmpty());
138 if (searchString().isEmpty() || !(_searchSenders || _searchMsgs))
141 checkMessagesForHighlight();
143 if (!_highlightItems.isEmpty()) {
144 if (!oldHighlightPos.isNull()) {
145 int start = 0; int end = _highlightItems.count() - 1;
149 startPos = _highlightItems[start]->scenePos();
150 endPos = _highlightItems[end]->scenePos();
151 if (startPos == oldHighlightPos) {
152 _currentHighlight = start;
155 if (endPos == oldHighlightPos) {
156 _currentHighlight = end;
159 if (end - start == 1) {
160 _currentHighlight = start;
163 if (end == 0 && start == 0) {
164 // Sometimes we can run into an issue where the start and end are both set
165 // to zero. Rather than endlessly spin this loop, bail out. Search seems
168 // Unfortunately, this seems specific to the contents of a buffer. First,
169 // find a buffer that you've encountered freezing, and keep track of what
170 // was loaded, where it was, and the two most recent search terms.
172 // 1. Load some backlog to a buffer
173 // 2. Search for term with any number of matches
174 // 3. Making sure to -type over existing words without first backspacing-,
175 // search for another term with only one match
176 // Expected: Search results found, no freezing
177 // Actual: Quassel hangs. startPos and endPos = same place, start = 0,
178 // end = 0, _currentHighlight appears to retain values from the
181 // Reset _currentHighlight to start, otherwise it'll retain the value from
182 // previous search, resulting in an index-out-of-bounds error.
183 _currentHighlight = start;
184 // Escape from the loop!
187 int pivot = (end + start) / 2;
188 QPointF pivotPos = _highlightItems[pivot]->scenePos();
189 if (startPos.y() == endPos.y()) {
190 if (oldHighlightPos.x() <= pivotPos.x())
196 if (oldHighlightPos.y() <= pivotPos.y())
204 _currentHighlight = _highlightItems.count() - 1;
206 _highlightItems[_currentHighlight]->setHighlighted(true);
207 emit newCurrentHighlight(_highlightItems[_currentHighlight]);
213 void ChatViewSearchController::checkMessagesForHighlight(int start, int end)
215 QAbstractItemModel *model = _scene->model();
219 end = model->rowCount() - 1;
225 for (int row = start; row <= end; row++) {
226 if (_searchOnlyRegularMsgs) {
227 index = model->index(row, 0);
228 if (!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
231 highlightLine(_scene->chatLine(row));
236 void ChatViewSearchController::updateHighlights(ChatLine *line)
238 QList<ChatItem *> checkItems;
240 checkItems << line->item(MessageModel::SenderColumn);
243 checkItems << line->item(MessageModel::ContentsColumn);
245 QHash<quint64, QHash<quint64, QRectF> > wordRects;
246 foreach(ChatItem *item, checkItems) {
247 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
248 wordRects[(quint64)(wordRect.x() + item->x())][(quint64)(wordRect.y())] = wordRect;
252 bool deleteAll = false;
253 QAbstractItemModel *model = _scene->model();
255 if (_searchOnlyRegularMsgs) {
256 QModelIndex index = model->index(line->row(), 0);
257 if (!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
261 foreach(QGraphicsItem *child, line->childItems()) {
262 auto *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
266 if (!deleteAll && wordRects.contains((quint64)(highlightItem->pos().x())) && wordRects[(quint64)(highlightItem->pos().x())].contains((quint64)(highlightItem->pos().y()))) {
267 QRectF &wordRect = wordRects[(quint64)(highlightItem->pos().x())][(quint64)(highlightItem->pos().y())];
268 highlightItem->updateGeometry(wordRect.width(), wordRect.height());
271 int pos = _highlightItems.indexOf(highlightItem);
272 if (pos == _currentHighlight) {
275 else if (pos < _currentHighlight) {
279 _highlightItems.removeAt(pos);
280 delete highlightItem;
286 void ChatViewSearchController::highlightLine(ChatLine *line)
288 QList<ChatItem *> checkItems;
290 checkItems << line->item(MessageModel::SenderColumn);
293 checkItems << line->item(MessageModel::ContentsColumn);
295 foreach(ChatItem *item, checkItems) {
296 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
297 _highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
303 void ChatViewSearchController::repositionHighlights()
305 QSet<ChatLine *> chatLines;
306 foreach(SearchHighlightItem *item, _highlightItems) {
307 auto *line = qgraphicsitem_cast<ChatLine *>(item->parentItem());
311 QList<ChatLine *> chatLineList(chatLines.toList());
312 foreach(ChatLine *line, chatLineList) {
313 repositionHighlights(line);
318 void ChatViewSearchController::repositionHighlights(ChatLine *line)
320 QList<SearchHighlightItem *> searchHighlights;
321 foreach(QGraphicsItem *child, line->childItems()) {
322 auto *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
324 searchHighlights << highlightItem;
327 if (searchHighlights.isEmpty())
330 QList<QPointF> wordPos;
331 if (_searchSenders) {
332 foreach(QRectF wordRect, line->senderItem()->findWords(searchString(), caseSensitive())) {
333 wordPos << QPointF(wordRect.x() + line->senderItem()->x(), wordRect.y());
337 foreach(QRectF wordRect, line->contentsItem()->findWords(searchString(), caseSensitive())) {
338 wordPos << QPointF(wordRect.x() + line->contentsItem()->x(), wordRect.y());
342 qSort(searchHighlights.begin(), searchHighlights.end(), SearchHighlightItem::firstInLine);
344 Q_ASSERT(wordPos.count() == searchHighlights.count());
345 for (int i = 0; i < searchHighlights.count(); i++) {
346 searchHighlights.at(i)->setPos(wordPos.at(i));
351 void ChatViewSearchController::sceneDestroyed()
353 // WARNING: don't call any methods on scene!
355 // the items will be automatically deleted when the scene is destroyed
356 // so we just have to clear the list;
357 _highlightItems.clear();
361 void ChatViewSearchController::setCaseSensitive(bool caseSensitive)
363 if (_caseSensitive == caseSensitive)
366 _caseSensitive = caseSensitive;
368 // we can reuse the original search results if the new search
369 // parameters are a restriction of the original one
370 updateHighlights(caseSensitive);
374 void ChatViewSearchController::setSearchSenders(bool searchSenders)
376 if (_searchSenders == searchSenders)
379 _searchSenders = searchSenders;
380 // we can reuse the original search results if the new search
381 // parameters are a restriction of the original one
382 updateHighlights(!searchSenders);
386 void ChatViewSearchController::setSearchMsgs(bool searchMsgs)
388 if (_searchMsgs == searchMsgs)
391 _searchMsgs = searchMsgs;
393 // we can reuse the original search results if the new search
394 // parameters are a restriction of the original one
395 updateHighlights(!searchMsgs);
399 void ChatViewSearchController::setSearchOnlyRegularMsgs(bool searchOnlyRegularMsgs)
401 if (_searchOnlyRegularMsgs == searchOnlyRegularMsgs)
404 _searchOnlyRegularMsgs = searchOnlyRegularMsgs;
406 // we can reuse the original search results if the new search
407 // parameters are a restriction of the original one
408 updateHighlights(searchOnlyRegularMsgs);
412 // ==================================================
413 // SearchHighlightItem
414 // ==================================================
415 SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
417 QGraphicsItem(parent),
422 setPos(wordRect.x(), wordRect.y());
423 updateGeometry(wordRect.width(), wordRect.height());
425 connect(&_timeLine, &QTimeLine::valueChanged, this, &SearchHighlightItem::updateHighlight);
429 void SearchHighlightItem::setHighlighted(bool highlighted)
431 _highlighted = highlighted;
434 _timeLine.setDirection(QTimeLine::Forward);
436 _timeLine.setDirection(QTimeLine::Backward);
438 if (_timeLine.state() != QTimeLine::Running)
445 void SearchHighlightItem::updateHighlight(qreal value)
447 _alpha = 70 + (int)(80 * value);
452 void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
457 painter->setPen(QPen(QColor(0, 0, 0), 1.5));
458 painter->setBrush(QColor(254, 237, 45, _alpha));
459 painter->setRenderHints(QPainter::Antialiasing);
460 qreal radius = boundingRect().height() * 0.30;
461 painter->drawRoundedRect(boundingRect(), radius, radius);
465 void SearchHighlightItem::updateGeometry(qreal width, qreal height)
467 prepareGeometryChange();
468 qreal sizedelta = height * 0.1;
469 _boundingRect = QRectF(-sizedelta, -sizedelta, width + 2 * sizedelta, height + 2 * sizedelta);
474 bool SearchHighlightItem::firstInLine(QGraphicsItem *item1, QGraphicsItem *item2)
476 if (item1->pos().y() != item2->pos().y())
477 return item1->pos().y() < item2->pos().y();
479 return item1->pos().x() < item2->pos().x();