5#include <QCoreApplication>
9#if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
10#include <QGuiApplication>
14#if defined(QT_UTILITIES_GUI_QTWIDGETS)
15#include <QApplication>
17#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
18#include <QDesktopWidget>
33 switch (documentStatus) {
35 if (documentPath.isEmpty()) {
36 return QCoreApplication::translate(
"Utilities::windowTitle",
"Unsaved - %1").arg(QCoreApplication::applicationName());
38 const QFileInfo file(documentPath);
39 return QCoreApplication::translate(
"Utilities::windowTitle",
"%1 - %2 - %3")
40 .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
43 if (documentPath.isEmpty()) {
44 return QCoreApplication::translate(
"Utilities::windowTitle",
"*Unsaved - %1").arg(QCoreApplication::applicationName());
46 const QFileInfo file(documentPath);
47 return QCoreApplication::translate(
"Utilities::windowTitle",
"*%1 - %2 - %3")
48 .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
51 return QCoreApplication::applicationName();
58#if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
64QColor windowFrameColorForPalette(
const QPalette &palette)
66 return palette.window().color().darker(108);
72QColor windowFrameColor()
74 return windowFrameColorForPalette(QGuiApplication::palette());
80QColor instructionTextColorForPalette(
const QPalette &palette)
83#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
84 palette.accent().color()
86 QColor(0x00, 0x33, 0x99)
94QColor instructionTextColor()
96 return instructionTextColorForPalette(QGuiApplication::palette());
104QString dialogStyleForPalette(
const QPalette &palette)
107 return QStringLiteral(
"#mainWidget { color: palette(text); background-color: "
108 "palette(base); border: none; }"
109 "#bottomWidget { background-color: palette(window); "
110 "color: palette(window-text); border-top: 1px solid %1; }"
111 "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; }"
112 "*[classNames~=\"input-invalid\"] { color: red; }")
113 .arg(windowFrameColorForPalette(palette).name(), instructionTextColorForPalette(palette).name());
116 return QStringLiteral(
"*[classNames~=\"heading\"] { font-weight: bold; }"
117 "*[classNames~=\"input-invalid\"] { color: red; }");
125const QString &dialogStyle()
127 static const auto style = dialogStyleForPalette(QGuiApplication::palette());
131#ifdef QT_UTILITIES_GUI_QTWIDGETS
133QRect availableScreenGeometryAtPoint(
const QPoint &point)
135#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
136 QScreen *
const screen = QGuiApplication::screenAt(point);
140 return screen->availableGeometry();
142 return QApplication::desktop()->availableGeometry(point);
147static QRect shrinkRectByMargins(QRect rect,
const QMargins &margins)
149 rect.setLeft(rect.left() + margins.left());
150 rect.setTop(rect.top() + margins.top());
151 rect.setRight(rect.right() - margins.right());
152 rect.setBottom(rect.bottom() - margins.bottom());
156static QRect limitRect(QRect rect,
const QRect &bounds)
158 if (rect.left() < bounds.left()) {
159 rect.setLeft(bounds.left());
161 if (rect.top() < bounds.top()) {
162 rect.setTop(bounds.top());
164 if (rect.right() > bounds.right()) {
165 rect.setRight(bounds.right());
167 if (rect.bottom() > bounds.bottom()) {
168 rect.setBottom(bounds.bottom());
173static QMargins widgetFrame(QWidget *widget,
const QMargins &defaultAssumption = QMargins(10, 25, 10, 10))
175 if (!widget->isWindow()) {
178 const auto widgetGeometry = widget->geometry();
179 const auto frameGeometry = widget->frameGeometry();
180 const auto frame = QMargins(widgetGeometry.left() - frameGeometry.left(), widgetGeometry.top() - frameGeometry.top(),
181 frameGeometry.right() - widgetGeometry.right(), frameGeometry.bottom() - widgetGeometry.bottom());
182 return frame.isNull() ? defaultAssumption : frame;
185static bool centerWidgetInternal(QWidget *widget,
const QWidget *parent,
const QPoint *position,
bool avoidOverflow)
187 const auto availableGeometry = parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos());
188 const auto alignedRect = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(), availableGeometry);
189 if (!avoidOverflow) {
190 widget->setGeometry(alignedRect);
193 const auto limitedRect = limitRect(alignedRect, shrinkRectByMargins(availableGeometry, widgetFrame(widget)));
194 widget->setGeometry(limitedRect);
195 return alignedRect != limitedRect;
204void centerWidget(QWidget *widget,
const QWidget *parent,
const QPoint *position)
206 centerWidgetInternal(widget, parent, position,
false);
219bool centerWidgetAvoidingOverflow(QWidget *widget,
const QWidget *parent,
const QPoint *position)
221 return centerWidgetInternal(widget, parent, position,
true);
231void cornerWidget(QWidget *widget,
const QPoint *position)
233 const QPoint cursorPos(position ? *position : QCursor::pos());
234 const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos));
235 const Qt::Alignment alignment
236 = (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight)
237 | (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
238 widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, alignment, widget->size(), availableGeometry));
244void makeHeading(QWidget *widget)
246 widget->setProperty(
"classNames", widget->property(
"classNames").toStringList() << QStringLiteral(
"heading"));
255void updateStyle(QWidget *widget)
257 widget->style()->unpolish(widget);
258 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.
QT_UTILITIES_EXPORT bool isPaletteDark(const QPalette &palette=QPalette())
Returns whether palette is dark.