Work on menus - 2.

This commit is contained in:
Martin Rotter 2015-11-08 19:48:29 +01:00
parent fa976120b8
commit 2d97915d86
6 changed files with 33 additions and 45 deletions

View File

@ -77,9 +77,7 @@ class RootItem : public QObject {
} }
// Returns list of specific actions which can be done with the item. // Returns list of specific actions which can be done with the item.
// NOTE: This method should always create new actions in memory // NOTE: Ownership of returned actions is not switched to caller, free them when needed.
// before returning them because caller takes ownership of any
// actions returned from here.
virtual QList<QAction*> specificContextMenuActions(); virtual QList<QAction*> specificContextMenuActions();
// TODO: pracovat s těmito věcmi // TODO: pracovat s těmito věcmi

View File

@ -177,19 +177,22 @@ void FormMain::updateAddItemMenu() {
m_ui->m_menuAddItem->clear(); m_ui->m_menuAddItem->clear();
foreach (ServiceRoot *activated_root, tabWidget()->feedMessageViewer()->feedsView()->sourceModel()->serviceRoots()) { 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);
if (root_menu == NULL) {
root_menu = new QMenu(activated_root->title(), m_ui->m_menuAddItem);
root_menu->setIcon(activated_root->icon()); root_menu->setIcon(activated_root->icon());
root_menu->setToolTip(activated_root->description()); root_menu->setToolTip(activated_root->description());
QList<QAction*> root_actions = activated_root->addItemMenu();
if (root_actions.isEmpty()) {
QAction *no_action = new QAction(qApp->icons()->fromTheme(QSL("dialog-error")), QAction *no_action = new QAction(qApp->icons()->fromTheme(QSL("dialog-error")),
tr("No possible actions"), tr("No possible actions"),
m_ui->m_menuAddItem); m_ui->m_menuAddItem);
no_action->setEnabled(false); no_action->setEnabled(false);
root_menu->addAction(no_action); root_menu->addAction(no_action);
} }
else {
root_menu->addActions(root_actions);
}
m_ui->m_menuAddItem->addMenu(root_menu); m_ui->m_menuAddItem->addMenu(root_menu);
} }

View File

@ -488,9 +488,6 @@ void FeedsView::initializeContextMenuCategories(RootItem *clicked_item) {
m_contextMenuCategories = new QMenu(tr("Context menu for categories"), this); m_contextMenuCategories = new QMenu(tr("Context menu for categories"), this);
} }
else { else {
// TODO: clear nevymaže z paměti.
// http://doc.qt.io/qt-4.8/qmenu.html#clear
m_contextMenuCategories->clear(); m_contextMenuCategories->clear();
} }
@ -506,11 +503,6 @@ void FeedsView::initializeContextMenuCategories(RootItem *clicked_item) {
if (!specific_actions.isEmpty()) { if (!specific_actions.isEmpty()) {
m_contextMenuCategories->addSeparator(); m_contextMenuCategories->addSeparator();
foreach (QAction *action, specific_actions) {
action->setParent(m_contextMenuCategories);
}
m_contextMenuCategories->addActions(specific_actions); m_contextMenuCategories->addActions(specific_actions);
} }
} }
@ -539,11 +531,6 @@ void FeedsView::initializeContextMenuFeeds(RootItem *clicked_item) {
if (!specific_actions.isEmpty()) { if (!specific_actions.isEmpty()) {
m_contextMenuFeeds->addSeparator(); m_contextMenuFeeds->addSeparator();
foreach (QAction *action, specific_actions) {
action->setParent(m_contextMenuFeeds);
}
m_contextMenuFeeds->addActions(specific_actions); m_contextMenuFeeds->addActions(specific_actions);
} }
} }

View File

