Qt Utilities  6.0.6
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 #include <QCoreApplication>
4 #include <QDir>
5 #include <QFileInfo>
6 
7 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
8 #include <QGuiApplication>
9 #include <QPalette>
10 #endif
11 
12 #if defined(QT_UTILITIES_GUI_QTWIDGETS)
13 #include <QApplication>
14 #include <QCursor>
15 #include <QDesktopWidget>
16 #include <QScreen>
17 #include <QStyle>
18 #include <QWidget>
19 #endif
20 
21 namespace QtUtilities {
22 
27 QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
28 {
29  switch (documentStatus) {
31  if (documentPath.isEmpty()) {
32  return QCoreApplication::translate("Utilities::windowTitle", "Unsaved - %1").arg(QCoreApplication::applicationName());
33  } else {
34  const QFileInfo file(documentPath);
35  return QCoreApplication::translate("Utilities::windowTitle", "%1 - %2 - %3")
36  .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
37  }
39  if (documentPath.isEmpty()) {
40  return QCoreApplication::translate("Utilities::windowTitle", "*Unsaved - %1").arg(QCoreApplication::applicationName());
41  } else {
42  const QFileInfo file(documentPath);
43  return QCoreApplication::translate("Utilities::windowTitle", "*%1 - %2 - %3")
44  .arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
45  }
47  return QCoreApplication::applicationName();
48  default:
49  return QString(); // to suppress warning: "control reaches end of non-void
50  // function"
51  }
52 }
53 
54 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
55 
56 #ifdef Q_OS_WIN32
57 
61 QColor windowFrameColor()
62 {
63  return QGuiApplication::palette().window().color().darker(108);
64 }
65 
69 QColor instructionTextColor()
70 {
71  const auto baseColor = QGuiApplication::palette().base().color();
72  return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().text().color();
73 }
74 
75 #endif
76 
81 const QString &dialogStyle()
82 {
83 #ifdef Q_OS_WIN32
84  static const auto style = QStringLiteral("#mainWidget { color: palette(text); background-color: "
85  "palette(base); border: none; }"
86  "#bottomWidget { background-color: palette(window); "
87  "color: palette(window-text); border-top: 1px solid %1; }"
88  "QMessageBox QLabel, QInputDialog QLabel, "
89  "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; "
90  "}"
91  "*[classNames~=\"input-invalid\"] { color: red; }")
92  .arg(windowFrameColor().name(), instructionTextColor().name());
93 #else
94  static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
95  "*[classNames~=\"input-invalid\"] { color: red; }");
96 #endif
97  return style;
98 }
99 
100 #ifdef QT_UTILITIES_GUI_QTWIDGETS
101 
102 QRect availableScreenGeometryAtPoint(const QPoint &point)
103 {
104 #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
105  QScreen *const screen = QGuiApplication::screenAt(point);
106  if (!screen) {
107  return QRect();
108  }
109  return screen->availableGeometry();
110 #else
111  return QApplication::desktop()->availableGeometry(point);
112 #endif
113 }
114 
122 void centerWidget(QWidget *widget, const QWidget *parent, const QPoint *position)
123 {
124  widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(),
125  parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos())));
126 }
127 
135 void cornerWidget(QWidget *widget, const QPoint *position)
136 {
137  const QPoint cursorPos(position ? *position : QCursor::pos());
138  const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos));
139  Qt::Alignment alignment = nullptr;
140  alignment |= (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight);
141  alignment |= (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
142  widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, alignment, widget->size(), availableGeometry));
143 }
144 
148 void makeHeading(QWidget *widget)
149 {
150  widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading"));
151 }
152 
159 void updateStyle(QWidget *widget)
160 {
161  widget->style()->unpolish(widget);
162  widget->style()->polish(widget);
163  widget->update();
164 }
165 
166 #endif
167 
168 #endif
169 
170 } // namespace QtUtilities
QtUtilities::DocumentStatus::Unsaved
@ Unsaved
QtUtilities
!
Definition: trylocker.h:8
QtUtilities::generateWindowTitle
QT_UTILITIES_EXPORT QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
Generates the window title string for the specified documentStatus and documentPath.
Definition: dialogutils.cpp:27
QtUtilities::DocumentStatus::Saved
@ Saved
text
#define text
Definition: xmlparsermacros.h:18
QtUtilities::DocumentStatus
DocumentStatus
The DocumentStatus enum specifies the status of the document in a window.
Definition: dialogutils.h:20
dialogutils.h
QtUtilities::DocumentStatus::NoDocument
@ NoDocument