Branch data 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 : 206 : 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 : 259 : auto inGrepMode() const -> bool { return m_mode; }
39 : :
40 : : /**
41 : : * @brief Enter content-search mode (search button toggled on).
42 : : */
43 : 22 : 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 : 20 : m_mode = false;
54 [ + + + + ]: 20 : if (m_busy) {
55 : 4 : m_busy = false;
56 : 4 : m_cancelled = true;
57 : : return true;
58 : : }
59 : : return false;
60 : : }
61 : :
62 : : /**
63 : : * @brief Begin a search for a non-empty query.
64 : : * @return true if a wait cursor should now be shown (search newly busy).
65 : : */
66 : : auto beginSearch() -> bool {
67 : 9 : m_cancelled = false;
68 [ + - ]: 9 : if (!m_busy) {
69 : 9 : m_busy = true;
70 : : return true;
71 : : }
72 : : return false;
73 : : }
74 : :
75 : : /**
76 : : * @brief Cancel the current search (e.g. query cleared).
77 : : * @return true if a wait cursor should be restored (a search was busy).
78 : : */
79 : : auto cancelSearch() -> bool {
80 : 8 : m_cancelled = true;
81 [ + + + + ]: 8 : if (m_busy) {
82 : 3 : m_busy = false;
83 : : return true;
84 : : }
85 : : return false;
86 : : }
87 : :
88 : : /**
89 : : * @brief Record that a search finished and report required side effects.
90 : : * @return Whether to restore the wait cursor and whether to discard the
91 : : * results (because the search was cancelled).
92 : : */
93 : : auto finishSearch() -> FinishOutcome {
94 : 20 : FinishOutcome outcome{m_busy, m_cancelled};
95 : 20 : m_busy = false;
96 : 20 : m_cancelled = false;
97 : : return outcome;
98 : : }
99 : :
100 : : private:
101 : : bool m_mode = false; ///< Content-search mode active.
102 : : bool m_busy = false; ///< A grep subprocess is in flight.
103 : : bool m_cancelled = false; ///< Pending results should be discarded.
104 : : };
105 : :
106 : : #endif // SRC_GREPSEARCHCONTROLLER_H_
|