diff options
author | John Tapsell <john.tapsell.ext@basyskom.de> | 2010-08-05 09:28:04 +0900 |
---|---|---|
committer | Tomas Junnonen <tomas.junnonen@nokia.com> | 2010-08-05 14:05:09 +0300 |
commit | 068bb7f13e6d1a99b0a50b42e8f55c9c4b054b05 (patch) | |
tree | c94b5aaf9dc60a05c47959e29d1e48cabbb4e523 | |
parent | e3fe5bf3f7085fe465eb2ae91d45efcc12a65720 (diff) |
Fixes: NB#176079 - MWidgetController does not connect to MWidgetModel::modified by default
RevBy: Tomas Junnonen
Details: Because we cannot call a virtual function from the constructor, we need to delay the
call of setupModel(). However we do not need to delay this call if setModel is not called from
the constructor. And we can at least connect the dataChanged signal immediately.
Includes unit test and doxygen improvements.
-rw-r--r-- | src/corelib/widgets/core/mwidgetcontroller.cpp | 41 | ||||
-rw-r--r-- | src/corelib/widgets/core/mwidgetcontroller.h | 13 | ||||
-rw-r--r-- | src/corelib/widgets/core/mwidgetcontroller_p.h | 3 | ||||
-rw-r--r-- | tests/ut_mwidgetcontroller/ut_mwidgetcontroller.cpp | 16 | ||||
-rw-r--r-- | tests/ut_mwidgetcontroller/ut_mwidgetcontroller.h | 1 |
5 files changed, 58 insertions, 16 deletions
diff --git a/src/corelib/widgets/core/mwidgetcontroller.cpp b/src/corelib/widgets/core/mwidgetcontroller.cpp index 444d79f4..9916e3ce 100644 --- a/src/corelib/widgets/core/mwidgetcontroller.cpp +++ b/src/corelib/widgets/core/mwidgetcontroller.cpp @@ -71,7 +71,8 @@ MWidgetControllerPrivate::~MWidgetControllerPrivate() MWidgetController::MWidgetController(QGraphicsItem *parent) : MWidget(*new MWidgetControllerPrivate, parent) { - setModel(new MWidgetModel); + Q_D(MWidgetController); + d->setModel(new MWidgetModel); MWidgetControllerPrivate::allSystemWidgets.insert(this); } @@ -79,7 +80,8 @@ MWidgetController::MWidgetController(QGraphicsItem *parent) : MWidgetController::MWidgetController(MWidgetModel *model, QGraphicsItem *parent) : MWidget(*new MWidgetControllerPrivate, parent) { - setModel(model == NULL ? new MWidgetModel : model); + Q_D(MWidgetController); + d->setModel(model == NULL ? new MWidgetModel : model); MWidgetControllerPrivate::allSystemWidgets.insert(this); } @@ -90,7 +92,8 @@ MWidgetController::MWidgetController(MWidgetControllerPrivate *dd, MWidgetModel { //The parent class must set a model Q_ASSERT(model); - setModel(model); + Q_D(MWidgetController); + d->setModel(model); MWidgetControllerPrivate::allSystemWidgets.insert(this); } @@ -159,8 +162,7 @@ void MWidgetController::updateData(const QList<const char *>& modifications) void MWidgetController::setupModel() { Q_D(MWidgetController); - connect(d->model, SIGNAL(modified(QList<const char *>)), - this, SLOT(updateData(QList<const char *>))); + d->modelSetup = true; } void MWidgetController::updateMicroFocus() @@ -176,26 +178,35 @@ void MWidgetController::updateMicroFocus() void MWidgetController::setModel(MWidgetModel *model) { Q_ASSERT_X(model, "MWidgetController", "MWidgetController::setModel() parameter model has to be valid!"); - Q_D(MWidgetController); + d->setModel(model); + //Call setupModel immediately since this is not called from the constructor + setupModel(); +} - if (model == d->model) +void MWidgetControllerPrivate::setModel(MWidgetModel *newModel) +{ + Q_Q(MWidgetController); + if (newModel == model) return; - if (d->model) { - disconnect(d->model, 0, this, 0); - d->model->decreaseReferenceCount(); + if (model) { + q->disconnect(model, 0, q, 0); + model->decreaseReferenceCount(); } - d->model = model; - d->model->increaseReferenceCount(); + model = newModel; + model->increaseReferenceCount(); + + q->connect(model, SIGNAL(modified(QList<const char *>)), + q, SLOT(updateData(QList<const char *>))); // setupModel() will be called on the first model access to finalize the model switch - d->modelSetup = false; + modelSetup = false; // set the model also to view if we already have one - if (d->view) { - d->view->setModel(d->model); + if (view) { + view->setModel(model); } } diff --git a/src/corelib/widgets/core/mwidgetcontroller.h b/src/corelib/widgets/core/mwidgetcontroller.h index 61fd2c40..d223cdec 100644 --- a/src/corelib/widgets/core/mwidgetcontroller.h +++ b/src/corelib/widgets/core/mwidgetcontroller.h @@ -217,9 +217,20 @@ protected Q_SLOTS: /*! Notification of model having changed. - This function is called when the model of the widget is replaced. + This function is called when the model of the widget is initially set or when later replaced. + You can reimplement this method to synchronize your widget's internal state with that of + the new model. + + An example would be an widget that creates a child label widget; on setupModel() it + should synchronize the label text with the model data. Subsequent updates to the model data + and the resulting changes to the label are delivered through updateData(). + + \note The widget uses the base implementation of this function to set up notification + of changes in the model. Because of this, it is very important that subclasses + call the base implementation. \sa setModel() + \sa updateData() */ virtual void setupModel(); diff --git a/src/corelib/widgets/core/mwidgetcontroller_p.h b/src/corelib/widgets/core/mwidgetcontroller_p.h index c2977ee1..453f9cbf 100644 --- a/src/corelib/widgets/core/mwidgetcontroller_p.h +++ b/src/corelib/widgets/core/mwidgetcontroller_p.h @@ -53,6 +53,9 @@ private: // only for MWidgetController */ void createView(); + /*! Set the model to the given model */ + void setModel(MWidgetModel *model); + MWidgetModel *model; MWidgetView *view; bool viewSetManually; diff --git a/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.cpp b/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.cpp index 7f8c0e84..746e7b76 100644 --- a/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.cpp +++ b/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.cpp @@ -28,6 +28,7 @@ int qQGraphicsWidgetSizeHintCallCount; int modelSetupCount; +int updateDataCount; bool Ut_MWidgetController::viewCreatesChildWidgets = false; bool Ut_MWidgetController::viewSetsItselfActive = false; @@ -85,6 +86,8 @@ public: void useModel(); protected: virtual void setupModel(); + virtual void updateData(const QList<const char *>& modifications); + }; void TestWidgetController::setView(MWidgetView *view) @@ -105,6 +108,12 @@ void TestWidgetController::setupModel() modelSetupCount++; } +void TestWidgetController::updateData(const QList<const char *>& modifications) +{ + Q_UNUSED(modifications); + updateDataCount++; +} + // QGraphicsWidget stubs (used by MWidgetController) QSizeF QGraphicsWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const @@ -130,6 +139,7 @@ void Ut_MWidgetController::cleanupTestCase() void Ut_MWidgetController::init() { modelSetupCount = 0; + updateDataCount = 0; qQGraphicsWidgetSizeHintCallCount = 0; controller = new TestWidgetController; viewCreatesChildWidgets = false; @@ -193,6 +203,12 @@ void Ut_MWidgetController::testSizeHint() QCOMPARE(modelSetupCount, 1); } +void Ut_MWidgetController::testUpdateData() +{ + controller->model()->setObjectName("Hello"); + QCOMPARE(updateDataCount, 1); +} + void Ut_MWidgetController::testActiveStateWhenViewIsSet() { MockWidgetView *view = new MockWidgetView(controller); diff --git a/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.h b/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.h index a35a9c74..52aacb67 100644 --- a/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.h +++ b/tests/ut_mwidgetcontroller/ut_mwidgetcontroller.h @@ -52,6 +52,7 @@ private slots: void testSizeHint(); void testActiveStateWhenViewIsSet(); void testActiveStateWhenViewIsUnset(); + void testUpdateData(); }; #endif // UT_MWIDGETCONTROLLER_H |