Core: Refactor handling of "window state" actions out of MainWindow

Currently the actions for fullscreen, minimize and zoom only apply to
the main window, even if a different window is active.
Refactor the handling of these actions into a WindowSupport class, and
use that instead for the main window. In a second step, this will be
used to add the functionality to the corresponding external windows
(e.g. help and editor windows)

Change-Id: Ief2c880f40948c3bb724196d6e0cfe888b8ece89
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
Eike Ziller
2014-07-17 17:04:02 +02:00
parent 60d7b23d88
commit fe5b3a39e8
7 changed files with 222 additions and 56 deletions

View File

@@ -100,7 +100,8 @@ SOURCES += mainwindow.cpp \
dialogs/addtovcsdialog.cpp \ dialogs/addtovcsdialog.cpp \
icorelistener.cpp \ icorelistener.cpp \
ioutputpane.cpp \ ioutputpane.cpp \
patchtool.cpp patchtool.cpp \
windowsupport.cpp
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
editmode.h \ editmode.h \
@@ -199,7 +200,8 @@ HEADERS += mainwindow.h \
documentmanager.h \ documentmanager.h \
removefiledialog.h \ removefiledialog.h \
dialogs/addtovcsdialog.h \ dialogs/addtovcsdialog.h \
patchtool.h patchtool.h \
windowsupport.h
FORMS += dialogs/newdialog.ui \ FORMS += dialogs/newdialog.ui \
dialogs/saveitemsdialog.ui \ dialogs/saveitemsdialog.ui \

View File

