2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2014-07-17 17:04:02 +02:00
|
|
|
|
|
|
|
|
#include "windowsupport.h"
|
|
|
|
|
|
2014-08-25 17:28:25 +02:00
|
|
|
#include "actionmanager/actioncontainer.h"
|
2014-07-17 17:04:02 +02:00
|
|
|
#include "actionmanager/actionmanager.h"
|
2015-02-26 13:38:54 +01:00
|
|
|
#include "actionmanager/command.h"
|
2014-07-17 17:04:02 +02:00
|
|
|
#include "coreconstants.h"
|
|
|
|
|
#include "icore.h"
|
|
|
|
|
|
2017-08-29 11:48:48 +02:00
|
|
|
#include <app/app_version.h>
|
2014-07-17 17:04:02 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2014-08-25 17:28:25 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2018-03-26 10:44:10 +02:00
|
|
|
#include <utils/stringutils.h>
|
2014-07-17 17:04:02 +02:00
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QEvent>
|
2014-08-25 17:28:25 +02:00
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QWidget>
|
2019-09-24 11:35:46 +02:00
|
|
|
#include <QWindowStateChangeEvent>
|
2014-07-17 17:04:02 +02:00
|
|
|
|
2020-06-26 13:59:38 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2014-07-17 17:04:02 +02:00
|
|
|
namespace Core {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-11-14 16:18:40 +01:00
|
|
|
Q_GLOBAL_STATIC(WindowList, m_windowList)
|
2014-08-25 17:28:25 +02:00
|
|
|
|
2014-07-17 17:04:02 +02:00
|
|
|
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);
|
|
|
|
|
|
2018-02-02 13:39:18 +01:00
|
|
|
if (useMacShortcuts) {
|
2014-07-17 17:04:02 +02:00
|
|
|
m_minimizeAction = new QAction(this);
|
|
|
|
|
ActionManager::registerAction(m_minimizeAction, Constants::MINIMIZE_WINDOW, context);
|
2016-02-02 09:10:54 +02:00
|
|
|
connect(m_minimizeAction, &QAction::triggered, m_window, &QWidget::showMinimized);
|
2014-07-17 17:04:02 +02:00
|
|
|
|
|
|
|
|
m_zoomAction = new QAction(this);
|
|
|
|
|
ActionManager::registerAction(m_zoomAction, Constants::ZOOM_WINDOW, context);
|
2020-08-18 10:32:11 +02:00
|
|
|
connect(m_zoomAction, &QAction::triggered, m_window, [this] {
|
|
|
|
|
if (m_window->isMaximized()) {
|
|
|
|
|
// similar to QWidget::showMaximized
|
|
|
|
|
m_window->ensurePolished();
|
|
|
|
|
m_window->setWindowState(m_window->windowState() & ~Qt::WindowMaximized);
|
|
|
|
|
m_window->setVisible(true);
|
|
|
|
|
} else {
|
|
|
|
|
m_window->showMaximized();
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-07-18 10:35:17 +02:00
|
|
|
|
|
|
|
|
m_closeAction = new QAction(this);
|
|
|
|
|
ActionManager::registerAction(m_closeAction, Constants::CLOSE_WINDOW, context);
|
2016-02-02 09:10:54 +02:00
|
|
|
connect(m_closeAction, &QAction::triggered, m_window, &QWidget::close, Qt::QueuedConnection);
|
2014-07-17 17:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_toggleFullScreenAction = new QAction(this);
|
|
|
|
|
updateFullScreenAction();
|
|
|
|
|
ActionManager::registerAction(m_toggleFullScreenAction, Constants::TOGGLE_FULLSCREEN, context);
|
2016-02-02 09:10:54 +02:00
|
|
|
connect(m_toggleFullScreenAction, &QAction::triggered, this, &WindowSupport::toggleFullScreen);
|
2014-08-25 17:28:25 +02:00
|
|
|
|
2018-11-14 16:18:40 +01:00
|
|
|
m_windowList->addWindow(window);
|
2015-06-04 16:36:45 +02:00
|
|
|
|
2022-07-19 22:37:03 +02:00
|
|
|
connect(ICore::instance(), &ICore::coreAboutToClose, this, [this] { m_shutdown = true; });
|
2014-07-17 17:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WindowSupport::~WindowSupport()
|
|
|
|
|
{
|
2015-06-04 16:36:45 +02:00
|
|
|
if (!m_shutdown) { // don't update all that stuff if we are shutting down anyhow
|
2018-02-02 13:39:18 +01:00
|
|
|
if (useMacShortcuts) {
|
2015-06-04 16:36:45 +02:00
|
|
|
ActionManager::unregisterAction(m_minimizeAction, Constants::MINIMIZE_WINDOW);
|
|
|
|
|
ActionManager::unregisterAction(m_zoomAction, Constants::ZOOM_WINDOW);
|
|
|
|
|
ActionManager::unregisterAction(m_closeAction, Constants::CLOSE_WINDOW);
|
|
|
|
|
}
|
|
|
|
|
ActionManager::unregisterAction(m_toggleFullScreenAction, Constants::TOGGLE_FULLSCREEN);
|
2018-11-14 16:18:40 +01:00
|
|
|
m_windowList->removeWindow(m_window);
|
2014-08-25 11:41:41 +02:00
|
|
|
}
|
2014-07-17 17:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-18 10:35:17 +02:00
|
|
|
void WindowSupport::setCloseActionEnabled(bool enabled)
|
|
|
|
|
{
|
2018-02-02 13:39:18 +01:00
|
|
|
if (useMacShortcuts)
|
2014-07-18 10:35:17 +02:00
|
|
|
m_closeAction->setEnabled(enabled);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-17 17:04:02 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2019-09-24 11:35:46 +02:00
|
|
|
m_previousWindowState = static_cast<QWindowStateChangeEvent *>(event)->oldState();
|
2014-07-17 17:04:02 +02:00
|
|
|
updateFullScreenAction();
|
2014-08-25 17:28:25 +02:00
|
|
|
} else if (event->type() == QEvent::WindowActivate) {
|
2018-11-14 16:18:40 +01:00
|
|
|
m_windowList->setActiveWindow(m_window);
|
2015-01-05 11:12:39 +01:00
|
|
|
} else if (event->type() == QEvent::Hide) {
|
|
|
|
|
// minimized windows are hidden, but we still want to show them
|
2018-11-14 16:18:40 +01:00
|
|
|
m_windowList->setWindowVisible(m_window, m_window->isMinimized());
|
2015-01-05 11:12:39 +01:00
|
|
|
} else if (event->type() == QEvent::Show) {
|
2018-11-14 16:18:40 +01:00
|
|
|
m_windowList->setWindowVisible(m_window, true);
|
2014-07-17 17:04:02 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowSupport::toggleFullScreen()
|
|
|
|
|
{
|
|
|
|
|
if (m_window->isFullScreen()) {
|
2019-09-24 11:35:46 +02:00
|
|
|
m_window->setWindowState(m_previousWindowState & ~Qt::WindowFullScreen);
|
2014-07-17 17:04:02 +02:00
|
|
|
} 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 16:18:40 +01:00
|
|
|
WindowList::~WindowList()
|
|
|
|
|
{
|
|
|
|
|
qDeleteAll(m_windowActions);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 17:28:25 +02:00
|
|
|
void WindowList::addWindow(QWidget *window)
|
|
|
|
|
{
|
2014-09-01 12:17:03 +04:00
|
|
|
#ifdef Q_OS_OSX
|
|
|
|
|
if (!m_dockMenu) {
|
2014-08-25 17:28:25 +02:00
|
|
|
m_dockMenu = new QMenu;
|
|
|
|
|
m_dockMenu->setAsDockMenu();
|
|
|
|
|
}
|
2014-09-01 12:17:03 +04:00
|
|
|
#endif
|
2014-08-25 17:28:25 +02:00
|
|
|
|
|
|
|
|
m_windows.append(window);
|
|
|
|
|
Id id = Id("QtCreator.Window.").withSuffix(m_windows.size());
|
|
|
|
|
m_windowActionIds.append(id);
|
2018-11-14 16:18:40 +01:00
|
|
|
auto action = new QAction(window->windowTitle());
|
2014-08-25 17:28:25 +02:00
|
|
|
m_windowActions.append(action);
|
2018-11-14 16:18:40 +01:00
|
|
|
QObject::connect(action, &QAction::triggered, [action, this]() { activateWindow(action); });
|
2014-08-25 17:28:25 +02:00
|
|
|
action->setCheckable(true);
|
|
|
|
|
action->setChecked(false);
|
2015-02-19 11:35:47 +01:00
|
|
|
Command *cmd = ActionManager::registerAction(action, id);
|
2014-08-25 17:28:25 +02:00
|
|
|
cmd->setAttribute(Command::CA_UpdateText);
|
|
|
|
|
ActionManager::actionContainer(Constants::M_WINDOW)->addAction(cmd, Constants::G_WINDOW_LIST);
|
2015-01-05 11:12:39 +01:00
|
|
|
action->setVisible(window->isVisible() || window->isMinimized()); // minimized windows are hidden but should be shown
|
2018-11-14 16:18:40 +01:00
|
|
|
QObject::connect(window, &QWidget::windowTitleChanged, [window, this]() { updateTitle(window); });
|
2014-08-25 17:28:25 +02:00
|
|
|
if (m_dockMenu)
|
|
|
|
|
m_dockMenu->addAction(action);
|
|
|
|
|
if (window->isActiveWindow())
|
|
|
|
|
setActiveWindow(window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowList::activateWindow(QAction *action)
|
|
|
|
|
{
|
|
|
|
|
int index = m_windowActions.indexOf(action);
|
|
|
|
|
QTC_ASSERT(index >= 0, return);
|
|
|
|
|
QTC_ASSERT(index < m_windows.size(), return);
|
|
|
|
|
ICore::raiseWindow(m_windows.at(index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowList::updateTitle(QWidget *window)
|
|
|
|
|
{
|
|
|
|
|
int index = m_windows.indexOf(window);
|
|
|
|
|
QTC_ASSERT(index >= 0, return);
|
|
|
|
|
QTC_ASSERT(index < m_windowActions.size(), return);
|
|
|
|
|
QString title = window->windowTitle();
|
2017-08-29 11:48:48 +02:00
|
|
|
if (title.endsWith(QStringLiteral("- ") + Constants::IDE_DISPLAY_NAME))
|
2014-08-25 17:28:25 +02:00
|
|
|
title.chop(12);
|
2018-03-26 10:44:10 +02:00
|
|
|
m_windowActions.at(index)->setText(Utils::quoteAmpersands(title.trimmed()));
|
2014-08-25 17:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowList::removeWindow(QWidget *window)
|
|
|
|
|
{
|
|
|
|
|
// remove window from list,
|
|
|
|
|
// remove last action from menu(s)
|
|
|
|
|
// and update all action titles, starting with the index where the window was
|
|
|
|
|
int index = m_windows.indexOf(window);
|
|
|
|
|
QTC_ASSERT(index >= 0, return);
|
|
|
|
|
|
|
|
|
|
ActionManager::unregisterAction(m_windowActions.last(), m_windowActionIds.last());
|
2014-11-21 10:01:11 +02:00
|
|
|
delete m_windowActions.takeLast();
|
2014-08-25 17:28:25 +02:00
|
|
|
m_windowActionIds.removeLast();
|
|
|
|
|
|
|
|
|
|
m_windows.removeOne(window);
|
|
|
|
|
|
|
|
|
|
for (int i = index; i < m_windows.size(); ++i)
|
|
|
|
|
updateTitle(m_windows.at(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowList::setActiveWindow(QWidget *window)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_windows.size(); ++i)
|
|
|
|
|
m_windowActions.at(i)->setChecked(m_windows.at(i) == window);
|
|
|
|
|
}
|
2014-07-17 17:04:02 +02:00
|
|
|
|
2015-01-05 11:12:39 +01:00
|
|
|
void WindowList::setWindowVisible(QWidget *window, bool visible)
|
|
|
|
|
{
|
|
|
|
|
int index = m_windows.indexOf(window);
|
|
|
|
|
QTC_ASSERT(index >= 0, return);
|
|
|
|
|
QTC_ASSERT(index < m_windowActions.size(), return);
|
|
|
|
|
m_windowActions.at(index)->setVisible(visible);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-17 17:04:02 +02:00
|
|
|
} // Internal
|
|
|
|
|
} // Core
|