ActionManager: transfer ownership from main window to core plugin

Also make a bit less dependent on main window:
- Menus do not need to start with main window as parent.
- Centering the presentation label on the main window is wrong in the
presence of extra windows anyhow. It should be centered on the active
window.

Unfortunately, actions still must be added to the main window, because
actions that are not children of visible widgets do not trigger.

Change-Id: Ibb99644a3723de476db465ebe6a9cdc0820ea692
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
Eike Ziller
2015-01-06 14:00:34 +01:00
parent dc86025879
commit e40477cc81
7 changed files with 39 additions and 29 deletions

View File

@@ -381,14 +381,16 @@ void ActionContainerPrivate::update()
*/ */
MenuActionContainer::MenuActionContainer(Id id) MenuActionContainer::MenuActionContainer(Id id)
: ActionContainerPrivate(id), m_menu(0) : ActionContainerPrivate(id),
m_menu(new QMenu)
{ {
m_menu->setObjectName(id.toString());
setOnAllDisabledBehavior(Disable); setOnAllDisabledBehavior(Disable);
} }
void MenuActionContainer::setMenu(QMenu *menu) MenuActionContainer::~MenuActionContainer()
{ {
m_menu = menu; delete m_menu;
} }
QMenu *MenuActionContainer::menu() const QMenu *MenuActionContainer::menu() const

View File

@@ -105,8 +105,8 @@ class MenuActionContainer : public ActionContainerPrivate
{ {
public: public:
explicit MenuActionContainer(Id id); explicit MenuActionContainer(Id id);
~MenuActionContainer();
void setMenu(QMenu *menu);
QMenu *menu() const; QMenu *menu() const;
void insertAction(QAction *before, QAction *action); void insertAction(QAction *before, QAction *action);
@@ -120,7 +120,7 @@ protected:
bool updateInternal(); bool updateInternal();
private: private:
QMenu *m_menu; QPointer<QMenu> m_menu;
}; };
class MenuBarActionContainer : public ActionContainerPrivate class MenuBarActionContainer : public ActionContainerPrivate

View File

