Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
various fixes and improvements
Browse files Browse the repository at this point in the history
1. Make all demo applications be able to record and restore the previous window geometry.
2. Improve the robustness of the widgets and quick implementation.

Signed-off-by: Yuhang Zhao <[email protected]>
  • Loading branch information
wangwenx190 committed May 13, 2022
1 parent 915e775 commit 8042a78
Show file tree
Hide file tree
Showing 21 changed files with 335 additions and 56 deletions.
5 changes: 0 additions & 5 deletions examples/mainwindow/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
*/

#include <QtWidgets/qapplication.h>
#include <framelessconfig_p.h>
#include "mainwindow.h"

FRAMELESSHELPER_USE_NAMESPACE

using namespace Global;

int main(int argc, char *argv[])
{
// Not necessary, but better call this function, before the construction
Expand All @@ -38,8 +35,6 @@ int main(int argc, char *argv[])

QApplication application(argc, argv);

FramelessConfig::instance()->set(Option::CenterWindowBeforeShow);

MainWindow mainWindow;
mainWindow.show();

Expand Down
37 changes: 37 additions & 0 deletions examples/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/qsettings.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qdir.h>
#include <QtWidgets/qboxlayout.h>
#include <Utils>
#include <StandardTitleBar>
Expand All @@ -34,13 +38,33 @@ FRAMELESSHELPER_USE_NAMESPACE

using namespace Global;

FRAMELESSHELPER_STRING_CONSTANT2(GeoKeyPath, "Window/Geometry")
FRAMELESSHELPER_STRING_CONSTANT2(StateKeyPath, "Window/State")

[[nodiscard]] static inline QSettings *appConfigFile()
{
const QFileInfo fileInfo(QCoreApplication::applicationFilePath());
const QString iniFileName = fileInfo.completeBaseName() + FRAMELESSHELPER_STRING_LITERAL(".ini");
const QString iniFilePath = fileInfo.canonicalPath() + QDir::separator() + iniFileName;
const auto settings = new QSettings(iniFilePath, QSettings::IniFormat);
return settings;
}

MainWindow::MainWindow(QWidget *parent, const Qt::WindowFlags flags) : FramelessMainWindow(parent, flags)
{
initialize();
}

MainWindow::~MainWindow() = default;

void MainWindow::closeEvent(QCloseEvent *event)
{
const QScopedPointer<QSettings> settings(appConfigFile());
settings->setValue(kGeoKeyPath, saveGeometry());
settings->setValue(kStateKeyPath, saveState());
FramelessMainWindow::closeEvent(event);
}

void MainWindow::initialize()
{
m_titleBar.reset(new StandardTitleBar(this));
Expand All @@ -60,6 +84,19 @@ void MainWindow::initialize()
helper->setSystemButton(m_titleBar->maximizeButton(), SystemButtonType::Maximize);
helper->setSystemButton(m_titleBar->closeButton(), SystemButtonType::Close);
helper->setHitTestVisible(mb); // IMPORTANT!
connect(helper, &FramelessWidgetsHelper::ready, this, [this, helper](){
const QScopedPointer<QSettings> settings(appConfigFile());
const QByteArray geoData = settings->value(kGeoKeyPath).toByteArray();
const QByteArray stateData = settings->value(kStateKeyPath).toByteArray();
if (geoData.isEmpty()) {
helper->moveWindowToDesktopCenter();
} else {
restoreGeometry(geoData);
}
if (!stateData.isEmpty()) {
restoreState(stateData);
}
});

setWindowTitle(tr("FramelessHelper demo application - Qt MainWindow"));
}
3 changes: 3 additions & 0 deletions examples/mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class MainWindow : public FRAMELESSHELPER_PREPEND_NAMESPACE(FramelessMainWindow)
explicit MainWindow(QWidget *parent = nullptr, const Qt::WindowFlags flags = {});
~MainWindow() override;

protected:
void closeEvent(QCloseEvent *event) override;

private:
void initialize();

Expand Down
6 changes: 0 additions & 6 deletions examples/openglwidget/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
#include <QApplication>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <framelesshelpercore_global.h>
#include <framelessconfig_p.h>
#include "mainwindow.h"

// This example demonstrates easy, cross-platform usage of OpenGL ES 3.0 functions via
Expand All @@ -66,8 +64,6 @@

FRAMELESSHELPER_USE_NAMESPACE

using namespace Global;

int main(int argc, char *argv[])
{
// Not necessary, but better call this function, before the construction
Expand All @@ -76,8 +72,6 @@ int main(int argc, char *argv[])

QApplication application(argc, argv);

FramelessConfig::instance()->set(Option::CenterWindowBeforeShow);

QSurfaceFormat fmt = {};
fmt.setDepthBufferSize(24);

Expand Down
31 changes: 31 additions & 0 deletions examples/openglwidget/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

#include "mainwindow.h"
#include "glwidget.h"
#include <QtCore/qsettings.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qdir.h>
#include <QtWidgets/qboxlayout.h>
#include <FramelessWidgetsHelper>
#include <StandardTitleBar>
Expand All @@ -33,13 +37,31 @@ FRAMELESSHELPER_USE_NAMESPACE

using namespace Global;

FRAMELESSHELPER_STRING_CONSTANT2(IniKeyPath, "Window/Geometry")

[[nodiscard]] static inline QSettings *appConfigFile()
{
const QFileInfo fileInfo(QCoreApplication::applicationFilePath());
const QString iniFileName = fileInfo.completeBaseName() + FRAMELESSHELPER_STRING_LITERAL(".ini");
const QString iniFilePath = fileInfo.canonicalPath() + QDir::separator() + iniFileName;
const auto settings = new QSettings(iniFilePath, QSettings::IniFormat);
return settings;
}

MainWindow::MainWindow(QWidget *parent) : FramelessWidget(parent)
{
initialize();
}

MainWindow::~MainWindow() = default;

void MainWindow::closeEvent(QCloseEvent *event)
{
const QScopedPointer<QSettings> settings(appConfigFile());
settings->setValue(kIniKeyPath, saveGeometry());
FramelessWidget::closeEvent(event);
}

void MainWindow::initialize()
{
resize(800, 600);
Expand All @@ -58,4 +80,13 @@ void MainWindow::initialize()
helper->setSystemButton(m_titleBar->minimizeButton(), SystemButtonType::Minimize);
helper->setSystemButton(m_titleBar->maximizeButton(), SystemButtonType::Maximize);
helper->setSystemButton(m_titleBar->closeButton(), SystemButtonType::Close);
connect(helper, &FramelessWidgetsHelper::ready, this, [this, helper](){
const QScopedPointer<QSettings> settings(appConfigFile());
const QByteArray data = settings->value(kIniKeyPath).toByteArray();
if (data.isEmpty()) {
helper->moveWindowToDesktopCenter();
} else {
restoreGeometry(data);
}
});
}
3 changes: 3 additions & 0 deletions examples/openglwidget/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class MainWindow : public FRAMELESSHELPER_PREPEND_NAMESPACE(FramelessWidget)
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;

protected:
void closeEvent(QCloseEvent *event) override;

private:
void initialize();

Expand Down
2 changes: 2 additions & 0 deletions examples/quick/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS QuickControls2)
set(SOURCES
qml.qrc
main.cpp
settings.h
settings.cpp
)

if(WIN32)
Expand Down
31 changes: 20 additions & 11 deletions examples/quick/MainWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,33 @@ import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 2.0
import org.wangwenx190.FramelessHelper 1.0
import Demo 1.0

FramelessWindow {
id: window
visible: false // Hide the window before we sets up it's correct size and position.
width: 800
height: 600
title: qsTr("FramelessHelper demo application - Qt Quick")
color: (FramelessUtils.systemTheme === FramelessHelperConstants.Dark)
? FramelessUtils.defaultSystemDarkColor : FramelessUtils.defaultSystemLightColor
onClosing: Settings.saveGeometry(window)

FramelessHelper.onReady: {
// Let FramelessHelper know what's our homemade title bar, otherwise
// our window won't be draggable.
FramelessHelper.titleBarItem = titleBar;
// Make our own items visible to the hit test and on Windows, enable
// the snap layouts feature (available since Windows 11).
FramelessHelper.setSystemButton(titleBar.minimizeButton, FramelessHelperConstants.Minimize);
FramelessHelper.setSystemButton(titleBar.maximizeButton, FramelessHelperConstants.Maximize);
FramelessHelper.setSystemButton(titleBar.closeButton, FramelessHelperConstants.Close);
if (!Settings.restoreGeometry(window)) {
FramelessHelper.moveWindowToDesktopCenter();
}
// Finally, show the window after everything is setted.
window.visible = true;
}

Timer {
interval: 500
Expand All @@ -55,19 +74,9 @@ FramelessWindow {
StandardTitleBar {
id: titleBar
anchors {
top: window.topBorderBottom
top: window.topBorderBottom // IMPORTANT!
left: parent.left
right: parent.right
}
Component.onCompleted: {
// Make our homemade title bar draggable, and open the system menu
// when the user right clicks on the title bar area.
FramelessHelper.titleBarItem = titleBar;
// Make our own items visible to the hit test and on Windows, enable
// the snap layout feature (available since Windows 11).
FramelessHelper.setSystemButton(titleBar.minimizeButton, FramelessHelperConstants.Minimize);
FramelessHelper.setSystemButton(titleBar.maximizeButton, FramelessHelperConstants.Maximize);
FramelessHelper.setSystemButton(titleBar.closeButton, FramelessHelperConstants.Close);
}
}
}
21 changes: 12 additions & 9 deletions examples/quick/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
#include <QtQuick/qquickwindow.h>
#include <QtQuickControls2/qquickstyle.h>
#include <framelessquickmodule.h>
#include <framelessconfig_p.h>
#include "settings.h"

FRAMELESSHELPER_USE_NAMESPACE

using namespace Global;

int main(int argc, char *argv[])
{
// Not necessary, but better call this function, before the construction
Expand All @@ -41,8 +39,6 @@ int main(int argc, char *argv[])

QGuiApplication application(argc, argv);

FramelessConfig::instance()->set(Option::CenterWindowBeforeShow);

// Allow testing other RHI backends through environment variable.
if (!qEnvironmentVariableIsSet("QSG_RHI_BACKEND")) {
// This line is not relevant to FramelessHelper, we change
Expand All @@ -63,6 +59,13 @@ int main(int argc, char *argv[])
// Don't forget to register our own custom QML types!
FramelessHelper::Quick::registerTypes(&engine);

qmlRegisterSingletonType<Settings>("Demo", 1, 0, "Settings",
[](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return new Settings;
});

// This line is not relevant to FramelessHelper, we change the default
// Qt Quick Controls theme to "Basic" (Qt6) or "Default" (Qt5) just
// because other themes will make our homemade system buttons look
Expand All @@ -73,11 +76,11 @@ int main(int argc, char *argv[])
QQuickStyle::setStyle(FRAMELESSHELPER_STRING_LITERAL("Default"));
#endif

const QUrl homepageUrl(FRAMELESSHELPER_STRING_LITERAL("qrc:///Demo/qml/MainWindow.qml"));
const QUrl mainUrl(FRAMELESSHELPER_STRING_LITERAL("qrc:///Demo/qml/MainWindow.qml"));
const QMetaObject::Connection connection = QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &application,
[&homepageUrl, &connection](QObject *object, const QUrl &url) {
if (url != homepageUrl) {
[&mainUrl, &connection](QObject *object, const QUrl &url) {
if (url != mainUrl) {
return;
}
if (object) {
Expand All @@ -87,7 +90,7 @@ int main(int argc, char *argv[])
}
}, Qt::QueuedConnection);

engine.load(homepageUrl);
engine.load(mainUrl);

return QCoreApplication::exec();
}
77 changes: 77 additions & 0 deletions examples/quick/settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* MIT License
*
* Copyright (C) 2022 by wangwenx190 (Yuhang Zhao)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "settings.h"
#include <QtCore/qsettings.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qdir.h>
#include <QtCore/qdatastream.h>
#include <QtGui/qwindow.h>

FRAMELESSHELPER_STRING_CONSTANT2(IniKeyPath, "Window/Geometry")

Settings::Settings(QObject *parent) : QObject(parent)
{
const QFileInfo fileInfo(QCoreApplication::applicationFilePath());
const QString iniFileName = fileInfo.completeBaseName() + FRAMELESSHELPER_STRING_LITERAL(".ini");
const QString iniFilePath = fileInfo.canonicalPath() + QDir::separator() + iniFileName;
m_settings.reset(new QSettings(iniFilePath, QSettings::IniFormat));
}

Settings::~Settings() = default;

void Settings::saveGeometry(QWindow *window)
{
Q_ASSERT(window);
if (!window) {
return;
}
QByteArray data = {};
QDataStream stream(&data, QDataStream::WriteOnly);
stream.setVersion(QDataStream::Qt_5_6);
stream << window->geometry();
m_settings->setValue(kIniKeyPath, data);
}

bool Settings::restoreGeometry(QWindow *window)
{
Q_ASSERT(window);
if (!window) {
return false;
}
const QByteArray data = m_settings->value(kIniKeyPath).toByteArray();
if (data.isEmpty()) {
return false;
}
QRect geometry = {};
QDataStream stream(data);
stream.setVersion(QDataStream::Qt_5_6);
stream >> geometry;
if (!geometry.isValid()) {
return false;
}
window->setGeometry(geometry);
return true;
}
Loading

0 comments on commit 8042a78

Please sign in to comment.