summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2023-04-12 22:48:29 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2023-04-12 22:48:29 +0800
commitb5f8ec99fd8e79871b2a5df0966f29bcf244632f (patch)
treed66b2c9b749c85bbf5e6affad00fadbc79eb567a
parent6188a0034cb971f71a1683450b1bc4dce8d45d4c (diff)
Headings support
-rw-r--r--Bootstrap.cxx117
-rw-r--r--Bootstrap.hxx28
-rw-r--r--Label.qml16
3 files changed, 135 insertions, 26 deletions
diff --git a/Bootstrap.cxx b/Bootstrap.cxx
index e550725..d123dca 100644
--- a/Bootstrap.cxx
+++ b/Bootstrap.cxx
@@ -1,6 +1,28 @@
#include "tomlc99/toml.h"
#include "Bootstrap.hxx"
+#define OVERRIDE_FONT_FAMILY(A, B) \
+ {\
+ auto fontFamily = toml_string_in(bootstrap, A);\
+ if (fontFamily.ok) {\
+ B.setFamily(fontFamily.u.s);\
+ free(fontFamily.u.s);\
+ bsBodyFont.setStyleHint(QFont::AnyStyle);\
+ }\
+ }
+
+#define OVERRIDE_FONT_SIZE(A, B) \
+ {\
+ auto fontSize = toml_double_in(bootstrap, A);\
+ if (fontSize.ok) B.setPointSizeF(fontSize.u.d);\
+ }
+
+#define OVERRIDE_FONT_WEIGHT(A, B) \
+ {\
+ auto fontWeight = toml_int_in(bootstrap, A);\
+ if (fontWeight.ok) B.setWeight(fontWeight.u.i);\
+ }
+
#define OVERRIDE_COLORS(A, B) \
colors = toml_array_in(bootstrap, A);\
if (colors) for (int i = 0; ; i++) {\
@@ -10,6 +32,15 @@
free(color.u.s);\
}
+#define OVERRIDE_HX_FONT_SIZE(A, B) \
+ size = toml_array_in(bootstrap, A);\
+ if (size) {\
+ auto rem = toml_double_at(size, 0);\
+ if (rem.ok) B[0] = rem.u.d * 16.0;\
+ auto factor = toml_double_at(size, 1);\
+ if (factor.ok) B[1] = factor.u.d / 100.0;\
+ }
+
Bootstrap::Bootstrap(QObject *parent):
QObject{parent},
bsMode{Mode::Light},
@@ -29,13 +60,25 @@ Bootstrap::Bootstrap(QObject *parent):
bsBodyFont{bsFontSansSerif},
bodyColors{{"#212529", "#adb5bd"}},
bodyBgs{{"#fff", "#212529"}},
+ borderColors{{"#dee2e6", "#495057"}},
+ htmlHeading{0},
+ bsHeadingFont{bsFontSansSerif},
headingColors{{bodyColors.at(0), bodyColors.at(1)}},
- borderColors{{"#dee2e6", "#495057"}}
+ h1FontSize{{22.0, .015}},
+ h2FontSize{{21.2, .009}},
+ h3FontSize{{20.8, .006}},
+ h4FontSize{{20.4, .003}},
+ h5FontSize{{20.0, .0}},
+ h6FontSize{{16.0, .0}}
{
bsBodyFont.setStyleHint(QFont::SansSerif);
bsBodyFont.setPointSizeF(16.0);
bsBodyFont.setWeight(QFont::Normal);
+ bsHeadingFont.setStyleHint(QFont::SansSerif);
+ bsHeadingFont.setPointSizeF(16.0);
+ bsHeadingFont.setWeight(QFont::Medium);
+
QFile conf{QStringLiteral(":/qtquickcontrols2.conf")};
if (!conf.open(QIODevice::ReadOnly | QIODevice::Text)) return;
auto toml = toml_parse(conf.readAll().data(), nullptr, 0);
@@ -50,23 +93,27 @@ Bootstrap::Bootstrap(QObject *parent):
auto mode = toml_int_in(bootstrap, "Mode");
if (mode.ok) bsMode = static_cast<Mode>(mode.u.i);
- auto fontFamily = toml_string_in(bootstrap, "BodyFontFamily");
- if (fontFamily.ok) {
- bsBodyFont.setFamily(fontFamily.u.s);
- free(fontFamily.u.s);
- bsBodyFont.setStyleHint(QFont::AnyStyle);
- }
- auto fontSize = toml_double_in(bootstrap, "BodyFontSize");
- if (fontSize.ok) bsBodyFont.setPointSizeF(fontSize.u.d);
- auto fontWeight = toml_int_in(bootstrap, "BodyFontWeight");
- if (fontWeight.ok) bsBodyFont.setWeight(fontWeight.u.i);
+ OVERRIDE_FONT_FAMILY("BodyFontFamily", bsBodyFont);
+ OVERRIDE_FONT_SIZE("BodyFontSize", bsBodyFont);
+ OVERRIDE_FONT_WEIGHT("BodyFontWeight", bsBodyFont);
toml_array_t *colors;
OVERRIDE_COLORS("BodyColors", bodyColors);
OVERRIDE_COLORS("BodyBgs", bodyBgs);
- OVERRIDE_COLORS("HeadingColors", headingColors);
OVERRIDE_COLORS("BorderColors", borderColors);
+ OVERRIDE_FONT_FAMILY("HeadingFontFamily", bsHeadingFont);
+ OVERRIDE_FONT_SIZE("HeadingFontSize", bsHeadingFont);
+ OVERRIDE_FONT_WEIGHT("HeadingFontWeight", bsHeadingFont);
+ OVERRIDE_COLORS("HeadingColors", headingColors);
+ toml_array_t *size;
+ OVERRIDE_HX_FONT_SIZE("H1FontSize", h1FontSize);
+ OVERRIDE_HX_FONT_SIZE("H2FontSize", h2FontSize);
+ OVERRIDE_HX_FONT_SIZE("H3FontSize", h3FontSize);
+ OVERRIDE_HX_FONT_SIZE("H4FontSize", h4FontSize);
+ OVERRIDE_HX_FONT_SIZE("H5FontSize", h5FontSize);
+ OVERRIDE_HX_FONT_SIZE("H6FontSize", h6FontSize);
+
toml_free(toml);
}
@@ -87,8 +134,8 @@ void Bootstrap::setMode(Mode mode)
emit modeChanged();
emit bodyColorChanged();
emit bodyBgChanged();
- emit headingColorChanged();
emit borderColorChanged();
+ emit headingColorChanged();
}
Bootstrap::Theme Bootstrap::theme() const
@@ -123,12 +170,48 @@ QColor Bootstrap::bodyBg() const
return bodyBgs.at(static_cast<int>(bsMode));
}
-QColor Bootstrap::headingColor() const
+QColor Bootstrap::borderColor() const
{
- return headingColors.at(static_cast<int>(bsMode));
+ return borderColors.at(static_cast<int>(bsMode));
}
-QColor Bootstrap::borderColor() const
+int Bootstrap::heading() const
{
- return borderColors.at(static_cast<int>(bsMode));
+ return htmlHeading;
+}
+
+void Bootstrap::setHeading(int heading)
+{
+ if (heading == htmlHeading) return;
+ htmlHeading = heading;
+ bsHeadingFont.setPointSizeF(headingFontSize());
+ emit headingChanged();
+}
+
+QFont Bootstrap::headingFont() const
+{
+ return bsHeadingFont;
+}
+
+qreal Bootstrap::headingFontSize(const qreal parentWidth)
+{
+ switch (htmlHeading) {
+ case 1:
+ return h1FontSize[0] + h1FontSize[1] * parentWidth;
+ case 2:
+ return h2FontSize[0] + h2FontSize[1] * parentWidth;
+ case 3:
+ return h3FontSize[0] + h3FontSize[1] * parentWidth;
+ case 4:
+ return h4FontSize[0] + h4FontSize[1] * parentWidth;
+ case 5:
+ return h5FontSize[0] + h5FontSize[1] * parentWidth;
+ default:
+ return h6FontSize[0] + h6FontSize[1] * parentWidth;
+ }
+}
+
+QColor Bootstrap::headingColor() const
+{
+ return headingColors.at(static_cast<int>(bsMode));
}
diff --git a/Bootstrap.hxx b/Bootstrap.hxx
index 6d27d07..320f76c 100644
--- a/Bootstrap.hxx
+++ b/Bootstrap.hxx
@@ -22,10 +22,13 @@ class Bootstrap : public QObject
Q_PROPERTY(QFont bodyFont READ bodyFont NOTIFY bodyFontChanged)
Q_PROPERTY(QColor bodyColor READ bodyColor NOTIFY bodyColorChanged)
Q_PROPERTY(QColor bodyBg READ bodyBg NOTIFY bodyBgChanged)
- Q_PROPERTY(QColor headingColor READ headingColor
- NOTIFY headingColorChanged)
Q_PROPERTY(QColor borderColor READ borderColor
NOTIFY borderColorChanged)
+ Q_PROPERTY(int heading READ heading WRITE setHeading
+ NOTIFY headingChanged)
+ Q_PROPERTY(QFont headingFont READ headingFont NOTIFY headingFontChanged)
+ Q_PROPERTY(QColor headingColor READ headingColor
+ NOTIFY headingColorChanged)
QML_ELEMENT
QML_UNCREATABLE("Bootstrap is an attached property")
@@ -61,9 +64,14 @@ class Bootstrap : public QObject
QFont bodyFont() const;
QColor bodyColor() const;
QColor bodyBg() const;
- QColor headingColor() const;
QColor borderColor() const;
+ int heading() const;
+ void setHeading(int heading);
+ QFont headingFont() const;
+ Q_INVOKABLE qreal headingFontSize(const qreal parentWidth = .0);
+ QColor headingColor() const;
+
signals:
void themeChanged();
void modeChanged();
@@ -71,8 +79,10 @@ class Bootstrap : public QObject
void bodyFontChanged();
void bodyColorChanged();
void bodyBgChanged();
- void headingColorChanged();
void borderColorChanged();
+ void headingChanged();
+ void headingFontChanged();
+ void headingColorChanged();
private:
Mode bsMode;
@@ -81,8 +91,16 @@ class Bootstrap : public QObject
QFont bsBodyFont;
QVector<QColor> bodyColors;
QVector<QColor> bodyBgs;
- QVector<QColor> headingColors;
QVector<QColor> borderColors;
+ int htmlHeading;
+ QFont bsHeadingFont;
+ QVector<QColor> headingColors;
+ QVector<qreal> h1FontSize;
+ QVector<qreal> h2FontSize;
+ QVector<qreal> h3FontSize;
+ QVector<qreal> h4FontSize;
+ QVector<qreal> h5FontSize;
+ QVector<qreal> h6FontSize;
};
QML_DECLARE_TYPEINFO(Bootstrap, QML_HAS_ATTACHED_PROPERTIES)
diff --git a/Label.qml b/Label.qml
index c2038e0..3b12c5d 100644
--- a/Label.qml
+++ b/Label.qml
@@ -5,10 +5,18 @@ import Bootstrap 5.3
Label {
id: control
font {
- family: Bootstrap.bodyFont.family
- pointSize: Bootstrap.bodyFont.pointSize
- weight: Bootstrap.bodyFont.weight
+ family: Bootstrap.heading
+ ? Bootstrap.headingFont.family
+ : Bootstrap.bodyFont.family
+ pointSize: Bootstrap.heading
+ ? Bootstrap.headingFontSize(parent.width)
+ : Bootstrap.bodyFont.pointSize
+ weight: Bootstrap.heading
+ ? Bootstrap.headingFont.weight
+ : Bootstrap.bodyFont.weight
}
- color: Bootstrap.bodyColor
+ color: Bootstrap.heading
+ ? Bootstrap.headingColor
+ : Bootstrap.bodyColor
linkColor: control.palette.link
}