Debian Codegraph @

src/librssguard/gui/guiutilities.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/guiutilities.h"
4
5 #include "definitions/definitions.h"
6 #include "miscellaneous/application.h"
7 #include "miscellaneous/settings.h"
8
9 #include <QDialog>
10 #include <QMainWindow>
11 #include <QScreen>
12 #include <QSettings>
13
14 #if defined(Q_OS_ANDROID)
15 #include <QApplication>
16 #include <QDesktopWidget>
17 #include <QSize>
18 #endif
19
20 void GuiUtilities::setLabelAsNotice(QLabel& label, bool is_warning, bool set_margins) {
21 if (set_margins) {
22 label.setMargin(6);
23 }
24
25 if (is_warning) {
26 label.setStyleSheet(QSL("font-weight: bold; font-style: italic; color: red"));
27 }
28 else {
29 label.setStyleSheet(QSL("font-style: italic;"));
30 }
31 }
32
33 void GuiUtilities::applyDialogProperties(QWidget& widget, const QIcon& icon, const QString& title) {
34 widget.setWindowFlags(Qt::WindowType::Dialog | Qt::WindowType::WindowTitleHint |
35 Qt::WindowType::WindowMaximizeButtonHint | Qt::WindowType::WindowCloseButtonHint);
36
37 widget.setWindowIcon(icon);
38
39 if (!title.isEmpty()) {
40 widget.setWindowTitle(title);
41 }
42
43 loadDialogSize(widget);
44 fixTooBigDialog(widget);
45 saveSizeOnWidgetClosed(widget);
46 }
47
48 void GuiUtilities::fixTooBigDialog(QWidget& widget, bool move_to_center) {
49 // We fix too big dialog size or out-of-bounds position.
50 auto size_widget = widget.frameGeometry().size();
51 auto size_widget_original = size_widget;
52 auto size_screen = widget.screen()->availableSize();
53
54 if (size_widget.width() > size_screen.width()) {
55 size_widget.setWidth(size_screen.width() * 0.95);
56 }
57
58 if (size_widget.height() > size_screen.height()) {
59 size_widget.setHeight(size_screen.height() * 0.95);
60 }
61
62 bool resized = false;
63
64 if (size_widget != size_widget_original) {
65 qWarningNN << LOGSEC_GUI << "Dialog" << QUOTE_W_SPACE(widget.metaObject()->className()) << "was down-sized from"
66 << QUOTE_W_SPACE(widget.size()) << "to" << QUOTE_W_SPACE_DOT(size_widget);
67 resized = true;
68 widget.resize(size_widget);
69 }
70
71 auto pos_widget = widget.pos();
72
73 if ((resized && move_to_center) || pos_widget.x() < 0 || pos_widget.y() < 0) {
74 // Calculate ideal position for centering the widget.
75 auto size_parent = widget.parentWidget() != nullptr ? widget.frameGeometry().size() : QSize(0, 0);
76
77 // If dialog is bigger than its parent, center it to screen.
78 // If dialog is smaller than its parent, center to parent.
79 bool screen_as_parent;
80 QSize size_to_center;
81
82 if (size_widget.width() > size_parent.width() || size_widget.height() > size_parent.height()) {
83 screen_as_parent = true;
84 size_to_center = size_screen;
85 }
86 else {
87 screen_as_parent = false;
88 size_to_center = size_parent;
89 }
90
91 auto origin_x = (size_to_center.width() - size_widget.width()) / 2.0;
92 auto origin_y = (size_to_center.height() - size_widget.height()) / 2.0;
93 auto origin_pos = QPoint(origin_x, origin_y);
94
95 if (origin_pos != pos_widget) {
96 qWarningNN << LOGSEC_GUI << "Dialog" << QUOTE_W_SPACE(widget.metaObject()->className()) << "was moved from"
97 << QUOTE_W_SPACE(pos_widget) << "to" << QUOTE_W_SPACE_DOT(origin_pos);
98 widget.move(screen_as_parent ? origin_pos : origin_pos + widget.parentWidget()->pos());
99 }
100 }
101 }
102
103 void GuiUtilities::loadDialogSize(QWidget& wdg) {
104 const QString on = wdg.objectName();
105
106 if (on.isEmpty()) {
107 qWarningNN << LOGSEC_GUI << "Object of class" << QUOTE_W_SPACE(wdg.metaObject()->className())
108 << "has no name, cannot load its size.";
109 return;
110 }
111
112 const QString key = QSL("%1_size").arg(on);
113
114 wdg.resize(qApp->settings()->value(GROUP(DialogGeometries), key, wdg.size()).toSize());
115 }
116
117 void GuiUtilities::saveSizeOnWidgetClosed(QWidget& wdg) {
118 const QString on = wdg.objectName();
119
120 if (on.isEmpty()) {
121 qWarningNN << LOGSEC_GUI << "Object of class" << QUOTE_W_SPACE(wdg.metaObject()->className())
122 << "has no name, cannot save its size when it closes.";
123 return;
124 }
125
126 QDialog* wdg_dialog = qobject_cast<QDialog*>(&wdg);
127
128 if (wdg_dialog != nullptr) {
129 QObject::connect(wdg_dialog, &QDialog::finished, [=]() {
130 const QString key = QSL("%1_size").arg(on);
131
132 qDebugNN << LOGSEC_GUI << "Saving size for dialog" << QUOTE_W_SPACE_DOT(on);
133 qApp->settings()->setValue(GROUP(DialogGeometries), key, wdg_dialog->size());
134 });
135 }
136 }
137
138 void GuiUtilities::restoreState(QWidget* wdg, QByteArray state) {
139 QHash<QString, QHash<QString, QVariant>> props;
140 QDataStream str(&state, QIODevice::OpenModeFlag::ReadOnly);
141
142 str >> props;
143
144 QList<QObject*> to_process = {wdg};
145
146 while (!to_process.isEmpty()) {
147 QObject* act = to_process.takeFirst();
148
149 if (props.contains(act->objectName())) {
150 auto saved_props = props.value(act->objectName());
151 auto saved_props_names = saved_props.keys();
152
153 for (const QString& saved_key : saved_props_names) {
154 act->setProperty(saved_key.toLocal8Bit().constData(), saved_props.value(saved_key));
155 }
156 }
157
158 to_process.append(act->children());
159 }
160 }
161
162 QByteArray GuiUtilities::saveState(QWidget* wdg) {
163 QHash<QString, QStringList> props_to_serialize{{QSL("QCheckBox"), {QSL("checked")}},
164 {QSL("QSpinBox"), {QSL("value")}}};
165 QHash<QString, QHash<QString, QVariant>> props;
166 QList<QObject*> to_process = {wdg};
167
168 while (!to_process.isEmpty()) {
169 QObject* act = to_process.takeFirst();
170 const QMetaObject* meta = act->metaObject();
171 auto act_props = props_to_serialize.value(meta->className());
172 QHash<QString, QVariant> props_obj;
173
174 for (const QString& prop : act_props) {
175 props_obj.insert(prop, act->property(prop.toLocal8Bit().constData()));
176 }
177
178 if (!props_obj.isEmpty()) {
179 props.insert(act->objectName(), props_obj);
180 }
181
182 to_process.append(act->children());
183 }
184
185 QByteArray arr;
186 QDataStream str(&arr, QIODevice::OpenModeFlag::WriteOnly);
187
188 str << props;
189 return arr;
190 }