1 /***************************************************************************
2 * Copyright (C) 2005-08 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) {
110 QAbstractItemModel *model = _scene->model();
114 QList<ChatLine *> chatLines;
116 foreach(SearchHighlightItem *highlightItem, _highlightItems) {
117 ChatLine *line = dynamic_cast<ChatLine *>(highlightItem->parentItem());
118 if(!line || chatLines.contains(line))
124 qDeleteAll(_highlightItems);
125 _highlightItems.clear();
126 Q_ASSERT(_highlightItems.isEmpty());
128 if(searchString().isEmpty() || !(_searchSenders || _searchMsgs))
133 foreach(ChatLine *line, chatLines) {
134 if(_searchOnlyRegularMsgs) {
135 index = model->index(line->row(), 0);
136 if(!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
142 // we have to crawl through the data
145 int rowCount = model->rowCount();
146 for(int row = 0; row < rowCount; row++) {
147 ChatLine *line = _scene->chatLine(row);
149 if(_searchOnlyRegularMsgs) {
150 index = model->index(row, 0);
151 if(!checkType((Message::Type)index.data(MessageModel::TypeRole).toInt()))
158 if(!_highlightItems.isEmpty()) {
159 _highlightItems.last()->setHighlighted(true);
160 _currentHighlight = _highlightItems.count() - 1;
161 emit newCurrentHighlight(_highlightItems.last());
165 void ChatViewSearchController::highlightLine(ChatLine *line) {
166 QList<ChatItem *> checkItems;
168 checkItems << &(line->item(MessageModel::SenderColumn));
171 checkItems << &(line->item(MessageModel::ContentsColumn));
173 foreach(ChatItem *item, checkItems) {
174 foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
175 _highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
180 void ChatViewSearchController::repositionHighlights() {
181 QSet<ChatLine *> chatLines;
182 foreach(SearchHighlightItem *item, _highlightItems) {
183 ChatLine *line = qgraphicsitem_cast<ChatLine *>(item->parentItem());
187 QList<ChatLine *> chatLineList(chatLines.toList());
188 foreach(ChatLine *line, chatLineList) {
189 repositionHighlights(line);
193 void ChatViewSearchController::repositionHighlights(ChatLine *line) {
194 QList<SearchHighlightItem *> searchHighlights;
195 foreach(QGraphicsItem *child, line->childItems()) {
196 SearchHighlightItem *highlightItem = qgraphicsitem_cast<SearchHighlightItem *>(child);
198 searchHighlights << highlightItem;
201 if(searchHighlights.isEmpty())
204 QList<QPointF> wordPos;
206 foreach(QRectF wordRect, line->senderItem().findWords(searchString(), caseSensitive())) {
207 wordPos << QPointF(wordRect.x() + line->senderItem().x(), wordRect.y());
211 foreach(QRectF wordRect, line->contentsItem().findWords(searchString(), caseSensitive())) {
212 wordPos << QPointF(wordRect.x() + line->contentsItem().x(), wordRect.y());
216 qSort(searchHighlights.begin(), searchHighlights.end(), SearchHighlightItem::firstInLine);
218 Q_ASSERT(wordPos.count() == searchHighlights.count());
219 for(int i = 0; i < searchHighlights.count(); i++) {
220 searchHighlights.at(i)->setPos(wordPos.at(i));
224 void ChatViewSearchController::sceneDestroyed() {
225 // WARNING: don't call any methods on scene!
227 // the items will be automatically deleted when the scene is destroyed
228 // so we just have to clear the list;
229 _highlightItems.clear();
232 void ChatViewSearchController::setCaseSensitive(bool caseSensitive) {
233 if(_caseSensitive == caseSensitive)
236 _caseSensitive = caseSensitive;
238 // we can reuse the original search results if the new search
239 // parameters are a restriction of the original one
240 updateHighlights(caseSensitive);
243 void ChatViewSearchController::setSearchSenders(bool searchSenders) {
244 if(_searchSenders == searchSenders)
247 _searchSenders = searchSenders;
248 // we can reuse the original search results if the new search
249 // parameters are a restriction of the original one
250 updateHighlights(!searchSenders);
253 void ChatViewSearchController::setSearchMsgs(bool searchMsgs) {
254 if(_searchMsgs == searchMsgs)
257 _searchMsgs = searchMsgs;
259 // we can reuse the original search results if the new search
260 // parameters are a restriction of the original one
261 updateHighlights(!searchMsgs);
264 void ChatViewSearchController::setSearchOnlyRegularMsgs(bool searchOnlyRegularMsgs) {
265 if(_searchOnlyRegularMsgs == searchOnlyRegularMsgs)
268 _searchOnlyRegularMsgs = searchOnlyRegularMsgs;
270 // we can reuse the original search results if the new search
271 // parameters are a restriction of the original one
272 updateHighlights(searchOnlyRegularMsgs);
276 // ==================================================
277 // SearchHighlightItem
278 // ==================================================
279 SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
281 QGraphicsItem(parent),
286 setPos(wordRect.x(), wordRect.y());
287 qreal sizedelta = wordRect.height() * 0.1;
288 _boundingRect = QRectF(-sizedelta, -sizedelta, wordRect.width() + 2 * sizedelta, wordRect.height() + 2 * sizedelta);
290 connect(&_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(updateHighlight(qreal)));
293 void SearchHighlightItem::setHighlighted(bool highlighted) {
294 _highlighted = highlighted;
297 _timeLine.setDirection(QTimeLine::Forward);
299 _timeLine.setDirection(QTimeLine::Backward);
301 if(_timeLine.state() != QTimeLine::Running)
307 void SearchHighlightItem::updateHighlight(qreal value) {
308 _alpha = 100 + 155 * value;
312 void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
316 painter->setPen(QPen(QColor(0, 0, 0, _alpha), 1.5));
317 painter->setBrush(QColor(254, 237, 45, _alpha));
318 painter->setRenderHints(QPainter::Antialiasing);
319 qreal radius = boundingRect().height() * 0.30;
320 painter->drawRoundedRect(boundingRect(), radius, radius);
323 bool SearchHighlightItem::firstInLine(QGraphicsItem *item1, QGraphicsItem *item2) {
324 if(item1->pos().y() != item2->pos().y())
325 return item1->pos().y() < item2->pos().y();
327 return item1->pos().x() < item2->pos().x();