From f8975a7d449bac21a2fdbc1a8ca85d1f5b99b362 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周二, 05 12月 2023 02:17:12 +0800
Subject: [PATCH] Implement mainwindow example

---
 examples/mainwindow/mainwindow.cpp |   89 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp
index f551a7f..65c4b12 100644
--- a/examples/mainwindow/mainwindow.cpp
+++ b/examples/mainwindow/mainwindow.cpp
@@ -1,15 +1,96 @@
 #include "mainwindow.h"
 
 #include <QtCore/QDebug>
+#include <QtCore/QTime>
+#include <QtWidgets/QPushButton>
 
 #include <QWKWidgets/widgetwindowagent.h>
 
-MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
-    auto agent = new QWK::WidgetWindowAgent(this);
-    if (!agent->setup(this)) {
-        qDebug() << "Frameless handle failed to initialize.";
+#include <widgetframe/windowbar.h>
+
+class ClockWidget : public QPushButton {
+public:
+    explicit ClockWidget(QWidget *parent = nullptr) : QPushButton(parent) {
+        startTimer(100);
     }
+
+    ~ClockWidget() override = default;
+
+protected:
+    void timerEvent(QTimerEvent *event) override {
+        setText(QTime::currentTime().toString(QStringLiteral("hh:mm:ss")));
+    }
+};
+
+MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
+    installWindowAgent();
 }
 
 MainWindow::~MainWindow() {
 }
+
+void MainWindow::installWindowAgent() {
+    auto agent = new QWK::WidgetWindowAgent(this);
+    if (!agent->setup(this)) {
+        qDebug() << "Frameless handle failed to initialize.";
+        return;
+    }
+
+    auto titleLabel = new QLabel();
+    titleLabel->setAlignment(Qt::AlignCenter);
+
+    auto menuBar = []() {
+        auto menuBar = new QMenuBar();
+        auto file = new QMenu("File(&F)");
+        file->addAction(new QAction("New(&N)"));
+        file->addAction(new QAction("Open(&O)"));
+
+        auto edit = new QMenu("Edit(&E)");
+        edit->addAction(new QAction("Undo(&U)"));
+        edit->addAction(new QAction("Redo(&R)"));
+
+        menuBar->addMenu(file);
+        menuBar->addMenu(edit);
+        return menuBar;
+    }();
+
+    auto iconButton = new QPushButton("I");
+    auto minButton = new QPushButton("鈥�");
+    auto maxButton = new QPushButton("馃棖");
+    maxButton->setCheckable(true);
+    auto closeButton = new QPushButton("鉁�");
+
+    auto windowBar = new QWK::WindowBar();
+    windowBar->setIconButton(iconButton);
+    windowBar->setMinButton(minButton);
+    windowBar->setMaxButton(maxButton);
+    windowBar->setCloseButton(closeButton);
+    windowBar->setMenuBar(menuBar);
+    windowBar->setTitleLabel(titleLabel);
+    windowBar->setHostWidget(this);
+
+    agent->setTitleBar(windowBar);
+    agent->setSystemButton(QWK::CoreWindowAgent::WindowIcon, iconButton);
+    agent->setSystemButton(QWK::CoreWindowAgent::Minimize, minButton);
+    agent->setSystemButton(QWK::CoreWindowAgent::Maximize, maxButton);
+    agent->setSystemButton(QWK::CoreWindowAgent::Close, closeButton);
+    agent->setHitTestVisible(menuBar, true);
+
+    connect(windowBar, &QWK::WindowBar::minimizeRequested, this, &QWidget::showMinimized);
+    connect(windowBar, &QWK::WindowBar::maximizeRequested, this, [this](bool max) {
+        if (max) {
+            showMaximized();
+        } else {
+            showNormal();
+        }
+    });
+    connect(windowBar, &QWK::WindowBar::closeRequested, this, &QWidget::close);
+
+    auto clockWidget = new ClockWidget();
+    clockWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+
+    setMenuWidget(windowBar);
+    setCentralWidget(clockWidget);
+    setWindowTitle("Example MainWindow");
+    resize(1024, 768);
+}

--
Gitblit v1.9.1