1 /***************************************************************************
2 * Copyright (C) 2005-2010 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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)
43 void ChatViewSearchController::setSearchString(const QString &searchString) {
44 QString oldSearchString = _searchString;
45 _searchString = searchString;
47 if(!searchString.startsWith(oldSearchString) || oldSearchString.isEmpty()) {
48 // we can't reuse our all findings... cler the scene and do it all over
52 updateHighlights(true);
57 void ChatViewSearchController::setScene(ChatScene *scene) {
63 disconnect(_scene, 0, this, 0);
64 qDeleteAll(_highlightItems);
65 _highlightItems.clear();
72 connect(_scene, SIGNAL(destroyed()), this, SLOT(sceneDestroyed()));
73 connect(_scene, SIGNAL(layoutChanged()), this, SLOT(repositionHighlights()));
77 void ChatViewSearchController::highlightNext() {
78 if(_highlightItems.isEmpty())
81 if(_currentHighlight < _highlightItems.count()) {
82 _highlightItems.at(_currentHighlight)->setHighlighted(false);
86 if(_currentHighlight >= _highlightItems.count())
87 _currentHighlight = 0;
88 _highlightItems.at(_currentHighlight)->setHighlighted(true);
89 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
92 void ChatViewSearchController::highlightPrev() {
93 if(_highlightItems.isEmpty())
96 if(_currentHighlight < _highlightItems.count()) {
97 _highlightItems.at(_currentHighlight)->setHighlighted(false);
101 if(_currentHighlight < 0)
102 _currentHighlight = _highlightItems.count() - 1;
103 _highlightItems.at(_currentHighlight)->setHighlighted(true);
104 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
107 void ChatViewSearchController::updateHighlights(bool reuse) {
112 QSet<ChatLine *> chatLines;
113 foreach(SearchHighlightItem *highlightItem, _highlightItems) {
114 ChatLine *line = qgraphicsitem_cast<ChatLine *>(highlightItem->parentItem());
118 foreach(ChatLine *line, QList<ChatLine *>(chatLines.toList())) {
119 updateHighlights(line);
122 QPointF oldHighlightPos;
123 if(!_highlightItems.isEmpty() && _currentHighlight < _highlightItems.count()) {
124 oldHighlightPos = _highlightItems[_currentHighlight]->scenePos();
126 qDeleteAll(_highlightItems);
127 _highlightItems.clear();
128 Q_ASSERT(_highlightItems.isEmpty());
130 if(searchString().isEmpty() || !(_searchSenders || _searchMsgs))
133 checkMessagesForHighlight();
135 if(!_highlightItems.isEmpty()) {
136 if(!oldHighlightPos.isNull()) {
137 int start = 0; int end = _highlightItems.count() - 1;
141 startPos = _highlightItems[start]->scenePos();
142 endPos = _highlightItems[end]->scenePos();
143 if(startPos == oldHighlightPos) {
144 _currentHighlight = start;
147 if(endPos == oldHighlightPos) {
148 _currentHighlight = end;
151 if(end - start == 1) {
152 _currentHighlight = start;
155 int pivot = (end + start) / 2;
156 QPointF pivotPos = _highlightItems[pivot]->scenePos();
157 if(startPos.y() == endPos.y()) {
158 if(oldHighlightPos.x() <= pivotPos.x())
163 if(oldHighlightPos.y() <= pivotPos.y())
170 _currentHighlight = _highlightItems.count() - 1;
172 _highlightItems[_currentHighlight]->setHighlighted(true);
173 emit newCurrentHighlight(_highlightItems[_currentHighlight]);
178 void ChatViewSearchController::checkMessagesForHighlight(int start, int end) {
179 QAbstractItemModel *model = _scene->model();
183 end = model->rowCount() - 1;
189 for(int row = start; row <= end; row++) {
190 if(_searchOnlyRegularMsgs) {
191 index = model->index(row, 0);
192 if(!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
195 highlightLine(_scene->chatLine(row));
199 void ChatViewSearchController::updateHighlights(ChatLine *line) {
200 QList<ChatItem *> checkItems;
202 checkItems << line->item(MessageModel::SenderColumn);
205 checkItems << line->item(MessageModel::ContentsColumn);
207 QHash<quint64, QHash<quint64, QRectF> > wordRects;
208 foreach(ChatItem *item, checkItems) {
209 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
210 wordRects[(quint64)(wordRect.x() + item->x())][(quint64)(wordRect.y())] = wordRect;
214 bool deleteAll = false;
215 QAbstractItemModel *model = _scene->model();
217 if(_searchOnlyRegularMsgs) {
218 QModelIndex index = model->index(line->row(), 0);
219 if(!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
224 foreach(QGraphicsItem *child, line->childItems()) {
225 SearchHighlightItem *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
229 if(!deleteAll && wordRects.contains((quint64)(highlightItem->pos().x())) && wordRects[(quint64)(highlightItem->pos().x())].contains((quint64)(highlightItem->pos().y()))) {
230 QRectF &wordRect = wordRects[(quint64)(highlightItem->pos().x())][(quint64)(highlightItem->pos().y())];
231 highlightItem->updateGeometry(wordRect.width(), wordRect.height());
233 int pos = _highlightItems.indexOf(highlightItem);
234 if(pos == _currentHighlight) {
236 } else if (pos < _currentHighlight) {
240 _highlightItems.removeAt(pos);
241 delete highlightItem;
246 void ChatViewSearchController::highlightLine(ChatLine *line) {
247 QList<ChatItem *> checkItems;
249 checkItems << line->item(MessageModel::SenderColumn);
252 checkItems << line->item(MessageModel::ContentsColumn);
254 foreach(ChatItem *item, checkItems) {
255 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
256 _highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
261 void ChatViewSearchController::repositionHighlights() {
262 QSet<ChatLine *> chatLines;
263 foreach(SearchHighlightItem *item, _highlightItems) {
264 ChatLine *line = qgraphicsitem_cast<ChatLine *>(item->parentItem());
268 QList<ChatLine *> chatLineList(chatLines.toList());
269 foreach(ChatLine *line, chatLineList) {
270 repositionHighlights(line);
274 void ChatViewSearchController::repositionHighlights(ChatLine *line) {
275 QList<SearchHighlightItem *> searchHighlights;
276 foreach(QGraphicsItem *child, line->childItems()) {
277 SearchHighlightItem *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
279 searchHighlights << highlightItem;
282 if(searchHighlights.isEmpty())
285 QList<QPointF> wordPos;
287 foreach(QRectF wordRect, line->senderItem()->findWords(searchString(), caseSensitive())) {
288 wordPos << QPointF(wordRect.x() + line->senderItem()->x(), wordRect.y());
292 foreach(QRectF wordRect, line->contentsItem()->findWords(searchString(), caseSensitive())) {
293 wordPos << QPointF(wordRect.x() + line->contentsItem()->x(), wordRect.y());
297 qSort(searchHighlights.begin(), searchHighlights.end(), SearchHighlightItem::firstInLine);
299 Q_ASSERT(wordPos.count() == searchHighlights.count());
300 for(int i = 0; i < searchHighlights.count(); i++) {
301 searchHighlights.at(i)->setPos(wordPos.at(i));
305 void ChatViewSearchController::sceneDestroyed() {
306 // WARNING: don't call any methods on scene!
308 // the items will be automatically deleted when the scene is destroyed
309 // so we just have to clear the list;
310 _highlightItems.clear();
313 void ChatViewSearchController::setCaseSensitive(bool caseSensitive) {
314 if(_caseSensitive == caseSensitive)
317 _caseSensitive = caseSensitive;
319 // we can reuse the original search results if the new search
320 // parameters are a restriction of the original one
321 updateHighlights(caseSensitive);
324 void ChatViewSearchController::setSearchSenders(bool searchSenders) {
325 if(_searchSenders == searchSenders)
328 _searchSenders = searchSenders;
329 // we can reuse the original search results if the new search
330 // parameters are a restriction of the original one
331 updateHighlights(!searchSenders);
334 void ChatViewSearchController::setSearchMsgs(bool searchMsgs) {
335 if(_searchMsgs == searchMsgs)
338 _searchMsgs = searchMsgs;
340 // we can reuse the original search results if the new search
341 // parameters are a restriction of the original one
342 updateHighlights(!searchMsgs);
345 void ChatViewSearchController::setSearchOnlyRegularMsgs(bool searchOnlyRegularMsgs) {
346 if(_searchOnlyRegularMsgs == searchOnlyRegularMsgs)
349 _searchOnlyRegularMsgs = searchOnlyRegularMsgs;
351 // we can reuse the original search results if the new search
352 // parameters are a restriction of the original one
353 updateHighlights(searchOnlyRegularMsgs);
357 // ==================================================
358 // SearchHighlightItem
359 // ==================================================
360 SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
362 QGraphicsItem(parent),
367 setPos(wordRect.x(), wordRect.y());
368 updateGeometry(wordRect.width(), wordRect.height());
370 connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(updateHighlight(qreal)));
373 void SearchHighlightItem::setHighlighted(bool highlighted) {
374 _highlighted = highlighted;
377 _timeLine.setDirection(QTimeLine::Forward);
379 _timeLine.setDirection(QTimeLine::Backward);
381 if(_timeLine.state() != QTimeLine::Running)
387 void SearchHighlightItem::updateHighlight(qreal value) {
388 _alpha = 70 + (int)(80 * value);
392 void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
396 painter->setPen(QPen(QColor(0, 0, 0), 1.5));
397 painter->setBrush(QColor(254, 237, 45, _alpha));
398 painter->setRenderHints(QPainter::Antialiasing);
399 qreal radius = boundingRect().height() * 0.30;
400 painter->drawRoundedRect(boundingRect(), radius, radius);
403 void SearchHighlightItem::updateGeometry(qreal width, qreal height) {
404 prepareGeometryChange();
405 qreal sizedelta = height * 0.1;
406 _boundingRect = QRectF(-sizedelta, -sizedelta, width + 2 * sizedelta, height + 2 * sizedelta);
410 bool SearchHighlightItem::firstInLine(QGraphicsItem *item1, QGraphicsItem *item2) {
411 if(item1->pos().y() != item2->pos().y())
412 return item1->pos().y() < item2->pos().y();
414 return item1->pos().x() < item2->pos().x();