From 2d97915d86c25d614e0b05775434ca8f3b264ee4 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Sun, 8 Nov 2015 19:48:29 +0100 Subject: [PATCH] Work on menus - 2. --- src/core/rootitem.h | 4 +-- src/gui/dialogs/formmain.cpp | 13 ++++--- src/gui/feedsview.cpp | 13 ------- src/services/abstract/serviceroot.h | 6 ++-- src/services/standard/standardserviceroot.cpp | 35 +++++++++---------- src/services/standard/standardserviceroot.h | 7 ++-- 6 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/core/rootitem.h b/src/core/rootitem.h index 3591023e7..16da2c970 100755 --- a/src/core/rootitem.h +++ b/src/core/rootitem.h @@ -77,9 +77,7 @@ class RootItem : public QObject { } // Returns list of specific actions which can be done with the item. - // NOTE: This method should always create new actions in memory - // before returning them because caller takes ownership of any - // actions returned from here. + // NOTE: Ownership of returned actions is not switched to caller, free them when needed. virtual QList specificContextMenuActions(); // TODO: pracovat s těmito věcmi diff --git a/src/gui/dialogs/formmain.cpp b/src/gui/dialogs/formmain.cpp index 52aac9651..ec213982f 100755 --- a/src/gui/dialogs/formmain.cpp +++ b/src/gui/dialogs/formmain.cpp @@ -177,19 +177,22 @@ void FormMain::updateAddItemMenu() { m_ui->m_menuAddItem->clear(); foreach (ServiceRoot *activated_root, tabWidget()->feedMessageViewer()->feedsView()->sourceModel()->serviceRoots()) { - QMenu *root_menu = activated_root->addItemMenu(); + QMenu *root_menu = new QMenu(activated_root->title(), m_ui->m_menuAddItem); + root_menu->setIcon(activated_root->icon()); + root_menu->setToolTip(activated_root->description()); - if (root_menu == NULL) { - root_menu = new QMenu(activated_root->title(), m_ui->m_menuAddItem); - root_menu->setIcon(activated_root->icon()); - root_menu->setToolTip(activated_root->description()); + QList root_actions = activated_root->addItemMenu(); + if (root_actions.isEmpty()) { QAction *no_action = new QAction(qApp->icons()->fromTheme(QSL("dialog-error")), tr("No possible actions"), m_ui->m_menuAddItem); no_action->setEnabled(false); root_menu->addAction(no_action); } + else { + root_menu->addActions(root_actions); + } m_ui->m_menuAddItem->addMenu(root_menu); } diff --git a/src/gui/feedsview.cpp b/src/gui/feedsview.cpp index 3696162ef..c38b2598f 100755 --- a/src/gui/feedsview.cpp +++ b/src/gui/feedsview.cpp @@ -488,9 +488,6 @@ void FeedsView::initializeContextMenuCategories(RootItem *clicked_item) { m_contextMenuCategories = new QMenu(tr("Context menu for categories"), this); } else { - - // TODO: clear nevymaže z paměti. - // http://doc.qt.io/qt-4.8/qmenu.html#clear m_contextMenuCategories->clear(); } @@ -506,11 +503,6 @@ void FeedsView::initializeContextMenuCategories(RootItem *clicked_item) { if (!specific_actions.isEmpty()) { m_contextMenuCategories->addSeparator(); - - foreach (QAction *action, specific_actions) { - action->setParent(m_contextMenuCategories); - } - m_contextMenuCategories->addActions(specific_actions); } } @@ -539,11 +531,6 @@ void FeedsView::initializeContextMenuFeeds(RootItem *clicked_item) { if (!specific_actions.isEmpty()) { m_contextMenuFeeds->addSeparator(); - - foreach (QAction *action, specific_actions) { - action->setParent(m_contextMenuFeeds); - } - m_contextMenuFeeds->addActions(specific_actions); } } diff --git a/src/services/abstract/serviceroot.h b/src/services/abstract/serviceroot.h index 242c4a99d..89b47efb0 100755 --- a/src/services/abstract/serviceroot.h +++ b/src/services/abstract/serviceroot.h @@ -22,7 +22,7 @@ class FeedsModel; -class QMenu; +class QAction; // THIS IS the root node of the service. // NOTE: The root usually contains some core functionality of the @@ -40,9 +40,7 @@ class ServiceRoot : public RootItem { // b) Add new category // c) ... // NOTE: Caller does NOT take ownership of created menu! - virtual QMenu* addItemMenu() = 0; - - // TODO: dodělat menu, které se zobrazi v menubaru "Services -> tato služba". + virtual QList addItemMenu() = 0; inline FeedsModel *feedsModel() const { return m_feedsModel; diff --git a/src/services/standard/standardserviceroot.cpp b/src/services/standard/standardserviceroot.cpp index b480a713d..147e4ca0a 100755 --- a/src/services/standard/standardserviceroot.cpp +++ b/src/services/standard/standardserviceroot.cpp @@ -32,11 +32,12 @@ #include #include #include -#include +#include StandardServiceRoot::StandardServiceRoot(bool load_from_db, FeedsModel *feeds_model, RootItem *parent) - : ServiceRoot(feeds_model, parent), m_recycleBin(new StandardRecycleBin(this)), m_addItemMenu(NULL) { + : ServiceRoot(feeds_model, parent), m_recycleBin(new StandardRecycleBin(this)), + m_addItemMenu(QList()), m_feedContextMenu(QList()), m_actionFeedFetchMetadata(NULL) { m_title = qApp->system()->getUsername() + QL1S("@") + QL1S(APP_LOW_NAME); m_icon = StandardServiceEntryPoint().icon(); m_description = tr("This is obligatory service account for standard RSS/RDF/ATOM feeds."); @@ -48,9 +49,8 @@ StandardServiceRoot::StandardServiceRoot(bool load_from_db, FeedsModel *feeds_mo } StandardServiceRoot::~StandardServiceRoot() { - if (m_addItemMenu != NULL) { - delete m_addItemMenu; - } + qDeleteAll(m_addItemMenu); + qDeleteAll(m_feedContextMenu); } bool StandardServiceRoot::canBeEdited() { @@ -216,14 +216,17 @@ QHash StandardServiceRoot::allCategories() { } QList StandardServiceRoot::getContextMenuForFeed(StandardFeed *feed) { - QList list; + if (m_feedContextMenu.isEmpty()) { + // Initialize. + m_actionFeedFetchMetadata = new QAction(qApp->icons()->fromTheme(QSL("download-manager")), tr("Fetch metadata"), NULL); + m_feedContextMenu.append(m_actionFeedFetchMetadata); + } - // Fetch feed metadata. - QAction *action_fetch_metadata = new QAction(qApp->icons()->fromTheme(QSL("download-manager")), tr("Fetch metadata"), NULL); - connect(action_fetch_metadata, SIGNAL(triggered()), feed, SLOT(fetchMetadataForItself())); + // Make connections. + disconnect(m_actionFeedFetchMetadata, SIGNAL(triggered()), 0, 0); + connect(m_actionFeedFetchMetadata, SIGNAL(triggered()), feed, SLOT(fetchMetadataForItself())); - list.append(action_fetch_metadata); - return list; + return m_feedContextMenu; } void StandardServiceRoot::assembleFeeds(FeedAssignment feeds) { @@ -328,14 +331,10 @@ bool StandardServiceRoot::mergeImportExportModel(FeedsImportExportModel *model, return !some_feed_category_error; } -QMenu *StandardServiceRoot::addItemMenu() { - if (m_addItemMenu == NULL) { - m_addItemMenu = new QMenu(title(), NULL); - m_addItemMenu->setIcon(icon()); - m_addItemMenu->setToolTip(description()); - +QList StandardServiceRoot::addItemMenu() { + if (m_addItemMenu.isEmpty()) { // TODO: Add items. - m_addItemMenu->addAction(new QAction("abc", m_addItemMenu)); + m_addItemMenu.append(new QAction("abc", this)); } return m_addItemMenu; diff --git a/src/services/standard/standardserviceroot.h b/src/services/standard/standardserviceroot.h index 598d552be..7c2b9ab92 100755 --- a/src/services/standard/standardserviceroot.h +++ b/src/services/standard/standardserviceroot.h @@ -66,7 +66,7 @@ class StandardServiceRoot : public ServiceRoot { bool mergeImportExportModel(FeedsImportExportModel *model, QString &output_message); // Return "add feed" and "add category" items. - QMenu *addItemMenu(); + QList addItemMenu(); private: void loadFromDatabase(); @@ -79,7 +79,10 @@ class StandardServiceRoot : public ServiceRoot { StandardRecycleBin *m_recycleBin; // Menus. - QMenu *m_addItemMenu; + QList m_addItemMenu; + QList m_feedContextMenu; + + QAction *m_actionFeedFetchMetadata; }; #endif // STANDARDSERVICEROOT_H