From a9b930d3df4961a7044ebd2beeb8fe1148506687 Mon Sep 17 00:00:00 2001 From: Ilia Ryabokon Date: Thu, 12 Dec 2013 14:47:28 +0300 Subject: [PATCH] Minor event signals bugfixes UBGraphicsTextItem --- src/domain/UBGraphicsItemDelegate.cpp | 12 ++++++++ src/domain/UBGraphicsItemDelegate.h | 3 ++ src/domain/UBGraphicsTextItem.cpp | 20 ++++++++++++ src/domain/UBGraphicsTextItem.h | 3 ++ src/domain/UBGraphicsTextItemDelegate.cpp | 37 +++++++++++++++++++++-- src/domain/UBGraphicsTextItemDelegate.h | 4 +++ 6 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/domain/UBGraphicsItemDelegate.cpp b/src/domain/UBGraphicsItemDelegate.cpp index 6b9d1946..db3a275e 100644 --- a/src/domain/UBGraphicsItemDelegate.cpp +++ b/src/domain/UBGraphicsItemDelegate.cpp @@ -407,6 +407,18 @@ void UBGraphicsItemDelegate::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) } +bool UBGraphicsItemDelegate::keyPressEvent(QKeyEvent *event) +{ + Q_UNUSED(event); + return true; +} + +bool UBGraphicsItemDelegate::keyReleaseEvent(QKeyEvent *event) +{ + Q_UNUSED(event); + return true; +} + QGraphicsItem *UBGraphicsItemDelegate::delegated() { QGraphicsItem *curDelegate = 0; diff --git a/src/domain/UBGraphicsItemDelegate.h b/src/domain/UBGraphicsItemDelegate.h index 20b34230..cc8ded60 100644 --- a/src/domain/UBGraphicsItemDelegate.h +++ b/src/domain/UBGraphicsItemDelegate.h @@ -255,6 +255,9 @@ class UBGraphicsItemDelegate : public QObject virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + virtual bool keyPressEvent(QKeyEvent *event); + virtual bool keyReleaseEvent(QKeyEvent *event); + virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value); virtual UBGraphicsScene *castUBGraphicsScene(); diff --git a/src/domain/UBGraphicsTextItem.cpp b/src/domain/UBGraphicsTextItem.cpp index 3ba635fa..783640a8 100644 --- a/src/domain/UBGraphicsTextItem.cpp +++ b/src/domain/UBGraphicsTextItem.cpp @@ -212,6 +212,26 @@ void UBGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) } } +void UBGraphicsTextItem::keyPressEvent(QKeyEvent *event) +{ + if (Delegate() && !Delegate()->keyPressEvent(event)) { + qDebug() << "UBGraphicsTextItem::keyPressEvent(QKeyEvent *event) has been rejected by delegate. Don't call base class method"; + return; + } + + QGraphicsTextItem::keyPressEvent(event); +} + +void UBGraphicsTextItem::keyReleaseEvent(QKeyEvent *event) +{ + if (Delegate() && !Delegate()->keyReleaseEvent(event)) { + qDebug() << "UBGraphicsTextItem::keyPressEvent(QKeyEvent *event) has been rejected by delegate. Don't call base class method"; + return; + } + + QGraphicsTextItem::keyReleaseEvent(event); +} + void UBGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QColor color = UBSettings::settings()->isDarkBackground() ? mColorOnDarkBackground : mColorOnLightBackground; diff --git a/src/domain/UBGraphicsTextItem.h b/src/domain/UBGraphicsTextItem.h index f44da941..fd7e2af4 100644 --- a/src/domain/UBGraphicsTextItem.h +++ b/src/domain/UBGraphicsTextItem.h @@ -110,6 +110,9 @@ class UBGraphicsTextItem : public QGraphicsTextItem, public UBItem, public UBRes virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual void keyPressEvent(QKeyEvent *event); + virtual void keyReleaseEvent(QKeyEvent *event); + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); diff --git a/src/domain/UBGraphicsTextItemDelegate.cpp b/src/domain/UBGraphicsTextItemDelegate.cpp index 3c22d6d9..b724494b 100644 --- a/src/domain/UBGraphicsTextItemDelegate.cpp +++ b/src/domain/UBGraphicsTextItemDelegate.cpp @@ -334,6 +334,8 @@ void UBGraphicsTextItemDelegate::alignButtonProcess() AlignTextButton *asAlText = static_cast(mAlignButton); if (asAlText->nextKind() == AlignTextButton::k_mixed) { restoreTextCursorFormats(); + asAlText->setNextKind(); + return; } asAlText->setNextKind(); @@ -366,14 +368,19 @@ void UBGraphicsTextItemDelegate::onCursorPositionChanged(const QTextCursor &curs qDebug() << "-----------------------"; qDebug() << "we have a selection!" << cursor.selectionStart(); qDebug() << "-----------------------"; - updateAlighButtonState(); +// updateAlighButtonState(); } void UBGraphicsTextItemDelegate::onModificationChanged(bool ch) { Q_UNUSED(ch); qDebug() << "modification changed"; - updateAlighButtonState(); +// updateAlighButtonState(); +} + +void UBGraphicsTextItemDelegate::onContentChanged() +{ + qDebug() << "onContentChanged"; } UBGraphicsTextItem* UBGraphicsTextItemDelegate::delegated() @@ -502,6 +509,31 @@ bool UBGraphicsTextItemDelegate::mouseReleaseEvent(QGraphicsSceneMouseEvent *eve return true; } +bool UBGraphicsTextItemDelegate::keyPressEvent(QKeyEvent *event) +{ + Q_UNUSED(event); + return true; +} + +bool UBGraphicsTextItemDelegate::keyReleaseEvent(QKeyEvent *event) +{ + if (!delegated()->hasFocus()) { + return true; + } + + switch (event->key()) { + case Qt::Key_Left: + case Qt::Key_Right: + case Qt::Key_Up: + case Qt::Key_Down: + updateAlighButtonState(); + break; + } + + qDebug() << "Key has been released" << QString::number(event->key(), 16); + return true; +} + void UBGraphicsTextItemDelegate::ChangeTextSize(qreal factor, textChangeMode changeMode) { if (scaleSize == changeMode) @@ -609,6 +641,7 @@ void UBGraphicsTextItemDelegate::updateAlighButtonState() return; } + qDebug() << "new cursor position" << delegated()->textCursor().position(); AlignTextButton *asAlBtn = static_cast(mAlignButton); if (!oneBlockSelection()) { diff --git a/src/domain/UBGraphicsTextItemDelegate.h b/src/domain/UBGraphicsTextItemDelegate.h index a6b4c5b1..1614a24d 100644 --- a/src/domain/UBGraphicsTextItemDelegate.h +++ b/src/domain/UBGraphicsTextItemDelegate.h @@ -129,6 +129,9 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual bool keyPressEvent(QKeyEvent *event); + virtual bool keyReleaseEvent(QKeyEvent *event); + private: UBGraphicsTextItem* delegated(); @@ -179,6 +182,7 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate void alignButtonProcess(); void onCursorPositionChanged(const QTextCursor& cursor); void onModificationChanged(bool ch); + void onContentChanged(); private: const int delta;