From 3f17626c646697dc8ce460d7826bd78d274601d7 Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <2546789017@qq.com>
Date: Fri, 22 Apr 2022 12:04:38 +0800
Subject: [PATCH] misc: minor tweaks
1. Finish the simple title bar of the QOpenGLWidget demo
2. Minor improvements of the Quick implementation
Signed-off-by: Yuhang Zhao <2546789017@qq.com>
---
examples/openglwidget/images.qrc | 6 +--
examples/openglwidget/mainwindow.cpp | 50 ++++++++++++++++---
examples/openglwidget/mainwindow.h | 14 +++++-
.../Quick/framelesshelperquick_global.h | 2 +-
.../Quick/framelessquickhelper.h | 19 ++++++-
src/quick/framelesshelperquick.qrc | 3 ++
src/quick/framelessquickhelper.cpp | 22 ++++++++
src/quick/module/qmldir | 6 +++
8 files changed, 109 insertions(+), 13 deletions(-)
create mode 100644 src/quick/module/qmldir
diff --git a/examples/openglwidget/images.qrc b/examples/openglwidget/images.qrc
index 8c94378..617c258 100644
--- a/examples/openglwidget/images.qrc
+++ b/examples/openglwidget/images.qrc
@@ -1,5 +1,5 @@
-
- images/qtlogo.png
-
+
+ images/qtlogo.png
+
diff --git a/examples/openglwidget/mainwindow.cpp b/examples/openglwidget/mainwindow.cpp
index 00ed266..abee208 100644
--- a/examples/openglwidget/mainwindow.cpp
+++ b/examples/openglwidget/mainwindow.cpp
@@ -25,6 +25,12 @@
#include "mainwindow.h"
#include "glwidget.h"
#include
+#include
+#include
+
+FRAMELESSHELPER_USE_NAMESPACE
+
+using namespace Global;
MainWindow::MainWindow(QWidget *parent) : FramelessWidget(parent)
{
@@ -33,19 +39,49 @@ MainWindow::MainWindow(QWidget *parent) : FramelessWidget(parent)
MainWindow::~MainWindow() = default;
+void MainWindow::updateMaximizeButton()
+{
+ m_maxBtn->setText(isZoomed() ? tr("RESTORE") : tr("MAXIMIZE"));
+}
+
void MainWindow::setupUi()
{
- resize(800, 600);
- setWindowTitle(tr("QOpenGLWidget demo"));
- m_titleBar.reset(new QWidget(this));
- m_titleBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- m_titleBar->setFixedHeight(30);
+ m_titleLabel.reset(new QLabel(this));
+ QFont f = font();
+ f.setPointSize(kDefaultTitleBarFontPointSize);
+ m_titleLabel->setFont(f);
+ connect(this, &MainWindow::windowTitleChanged, m_titleLabel.data(), &QLabel::setText);
+ m_minBtn.reset(new QPushButton(this));
+ m_minBtn->setText(tr("MINIMIZE"));
+ connect(m_minBtn.data(), &QPushButton::clicked, this, &MainWindow::showMinimized);
+ m_maxBtn.reset(new QPushButton(this));
+ updateMaximizeButton();
+ connect(m_maxBtn.data(), &QPushButton::clicked, this, &MainWindow::toggleMaximized);
+ connect(this, &MainWindow::zoomedChanged, this, &MainWindow::updateMaximizeButton);
+ m_closeBtn.reset(new QPushButton(this));
+ m_closeBtn->setText(tr("CLOSE"));
+ connect(m_closeBtn.data(), &QPushButton::clicked, this, &MainWindow::close);
+ m_titleBarWidget.reset(new QWidget(this));
+ m_titleBarWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
+ m_titleBarWidget->setFixedHeight(kDefaultTitleBarHeight);
+ const auto titleBarLayout = new QHBoxLayout(m_titleBarWidget.data());
+ titleBarLayout->setSpacing(0);
+ titleBarLayout->setContentsMargins(0, 0, 0, 0);
+ titleBarLayout->addSpacerItem(new QSpacerItem(kDefaultTitleBarTitleLabelMargin, kDefaultTitleBarTitleLabelMargin));
+ titleBarLayout->addWidget(m_titleLabel.data());
+ titleBarLayout->addStretch();
+ titleBarLayout->addWidget(m_minBtn.data());
+ titleBarLayout->addWidget(m_maxBtn.data());
+ titleBarLayout->addWidget(m_closeBtn.data());
+ m_titleBarWidget->setLayout(titleBarLayout);
m_glWidget.reset(new GLWidget(this));
const auto mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0, 0, 0, 0);
- mainLayout->addWidget(m_titleBar.data());
+ mainLayout->addWidget(m_titleBarWidget.data());
mainLayout->addWidget(m_glWidget.data());
setLayout(mainLayout);
- setTitleBarWidget(m_titleBar.data());
+ setTitleBarWidget(m_titleBarWidget.data());
+ resize(800, 600);
+ setWindowTitle(tr("QOpenGLWidget demo"));
}
diff --git a/examples/openglwidget/mainwindow.h b/examples/openglwidget/mainwindow.h
index 2b9a38f..e726c4b 100644
--- a/examples/openglwidget/mainwindow.h
+++ b/examples/openglwidget/mainwindow.h
@@ -26,6 +26,11 @@
#include
+QT_BEGIN_NAMESPACE
+class QLabel;
+class QPushButton;
+QT_END_NAMESPACE
+
class GLWidget;
class MainWindow : public FRAMELESSHELPER_PREPEND_NAMESPACE(FramelessWidget)
@@ -37,10 +42,17 @@ public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
+public Q_SLOTS:
+ void updateMaximizeButton();
+
private:
void setupUi();
private:
- QScopedPointer m_titleBar;
+ QScopedPointer m_titleLabel;
+ QScopedPointer m_minBtn;
+ QScopedPointer m_maxBtn;
+ QScopedPointer m_closeBtn;
+ QScopedPointer m_titleBarWidget;
QScopedPointer m_glWidget;
};
diff --git a/include/FramelessHelper/Quick/framelesshelperquick_global.h b/include/FramelessHelper/Quick/framelesshelperquick_global.h
index 799600b..dffe1e2 100644
--- a/include/FramelessHelper/Quick/framelesshelperquick_global.h
+++ b/include/FramelessHelper/Quick/framelesshelperquick_global.h
@@ -26,7 +26,7 @@
#include
#if __has_include()
-#include
+# include
#endif
#ifndef FRAMELESSHELPER_QUICK_API
diff --git a/include/FramelessHelper/Quick/framelessquickhelper.h b/include/FramelessHelper/Quick/framelessquickhelper.h
index 5a0a2ab..dc24b86 100644
--- a/include/FramelessHelper/Quick/framelessquickhelper.h
+++ b/include/FramelessHelper/Quick/framelessquickhelper.h
@@ -25,6 +25,9 @@
#pragma once
#include "framelesshelperquick_global.h"
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
+# include
+#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QT_BEGIN_NAMESPACE
class QQmlEngine;
@@ -35,6 +38,20 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
namespace FramelessHelper::Quick
{
FRAMELESSHELPER_QUICK_API void registerTypes(QQmlEngine *engine);
-}
+} // namespace FramelessHelper::Quick
+
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
+class FRAMELESSHELPER_QUICK_API FramelessHelperQuickPlugin : public QQmlEngineExtensionPlugin
+{
+ Q_OBJECT
+ Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
+
+public:
+ explicit FramelessHelperQuickPlugin(QObject *parent = nullptr);
+ ~FramelessHelperQuickPlugin() override;
+
+ void initializeEngine(QQmlEngine *engine, const char *uri) override;
+};
+#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
FRAMELESSHELPER_END_NAMESPACE
diff --git a/src/quick/framelesshelperquick.qrc b/src/quick/framelesshelperquick.qrc
index 08ce3ab..fb787fc 100644
--- a/src/quick/framelesshelperquick.qrc
+++ b/src/quick/framelesshelperquick.qrc
@@ -1,4 +1,7 @@
+
+ module/qmldir
+
../core/images/dark/chrome-close.svg
../core/images/dark/chrome-maximize.svg
diff --git a/src/quick/framelessquickhelper.cpp b/src/quick/framelessquickhelper.cpp
index bb76ea7..a4fefb2 100644
--- a/src/quick/framelessquickhelper.cpp
+++ b/src/quick/framelessquickhelper.cpp
@@ -93,4 +93,26 @@ void FramelessHelper::Quick::registerTypes(QQmlEngine *engine)
qmlRegisterModule(QUICK_URI_FULL);
}
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
+FramelessHelperQuickPlugin::FramelessHelperQuickPlugin(QObject *parent) : QQmlEngineExtensionPlugin(parent)
+{
+}
+
+FramelessHelperQuickPlugin::~FramelessHelperQuickPlugin() = default;
+
+void FramelessHelperQuickPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
+{
+ Q_ASSERT(engine);
+ Q_ASSERT(uri);
+ if (!engine || !uri) {
+ return;
+ }
+ Q_ASSERT(QLatin1String(uri) == QLatin1String(FRAMELESSHELPER_QUICK_URI));
+ if (QLatin1String(uri) != QLatin1String(FRAMELESSHELPER_QUICK_URI)) {
+ return;
+ }
+ FramelessHelper::Quick::registerTypes(engine);
+}
+#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
+
FRAMELESSHELPER_END_NAMESPACE
diff --git a/src/quick/module/qmldir b/src/quick/module/qmldir
new file mode 100644
index 0000000..659d2dc
--- /dev/null
+++ b/src/quick/module/qmldir
@@ -0,0 +1,6 @@
+module org.wangwenx190.FramelessHelper
+linktarget FramelessHelperQuick
+plugin FramelessHelperQuick
+classname FramelessHelperQuickPlugin
+typeinfo FramelessHelperQuick.qmltypes
+prefer :/org/wangwenx190/FramelessHelper/