Line data Source code
1 : // SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief GrepSearchController: the mode/busy/cancelled state machine behind
6 : * MainWindow's content search.
7 : */
8 : #ifndef SRC_GREPSEARCHCONTROLLER_H_
9 : #define SRC_GREPSEARCHCONTROLLER_H_
10 :
11 : /**
12 : * @class GrepSearchController
13 : * @ingroup ui
14 : * @brief State machine for MainWindow's content-search (grep) mode.
15 : *
16 : * Extracted from MainWindow, where the search state lived in three loose
17 : * booleans (mode / busy / cancelled) poked from six different slots. This
18 : * owns those flags behind intent-named transitions so the slots no longer
19 : * introspect raw booleans; each transition returns what UI side effect the
20 : * caller still has to perform (wait-cursor handling, discarding stale
21 : * results). It holds no widgets — MainWindow keeps those and reacts to the
22 : * returned outcomes.
23 : */
24 131 : class GrepSearchController {
25 : public:
26 : /**
27 : * @brief Outcome of a search-completion transition.
28 : */
29 : struct FinishOutcome {
30 : bool restoreCursor; ///< A wait cursor was active and must be restored.
31 : bool discard; ///< The finished search was cancelled; ignore results.
32 : };
33 :
34 : /**
35 : * @brief Whether content-search mode is currently active.
36 : * @return true if in grep mode.
37 : */
38 164 : auto inGrepMode() const -> bool { return m_mode; }
39 :
40 : /**
41 : * @brief Enter content-search mode (search button toggled on).
42 : */
43 7 : void enterGrepMode() { m_mode = true; }
44 :
45 : /**
46 : * @brief Leave content-search mode (search button toggled off).
47 : *
48 : * If a search was in flight it is marked cancelled so its pending results
49 : * are discarded when they arrive.
50 : * @return true if a busy search was interrupted (restore the wait cursor).
51 : */
52 : auto leaveGrepMode() -> bool {
53 5 : m_mode = false;
54 5 : if (m_busy) {
55 1 : m_busy = false;
56 1 : m_cancelled = true;
57 : return true;
58 : }
59 : return false;
60 : }
61 :
62 : /**
63 : * @brief Drop the mode flag only, without touching busy/cancelled state.
64 : *
65 : * Used when the panel is cleared and the search UI is reset in place.
66 : */
67 2 : void clearGrepMode() { m_mode = false; }
68 :
69 : /**
70 : * @brief Begin a search for a non-empty query.
71 : * @return true if a wait cursor should now be shown (search newly busy).
72 : */
73 : auto beginSearch() -> bool {
74 3 : m_cancelled = false;
75 3 : if (!m_busy) {
76 3 : m_busy = true;
77 : return true;
78 : }
79 : return false;
80 : }
81 :
82 : /**
83 : * @brief Cancel the current search (e.g. query cleared).
84 : * @return true if a wait cursor should be restored (a search was busy).
85 : */
86 : auto cancelSearch() -> bool {
87 1 : m_cancelled = true;
88 1 : if (m_busy) {
89 1 : m_busy = false;
90 : return true;
91 : }
92 : return false;
93 : }
94 :
95 : /**
96 : * @brief Record that a search finished and report required side effects.
97 : * @return Whether to restore the wait cursor and whether to discard the
98 : * results (because the search was cancelled).
99 : */
100 : auto finishSearch() -> FinishOutcome {
101 6 : FinishOutcome outcome{m_busy, m_cancelled};
102 6 : m_busy = false;
103 6 : m_cancelled = false;
104 : return outcome;
105 : }
106 :
107 : private:
108 : bool m_mode = false; ///< Content-search mode active.
109 : bool m_busy = false; ///< A grep subprocess is in flight.
110 : bool m_cancelled = false; ///< Pending results should be discarded.
111 : };
112 :
113 : #endif // SRC_GREPSEARCHCONTROLLER_H_
|