LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
lineeditbuttonmanager.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 <stdexcept>
11 #include <QLineEdit>
12 #include <QStyle>
13 #include <QToolButton>
14 #include <QEvent>
15 #include <QtDebug>
16 
17 namespace LC::Util
18 {
20  : QObject { edit }
21  , Edit_ { edit }
22  , FrameWidth_ { edit->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth) }
23  , Pad_ { 1 + FrameWidth_ }
24  {
25  edit->installEventFilter (this);
26 
27  const auto markName = "LC.Core.HasLineEditButtonManager";
28  if (edit->property (markName).toBool ())
29  {
30  std::string str { "LineEditButtonManager is already installed on the edit" };
31 
32  const auto& name = edit->objectName ();
33  if (!name.isEmpty ())
34  str += " " + name.toStdString ();
35 
36  throw std::runtime_error (str);
37  }
38 
39  edit->setProperty (markName, true);
40  }
41 
42  void LineEditButtonManager::Add (QToolButton *button)
43  {
44  Buttons_ << button;
45 
46  const auto& buttonSH = button->sizeHint ();
47  Pad_ += buttonSH.width ();
48 
49  Edit_->setStyleSheet (QStringLiteral ("QLineEdit { padding-right: %1px; }")
50  .arg (Pad_));
51 
52  UpdatePos ();
53  }
54 
55  bool LineEditButtonManager::eventFilter (QObject *obj, QEvent *event)
56  {
57  if (event->type () == QEvent::Resize ||
58  event->type () == QEvent::Move)
59  UpdatePos ();
60 
61  return QObject::eventFilter (obj, event);
62  }
63 
64  void LineEditButtonManager::UpdatePos ()
65  {
66  int sumWidth = 0;
67 
68  for (const auto button : Buttons_)
69  {
70  const auto& hint = button->sizeHint ();
71 
72  sumWidth += hint.width ();
73 
74  const auto& rect = Edit_->rect ();
75  const int frameWidth = Edit_->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth);
76  button->move (rect.right () - frameWidth - sumWidth,
77  (rect.bottom () + 1 - hint.height ()) / 2);
78  }
79  }
80 }
LC::Util
Definition: icoreproxy.h:33
LC::Util::Move
@ Move
Definition: winflags.h:43
lineeditbuttonmanager.h
LC::Util::Resize
@ Resize
Definition: winflags.h:44
LC::Util::LineEditButtonManager::LineEditButtonManager
LineEditButtonManager(QLineEdit *edit)
Constructs the manager for the line edit.
Definition: lineeditbuttonmanager.cpp:25