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