diff options
author | Janne Heikkilä <janne.heikkila@symbio.com> | 2010-10-22 14:54:25 +0300 |
---|---|---|
committer | Stanislav Ionascu <stanislav.ionascu@nokia.com> | 2010-12-30 12:55:15 +0200 |
commit | cc5e7d9acb7ed3fc0e94a976d663406a8f6b0bfc (patch) | |
tree | 6d8662c5a441ec79d310f9aad7f84e401799124a | |
parent | f0be6883dfae636784f6a3f3a995d21db2bbe89c (diff) |
Fixes: NB#199035 - MObjectMenu doesn't work without a target widget
RevBy: Stanislav Ionascu
Details: Passing target widget to objectmenu's constructor is now optional. Menu
will also include/show actions that are added to the objectmenu itself.
-rw-r--r-- | src/corelib/widgets/mobjectmenu.cpp | 55 | ||||
-rw-r--r-- | src/corelib/widgets/mobjectmenu.h | 57 | ||||
-rw-r--r-- | tests/ut_mobjectmenu/ut_mobjectmenu.cpp | 72 | ||||
-rw-r--r-- | tests/ut_mobjectmenu/ut_mobjectmenu.h | 3 |
4 files changed, 142 insertions, 45 deletions
diff --git a/src/corelib/widgets/mobjectmenu.cpp b/src/corelib/widgets/mobjectmenu.cpp index 29a45c2b..ee569366 100644 --- a/src/corelib/widgets/mobjectmenu.cpp +++ b/src/corelib/widgets/mobjectmenu.cpp @@ -31,42 +31,49 @@ M_REGISTER_WIDGET_NO_CREATE(MObjectMenu) MObjectMenu::MObjectMenu(MWidget *target) : MSceneWindow(new MSceneWindowPrivate, new MObjectMenuModel, MSceneWindow::ObjectMenu) { - QList<QAction *> actionList = target->actions(); - MActionList list; - - // go trough all actions in the target widget and add the the actions - // which are associated to object menu to model. - const int actionListCount = actionList.count(); - for (int i = 0; i < actionListCount; ++i) { - MAction *action = qobject_cast<MAction *>(actionList.at(i)); - if (action && (action->location() & MAction::ObjectMenuLocation)) { - list.append(action); + if (target) { + // go trough all actions in the target widget and add the the actions + // which are associated to object menu to model. + QList<QAction *> actionList = target->actions(); + MActionList list; + const int actionListCount = actionList.count(); + for (int i = 0; i < actionListCount; ++i) { + MAction *action = qobject_cast<MAction *>(actionList.at(i)); + if (action && (action->location() & MAction::ObjectMenuLocation)) { + list.append(action); + } } - } - model()->setActions(list); + model()->setActions(list); - QVariant v = target->property("contentURI"); - if(v.isValid()) - model()->setContentURI(v.toString()); + QVariant v = target->property("contentURI"); + if(v.isValid()) + model()->setContentURI(v.toString()); - v = target->property("objectMenuTitle"); - if(v.isValid()) - model()->setTitle(v.toString()); + v = target->property("objectMenuTitle"); + if(v.isValid()) + model()->setTitle(v.toString()); - v = target->property("objectMenuIconId"); - if(v.isValid()) - model()->setIconId(v.toString()); + v = target->property("objectMenuIconId"); + if(v.isValid()) + model()->setIconId(v.toString()); - // install event filter to the target widget, so we get notified - // when actions are added,removed or changed. - target->installEventFilter(this); + // install event filter to the target widget, so we get notified + // when actions are added,removed or changed. + target->installEventFilter(this); + } + else { + // When there's no target widget, install event filter into itself to + // get notifications of added, removed or changed actions. + installEventFilter(this); + } } MObjectMenu::MObjectMenu(const QList<MAction *> &actionList) : MSceneWindow(new MSceneWindowPrivate, new MObjectMenuModel, MSceneWindow::ObjectMenu) { model()->setActions(actionList); + installEventFilter(this); } MObjectMenu::~MObjectMenu() diff --git a/src/corelib/widgets/mobjectmenu.h b/src/corelib/widgets/mobjectmenu.h index 40f14043..8f84dcc5 100644 --- a/src/corelib/widgets/mobjectmenu.h +++ b/src/corelib/widgets/mobjectmenu.h @@ -29,14 +29,18 @@ class QGraphicsSceneContextMenuEvent; /*! \class MObjectMenu - \brief MObjectMenu is a context-menu like widget, which shows object menu actions from any widget. + \brief MObjectMenu is a context-menu like widget, which shows object menu + actions from any widget or from itself. \ingroup widgets - This class is used by MWidget and will be launched automatically if the - widget gets a context menu event and the widget has some object menu - actions associated to it. To add context menu actions to a widget see the - code snippet below. + This class can be used with or without a MWidget as a source for the actions. + If target widget is not defined the actions are gathered only from the + object menu instance itself. + + Menu is launched automatically by MWidget when it gets a context menu event + and it has some object menu actions associated to it. To add context menu + actions to a widget see the code snippet below. \code // Create an action with title "Edit" and parent it to widget // so the action is freed when the widget gets destroyed. @@ -49,18 +53,43 @@ class QGraphicsSceneContextMenuEvent; widget->addAction(action); \endcode - This class can also be used manually, but normally it's enough to just add - actions to a widget and let the system handle the menu. + When using this class without a target widget, the actions need to be added + manually to the instance of MObjectMenu class. In this case, the menu needs to + be shown manually as well. + \code + //manually initialized an instance of MObjectMenu + MObjectMenu* objectMenu = new MObjectMenu(NULL); + . + . + . + // Create an action with title "Edit" and parent it to objecMenu + // so the action is freed when the menu gets destroyed. + MAction* action = new MAction("Edit", objectMenu); + + // Associate this action to object menu. + action->setLocation(MAction::ObjectMenu); + + // And add the action to object menu instance + objectMenu->addAction(action); + . + . + . + //manually show the object menu when needed + sceneManager()->appearSceneWindow(objectMenu); + \endcode + + Please notice that in most cases it should be enough to use the automatic + object menu launching functionality provided by the MWidget. \section MObjectMenuOverview Overview Object Menu is a popup menu of commands attached to an element that contains functions related to it. It can be seen to be quite close to the idea of a context-sensitive menu ("right click menu"). - Object menu is opened by long tapping the element. There is a visual - transition for opening the menu, starting after a certain time period. - When the menu is activated, the background is dimmed (including Home - and Back buttons). + Usually the object menu is opened by long tapping the element. There is + a visual transition for opening the menu, starting after a certain time + period. When the menu is activated, the background is dimmed (including + Home and Back buttons). \section MObjectMenuUsageGuidlines Usage guidlines \li Object menu is a good place for shortcuts: for things that would @@ -127,8 +156,10 @@ public: /*! \brief Constructs an object menu. - Object menu is associated to exactly one widget. The menu shows - automatically all object menu actions from the \a target widget. + Object menu can be associated with a \a target widget. If \a target + widget is valid, menu will show automatically all the object menu + actions from the specified widget. If \a target is 0, menu includes + only the actions that will be added to the MObjectMenu instance itself. */ explicit MObjectMenu(MWidget *target); diff --git a/tests/ut_mobjectmenu/ut_mobjectmenu.cpp b/tests/ut_mobjectmenu/ut_mobjectmenu.cpp index ef46c5cf..11b4b713 100644 --- a/tests/ut_mobjectmenu/ut_mobjectmenu.cpp +++ b/tests/ut_mobjectmenu/ut_mobjectmenu.cpp @@ -47,21 +47,24 @@ void Ut_MObjectMenu::cleanupTestCase() void Ut_MObjectMenu::testConstructionAndDestruction() { + //test construction and destruction with target widget MWidget *widget = new MWidget(); MObjectMenu *menu = new MObjectMenu(widget); MObjectMenuModel *model = dynamic_cast<MObjectMenuModel *>(menu->model()); - - // check that the model has been created. QVERIFY(model != NULL); - - // check that the action count is zero QVERIFY(model->actions().count() == 0); - delete menu; delete widget; + + //test construction and destruction without target widget + menu = new MObjectMenu(NULL); + model = dynamic_cast<MObjectMenuModel *>(menu->model()); + QVERIFY(model != NULL); + QVERIFY(model->actions().count() == 0); + delete menu; } -void Ut_MObjectMenu::testActionsAddingAndRemoving() +void Ut_MObjectMenu::testActionsUsingTargetWidget() { MWidget *widget = new MWidget(); MAction *action1 = new MAction("Test1", widget); @@ -117,7 +120,6 @@ void Ut_MObjectMenu::testActionsAddingAndRemoving() QCOMPARE(model->actions().at(0), action2); QCOMPARE(model->actions().at(1), action1); - action1->setText("Test234"); QCOMPARE(addedSpy.count(), 2); QCOMPARE(removedSpy.count(), 1); @@ -125,6 +127,62 @@ void Ut_MObjectMenu::testActionsAddingAndRemoving() QCOMPARE(model->actions().count(), 2); QCOMPARE(model->actions().at(0), action2); QCOMPARE(model->actions().at(1), action1); + + delete widget; + delete menu; +} + +void Ut_MObjectMenu::testActionsUsingObjectMenu() +{ + MObjectMenu *menu = new MObjectMenu(NULL); + MObjectMenuModel *model = dynamic_cast<MObjectMenuModel *>(menu->model()); + QCOMPARE(model->actions().count(), 0); + + QSignalSpy addedSpy(model, SIGNAL(actionAdded(MAction *))); + QSignalSpy removedSpy(model, SIGNAL(actionRemoved(MAction *))); + QSignalSpy modifiedSpy(model, SIGNAL(actionModified(MAction *))); + + MAction *action1 = new MAction("Test1", menu); + MAction *action2 = new MAction("Test2", menu); + + menu->addAction(action1); + QCOMPARE(addedSpy.count(), 1); + QCOMPARE(removedSpy.count(), 0); + QCOMPARE(modifiedSpy.count(), 0); + QCOMPARE(model->actions().count(), 1); + QCOMPARE(model->actions().at(0), action1); + + menu->removeAction(action1); + QCOMPARE(addedSpy.count(), 1); + QCOMPARE(removedSpy.count(), 1); + QCOMPARE(modifiedSpy.count(), 0); + QCOMPARE(model->actions().count(), 0); + + menu->addAction(action1); + menu->addAction(action2); + QCOMPARE(addedSpy.count(), 3); + QCOMPARE(removedSpy.count(), 1); + QCOMPARE(modifiedSpy.count(), 0); + QCOMPARE(model->actions().count(), 2); + QCOMPARE(model->actions().at(0), action1); + QCOMPARE(model->actions().at(1), action2); + + action1->setText("Test234"); + QCOMPARE(addedSpy.count(), 3); + QCOMPARE(removedSpy.count(), 1); + QCOMPARE(modifiedSpy.count(), 1); + QCOMPARE(model->actions().count(), 2); + QCOMPARE(model->actions().at(0), action1); + QCOMPARE(model->actions().at(1), action2); + + menu->removeAction(action1); + menu->removeAction(action2); + QCOMPARE(addedSpy.count(), 3); + QCOMPARE(removedSpy.count(), 3); + QCOMPARE(modifiedSpy.count(), 1); + QCOMPARE(model->actions().count(), 0); + + delete menu; } void Ut_MObjectMenu::testCursorPosition() diff --git a/tests/ut_mobjectmenu/ut_mobjectmenu.h b/tests/ut_mobjectmenu/ut_mobjectmenu.h index 0b7fc0be..ef3fd52f 100644 --- a/tests/ut_mobjectmenu/ut_mobjectmenu.h +++ b/tests/ut_mobjectmenu/ut_mobjectmenu.h @@ -40,7 +40,8 @@ private slots: void cleanupTestCase(); void testConstructionAndDestruction(); - void testActionsAddingAndRemoving(); + void testActionsUsingTargetWidget(); + void testActionsUsingObjectMenu(); void testCursorPosition(); void testTitle(); void testIconId(); |