LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
clearlineeditaddon.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 
9 #include "clearlineeditaddon.h"
10 #include <QLineEdit>
11 #include <QToolButton>
12 #include <QApplication>
13 #include <QStyle>
14 #include <QShortcut>
15 #include "lineeditbuttonmanager.h"
17 
18 namespace LC::Util
19 {
20  ClearLineEditAddon::ClearLineEditAddon (const ICoreProxy_ptr& proxy, QLineEdit *edit)
21  : ClearLineEditAddon { proxy, edit, new LineEditButtonManager { edit } }
22  {
23  }
24 
26  QLineEdit *edit, LineEditButtonManager *mgr)
27  : QObject { edit }
28  , Button_ { new QToolButton { edit } }
29  , EscShortcut_ { new QShortcut { Qt::Key_Escape, edit, SLOT (clear ()), nullptr, Qt::WidgetShortcut } }
30  {
31  const bool isRtl = QApplication::layoutDirection () == Qt::RightToLeft;
32  const auto& icon = proxy->GetIconThemeManager ()->GetIcon (isRtl ?
33  QStringLiteral ("edit-clear-locationbar-ltr") :
34  QStringLiteral ("edit-clear-locationbar-rtl"));
35 
36  Button_->setIconSize (QSize (16, 16));
37  Button_->setIcon (icon);
38  Button_->setCursor (Qt::ArrowCursor);
39  Button_->setStyleSheet (QStringLiteral ("QToolButton { border: none; padding: 0px; }"));
40  Button_->hide ();
41 
42  connect (Button_,
43  &QToolButton::clicked,
44  edit,
45  &QLineEdit::clear);
46 
47  connect (edit,
48  &QLineEdit::textChanged,
49  this,
50  [this] (const QString& str) { Button_->setVisible (!str.isEmpty ()); });
51  Button_->setVisible (!edit->text ().isEmpty ());
52 
53  mgr->Add (Button_);
54  }
55 
56  void ClearLineEditAddon::SetEscClearsEdit (bool clears)
57  {
58  EscShortcut_->setEnabled (clears);
59  }
60 }
LC::Util
Definition: icoreproxy.h:33
LC::Util::LineEditButtonManager
Manages additional overlay buttons in a QLineEdit.
Definition: lineeditbuttonmanager.h:36
lineeditbuttonmanager.h
iiconthememanager.h
ICoreProxy_ptr
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181
clearlineeditaddon.h
LC::Util::ClearLineEditAddon::SetEscClearsEdit
void SetEscClearsEdit(bool clears)
Toggles whether Esc button clears the line edit.
Definition: clearlineeditaddon.cpp:62
LC::Util::ClearLineEditAddon::ClearLineEditAddon
ClearLineEditAddon(const ICoreProxy_ptr &proxy, QLineEdit *edit)
Creates the addon and installs it on the given edit.
Definition: clearlineeditaddon.cpp:26