swift
settingsfontcomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QColorDialog>
7 #include <QPushButton>
8 #include <QStringBuilder>
9 
10 #include "ui_settingsfontcomponent.h"
11 
12 #include "gui/guiapplication.h"
13 #include "gui/guiutility.h"
14 #include "misc/logmessage.h"
15 
16 using namespace swift::misc;
17 
18 namespace swift::gui::components
19 {
20  CSettingsFontComponent::CSettingsFontComponent(QWidget *parent) : QFrame(parent), ui(new Ui::CSettingsFontComponent)
21  {
22  ui->setupUi(this);
24 
25  // due to the problems with overriding a color (e.g T571) we use "no color" as default
26  this->initValues();
27  connect(ui->tb_SettingsGuiFontColor, &QToolButton::clicked, this, &CSettingsFontComponent::fontColorDialog);
28  connect(ui->tb_SettingsGuiNoFontColor, &QToolButton::clicked, this, &CSettingsFontComponent::noColor);
29  connect(ui->pb_Ok, &QPushButton::clicked, this, &CSettingsFontComponent::changeFont, Qt::QueuedConnection);
30  connect(ui->pb_CancelOrReset, &QToolButton::clicked, this, &CSettingsFontComponent::resetFontAndReject,
31  Qt::QueuedConnection);
32  connect(ui->pb_Reset, &QToolButton::clicked, this, &CSettingsFontComponent::clearQssAndResetFont,
33  Qt::QueuedConnection);
34 
35  // only after the complete startup style sheet font overrides are available
36  connect(sGui, &CGuiApplication::startUpCompleted, this, &CSettingsFontComponent::initValues);
37  }
38 
40 
42  {
43  m_mode = m;
45  {
46  ui->pb_CancelOrReset->setText("reset");
47  ui->pb_Reset->setVisible(false);
48  }
49  else
50  {
51  ui->pb_CancelOrReset->setText("cancel");
52  ui->pb_Reset->setVisible(true);
53  }
54  }
55 
56  void CSettingsFontComponent::setFont(const QFont &font)
57  {
58  m_cancelFont = font;
59  this->resetFont();
60  }
61 
62  QFont CSettingsFontComponent::getFont() const { return ui->cb_SettingsGuiFont->font(); }
63 
65  {
66  const QString fontSize = ui->cb_SettingsGuiFontSize->currentText().append("pt");
67  const QString fontFamily = ui->cb_SettingsGuiFont->currentFont().family();
68  const QString fontStyleCombined = ui->cb_SettingsGuiFontStyle->currentText();
69  return QStringList({ fontFamily, fontSize, fontStyleCombined });
70  }
71 
73  {
74  ui->le_SettingsGuiFontColor->setVisible(withColor);
75  ui->tb_SettingsGuiFontColor->setVisible(withColor);
76  }
77 
79  {
80  if (!sGui || sGui->isShuttingDown()) { return; }
81  m_noColorDefault = false;
82  this->initValues();
83  }
84 
85  void CSettingsFontComponent::changeFont()
86  {
87  if (!sGui || sGui->isShuttingDown()) { return; }
88  const QString fontSize = ui->cb_SettingsGuiFontSize->currentText().append("pt");
89  const QString fontFamily = ui->cb_SettingsGuiFont->currentFont().family();
90  const QString fontStyleCombined = ui->cb_SettingsGuiFontStyle->currentText();
91 
92  QString fontColor = m_selectedColor.name();
93  if (!m_selectedColor.isValid() || m_selectedColor.name().isEmpty()) { fontColor.clear(); }
94  ui->le_SettingsGuiFontColor->setText(fontColor);
95  m_qss = CStyleSheetUtility::asStylesheet(fontFamily, fontSize, CStyleSheetUtility::fontStyle(fontStyleCombined),
96  CStyleSheetUtility::fontWeight(fontStyleCombined), fontColor);
98  {
99  const bool ok = sGui->updateFont(m_qss);
100  if (ok) { CLogMessage(this).info(u"Updated font style"); }
101  else { CLogMessage(this).warning(u"Updating style failed"); }
102  }
103  emit this->accept();
104  }
105 
106  void CSettingsFontComponent::fontColorDialog()
107  {
108  const QColor c =
109  QColorDialog::getColor(m_selectedColor.isValid() ? m_selectedColor : m_cancelColor, this, "Font color");
110  if (c == m_selectedColor) { return; }
111  m_selectedColor = c;
112  ui->le_SettingsGuiFontColor->setText(m_selectedColor.name());
113  }
114 
115  void CSettingsFontComponent::noColor()
116  {
117  m_selectedColor = QColor(); // invalid color
118  m_noColorDefault = true;
119  ui->le_SettingsGuiFontColor->clear();
120  }
121 
122  void CSettingsFontComponent::initValues()
123  {
124  // Font
125  if (!sGui) { return; }
126  m_cancelFont = this->font();
127  const QString colorString(sGui->getStyleSheetUtility().fontColorString());
128  m_cancelColor = colorString.isEmpty() ? QColor() : QColor(colorString);
129  this->initUiValues(m_cancelFont, m_noColorDefault ? QColor() : m_cancelColor);
130  }
131 
132  void CSettingsFontComponent::initUiValues(const QFont &font, const QColor &color)
133  {
134  ui->cb_SettingsGuiFontStyle->setCurrentText(CStyleSheetUtility::fontAsCombinedWeightStyle(font));
135  ui->cb_SettingsGuiFont->setCurrentFont(font);
136  ui->cb_SettingsGuiFontSize->setCurrentText(QString::number(font.pointSize()));
137 
138  const bool valid = color.isValid();
139  m_selectedColor = color; // color
140  ui->le_SettingsGuiFontColor->setText(valid ? m_selectedColor.name() : "");
141  m_qss.clear();
142  }
143 
144  void CSettingsFontComponent::resetFont()
145  {
146  if (!sGui || sGui->isShuttingDown()) { return; }
147  this->initUiValues(m_cancelFont, m_noColorDefault ? QColor() : m_cancelColor);
149  }
150 
151  void CSettingsFontComponent::resetFontAndReject()
152  {
153  this->resetFont();
154  emit this->reject();
155  }
156 
157  void CSettingsFontComponent::clearQssAndResetFont()
158  {
159  m_qss.clear();
160  this->resetFont();
161  emit this->accept();
162  }
163 } // namespace swift::gui::components
bool isShuttingDown() const
Is application shutting down?
void startUpCompleted(bool success)
Startup has been completed Will be triggered shortly before starting the event loop.
const CStyleSheetUtility & getStyleSheetUtility() const
Style sheet handling.
bool resetFont()
Reset the font to default.
bool updateFont(const QString &fontFamily, const QString &fontSize, const QString &fontStyle, const QString &fontWeight, const QString &fontColor)
Update the fonts.
static QString asStylesheet(const QString &fontFamily, const QString &fontSize, const QString &fontStyle, const QString &fontWeight, const QString &fontColorString={})
Parameters as stylesheet.
static QString fontStyle(const QString &combinedStyleAndWeight)
Get the font style.
static QString fontAsCombinedWeightStyle(const QFont &font)
Font as combined weight and style.
QString fontColorString() const
Current font color from style sheet.
static QString fontWeight(const QString &combinedStyleAndWeight)
Get the font weight.
void reject()
To be used with dialogs.
void setFont(const QFont &font)
Set the current font.
void accept()
To be used with dialogs.
void setWithColorSelection(bool withColor)
With color selection.
QStringList getFamilySizeStyle() const
Strings such as.
@ DirectUpdate
directly updating a font qss file
void setStyleSheetDefaultColor()
Set the default color from style sheet.
Class for emitting a log message.
Definition: logmessage.h:27
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.