forked from pbek/QOwnNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasterdialog.cpp
More file actions
109 lines (89 loc) · 2.68 KB
/
masterdialog.cpp
File metadata and controls
109 lines (89 loc) · 2.68 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
109
#include "masterdialog.h"
#include <QApplication>
#include <QGuiApplication>
#include <QKeyEvent>
#include <QScreen>
#include <QSettings>
#include "services/metricsservice.h"
MasterDialog::MasterDialog(QWidget *parent) : QDialog(parent) {
installEventFilter(this);
}
void MasterDialog::closeEvent(QCloseEvent *event) {
// storeGeometrySettings();
QDialog::closeEvent(event);
}
void MasterDialog::resizeEvent(QResizeEvent *event) {
// save the geometry of the dialog
storeGeometrySettings();
QDialog::resizeEvent(event);
}
/**
* Stores the geometry of the dialog
*/
void MasterDialog::storeGeometrySettings() const {
QSettings settings;
settings.setValue(getGeometrySettingKey(), saveGeometry());
}
bool MasterDialog::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::Move) {
// save the geometry of the dialog
storeGeometrySettings();
}
return QDialog::eventFilter(obj, event);
}
/**
* Returns the geometry settings key
*
* @return settings key
*/
const QString MasterDialog::getGeometrySettingKey() const {
return objectName() + "/geometry";
}
void MasterDialog::show() {
handleOpenDialog();
return QDialog::show();
}
void MasterDialog::open() {
handleOpenDialog();
return QDialog::open();
}
int MasterDialog::exec() {
handleOpenDialog();
return QDialog::exec();
}
/**
* Handles dialog opening
*/
void MasterDialog::handleOpenDialog() {
// restore the geometry of the dialog
QSettings settings;
QByteArray geometryData =
settings.value(getGeometrySettingKey()).toByteArray();
// restore the geometry if there is some data
if (geometryData.length() > 0) {
restoreGeometry(geometryData);
} else {
const QRect screenGeometry =
QGuiApplication::primaryScreen()->availableGeometry();
// maximize the dialog window if it looks like that it doesn't fit on
// the current screen
if (((window()->width() + 150) > screenGeometry.width()) ||
((window()->height() + 150) > screenGeometry.height())) {
setWindowState(windowState() ^ Qt::WindowMaximized);
}
}
// send metrics for all dialogs but the MainWindow
if (objectName() != QStringLiteral("MainWindow")) {
MetricsService::instance()->sendVisitIfEnabled("dialog/" + objectName());
}
}
void MasterDialog::setIgnoreReturnKey(bool ignore) {
_ignoreReturnKey = ignore;
}
void MasterDialog::keyPressEvent(QKeyEvent *keyEvent) {
if (_ignoreReturnKey && (keyEvent->key() == Qt::Key_Enter ||
keyEvent->key() == Qt::Key_Return)) {
return;
}
QDialog::keyPressEvent(keyEvent);
}