ADTF  3.18.4
search_text_widget.h
Go to the documentation of this file.
1 
8 /*
9  * This file depends on Qt which is licensed under LGPLv3.
10  * See ADTF_DIR/3rdparty/qt5 and doc/license for detailed information.
11  */
12 
13 #pragma once
14 
15 #include <adtf_utils.h>
16 
17 #include <QActionGroup>
18 #include <QLabel>
19 #include <QLineEdit>
20 #include <QMetaType>
21 #include <QObject>
22 #include <QPushButton>
23 #include <QSortFilterProxyModel>
24 #include <QSpinBox>
25 #include <QStandardItemModel>
26 #include <QString>
27 #include <QThreadPool>
28 #include <QSet>
29 #include <unordered_set>
30 
31 namespace adtf
32 {
33 namespace ui
34 {
35 namespace widget
36 {
37 namespace osborn
38 {
39 
41 enum class FilterType
42 {
43  normal,
44  wildcard,
45  regex,
46  fuzzy
47 };
48 
49 class cSearchTextWidget;
50 
51 // cFilterModel
52 
53 class cFilterModel : public QSortFilterProxyModel
54 {
55  Q_OBJECT
56 
57  public:
58  using QSortFilterProxyModel::QSortFilterProxyModel;
59  cFilterModel(QObject* parent = nullptr);
60 
61  public:
62  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
63 
64  void SetFilterType(FilterType eFilterType);
65  void SetFilterColumn(int64_t nFilterColumn);
66  void ClearMatchingData();
67  void SetMatchingTexts(const QSet<QString>& oMatchingTexts);
68 
69  private:
70  bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
71 
72  void ClearMatchings();
73 
74  private:
75  FilterType m_eFilterType = FilterType::normal;
76  int64_t m_nFilterColumn = -1;
77 
78  bool m_bUseMatchingData = false;
79 
80  QSet<QString> m_setMatchingTexts;
81 };
82 
83 // cSearchTextWidget
89 class cSearchTextWidget : public QFrame
90 {
91  Q_OBJECT
92 
93  signals:
99  void resultChanged();
100 
101  public:
102  /*
103  * Constructor.
104  * @param pParent parent widget
105  */
106  cSearchTextWidget(QWidget* pParent = nullptr);
107 
108  public:
109  /*
110  * Show bottom border.
111  * @param show Whether to show.
112  */
113  void ShowBottomBorder(bool show);
114 
115  /*
116  * Set the model which is filtered.
117  * @param oFilterModel The filter model.
118  */
119  void SetFilterModel(cFilterModel& oFilterModel);
120 
121  /*
122  * Returns index of selected filter column.
123  * @return index
124  */
125  uint64_t GetSelectedFilterColumn() const;
126  /*
127  * Set the names of filter columns which can be single-selected.
128  * @param vecFilterColumnNames The column names.
129  */
130  void SetFilterColumnNames(const std::vector<QString>& vecFilterColumnNames);
131  /*
132  * Set the names of filter columns which can be single-selected.
133  * @param oModel The model from which the column names are obtained.
134  */
135  void SetFilterColumnNamesFromModel(const QAbstractItemModel& oModel);
136 
137  /*
138  * Set the search text.
139  * strSearchText The text.
140  */
141  void SetSearchText(const QString& strSearchText);
143  void Clear();
144 
145  /*
146  * Returns the selected filter type.
147  * @return selected filter type
148  */
149  FilterType GetSelectedFilterType() const;
150  /*
151  * Select the filter type.
152  * @param oFilterType The filter type.
153  */
154  void SelectFilterType(FilterType oFilterType);
155 
157  bool IsCaseSensitive() const;
158  /*
159  * Set whether 'case sensitive' is set or not.
160  * @param bCaseSensitive Whether 'case sensitive' should be set.
161  */
162  void SetCaseSensitive(bool bCaseSensitive);
163 
164  private:
165  bool event(QEvent* event) override;
166 
167  void UpdateStyle();
168  void UpdateUi();
169 
170  void SetItemTextOutOfDate();
171 
172  void HandleChangedSearchParameters(bool bTextChanged);
173 
174  void SearchNormalString();
175  void SearchWildcard();
176  void SearchRegexString();
177  void SearchFuzzy();
178 
179  QSet<QString> ComputeItemTexts(const QModelIndex& oModelIndex, const uint64_t nFilterColumn);
180  void ComputeTreeModelIndexs(const QModelIndex& oModelIndex,
181  const uint64_t nFilterColumn,
182  const std::function<void(const QModelIndex& oIndex)>& fnCallback);
183 
184  private:
185  bool m_bShowBottomBorder = true;
186 
187  cFilterModel* m_pFilterModel = nullptr;
188  QAbstractItemModel* m_pSourceModel = nullptr;
189 
190  QLineEdit* m_pSearchTextLineEdit = nullptr;
191  QLabel* m_pErrorLabel = nullptr;
192  QToolButton* m_pClearButton = nullptr;
193  QMenu* m_pFilterMenu = nullptr;
194  QActionGroup* m_pFilterActionGroup = nullptr;
195  QAction* m_pFuzzyFilterAction = nullptr;
196  QAction* m_pCaseSensitiveAction = nullptr;
197  QPushButton* m_pFilterButton = nullptr;
198  QMenu* m_pFilterColumnMenu = nullptr;
199  QActionGroup* m_pFilterColumnActionGroup = nullptr;
200  QWidget* m_pFuzzyParametersWidget = nullptr;
201  QSpinBox* m_pFuzzyScaledScoreThresholdSpinBox = nullptr;
202 
203  bool m_bIsInInitialization = false;
204 
205  QSet<QString> m_setItemTextCache;
206 
207  QThreadPool m_oSearchThreadPool;
208  std::atomic<bool> m_bSearchCanceled = false;
209  bool m_bSearchRequested = false;
210  std::chrono::time_point<std::chrono::system_clock> m_oKeyPressedTimestamp;
211 };
212 
213 };
214 
217 
218 }
219 }
220 } // namespace adtf
221 Q_DECLARE_METATYPE(adtf::ui::widget::osborn::FilterType);
Copyright © Audi Electronics Venture GmbH.
cSearchTextWidget allows to filter a given QSortProxyModel to be filtered by normal text,...
bool IsCaseSensitive() const
Returns whether 'case sensitive' is selected.
void selectedFilterColumnChanged()
This signal is emitted when the selected filter column changed.
void Clear()
Clears the search text.
void resultChanged()
This signal is emitted when the filtered result changed.
void searchParametersChanged()
This signal is emitted when the search parameters changed.
Namespace for entire ADTF SDK.