LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
findnotificationwk.h
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 #pragma once
10 
11 #include "findnotification.h"
12 #include <QWebPage>
13 #include <QWebView>
14 
15 namespace LC::Util
16 {
32  class UTIL_GUI_API FindNotificationWk : public FindNotification
33  {
34  QWebView * const WebView_;
35  QString PreviousFindText_;
36  public:
46  FindNotificationWk (const ICoreProxy_ptr& proxy, QWebView *near)
47  : FindNotification { proxy, near }
48  , WebView_ { near }
49  {
50  connect (near,
51  &QWebView::loadFinished,
52  this,
53  [this]
54  {
55  if (PreviousFindText_.isEmpty ())
56  return;
57 
58  ClearFindResults ();
59  FindNext ();
60  });
61  }
62 
69  static QWebPage::FindFlags ToPageFlags (FindFlags findFlags)
70  {
71  QWebPage::FindFlags pageFlags;
72  auto check = [&pageFlags, findFlags] (FindFlag ourFlag, QWebPage::FindFlag pageFlag)
73  {
74  if (findFlags & ourFlag)
75  pageFlags |= pageFlag;
76  };
77  check (FindCaseSensitively, QWebPage::FindCaseSensitively);
78  check (FindBackwards, QWebPage::FindBackward);
79  check (FindWrapsAround, QWebPage::FindWrapsAroundDocument);
80  return pageFlags;
81  }
82  private:
83  void ClearFindResults ()
84  {
85  PreviousFindText_.clear ();
86  WebView_->page ()->findText ({}, QWebPage::HighlightAllOccurrences);
87  }
88  protected:
89  void HandleNext (const QString& text, FindFlags findFlags) override
90  {
91  const auto flags = ToPageFlags (findFlags);
92 
93  if (PreviousFindText_ != text)
94  {
95  const auto nflags = flags | QWebPage::HighlightAllOccurrences;
96  WebView_->page ()->findText ({}, nflags);
97  WebView_->page ()->findText (text, nflags);
98  PreviousFindText_ = text;
99  }
100 
101  const auto found = WebView_->page ()->findText (text, flags);
102  SetSuccessful (found);
103  }
104 
105  void Reject () override
106  {
108  ClearFindResults ();
109  }
110  };
111 }
LC::Util
Definition: icoreproxy.h:33
findnotification.h
LC::Util::FindNotification::Reject
virtual void Reject()
Definition: findnotification.cpp:134
UTIL_GUI_API
#define UTIL_GUI_API
Definition: guiconfig.h:16
ICoreProxy_ptr
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181