1 /***************************************************************************
2 * Copyright (C) 2005-09 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>
27 #include "chatlinemodel.h"
28 #include "chatscene.h"
29 #include "messagemodel.h"
31 ChatViewSearchController::ChatViewSearchController(QObject *parent)
35 _caseSensitive(false),
36 _searchSenders(false),
38 _searchOnlyRegularMsgs(true)
42 void ChatViewSearchController::setSearchString(const QString &searchString) {
43 QString oldSearchString = _searchString;
44 _searchString = searchString;
46 if(!searchString.startsWith(oldSearchString) || oldSearchString.isEmpty()) {
47 // we can't reuse our all findings... cler the scene and do it all over
51 updateHighlights(true);
56 void ChatViewSearchController::setScene(ChatScene *scene) {
62 disconnect(_scene, 0, this, 0);
63 qDeleteAll(_highlightItems);
64 _highlightItems.clear();
71 connect(_scene, SIGNAL(destroyed()), this, SLOT(sceneDestroyed()));
72 connect(_scene, SIGNAL(layoutChanged()), this, SLOT(repositionHighlights()));
76 void ChatViewSearchController::highlightNext() {
77 if(_highlightItems.isEmpty())
80 if(_currentHighlight < _highlightItems.count()) {
81 _highlightItems.at(_currentHighlight)->setHighlighted(false);
85 if(_currentHighlight >= _highlightItems.count())
86 _currentHighlight = 0;
87 _highlightItems.at(_currentHighlight)->setHighlighted(true);
88 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
91 void ChatViewSearchController::highlightPrev() {
92 if(_highlightItems.isEmpty())
95 if(_currentHighlight < _highlightItems.count()) {
96 _highlightItems.at(_currentHighlight)->setHighlighted(false);
100 if(_currentHighlight < 0)
101 _currentHighlight = _highlightItems.count() - 1;
102 _highlightItems.at(_currentHighlight)->setHighlighted(true);
103 emit newCurrentHighlight(_highlightItems.at(_currentHighlight));
106 void ChatViewSearchController::updateHighlights(bool reuse) {
111 QSet<ChatLine *> chatLines;
112 foreach(SearchHighlightItem *highlightItem, _highlightItems) {
113 ChatLine *line = qgraphicsitem_cast<ChatLine *>(highlightItem->parentItem());
117 foreach(ChatLine *line, QList<ChatLine *>(chatLines.toList())) {
118 updateHighlights(line);
121 QPointF oldHighlightPos;
122 if(!_highlightItems.isEmpty() && _currentHighlight < _highlightItems.count()) {
123 oldHighlightPos = _highlightItems[_currentHighlight]->scenePos();
125 qDeleteAll(_highlightItems);
126 _highlightItems.clear();
127 Q_ASSERT(_highlightItems.isEmpty());
129 if(searchString().isEmpty() || !(_searchSenders || _searchMsgs))
132 checkMessagesForHighlight();
134 if(!_highlightItems.isEmpty()) {
135 if(!oldHighlightPos.isNull()) {
136 int start = 0; int end = _highlightItems.count() - 1;
140 startPos = _highlightItems[start]->scenePos();
141 endPos = _highlightItems[end]->scenePos();
142 if(startPos == oldHighlightPos) {
143 _currentHighlight = start;
146 if(endPos == oldHighlightPos) {
147 _currentHighlight = end;
150 if(end - start == 1) {
151 _currentHighlight = start;
154 int pivot = (end + start) / 2;
155 QPointF pivotPos = _highlightItems[pivot]->scenePos();
156 if(startPos.y() == endPos.y()) {
157 if(oldHighlightPos.x() <= pivotPos.x())
162 if(oldHighlightPos.y() <= pivotPos.y())
169 _currentHighlight = _highlightItems.count() - 1;
171 _highlightItems[_currentHighlight]->setHighlighted(true);
172 emit newCurrentHighlight(_highlightItems[_currentHighlight]);
177 void ChatViewSearchController::checkMessagesForHighlight(int start, int end) {
178 QAbstractItemModel *model = _scene->model();
182 end = model->rowCount() - 1;
188 for(int row = start; row <= end; row++) {
189 if(_searchOnlyRegularMsgs) {
190 index = model->index(row, 0);
191 if(!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
194 highlightLine(_scene->chatLine(row));
198 void ChatViewSearchController::updateHighlights(ChatLine *line) {
199 QList<ChatItem *> checkItems;
201 checkItems << &(line->item(MessageModel::SenderColumn));
204 checkItems << &(line->item(MessageModel::ContentsColumn));
206 QHash<quint64, QHash<quint64, QRectF> > wordRects;
207 foreach(ChatItem *item, checkItems) {
208 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
209 wordRects[(quint64)(wordRect.x() + item->x())][(quint64)(wordRect.y())] = wordRect;
213 bool deleteAll = false;
214 QAbstractItemModel *model = _scene->model();
216 if(_searchOnlyRegularMsgs) {
217 QModelIndex index = model->index(line->row(), 0);
218 if(!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
223 foreach(QGraphicsItem *child, line->childItems()) {
224 SearchHighlightItem *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
228 if(!deleteAll && wordRects.contains((quint64)(highlightItem->pos().x())) && wordRects[(quint64)(highlightItem->pos().x())].contains((quint64)(highlightItem->pos().y()))) {
229 QRectF &wordRect = wordRects[(quint64)(highlightItem->pos().x())][(quint64)(highlightItem->pos().y())];
230 highlightItem->updateGeometry(wordRect.width(), wordRect.height());
232 int pos = _highlightItems.indexOf(highlightItem);
233 if(pos == _currentHighlight) {
235 } else if (pos < _currentHighlight) {
239 _highlightItems.removeAt(pos);
240 delete highlightItem;
245 void ChatViewSearchController::highlightLine(ChatLine *line) {
246 QList<ChatItem *> checkItems;
248 checkItems << &(line->item(MessageModel::SenderColumn));
251 checkItems << &(line->item(MessageModel::ContentsColumn));
253 foreach(ChatItem *item, checkItems) {
254 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
255 _highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
260 void ChatViewSearchController::repositionHighlights() {
261 QSet<ChatLine *> chatLines;
262 foreach(SearchHighlightItem *item, _highlightItems) {
263 ChatLine *line = qgraphicsitem_cast<ChatLine *>(item->parentItem());
267 QList<ChatLine *> chatLineList(chatLines.toList());
268 foreach(ChatLine *line, chatLineList) {
269 repositionHighlights(line);
273 void ChatViewSearchController::repositionHighlights(ChatLine *line) {
274 QList<SearchHighlightItem *> searchHighlights;
275 foreach(QGraphicsItem *child, line->childItems()) {
276 SearchHighlightItem *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
278 searchHighlights << highlightItem;
281 if(searchHighlights.isEmpty())
284 QList<QPointF> wordPos;
286 foreach(QRectF wordRect, line->senderItem().findWords(searchString(), caseSensitive())) {
287 wordPos << QPointF(wordRect.x() + line->senderItem().x(), wordRect.y());
291 foreach(QRectF wordRect, line->contentsItem().findWords(searchString(), caseSensitive())) {
292 wordPos << QPointF(wordRect.x() + line->contentsItem().x(), wordRect.y());
296 qSort(searchHighlights.begin(), searchHighlights.end(), SearchHighlightItem::firstInLine);
298 Q_ASSERT(wordPos.count() == searchHighlights.count());
299 for(int i = 0; i < searchHighlights.count(); i++) {
300 searchHighlights.at(i)->setPos(wordPos.at(i));
304 void ChatViewSearchController::sceneDestroyed() {
305 // WARNING: don't call any methods on scene!
307 // the items will be automatically deleted when the scene is destroyed
308 // so we just have to clear the list;
309 _highlightItems.clear();
312 void ChatViewSearchController::setCaseSensitive(bool caseSensitive) {
313 if(_caseSensitive == caseSensitive)
316 _caseSensitive = caseSensitive;
318 // we can reuse the original search results if the new search
319 // parameters are a restriction of the original one
320 updateHighlights(caseSensitive);
323 void ChatViewSearchController::setSearchSenders(bool searchSenders) {
324 if(_searchSenders == searchSenders)
327 _searchSenders = searchSenders;
328 // we can reuse the original search results if the new search
329 // parameters are a restriction of the original one
330 updateHighlights(!searchSenders);
333 void ChatViewSearchController::setSearchMsgs(bool searchMsgs) {
334 if(_searchMsgs == searchMsgs)
337 _searchMsgs = searchMsgs;
339 // we can reuse the original search results if the new search
340 // parameters are a restriction of the original one
341 updateHighlights(!searchMsgs);
344 void ChatViewSearchController::setSearchOnlyRegularMsgs(bool searchOnlyRegularMsgs) {
345 if(_searchOnlyRegularMsgs == searchOnlyRegularMsgs)
348 _searchOnlyRegularMsgs = searchOnlyRegularMsgs;
350 // we can reuse the original search results if the new search
351 // parameters are a restriction of the original one
352 updateHighlights(searchOnlyRegularMsgs);
356 // ==================================================
357 // SearchHighlightItem
358 // ==================================================
359 SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
361 QGraphicsItem(parent),
366 setPos(wordRect.x(), wordRect.y());
367 updateGeometry(wordRect.width(), wordRect.height());
369 connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(updateHighlight(qreal)));
372 void SearchHighlightItem::setHighlighted(bool highlighted) {
373 _highlighted = highlighted;
376 _timeLine.setDirection(QTimeLine::Forward);
378 _timeLine.setDirection(QTimeLine::Backward);
380 if(_timeLine.state() != QTimeLine::Running)
386 void SearchHighlightItem::updateHighlight(qreal value) {
387 _alpha = 100 + (int)(155 * value);
391 void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
395 painter->setPen(QPen(QColor(0, 0, 0, _alpha), 1.5));
396 painter->setBrush(QColor(254, 237, 45, _alpha));
397 painter->setRenderHints(QPainter::Antialiasing);
398 qreal radius = boundingRect().height() * 0.30;
399 painter->drawRoundedRect(boundingRect(), radius, radius);
402 void SearchHighlightItem::updateGeometry(qreal width, qreal height) {
403 prepareGeometryChange();
404 qreal sizedelta = height * 0.1;
405 _boundingRect = QRectF(-sizedelta, -sizedelta, width + 2 * sizedelta, height + 2 * sizedelta);
409 bool SearchHighlightItem::firstInLine(QGraphicsItem *item1, QGraphicsItem *item2) {
410 if(item1->pos().y() != item2->pos().y())
411 return item1->pos().y() < item2->pos().y();
413 return item1->pos().x() < item2->pos().x();