Debian Codegraph @

src/Gui/Dialogs/DlgPreferencesImp.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /****************************************************************************
4 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
5 * Copyright (c) 2023 FreeCAD Project Association *
6 * *
7 * This file is part of FreeCAD. *
8 * *
9 * FreeCAD is free software: you can redistribute it and/or modify it *
10 * under the terms of the GNU Lesser General Public License as *
11 * published by the Free Software Foundation, either version 2.1 of the *
12 * License, or (at your option) any later version. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17 * Lesser General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Lesser General Public *
20 * License along with FreeCAD. If not, see *
21 * <https://www.gnu.org/licenses/>. *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef GUI_DIALOG_DLGPREFERENCESIMP_H
27 #define GUI_DIALOG_DLGPREFERENCESIMP_H
28
29 #include <QDialog>
30 #include <QStandardItemModel>
31 #include <QStackedWidget>
32 #include <QPointer>
33 #include <QListWidget>
34 #include <QLineEdit>
35 #include <memory>
36 #include <FCGlobal.h>
37
38 class QAbstractButton;
39 class QListWidgetItem;
40 class QTabWidget;
41 class QKeyEvent;
42 class QMouseEvent;
43
44 namespace Gui::Dialog
45 {
46 class PreferencePage;
47 class Ui_DlgPreferences;
48 class DlgPreferencesImp;
49
50 class GuiExport PreferencesSearchController: public QObject
51 {
52 Q_OBJECT
53
54 private:
55 enum class PopupAction
56 {
57 KeepOpen, // don't close popup (used for keyboard navigation)
58 CloseAfter // close popup (used for mouse clicks and Enter/Return)
59 };
60
61 public:
62 // Custom data roles for separating path and widget text
63 enum SearchDataRole
64 {
65 PathRole = Qt::UserRole + 10, // Path to page (e.g., "Display/3D View")
66 WidgetTextRole = Qt::UserRole + 11 // Text from the widget (e.g., "Enable anti-aliasing")
67 };
68
69 // Search results structure
70 struct SearchResult
71 {
72 QString groupName;
73 QString pageName;
74 QPointer<QWidget> widget;
75 QString matchText;
76 QString groupBoxName;
77 QString tabName; // The tab name (like "Display")
78 QString pageDisplayName; // The page display name (like "3D View")
79 bool isPageLevelMatch = false; // True if this is a page title match
80 int score = 0; // Fuzzy search score for sorting
81 };
82
83 explicit PreferencesSearchController(DlgPreferencesImp* parentDialog, QObject* parent = nullptr);
84 ~PreferencesSearchController() = default;
85
86 // Setup methods
87 void setPreferencesModel(QStandardItemModel* model);
88 void setGroupNameRole(int role);
89 void setPageNameRole(int role);
90
91 // UI access methods
92 QListWidget* getSearchResultsList() const;
93 bool isPopupVisible() const;
94 bool isPopupUnderMouse() const;
95 bool isPopupAncestorOf(QWidget* widget) const;
96
97 // Event handling
98 bool handleSearchBoxKeyPress(QKeyEvent* keyEvent);
99 bool handlePopupKeyPress(const QKeyEvent* keyEvent);
100 bool isClickOutsidePopup(const QMouseEvent* mouseEvent) const;
101
102 // Focus management
103 void ensureSearchBoxFocus();
104
105 // Search functionality
106 void performSearch(const QString& searchText);
107 void clearHighlights();
108 void hideSearchResultsList();
109 void showSearchResultsList();
110
111 // Navigation
112 void navigateToCurrentSearchResult(PopupAction action);
113
114 Q_SIGNALS:
115 void navigationRequested(const QString& groupName, const QString& pageName);
116
117 public Q_SLOTS:
118 void onSearchTextChanged(const QString& text);
119 void onSearchResultSelected();
120 void onSearchResultClicked();
121 void onSearchResultDoubleClicked();
122
123 private:
124 // Search implementation
125 void collectSearchResults(
126 QWidget* widget,
127 const QString& searchText,
128 const QString& groupName,
129 const QString& pageName,
130 const QString& pageDisplayName,
131 const QString& tabName
132 );
133 void populateSearchResultsList();
134
135 template<typename WidgetType>
136 void searchWidgetType(
137 QWidget* parentWidget,
138 const QString& searchText,
139 const QString& groupName,
140 const QString& pageName,
141 const QString& pageDisplayName,
142 const QString& tabName
143 );
144
145 // UI helpers
146 void configurePopupSize();
147 int calculatePopupHeight(int popupWidth) const;
148 void applyHighlightToWidget(QWidget* widget);
149 static QString getHighlightStyleForWidget(QWidget* widget);
150
151 // Search result navigation
152 void selectNextSearchResult();
153 void selectPreviousSearchResult();
154
155 // Utility methods
156 static QString findGroupBoxForWidget(QWidget* widget);
157 static bool fuzzyMatch(const QString& searchText, const QString& targetText, int& score);
158 static bool isExactMatch(const QString& searchText, const QString& targetText);
159
160 private:
161 DlgPreferencesImp* m_parentDialog;
162 QStandardItemModel* m_preferencesModel;
163 int m_groupNameRole;
164 int m_pageNameRole;
165
166 // UI components
167 QLineEdit* m_searchBox;
168 QListWidget* m_searchResultsList;
169
170 // Search state
171 QList<SearchResult> m_searchResults;
172 QString m_lastSearchText;
173 QList<QWidget*> m_highlightedWidgets;
174 QMap<QWidget*, QString> m_originalStyles;
175 };
176
177 class PreferencesPageItem: public QStandardItem
178 {
179 public:
180 QWidget* getWidget() const;
181 void setWidget(QWidget* widget);
182
183 bool isExpanded() const;
184 void setExpanded(bool expanded);
185
186 static constexpr char const* PropertyName = "SettingsPageItem";
187
188 private:
189 QWidget* _widget = nullptr;
190 bool _expanded = false;
191 };
192
193 /**
194 * This class implements a dialog containing several preference pages.
195 *
196 * To append your own page you just have to take note of these points:
197 *
198 * \li Each preference page can be created by the Qt Designer selecting the "Widget" item
199 * in the project dialog.
200 *
201 * \li To save or load the widgets' settings automatically (e.g. combo boxes, line edits,
202 * check boxes, ...) you can make use of the classes inherited from @ref PrefWidget such as:
203 * PrefSpinBox, PrefLineEdit, PrefComboBox, PrefListBox, PrefCheckBox, PrefRadioButton and
204 * PrefSlider. If you have compiled and installed the library under src/Tools/plugins/widgets
205 * to QTDIR/plugins/designer you should see the new category "Preferences".
206 * Moreover you have to make sure to have specified the "prefEntry" and "prefPath" properties for
207 * each preference widget you have used inside your form in Qt Designer.
208 *
209 * \li For each widget inside your page - you want to save or load - you have to call
210 * \<objectname\>->onSave() or \<objectname\>->onRestore(). The best way to this is either to
211 * define the protected slots saveSettings() and loadSettings() in your form and overwrite
212 * them in a subclass or define these slots in this subclass directly.
213 *
214 * See the example below for more details:
215 *
216 * \code
217 * // This class was created by Qt's uic tool
218 * class MyPrefPage : public QWidget
219 * {
220 * public:
221 * MyPrefPage( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 )
222 * {
223 * myLineEdit->setProperty( "prefEntry", "lineedit" );
224 * myLineEdit->setProperty( "prefPath", "GroupName" );
225 * myCheckBox->setProperty( "prefEntry", "checkbox" );
226 * myCheckBox->setProperty( "prefPath", "GroupName" );
227 * ...
228 * }
229 *
230 * PrefLineEdit* myLineEdit;
231 * PrefCheckBox* myCheckBox;
232 * };
233 * \endcode
234 * In the derived class you just have to implement the methods saveSettings() and loadSettings()
235 * in the following way:.
236 * \code
237 * class MyPrefPageImp : public MyPrefPage
238 * {
239 * public:
240 * MyPrefPageImp( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 )
241 * {
242 * }
243 * protected Q_SLOTS:
244 * void saveSettings()
245 * {
246 * myLineEdit->onSave();
247 * myCheckBox->onSave();
248 * }
249 * void loadSettings();
250 * {
251 * myLineEdit->onRestore();
252 * myCheckBox->onRestore();
253 * }
254 * };
255 * \endcode
256 *
257 * \li Now you have to make the widget factory to know your class by adding the line
258 * new PrefPageProducer<MyPrefPageImp> (QT_TR_NOOP("My category"));
259 *
260 * \see PrefWidget
261 * \author Werner Mayer, Jürgen Riegel
262 */
263 class GuiExport DlgPreferencesImp: public QDialog
264 {
265 Q_OBJECT
266
267 static constexpr double maxScreenWidthCoveragePercent = 0.8; // maximum % of screen width taken
268 // by the dialog
269 static constexpr int minVerticalEmptySpace = 100; // px of vertical space to leave
270
271 public:
272 static void addPage(const std::string& className, const std::string& group);
273 static void removePage(const std::string& className, const std::string& group);
274 static void setGroupData(const std::string& group, const std::string& icon, const QString& tip);
275 static void getGroupData(const std::string& group, std::string& icon, QString& tip);
276 static void reloadSettings();
277
278 static PreferencePage* createPreferencePage(
279 const std::string& pageName,
280 const std::string& groupName
281 );
282
283 explicit DlgPreferencesImp(QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags());
284 ~DlgPreferencesImp() override;
285
286 void accept() override;
287 void reject() override;
288 void reload();
289 void activateGroupPage(const QString& group, int index);
290 void activateGroupPageByPageName(const QString& group, const QString& pageName);
291 void activeGroupPage(QString& group, int& index) const;
292
293 protected:
294 void changeEvent(QEvent* e) override;
295 void showEvent(QShowEvent*) override;
296 bool eventFilter(QObject* obj, QEvent* event) override;
297
298 protected Q_SLOTS:
299 void onButtonBoxClicked(QAbstractButton*);
300 void onPageSelected(const QModelIndex& index);
301 void onStackWidgetChange(int index);
302
303 void onGroupExpanded(const QModelIndex& index);
304 void onGroupCollapsed(const QModelIndex& index);
305
306 void onNavigationRequested(const QString& groupName, const QString& pageName);
307
308 private:
309 /** @name for internal use only */
310 //@{
311 void setupConnections();
312 void setupPages();
313 void reloadPages();
314
315 PreferencesPageItem* getCurrentPage() const;
316
317 PreferencesPageItem* createGroup(const std::string& groupName);
318 void createPageInGroup(PreferencesPageItem* item, const std::string& pageName);
319
320 void applyChanges();
321 void showResetOptions();
322 void restoreDefaults();
323 void restorePageDefaults(PreferencesPageItem* item);
324 void restartIfRequired();
325
326 void updatePageDependentWidgets();
327
328 QPixmap loadIconForGroup(const std::string& name) const;
329
330 void addSizeHint(QWidget*);
331 int minimumPageWidth() const;
332 int minimumDialogWidth(int) const;
333 void expandToMinimumDialogWidth();
334
335 // Navigation helper for search controller
336 void navigateToSearchResult(const QString& groupName, const QString& pageName);
337 //@}
338
339 private:
340 using TGroupPages = std::pair<std::string, std::list<std::string>>;
341
342 static std::list<TGroupPages> _pages; /**< Name of all registered preference pages */
343
344 QStandardItemModel _model;
345 QSize _sizeHintOfPages;
346
347 struct Group
348 {
349 std::string iconName;
350 QString tooltip;
351 };
352 static std::map<std::string, Group> _groupMap;
353 std::unique_ptr<Ui_DlgPreferences> ui;
354
355 bool invalidParameter;
356 bool canEmbedScrollArea;
357 bool restartRequired;
358
359 // Search controller
360 std::unique_ptr<PreferencesSearchController> m_searchController;
361
362 /**< A name for our Qt::UserRole, used when storing user data in a list item */
363 static const int GroupNameRole;
364 static const int PageNameRole;
365
366 static constexpr char const* GroupNameProperty = "GroupName";
367 static constexpr char const* PageNameProperty = "PageName";
368
369 static DlgPreferencesImp* _activeDialog; /**< Defaults to the nullptr, points to the current
370 instance if there is one */
371
372 // Friend class to allow search controller access to UI
373 friend class PreferencesSearchController;
374 };
375
376 } // namespace Gui::Dialog
377
378 #endif // GUI_DIALOG_DLGPREFERENCESIMP_H