Debian Codegraph @

misc/dialogutils.cpp

raw symbols SCIP
1 #include "./dialogutils.h"
2
3 #include "../misc/desktoputils.h"
4
5 #include <QCoreApplication>
6 #include <QDir>
7 #include <QFileInfo>
8
9 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
10 #include <QGuiApplication>
11 #include <QPalette>
12 #endif
13
14 #if defined(QT_UTILITIES_GUI_QTWIDGETS)
15 #include <QApplication>
16 #include <QCursor>
17 #if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
18 #include <QDesktopWidget>
19 #endif
20 #include <QScreen>
21 #include <QStyle>
22 #include <QWidget>
23 #endif
24
25 namespace QtUtilities {
26
27 /*!
28 * \brief Generates the window title string for the specified \a documentStatus
29 * and \a documentPath.
30 */
31 QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
32 {
33 switch (documentStatus) {
34 case DocumentStatus::Saved:
35 if (documentPath.isEmpty()) {
36 return QCoreApplication::translate("Utilities::windowTitle", "Unsaved - %1").arg(QCoreApplication::applicationName());
37 } else {
38 const QFileInfo file(documentPath);
39 return QCoreApplication::translate("Utilities::windowTitle", "%1 - %2 - %3")
40 .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
41 }
42 case DocumentStatus::Unsaved:
43 if (documentPath.isEmpty()) {
44 return QCoreApplication::translate("Utilities::windowTitle", "*Unsaved - %1").arg(QCoreApplication::applicationName());
45 } else {
46 const QFileInfo file(documentPath);
47 return QCoreApplication::translate("Utilities::windowTitle", "*%1 - %2 - %3")
48 .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
49 }
50 case DocumentStatus::NoDocument:
51 return QCoreApplication::applicationName();
52 default:
53 return QString(); // to suppress warning: "control reaches end of non-void
54 // function"
55 }
56 }
57
58 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
59
60 #ifdef Q_OS_WIN32
61 /*!
62 * \brief Returns the color used to draw frames.
63 */
64 QColor windowFrameColorForPalette(const QPalette &palette)
65 {
66 return palette.window().color().darker(108);
67 }
68
69 /*!
70 * \brief Returns the color used to draw frames.
71 */
72 QColor windowFrameColor()
73 {
74 return windowFrameColorForPalette(QGuiApplication::palette());
75 }
76
77 /*!
78 * \brief Returns the color used to draw instructions.
79 */
80 QColor instructionTextColorForPalette(const QPalette &palette)
81 {
82 return isPaletteDark(palette) ? palette.text().color() : QColor(0x00, 0x33, 0x99);
83 }
84
85 /*!
86 * \brief Returns the color used to draw instructions.
87 */
88 QColor instructionTextColor()
89 {
90 return instructionTextColorForPalette(QGuiApplication::palette());
91 }
92 #endif
93
94 /*!
95 * \brief Returns the stylesheet for dialogs and other windows used in my
96 * applications.
97 */
98 QString dialogStyleForPalette(const QPalette &palette)
99 {
100 #ifdef Q_OS_WINDOWS
101 return QStringLiteral("#mainWidget { color: palette(text); background-color: "
102 "palette(base); border: none; }"
103 "#bottomWidget { background-color: palette(window); "
104 "color: palette(window-text); border-top: 1px solid %1; }"
105 "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; }"
106 "*[classNames~=\"input-invalid\"] { color: red; }")
107 .arg(windowFrameColorForPalette(palette).name(), instructionTextColorForPalette(palette).name());
108 #else
109 Q_UNUSED(palette)
110 return QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
111 "*[classNames~=\"input-invalid\"] { color: red; }");
112 #endif
113 }
114
115 /*!
116 * \brief Returns the stylesheet for dialogs and other windows used in my
117 * applications.
118 */
119 const QString &dialogStyle()
120 {
121 static const auto style = dialogStyleForPalette(QGuiApplication::palette());
122 return style;
123 }
124
125 #ifdef QT_UTILITIES_GUI_QTWIDGETS
126
127 QRect availableScreenGeometryAtPoint(const QPoint &point)
128 {
129 #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
130 QScreen *const screen = QGuiApplication::screenAt(point);
131 if (!screen) {
132 return QRect();
133 }
134 return screen->availableGeometry();
135 #else
136 return QApplication::desktop()->availableGeometry(point);
137 #endif
138 }
139
140 /// \cond
141 static QRect shrinkRectByMargins(QRect rect, const QMargins &margins)
142 {
143 rect.setLeft(rect.left() + margins.left());
144 rect.setTop(rect.top() + margins.top());
145 rect.setRight(rect.right() - margins.right());
146 rect.setBottom(rect.bottom() - margins.bottom());
147 return rect;
148 }
149
150 static QRect limitRect(QRect rect, const QRect &bounds)
151 {
152 if (rect.left() < bounds.left()) {
153 rect.setLeft(bounds.left());
154 }
155 if (rect.top() < bounds.top()) {
156 rect.setTop(bounds.top());
157 }
158 if (rect.right() > bounds.right()) {
159 rect.setRight(bounds.right());
160 }
161 if (rect.bottom() > bounds.bottom()) {
162 rect.setBottom(bounds.bottom());
163 }
164 return rect;
165 }
166
167 static QMargins widgetFrame(QWidget *widget, const QMargins &defaultAssumption = QMargins(10, 25, 10, 10))
168 {
169 if (!widget->isWindow()) {
170 return QMargins();
171 }
172 const auto widgetGeometry = widget->geometry();
173 const auto frameGeometry = widget->frameGeometry();
174 const auto frame = QMargins(widgetGeometry.left() - frameGeometry.left(), widgetGeometry.top() - frameGeometry.top(),
175 frameGeometry.right() - widgetGeometry.right(), frameGeometry.bottom() - widgetGeometry.bottom());
176 return frame.isNull() ? defaultAssumption : frame;
177 }
178
179 static bool centerWidgetInternal(QWidget *widget, const QWidget *parent, const QPoint *position, bool avoidOverflow)
180 {
181 const auto availableGeometry = parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos());
182 const auto alignedRect = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(), availableGeometry);
183 if (!avoidOverflow) {
184 widget->setGeometry(alignedRect);
185 return false;
186 }
187 const auto limitedRect = limitRect(alignedRect, shrinkRectByMargins(availableGeometry, widgetFrame(widget)));
188 widget->setGeometry(limitedRect);
189 return alignedRect != limitedRect;
190 }
191 /// \endcond
192
193 /*!
194 * \brief Moves the specified \a widget to be centered within the (available) screen area or \a parent if specified.
195 * \remarks
196 * - The screen containing the current cursor position is used unless \a position is specified.
197 */
198 void centerWidget(QWidget *widget, const QWidget *parent, const QPoint *position)
199 {
200 centerWidgetInternal(widget, parent, position, false);
201 }
202
203 /*!
204 * \brief Moves the specified \a widget to be centered within the (available) screen area or \a parent if specified.
205 * \returns Returns whether an overflow occurred.
206 * \remarks
207 * - If the widget overflows it is resized to take the whole available space in the dimension(s) that overflow.
208 * If the widget is a window, its frame is attempted to be taken into account. If the window frame can not be determined
209 * a generous assumption is made. It can nevertheless make sense to simply show \a widget using QWidget::showMaximized()
210 * to make it simply fill the entire screen after all.
211 * - The screen containing the current cursor position is used unless \a position is specified.
212 */
213 bool centerWidgetAvoidingOverflow(QWidget *widget, const QWidget *parent, const QPoint *position)
214 {
215 return centerWidgetInternal(widget, parent, position, true);
216 }
217
218 /*!
219 * \brief Moves the specified \a widget to the corner which is closest to the
220 * current cursor position or \a position if specified.
221 *
222 * If there are multiple screens available, the screen where the cursor currently
223 * is located is chosen.
224 */
225 void cornerWidget(QWidget *widget, const QPoint *position)
226 {
227 const QPoint cursorPos(position ? *position : QCursor::pos());
228 const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos));
229 const Qt::Alignment alignment
230 = (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight)
231 | (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
232 widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, alignment, widget->size(), availableGeometry));
233 }
234
235 /*!
236 * \brief Makes \a widget a heading.
237 */
238 void makeHeading(QWidget *widget)
239 {
240 widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading"));
241 }
242
243 /*!
244 * \brief Updates the widget style.
245 * \remarks
246 * Useful when dynamic properties are used in the stylesheet because
247 * the widget style does not update automatically when a property changes.
248 */
249 void updateStyle(QWidget *widget)
250 {
251 widget->style()->unpolish(widget);
252 widget->style()->polish(widget);
253 widget->update();
254 }
255
256 #endif
257
258 #endif
259
260 } // namespace QtUtilities