@@ -32,17 +32,19 @@
#include "actionmanager_p.h" #include "actionmanager_p.h"
#include "actioncontainer_p.h" #include "actioncontainer_p.h"
#include "command_p.h" #include "command_p.h"
#include <coreplugin/id.h>
#include <coreplugin/mainwindow.h>
#include <coreplugin/icore.h>
#include <coreplugin/id.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QAction>
#include <QApplication>
#include <QDebug> #include <QDebug>
#include <QSettings> #include <QDesktopWidget>
#include <QLabel> #include <QLabel>
#include <QMenu> #include <QMenu>
#include <QAction>
#include <QMenuBar> #include <QMenuBar>
#include <QSettings>
namespace { namespace {
enum { warnAboutFindFailures = 0 }; enum { warnAboutFindFailures = 0 };
@@ -200,11 +202,7 @@ ActionContainer *ActionManager::createMenu(Id id)
if (it != d->m_idContainerMap.constEnd()) if (it != d->m_idContainerMap.constEnd())
return it.value(); return it.value();
QMenu *m = new QMenu(ICore::mainWindow());
m->setObjectName(QLatin1String(id.name()));
MenuActionContainer *mc = new MenuActionContainer(id); MenuActionContainer *mc = new MenuActionContainer(id);
mc->setMenu(m);
d->m_idContainerMap.insert(id, mc); d->m_idContainerMap.insert(id, mc);
connect(mc, SIGNAL(destroyed()), d, SLOT(containerDestroyed())); connect(mc, SIGNAL(destroyed()), d, SLOT(containerDestroyed()));
@@ -327,8 +325,8 @@ void ActionManager::unregisterAction(QAction *action, Id id)
a->removeOverrideAction(action); a->removeOverrideAction(action);
if (a->isEmpty()) { if (a->isEmpty()) {
// clean up // clean up
// ActionContainers listen to the commands' destroyed signals
ICore::mainWindow()->removeAction(a->action()); ICore::mainWindow()->removeAction(a->action());
// ActionContainers listen to the commands' destroyed signals
delete a->action(); delete a->action();
d->m_idCmdMap.remove(id); d->m_idCmdMap.remove(id);
delete a; delete a;
@@ -378,8 +376,9 @@ bool ActionManager::isPresentationModeEnabled()
return d->m_presentationLabel; return d->m_presentationLabel;
} }
void ActionManager::initialize() void ActionManager::initialize(QObject *parent)
{ {
new ActionManager(parent);
d->initialize(); d->initialize();
} }
@@ -463,7 +462,17 @@ void ActionManagerPrivate::showShortcutPopup(const QString &shortcut)
m_presentationLabel->setText(shortcut); m_presentationLabel->setText(shortcut);
m_presentationLabel->adjustSize(); m_presentationLabel->adjustSize();
QPoint p = ICore::mainWindow()->mapToGlobal(ICore::mainWindow()->rect().center() - m_presentationLabel->rect().center()); QWidget *window = QApplication::activeWindow();
if (!window && !QApplication::topLevelWidgets().isEmpty())
window = QApplication::topLevelWidgets().first();
QPoint center;
if (window) {
center = window->mapToGlobal(window->rect().center());
} else {
QTC_ASSERT(QApplication::desktop(), return);
center = QApplication::desktop()->screenGeometry().center();
}
QPoint p = center - m_presentationLabel->rect().center();
m_presentationLabel->move(p); m_presentationLabel->move(p);
m_presentationLabel->show(); m_presentationLabel->show();

View File

@@ -48,7 +48,10 @@ namespace Core {
class ActionContainer; class ActionContainer;
namespace Internal { class MainWindow; } namespace Internal {
class CorePlugin;
class MainWindow;
} // Internal
class CORE_EXPORT ActionManager : public QObject class CORE_EXPORT ActionManager : public QObject
{ {
@@ -78,11 +81,12 @@ signals:
private: private:
ActionManager(QObject *parent = 0); ActionManager(QObject *parent = 0);
~ActionManager(); ~ActionManager();
static void initialize(); static void initialize(QObject *parent);
void saveSettings(QSettings *settings); static void saveSettings(QSettings *settings);
void setContext(const Context &context); static void setContext(const Context &context);
friend class Core::Internal::MainWindow; friend class Core::Internal::CorePlugin; // initialization
friend class Core::Internal::MainWindow; // saving settings and setting context
}; };
} // namespace Core } // namespace Core

View File

@@ -167,6 +167,7 @@ void CorePlugin::parseArguments(const QStringList &arguments)
bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage) bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage)
{ {
ActionManager::initialize(this);
Theme::initialPalette(); // Initialize palette before setting it Theme::initialPalette(); // Initialize palette before setting it
qsrand(QDateTime::currentDateTime().toTime_t()); qsrand(QDateTime::currentDateTime().toTime_t());
parseArguments(arguments); parseArguments(arguments);

View File

@@ -117,7 +117,6 @@ MainWindow::MainWindow() :
this)), this)),
m_printer(0), m_printer(0),
m_windowSupport(0), m_windowSupport(0),
m_actionManager(new ActionManager(this)),
m_editorManager(0), m_editorManager(0),
m_externalToolManager(0), m_externalToolManager(0),
m_progressManager(new ProgressManagerPrivate), m_progressManager(new ProgressManagerPrivate),
@@ -146,8 +145,6 @@ MainWindow::MainWindow() :
m_toggleSideBarAction(0), m_toggleSideBarAction(0),
m_toggleSideBarButton(new QToolButton) m_toggleSideBarButton(new QToolButton)
{ {
ActionManager::initialize(); // must be done before registering any actions
(void) new DocumentManager(this); (void) new DocumentManager(this);
OutputPaneManager::create(); OutputPaneManager::create();
@@ -1000,7 +997,7 @@ void MainWindow::writeSettings()
settings->endGroup(); settings->endGroup();
DocumentManager::saveSettings(); DocumentManager::saveSettings();
m_actionManager->saveSettings(settings); ActionManager::saveSettings(settings);
EditorManagerPrivate::saveSettings(); EditorManagerPrivate::saveSettings();
m_navigationWidget->saveSettings(settings); m_navigationWidget->saveSettings(settings);
} }
@@ -1043,7 +1040,7 @@ void MainWindow::updateContext()
uniquecontexts.add(id); uniquecontexts.add(id);
} }
m_actionManager->setContext(uniquecontexts); ActionManager::setContext(uniquecontexts);
emit m_coreImpl->contextChanged(m_activeContext, m_additionalContexts); emit m_coreImpl->contextChanged(m_activeContext, m_additionalContexts);
} }

View File

@@ -49,7 +49,6 @@ QT_END_NAMESPACE
namespace Core { namespace Core {
class ActionManager;
class StatusBarWidget; class StatusBarWidget;
class EditorManager; class EditorManager;
class ExternalToolManager; class ExternalToolManager;
@@ -69,7 +68,6 @@ class VcsManager;
namespace Internal { namespace Internal {
class ActionManagerPrivate;
class FancyTabWidget; class FancyTabWidget;
class GeneralSettings; class GeneralSettings;
class ProgressManagerPrivate; class ProgressManagerPrivate;
@@ -168,7 +166,6 @@ private:
SettingsDatabase *m_settingsDatabase; SettingsDatabase *m_settingsDatabase;
mutable QPrinter *m_printer; mutable QPrinter *m_printer;
WindowSupport *m_windowSupport; WindowSupport *m_windowSupport;
ActionManager *m_actionManager;
EditorManager *m_editorManager; EditorManager *m_editorManager;
ExternalToolManager *m_externalToolManager; ExternalToolManager *m_externalToolManager;
MessageManager *m_messageManager; MessageManager *m_messageManager;