@ -22,7 +22,7 @@
class FeedsModel; class FeedsModel;
class QMenu; class QAction;
// THIS IS the root node of the service. // THIS IS the root node of the service.
// NOTE: The root usually contains some core functionality of the // NOTE: The root usually contains some core functionality of the
@ -40,9 +40,7 @@ class ServiceRoot : public RootItem {
// b) Add new category // b) Add new category
// c) ... // c) ...
// NOTE: Caller does NOT take ownership of created menu! // NOTE: Caller does NOT take ownership of created menu!
virtual QMenu* addItemMenu() = 0; virtual QList<QAction*> addItemMenu() = 0;
// TODO: dodělat menu, které se zobrazi v menubaru "Services -> tato služba".
inline FeedsModel *feedsModel() const { inline FeedsModel *feedsModel() const {
return m_feedsModel; return m_feedsModel;

View File

@ -32,11 +32,12 @@
#include <QSqlError> #include <QSqlError>
#include <QStack> #include <QStack>
#include <QCoreApplication> #include <QCoreApplication>
#include <QMenu> #include <QAction>
StandardServiceRoot::StandardServiceRoot(bool load_from_db, FeedsModel *feeds_model, RootItem *parent) 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<QAction*>()), m_feedContextMenu(QList<QAction*>()), m_actionFeedFetchMetadata(NULL) {
m_title = qApp->system()->getUsername() + QL1S("@") + QL1S(APP_LOW_NAME); m_title = qApp->system()->getUsername() + QL1S("@") + QL1S(APP_LOW_NAME);
m_icon = StandardServiceEntryPoint().icon(); m_icon = StandardServiceEntryPoint().icon();
m_description = tr("This is obligatory service account for standard RSS/RDF/ATOM feeds."); 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() { StandardServiceRoot::~StandardServiceRoot() {
if (m_addItemMenu != NULL) { qDeleteAll(m_addItemMenu);
delete m_addItemMenu; qDeleteAll(m_feedContextMenu);
}
} }
bool StandardServiceRoot::canBeEdited() { bool StandardServiceRoot::canBeEdited() {
@ -216,14 +216,17 @@ QHash<int,StandardCategory*> StandardServiceRoot::allCategories() {
} }
QList<QAction*> StandardServiceRoot::getContextMenuForFeed(StandardFeed *feed) { QList<QAction*> StandardServiceRoot::getContextMenuForFeed(StandardFeed *feed) {
QList<QAction*> 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. // Make connections.
QAction *action_fetch_metadata = new QAction(qApp->icons()->fromTheme(QSL("download-manager")), tr("Fetch metadata"), NULL); disconnect(m_actionFeedFetchMetadata, SIGNAL(triggered()), 0, 0);
connect(action_fetch_metadata, SIGNAL(triggered()), feed, SLOT(fetchMetadataForItself())); connect(m_actionFeedFetchMetadata, SIGNAL(triggered()), feed, SLOT(fetchMetadataForItself()));
list.append(action_fetch_metadata); return m_feedContextMenu;
return list;
} }
void StandardServiceRoot::assembleFeeds(FeedAssignment feeds) { void StandardServiceRoot::assembleFeeds(FeedAssignment feeds) {
@ -328,14 +331,10 @@ bool StandardServiceRoot::mergeImportExportModel(FeedsImportExportModel *model,
return !some_feed_category_error; return !some_feed_category_error;
} }
QMenu *StandardServiceRoot::addItemMenu() { QList<QAction*> StandardServiceRoot::addItemMenu() {
if (m_addItemMenu == NULL) { if (m_addItemMenu.isEmpty()) {
m_addItemMenu = new QMenu(title(), NULL);
m_addItemMenu->setIcon(icon());
m_addItemMenu->setToolTip(description());
// TODO: Add items. // TODO: Add items.
m_addItemMenu->addAction(new QAction("abc", m_addItemMenu)); m_addItemMenu.append(new QAction("abc", this));
} }
return m_addItemMenu; return m_addItemMenu;

View File

@ -66,7 +66,7 @@ class StandardServiceRoot : public ServiceRoot {
bool mergeImportExportModel(FeedsImportExportModel *model, QString &output_message); bool mergeImportExportModel(FeedsImportExportModel *model, QString &output_message);
// Return "add feed" and "add category" items. // Return "add feed" and "add category" items.
QMenu *addItemMenu(); QList<QAction*> addItemMenu();
private: private:
void loadFromDatabase(); void loadFromDatabase();
@ -79,7 +79,10 @@ class StandardServiceRoot : public ServiceRoot {
StandardRecycleBin *m_recycleBin; StandardRecycleBin *m_recycleBin;
// Menus. // Menus.
QMenu *m_addItemMenu; QList<QAction*> m_addItemMenu;
QList<QAction*> m_feedContextMenu;
QAction *m_actionFeedFetchMetadata;
}; };
#endif // STANDARDSERVICEROOT_H #endif // STANDARDSERVICEROOT_H