LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
clearlineeditaddon.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 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 "clearlineeditaddon.h"
31 #include <QLineEdit>
32 #include <QToolButton>
33 #include <QApplication>
34 #include <QStyle>
35 
36 namespace LeechCraft
37 {
38 namespace Util
39 {
41  : QObject (edit)
42  , Button_ (new QToolButton (edit))
43  , Edit_ (edit)
44  {
45  const bool isRtl = QApplication::layoutDirection () == Qt::RightToLeft;
46  const auto& icon = proxy->GetIcon (isRtl ?
47  "edit-clear-locationbar-ltr" :
48  "edit-clear-locationbar-rtl");
49 
50  Button_->setIconSize (QSize (16, 16));
51  Button_->setIcon (icon);
52  Button_->setCursor (Qt::ArrowCursor);
53  Button_->setStyleSheet ("QToolButton { border: none; padding: 0px; }");
54  Button_->hide ();
55 
56  connect (Button_,
57  SIGNAL (clicked ()),
58  edit,
59  SLOT (clear ()));
60  connect (edit,
61  SIGNAL (textChanged (QString)),
62  this,
63  SLOT (updateButton (QString)));
64 
65  const int frameWidth = edit->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth);
66  edit->setStyleSheet (QString ("QLineEdit { padding-right: %1px; }")
67  .arg (Button_->sizeHint ().width () + frameWidth + 1));
68  const auto msz = edit->minimumSizeHint ();
69  edit->setMinimumSize (qMax (msz.width (), Button_->sizeHint ().height () + frameWidth * 2 + 2),
70  qMax (msz.height(), Button_->sizeHint ().height () + frameWidth * 2 + 2));
71 
72  UpdatePos ();
73 
74  edit->installEventFilter (this);
75  updateButton (edit->text ());
76  }
77 
78  bool ClearLineEditAddon::eventFilter (QObject *obj, QEvent *event)
79  {
80  if (event->type () == QEvent::Resize ||
81  event->type () == QEvent::Move)
82  UpdatePos ();
83 
84  return QObject::eventFilter (obj, event);
85  }
86 
87  void ClearLineEditAddon::UpdatePos ()
88  {
89  const auto& hint = Button_->sizeHint ();
90  const auto& rect = Edit_->rect ();
91  const int frameWidth = Edit_->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth);
92  Button_->move (rect.right () - frameWidth - hint.width (),
93  (rect.bottom () + 1 - hint.height ()) / 2);
94  }
95 
96  void ClearLineEditAddon::updateButton (const QString& text)
97  {
98  Button_->setVisible (!text.isEmpty ());
99  }
100 }
101 }
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:192
ClearLineEditAddon(ICoreProxy_ptr proxy, QLineEdit *edit)
Creates the addon and installs it on the given edit.