LeechCraft  0.6.70-6645-gcd10d7e
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 <xmlsettingsdialog/basesettingsmanager.h>
32 #include <util/sll/qtutil.h>
33 #include <util/sll/slotclosure.h>
35 #include "ui_wkfontswidget.h"
36 #include "massfontchangedialog.h"
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
42  WkFontsWidget::WkFontsWidget (BaseSettingsManager *bsm, QWidget *parent)
43  : QWidget { parent }
44  , Ui_ { std::make_shared<Ui::WkFontsWidget> () }
45  , BSM_ { bsm }
46  {
47  Ui_->setupUi (this);
48 
49  Family2Chooser_ [QWebSettings::StandardFont] = Ui_->StandardChooser_;
50  Family2Chooser_ [QWebSettings::FixedFont] = Ui_->FixedChooser_;
51  Family2Chooser_ [QWebSettings::SerifFont] = Ui_->SerifChooser_;
52  Family2Chooser_ [QWebSettings::SansSerifFont] = Ui_->SansSerifChooser_;
53  Family2Chooser_ [QWebSettings::CursiveFont] = Ui_->CursiveChooser_;
54  Family2Chooser_ [QWebSettings::FantasyFont] = Ui_->FantasyChooser_;
55 
56  Family2Name_ [QWebSettings::StandardFont] = "StandardFont";
57  Family2Name_ [QWebSettings::FixedFont] = "FixedFont";
58  Family2Name_ [QWebSettings::SerifFont] = "SerifFont";
59  Family2Name_ [QWebSettings::SansSerifFont] = "SansSerifFont";
60  Family2Name_ [QWebSettings::CursiveFont] = "CursiveFont";
61  Family2Name_ [QWebSettings::FantasyFont] = "FantasyFont";
62 
63  ResetFontChoosers ();
64 
65  for (const auto& pair : Util::Stlize (Family2Chooser_))
67  {
68  [this, pair] { PendingFontChanges_ [pair.first] = pair.second->GetFont (); },
69  pair.second,
70  SIGNAL (fontChanged (QFont)),
71  this
72  };
73 
74  Size2Spinbox_ [QWebSettings::DefaultFontSize] = Ui_->SizeDefault_;
75  Size2Spinbox_ [QWebSettings::DefaultFixedFontSize] = Ui_->SizeFixedWidth_;
76  Size2Spinbox_ [QWebSettings::MinimumFontSize] = Ui_->SizeMinimum_;
77 
78  Size2Name_ [QWebSettings::DefaultFontSize] = "FontSize";
79  Size2Name_ [QWebSettings::DefaultFixedFontSize] = "FixedFontSize";
80  Size2Name_ [QWebSettings::MinimumFontSize] = "MinimumFontSize";
81 
82  ResetSizeChoosers ();
83 
84  for (const auto& pair : Util::Stlize (Size2Spinbox_))
86  {
87  [this, pair] { PendingSizeChanges_ [pair.first] = pair.second->value (); },
88  pair.second,
89  SIGNAL (valueChanged (int)),
90  this
91  };
92 
93  ResetZoom ();
94 
96  {
97  [this] { IsFontZoomDirty_ = true; },
98  Ui_->Zoom_,
99  SIGNAL (valueChanged (int)),
100  this
101  };
102  }
103 
104  void WkFontsWidget::SetFontZoomTooltip (const QString& label)
105  {
106  Ui_->Zoom_->setToolTip (label);
107  }
108 
110  {
111  Settables_ << settable;
113  {
114  [settable, this] { Settables_.removeAll (settable); },
115  settable->GetQObject (),
116  SIGNAL (destroyed ()),
117  this
118  };
119 
120  for (const auto& pair : Util::Stlize (Family2Chooser_))
121  settable->SetFontFamily (pair.first, pair.second->GetFont ());
122 
123  for (const auto& pair : Util::Stlize (Size2Spinbox_))
124  settable->SetFontSize (pair.first, pair.second->value ());
125 
126  settable->SetFontSizeMultiplier (Ui_->Zoom_->value () / 100.);
127  }
128 
129  void WkFontsWidget::ResetFontChoosers ()
130  {
131  for (const auto& pair : Util::Stlize (Family2Chooser_))
132  {
133  const auto& option = Family2Name_ [pair.first];
134  pair.second->SetFont (BSM_->property (option).value<QFont> ());
135  }
136  }
137 
138  void WkFontsWidget::ResetSizeChoosers ()
139  {
140  for (const auto& pair : Util::Stlize (Size2Spinbox_))
141  {
142  const auto& option = Size2Name_ [pair.first];
143  pair.second->setValue (BSM_->Property (option, 10).toInt ());
144  }
145  }
146 
147  void WkFontsWidget::ResetZoom ()
148  {
149  const auto factor = BSM_->Property ("FontSizeMultiplier", 1).toDouble ();
150  Ui_->Zoom_->setValue (factor * 100);
151  }
152 
153  void WkFontsWidget::on_ChangeAll__released ()
154  {
155 #ifndef USE_CPP14
156  const auto dialog = new MassFontChangeDialog
157  {
158  Family2Chooser_ [QWebSettings::StandardFont]->GetFont (),
159  Family2Chooser_.keys (),
160  this
161  };
162 #else
163  QHash<QString, QList<QWebSettings::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  [] (auto left, auto right) { return left.second.size () < right.second.size (); });
170 
171  const auto dialog = new MassFontChangeDialog { maxElem->first, maxElem->second, this };
172 #endif
173  dialog->show ();
175  {
176  [dialog, this]
177  {
178  dialog->deleteLater ();
179  if (dialog->result () == QDialog::Rejected)
180  return;
181 
182  const auto& font = dialog->GetFont ();
183  for (const auto family : dialog->GetFamilies ())
184  {
185  PendingFontChanges_ [family] = font;
186  Family2Chooser_ [family]->SetFont (font);
187  }
188  },
189  dialog,
190  SIGNAL (finished (int)),
191  dialog
192  };
193  }
194 
196  {
197  for (const auto& pair : Util::Stlize (PendingFontChanges_))
198  {
199  BSM_->setProperty (Family2Name_ [pair.first], pair.second);
200  emit fontChanged (pair.first, pair.second);
201 
202  for (const auto settable : Settables_)
203  settable->SetFontFamily (pair.first, pair.second);
204  }
205 
206  for (const auto& pair : Util::Stlize (PendingSizeChanges_))
207  {
208  BSM_->setProperty (Size2Name_ [pair.first], pair.second);
209  emit sizeChanged (pair.first, pair.second);
210 
211  for (const auto settable : Settables_)
212  settable->SetFontSize (pair.first, pair.second);
213  }
214 
215  if (IsFontZoomDirty_)
216  {
217  const auto factor = Ui_->Zoom_->value () / 100.;
218 
219  BSM_->setProperty ("FontSizeMultiplier", factor);
220  emit sizeMultiplierChanged (factor);
221 
222  for (const auto settable : Settables_)
223  settable->SetFontSizeMultiplier (factor);
224  }
225 
226  PendingFontChanges_.clear ();
227  PendingSizeChanges_.clear ();
228  IsFontZoomDirty_ = false;
229  }
230 
232  {
233  ResetFontChoosers ();
234  ResetSizeChoosers ();
235  ResetZoom ();
236 
237  PendingFontChanges_.clear ();
238  PendingSizeChanges_.clear ();
239  IsFontZoomDirty_ = false;
240  }
241 }
242 }
Interface to aid WebKit-view-containing tabs to expose the view fonts configuration to the user...
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 sizeChanged(QWebSettings::FontSize type, int size)
Notifies the size for the given font type has been changed.
Executes a given functor upon a signal (or a list of signals).
Definition: slotclosure.h:100
auto Stlize(Assoc &&assoc) -> detail::StlAssocRange< decltype(assoc.begin()), Assoc, PairType >
Converts an Qt&#39;s associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:126
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.
void fontChanged(QWebSettings::FontFamily family, const QFont &font)
Notifies the font for the given family has been changed.