LeechCraft  0.6.70-10870-g558588d6ec
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_ [IWkFontsSettable::FontFamily::StandardFont] = Ui_->StandardChooser_;
50  Family2Chooser_ [IWkFontsSettable::FontFamily::FixedFont] = Ui_->FixedChooser_;
51  Family2Chooser_ [IWkFontsSettable::FontFamily::SerifFont] = Ui_->SerifChooser_;
52  Family2Chooser_ [IWkFontsSettable::FontFamily::SansSerifFont] = Ui_->SansSerifChooser_;
53  Family2Chooser_ [IWkFontsSettable::FontFamily::CursiveFont] = Ui_->CursiveChooser_;
54  Family2Chooser_ [IWkFontsSettable::FontFamily::FantasyFont] = Ui_->FantasyChooser_;
55 
56  Family2Name_ [IWkFontsSettable::FontFamily::StandardFont] = "StandardFont";
57  Family2Name_ [IWkFontsSettable::FontFamily::FixedFont] = "FixedFont";
58  Family2Name_ [IWkFontsSettable::FontFamily::SerifFont] = "SerifFont";
59  Family2Name_ [IWkFontsSettable::FontFamily::SansSerifFont] = "SansSerifFont";
60  Family2Name_ [IWkFontsSettable::FontFamily::CursiveFont] = "CursiveFont";
61  Family2Name_ [IWkFontsSettable::FontFamily::FantasyFont] = "FantasyFont";
62 
63  ResetFontChoosers ();
64 
65  for (const auto& pair : Util::Stlize (Family2Chooser_))
66  new Util::SlotClosure<Util::NoDeletePolicy>
67  {
68  [this, pair] { PendingFontChanges_ [pair.first] = pair.second->GetFont (); },
69  pair.second,
70  SIGNAL (fontChanged (QFont)),
71  this
72  };
73 
74  Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFontSize] = Ui_->SizeDefault_;
75  Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = Ui_->SizeFixedWidth_;
76  Size2Spinbox_ [IWkFontsSettable::FontSize::MinimumFontSize] = Ui_->SizeMinimum_;
77 
78  Size2Name_ [IWkFontsSettable::FontSize::DefaultFontSize] = "FontSize";
79  Size2Name_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = "FixedFontSize";
80  Size2Name_ [IWkFontsSettable::FontSize::MinimumFontSize] = "MinimumFontSize";
81 
82  ResetSizeChoosers ();
83 
84  for (const auto& pair : Util::Stlize (Size2Spinbox_))
85  new Util::SlotClosure<Util::NoDeletePolicy>
86  {
87  [this, pair] { PendingSizeChanges_ [pair.first] = pair.second->value (); },
88  pair.second,
89  SIGNAL (valueChanged (int)),
90  this
91  };
92 
93  ResetZoom ();
94 
95  new Util::SlotClosure<Util::NoDeletePolicy>
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  QHash<QString, QList<IWkFontsSettable::FontFamily>> families;
156  for (const auto& pair : Util::Stlize (Family2Chooser_))
157  families [pair.second->GetFont ().family ()] << pair.first;
158 
159  const auto& stlized = Util::Stlize (families);
160  const auto& maxElem = std::max_element (stlized.begin (), stlized.end (),
161  [] (auto left, auto right) { return left.second.size () < right.second.size (); });
162 
163  const auto dialog = new MassFontChangeDialog { maxElem->first, maxElem->second, this };
164 
165  dialog->show ();
166  new Util::SlotClosure<Util::DeleteLaterPolicy>
167  {
168  [dialog, this]
169  {
170  dialog->deleteLater ();
171  if (dialog->result () == QDialog::Rejected)
172  return;
173 
174  const auto& font = dialog->GetFont ();
175  for (const auto family : dialog->GetFamilies ())
176  {
177  PendingFontChanges_ [family] = font;
178  Family2Chooser_ [family]->SetFont (font);
179  }
180  },
181  dialog,
182  SIGNAL (finished (int)),
183  dialog
184  };
185  }
186 
188  {
189  for (const auto& pair : Util::Stlize (PendingFontChanges_))
190  {
191  BSM_->setProperty (Family2Name_ [pair.first], pair.second);
192  emit fontChanged (pair.first, pair.second);
193 
194  for (const auto settable : Settables_)
195  settable->SetFontFamily (pair.first, pair.second);
196  }
197 
198  for (const auto& pair : Util::Stlize (PendingSizeChanges_))
199  {
200  BSM_->setProperty (Size2Name_ [pair.first], pair.second);
201  emit sizeChanged (pair.first, pair.second);
202 
203  for (const auto settable : Settables_)
204  settable->SetFontSize (pair.first, pair.second);
205  }
206 
207  if (IsFontZoomDirty_)
208  {
209  const auto factor = Ui_->Zoom_->value () / 100.;
210 
211  BSM_->setProperty ("FontSizeMultiplier", factor);
212  emit sizeMultiplierChanged (factor);
213 
214  for (const auto settable : Settables_)
215  settable->SetFontSizeMultiplier (factor);
216  }
217 
218  PendingFontChanges_.clear ();
219  PendingSizeChanges_.clear ();
220  IsFontZoomDirty_ = false;
221  }
222 
224  {
225  ResetFontChoosers ();
226  ResetSizeChoosers ();
227  ResetZoom ();
228 
229  PendingFontChanges_.clear ();
230  PendingSizeChanges_.clear ();
231  IsFontZoomDirty_ = false;
232  }
233 }
234 }
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...
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.
Executes a given functor upon a signal (or a list of signals).
Definition: slotclosure.h:100
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 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.