LeechCraft  0.6.70-13729-g7046a9d2a7
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  * 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 "lineeditbuttonmanager.h"
31 #include <stdexcept>
32 #include <QLineEdit>
33 #include <QStyle>
34 #include <QToolButton>
35 #include <QEvent>
36 #include <QtDebug>
37 
38 namespace LC
39 {
40 namespace Util
41 {
43  : QObject { edit }
44  , Edit_ { edit }
45  , FrameWidth_ { edit->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth) }
46  , Pad_ { 1 + FrameWidth_ }
47  {
48  edit->installEventFilter (this);
49 
50  if (edit->findChildren<LineEditButtonManager*> ().size () > 1)
51  {
52  std::string str { "LineEditButtonManager is already installed on the edit" };
53 
54  const auto& name = edit->objectName ();
55  if (!name.isEmpty ())
56  str += " " + name.toStdString ();
57 
58  throw std::runtime_error (str);
59  }
60  }
61 
62  void LineEditButtonManager::Add (QToolButton *button)
63  {
64  Buttons_ << button;
65 
66  const auto& buttonSH = button->sizeHint ();
67  Pad_ += buttonSH.width ();
68 
69  Edit_->setStyleSheet (QString ("QLineEdit { padding-right: %1px; }")
70  .arg (Pad_));
71 
72  UpdatePos ();
73  }
74 
75  bool LineEditButtonManager::eventFilter (QObject *obj, QEvent *event)
76  {
77  if (event->type () == QEvent::Resize ||
78  event->type () == QEvent::Move)
79  UpdatePos ();
80 
81  return QObject::eventFilter (obj, event);
82  }
83 
84  void LineEditButtonManager::UpdatePos ()
85  {
86  int sumWidth = 0;
87 
88  for (const auto button : Buttons_)
89  {
90  const auto& hint = button->sizeHint ();
91 
92  sumWidth += hint.width ();
93 
94  const auto& rect = Edit_->rect ();
95  const int frameWidth = Edit_->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth);
96  button->move (rect.right () - frameWidth - sumWidth,
97  (rect.bottom () + 1 - hint.height ()) / 2);
98  }
99  }
100 }
101 }
LineEditButtonManager(QLineEdit *edit)
Constructs the manager for the line edit.
void Add(QToolButton *button)
Adds a button to the line edit.
bool eventFilter(QObject *, QEvent *)
Definition: constants.h:35