LeechCraft 0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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
17namespace 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,
51 [this] (const auto& str) { Ui_->FindButton_->setEnabled (!str.isEmpty ()); });
52 connect (Ui_->FindButton_,
53 &QPushButton::released,
54 this,
55 [this]
56 {
57 auto flags = GetDirectionlessFlags ();
58 if (Ui_->SearchBackwards_->checkState () == Qt::Checked)
59 flags |= FindBackwards;
60 HandleNext (Ui_->Pattern_->text (), flags);
61 });
62 connect (Ui_->CloseButton_,
63 &QPushButton::released,
64 this,
66 }
67
69
71 {
72 EscShortcut_->setEnabled (close);
73 }
74
75 void FindNotification::SetFlags (FindFlags flags)
76 {
77 Ui_->MatchCase_->setCheckState (flags & FindCaseSensitively ? Qt::Checked : Qt::Unchecked);
78 Ui_->WrapAround_->setCheckState (flags & FindWrapsAround ? Qt::Checked : Qt::Unchecked);
79 Ui_->SearchBackwards_->setCheckState (flags & FindBackwards ? Qt::Checked : Qt::Unchecked);
80 }
81
82 void FindNotification::SetText (const QString& text)
83 {
84 Ui_->Pattern_->setText (text);
85 }
86
88 {
89 return Ui_->Pattern_->text ();
90 }
91
93 {
94 auto ss = QStringLiteral ("QLineEdit { background-color: ");
95 if (!success)
96 ss.append ("#FF0000");
97 else
98 {
99 auto color = QApplication::palette ().color (QPalette::Base);
100 color.setRedF (color.redF () / 2);
101 color.setBlueF (color.blueF () / 2);
102 ss.append (color.name ());
103 }
104 ss.append (" }");
105 Ui_->Pattern_->setStyleSheet (ss);
106 }
107
108 auto FindNotification::GetDirectionlessFlags () const -> FindFlags
109 {
110 FindFlags flags;
111 if (Ui_->MatchCase_->checkState () == Qt::Checked)
112 flags |= FindCaseSensitively;
113 if (Ui_->WrapAround_->checkState () == Qt::Checked)
114 flags |= FindWrapsAround;
115 return flags;
116 }
117
119 {
120 const auto& text = GetText ();
121 if (text.isEmpty ())
122 return;
123
124 HandleNext (text, GetDirectionlessFlags ());
125 }
126
128 {
129 const auto& text = GetText ();
130 if (text.isEmpty ())
131 return;
132
133 HandleNext (text, GetDirectionlessFlags () | FindBackwards);
134 }
135
137 {
138 SetText ({});
139 }
140
142 {
143 Ui_->Pattern_->clear ();
144 hide ();
145 }
146}
virtual IPluginsManager * GetPluginsManager() const =0
Returns the application's plugin manager.
virtual IShortcutProxy * GetShortcutProxy() const =0
Returns the shortcut proxy used to communicate with the shortcut manager.
virtual QObject * GetPluginByID(const QByteArray &id) const =0
Returns plugin identified by its id.
Provides a "clear text" action for line edits.
void SetEscClearsEdit(bool clears)
Toggles whether Esc button clears the line edit.
void SetFlags(FindFlags flags)
Sets the flags and updates the widget visual state accordingly.
void SetEscCloses(bool close)
Sets whether Esc closes the widget.
void FindNext()
Search for the next 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 Clear()
Clears the text in the find field.
FindNotification(const ICoreProxy_ptr &proxy, QWidget *near)
Creates the search widget in parent layout of near.
void SetSuccessful(bool successful)
Updates the widget to show whether the search has been successful.
QString GetText() const
Returns the currently entered text in the find field.
void FindPrevious()
Search for the previous occurrence of the search text.
PageNotification(QWidget *parent)
Creates the widget embedding into the parent layout of the 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:14
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition icoreproxy.h:177