LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
findnotification.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 "findnotification.h"
10 #include <QShortcut>
13 #include "clearlineeditaddon.h"
14 #include "util/shortcuts/util.h"
15 #include "ui_findnotification.h"
16 
17 namespace LC::Util
18 {
19  FindNotification::FindNotification (const ICoreProxy_ptr& proxy, QWidget *parent)
20  : Util::PageNotification { parent }
21  , Ui_ { std::make_unique<Ui::FindNotification> () }
22  , EscShortcut_ { new QShortcut { Qt::Key_Escape, this, this, &FindNotification::Reject } }
23  {
24  Ui_->setupUi (this);
25 
26  setFocusProxy (Ui_->Pattern_);
27 
28  EscShortcut_->setContext (Qt::WidgetWithChildrenShortcut);
29 
30  const auto addon = new Util::ClearLineEditAddon { proxy, Ui_->Pattern_ };
31  addon->SetEscClearsEdit (false);
32 
33  const auto coreInstance = proxy->GetPluginsManager ()->GetPluginByID ("org.LeechCraft.CoreInstance");
34  const auto scProxy = proxy->GetShortcutProxy ();
35 
36  CreateShortcuts (scProxy->GetShortcuts (coreInstance, QStringLiteral ("Find.Show")),
37  [this]
38  {
39  show ();
40  setFocus ();
41  },
42  parent);
43  CreateShortcuts (scProxy->GetShortcuts (coreInstance, QStringLiteral ("Find.Next")),
44  this, &FindNotification::FindNext, parent);
45  CreateShortcuts (scProxy->GetShortcuts (coreInstance, QStringLiteral ("Find.Prev")),
46  this, &FindNotification::FindPrevious, parent);
47 
48  connect (Ui_->Pattern_,
49  &QLineEdit::textChanged,
50  [this] (const auto& str) { Ui_->FindButton_->setEnabled (!str.isEmpty ()); });
51  connect (Ui_->FindButton_,
52  &QPushButton::released,
53  [this]
54  {
55  auto flags = GetFlags ();
56  if (Ui_->SearchBackwards_->checkState () == Qt::Checked)
57  flags |= FindBackwards;
58  HandleNext (Ui_->Pattern_->text (), flags);
59  });
60  }
61 
63 
64  void FindNotification::SetEscCloses (bool close)
65  {
66  EscShortcut_->setEnabled (close);
67  }
68 
69  void FindNotification::SetText (const QString& text)
70  {
71  Ui_->Pattern_->setText (text);
72  }
73 
74  QString FindNotification::GetText () const
75  {
76  return Ui_->Pattern_->text ();
77  }
78 
79  void FindNotification::SetSuccessful (bool success)
80  {
81  auto ss = QStringLiteral ("QLineEdit { background-color: ");
82  if (!success)
83  ss.append ("#FF0000");
84  else
85  {
86  auto color = QApplication::palette ().color (QPalette::Base);
87  color.setRedF (color.redF () / 2);
88  color.setBlueF (color.blueF () / 2);
89  ss.append (color.name ());
90  }
91  ss.append (" }");
92  Ui_->Pattern_->setStyleSheet (ss);
93  }
94 
95  auto FindNotification::GetFlags () const -> FindFlags
96  {
97  FindFlags flags;
98  if (Ui_->MatchCase_->checkState () == Qt::Checked)
99  flags |= FindCaseSensitively;
100  if (Ui_->WrapAround_->checkState () == Qt::Checked)
101  flags |= FindWrapsAround;
102  return flags;
103  }
104 
106  {
107  const auto& text = GetText ();
108  if (text.isEmpty ())
109  return;
110 
111  HandleNext (text, GetFlags ());
112  }
113 
115  {
116  const auto& text = GetText ();
117  if (text.isEmpty ())
118  return;
119 
121  }
122 
124  {
125  SetText ({});
126  }
127 
129  {
130  Ui_->Pattern_->clear ();
131  hide ();
132  }
133 }
ipluginsmanager.h
util.h
LC::Util::FindNotification::FindPrevious
void FindPrevious()
Search for the previous occurrence of the search text.
Definition: findnotification.cpp:120
LC::Util::FindNotification::SetEscCloses
void SetEscCloses(bool close)
Sets whether Esc closes the widget.
Definition: findnotification.cpp:70
LC::Util::FindNotification::FindWrapsAround
@ FindWrapsAround
Definition: findnotification.h:86
LC::Util::FindNotification::HandleNext
virtual void HandleNext(const QString &text, FindFlags flags)=0
Called each time the user requests a search.
LC::Util
Definition: icoreproxy.h:33
LC::Util::FindNotification::GetText
QString GetText() const
Returns the currently entered text in the find field.
Definition: findnotification.cpp:80
LC::Util::CreateShortcuts
void CreateShortcuts(const QList< QKeySequence > &shortcuts, const std::function< void()> &func, QWidget *parent)
Makes func invokable with shortcuts in seq.
Definition: util.cpp:19
LC::Util::FindNotification::FindBackwards
@ FindBackwards
Definition: findnotification.h:80
findnotification.h
LC::Util::FindNotification::FindNotification
FindNotification(const ICoreProxy_ptr &proxy, QWidget *near)
Creates the search widget in parent layout of near.
Definition: findnotification.cpp:25
LC::Util::ClearLineEditAddon
Provides a "clear text" action for line edits.
Definition: clearlineeditaddon.h:42
LC::Util::FindNotification::Reject
virtual void Reject()
Definition: findnotification.cpp:134
ICoreProxy_ptr
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181
clearlineeditaddon.h
LC::Util::FindNotification::SetSuccessful
void SetSuccessful(bool successful)
Updates the widget to show whether the search has been successful.
Definition: findnotification.cpp:85
LC::Util::ClearLineEditAddon::SetEscClearsEdit
void SetEscClearsEdit(bool clears)
Toggles whether Esc button clears the line edit.
Definition: clearlineeditaddon.cpp:62
LC::Util::FindNotification::SetText
void SetText(const QString &text)
Sets the text in the find field.
Definition: findnotification.cpp:75
LC::Util::FindNotification::FindNext
void FindNext()
Search for the next occurrence of the search text.
Definition: findnotification.cpp:111
LC::Util::FindNotification::GetFlags
FindFlags GetFlags() const
Returns the current find flags except the direction.
Definition: findnotification.cpp:101
ishortcutproxy.h
LC::Util::FindNotification::FindCaseSensitively
@ FindCaseSensitively
Definition: findnotification.h:76
LC::Util::FindNotification::Clear
void Clear()
Clears the text in the find field.
Definition: findnotification.cpp:129
LC::Util::FindNotification::~FindNotification
~FindNotification() override