swift
settingstextmessagestyle.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QKeySequence>
7 #include <QPushButton>
8 #include <QRegularExpression>
9 #include <QShortcut>
10 #include <QStringBuilder>
11 #include <QTextEdit>
12 
13 #include "ui_settingstextmessagestyle.h"
14 
17 #include "gui/shortcut.h"
18 
19 namespace swift::gui::components
20 {
22  : QFrame(parent), ui(new Ui::CSettingsTextMessageStyle)
23  {
24  ui->setupUi(this);
25 
26  connect(ui->pb_Font, &QPushButton::released, this, &CSettingsTextMessageStyle::changeFont);
27  connect(ui->pb_Style, &QPushButton::released, this, &CSettingsTextMessageStyle::changeStyle);
28  connect(ui->pb_Reset, &QPushButton::released, this, &CSettingsTextMessageStyle::resetStyle);
29  connect(ui->pb_Reset, &QPushButton::released, this, &CSettingsTextMessageStyle::changed);
30  connect(ui->pb_FontMinus, &QPushButton::released, this, &CSettingsTextMessageStyle::fontSizeMinus);
31  connect(ui->pb_FontPlus, &QPushButton::released, this, &CSettingsTextMessageStyle::fontSizePlus);
32 
33  // QShortcut *sc = new QShortcut(CShortcut::keyFontMinus(), this);
34  // sc->setContext(Qt::WidgetWithChildrenShortcut);
35  // QObject::connect(sc, &QShortcut::activatedAmbiguously, this,
36  // &CSettingsTextMessageStyle::fontSizePlus);
37  }
38 
40 
42  {
43  if (m_fontSettingsDialog) { return m_fontSettingsDialog->getFamilySizeStyle(); }
44 
45  static const QStringList empty({ "", "", "" });
46  return empty;
47  }
48 
49  void CSettingsTextMessageStyle::changeFont()
50  {
51  if (!m_fontSettingsDialog)
52  {
53  m_fontSettingsDialog = new CSettingsFontDialog(this);
54  m_fontSettingsDialog->setWithColorSelection(false);
55  }
56 
57  const QDialog::DialogCode r = static_cast<QDialog::DialogCode>(m_fontSettingsDialog->exec());
58  if (r == QDialog::Accepted)
59  {
60  const QStringList familySizeStyle = this->getFamilySizeStyle();
61  this->setFontFamilySizeStyle(familySizeStyle);
62  emit this->changed();
63  }
64  }
65 
66  void CSettingsTextMessageStyle::changeStyle()
67  {
68  if (!m_textEditDialog)
69  {
70  m_textEditDialog = new CTextEditDialog(this);
71  m_textEditDialog->resize(400, 300);
72  }
73 
74  m_textEditDialog->textEdit()->setPlainText(m_style);
75  const QDialog::DialogCode r = static_cast<QDialog::DialogCode>(m_textEditDialog->exec());
76  if (r == QDialog::Accepted)
77  {
78  m_style = m_textEditDialog->textEdit()->toPlainText();
79  emit this->changed();
80  }
81  }
82 
83  bool CSettingsTextMessageStyle::setFontFamilySizeStyle(const QStringList &familySizeStlye)
84  {
85  if (familySizeStlye.size() != 3) { return false; }
86  static const QString f("font-family: \"%1\"; font-size: %2; font-style: %3");
87 
88  const QString tableStyle =
89  u"table { " % f.arg(familySizeStlye.at(0), familySizeStlye.at(1), familySizeStlye.at(2)) % u" }";
90  this->replaceTableStyle(tableStyle);
91  return true;
92  }
93 
94  void CSettingsTextMessageStyle::replaceTableStyle(const QString &newTableStyle)
95  {
96  QString style = m_style;
97  thread_local const QRegularExpression re("table\\s*\\{.*\\}");
98  style.replace(re, newTableStyle);
99  m_style = style;
100  }
101 
103  {
104  if (this->changeFontSize(false)) { emit this->changed(); }
105  }
106 
108  {
109  if (this->changeFontSize(true)) { emit this->changed(); }
110  }
111 
112  bool CSettingsTextMessageStyle::changeFontSize(bool increase)
113  {
114  QString style = m_style;
115  thread_local const QRegularExpression re("table\\s*\\{.*:\\s*(\\d{1,2}).*\\}");
116  const QRegularExpressionMatch match = re.match(style);
117  const QStringList matches = match.capturedTexts();
118  if (matches.size() != 2) { return false; }
119 
120  bool ok;
121  int ptSize = matches.last().toInt(&ok);
122  if (!ok) { return false; }
123  if (increase)
124  {
125  ptSize++;
126  if (ptSize > 16) { return false; }
127  }
128  else
129  {
130  ptSize--;
131  if (ptSize < 6) { return false; }
132  }
133 
134  const QString pt = QString::number(ptSize) % u"pt";
135  QString tableStyle = matches.front();
136  thread_local const QRegularExpression rePt("\\d{1,2}pt");
137  tableStyle.replace(rePt, pt);
138  this->replaceTableStyle(tableStyle);
139  return true;
140  }
141 } // namespace swift::gui::components
void setWithColorSelection(bool withColor)
With color selection.
QStringList getFamilySizeStyle() const
Family, size and style.
CSettingsTextMessageStyle(QWidget *parent=nullptr)
Constructor.
QStringList getFamilySizeStyle() const
Fmily, size and style.
void changed()
Font or style changed from within the component.
QTextEdit * textEdit() const
Access to text edit.
High level reusable GUI components.
Definition: aboutdialog.cpp:13