LeechCraft  0.6.70-15082-g543737046d
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
19 {
20 namespace Util
21 {
22  WkFontsWidget::WkFontsWidget (BaseSettingsManager *bsm, QWidget *parent)
23  : QWidget { parent }
24  , Ui_ { std::make_shared<Ui::WkFontsWidget> () }
25  , BSM_ { bsm }
26  {
27  Ui_->setupUi (this);
28 
29  Family2Chooser_ [IWkFontsSettable::FontFamily::StandardFont] = Ui_->StandardChooser_;
30  Family2Chooser_ [IWkFontsSettable::FontFamily::FixedFont] = Ui_->FixedChooser_;
31  Family2Chooser_ [IWkFontsSettable::FontFamily::SerifFont] = Ui_->SerifChooser_;
32  Family2Chooser_ [IWkFontsSettable::FontFamily::SansSerifFont] = Ui_->SansSerifChooser_;
33  Family2Chooser_ [IWkFontsSettable::FontFamily::CursiveFont] = Ui_->CursiveChooser_;
34  Family2Chooser_ [IWkFontsSettable::FontFamily::FantasyFont] = Ui_->FantasyChooser_;
35 
36  Family2Name_ [IWkFontsSettable::FontFamily::StandardFont] = "StandardFont";
37  Family2Name_ [IWkFontsSettable::FontFamily::FixedFont] = "FixedFont";
38  Family2Name_ [IWkFontsSettable::FontFamily::SerifFont] = "SerifFont";
39  Family2Name_ [IWkFontsSettable::FontFamily::SansSerifFont] = "SansSerifFont";
40  Family2Name_ [IWkFontsSettable::FontFamily::CursiveFont] = "CursiveFont";
41  Family2Name_ [IWkFontsSettable::FontFamily::FantasyFont] = "FantasyFont";
42 
43  ResetFontChoosers ();
44 
45  for (const auto& pair : Util::Stlize (Family2Chooser_))
46  connect (pair.second,
48  [this, pair] { PendingFontChanges_ [pair.first] = pair.second->GetFont (); });
49 
50  Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFontSize] = Ui_->SizeDefault_;
51  Size2Spinbox_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = Ui_->SizeFixedWidth_;
52  Size2Spinbox_ [IWkFontsSettable::FontSize::MinimumFontSize] = Ui_->SizeMinimum_;
53 
54  Size2Name_ [IWkFontsSettable::FontSize::DefaultFontSize] = "FontSize";
55  Size2Name_ [IWkFontsSettable::FontSize::DefaultFixedFontSize] = "FixedFontSize";
56  Size2Name_ [IWkFontsSettable::FontSize::MinimumFontSize] = "MinimumFontSize";
57 
58  ResetSizeChoosers ();
59 
60  for (const auto& pair : Util::Stlize (Size2Spinbox_))
61  connect (pair.second,
62  qOverload<int> (&QSpinBox::valueChanged),
63  [this, pair] { PendingSizeChanges_ [pair.first] = pair.second->value (); });
64  }
65 
67  {
68  Settables_ << settable;
69  connect (settable->GetQObject (),
70  &QObject::destroyed,
71  [this, settable] { Settables_.removeOne (settable); });
72 
73  for (const auto& pair : Util::Stlize (Family2Chooser_))
74  settable->SetFontFamily (pair.first, pair.second->GetFont ());
75 
76  for (const auto& pair : Util::Stlize (Size2Spinbox_))
77  settable->SetFontSize (pair.first, pair.second->value ());
78  }
79 
81  {
82  Size2Spinbox_ [type]->setValue (size);
83  PendingSizeChanges_ [type] = size;
84 
85  QTimer::singleShot (1000, this, [this] { ApplyPendingSizeChanges (); });
86  }
87 
88  void WkFontsWidget::ResetFontChoosers ()
89  {
90  for (const auto& pair : Util::Stlize (Family2Chooser_))
91  {
92  const auto& option = Family2Name_ [pair.first];
93  pair.second->SetFont (BSM_->property (option.data ()).value<QFont> ());
94  }
95  }
96 
97  void WkFontsWidget::ResetSizeChoosers ()
98  {
99  for (const auto& pair : Util::Stlize (Size2Spinbox_))
100  {
101  const auto& option = Size2Name_ [pair.first];
102  pair.second->setValue (BSM_->Property (option, 10).toInt ());
103  }
104  }
105 
106  void WkFontsWidget::ApplyPendingSizeChanges ()
107  {
108  for (const auto& pair : Util::Stlize (PendingSizeChanges_))
109  {
110  BSM_->setProperty (Size2Name_ [pair.first].data (), pair.second);
111  emit sizeChanged (pair.first, pair.second);
112 
113  for (const auto settable : Settables_)
114  settable->SetFontSize (pair.first, pair.second);
115  }
116 
117  PendingSizeChanges_.clear ();
118  }
119 
120  void WkFontsWidget::on_ChangeAll__released ()
121  {
122  QHash<QString, QList<IWkFontsSettable::FontFamily>> families;
123  for (const auto& pair : Util::Stlize (Family2Chooser_))
124  families [pair.second->GetFont ().family ()] << pair.first;
125 
126  const auto& stlized = Util::Stlize (families);
127  const auto& maxPair = *std::max_element (stlized.begin (), stlized.end (),
128  ComparingBy ([] (auto pair) { return pair.second.size (); }));
129 
130  const auto dialog = new MassFontChangeDialog { maxPair.first, maxPair.second, this };
131  dialog->show ();
132  connect (dialog,
133  &QDialog::finished,
134  [dialog, this] (int result)
135  {
136  if (result == QDialog::Rejected)
137  return;
138 
139  const auto& font = dialog->GetFont ();
140  for (const auto family : dialog->GetFamilies ())
141  {
142  PendingFontChanges_ [family] = font;
143  Family2Chooser_ [family]->SetFont (font);
144  }
145  });
146  }
147 
148  void WkFontsWidget::accept ()
149  {
150  ApplyPendingSizeChanges ();
151 
152  for (const auto& pair : Util::Stlize (PendingFontChanges_))
153  {
154  BSM_->setProperty (Family2Name_ [pair.first].data (), pair.second);
155  emit fontChanged (pair.first, pair.second);
156 
157  for (const auto settable : Settables_)
158  settable->SetFontFamily (pair.first, pair.second);
159  }
160 
161  PendingFontChanges_.clear ();
162  }
163 
164  void WkFontsWidget::reject ()
165  {
166  ResetFontChoosers ();
167  ResetSizeChoosers ();
168 
169  PendingFontChanges_.clear ();
170  PendingSizeChanges_.clear ();
171  }
172 }
173 }
IWkFontsSettable::FontSize::DefaultFontSize
@ DefaultFontSize
LC::Util::ComparingBy
auto ComparingBy(R r)
Definition: prelude.h:227
massfontchangedialog.h
iwkfontssettable.h
IWkFontsSettable::FontSize
FontSize
Enumeration for possible font sizes.
Definition: iwkfontssettable.h:44
LC::Util::WkFontsWidget::sizeChanged
void sizeChanged(IWkFontsSettable::FontSize type, int size)
Notifies the size for the given font type has been changed.
IWkFontsSettable::FontFamily::FantasyFont
@ FantasyFont
IWkFontsSettable
Interface to aid WebKit-like-view-containing tabs to expose the view fonts configuration to the user.
Definition: iwkfontssettable.h:24
LC::Util::Stlize
auto Stlize(Assoc &&assoc) noexcept
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:49
wkfontswidget.h
IWkFontsSettable::FontFamily::CursiveFont
@ CursiveFont
LC::Util::FontChooserWidget::fontChanged
void fontChanged(QFont font)
Emitted when another font has been chosen.
LC::Util::WkFontsWidget::fontChanged
void fontChanged(IWkFontsSettable::FontFamily family, const QFont &font)
Notifies the font for the given family has been changed.
LC::Util::WkFontsWidget::SetSize
void SetSize(IWkFontsSettable::FontSize type, int size)
Sets the size for the given font size type.
Definition: wkfontswidget.cpp:92
IWkFontsSettable::FontSize::DefaultFixedFontSize
@ DefaultFixedFontSize
LC::Util::WkFontsWidget::RegisterSettable
void RegisterSettable(IWkFontsSettable *settable)
Registers an object to be automatically updated whenever font settings change.
Definition: wkfontswidget.cpp:78
qtutil.h
IWkFontsSettable::FontFamily::SerifFont
@ SerifFont
prelude.h
LC
Definition: constants.h:14
LC::Util::WkFontsWidget::reject
void reject()
Definition: wkfontswidget.cpp:176
IWkFontsSettable::FontSize::MinimumFontSize
@ MinimumFontSize
LC::Util::WkFontsWidget::accept
void accept()
Definition: wkfontswidget.cpp:160
LC::Util::WkFontsWidget::WkFontsWidget
WkFontsWidget(Util::BaseSettingsManager *bsm, QWidget *parent=nullptr)
Creates the fonts settings widget.
Definition: wkfontswidget.cpp:34
IWkFontsSettable::FontFamily::SansSerifFont
@ SansSerifFont
IWkFontsSettable::FontFamily::StandardFont
@ StandardFont
IWkFontsSettable::FontFamily::FixedFont
@ FixedFont