LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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] { PendingChanges_ [pair.first] = pair.second->GetFont (); },
69  pair.second,
70  SIGNAL (fontChanged (QFont)),
71  this
72  };
73  }
74 
76  {
77  Settables_ << settable;
79  {
80  [settable, this] { Settables_.removeAll (settable); },
81  settable->GetQObject (),
82  SIGNAL (destroyed ()),
83  this
84  };
85 
86  for (const auto& pair : Util::Stlize (Family2Chooser_))
87  settable->SetFontFamily (pair.first, pair.second->GetFont ());
88  }
89 
90  void WkFontsWidget::ResetFontChoosers ()
91  {
92  for (const auto& pair : Util::Stlize (Family2Chooser_))
93  {
94  const auto& option = Family2Name_ [pair.first];
95  pair.second->SetFont (BSM_->property (option).value<QFont> ());
96  }
97  }
98 
99  void WkFontsWidget::on_ChangeAll__released ()
100  {
101 #ifndef USE_CPP14
102  const auto dialog = new MassFontChangeDialog
103  {
104  Family2Chooser_ [QWebSettings::StandardFont]->GetFont (),
105  Family2Chooser_.keys (),
106  this
107  };
108 #else
109  QHash<QString, QList<QWebSettings::FontFamily>> families;
110  for (const auto& pair : Util::Stlize (Family2Chooser_))
111  families [pair.second->GetFont ().family ()] << pair.first;
112 
113  const auto& stlized = Util::Stlize (families);
114  const auto& maxElem = std::max_element (stlized.begin (), stlized.end (),
115  [] (auto left, auto right) { return left.second.size () < right.second.size (); });
116 
117  const auto dialog = new MassFontChangeDialog { maxElem->first, maxElem->second, this };
118 #endif
119  dialog->show ();
120  new Util::SlotClosure<Util::DeleteLaterPolicy>
121  {
122  [dialog, this]
123  {
124  dialog->deleteLater ();
125  if (dialog->result () == QDialog::Rejected)
126  return;
127 
128  const auto& font = dialog->GetFont ();
129  for (const auto family : dialog->GetFamilies ())
130  {
131  PendingChanges_ [family] = font;
132  Family2Chooser_ [family]->SetFont (font);
133  }
134  },
135  dialog,
136  SIGNAL (finished (int)),
137  dialog
138  };
139  }
140 
142  {
143  for (const auto& pair : Util::Stlize (PendingChanges_))
144  {
145  emit fontChanged (pair.first, pair.second);
146  BSM_->setProperty (Family2Name_ [pair.first], pair.second);
147 
148  for (const auto settable : Settables_)
149  settable->SetFontFamily (pair.first, pair.second);
150  }
151 
152  PendingChanges_.clear ();
153  }
154 
156  {
157  ResetFontChoosers ();
158  PendingChanges_.clear ();
159  }
160 }
161 }
Interface to aid WebKit-view-containing tabs to expose the view fonts configuration to the user...
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
auto Stlize(Assoc &&assoc) -> detail::StlAssocRange< decltype(assoc.begin()), Assoc, PairType >
Converts an Qt'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 fontChanged(QWebSettings::FontFamily family, const QFont &font)
Notifies the font for the given family has been changed.