LeechCraft  0.6.70-11552-gf61ee51c3d
Modular cross-platform feature rich live environment.
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "wkfontswidget.h"
31 #include <QTimer>
32 #include <xmlsettingsdialog/basesettingsmanager.h>
33 #include <util/sll/qtutil.h>
34 #include <util/sll/overload.h>
35 #include <util/sll/prelude.h>
37 #include "ui_wkfontswidget.h"
38 #include "massfontchangedialog.h"
39 
40 namespace LeechCraft
41 {
42 namespace Util
43 {
44  WkFontsWidget::WkFontsWidget (BaseSettingsManager *bsm, QWidget *parent)
45  : QWidget { parent }
46  , Ui_ { std::make_shared<Ui::WkFontsWidget> () }
47  , BSM_ { bsm }
48  {
49  Ui_->setupUi (this);
50 
51  Family2Chooser_ [IWkFontsSettable::FontFamily::StandardFont] = Ui_->StandardChooser_;
52  Family2Chooser_ [IWkFontsSettable::FontFamily::FixedFont] = Ui_->FixedChooser_;
53  Family2Chooser_ [IWkFontsSettable::FontFamily::SerifFont] = Ui_->SerifChooser_;
54  Family2Chooser_ [IWkFontsSettable::FontFamily::SansSerifFont] = Ui_->SansSerifChooser_;
55  Family2Chooser_ [IWkFontsSettable::FontFamily::CursiveFont] = Ui_->CursiveChooser_;
56  Family2Chooser_ [IWkFontsSettable::FontFamily::FantasyFont] = Ui_->FantasyChooser_;
57 
58  Family2Name_ [IWkFontsSettable::FontFamily::StandardFont] = "StandardFont";
59  Family2Name_ [IWkFontsSettable::FontFamily::FixedFont] = "FixedFont";
60  Family2Name_ [IWkFontsSettable::FontFamily::SerifFont] = "SerifFont";
61  Family2Name_ [IWkFontsSettable::FontFamily::SansSerifFont] = "SansSerifFont";
62  Family2Name_ [IWkFontsSettable::FontFamily::CursiveFont] = "CursiveFont";
63  Family2Name_ [IWkFontsSettable::FontFamily::FantasyFont] = "FantasyFont";
64 
65  ResetFontChoosers ();
66 
67  for (const auto& pair : Util::Stlize (Family2Chooser_))
68  connect (pair.second,
70  [this, pair] { PendingFontChanges_ [pair.first] = pair.second->GetFont (); });
71 
72  Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFontSize] = Ui_->SizeDefault_;
73  Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = Ui_->SizeFixedWidth_;
74  Size2Spinbox_ [IWkFontsSettable::FontSize::MinimumFontSize] = Ui_->SizeMinimum_;
75 
76  Size2Name_ [IWkFontsSettable::FontSize::DefaultFontSize] = "FontSize";
77  Size2Name_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = "FixedFontSize";
78  Size2Name_ [IWkFontsSettable::FontSize::MinimumFontSize] = "MinimumFontSize";
79 
80  ResetSizeChoosers ();
81 
82  for (const auto& pair : Util::Stlize (Size2Spinbox_))
83  connect (pair.second,
84  Util::Overload<int> (&QSpinBox::valueChanged),
85  [this, pair] { PendingSizeChanges_ [pair.first] = pair.second->value (); });
86 
87  ResetZoom ();
88 
89  connect (Ui_->Zoom_,
90  Util::Overload<int> (&QSpinBox::valueChanged),
91  [this] { IsFontZoomDirty_ = true; });
92  }
93 
94  void WkFontsWidget::SetFontZoomTooltip (const QString& label)
95  {
96  Ui_->Zoom_->setToolTip (label);
97  }
98 
100  {
101  Settables_ << settable;
102  connect (settable->GetQObject (),
103  &QObject::destroyed,
104  [this, settable] { Settables_.removeOne (settable); });
105 
106  for (const auto& pair : Util::Stlize (Family2Chooser_))
107  settable->SetFontFamily (pair.first, pair.second->GetFont ());
108 
109  for (const auto& pair : Util::Stlize (Size2Spinbox_))
110  settable->SetFontSize (pair.first, pair.second->value ());
111 
112  settable->SetFontSizeMultiplier (Ui_->Zoom_->value () / 100.);
113  }
114 
116  {
117  Size2Spinbox_ [type]->setValue (size);
118  PendingSizeChanges_ [type] = size;
119 
120  QTimer::singleShot (1000, this, [this] { ApplyPendingSizeChanges (); });
121  }
122 
123  void WkFontsWidget::ResetFontChoosers ()
124  {
125  for (const auto& pair : Util::Stlize (Family2Chooser_))
126  {
127  const auto& option = Family2Name_ [pair.first];
128  pair.second->SetFont (BSM_->property (option).value<QFont> ());
129  }
130  }
131 
132  void WkFontsWidget::ResetSizeChoosers ()
133  {
134  for (const auto& pair : Util::Stlize (Size2Spinbox_))
135  {
136  const auto& option = Size2Name_ [pair.first];
137  pair.second->setValue (BSM_->Property (option, 10).toInt ());
138  }
139  }
140 
141  void WkFontsWidget::ResetZoom ()
142  {
143  const auto factor = BSM_->Property ("FontSizeMultiplier", 1).toDouble ();
144  Ui_->Zoom_->setValue (factor * 100);
145  }
146 
147  void WkFontsWidget::ApplyPendingSizeChanges ()
148  {
149  for (const auto& pair : Util::Stlize (PendingSizeChanges_))
150  {
151  BSM_->setProperty (Size2Name_ [pair.first], pair.second);
152  emit sizeChanged (pair.first, pair.second);
153 
154  for (const auto settable : Settables_)
155  settable->SetFontSize (pair.first, pair.second);
156  }
157 
158  PendingSizeChanges_.clear ();
159  }
160 
161  void WkFontsWidget::on_ChangeAll__released ()
162  {
163  QHash<QString, QList<IWkFontsSettable::FontFamily>> families;
164  for (const auto& pair : Util::Stlize (Family2Chooser_))
165  families [pair.second->GetFont ().family ()] << pair.first;
166 
167  const auto& stlized = Util::Stlize (families);
168  const auto& maxElem = std::max_element (stlized.begin (), stlized.end (),
169  ComparingBy ([] (auto pair) { return pair.second.size (); }));
170 
171  const auto dialog = new MassFontChangeDialog { maxElem->first, maxElem->second, this };
172  dialog->show ();
173  connect (dialog,
174  &QDialog::finished,
175  [dialog, this] (int result)
176  {
177  if (result == QDialog::Rejected)
178  return;
179 
180  const auto& font = dialog->GetFont ();
181  for (const auto family : dialog->GetFamilies ())
182  {
183  PendingFontChanges_ [family] = font;
184  Family2Chooser_ [family]->SetFont (font);
185  }
186  });
187  }
188 
190  {
191  ApplyPendingSizeChanges ();
192 
193  for (const auto& pair : Util::Stlize (PendingFontChanges_))
194  {
195  BSM_->setProperty (Family2Name_ [pair.first], pair.second);
196  emit fontChanged (pair.first, pair.second);
197 
198  for (const auto settable : Settables_)
199  settable->SetFontFamily (pair.first, pair.second);
200  }
201 
202  if (IsFontZoomDirty_)
203  {
204  const auto factor = Ui_->Zoom_->value () / 100.;
205 
206  BSM_->setProperty ("FontSizeMultiplier", factor);
207  emit sizeMultiplierChanged (factor);
208 
209  for (const auto settable : Settables_)
210  settable->SetFontSizeMultiplier (factor);
211  }
212 
213  PendingFontChanges_.clear ();
214  IsFontZoomDirty_ = false;
215  }
216 
218  {
219  ResetFontChoosers ();
220  ResetSizeChoosers ();
221  ResetZoom ();
222 
223  PendingFontChanges_.clear ();
224  PendingSizeChanges_.clear ();
225  IsFontZoomDirty_ = false;
226  }
227 }
228 }
auto Stlize(Assoc &&assoc) -> detail::StlAssocRange< detail::Identity, detail::Identity, decltype(assoc.begin()), Assoc, PairType >
Converts an Qt&#39;s associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:166
Interface to aid WebKit-like-view-containing tabs to expose the view fonts configuration to the user...
virtual void SetFontFamily(FontFamily family, const QFont &font)=0
Sets the font for the given font family.
auto ComparingBy(R r)
Definition: prelude.h:252
void SetFontZoomTooltip(const QString &tooltip)
Sets the tooltip for the font size multiplier control.
WkFontsWidget(Util::BaseSettingsManager *bsm, QWidget *parent=nullptr)
Creates the fonts settings widget.
void SetSize(IWkFontsSettable::FontSize type, int size)
Sets the size for the given font size type.
void RegisterSettable(IWkFontsSettable *settable)
Registers an object to be automatically updated whenever font settings change.
void sizeMultiplierChanged(qreal factor)
Notifies the text zoom factor has been changed.
FontSize
Enumeration for possible font sizes.
void sizeChanged(IWkFontsSettable::FontSize type, int size)
Notifies the size for the given font type has been changed.
void fontChanged(IWkFontsSettable::FontFamily family, const QFont &font)
Notifies the font for the given family has been changed.
void fontChanged(QFont font)
Emitted when another font has been chosen.