LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
fontsizescrollchanger.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 
10 #include <QWidget>
11 #include <QWheelEvent>
13 #include <util/sll/visitor.h>
14 
15 namespace LC::Util
16 {
17  namespace
18  {
19  auto AddSteps (int size, int steps)
20  {
21  constexpr auto minFontSize = 6;
22  return std::max (minFontSize, size + steps);
23  }
24 
25  auto AddSteps (QFont font, int steps)
26  {
27  if (const auto pts = font.pointSize (); pts > 0)
28  font.setPointSize (AddSteps (pts, steps));
29  else if (const auto pxs = font.pixelSize (); pxs > 0)
30  font.setPixelSize (AddSteps (pxs, steps));
31  return font;
32  }
33  }
34 
35  void InstallFontSizeChanger (QWidget& widget, const FontSizeChangerParams& params)
36  {
37  widget.installEventFilter (Util::MakeLambdaEventFilter<QEvent::Wheel> ([params] (QWheelEvent *e)
38  {
39  if (!(e->modifiers () & Qt::ControlModifier))
40  return false;
41 
42  constexpr qreal degreesPerDelta = 1 / 8.;
43  constexpr qreal degreesPerStep = 15.;
44 
45  auto degrees = e->angleDelta ().y () * degreesPerDelta;
46  int steps = static_cast<int> (degrees / degreesPerStep);
47 
48  Visit (params,
49  [=] (const auto& methods)
50  {
51  const auto newFont = AddSteps (methods.GetView_ (), steps);
52  methods.SetView_ (newFont);
53  if (e->modifiers () & Qt::ShiftModifier)
54  methods.SetDefault_ (newFont);
55  });
56 
57  return true;
58  },
59  widget));
60  }
61 }
constexpr detail::AggregateType< detail::AggregateFunction::Max, Ptr > max
Definition: oral.h:971
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:215
std::variant< PixelBasedParams, FontBasedParams > FontSizeChangerParams
void InstallFontSizeChanger(QWidget &widget, const FontSizeChangerParams &params)