refs #347, CMainWindow for frameless main windows

This commit is contained in:
Klaus Basan
2014-11-22 18:51:00 +01:00
committed by Roland Winklmeier
parent facbefeeea
commit 42a4e0b48b
15 changed files with 266 additions and 112 deletions

View File

@@ -0,0 +1,116 @@
/* Copyright (C) 2014
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "enableforframelesswindow.h"
#include "blackmisc/icons.h"
#include <QSizeGrip>
#include <QStatusBar>
#include <QPushButton>
using namespace BlackMisc;
namespace BlackGui
{
CEnableForFramelessWindow::CEnableForFramelessWindow(CEnableForFramelessWindow::WindowMode mode, bool isMainApplicationWindow, QWidget *correspondingWidget) :
m_windowMode(mode), m_mainApplicationWindow(isMainApplicationWindow), m_widget(correspondingWidget)
{
Q_ASSERT(correspondingWidget);
this->setWindowAttributes(mode);
}
void CEnableForFramelessWindow::setMode(CEnableForFramelessWindow::WindowMode mode)
{
if (mode == this->m_windowMode) { return; }
this->m_widget->setWindowFlags(modeToWindowFlags(mode));
this->setWindowAttributes(mode);
this->m_widget->show();
this->m_windowMode = mode;
}
void CEnableForFramelessWindow::setFrameless(bool frameless)
{
setMode(frameless ? WindowFrameless : WindowNormal);
}
void CEnableForFramelessWindow::setWindowAttributes(CEnableForFramelessWindow::WindowMode mode)
{
bool frameless = (mode == WindowFrameless);
// http://stackoverflow.com/questions/18316710/frameless-and-transparent-window-qt5
this->m_widget->setAttribute(Qt::WA_NoSystemBackground, frameless);
this->m_widget->setAttribute(Qt::WA_TranslucentBackground, frameless);
this->m_widget->setProperty("frameless", frameless);
}
bool CEnableForFramelessWindow::handleMouseMoveEvent(QMouseEvent *event)
{
Q_ASSERT(this->m_widget);
if (this->m_windowMode == WindowFrameless && event->buttons() & Qt::LeftButton)
{
this->m_widget->move(event->globalPos() - this->m_framelessDragPosition);
event->accept();
return true;
}
return false;
}
bool CEnableForFramelessWindow::handleMousePressEvent(QMouseEvent *event)
{
Q_ASSERT(this->m_widget);
if (this->m_windowMode == WindowFrameless && event->button() == Qt::LeftButton)
{
this->m_framelessDragPosition = event->globalPos() - this->m_widget->frameGeometry().topLeft();
event->accept();
return true;
}
return false;
}
void CEnableForFramelessWindow::addFramelessSizeGrip(QStatusBar *statusBar)
{
if (!statusBar) { return; }
QSizeGrip *grip = new QSizeGrip(this->m_widget);
grip->setObjectName("sg_FramelessSizeGrip");
statusBar->addPermanentWidget(grip);
}
QHBoxLayout *CEnableForFramelessWindow::addFramelessCloseButton(QMenuBar *menuBar)
{
Q_ASSERT(isFrameless());
Q_ASSERT(menuBar);
Q_ASSERT(this->m_widget);
if (!m_framelessCloseButton)
{
m_framelessCloseButton = new QPushButton(this->m_widget);
m_framelessCloseButton->setObjectName("pb_FramelessCloseButton");
m_framelessCloseButton->setIcon(CIcons::close16());
QObject::connect(m_framelessCloseButton, &QPushButton::clicked, this->m_widget, &QWidget::close);
}
QHBoxLayout *menuBarLayout = new QHBoxLayout;
menuBarLayout->setObjectName("hl_MenuBar");
menuBarLayout->addWidget(menuBar, 0, Qt::AlignTop | Qt::AlignLeft);
menuBarLayout->addWidget(m_framelessCloseButton, 0, Qt::AlignTop | Qt::AlignRight);
return menuBarLayout;
}
Qt::WindowFlags CEnableForFramelessWindow::modeToWindowFlags(CEnableForFramelessWindow::WindowMode mode)
{
switch (mode)
{
case WindowFrameless:
return (Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
case WindowNormal:
default:
return (Qt::Tool | Qt::WindowStaysOnTopHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
}
}
} // namespace

View File

@@ -0,0 +1,85 @@
/* Copyright (C) 2014
* swift project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_ENABLEFORFRAMLESSWINDOW_H
#define BLACKGUI_ENABLEFORFRAMLESSWINDOW_H
#include <QWidget>
#include <QStatusBar>
#include <QMouseEvent>
#include <QPushButton>
#include <QHBoxLayout>
#include <QMenuBar>
namespace BlackGui
{
//! Main window which can be frameless
//! \details QMainWindows cannot be promoted. Hence a derived class does not work properly here.
//! Furthermore frameless functionality is also required for CDockWidgets as well.
class CEnableForFramelessWindow
{
public:
//! Window modes
enum WindowMode
{
WindowFrameless,
WindowNormal
};
//! Constructor
CEnableForFramelessWindow(WindowMode mode, bool isMainApplicationWindow, QWidget *correspondingWidget);
//! Window mode
void setMode(WindowMode mode);
//! Framless
void setFrameless(bool frameless);
//! Frameless?
bool isFrameless() const { return this->m_windowMode == WindowFrameless; }
//! The main application
bool isMainApplicationWindow() const { return m_mainApplicationWindow; }
//! Corresponding QMainWindow
QWidget *getWidget() const { return m_widget; }
protected:
//! Resize grip handle
void addFramelessSizeGrip(QStatusBar *statusBar);
//! Attributes
void setWindowAttributes(WindowMode mode);
//! Close button for frameless windows
QHBoxLayout *addFramelessCloseButton(QMenuBar *menuBar);
//! Translate mode
static Qt::WindowFlags modeToWindowFlags(WindowMode mode);
QPoint m_framelessDragPosition; //!< position, if moving is handled with frameless window */
QPushButton *m_framelessCloseButton = nullptr; //!< close button
WindowMode m_windowMode = WindowNormal; //!< Window mode, \sa WindowMode
bool m_mainApplicationWindow = false; //!< is the main application window (only 1)
QWidget *m_widget = nullptr; //!< corresponding main window or dock widget
//! Mouse press, required for frameless window
bool handleMousePressEvent(QMouseEvent *event);
//! Mouse moving, required for frameless window
bool handleMouseMoveEvent(QMouseEvent *event);
};
} // namespace
#endif // guard

