LeechCraft  0.6.70-10870-g558588d6ec
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  * 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 "findnotification.h"
31 #include <QShortcut>
34 #include "clearlineeditaddon.h"
35 #include "util/shortcuts/util.h"
36 #include "ui_findnotification.h"
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
43  : Util::PageNotification { parent }
44  , Ui_ { new Ui::FindNotification }
45  , EscShortcut_ { new QShortcut { Qt::Key_Escape, this, SLOT (reject ()) } }
46  {
47  Ui_->setupUi (this);
48 
49  setFocusProxy (Ui_->Pattern_);
50 
51  EscShortcut_->setContext (Qt::WidgetWithChildrenShortcut);
52 
53  const auto addon = new Util::ClearLineEditAddon { proxy, Ui_->Pattern_ };
54  addon->SetEscClearsEdit (false);
55 
56  const auto coreInstance = proxy->GetPluginsManager ()->
57  GetPluginByID ("org.LeechCraft.CoreInstance");
58  const auto scProxy = proxy->GetShortcutProxy ();
59 
60  CreateShortcuts (scProxy->GetShortcuts (coreInstance, "Find.Show"),
61  [this]
62  {
63  show ();
64  setFocus ();
65  },
66  parent);
67  CreateShortcuts (scProxy->GetShortcuts (coreInstance, "Find.Next"),
68  this, SLOT (findNext ()), parent);
69  CreateShortcuts (scProxy->GetShortcuts (coreInstance, "Find.Prev"),
70  this, SLOT (findPrevious ()), parent);
71  }
72 
74  {
75  delete Ui_;
76  }
77 
79  {
80  EscShortcut_->setEnabled (close);
81  }
82 
83  void FindNotification::SetText (const QString& text)
84  {
85  Ui_->Pattern_->setText (text);
86  }
87 
88  QString FindNotification::GetText () const
89  {
90  return Ui_->Pattern_->text ();
91  }
92 
93  void FindNotification::SetSuccessful (bool success)
94  {
95  auto ss = QString { "QLineEdit {"
96  "background-color:rgb(" };
97  if (!success)
98  ss.append ("255,0,0");
99  else
100  {
101  auto color = QApplication::palette ().color (QPalette::Base);
102  color.setRedF (color.redF () / 2);
103  color.setBlueF (color.blueF () / 2);
104 
105  int r = 0, g = 0, b = 0;
106  color.getRgb (&r, &g, &b);
107 
108  ss.append (QString ("%1,%2,%3")
109  .arg (r)
110  .arg (g)
111  .arg (b));
112  }
113  ss.append (") }");
114  Ui_->Pattern_->setStyleSheet (ss);
115  }
116 
117  auto FindNotification::GetFlags () const -> FindFlags
118  {
119  FindFlags flags;
120  if (Ui_->MatchCase_->checkState () == Qt::Checked)
121  flags |= FindCaseSensitively;
122  if (Ui_->WrapAround_->checkState () == Qt::Checked)
123  flags |= FindWrapsAround;
124  return flags;
125  }
126 
128  {
129  const auto& text = GetText ();
130  if (text.isEmpty ())
131  return;
132 
133  handleNext (text, GetFlags ());
134  }
135 
137  {
138  const auto& text = GetText ();
139  if (text.isEmpty ())
140  return;
141 
142  handleNext (text, GetFlags () | FindBackwards);
143  }
144 
146  {
147  SetText ({});
148  }
149 
151  {
152  Ui_->Pattern_->clear ();
153  hide ();
154  }
155 
156  void FindNotification::on_Pattern__textChanged (const QString& newText)
157  {
158  Ui_->FindButton_->setEnabled (!newText.isEmpty ());
159  }
160 
161  void FindNotification::on_FindButton__released ()
162  {
163  auto flags = GetFlags ();
164  if (Ui_->SearchBackwards_->checkState () == Qt::Checked)
165  flags |= FindBackwards;
166 
167  handleNext (Ui_->Pattern_->text (), flags);
168  }
169 }
170 }
void SetText(const QString &text)
Sets the text in the find field.
void findPrevious()
Search for the previous occurrence of the search text.
QString GetText() const
Returns the currently entered text in the find field.
void SetSuccessful(bool successful)
Updates the widget to show whether the search has been successful.
void CreateShortcuts(const QList< QKeySequence > &shortcuts, const std::function< void()> &func, QWidget *parent)
Makes func invokable with shortcuts in seq.
Definition: util.cpp:38
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
virtual void handleNext(const QString &text, FindFlags flags)=0
Called each time the user requests a search.
void findNext()
Search for the next occurrence of the search text.
A horizontal widget embedding into the parent layout of the passed parent widget. ...
void SetEscCloses(bool close)
Sets whether Esc closes the widget.
FindNotification(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.
void clear()
Clears the text in the find field.