qtutilities/misc/dialogutils.cpp

171 lines
5.8 KiB
C++
Raw Normal View History

2015-09-06 20:19:21 +02:00
#include "./dialogutils.h"
2017-05-01 03:16:25 +02:00
#include <QCoreApplication>
2019-05-04 22:16:46 +02:00
#include <QDir>
#include <QFileInfo>
#if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
2017-05-01 03:16:25 +02:00
#include <QGuiApplication>
#include <QPalette>
2019-05-04 22:16:46 +02:00
#endif
#if defined(QT_UTILITIES_GUI_QTWIDGETS)
2017-05-01 03:16:25 +02:00
#include <QApplication>
#include <QCursor>
#include <QDesktopWidget>
2019-05-04 22:16:46 +02:00
#include <QScreen>
#include <QStyle>
#include <QWidget>
2017-05-01 03:16:25 +02:00
#endif
namespace QtUtilities {
2016-02-05 20:23:02 +01:00
/*!
* \brief Generates the window title string for the specified \a documentStatus
* and \a documentPath.
*/
QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
{
2017-05-01 03:16:25 +02:00
switch (documentStatus) {
case DocumentStatus::Saved:
2017-05-01 03:16:25 +02:00
if (documentPath.isEmpty()) {
return QCoreApplication::translate("Utilities::windowTitle", "Unsaved - %1").arg(QCoreApplication::applicationName());
} else {
2018-05-23 23:20:01 +02:00
const QFileInfo file(documentPath);
2017-05-01 03:16:25 +02:00
return QCoreApplication::translate("Utilities::windowTitle", "%1 - %2 - %3")
.arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
}
case DocumentStatus::Unsaved:
2017-05-01 03:16:25 +02:00
if (documentPath.isEmpty()) {
return QCoreApplication::translate("Utilities::windowTitle", "*Unsaved - %1").arg(QCoreApplication::applicationName());
} else {
2018-05-23 23:20:01 +02:00
const QFileInfo file(documentPath);
2017-05-01 03:16:25 +02:00
return QCoreApplication::translate("Utilities::windowTitle", "*%1 - %2 - %3")
.arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
}
case DocumentStatus::NoDocument:
return QCoreApplication::applicationName();
2015-07-31 01:25:30 +02:00
default:
2017-05-04 22:46:37 +02:00
return QString(); // to suppress warning: "control reaches end of non-void
// function"
}
}
#if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
2017-05-01 03:16:25 +02:00
#ifdef Q_OS_WIN32
2016-02-05 20:23:02 +01:00
/*!
* \brief Returns the color used to draw frames.
*/
QColor windowFrameColor()
{
return QGuiApplication::palette().window().color().darker(108);
}
2016-02-05 20:23:02 +01:00
/*!
* \brief Returns the color used to draw instructions.
*/
QColor instructionTextColor()
{
const auto baseColor = QGuiApplication::palette().base().color();
return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().text().color();
}
2017-05-01 03:16:25 +02:00
#endif
2016-02-05 20:23:02 +01:00
/*!
2017-05-04 22:46:37 +02:00
* \brief Returns the stylesheet for dialogs and other windows used in my
* applications.
2016-02-05 20:23:02 +01:00
*/
const QString &dialogStyle()
{
2017-05-01 03:16:25 +02:00
#ifdef Q_OS_WIN32
2017-05-04 22:46:37 +02:00
static const auto style = QStringLiteral("#mainWidget { color: palette(text); background-color: "
"palette(base); border: none; }"
"#bottomWidget { background-color: palette(window); "
"color: palette(window-text); border-top: 1px solid %1; }"
"QMessageBox QLabel, QInputDialog QLabel, "
"*[classNames~=\"heading\"] { font-size: 12pt; color: %2; "
"}"
"*[classNames~=\"input-invalid\"] { color: red; }")
.arg(windowFrameColor().name(), instructionTextColor().name());
2017-05-01 03:16:25 +02:00
#else
2016-02-05 20:23:02 +01:00
static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
"*[classNames~=\"input-invalid\"] { color: red; }");
2017-05-01 03:16:25 +02:00
#endif
return style;
}
2017-05-01 03:16:25 +02:00
#ifdef QT_UTILITIES_GUI_QTWIDGETS
2019-05-04 22:16:46 +02:00
QRect availableScreenGeometryAtPoint(const QPoint &point)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
QScreen *const screen = QGuiApplication::screenAt(point);
if (!screen) {
return QRect();
}
return screen->availableGeometry();
#else
return QApplication::desktop()->availableGeometry(point);
#endif
}
2016-08-27 14:54:19 +02:00
/*!
2017-05-04 22:46:37 +02:00
* \brief Moves the specified \a widget in the middle of the (available) screen
2019-05-04 22:16:46 +02:00
* area or \a parent if specified.
*
* The screen containing the current cursor position is used unless \a position
* is specified.
2016-08-27 14:54:19 +02:00
*/
void centerWidget(QWidget *widget, const QWidget *parent, const QPoint *position)
2016-08-27 14:54:19 +02:00
{
widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(),
parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos())));
2016-08-27 14:54:19 +02:00
}
/*!
2017-05-04 22:46:37 +02:00
* \brief Moves the specified \a widget to the corner which is closest to the
* current cursor position or \a position if specified.
2019-05-04 22:16:46 +02:00
*
* If there are multiple screens available, the screen where the cursor currently
* is located is chosen.
2016-08-27 14:54:19 +02:00
*/
void cornerWidget(QWidget *widget, const QPoint *position)
2016-08-27 14:54:19 +02:00
{
const QPoint cursorPos(position ? *position : QCursor::pos());
2019-05-04 22:16:46 +02:00
const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos));
Qt::Alignment alignment = nullptr;
2016-08-27 14:54:19 +02:00
alignment |= (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight);
alignment |= (cursorPos.y() - availableGeometry.top() < availableGeometry.bottom() - cursorPos.y() ? Qt::AlignTop : Qt::AlignBottom);
2017-05-01 03:16:25 +02:00
widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, alignment, widget->size(), availableGeometry));
2016-08-27 14:54:19 +02:00
}
2016-02-05 20:23:02 +01:00
/*!
* \brief Makes \a widget a heading.
*/
void makeHeading(QWidget *widget)
{
widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading"));
}
2016-02-05 20:23:02 +01:00
/*!
* \brief Updates the widget style.
* \remarks Useful when dynamic properties are used in the stylesheet because
2017-05-04 22:46:37 +02:00
* the widget style does not update automatically when a property
* changes.
2016-02-05 20:23:02 +01:00
*/
void updateStyle(QWidget *widget)
{
widget->style()->unpolish(widget);
widget->style()->polish(widget);
widget->update();
}
2017-05-01 03:16:25 +02:00
#endif
#endif
} // namespace QtUtilities