added Dialogs::updateStyle()

This commit is contained in:
Martchus 2016-02-05 20:23:02 +01:00
parent b5ee1adc3f
commit 1f890920e6
3 changed files with 55 additions and 19 deletions

View File

@ -9,7 +9,7 @@ namespace DesktopUtils {
/*! /*!
* \brief Shows the specified file or directory using the default file browser. * \brief Shows the specified file or directory using the default file browser.
* \remarks * \remarks \a path musn't be specified as URL. (Conversion to URL is the purpose of this function).
*/ */
bool LIB_EXPORT openLocalFileOrDir(const QString &path) bool LIB_EXPORT openLocalFileOrDir(const QString &path)
{ {

View File

@ -1,17 +1,22 @@
#include "./dialogutils.h" #include "./dialogutils.h"
#ifdef GUI_NONE #ifdef GUI_NONE
#include <QCoreApplication> # include <QCoreApplication>
#else #else
#include <QGuiApplication> # include <QGuiApplication>
#include <QPalette> # include <QPalette>
#include <QWidget> # include <QWidget>
# include <QStyle>
#endif #endif
#include <QFileInfo> #include <QFileInfo>
#include <QDir> #include <QDir>
namespace Dialogs { namespace Dialogs {
/*!
* \brief Generates the window title string for the specified \a documentStatus
* and \a documentPath.
*/
QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath) QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath)
{ {
switch(documentStatus) { switch(documentStatus) {
@ -38,38 +43,65 @@ QString generateWindowTitle(DocumentStatus documentStatus, const QString &docume
#ifndef GUI_NONE #ifndef GUI_NONE
#ifdef Q_OS_WIN32 # ifdef Q_OS_WIN32
/*!
* \brief Returns the color used to draw frames.
*/
QColor windowFrameColor() QColor windowFrameColor()
{ {
return QGuiApplication::palette().window().color().darker(108); return QGuiApplication::palette().window().color().darker(108);
} }
/*!
* \brief Returns the color used to draw instructions.
*/
QColor instructionTextColor() QColor instructionTextColor()
{ {
const auto baseColor = QGuiApplication::palette().base().color(); const auto baseColor = QGuiApplication::palette().base().color();
return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().text().color(); return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().text().color();
} }
#endif # endif
/*!
* \brief Returns the stylesheet for dialogs and other windows used in my applications.
*/
const QString &dialogStyle() const QString &dialogStyle()
{ {
#ifdef Q_OS_WIN32 # ifdef Q_OS_WIN32
static const auto style = QStringLiteral(" #mainWidget { color: palette(text); background-color: palette(base); border: none; }" 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; }" "#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; } ").arg( "QMessageBox QLabel, QInputDialog QLabel, *[classNames~=\"heading\"] { font-size: 12pt; color: %2; }"
"*[classNames~=\"input-invalid\"] { color: red; }").arg(
windowFrameColor().name(), instructionTextColor().name()); windowFrameColor().name(), instructionTextColor().name());
#else # else
static const auto style = QStringLiteral(" *[classNames~=\"heading\"] { font-weight: bold; } "); static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }"
#endif "*[classNames~=\"input-invalid\"] { color: red; }");
# endif
return style; return style;
} }
/*!
* \brief Makes \a widget a heading.
*/
void makeHeading(QWidget *widget) void makeHeading(QWidget *widget)
{ {
widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading")); widget->setProperty("classNames", widget->property("classNames").toStringList() << QStringLiteral("heading"));
} }
/*!
* \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 #endif
} // namespace Dialogs } // namespace Dialogs

View File

@ -11,21 +11,25 @@ QT_FORWARD_DECLARE_CLASS(QColor)
namespace Dialogs { namespace Dialogs {
/*!
* \brief The DocumentStatus enum specifies the status of the document in a window.
*/
enum class DocumentStatus { enum class DocumentStatus {
NoDocument, NoDocument, /**< There is no document opened. The document path is ignored in this case. */
Saved, Saved, /**< There is a document opened. All modifications have been saved yet. */
Unsaved Unsaved /**< There is a document opened and there are unsaved modifications. */
}; };
QString LIB_EXPORT generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath); QString LIB_EXPORT generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath);
#ifndef GUI_NONE #ifndef GUI_NONE
#ifdef Q_OS_WIN32 # ifdef Q_OS_WIN32
QColor LIB_EXPORT windowFrameColor(); QColor LIB_EXPORT windowFrameColor();
QColor LIB_EXPORT instructionTextColor(); QColor LIB_EXPORT instructionTextColor();
#endif # endif
const QString LIB_EXPORT &dialogStyle(); const QString LIB_EXPORT &dialogStyle();
void LIB_EXPORT makeHeading(QWidget *widget); void LIB_EXPORT makeHeading(QWidget *widget);
void LIB_EXPORT updateStyle(QWidget *widget);
#endif #endif
} // namespace Dialogs } // namespace Dialogs