LeechCraft  0.6.70-13729-g7046a9d2a7
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 LC
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 
89  {
90  Settables_ << settable;
91  connect (settable->GetQObject (),
92  &QObject::destroyed,
93  [this, settable] { Settables_.removeOne (settable); });
94 
95  for (const auto& pair : Util::Stlize (Family2Chooser_))
96  settable->SetFontFamily (pair.first, pair.second->GetFont ());
97 
98  for (const auto& pair : Util::Stlize (Size2Spinbox_))
99  settable->SetFontSize (pair.first, pair.second->value ());
100  }
101 
103  {
104  Size2Spinbox_ [type]->setValue (size);
105  PendingSizeChanges_ [type] = size;
106 
107  QTimer::singleShot (1000, this, [this] { ApplyPendingSizeChanges (); });
108  }
109 
110  void WkFontsWidget::ResetFontChoosers ()
111  {
112  for (const auto& pair : Util::Stlize (Family2Chooser_))
113  {
114  const auto& option = Family2Name_ [pair.first];
115  pair.second->SetFont (BSM_->property (option).value<QFont> ());
116  }
117  }
118 
119  void WkFontsWidget::ResetSizeChoosers ()
120  {
121  for (const auto& pair : Util::Stlize (Size2Spinbox_))
122  {
123  const auto& option = Size2Name_ [pair.first];
124  pair.second->setValue (BSM_->Property (option, 10).toInt ());
125  }
126  }
127 
128  void WkFontsWidget::ApplyPendingSizeChanges ()
129  {
130  for (const auto& pair : Util::Stlize (PendingSizeChanges_))
131  {
132  BSM_->setProperty (Size2Name_ [pair.first], pair.second);
133  emit sizeChanged (pair.first, pair.second);
134 
135  for (const auto settable : Settables_)
136  settable->SetFontSize (pair.first, pair.second);
137  }
138 
139  PendingSizeChanges_.clear ();
140  }
141 
142  void WkFontsWidget::on_ChangeAll__released ()
143  {
144  QHash<QString, QList<IWkFontsSettable::FontFamily>> families;
145  for (const auto& pair : Util::Stlize (Family2Chooser_))
146  families [pair.second->GetFont ().family ()] << pair.first;
147 
148  const auto& stlized = Util::Stlize (families);
149  const auto& maxPair = *std::max_element (stlized.begin (), stlized.end (),
150  ComparingBy ([] (auto pair) { return pair.second.size (); }));
151 
152  const auto dialog = new MassFontChangeDialog { maxPair.first, maxPair.second, this };
153  dialog->show ();
154  connect (dialog,
155  &QDialog::finished,
156  [dialog, this] (int result)
157  {
158  if (result == QDialog::Rejected)
159  return;
160 
161  const auto& font = dialog->GetFont ();
162  for (const auto family : dialog->GetFamilies ())
163  {
164  PendingFontChanges_ [family] = font;
165  Family2Chooser_ [family]->SetFont (font);
166  }
167  });
168  }
169 
171  {
172  ApplyPendingSizeChanges ();
173 
174  for (const auto& pair : Util::Stlize (PendingFontChanges_))
175  {
176  BSM_->setProperty (Family2Name_ [pair.first], pair.second);
177  emit fontChanged (pair.first, pair.second);
178 
179  for (const auto settable : Settables_)
180  settable->SetFontFamily (pair.first, pair.second);
181  }
182 
183  PendingFontChanges_.clear ();
184  }
185 
187  {
188  ResetFontChoosers ();
189  ResetSizeChoosers ();
190 
191  PendingFontChanges_.clear ();
192  PendingSizeChanges_.clear ();
193  }
194 }
195 }
auto Stlize(Assoc &&assoc)
Converts an Qt&#39;s associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:115
void fontChanged(QFont font)
Emitted when another font has been chosen.
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:242
void SetSize(IWkFontsSettable::FontSize type, int size)
Sets the size for the given font size type.
void sizeChanged(IWkFontsSettable::FontSize type, int size)
Notifies the size for the given font type has been changed.
FontSize
Enumeration for possible font sizes.
void RegisterSettable(IWkFontsSettable *settable)
Registers an object to be automatically updated whenever font settings change.
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.
Definition: constants.h:35