3 #include <QCoreApplication>
7 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
8 #include <QGuiApplication>
12 #if defined(QT_UTILITIES_GUI_QTWIDGETS)
13 #include <QApplication>
15 #if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
16 #include <QDesktopWidget>
31 switch (documentStatus) {
33 if (documentPath.isEmpty()) {
34 return QCoreApplication::translate(
"Utilities::windowTitle",
"Unsaved - %1").arg(QCoreApplication::applicationName());
36 const QFileInfo file(documentPath);
37 return QCoreApplication::translate(
"Utilities::windowTitle",
"%1 - %2 - %3")
38 .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
41 if (documentPath.isEmpty()) {
42 return QCoreApplication::translate(
"Utilities::windowTitle",
"*Unsaved - %1").arg(QCoreApplication::applicationName());
44 const QFileInfo file(documentPath);
45 return QCoreApplication::translate(
"Utilities::windowTitle",
"*%1 - %2 - %3")
46 .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
49 return QCoreApplication::applicationName();
56 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
63 QColor windowFrameColor()
65 return QGuiApplication::palette().window().color().darker(108);
71 QColor instructionTextColor()
73 const auto baseColor = QGuiApplication::palette().base().color();
74 return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().
text().color();
83 const QString &dialogStyle()
86 static const auto style = QStringLiteral(
"#mainWidget { color: palette(text); background-color: "
87 "palette(base); border: none; }"
88 "#bottomWidget { background-color: palette(window); "
89 "color: palette(window-text); border-top: 1px solid %1; }"
90 "QMessageBox QLabel, QInputDialog QLabel, "
91 "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; "
93 "*[classNames~=\"input-invalid\"] { color: red; }")
94 .arg(windowFrameColor().name(), instructionTextColor().name());
96 static const auto style = QStringLiteral(
"*[classNames~=\"heading\"] { font-weight: bold; }"
97 "*[classNames~=\"input-invalid\"] { color: red; }");
102 #ifdef QT_UTILITIES_GUI_QTWIDGETS
104 QRect availableScreenGeometryAtPoint(
const QPoint &point)
106 #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
107 QScreen *
const screen = QGuiApplication::screenAt(point);
111 return screen->availableGeometry();
113 return QApplication::desktop()->availableGeometry(point);
118 static QRect shrinkRectByMargins(QRect rect,
const QMargins &margins)
120 rect.setLeft(rect.left() + margins.left());
121 rect.setTop(rect.top() + margins.top());
122 rect.setRight(rect.right() - margins.right());
123 rect.setBottom(rect.bottom() - margins.bottom());
127 static QRect limitRect(QRect rect,
const QRect &bounds)
129 if (rect.left() < bounds.left()) {
130 rect.setLeft(bounds.left());
132 if (rect.top() < bounds.top()) {
133 rect.setTop(bounds.top());
135 if (rect.right() > bounds.right()) {
136 rect.setRight(bounds.right());
138 if (rect.bottom() > bounds.bottom()) {
139 rect.setBottom(bounds.bottom());
144 static QMargins widgetFrame(QWidget *widget,
const QMargins &defaultAssumption = QMargins(10, 25, 10, 10))
146 if (!widget->isWindow()) {
149 const auto widgetGeometry = widget->geometry();
150 const auto frameGeometry = widget->frameGeometry();
151 const auto frame = QMargins(widgetGeometry.left() - frameGeometry.left(), widgetGeometry.top() - frameGeometry.top(),
152 frameGeometry.right() - widgetGeometry.right(), frameGeometry.bottom() - widgetGeometry.bottom());
153 return frame.isNull() ? defaultAssumption : frame;
156 static bool centerWidgetInternal(QWidget *widget,
const QWidget *parent,
const QPoint *position,
bool avoidOverflow)
158 const auto availableGeometry = parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos());
159 const auto alignedRect = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(), availableGeometry);
160 if (!avoidOverflow) {
161 widget->setGeometry(alignedRect);
164 const auto limitedRect = limitRect(alignedRect, shrinkRectByMargins(availableGeometry, widgetFrame(widget)));
165 widget->setGeometry(limitedRect);
166 return alignedRect != limitedRect;
175 void centerWidget(QWidget *widget,
const QWidget *parent,
const QPoint *position)
177 centerWidgetInternal(widget, parent, position,
false);
190 bool centerWidgetAvoidingOverflow(QWidget *widget,
const QWidget *parent,
const QPoint *position)
192 return centerWidgetInternal(widget, parent, position,
true);
202 void cornerWidget(QWidget *widget,
const QPoint *position)
204 const QPoint cursorPos(position ? *position : QCursor::pos());
205 const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos));
206 const Qt::Alignment alignment
207 = (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight)
208 | (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
209 widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, alignment, widget->size(), availableGeometry));
215 void makeHeading(QWidget *widget)
217 widget->setProperty(
"classNames", widget->property(
"classNames").toStringList() << QStringLiteral(
"heading"));
226 void updateStyle(QWidget *widget)
228 widget->style()->unpolish(widget);
229 widget->style()->polish(widget);
DocumentStatus
The DocumentStatus enum specifies the status of the document in a window.
QT_UTILITIES_EXPORT QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
Generates the window title string for the specified documentStatus and documentPath.