View File

@@ -14,25 +14,23 @@
namespace BlackGui
{
QMainWindow *CGuiUtility::mainWindow()
CEnableForFramelessWindow *CGuiUtility::mainApplicationWindow()
{
QWidgetList tlw = topLevelApplicationWidgetsWithName();
foreach(QWidget * w, tlw)
{
QMainWindow *mw = qobject_cast<QMainWindow *>(w);
CEnableForFramelessWindow *mw = dynamic_cast<CEnableForFramelessWindow *>(w);
if (!mw) { continue; }
QString n = mw->objectName().toLower();
if (n.contains("main") && n.contains("window")) return mw;
if (mw->isMainApplicationWindow()) return mw;
}
return nullptr;
}
bool CGuiUtility::isMainWindowFrameless()
{
QMainWindow *mw = mainWindow();
CEnableForFramelessWindow *mw = mainApplicationWindow();
Q_ASSERT(mw); // there should be a main window
if (!mw) return false;
return (mw->windowFlags() & Qt::FramelessWindowHint);
return (mw && mw->isFrameless());
}
QWidgetList CGuiUtility::topLevelApplicationWidgetsWithName()
@@ -41,7 +39,7 @@ namespace BlackGui
QWidgetList rl;
foreach(QWidget * w, tlw)
{
if (w->objectName().isEmpty()) continue;
if (w->objectName().isEmpty()) { continue; }
rl.append(w);
}
return rl;
@@ -49,8 +47,8 @@ namespace BlackGui
QPoint CGuiUtility::mainWindowPosition()
{
QMainWindow *mw = mainWindow();
return (mw) ? mw->pos() : QPoint();
CEnableForFramelessWindow *mw = mainApplicationWindow();
return (mw) ? mw->getWidget()->pos() : QPoint();
}
QPoint CGuiUtility::introWindowPosition()
@@ -59,7 +57,7 @@ namespace BlackGui
foreach(QWidget * w, tlw)
{
QString n = w->objectName().toLower();
if (n.contains("intro")) return w->pos();
if (n.contains("intro")) { return w->pos(); }
}
return QPoint(0, 0);
}
@@ -73,7 +71,7 @@ namespace BlackGui
QString CGuiUtility::replaceTabCountValue(const QString &oldName, int count)
{
const QString v = QString("(").append(QString::number(count)).append(")");
if (oldName.isEmpty()) {return v; }
if (oldName.isEmpty()) { return v; }
int index = oldName.lastIndexOf('(');
if (index == 0) { return v; }
if (index < 0) { return QString(oldName).append(" ").append(v); }

View File

@@ -13,7 +13,7 @@
#define BLACKGUI_GUIUTILITY_H
#include <QWidgetList>
#include <QMainWindow>
#include "enableforframelesswindow.h"
namespace BlackGui
{
@@ -24,7 +24,7 @@ namespace BlackGui
public:
//! Main application window
static QMainWindow *mainWindow();
static CEnableForFramelessWindow *mainApplicationWindow();
//! Is main window frameless?
static bool isMainWindowFrameless();

View File

@@ -25,7 +25,8 @@ using namespace BlackMisc;
namespace BlackGui
{
CInfoArea::CInfoArea(QWidget *parent) : QMainWindow(parent)
CInfoArea::CInfoArea(QWidget *parent) :
QMainWindow(parent), CEnableForFramelessWindow(CEnableForFramelessWindow::WindowNormal, false, this)
{
this->ps_setInfoAreaFloating(this->m_infoAreaFloating);
this->setWindowIcon(CIcons::swift24());
@@ -39,7 +40,7 @@ namespace BlackGui
// after(!) GUI is setup
if (this->m_dockWidgetInfoAreas.isEmpty())
{
this->m_dockWidgetInfoAreas = this->initOwnDockWidgetInfoAreas();
this->m_dockWidgetInfoAreas = this->findOwnDockWidgetInfoAreas();
Q_ASSERT(!this->m_dockWidgetInfoAreas.isEmpty());
}
@@ -401,13 +402,19 @@ namespace BlackGui
// this completely initializes the tab bar and all docked widgets
if (init)
{
// float
QPoint offset(i * 25, i * 20);
after->setVisible(false);
after->setFloating(true);
after->setOffsetWhenFloating(offset);
after->setPreferredSizeWhenFloating(this->getPreferredSizeWhenFloating(i));
QSize floatingSize = this->getPreferredSizeWhenFloating(i);
after->setPreferredSizeWhenFloating(floatingSize);
// dock again
after->setFloating(false);
after->setVisible(true);
// reset floating flag
after->resetWasAlreadyFLoating();
}
else
@@ -497,7 +504,7 @@ namespace BlackGui
}
}
QList<CDockWidgetInfoArea *> CInfoArea::initOwnDockWidgetInfoAreas()
QList<CDockWidgetInfoArea *> CInfoArea::findOwnDockWidgetInfoAreas() const
{
QList<CDockWidgetInfoArea *> infoAreas = this->findChildren<CDockWidgetInfoArea *>();
if (infoAreas.isEmpty()) { return infoAreas; }

View File

@@ -13,6 +13,7 @@
#define BLACKGUI_INFOAREA_H
#include "dockwidgetinfoarea.h"
#include "enableforframelesswindow.h"
#include <QMainWindow>
#include <QTabBar>
#include <QPixmap>
@@ -22,7 +23,7 @@ namespace BlackGui
{
//! Info area, hosting dockable widgets
//! \sa CDockWidgetInfoArea
class CInfoArea : public QMainWindow
class CInfoArea : public QMainWindow, CEnableForFramelessWindow
{
Q_OBJECT
@@ -182,11 +183,11 @@ namespace BlackGui
void setMarginsWhenDocked(int left, int top, int right, int bottom);
//! Nested info areas
QList<CInfoArea *> getChildInfoAreas() { return this->findChildren<CInfoArea *>(); }
QList<CInfoArea *> getChildInfoAreas() const { return this->findChildren<CInfoArea *>(); }
//! Direct dock widget areas, not the nested dock widget areas
//! \remarks result stored in m_dockableWidgets
QList<CDockWidgetInfoArea *> initOwnDockWidgetInfoAreas();
QList<CDockWidgetInfoArea *> findOwnDockWidgetInfoAreas() const;
private slots:
//! Tab bar has been double clicked

View File

@@ -11,8 +11,8 @@ Used dynamic properties
mainframeless (infobar.qss)
**/
/** putting background here works for framed, but not for frameless window **/
#MainWindow {
/** Main window **/
QMainWindow {
background-image: url(:/textures/icons/textures/texture-outer.jpg);
background-color: darkslategray;
}

View File

@@ -7,21 +7,17 @@
* contained in the LICENSE file.
*/
#ifndef SAMPLE_GUIMODEENUMS_H
#define SAMPLE_GUIMODEENUMS_H
#ifndef STDGUI_GUIMODEENUMS_H
#define STDGUI_GUIMODEENUMS_H
//! Modes, how GUI can be started (core/GUI)
struct GuiModes {
struct GuiModes
{
public:
//! Window modes
enum WindowMode {
WindowFrameless,
WindowNormal
};
//! Core runs how and where?
enum CoreMode {
enum CoreMode
{
CoreInGuiProcess,
CoreExternal,
CoreExternalAudioLocal

View File

@@ -33,12 +33,12 @@ CIntroWindow::~CIntroWindow() { }
/*
* Window mode
*/
GuiModes::WindowMode CIntroWindow::getWindowMode() const
BlackGui::CEnableForFramelessWindow::WindowMode CIntroWindow::getWindowMode() const
{
if (this->ui->rb_WindowFrameless->isChecked())
return GuiModes::WindowFrameless;
return BlackGui::CEnableForFramelessWindow::WindowFrameless;
else
return GuiModes::WindowNormal;
return BlackGui::CEnableForFramelessWindow::WindowNormal;
}
/*

View File

@@ -9,10 +9,11 @@
//! \file
#ifndef SAMPLE_INTROWINDOW_H
#define SAMPLE_INTROWINDOW_H
#ifndef STDGUI_INTROWINDOW_H
#define STDGUI_INTROWINDOW_H
#include "guimodeenums.h"
#include "blackgui/enableforframelesswindow.h"
#include <QDialog>
#include <QScopedPointer>
@@ -31,7 +32,7 @@ public:
~CIntroWindow();
//! Selected window mode
GuiModes::WindowMode getWindowMode() const;
BlackGui::CEnableForFramelessWindow::WindowMode getWindowMode() const;
//! Get core mode
GuiModes::CoreMode getCoreMode() const;

View File

@@ -15,6 +15,7 @@
#include "blackcore/context_runtime_config.h"
#include "blacksim/blacksimfreefunctions.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/icons.h"
#include "blackmisc/loghandler.h"
@@ -24,6 +25,8 @@
#include <QPushButton>
using namespace BlackGui;
using namespace BlackMisc;
using namespace BlackCore;
/*!
* \brief Main
@@ -42,11 +45,11 @@ int main(int argc, char *argv[])
// Translations
QFile file(":blackmisc/translations/blackmisc_i18n_de.qm");
qDebug() << (file.exists() ? "Found translations in resources" : "No translations in resources");
CLogMessage("swift.standardgui.main").debug() << (file.exists() ? "Found translations in resources" : "No translations in resources");
QTranslator translator;
if (translator.load("blackmisc_i18n_de", ":blackmisc/translations/"))
{
qDebug() << "Translator loaded";
CLogMessage("swift.standardgui.main").debug() << "Translator loaded";
}
// application
@@ -66,7 +69,7 @@ int main(int argc, char *argv[])
a.setStyleSheet(s);
// modes
GuiModes::WindowMode windowMode;
BlackGui::CMainWindow::WindowMode windowMode;
// Dialog to decide external or internal core
CIntroWindow intro;

View File

@@ -38,23 +38,13 @@ using namespace BlackMisc::Hardware;
/*
* Constructor
*/
MainWindow::MainWindow(GuiModes::WindowMode windowMode, QWidget *parent) :
QMainWindow(parent, windowMode == GuiModes::WindowFrameless ?
(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) :
(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint)),
ui(new Ui::MainWindow),
m_windowMode(windowMode)
MainWindow::MainWindow(BlackGui::CMainWindow::WindowMode windowMode, QWidget *parent) :
BlackGui::CMainWindow(windowMode, parent),
ui(new Ui::MainWindow)
{
if (windowMode == GuiModes::WindowFrameless)
{
// http://stackoverflow.com/questions/18316710/frameless-and-transparent-window-qt5
this->setAttribute(Qt::WA_NoSystemBackground, true);
this->setAttribute(Qt::WA_TranslucentBackground, true);
// this->setAttribute(Qt::WA_PaintOnScreen);
}
// GUI
ui->setupUi(this);
this->ui->wi_CentralWidgetOutside->setProperty("mainframeless", this->isFrameless());
this->m_compInfoWindow = new CInfoWindowComponent(this); // setupUi has to be first!
}
@@ -118,29 +108,6 @@ void MainWindow::closeEvent(QCloseEvent *event)
QApplication::exit();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
// this->ui->fr_PseudoWindowBar->geometry().contains(event->pos())
if (this->m_windowMode == GuiModes::WindowFrameless && event->buttons() & Qt::LeftButton)
{
move(event->globalPos() - this->m_dragPosition);
event->accept();
return;
}
QWidget::mouseMoveEvent(event);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (this->m_windowMode == GuiModes::WindowFrameless && event->button() == Qt::LeftButton)
{
this->m_dragPosition = event->globalPos() - this->frameGeometry().topLeft();
event->accept();
return;
}
QWidget::mousePressEvent(event);
}
/*
* Set main page
*/

View File

@@ -9,8 +9,8 @@
//! \file
#ifndef SAMPLE_MAINWINDOW_H
#define SAMPLE_MAINWINDOW_H
#ifndef STDGUI_SWIFTGUI_H
#define STDGUI_SWIFTGUI_H
// clash with struct interface in objbase.h used to happen
#pragma push_macro("interface")
@@ -29,6 +29,7 @@
#include "blackgui/models/userlistmodel.h"
#include "blackgui/models/statusmessagelistmodel.h"
#include "blackgui/models/keyboardkeylistmodel.h"
#include "blackgui/mainwindow.h"
#include "blackgui/managedstatusbar.h"
#include "blackmisc/nwtextmessage.h"
#include "blackmisc/loghandler.h"
@@ -44,7 +45,7 @@ namespace Ui { class MainWindow; }
//! swift GUI
class MainWindow :
public QMainWindow,
public BlackGui::CMainWindow,
public BlackGui::Components::CEnableForRuntime
{
Q_OBJECT
@@ -60,7 +61,7 @@ public:
};
//! Constructor
explicit MainWindow(GuiModes::WindowMode windowMode, QWidget *parent = nullptr);
explicit MainWindow(BlackGui::CMainWindow::WindowMode windowMode, QWidget *parent = nullptr);
//! Destructor
~MainWindow();
@@ -83,18 +84,11 @@ protected:
//! Close event, e.g. when window is closed
void closeEvent(QCloseEvent *event);
//! Mouse moving, required for frameless window
void mouseMoveEvent(QMouseEvent *event);
//! Mouse press, required for frameless window
void mousePressEvent(QMouseEvent *event);
private:
QScopedPointer<Ui::MainWindow> ui;
bool m_init = false;
BlackGui::Components::CInfoWindowComponent *m_compInfoWindow = nullptr; //!< the info window (popup
BlackGui::CManagedStatusBar m_statusBar;
GuiModes::WindowMode m_windowMode = GuiModes::WindowNormal;
BlackInput::IKeyboard *m_keyboard = nullptr; //!< hotkeys
BlackMisc::CLogSubscriber m_logSubscriber { this, &MainWindow::ps_displayStatusMessageInGui };
@@ -105,12 +99,6 @@ private:
QTimer *m_timerContextWatchdog = nullptr; //!< core available?
BlackMisc::Aviation::CAircraft m_ownAircraft; //!< own aircraft's state
// frameless window
QPoint m_dragPosition; /*!< position, if moving is handled with frameless window */
// context menus
QMenu *m_contextMenuStatusMessageList = nullptr; /*!< context menu for status message list */
// cockpit
QString m_transponderResetValue; //!< Temp. storage of XPdr mode to reset, req. until timer allows singleShoot with Lambdas
QWidget *m_inputFocusedWidget = nullptr; //!< currently used widget for input, mainly used with cockpit

View File

@@ -204,7 +204,7 @@
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>

View File

@@ -48,25 +48,17 @@ void MainWindow::init(const CRuntimeConfig &runtimeConfig)
// with frameless window, we shift menu and statusbar into central widget
// http://stackoverflow.com/questions/18316710/frameless-and-transparent-window-qt5
if (this->m_windowMode == GuiModes::WindowFrameless)
if (this->isFrameless())
{
this->ui->wi_CentralWidgetOutside->setStyleSheet("#wi_CentralWidgetOutside {border: 2px solid green; border-radius: 20px; }");
this->ui->vl_CentralWidgetOutside->setContentsMargins(8, 8, 8, 8);
QHBoxLayout *menuBarLayout = new QHBoxLayout();
QPushButton *closeIcon = new QPushButton(this);
closeIcon->setStyleSheet("margin: 0; padding: 0; background: transparent;");
closeIcon->setIcon(CIcons::close16());
QObject::connect(closeIcon, &QPushButton::clicked, this, &QMainWindow::close);
menuBarLayout->addWidget(this->ui->mb_MainMenuBar, 0, Qt::AlignTop | Qt::AlignLeft);
menuBarLayout->addWidget(closeIcon, 0, Qt::AlignTop | Qt::AlignRight);
QHBoxLayout *menuBarLayout = this->addFramelessCloseButton(this->ui->mb_MainMenuBar);
this->ui->vl_CentralWidgetOutside->insertLayout(0, menuBarLayout, 0);
QSizeGrip *grip = new QSizeGrip(this);
grip->setStyleSheet("margin-right: 25px; background-color: transparent;");
// move the status bar intothe frame (otherwise it is dangling outside)
this->ui->sb_MainStatusBar->setParent(this->ui->wi_CentralWidgetOutside);
this->ui->vl_CentralWidgetOutside->addWidget(this->ui->sb_MainStatusBar, 0);
this->ui->sb_MainStatusBar->addPermanentWidget(grip);
// grip
this->addFramelessSizeGrip(this->ui->sb_MainStatusBar);
}
// timers