forked from qtdevs/FramelessHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
108 lines (94 loc) · 3.75 KB
/
MainWindow.cpp
File metadata and controls
108 lines (94 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "FramelessHelper.h"
#include <QDebug>
#include <QDesktopWidget>
#include <QFileInfo>
#include <QPainter>
#include <QScreen>
#include <QSettings>
#include <QTimer>
#include <QWindow>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent, Qt::FramelessWindowHint), ui(new Ui::MainWindow()) {
ui->setupUi(this);
setWindowTitle("Qt Widgets Inside");
auto helper = new FramelessHelper(this);
helper->setDraggableMargins(3, 3, 3, 3);
helper->setMaximizedMargins(0, 0, 0, 0);
helper->setTitleBarHeight(32);
helper->addExcludeItem(ui->minimizeButton);
helper->addExcludeItem(ui->maximizeButton);
helper->addExcludeItem(ui->closeButton);
connect(ui->minimizeButton, &QPushButton::clicked, helper,
&FramelessHelper::triggerMinimizeButtonAction);
connect(ui->maximizeButton, &QPushButton::clicked, helper,
&FramelessHelper::triggerMaximizeButtonAction);
connect(ui->closeButton, &QPushButton::clicked, helper,
&FramelessHelper::triggerCloseButtonAction);
connect(helper, &FramelessHelper::maximizedChanged, this,
&MainWindow::updateMaximizeButton);
ui->maximizeButton->setIcon(
QIcon(QStringLiteral(":/res/maximize-button1.png")));
QTimer::singleShot(100, this, &MainWindow::syncPosition);
}
MainWindow::~MainWindow() {
const QFileInfo fileInfo(QCoreApplication::applicationFilePath());
const QString iniFileName = fileInfo.completeBaseName() + ".ini";
const QString iniFilePath = fileInfo.canonicalPath() + u'/' + iniFileName;
QSettings(iniFilePath, QSettings::IniFormat)
.setValue(this->objectName() + "/Geometry", geometry());
QSettings(iniFilePath, QSettings::IniFormat)
.setValue(this->objectName() + "/DevicePixelRatio", devicePixelRatioF());
delete ui;
}
void MainWindow::updateMaximizeButton(bool maximized) {
if (maximized) {
ui->maximizeButton->setIcon(
QIcon(QStringLiteral(":/res/maximize-button2.png")));
ui->maximizeButton->setToolTip(tr("Restore"));
} else {
ui->maximizeButton->setIcon(
QIcon(QStringLiteral(":/res/maximize-button1.png")));
ui->maximizeButton->setToolTip(tr("Maximize"));
}
ui->maximizeButton->setAttribute(Qt::WA_UnderMouse, false);
}
void MainWindow::paintEvent(QPaintEvent *event) {
Q_UNUSED(event);
QPainter painter(this);
QImage backgroundImage(QStringLiteral(":/res/background.png"));
painter.drawImage(contentsRect(), backgroundImage);
#if 1
painter.setPen(Qt::red);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
painter.setPen(Qt::blue);
painter.drawRect(rect().adjusted(4, 4, -5, -5));
#endif
}
void MainWindow::syncPosition() {
// 窗口创建之后才能修改大小
const QFileInfo fileInfo(QCoreApplication::applicationFilePath());
const QString iniFileName = fileInfo.completeBaseName() + ".ini";
const QString iniFilePath = fileInfo.canonicalPath() + u'/' + iniFileName;
const auto savedGeometry =
QSettings(iniFilePath, QSettings::IniFormat)
.value(this->objectName() + "/Geometry", geometry())
.toRect();
auto const rec = QApplication::desktop()->availableGeometry(this);
if (rec.width() <= savedGeometry.width() ||
rec.height() <= savedGeometry.height()) {
// return;
}
if (savedGeometry.isValid() && !parent()) {
const auto savedDpr = QSettings(iniFilePath, QSettings::IniFormat)
.value(this->objectName() + "/DevicePixelRatio",
devicePixelRatioF())
.toReal();
// Qt doesn't support dpr < 1.
const qreal oldDpr = std::max(savedDpr, qreal(1));
const qreal scale = (devicePixelRatioF() / oldDpr);
setGeometry(
{savedGeometry.topLeft() * scale, savedGeometry.size() * scale});
}
}