Qt Utilities  5.6.0
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 #ifdef GUI_NONE
4 # include <QCoreApplication>
5 #else
6 # include <QGuiApplication>
7 # include <QPalette>
8 # include <QWidget>
9 # include <QStyle>
10 # ifdef GUI_QTWIDGETS
11 # include <QApplication>
12 # include <QDesktopWidget>
13 # include <QCursor>
14 # endif
15 #endif
16 #include <QFileInfo>
17 #include <QDir>
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  QFileInfo file(documentPath);
33  return QCoreApplication::translate("Utilities::windowTitle", "%1 - %2 - %3").arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
34  }
36  if(documentPath.isEmpty()) {
37  return QCoreApplication::translate("Utilities::windowTitle", "*Unsaved - %1").arg(QCoreApplication::applicationName());
38  } else {
39  QFileInfo file(documentPath);
40  return QCoreApplication::translate("Utilities::windowTitle", "*%1 - %2 - %3").arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
41  }
43  return QCoreApplication::applicationName();
44  default:
45  return QString(); // to suppress warning: "control reaches end of non-void function"
46  }
47 }
48 
49 #ifndef GUI_NONE
50 
51 # ifdef Q_OS_WIN32
52 
56 QColor windowFrameColor()
57 {
58  return QGuiApplication::palette().window().color().darker(108);
59 }
60 
64 QColor instructionTextColor()
65 {
66  const auto baseColor = QGuiApplication::palette().base().color();
67  return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().text().color();
68 }
69 
70 # endif
71 
75 const QString &dialogStyle()
76 {
77 # ifdef Q_OS_WIN32
78  static const auto style = QStringLiteral("#mainWidget { color: palette(text); background-color: palette(base); border: none; }"
79  "#bottomWidget { background-color: palette(window); color: palette(window-text); border-top: 1px solid %1; }"
80  "QMessageBox QLabel, QInputDialog QLabel, *[classNames~=\"heading\"] { font-size: 12pt; color: %2; }"
81  "*[classNames~=\"input-invalid\"] { color: red; }").arg(
82  windowFrameColor().name(), instructionTextColor().name());
83 # else
84  static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
85  "*[classNames~=\"input-invalid\"] { color: red; }");
86 # endif
87  return style;
88 }
89 
90 # ifdef GUI_QTWIDGETS
91 
96 void centerWidget(QWidget *widget)
97 {
98  widget->setGeometry(
99  QStyle::alignedRect(
100  Qt::LeftToRight,
101  Qt::AlignCenter,
102  widget->size(),
103  QApplication::desktop()->availableGeometry(QCursor::pos())
104  )
105  );
106 }
107 
112 void cornerWidget(QWidget *widget)
113 {
114  const QPoint cursorPos(QCursor::pos());
115  const QRect availableGeometry(QApplication::desktop()->availableGeometry(cursorPos));
116  Qt::Alignment alignment = 0;
117  alignment |= (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight);
118  alignment |= (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
119  widget->setGeometry(
120  QStyle::alignedRect(
121  Qt::LeftToRight,
122  alignment,
123  widget->size(),
124  availableGeometry
125  )
126  );
127 }
128 
132 void makeHeading(QWidget *widget)
133 {
134  widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading"));
135 }
136 
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
154 
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:17
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
Definition: dialogutils.h:12
const QString QT_UTILITIES_EXPORT & dialogStyle()
Returns the stylesheet for dialogs and other windows used in my applications.
Definition: dialogutils.cpp:75