@@ -28,6 +28,7 @@
****************************************************************************/ ****************************************************************************/
#include "icore.h" #include "icore.h"
#include "windowsupport.h"
#include <app/app_version.h> #include <app/app_version.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
@@ -523,6 +524,11 @@ void ICore::removeContextObject(IContext *context)
m_mainwindow->removeContextObject(context); m_mainwindow->removeContextObject(context);
} }
void ICore::registerWindow(QWidget *window, const Context &context)
{
new WindowSupport(window, context); // deletes itself when widget is destroyed
}
void ICore::openFiles(const QStringList &arguments, ICore::OpenFilesFlags flags) void ICore::openFiles(const QStringList &arguments, ICore::OpenFilesFlags flags)
{ {
m_mainwindow->openFiles(arguments, flags); m_mainwindow->openFiles(arguments, flags);

View File

@@ -109,6 +109,9 @@ public:
static void addContextObject(IContext *context); static void addContextObject(IContext *context);
static void removeContextObject(IContext *context); static void removeContextObject(IContext *context);
// manages the minimize, zoom and fullscreen actions for the window
static void registerWindow(QWidget *window, const Context &context);
enum OpenFilesFlags { enum OpenFilesFlags {
None = 0, None = 0,
SwitchMode = 1, SwitchMode = 1,

View File

@@ -54,6 +54,7 @@
#include "statusbarwidget.h" #include "statusbarwidget.h"
#include "externaltoolmanager.h" #include "externaltoolmanager.h"
#include "editormanager/systemeditor.h" #include "editormanager/systemeditor.h"
#include "windowsupport.h"
#include <app/app_version.h> #include <app/app_version.h>
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
@@ -111,6 +112,7 @@ MainWindow::MainWindow() :
QLatin1String("QtCreator"), QLatin1String("QtCreator"),
this)), this)),
m_printer(0), m_printer(0),
m_windowSupport(0),
m_actionManager(new ActionManager(this)), m_actionManager(new ActionManager(this)),
m_editorManager(0), m_editorManager(0),
m_externalToolManager(0), m_externalToolManager(0),
@@ -138,9 +140,6 @@ MainWindow::MainWindow() :
m_exitAction(0), m_exitAction(0),
m_optionsAction(0), m_optionsAction(0),
m_toggleSideBarAction(0), m_toggleSideBarAction(0),
m_toggleFullScreenAction(0),
m_minimizeAction(0),
m_zoomAction(0),
m_toggleSideBarButton(new QToolButton) m_toggleSideBarButton(new QToolButton)
{ {
ActionManager::initialize(); // must be done before registering any actions ActionManager::initialize(); // must be done before registering any actions
@@ -236,21 +235,6 @@ void MainWindow::setOverrideColor(const QColor &color)
m_overrideColor = color; m_overrideColor = color;
} }
void MainWindow::updateFullScreenAction()
{
if (isFullScreen()) {
if (Utils::HostOsInfo::isMacHost())
m_toggleFullScreenAction->setText(tr("Exit Full Screen"));
else
m_toggleFullScreenAction->setChecked(true);
} else {
if (Utils::HostOsInfo::isMacHost())
m_toggleFullScreenAction->setText(tr("Enter Full Screen"));
else
m_toggleFullScreenAction->setChecked(false);
}
}
bool MainWindow::isNewItemDialogRunning() const bool MainWindow::isNewItemDialogRunning() const
{ {
return !m_newDialog.isNull(); return !m_newDialog.isNull();
@@ -345,6 +329,7 @@ bool MainWindow::init(QString *errorMessage)
void MainWindow::extensionsInitialized() void MainWindow::extensionsInitialized()
{ {
m_windowSupport = new WindowSupport(this, Context("Core.MainWindow"));
m_editorManager->init(); m_editorManager->init();
m_statusBarManager->extensionsInitalized(); m_statusBarManager->extensionsInitalized();
OutputPaneManager::instance()->init(); OutputPaneManager::instance()->init();
@@ -386,6 +371,11 @@ void MainWindow::closeEvent(QCloseEvent *event)
m_navigationWidget->closeSubWidgets(); m_navigationWidget->closeSubWidgets();
event->accept(); event->accept();
// explicitly delete window support, because that calls methods from ICore that call methods
// from mainwindow, so mainwindow still needs to be alive
delete m_windowSupport;
m_windowSupport = 0;
} }
void MainWindow::openDroppedFiles(const QStringList &files) void MainWindow::openDroppedFiles(const QStringList &files)
@@ -640,30 +630,29 @@ void MainWindow::registerDefaultActions()
if (UseMacShortcuts) { if (UseMacShortcuts) {
// Minimize Action // Minimize Action
m_minimizeAction = new QAction(tr("Minimize"), this); QAction *minimizeAction = new QAction(tr("Minimize"), this);
cmd = ActionManager::registerAction(m_minimizeAction, Constants::MINIMIZE_WINDOW, globalContext); minimizeAction->setEnabled(false); // actual implementation in WindowSupport
cmd = ActionManager::registerAction(minimizeAction, Constants::MINIMIZE_WINDOW, globalContext);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+M"))); cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+M")));
mwindow->addAction(cmd, Constants::G_WINDOW_SIZE); mwindow->addAction(cmd, Constants::G_WINDOW_SIZE);
connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(showMinimized()));
// Zoom Action // Zoom Action
m_zoomAction = new QAction(tr("Zoom"), this); QAction *zoomAction = new QAction(tr("Zoom"), this);
cmd = ActionManager::registerAction(m_zoomAction, Constants::ZOOM_WINDOW, globalContext); zoomAction->setEnabled(false); // actual implementation in WindowSupport
cmd = ActionManager::registerAction(zoomAction, Constants::ZOOM_WINDOW, globalContext);
mwindow->addAction(cmd, Constants::G_WINDOW_SIZE); mwindow->addAction(cmd, Constants::G_WINDOW_SIZE);
connect(m_zoomAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
} }
// Full Screen Action // Full Screen Action
m_toggleFullScreenAction = new QAction(tr("Full Screen"), this); QAction *toggleFullScreenAction = new QAction(tr("Full Screen"), this);
m_toggleFullScreenAction->setMenuRole(QAction::NoRole); toggleFullScreenAction->setMenuRole(QAction::NoRole);
m_toggleFullScreenAction->setCheckable(!Utils::HostOsInfo::isMacHost()); toggleFullScreenAction->setCheckable(!Utils::HostOsInfo::isMacHost());
updateFullScreenAction(); toggleFullScreenAction->setEnabled(false); // actual implementation in WindowSupport
cmd = ActionManager::registerAction(m_toggleFullScreenAction, Constants::TOGGLE_FULLSCREEN, globalContext); cmd = ActionManager::registerAction(toggleFullScreenAction, Constants::TOGGLE_FULLSCREEN, globalContext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Ctrl+Meta+F") : tr("Ctrl+Shift+F11"))); cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Ctrl+Meta+F") : tr("Ctrl+Shift+F11")));
if (Utils::HostOsInfo::isMacHost()) if (Utils::HostOsInfo::isMacHost())
cmd->setAttribute(Command::CA_UpdateText); cmd->setAttribute(Command::CA_UpdateText);
mwindow->addAction(cmd, Constants::G_WINDOW_SIZE); mwindow->addAction(cmd, Constants::G_WINDOW_SIZE);
connect(m_toggleFullScreenAction, SIGNAL(triggered()), this, SLOT(toggleFullScreen()));
if (UseMacShortcuts) if (UseMacShortcuts)
mwindow->addSeparator(globalContext, Constants::G_WINDOW_SIZE); mwindow->addSeparator(globalContext, Constants::G_WINDOW_SIZE);
@@ -906,15 +895,6 @@ void MainWindow::changeEvent(QEvent *e)
qDebug() << "main window activated"; qDebug() << "main window activated";
emit windowActivated(); emit windowActivated();
} }
} else if (e->type() == QEvent::WindowStateChange) {
if (Utils::HostOsInfo::isMacHost()) {
bool minimized = isMinimized();
if (debugMainWindow)
qDebug() << "main window state changed to minimized=" << minimized;
m_minimizeAction->setEnabled(!minimized);
m_zoomAction->setEnabled(!minimized);
}
updateFullScreenAction();
} }
} }
@@ -1121,15 +1101,6 @@ QPrinter *MainWindow::printer() const
return m_printer; return m_printer;
} }
void MainWindow::toggleFullScreen()
{
if (isFullScreen()) {
setWindowState(windowState() & ~Qt::WindowFullScreen);
} else {
setWindowState(windowState() | Qt::WindowFullScreen);
}
}
// Display a warning with an additional button to open // Display a warning with an additional button to open
// the debugger settings dialog if settingsId is nonempty. // the debugger settings dialog if settingsId is nonempty.

