From c91b7cb03c746b4e3a94afc7217b3b08afdef678 Mon Sep 17 00:00:00 2001 From: krepa098 Date: Tue, 9 Dec 2014 13:11:42 +0100 Subject: [PATCH] don't depend on valid cursur --- src/chatlog/content/text.cpp | 16 +++++++++++++++- src/chatlog/content/text.h | 31 +++++++++++++++++-------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/chatlog/content/text.cpp b/src/chatlog/content/text.cpp index 81a613511..c3510a346 100644 --- a/src/chatlog/content/text.cpp +++ b/src/chatlog/content/text.cpp @@ -78,7 +78,10 @@ void Text::selectionMouseMove(QPointF scenePos) ensureIntegrity(); int cur = cursorFromPos(scenePos); if(cur >= 0) + { cursor.setPosition(cur, QTextCursor::KeepAnchor); + selectedText = cursor.selectedText(); + } update(); } @@ -89,12 +92,17 @@ void Text::selectionStarted(QPointF scenePos) int cur = cursorFromPos(scenePos); if(cur >= 0) cursor.setPosition(cur); + + selectedText.clear(); + selectedText.squeeze(); } void Text::selectionCleared() { ensureIntegrity(); cursor.setPosition(0); + selectedText.clear(); + selectedText.squeeze(); freeResources(); update(); @@ -104,6 +112,7 @@ void Text::selectAll() { ensureIntegrity(); cursor.select(QTextCursor::Document); + selectedText = text; update(); } @@ -118,7 +127,7 @@ bool Text::isOverSelection(QPointF scenePos) const QString Text::getSelectedText() const { - return cursor.selectedText(); + return selectedText; } QRectF Text::boundingSceneRect() const @@ -181,6 +190,11 @@ void Text::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) QDesktopServices::openUrl(anchor); } +QString Text::getText() const +{ + return text; +} + void Text::ensureIntegrity() { if(!doc) diff --git a/src/chatlog/content/text.h b/src/chatlog/content/text.h index a2a83096e..d218e72ae 100644 --- a/src/chatlog/content/text.h +++ b/src/chatlog/content/text.h @@ -32,24 +32,26 @@ public: void setText(const QString& txt); - virtual void setWidth(qreal width); + virtual void setWidth(qreal width) override; - virtual void selectionMouseMove(QPointF scenePos); - virtual void selectionStarted(QPointF scenePos); - virtual void selectionCleared(); - virtual void selectAll(); - virtual bool isOverSelection(QPointF scenePos) const; - virtual QString getSelectedText() const; + virtual void selectionMouseMove(QPointF scenePos) override; + virtual void selectionStarted(QPointF scenePos) override; + virtual void selectionCleared() override; + virtual void selectAll() override; + virtual bool isOverSelection(QPointF scenePos) const override; + virtual QString getSelectedText() const override; - virtual QRectF boundingSceneRect() const; - virtual QRectF boundingRect() const; - virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + virtual QRectF boundingSceneRect() const override; + virtual QRectF boundingRect() const override; + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; - virtual void visibilityChanged(bool isVisible); + virtual void visibilityChanged(bool isVisible) override; - virtual qreal firstLineVOffset() const; - virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual qreal firstLineVOffset() const override; + virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override; + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; + + virtual QString getText() const override; protected: // dynamic resource management @@ -63,6 +65,7 @@ private: CustomTextDocument* doc = nullptr; QString text; QString elidedText; + QString selectedText; QSizeF size; bool isVisible = false; bool elide = false;