LeechCraft 0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
wkfontswidget.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "wkfontswidget.h"
10#include <QTimer>
11#include <xmlsettingsdialog/basesettingsmanager.h>
12#include <util/sll/qtutil.h>
13#include <util/sll/prelude.h>
15#include "ui_wkfontswidget.h"
17
18namespace LC::Util
19{
20 WkFontsWidget::WkFontsWidget (BaseSettingsManager *bsm, QWidget *parent)
21 : QWidget { parent }
22 , Ui_ { std::make_shared<Ui::WkFontsWidget> () }
23 , BSM_ { bsm }
24 {
25 Ui_->setupUi (this);
26
27 Family2Chooser_ [IWkFontsSettable::FontFamily::StandardFont] = Ui_->StandardChooser_;
28 Family2Chooser_ [IWkFontsSettable::FontFamily::FixedFont] = Ui_->FixedChooser_;
29 Family2Chooser_ [IWkFontsSettable::FontFamily::SerifFont] = Ui_->SerifChooser_;
30 Family2Chooser_ [IWkFontsSettable::FontFamily::SansSerifFont] = Ui_->SansSerifChooser_;
31 Family2Chooser_ [IWkFontsSettable::FontFamily::CursiveFont] = Ui_->CursiveChooser_;
32 Family2Chooser_ [IWkFontsSettable::FontFamily::FantasyFont] = Ui_->FantasyChooser_;
33
34 Family2Name_ [IWkFontsSettable::FontFamily::StandardFont] = "StandardFont"_qba;
35 Family2Name_ [IWkFontsSettable::FontFamily::FixedFont] = "FixedFont"_qba;
36 Family2Name_ [IWkFontsSettable::FontFamily::SerifFont] = "SerifFont"_qba;
37 Family2Name_ [IWkFontsSettable::FontFamily::SansSerifFont] = "SansSerifFont"_qba;
38 Family2Name_ [IWkFontsSettable::FontFamily::CursiveFont] = "CursiveFont"_qba;
39 Family2Name_ [IWkFontsSettable::FontFamily::FantasyFont] = "FantasyFont"_qba;
40
41 ResetFontChoosers ();
42
43 for (const auto& [family, chooser] : std::as_const (Family2Chooser_).asKeyValueRange ())
44 connect (chooser,
46 this,
47 [this, family, chooser] { PendingFontChanges_ [family] = chooser->GetFont (); });
48
49 Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFontSize] = Ui_->SizeDefault_;
50 Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = Ui_->SizeFixedWidth_;
51 Size2Spinbox_ [IWkFontsSettable::FontSize::MinimumFontSize] = Ui_->SizeMinimum_;
52
53 Size2Name_ [IWkFontsSettable::FontSize::DefaultFontSize] = "FontSize"_qba;
54 Size2Name_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = "FixedFontSize"_qba;
55 Size2Name_ [IWkFontsSettable::FontSize::MinimumFontSize] = "MinimumFontSize"_qba;
56
57 ResetSizeChoosers ();
58
59 for (const auto& [size, spinbox] : std::as_const (Size2Spinbox_).asKeyValueRange ())
60 connect (spinbox,
61 &QSpinBox::valueChanged,
62 this,
63 [this, size, spinbox] { PendingSizeChanges_ [size] = spinbox->value (); });
64
65 connect (Ui_->ChangeAll_,
66 &QPushButton::released,
67 this,
68 &WkFontsWidget::ChangeAllFonts);
69 }
70
72 {
73 Settables_ << settable;
74 connect (settable->GetQObject (),
75 &QObject::destroyed,
76 this,
77 [this, settable] { Settables_.removeOne (settable); });
78
79 for (const auto& pair : std::as_const (Family2Chooser_).asKeyValueRange ())
80 settable->SetFontFamily (pair.first, pair.second->GetFont ());
81
82 for (const auto& pair : std::as_const (Size2Spinbox_).asKeyValueRange ())
83 settable->SetFontSize (pair.first, pair.second->value ());
84 }
85
87 {
88 Size2Spinbox_ [type]->setValue (size);
89 PendingSizeChanges_ [type] = size;
90
91 QTimer::singleShot (1000, this, [this] { ApplyPendingSizeChanges (); });
92 }
93
94 void WkFontsWidget::ResetFontChoosers ()
95 {
96 for (const auto& pair : std::as_const (Family2Chooser_).asKeyValueRange ())
97 {
98 const auto& option = Family2Name_ [pair.first];
99 pair.second->SetFont (BSM_->property (option.data ()).value<QFont> ());
100 }
101 }
102
103 void WkFontsWidget::ResetSizeChoosers ()
104 {
105 for (const auto& pair : std::as_const (Size2Spinbox_).asKeyValueRange ())
106 {
107 const auto& option = Size2Name_ [pair.first];
108 pair.second->setValue (BSM_->Property (option, 10).toInt ());
109 }
110 }
111
112 void WkFontsWidget::ApplyPendingSizeChanges ()
113 {
114 for (const auto& pair : std::as_const (PendingSizeChanges_).asKeyValueRange ())
115 {
116 BSM_->setProperty (Size2Name_ [pair.first].data (), pair.second);
117 emit sizeChanged (pair.first, pair.second);
118
119 for (const auto settable : Settables_)
120 settable->SetFontSize (pair.first, pair.second);
121 }
122
123 PendingSizeChanges_.clear ();
124 }
125
126 void WkFontsWidget::ChangeAllFonts ()
127 {
128 QHash<QString, QList<IWkFontsSettable::FontFamily>> families;
129 for (const auto& pair : std::as_const (Family2Chooser_).asKeyValueRange ())
130 families [pair.second->GetFont ().family ()] << pair.first;
131
132 const auto& stlized = std::as_const (families).asKeyValueRange ();
133 const auto& maxPair = *std::max_element (stlized.begin (), stlized.end (),
134 ComparingBy ([] (auto pair) { return pair.second.size (); }));
135
136 const auto dialog = new MassFontChangeDialog { maxPair.first, maxPair.second, this };
137 dialog->show ();
138 connect (dialog,
139 &QDialog::finished,
140 this,
141 [dialog, this] (int result)
142 {
143 if (result == QDialog::Rejected)
144 return;
145
146 const auto& font = dialog->GetFont ();
147 for (const auto family : dialog->GetFamilies ())
148 {
149 PendingFontChanges_ [family] = font;
150 Family2Chooser_ [family]->SetFont (font);
151 }
152 });
153 }
154
156 {
157 ApplyPendingSizeChanges ();
158
159 for (const auto& pair : std::as_const (PendingFontChanges_).asKeyValueRange ())
160 {
161 BSM_->setProperty (Family2Name_ [pair.first].data (), pair.second);
162 emit fontChanged (pair.first, pair.second);
163
164 for (const auto settable : Settables_)
165 settable->SetFontFamily (pair.first, pair.second);
166 }
167
168 PendingFontChanges_.clear ();
169 }
170
172 {
173 ResetFontChoosers ();
174 ResetSizeChoosers ();
175
176 PendingFontChanges_.clear ();
177 PendingSizeChanges_.clear ();
178 }
179}
Interface to aid WebKit-like-view-containing tabs to expose the view fonts configuration to the user.
virtual void SetFontSize(FontSize type, int size)=0
Sets the size for the given font size type.
virtual QObject * GetQObject()=0
Returns this tab as a QObject.
virtual void SetFontFamily(FontFamily family, const QFont &font)=0
Sets the font for the given font family.
FontSize
Enumeration for possible font sizes.
void fontChanged(QFont font)
Emitted when another font has been chosen.
void RegisterSettable(IWkFontsSettable *settable)
Registers an object to be automatically updated whenever font settings change.
void sizeChanged(IWkFontsSettable::FontSize type, int size)
Notifies the size for the given font type has been changed.
void SetSize(IWkFontsSettable::FontSize type, int size)
Sets the size for the given font size type.
WkFontsWidget(Util::BaseSettingsManager *bsm, QWidget *parent=nullptr)
Creates the fonts settings widget.
void fontChanged(IWkFontsSettable::FontFamily family, const QFont &font)
Notifies the font for the given family has been changed.
auto ComparingBy(R r)
Definition prelude.h:207