/* 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. 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 "blackgui/enableforframelesswindow.h" #include "blackgui/guiutility.h" #include "blackgui/foreignwindows.h" #include "blackmisc/icons.h" #include "blackmisc/stringutils.h" #include "blackmisc/worker.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace BlackMisc; namespace BlackGui { CEnableForFramelessWindow::CEnableForFramelessWindow(CEnableForFramelessWindow::WindowMode mode, bool isMainApplicationWindow, const char *framelessPropertyName, QWidget *correspondingWidget) : m_windowMode(mode), m_isMainApplicationWindow(isMainApplicationWindow), m_widget(correspondingWidget), m_framelessPropertyName(framelessPropertyName) { Q_ASSERT(correspondingWidget); Q_ASSERT(!m_framelessPropertyName.isEmpty()); m_originalWindowMode = mode; this->setWindowAttributes(mode); this->windowFlagsChanged(); } void CEnableForFramelessWindow::setMode(CEnableForFramelessWindow::WindowMode mode) { if (mode == m_windowMode) { return; } m_windowMode = mode; // set the main window or dock widget flags and attributes m_widget->setWindowFlags(modeToWindowFlags(mode)); this->windowFlagsChanged(); this->setWindowAttributes(mode); m_widget->show(); } void CEnableForFramelessWindow::setFrameless(bool frameless) { WindowMode nonFrameLessMode = m_originalWindowMode; if (nonFrameLessMode == WindowFrameless) { nonFrameLessMode = WindowNormal; } setMode(frameless ? WindowFrameless : nonFrameLessMode); } void CEnableForFramelessWindow::alwaysOnTop(bool onTop) { Qt::WindowFlags flags = m_widget->windowFlags(); if (onTop) { flags |= Qt::WindowStaysOnTopHint; } else { flags &= ~Qt::WindowStaysOnTopHint; } m_widget->setWindowFlags(flags); this->windowFlagsChanged(); } void CEnableForFramelessWindow::activate() { if (!m_widget) { return; } m_widget->setWindowState(Qt::WindowActive); } CEnableForFramelessWindow::WindowMode CEnableForFramelessWindow::stringToWindowMode(const QString &s) { const QString ws(s.trimmed().toLower()); if (ws.isEmpty()) { return WindowNormal; } if (ws.contains("frameless") || ws.startsWith("f")) { return WindowFrameless; } if (ws.contains("tool") || ws.startsWith("t")) { return WindowTool; } return WindowNormal; } const QString &CEnableForFramelessWindow::windowModeToString(CEnableForFramelessWindow::WindowMode m) { static const QString n("normal"); static const QString f("frameless"); static const QString t("tool"); switch (m) { case WindowFrameless: return f; case WindowNormal: return n; case WindowTool: return t; default: break; } return n; } void CEnableForFramelessWindow::windowFlagsChanged() { // void } void CEnableForFramelessWindow::setWindowAttributes(CEnableForFramelessWindow::WindowMode mode) { Q_ASSERT_X(m_widget, "CEnableForFramelessWindow::setWindowAttributes", "Missing widget representing window"); Q_ASSERT_X(!m_framelessPropertyName.isEmpty(), "CEnableForFramelessWindow::setWindowAttributes", "Missing property name"); bool frameless = (mode == WindowFrameless); // http://stackoverflow.com/questions/18316710/frameless-and-transparent-window-qt5 // https://bugreports.qt.io/browse/QTBUG-52206 // UpdateLayeredWindowIndirect failed for ptDst if (m_isMainApplicationWindow && CGuiUtility::isTopLevelWindow(m_widget)) { m_widget->setAttribute(Qt::WA_NativeWindow); // causeing a BLACK background m_widget->setAttribute(Qt::WA_NoSystemBackground, frameless); m_widget->setAttribute(Qt::WA_TranslucentBackground, frameless); // causing QTBUG-52206 } // Qt::WA_PaintOnScreen leads to a warning // setMask(QRegion(10, 10, 10, 10) would work, but requires "complex" calcs for rounded corners //! \fixme further improve transparent widget, try out void QWidget::setMask this->setDynamicProperties(frameless); } void CEnableForFramelessWindow::setDynamicProperties(bool frameless) { Q_ASSERT_X(m_widget, "CEnableForFramelessWindow::setDynamicProperties", "Missing widget representing window"); Q_ASSERT_X(!m_framelessPropertyName.isEmpty(), "CEnableForFramelessWindow::setDynamicProperties", "Missing property name"); // property selector will check on string, so I directly provide a string const QString f(BlackMisc::boolToTrueFalse(frameless)); m_widget->setProperty(m_framelessPropertyName.constData(), f); for (QObject *w : m_widget->children()) { if (w && w->isWidgetType()) { w->setProperty(m_framelessPropertyName.constData(), f); } } } void CEnableForFramelessWindow::showMinimizedModeChecked() { if (m_windowMode == CEnableForFramelessWindow::WindowTool) { this->toolToNormalWindow(); } m_widget->showMinimized(); } void CEnableForFramelessWindow::showNormalModeChecked() { if (m_windowMode == CEnableForFramelessWindow::WindowTool) { this->normalToToolWindow(); } m_widget->showMinimized(); } bool CEnableForFramelessWindow::handleMousePressEvent(QMouseEvent *event) { Q_ASSERT(m_widget); if (m_windowMode == WindowFrameless && event->button() == Qt::LeftButton) { m_framelessDragPosition = event->globalPos() - m_widget->frameGeometry().topLeft(); event->accept(); return true; } return false; } bool CEnableForFramelessWindow::handleMouseMoveEvent(QMouseEvent *event) { Q_ASSERT(m_widget); if (m_windowMode == WindowFrameless && event->buttons() & Qt::LeftButton && !m_framelessDragPosition.isNull()) { m_widget->move(event->globalPos() - m_framelessDragPosition); event->accept(); return true; } return false; } bool CEnableForFramelessWindow::handleChangeEvent(QEvent *event) { if (event->type() != QEvent::WindowStateChange) { return false; } if (m_windowMode != WindowTool) { return false; } if (!m_widget) { return false; } // make sure a tool window is changed to Normal window so it is show in taskbar // here we are already in transition state, so isMinimized means will be minimize right now // this check here is needed if minimized is called from somewhere else than ps_showMinimized QPointer widgetSelf(m_widget); // almost as good as myself if (m_widget->isMinimized()) { // still tool, force normal window // decouple, otherwise we end up in infinite loop as it triggers a new changeEvent QTimer::singleShot(0, m_widget, [ = ] { if (!widgetSelf) { return; } this->showMinimizedModeChecked(); }); } else { // not tool, force tool window // decouple, otherwise we end up in infinite loop as it triggers a new changeEvent QTimer::singleShot(0, m_widget, [ = ] { if (!widgetSelf) { return; } this->showNormalModeChecked(); }); } event->accept(); return true; } void CEnableForFramelessWindow::addFramelessSizeGripToStatusBar(QStatusBar *statusBar) { if (!statusBar) { return; } if (!m_framelessSizeGrip) { m_framelessSizeGrip = new QSizeGrip(m_widget); m_framelessSizeGrip->setObjectName("sg_FramelessSizeGrip"); statusBar->addPermanentWidget(m_framelessSizeGrip); } else { m_framelessSizeGrip->show(); } statusBar->repaint(); } void CEnableForFramelessWindow::hideFramelessSizeGripInStatusBar() { if (!m_framelessSizeGrip) { return; } m_framelessSizeGrip->hide(); } QHBoxLayout *CEnableForFramelessWindow::addFramelessCloseButton(QMenuBar *menuBar) { Q_ASSERT(isFrameless()); Q_ASSERT(menuBar); Q_ASSERT(m_widget); if (!m_framelessCloseButton) { m_framelessCloseButton = new QPushButton(m_widget); m_framelessCloseButton->setObjectName("pb_FramelessCloseButton"); m_framelessCloseButton->setIcon(CIcons::close16()); QObject::connect(m_framelessCloseButton, &QPushButton::clicked, m_widget, &QWidget::close, Qt::QueuedConnection); } 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; } void CEnableForFramelessWindow::toolToNormalWindow() { m_widget->setWindowFlags((m_widget->windowFlags() & (~Qt::Tool)) | Qt::Window); this->windowFlagsChanged(); m_originalWindowMode = WindowNormal; } void CEnableForFramelessWindow::normalToToolWindow() { m_widget->setWindowFlags(m_widget->windowFlags() | Qt::Tool); this->windowFlagsChanged(); m_originalWindowMode = WindowTool; } bool CEnableForFramelessWindow::isToolWindow() const { return (m_widget->windowFlags() & Qt::Tool) == Qt::Tool; } Qt::WindowFlags CEnableForFramelessWindow::modeToWindowFlags(CEnableForFramelessWindow::WindowMode mode) { switch (mode) { case WindowFrameless: return (Qt::Window | Qt::FramelessWindowHint); case WindowTool: // tool window and minimized not supported on Windows // tool window always with close button on Windows return (Qt::Tool | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); case WindowNormal: default: return (Qt::Window | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); } } } // namespace