From 525f585465029075bace5f68997bea07e64f7725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Nilsen?= Date: Fri, 7 Aug 2009 10:21:42 +0200 Subject: Add ChangeFlags to QGraphicsEffect and pass flags to sourceChanged. This is way more flexible as it allows for extension in the future. Instead of having several virtual functions, which in most cases when re-implemented have to do exactly the same, we now have one virtual function which takes a ChangedFlags parameter do describe what kind of changes that occurred to the source. --- src/gui/graphicsview/qgraphicseffect.cpp | 13 +++------- src/gui/graphicsview/qgraphicseffect.h | 12 +++++++-- src/gui/graphicsview/qgraphicseffect_p.h | 6 ++++- tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp | 30 +++++++++------------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/gui/graphicsview/qgraphicseffect.cpp b/src/gui/graphicsview/qgraphicseffect.cpp index 6f60ab9..b04af7a 100644 --- a/src/gui/graphicsview/qgraphicseffect.cpp +++ b/src/gui/graphicsview/qgraphicseffect.cpp @@ -316,19 +316,12 @@ void QGraphicsEffect::updateBoundingRect() This virtual function is called by QGraphicsEffect to notify the effect that the source has changed. If the effect applies any cache, then this cache must be purged in order to reflect the new appearance of the source. -*/ -void QGraphicsEffect::sourceChanged() -{ -} -/*! - This virtual function is called by QGraphicsEffect to notify the effect - that the source's bounding rectangle has changed. If the effect applies any - cache, then this cache must be purged in order to reflect the new - appearance of the source. + The \a flags describes what has changed. */ -void QGraphicsEffect::sourceBoundingRectChanged() +void QGraphicsEffect::sourceChanged(ChangeFlags flags) { + Q_UNUSED(flags); } QGraphicsGrayscaleEffect::QGraphicsGrayscaleEffect() diff --git a/src/gui/graphicsview/qgraphicseffect.h b/src/gui/graphicsview/qgraphicseffect.h index bb79563..d171b1b 100644 --- a/src/gui/graphicsview/qgraphicseffect.h +++ b/src/gui/graphicsview/qgraphicseffect.h @@ -94,8 +94,17 @@ class QGraphicsEffectPrivate; class Q_GUI_EXPORT QGraphicsEffect : public QObject { Q_OBJECT + Q_FLAGS(ChangeFlags) Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) public: + enum ChangeFlag { + SourceAttached = 0x1, + SourceDetached = 0x2, + SourceBoundingRectChanged = 0x4, + SourceInvalidated = 0x8 + }; + Q_DECLARE_FLAGS(ChangeFlags, ChangeFlag); + QGraphicsEffect(); virtual ~QGraphicsEffect(); @@ -114,8 +123,7 @@ public Q_SLOTS: protected: QGraphicsEffect(QGraphicsEffectPrivate &d); virtual void draw(QPainter *painter, QGraphicsEffectSource *source) = 0; - virtual void sourceChanged(); - virtual void sourceBoundingRectChanged(); + virtual void sourceChanged(ChangeFlags flags); void updateBoundingRect(); private: diff --git a/src/gui/graphicsview/qgraphicseffect_p.h b/src/gui/graphicsview/qgraphicseffect_p.h index 6664a03..6d546cc 100644 --- a/src/gui/graphicsview/qgraphicseffect_p.h +++ b/src/gui/graphicsview/qgraphicseffect_p.h @@ -94,12 +94,16 @@ public: inline void setGraphicsEffectSource(QGraphicsEffectSource *newSource) { + QGraphicsEffect::ChangeFlags flags; if (source) { + flags |= QGraphicsEffect::SourceDetached; source->d_func()->detach(); delete source; } source = newSource; - q_func()->sourceChanged(); + if (newSource) + flags |= QGraphicsEffect::SourceAttached; + q_func()->sourceChanged(flags); } QGraphicsEffectSource *source; diff --git a/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp b/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp index ee8a973..b4c78f7 100644 --- a/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp +++ b/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp @@ -99,9 +99,8 @@ class CustomEffect : public QGraphicsEffect { public: CustomEffect() - : QGraphicsEffect(), numRepaints(0), m_margin(10), m_sourceChanged(false), - m_sourceBoundingRectChanged(false), doNothingInDraw(false), - m_painter(0), m_styleOption(0), m_source(0) + : QGraphicsEffect(), numRepaints(0), m_margin(10), + doNothingInDraw(false), m_painter(0), m_styleOption(0), m_source(0) {} QRectF boundingRectFor(const QRectF &rect) const @@ -110,8 +109,7 @@ public: void reset() { numRepaints = 0; - m_sourceChanged = false; - m_sourceBoundingRectChanged = false; + m_sourceChangedFlags = QGraphicsEffect::ChangeFlags(); m_painter = 0; m_styleOption = 0; m_source = 0; @@ -137,16 +135,12 @@ public: source->draw(painter); } - void sourceChanged() - { m_sourceChanged = true; } - - void sourceBoundingRectChanged() - { m_sourceBoundingRectChanged = true; } + void sourceChanged(QGraphicsEffect::ChangeFlags flags) + { m_sourceChangedFlags |= flags; } int numRepaints; int m_margin; - bool m_sourceChanged; - bool m_sourceBoundingRectChanged; + QGraphicsEffect::ChangeFlags m_sourceChangedFlags; bool doNothingInDraw; QPainter *m_painter; const QStyleOption *m_styleOption; @@ -166,34 +160,34 @@ void tst_QGraphicsEffect::source() { QPointer effect = new CustomEffect; QVERIFY(!effect->source()); - QVERIFY(!effect->m_sourceChanged); + QVERIFY(!effect->m_sourceChangedFlags); // Install effect on QGraphicsItem. QGraphicsItem *item = new QGraphicsRectItem(0, 0, 10, 10); item->setGraphicsEffect(effect); QVERIFY(effect->source()); QCOMPARE(effect->source()->graphicsItem(), item); - QVERIFY(effect->m_sourceChanged); + QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceAttached); effect->reset(); // Make sure disabling/enabling the effect doesn't change the source. effect->setEnabled(false); QVERIFY(effect->source()); QCOMPARE(effect->source()->graphicsItem(), item); - QVERIFY(!effect->m_sourceChanged); + QVERIFY(!effect->m_sourceChangedFlags); effect->reset(); effect->setEnabled(true); QVERIFY(effect->source()); QCOMPARE(effect->source()->graphicsItem(), item); - QVERIFY(!effect->m_sourceChanged); + QVERIFY(!effect->m_sourceChangedFlags); effect->reset(); // Uninstall effect on QGraphicsItem. effect->reset(); item->setGraphicsEffect(0); QVERIFY(!effect->source()); - QVERIFY(effect->m_sourceChanged); + QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceDetached); // The item takes ownership and should delete the effect when destroyed. item->setGraphicsEffect(effect); @@ -304,7 +298,7 @@ void tst_QGraphicsEffect::draw() QTest::qWait(50); QCOMPARE(effect->numRepaints, 1); QCOMPARE(item->numRepaints, 1); - QVERIFY(effect->m_sourceBoundingRectChanged); + QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceBoundingRectChanged); effect->reset(); item->reset(); -- cgit v0.12