1 #include "qt_helpers.hpp"
2 
3 #include <QString>
4 #include <QFont>
5 #include <QWidget>
6 #include <QStyle>
7 #include <QVariant>
8 #include <QDateTime>
9 
font_as_stylesheet(QFont const & font)10 QString font_as_stylesheet (QFont const& font)
11 {
12   QString font_weight;
13   switch (font.weight ())
14     {
15     case QFont::Light: font_weight = "light"; break;
16     case QFont::Normal: font_weight = "normal"; break;
17     case QFont::DemiBold: font_weight = "demibold"; break;
18     case QFont::Bold: font_weight = "bold"; break;
19     case QFont::Black: font_weight = "black"; break;
20     }
21   return QString {
22       " font-family: %1;\n"
23       " font-size: %2pt;\n"
24       " font-style: %3;\n"
25       " font-weight: %4;\n"}
26   .arg (font.family ())
27      .arg (font.pointSize ())
28      .arg (font.styleName ())
29      .arg (font_weight);
30 }
31 
update_dynamic_property(QWidget * widget,char const * property,QVariant const & value)32 void update_dynamic_property (QWidget * widget, char const * property, QVariant const& value)
33 {
34   widget->setProperty (property, value);
35   widget->style ()->unpolish (widget);
36   widget->style ()->polish (widget);
37   widget->update ();
38 }
39 
qt_round_date_time_to(QDateTime dt,int milliseconds)40 QDateTime qt_round_date_time_to (QDateTime dt, int milliseconds)
41 {
42   dt.setMSecsSinceEpoch (dt.addMSecs (milliseconds / 2).toMSecsSinceEpoch () / milliseconds * milliseconds);
43   return dt;
44 }
45 
qt_truncate_date_time_to(QDateTime dt,int milliseconds)46 QDateTime qt_truncate_date_time_to (QDateTime dt, int milliseconds)
47 {
48   dt.setMSecsSinceEpoch (dt.toMSecsSinceEpoch () / milliseconds * milliseconds);
49   return dt;
50 }
51