Qt Utilities 6.14.4
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
dialogutils.cpp
Go to the documentation of this file.
1#include "./dialogutils.h"
2
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
25namespace QtUtilities {
26
31QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
32{
33 switch (documentStatus) {
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 }
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 }
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
64QColor windowFrameColorForPalette(const QPalette &palette)
65{
66 return palette.window().color().darker(108);
67}
68
72QColor windowFrameColor()
73{
74 return windowFrameColorForPalette(QGuiApplication::palette());
75}
76
80QColor instructionTextColorForPalette(const QPalette &palette)
81{
82 return isPaletteDark(palette) ? palette.text().color() : QColor(0x00, 0x33, 0x99);
83}
84
88QColor instructionTextColor()
89{
90 return instructionTextColorForPalette(QGuiApplication::palette());
91}
92#endif
93
98QString 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 "QMessageBox QLabel, QInputDialog QLabel, "
106 "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; "
107 "}"
108 "*[classNames~=\"input-invalid\"] { color: red; }")
109 .arg(windowFrameColorForPalette(palette).name(), instructionTextColorForPalette(palette).name());
110#else
111 Q_UNUSED(palette)
112 return QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
113 "*[classNames~=\"input-invalid\"] { color: red; }");
114#endif
115}
116
121const QString &dialogStyle()
122{
123 static const auto style = dialogStyleForPalette(QGuiApplication::palette());
124 return style;
125}
126
127#ifdef QT_UTILITIES_GUI_QTWIDGETS
128
129QRect availableScreenGeometryAtPoint(const QPoint &point)
130{
131#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
132 QScreen *const screen = QGuiApplication::screenAt(point);
133 if (!screen) {
134 return QRect();
135 }
136 return screen->availableGeometry();
137#else
138 return QApplication::desktop()->availableGeometry(point);
139#endif
140}
141
143static QRect shrinkRectByMargins(QRect rect, const QMargins &margins)
144{
145 rect.setLeft(rect.left() + margins.left());
146 rect.setTop(rect.top() + margins.top());
147 rect.setRight(rect.right() - margins.right());
148 rect.setBottom(rect.bottom() - margins.bottom());
149 return rect;
150}
151
152static QRect limitRect(QRect rect, const QRect &bounds)
153{
154 if (rect.left() < bounds.left()) {
155 rect.setLeft(bounds.left());
156 }
157 if (rect.top() < bounds.top()) {
158 rect.setTop(bounds.top());
159 }
160 if (rect.right() > bounds.right()) {
161 rect.setRight(bounds.right());
162 }
163 if (rect.bottom() > bounds.bottom()) {
164 rect.setBottom(bounds.bottom());
165 }
166 return rect;
167}
168
169static QMargins widgetFrame(QWidget *widget, const QMargins &defaultAssumption = QMargins(10, 25, 10, 10))
170{
171 if (!widget->isWindow()) {
172 return QMargins();
173 }
174 const auto widgetGeometry = widget->geometry();
175 const auto frameGeometry = widget->frameGeometry();
176 const auto frame = QMargins(widgetGeometry.left() - frameGeometry.left(), widgetGeometry.top() - frameGeometry.top(),
177 frameGeometry.right() - widgetGeometry.right(), frameGeometry.bottom() - widgetGeometry.bottom());
178 return frame.isNull() ? defaultAssumption : frame;
179}
180
181static bool centerWidgetInternal(QWidget *widget, const QWidget *parent, const QPoint *position, bool avoidOverflow)
182{
183 const auto availableGeometry = parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos());
184 const auto alignedRect = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(), availableGeometry);
185 if (!avoidOverflow) {
186 widget->setGeometry(alignedRect);
187 return false;
188 }
189 const auto limitedRect = limitRect(alignedRect, shrinkRectByMargins(availableGeometry, widgetFrame(widget)));
190 widget->setGeometry(limitedRect);
191 return alignedRect != limitedRect;
192}
194
200void centerWidget(QWidget *widget, const QWidget *parent, const QPoint *position)
201{
202 centerWidgetInternal(widget, parent, position, false);
203}
204
215bool centerWidgetAvoidingOverflow(QWidget *widget, const QWidget *parent, const QPoint *position)
216{
217 return centerWidgetInternal(widget, parent, position, true);
218}
219
227void cornerWidget(QWidget *widget, const QPoint *position)
228{
229 const QPoint cursorPos(position ? *position : QCursor::pos());
230 const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos));
231 const Qt::Alignment alignment
232 = (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight)
233 | (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
234 widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, alignment, widget->size(), availableGeometry));
235}
236
240void makeHeading(QWidget *widget)
241{
242 widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading"));
243}
244
251void updateStyle(QWidget *widget)
252{
253 widget->style()->unpolish(widget);
254 widget->style()->polish(widget);
255 widget->update();
256}
257
258#endif
259
260#endif
261
262} // namespace QtUtilities
DocumentStatus
The DocumentStatus enum specifies the status of the document in a window.
Definition dialogutils.h:21
QT_UTILITIES_EXPORT QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
Generates the window title string for the specified documentStatus and documentPath.
QT_UTILITIES_EXPORT bool isPaletteDark(const QPalette &palette=QPalette())
Returns whether palette is dark.