+void ChatViewSearchController::updateHighlights(bool reuse)
+{
+ if (!_scene)
+ return;
+
+ if (reuse) {
+ QSet<ChatLine *> chatLines;
+ foreach(SearchHighlightItem *highlightItem, _highlightItems) {
+ ChatLine *line = qgraphicsitem_cast<ChatLine *>(highlightItem->parentItem());
+ if (line)
+ chatLines << line;
+ }
+ foreach(ChatLine *line, QList<ChatLine *>(chatLines.toList())) {
+ updateHighlights(line);
+ }
+ }
+ else {
+ QPointF oldHighlightPos;
+ if (!_highlightItems.isEmpty() && _currentHighlight < _highlightItems.count()) {
+ oldHighlightPos = _highlightItems[_currentHighlight]->scenePos();
+ }
+ qDeleteAll(_highlightItems);
+ _highlightItems.clear();
+ Q_ASSERT(_highlightItems.isEmpty());
+
+ if (searchString().isEmpty() || !(_searchSenders || _searchMsgs))
+ return;
+
+ checkMessagesForHighlight();
+
+ if (!_highlightItems.isEmpty()) {
+ if (!oldHighlightPos.isNull()) {
+ int start = 0; int end = _highlightItems.count() - 1;
+ QPointF startPos;
+ QPointF endPos;
+ while (1) {
+ startPos = _highlightItems[start]->scenePos();
+ endPos = _highlightItems[end]->scenePos();
+ if (startPos == oldHighlightPos) {
+ _currentHighlight = start;
+ break;
+ }
+ if (endPos == oldHighlightPos) {
+ _currentHighlight = end;
+ break;
+ }
+ if (end - start == 1) {
+ _currentHighlight = start;
+ break;
+ }
+ int pivot = (end + start) / 2;
+ QPointF pivotPos = _highlightItems[pivot]->scenePos();
+ if (startPos.y() == endPos.y()) {
+ if (oldHighlightPos.x() <= pivotPos.x())
+ end = pivot;
+ else
+ start = pivot;
+ }
+ else {
+ if (oldHighlightPos.y() <= pivotPos.y())
+ end = pivot;
+ else
+ start = pivot;
+ }
+ }
+ }
+ else {
+ _currentHighlight = _highlightItems.count() - 1;
+ }
+ _highlightItems[_currentHighlight]->setHighlighted(true);
+ emit newCurrentHighlight(_highlightItems[_currentHighlight]);
+ }