1 /***************************************************************************
2 * Copyright (C) 2005-2014 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)
36 _caseSensitive(false),
37 _searchSenders(false),
39 _searchOnlyRegularMsgs(true)
44 void ChatViewSearchController::setSearchString(const QString &searchString)
46 QString oldSearchString = _searchString;
47 _searchString = searchString;
49 if (!searchString.startsWith(oldSearchString) || oldSearchString.isEmpty()) {
50 // we can't reuse our all findings... cler the scene and do it all over
55 updateHighlights(true);
61 void ChatViewSearchController::setScene(ChatScene *scene)
68 disconnect(_scene, 0, this, 0);
69 disconnect(Client::messageModel(), 0, this, 0);
70 qDeleteAll(_highlightItems);
71 _highlightItems.clear();
78 connect(_scene, SIGNAL(destroyed()), this, SLOT(sceneDestroyed()));
79 connect(_scene, SIGNAL(layoutChanged()), this, SLOT(repositionHighlights()));
80 connect(Client::messageModel(), SIGNAL(finishedBacklogFetch(BufferId)), this, SLOT(updateHighlights()));
85 void ChatViewSearchController::highlightNext()
87 if (_highlightItems.isEmpty())
90 if (_currentHighlight < _highlightItems.count()) {
91 _highlightItems.at(_currentHighlight)->setHighlighted(false);
95 if (_currentHighlight >= _highlightItems.count())
96 _currentHighlight = 0;
97 _highlightItems.at(_currentHighlight)->setHighlighted(true);
98 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
102 void ChatViewSearchController::highlightPrev()
104 if (_highlightItems.isEmpty())
107 if (_currentHighlight < _highlightItems.count()) {
108 _highlightItems.at(_currentHighlight)->setHighlighted(false);
112 if (_currentHighlight < 0)
113 _currentHighlight = _highlightItems.count() - 1;
114 _highlightItems.at(_currentHighlight)->setHighlighted(true);
115 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
119 void ChatViewSearchController::updateHighlights(bool reuse)
125 QSet<ChatLine *> chatLines;
126 foreach(SearchHighlightItem *highlightItem, _highlightItems) {
127 ChatLine *line = qgraphicsitem_cast<ChatLine *>(highlightItem->parentItem());
131 foreach(ChatLine *line, QList<ChatLine *>(chatLines.toList())) {
132 updateHighlights(line);
136 QPointF oldHighlightPos;
137 if (!_highlightItems.isEmpty() && _currentHighlight < _highlightItems.count()) {
138 oldHighlightPos = _highlightItems[_currentHighlight]->scenePos();
140 qDeleteAll(_highlightItems);
141 _highlightItems.clear();
142 Q_ASSERT(_highlightItems.isEmpty());
144 if (searchString().isEmpty() || !(_searchSenders || _searchMsgs))
147 checkMessagesForHighlight();
149 if (!_highlightItems.isEmpty()) {
150 if (!oldHighlightPos.isNull()) {
151 int start = 0; int end = _highlightItems.count() - 1;
155 startPos = _highlightItems[start]->scenePos();
156 endPos = _highlightItems[end]->scenePos();
157 if (startPos == oldHighlightPos) {
158 _currentHighlight = start;
161 if (endPos == oldHighlightPos) {
162 _currentHighlight = end;
165 if (end - start == 1) {
166 _currentHighlight = start;
169 int pivot = (end + start) / 2;
170 QPointF pivotPos = _highlightItems[pivot]->scenePos();
171 if (startPos.y() == endPos.y()) {
172 if (oldHighlightPos.x() <= pivotPos.x())
178 if (oldHighlightPos.y() <= pivotPos.y())
186 _currentHighlight = _highlightItems.count() - 1;
188 _highlightItems[_currentHighlight]->setHighlighted(true);
189 emit newCurrentHighlight(_highlightItems[_currentHighlight]);
195 void ChatViewSearchController::checkMessagesForHighlight(int start, int end)
197 QAbstractItemModel *model = _scene->model();
201 end = model->rowCount() - 1;
207 for (int row = start; row <= end; row++) {
208 if (_searchOnlyRegularMsgs) {
209 index = model->index(row, 0);
210 if (!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
213 highlightLine(_scene->chatLine(row));
218 void ChatViewSearchController::updateHighlights(ChatLine *line)
220 QList<ChatItem *> checkItems;
222 checkItems << line->item(MessageModel::SenderColumn);
225 checkItems << line->item(MessageModel::ContentsColumn);
227 QHash<quint64, QHash<quint64, QRectF> > wordRects;
228 foreach(ChatItem *item, checkItems) {
229 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
230 wordRects[(quint64)(wordRect.x() + item->x())][(quint64)(wordRect.y())] = wordRect;
234 bool deleteAll = false;
235 QAbstractItemModel *model = _scene->model();
237 if (_searchOnlyRegularMsgs) {
238 QModelIndex index = model->index(line->row(), 0);
239 if (!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
243 foreach(QGraphicsItem *child, line->childItems()) {
244 SearchHighlightItem *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
248 if (!deleteAll && wordRects.contains((quint64)(highlightItem->pos().x())) && wordRects[(quint64)(highlightItem->pos().x())].contains((quint64)(highlightItem->pos().y()))) {
249 QRectF &wordRect = wordRects[(quint64)(highlightItem->pos().x())][(quint64)(highlightItem->pos().y())];
250 highlightItem->updateGeometry(wordRect.width(), wordRect.height());
253 int pos = _highlightItems.indexOf(highlightItem);
254 if (pos == _currentHighlight) {
257 else if (pos < _currentHighlight) {
261 _highlightItems.removeAt(pos);
262 delete highlightItem;
268 void ChatViewSearchController::highlightLine(ChatLine *line)
270 QList<ChatItem *> checkItems;
272 checkItems << line->item(MessageModel::SenderColumn);
275 checkItems << line->item(MessageModel::ContentsColumn);
277 foreach(ChatItem *item, checkItems) {
278 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
279 _highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
285 void ChatViewSearchController::repositionHighlights()
287 QSet<ChatLine *> chatLines;
288 foreach(SearchHighlightItem *item, _highlightItems) {
289 ChatLine *line = qgraphicsitem_cast<ChatLine *>(item->parentItem());
293 QList<ChatLine *> chatLineList(chatLines.toList());
294 foreach(ChatLine *line, chatLineList) {
295 repositionHighlights(line);
300 void ChatViewSearchController::repositionHighlights(ChatLine *line)
302 QList<SearchHighlightItem *> searchHighlights;
303 foreach(QGraphicsItem *child, line->childItems()) {
304 SearchHighlightItem *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
306 searchHighlights << highlightItem;
309 if (searchHighlights.isEmpty())
312 QList<QPointF> wordPos;
313 if (_searchSenders) {
314 foreach(QRectF wordRect, line->senderItem()->findWords(searchString(), caseSensitive())) {
315 wordPos << QPointF(wordRect.x() + line->senderItem()->x(), wordRect.y());
319 foreach(QRectF wordRect, line->contentsItem()->findWords(searchString(), caseSensitive())) {
320 wordPos << QPointF(wordRect.x() + line->contentsItem()->x(), wordRect.y());
324 qSort(searchHighlights.begin(), searchHighlights.end(), SearchHighlightItem::firstInLine);
326 Q_ASSERT(wordPos.count() == searchHighlights.count());
327 for (int i = 0; i < searchHighlights.count(); i++) {
328 searchHighlights.at(i)->setPos(wordPos.at(i));
333 void ChatViewSearchController::sceneDestroyed()
335 // WARNING: don't call any methods on scene!
337 // the items will be automatically deleted when the scene is destroyed
338 // so we just have to clear the list;
339 _highlightItems.clear();
343 void ChatViewSearchController::setCaseSensitive(bool caseSensitive)
345 if (_caseSensitive == caseSensitive)
348 _caseSensitive = caseSensitive;
350 // we can reuse the original search results if the new search
351 // parameters are a restriction of the original one
352 updateHighlights(caseSensitive);
356 void ChatViewSearchController::setSearchSenders(bool searchSenders)
358 if (_searchSenders == searchSenders)
361 _searchSenders = searchSenders;
362 // we can reuse the original search results if the new search
363 // parameters are a restriction of the original one
364 updateHighlights(!searchSenders);
368 void ChatViewSearchController::setSearchMsgs(bool searchMsgs)
370 if (_searchMsgs == searchMsgs)
373 _searchMsgs = searchMsgs;
375 // we can reuse the original search results if the new search
376 // parameters are a restriction of the original one
377 updateHighlights(!searchMsgs);
381 void ChatViewSearchController::setSearchOnlyRegularMsgs(bool searchOnlyRegularMsgs)
383 if (_searchOnlyRegularMsgs == searchOnlyRegularMsgs)
386 _searchOnlyRegularMsgs = searchOnlyRegularMsgs;
388 // we can reuse the original search results if the new search
389 // parameters are a restriction of the original one
390 updateHighlights(searchOnlyRegularMsgs);
394 // ==================================================
395 // SearchHighlightItem
396 // ==================================================
397 SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
399 QGraphicsItem(parent),
404 setPos(wordRect.x(), wordRect.y());
405 updateGeometry(wordRect.width(), wordRect.height());
407 connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(updateHighlight(qreal)));
411 void SearchHighlightItem::setHighlighted(bool highlighted)
413 _highlighted = highlighted;
416 _timeLine.setDirection(QTimeLine::Forward);
418 _timeLine.setDirection(QTimeLine::Backward);
420 if (_timeLine.state() != QTimeLine::Running)
427 void SearchHighlightItem::updateHighlight(qreal value)
429 _alpha = 70 + (int)(80 * value);
434 void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
439 painter->setPen(QPen(QColor(0, 0, 0), 1.5));
440 painter->setBrush(QColor(254, 237, 45, _alpha));
441 painter->setRenderHints(QPainter::Antialiasing);
442 qreal radius = boundingRect().height() * 0.30;
443 painter->drawRoundedRect(boundingRect(), radius, radius);
447 void SearchHighlightItem::updateGeometry(qreal width, qreal height)
449 prepareGeometryChange();
450 qreal sizedelta = height * 0.1;
451 _boundingRect = QRectF(-sizedelta, -sizedelta, width + 2 * sizedelta, height + 2 * sizedelta);
456 bool SearchHighlightItem::firstInLine(QGraphicsItem *item1, QGraphicsItem *item2)
458 if (item1->pos().y() != item2->pos().y())
459 return item1->pos().y() < item2->pos().y();
461 return item1->pos().x() < item2->pos().x();