LeechCraft  0.6.70-16373-g319c272718
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, "Find.Show"),
37  [this]
38  {
39  show ();
40  setFocus ();
41  },
42  parent);
43  CreateShortcuts (scProxy->GetShortcuts (coreInstance, "Find.Next"),
44  this, &FindNotification::FindNext, parent);
45  CreateShortcuts (scProxy->GetShortcuts (coreInstance, "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  connect (Ui_->CloseButton_,
61  &QPushButton::released,
62  this,
64  }
65 
67 
69  {
70  EscShortcut_->setEnabled (close);
71  }
72 
73  void FindNotification::SetText (const QString& text)
74  {
75  Ui_->Pattern_->setText (text);
76  }
77 
78  QString FindNotification::GetText () const
79  {
80  return Ui_->Pattern_->text ();
81  }
82 
83  void FindNotification::SetSuccessful (bool success)
84  {
85  auto ss = QStringLiteral ("QLineEdit { background-color: ");
86  if (!success)
87  ss.append ("#FF0000");
88  else
89  {
90  auto color = QApplication::palette ().color (QPalette::Base);
91  color.setRedF (color.redF () / 2);
92  color.setBlueF (color.blueF () / 2);
93  ss.append (color.name ());
94  }
95  ss.append (" }");
96  Ui_->Pattern_->setStyleSheet (ss);
97  }
98 
99  auto FindNotification::GetFlags () const -> FindFlags
100  {
101  FindFlags flags;
102  if (Ui_->MatchCase_->checkState () == Qt::Checked)
103  flags |= FindCaseSensitively;
104  if (Ui_->WrapAround_->checkState () == Qt::Checked)
105  flags |= FindWrapsAround;
106  return flags;
107  }
108 
110  {
111  const auto& text = GetText ();
112  if (text.isEmpty ())
113  return;
114 
115  HandleNext (text, GetFlags ());
116  }
117 
119  {
120  const auto& text = GetText ();
121  if (text.isEmpty ())
122  return;
123 
124  HandleNext (text, GetFlags () | FindBackwards);
125  }
126 
128  {
129  SetText ({});
130  }
131 
133  {
134  Ui_->Pattern_->clear ();
135  hide ();
136  }
137 }
void SetSuccessful(bool successful)
Updates the widget to show whether the search has been successful.
void FindNext()
Search for the next occurrence of the search text.
void FindPrevious()
Search for the previous occurrence of the search text.
virtual void HandleNext(const QString &text, FindFlags flags)=0
Called each time the user requests a search.
void SetText(const QString &text)
Sets the text in the find field.
void SetEscCloses(bool close)
Sets whether Esc closes the widget.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181
FindNotification(const ICoreProxy_ptr &proxy, QWidget *near)
Creates the search widget in parent layout of near.
FindFlags GetFlags() const
Returns the current find flags except the direction.
QString GetText() const
Returns the currently entered text in the find field.
void Clear()
Clears the text in the find field.
A horizontal widget embedding into the parent layout of the passed parent widget. ...
void CreateShortcuts(const QList< QKeySequence > &shortcuts, const std::function< void()> &func, QWidget *parent)
Makes func invokable with shortcuts in seq.
Definition: util.cpp:13