LeechCraft 0.6.70-14794-g33744ae6ce
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/overload.h>
14#include <util/sll/prelude.h>
16#include "ui_wkfontswidget.h"
18
19namespace LC
20{
21namespace Util
22{
23 WkFontsWidget::WkFontsWidget (BaseSettingsManager *bsm, QWidget *parent)
24 : QWidget { parent }
25 , Ui_ { std::make_shared<Ui::WkFontsWidget> () }
26 , BSM_ { bsm }
27 {
28 Ui_->setupUi (this);
29
30 Family2Chooser_ [IWkFontsSettable::FontFamily::StandardFont] = Ui_->StandardChooser_;
31 Family2Chooser_ [IWkFontsSettable::FontFamily::FixedFont] = Ui_->FixedChooser_;
32 Family2Chooser_ [IWkFontsSettable::FontFamily::SerifFont] = Ui_->SerifChooser_;
33 Family2Chooser_ [IWkFontsSettable::FontFamily::SansSerifFont] = Ui_->SansSerifChooser_;
34 Family2Chooser_ [IWkFontsSettable::FontFamily::CursiveFont] = Ui_->CursiveChooser_;
35 Family2Chooser_ [IWkFontsSettable::FontFamily::FantasyFont] = Ui_->FantasyChooser_;
36
37 Family2Name_ [IWkFontsSettable::FontFamily::StandardFont] = "StandardFont";
38 Family2Name_ [IWkFontsSettable::FontFamily::FixedFont] = "FixedFont";
39 Family2Name_ [IWkFontsSettable::FontFamily::SerifFont] = "SerifFont";
40 Family2Name_ [IWkFontsSettable::FontFamily::SansSerifFont] = "SansSerifFont";
41 Family2Name_ [IWkFontsSettable::FontFamily::CursiveFont] = "CursiveFont";
42 Family2Name_ [IWkFontsSettable::FontFamily::FantasyFont] = "FantasyFont";
43
44 ResetFontChoosers ();
45
46 for (const auto& pair : Util::Stlize (Family2Chooser_))
47 connect (pair.second,
49 [this, pair] { PendingFontChanges_ [pair.first] = pair.second->GetFont (); });
50
51 Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFontSize] = Ui_->SizeDefault_;
52 Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = Ui_->SizeFixedWidth_;
53 Size2Spinbox_ [IWkFontsSettable::FontSize::MinimumFontSize] = Ui_->SizeMinimum_;
54
55 Size2Name_ [IWkFontsSettable::FontSize::DefaultFontSize] = "FontSize";
56 Size2Name_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = "FixedFontSize";
57 Size2Name_ [IWkFontsSettable::FontSize::MinimumFontSize] = "MinimumFontSize";
58
59 ResetSizeChoosers ();
60
61 for (const auto& pair : Util::Stlize (Size2Spinbox_))
62 connect (pair.second,
63 Util::Overload<int> (&QSpinBox::valueChanged),
64 [this, pair] { PendingSizeChanges_ [pair.first] = pair.second->value (); });
65 }
66
68 {
69 Settables_ << settable;
70 connect (settable->GetQObject (),
71 &QObject::destroyed,
72 [this, settable] { Settables_.removeOne (settable); });
73
74 for (const auto& pair : Util::Stlize (Family2Chooser_))
75 settable->SetFontFamily (pair.first, pair.second->GetFont ());
76
77 for (const auto& pair : Util::Stlize (Size2Spinbox_))
78 settable->SetFontSize (pair.first, pair.second->value ());
79 }
80
82 {
83 Size2Spinbox_ [type]->setValue (size);
84 PendingSizeChanges_ [type] = size;
85
86 QTimer::singleShot (1000, this, [this] { ApplyPendingSizeChanges (); });
87 }
88
89 void WkFontsWidget::ResetFontChoosers ()
90 {
91 for (const auto& pair : Util::Stlize (Family2Chooser_))
92 {
93 const auto& option = Family2Name_ [pair.first];
94 pair.second->SetFont (BSM_->property (option.data ()).value<QFont> ());
95 }
96 }
97
98 void WkFontsWidget::ResetSizeChoosers ()
99 {
100 for (const auto& pair : Util::Stlize (Size2Spinbox_))
101 {
102 const auto& option = Size2Name_ [pair.first];
103 pair.second->setValue (BSM_->Property (option, 10).toInt ());
104 }
105 }
106
107 void WkFontsWidget::ApplyPendingSizeChanges ()
108 {
109 for (const auto& pair : Util::Stlize (PendingSizeChanges_))
110 {
111 BSM_->setProperty (Size2Name_ [pair.first].data (), pair.second);
112 emit sizeChanged (pair.first, pair.second);
113
114 for (const auto settable : Settables_)
115 settable->SetFontSize (pair.first, pair.second);
116 }
117
118 PendingSizeChanges_.clear ();
119 }
120
121 void WkFontsWidget::on_ChangeAll__released ()
122 {
123 QHash<QString, QList<IWkFontsSettable::FontFamily>> families;
124 for (const auto& pair : Util::Stlize (Family2Chooser_))
125 families [pair.second->GetFont ().family ()] << pair.first;
126
127 const auto& stlized = Util::Stlize (families);
128 const auto& maxPair = *std::max_element (stlized.begin (), stlized.end (),
129 ComparingBy ([] (auto pair) { return pair.second.size (); }));
130
131 const auto dialog = new MassFontChangeDialog { maxPair.first, maxPair.second, this };
132 dialog->show ();
133 connect (dialog,
134 &QDialog::finished,
135 [dialog, this] (int result)
136 {
137 if (result == QDialog::Rejected)
138 return;
139
140 const auto& font = dialog->GetFont ();
141 for (const auto family : dialog->GetFamilies ())
142 {
143 PendingFontChanges_ [family] = font;
144 Family2Chooser_ [family]->SetFont (font);
145 }
146 });
147 }
148
150 {
151 ApplyPendingSizeChanges ();
152
153 for (const auto& pair : Util::Stlize (PendingFontChanges_))
154 {
155 BSM_->setProperty (Family2Name_ [pair.first].data (), pair.second);
156 emit fontChanged (pair.first, pair.second);
157
158 for (const auto settable : Settables_)
159 settable->SetFontFamily (pair.first, pair.second);
160 }
161
162 PendingFontChanges_.clear ();
163 }
164
166 {
167 ResetFontChoosers ();
168 ResetSizeChoosers ();
169
170 PendingFontChanges_.clear ();
171 PendingSizeChanges_.clear ();
172 }
173}
174}
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.
A settings widget for configuring WebKit fonts.
Definition: wkfontswidget.h:69
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 Stlize(Assoc &&assoc)
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:43
auto ComparingBy(R r)
Definition: prelude.h:221
Definition: constants.h:15
STL namespace.