qtutilities/misc/dialogutils.cpp

109 lines
3.6 KiB
C++
Raw Normal View History

2015-09-06 20:19:21 +02:00
#include "./dialogutils.h"
#ifdef GUI_NONE
2016-02-05 20:23:02 +01:00
# include <QCoreApplication>
#else
2016-02-05 20:23:02 +01:00
# include <QGuiApplication>
# include <QPalette>
# include <QWidget>
# include <QStyle>
#endif
#include <QFileInfo>
#include <QDir>
namespace Dialogs {
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)
{
switch(documentStatus) {
case DocumentStatus::Saved:
if(documentPath.isEmpty()) {
return QCoreApplication::translate("Utilities::windowTitle", "Unsaved - %1").arg(QCoreApplication::applicationName());
} else {
QFileInfo file(documentPath);
return QCoreApplication::translate("Utilities::windowTitle", "%1 - %2 - %3").arg(file.fileName(), file.dir().path(), QCoreApplication::applicationName());
}
case DocumentStatus::Unsaved:
if(documentPath.isEmpty()) {
return QCoreApplication::translate("Utilities::windowTitle", "*Unsaved - %1").arg(QCoreApplication::applicationName());
} else {
QFileInfo file(documentPath);
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:
return QString(); // to suppress warning: "control reaches end of non-void function"
}
}
#ifndef GUI_NONE
2016-02-05 20:23:02 +01:00
# ifdef Q_OS_WIN32
/*!
* \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();
}
2016-02-05 20:23:02 +01:00
# endif
2016-02-05 20:23:02 +01:00
/*!
* \brief Returns the stylesheet for dialogs and other windows used in my applications.
*/
const QString &dialogStyle()
{
2016-02-05 20:23:02 +01:00
# ifdef Q_OS_WIN32
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());
2016-02-05 20:23:02 +01:00
# else
static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
"*[classNames~=\"input-invalid\"] { color: red; }");
# endif
return style;
}
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
* the widget style does not update automatically when a property changes.
*/
void updateStyle(QWidget *widget)
{
widget->style()->unpolish(widget);
widget->style()->polish(widget);
widget->update();
}
#endif
} // namespace Dialogs