View File

@@ -76,6 +76,7 @@ class ToolSettings;
class MimeTypeSettings; class MimeTypeSettings;
class StatusBarManager; class StatusBarManager;
class VersionDialog; class VersionDialog;
class WindowSupport;
class SystemEditor; class SystemEditor;
class MainWindow : public Utils::AppMainWindow class MainWindow : public Utils::AppMainWindow
@@ -107,8 +108,6 @@ public:
void setOverrideColor(const QColor &color); void setOverrideColor(const QColor &color);
void updateFullScreenAction();
bool isNewItemDialogRunning() const; bool isNewItemDialogRunning() const;
signals: signals:
@@ -119,7 +118,6 @@ public slots:
void newFile(); void newFile();
void openFileWith(); void openFileWith();
void exit(); void exit();
void toggleFullScreen();
void showNewItemDialog(const QString &title, void showNewItemDialog(const QString &title,
const QList<IWizardFactory *> &factories, const QList<IWizardFactory *> &factories,
@@ -167,6 +165,7 @@ private:
Context m_additionalContexts; Context m_additionalContexts;
SettingsDatabase *m_settingsDatabase; SettingsDatabase *m_settingsDatabase;
mutable QPrinter *m_printer; mutable QPrinter *m_printer;
WindowSupport *m_windowSupport;
ActionManager *m_actionManager; ActionManager *m_actionManager;
EditorManager *m_editorManager; EditorManager *m_editorManager;
ExternalToolManager *m_externalToolManager; ExternalToolManager *m_externalToolManager;
@@ -205,9 +204,6 @@ private:
QAction *m_optionsAction; QAction *m_optionsAction;
QAction *m_toggleSideBarAction; QAction *m_toggleSideBarAction;
QAction *m_toggleModeSelectorAction; QAction *m_toggleModeSelectorAction;
QAction *m_toggleFullScreenAction;
QAction *m_minimizeAction;
QAction *m_zoomAction;
QToolButton *m_toggleSideBarButton; QToolButton *m_toggleSideBarButton;
QColor m_overrideColor; QColor m_overrideColor;

View File

@@ -0,0 +1,118 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "windowsupport.h"
#include "actionmanager/actionmanager.h"
#include "coreconstants.h"
#include "icore.h"
#include <utils/hostosinfo.h>
#include <QAction>
#include <QWidget>
#include <QEvent>
namespace Core {
namespace Internal {
WindowSupport::WindowSupport(QWidget *window, const Context &context)
: QObject(window),
m_window(window)
{
m_window->installEventFilter(this);
m_contextObject = new IContext(this);
m_contextObject->setWidget(window);
m_contextObject->setContext(context);
ICore::addContextObject(m_contextObject);
if (UseMacShortcuts) {
m_minimizeAction = new QAction(this);
ActionManager::registerAction(m_minimizeAction, Constants::MINIMIZE_WINDOW, context);
connect(m_minimizeAction, SIGNAL(triggered()), m_window, SLOT(showMinimized()));
m_zoomAction = new QAction(this);
ActionManager::registerAction(m_zoomAction, Constants::ZOOM_WINDOW, context);
connect(m_zoomAction, SIGNAL(triggered()), m_window, SLOT(showMaximized()));
}
m_toggleFullScreenAction = new QAction(this);
updateFullScreenAction();
ActionManager::registerAction(m_toggleFullScreenAction, Constants::TOGGLE_FULLSCREEN, context);
connect(m_toggleFullScreenAction, SIGNAL(triggered()), this, SLOT(toggleFullScreen()));
}
WindowSupport::~WindowSupport()
{
ICore::removeContextObject(m_contextObject);
}
bool WindowSupport::eventFilter(QObject *obj, QEvent *event)
{
if (obj != m_window)
return false;
if (event->type() == QEvent::WindowStateChange) {
if (Utils::HostOsInfo::isMacHost()) {
bool minimized = m_window->isMinimized();
m_minimizeAction->setEnabled(!minimized);
m_zoomAction->setEnabled(!minimized);
}
updateFullScreenAction();
}
return false;
}
void WindowSupport::toggleFullScreen()
{
if (m_window->isFullScreen()) {
m_window->setWindowState(m_window->windowState() & ~Qt::WindowFullScreen);
} else {
m_window->setWindowState(m_window->windowState() | Qt::WindowFullScreen);
}
}
void WindowSupport::updateFullScreenAction()
{
if (m_window->isFullScreen()) {
if (Utils::HostOsInfo::isMacHost())
m_toggleFullScreenAction->setText(tr("Exit Full Screen"));
else
m_toggleFullScreenAction->setChecked(true);
} else {
if (Utils::HostOsInfo::isMacHost())
m_toggleFullScreenAction->setText(tr("Enter Full Screen"));
else
m_toggleFullScreenAction->setChecked(false);
}
}
} // Internal
} // Core

View File

@@ -0,0 +1,70 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef WINDOWSUPPORT_H
#define WINDOWSUPPORT_H
#include "icontext.h"
#include <QObject>
QT_BEGIN_NAMESPACE
class QAction;
class QWidget;
QT_END_NAMESPACE
namespace Core {
namespace Internal {
class WindowSupport : public QObject
{
Q_OBJECT
public:
WindowSupport(QWidget *window, const Context &context);
~WindowSupport();
protected:
bool eventFilter(QObject *obj, QEvent *event);
private slots:
void toggleFullScreen();
void updateFullScreenAction();
private:
QWidget *m_window;
IContext *m_contextObject;
QAction *m_minimizeAction;
QAction *m_zoomAction;
QAction *m_toggleFullScreenAction;
};
} // Internal
} // Core
#endif // WINDOWSUPPORT_H