Qt Utilities  5.12.2
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
dialogutils.cpp
Go to the documentation of this file.
1 #include "./dialogutils.h"
2 
3 #if !defined(QT_UTILITIES_GUI_QTWIDGETS) && !defined(QT_UTILITIES_GUI_QTQUICK)
4 #include <QCoreApplication>
5 #else
6 #include <QGuiApplication>
7 #include <QPalette>
8 #include <QStyle>
9 #include <QWidget>
10 #ifdef QT_UTILITIES_GUI_QTWIDGETS
11 #include <QApplication>
12 #include <QCursor>
13 #include <QDesktopWidget>
14 #endif
15 #endif
16 #include <QDir>
17 #include <QFileInfo>
18 
19 namespace Dialogs {
20 
25 QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
26 {
27  switch (documentStatus) {
29  if (documentPath.isEmpty()) {
30  return QCoreApplication::translate("Utilities::windowTitle", "Unsaved - %1").arg(QCoreApplication::applicationName());
31  } else {
32  const QFileInfo file(documentPath);
33  return QCoreApplication::translate("Utilities::windowTitle", "%1 - %2 - %3")
34  .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
35  }
37  if (documentPath.isEmpty()) {
38  return QCoreApplication::translate("Utilities::windowTitle", "*Unsaved - %1").arg(QCoreApplication::applicationName());
39  } else {
40  const QFileInfo file(documentPath);
41  return QCoreApplication::translate("Utilities::windowTitle", "*%1 - %2 - %3")
42  .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
43  }
45  return QCoreApplication::applicationName();
46  default:
47  return QString(); // to suppress warning: "control reaches end of non-void
48  // function"
49  }
50 }
51 
52 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
53 
54 #ifdef Q_OS_WIN32
55 
59 QColor windowFrameColor()
60 {
61  return QGuiApplication::palette().window().color().darker(108);
62 }
63 
67 QColor instructionTextColor()
68 {
69  const auto baseColor = QGuiApplication::palette().base().color();
70  return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().text().color();
71 }
72 
73 #endif
74 
79 const QString &dialogStyle()
80 {
81 #ifdef Q_OS_WIN32
82  static const auto style = QStringLiteral("#mainWidget { color: palette(text); background-color: "
83  "palette(base); border: none; }"
84  "#bottomWidget { background-color: palette(window); "
85  "color: palette(window-text); border-top: 1px solid %1; }"
86  "QMessageBox QLabel, QInputDialog QLabel, "
87  "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; "
88  "}"
89  "*[classNames~=\"input-invalid\"] { color: red; }")
90  .arg(windowFrameColor().name(), instructionTextColor().name());
91 #else
92  static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
93  "*[classNames~=\"input-invalid\"] { color: red; }");
94 #endif
95  return style;
96 }
97 
98 #ifdef QT_UTILITIES_GUI_QTWIDGETS
99 
106 void centerWidget(QWidget *widget)
107 {
108  widget->setGeometry(
109  QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(), QApplication::desktop()->availableGeometry(QCursor::pos())));
110 }
111 
118 void cornerWidget(QWidget *widget)
119 {
120  const QPoint cursorPos(QCursor::pos());
121  const QRect availableGeometry(QApplication::desktop()->availableGeometry(cursorPos));
122  Qt::Alignment alignment = 0;
123  alignment |= (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight);
124  alignment |= (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
125  widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, alignment, widget->size(), availableGeometry));
126 }
127 
131 void makeHeading(QWidget *widget)
132 {
133  widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading"));
134 }
135 
142 void updateStyle(QWidget *widget)
143 {
144  widget->style()->unpolish(widget);
145  widget->style()->polish(widget);
146  widget->update();
147 }
148 
149 #endif
150 
151 #endif
152 
153 } // namespace Dialogs
QString QT_UTILITIES_EXPORT generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
Generates the window title string for the specified documentStatus and documentPath.
Definition: dialogutils.cpp:25
DocumentStatus
The DocumentStatus enum specifies the status of the document in a window.
Definition: dialogutils.h:18
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
Definition: dialogutils.h:12