From b4fa8323af5e8834812e1a565067f1e87a4a7e90 Mon Sep 17 00:00:00 2001 From: krepa098 Date: Thu, 15 Jan 2015 11:48:41 +0100 Subject: [PATCH] refactoring, reverted ChatLog::getVisibleRect --- src/chatlog/chatline.cpp | 15 ++++++++++++ src/chatlog/chatline.h | 5 ++++ src/chatlog/chatlog.cpp | 50 ++++++++++++---------------------------- src/chatlog/chatlog.h | 2 +- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/chatlog/chatline.cpp b/src/chatlog/chatline.cpp index 801825c9e..b0a56adea 100644 --- a/src/chatlog/chatline.cpp +++ b/src/chatlog/chatline.cpp @@ -260,3 +260,18 @@ void ChatLine::moveBy(qreal deltaY) bbox.moveTop(bbox.top() + deltaY); } + +bool ChatLine::lessThanBSRectTop(const ChatLine::Ptr lhs, const qreal rhs) +{ + return lhs->boundingSceneRect().top() < rhs; +} + +bool ChatLine::lessThanBSRectBottom(const ChatLine::Ptr lhs, const qreal rhs) +{ + return lhs->boundingSceneRect().bottom() < rhs; +} + +bool ChatLine::lessThanRowIndex(const ChatLine::Ptr lhs, const ChatLine::Ptr rhs) +{ + return lhs->getRowIndex() < rhs->getRowIndex(); +} diff --git a/src/chatlog/chatline.h b/src/chatlog/chatline.h index 458e9c436..7514654cb 100644 --- a/src/chatlog/chatline.h +++ b/src/chatlog/chatline.h @@ -94,6 +94,11 @@ protected: void setRowIndex(int idx); void visibilityChanged(bool visible); + //comparators + static bool lessThanBSRectTop(const ChatLine::Ptr lhs, const qreal rhs); + static bool lessThanBSRectBottom(const ChatLine::Ptr lhs, const qreal rhs); + static bool lessThanRowIndex(const ChatLine::Ptr lhs, const ChatLine::Ptr rhs); + private: int rowIndex = -1; std::vector content; // 3 columns diff --git a/src/chatlog/chatlog.cpp b/src/chatlog/chatlog.cpp index e3968e9a4..b2d8239ad 100644 --- a/src/chatlog/chatlog.cpp +++ b/src/chatlog/chatlog.cpp @@ -35,16 +35,6 @@ T clamp(T x, T min, T max) return x; } -auto compareSmaller = [](const ChatLine::Ptr lhs, const qreal rhs) -> bool -{ - return lhs->boundingSceneRect().top() < rhs; -}; - -auto compareGreater = [](const qreal lhs, const ChatLine::Ptr rhs) -> bool -{ - return lhs < rhs->boundingSceneRect().bottom(); -}; - ChatLog::ChatLog(QWidget* parent) : QGraphicsView(parent) { @@ -71,7 +61,7 @@ ChatLog::ChatLog(QWidget* parent) copyAction = new QAction(this); copyAction->setShortcut(QKeySequence::Copy); addAction(copyAction); - connect(copyAction, &QAction::triggered, this, [ = ](bool) + connect(copyAction, &QAction::triggered, this, [this](bool) { copySelectedText(); }); @@ -148,7 +138,7 @@ void ChatLog::clearSelection() QRect ChatLog::getVisibleRect() const { - return mapToScene(calculateSceneRect().toRect()).boundingRect().toRect(); + return mapToScene(viewport()->rect()).boundingRect().toRect(); } void ChatLog::updateSceneRect() @@ -282,7 +272,7 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev) if(selectionMode == None && (clickPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) { QPointF sceneClickPos = mapToScene(clickPos.toPoint()); - ChatLine::Ptr line = findLineByYPos(scenePos.y()); + ChatLine::Ptr line = findLineByPosY(scenePos.y()); ChatLineContent* content = getContentFromPos(sceneClickPos); if(content) @@ -313,7 +303,7 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev) if(selectionMode != None) { ChatLineContent* content = getContentFromPos(scenePos); - ChatLine::Ptr line = findLineByYPos(scenePos.y()); + ChatLine::Ptr line = findLineByPosY(scenePos.y()); if(!content && !line.get()) return; @@ -369,18 +359,11 @@ ChatLineContent* ChatLog::getContentFromPos(QPointF scenePos) const if(lines.empty()) return nullptr; - //the first visible line - auto lowerBound = std::upper_bound(lines.cbegin(), lines.cend(), scenePos.y(), compareGreater); - - //the last visible line - auto upperBound = std::lower_bound(lines.cbegin(), lines.cend(), scenePos.y(), compareSmaller); + auto itr = std::lower_bound(lines.cbegin(), lines.cend(), scenePos.y(), ChatLine::lessThanBSRectBottom); //find content - for(auto itr = lowerBound; itr != upperBound; ++itr) - { - if((*itr)->boundingSceneRect().contains(scenePos)) - return (*itr)->getContent(scenePos); - } + if(itr != lines.cend() && (*itr)->boundingSceneRect().contains(scenePos)) + return (*itr)->getContent(scenePos); return nullptr; } @@ -620,10 +603,10 @@ void ChatLog::checkVisibility() return; // find first visible line - auto lowerBound = std::upper_bound(lines.cbegin(), lines.cend(), getVisibleRect().top(), compareGreater); + auto lowerBound = std::lower_bound(lines.cbegin(), lines.cend(), getVisibleRect().top(), ChatLine::lessThanBSRectBottom); // find last visible line - auto upperBound = std::lower_bound(lines.cbegin(), lines.cend(), getVisibleRect().bottom(), compareSmaller); + auto upperBound = std::lower_bound(lines.cbegin(), lines.cend(), getVisibleRect().bottom(), ChatLine::lessThanBSRectTop); // set visibilty QList newVisibleLines; @@ -644,13 +627,10 @@ void ChatLog::checkVisibility() visibleLines = newVisibleLines; // enforce order - std::sort(visibleLines.begin(), visibleLines.end(), [](const ChatLine::Ptr lhs, const ChatLine::Ptr rhs) - { - return lhs->getRowIndex() < rhs->getRowIndex(); - }); + std::sort(visibleLines.begin(), visibleLines.end(), ChatLine::lessThanRowIndex); //if(!visibleLines.empty()) - // qDebug() << "visible from " << visibleLines.first()->getRowIndex() << "to " << visibleLines.last()->getRowIndex() << " total " << visibleLines.size(); + // qDebug() << "visible from " << visibleLines.first()->getRowIndex() << "to " << visibleLines.last()->getRowIndex() << " total " << visibleLines.size(); } void ChatLog::scrollContentsBy(int dx, int dy) @@ -710,12 +690,12 @@ void ChatLog::updateTypingNotification() notification->layout(useableWidth(), QPointF(0.0, posY)); } -ChatLine::Ptr ChatLog::findLineByYPos(qreal yPos) const +ChatLine::Ptr ChatLog::findLineByPosY(qreal yPos) const { - auto lowerBound = std::upper_bound(lines.cbegin(), lines.cend(), yPos, compareGreater); + auto itr = std::lower_bound(lines.cbegin(), lines.cend(), yPos, ChatLine::lessThanBSRectBottom); - if(lowerBound != lines.cend()) - return *lowerBound; + if(itr != lines.cend()) + return *itr; return ChatLine::Ptr(); } diff --git a/src/chatlog/chatlog.h b/src/chatlog/chatlog.h index 0d6ee5327..af031842f 100644 --- a/src/chatlog/chatlog.h +++ b/src/chatlog/chatlog.h @@ -78,7 +78,7 @@ protected: void updateMultiSelectionRect(); void updateTypingNotification(); - ChatLine::Ptr findLineByYPos(qreal yPos) const; + ChatLine::Ptr findLineByPosY(qreal yPos) const; private slots: void onSelectionTimerTimeout();