Debian Codegraph @

src/Gui/Tools.h

raw
1 /***************************************************************************
2 * Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * Copyright (c) 2025 Kacper Donat <[email protected]> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24 #ifndef GUI_TOOLS_H
25 #define GUI_TOOLS_H
26
27 #include <FCConfig.h>
28 #include <FCGlobal.h>
29
30 #include <QFontMetrics>
31 #include <QKeyEvent>
32 #include <QKeySequence>
33 #include <QModelIndex>
34
35 /**
36 * @brief The QtTools namespace
37 *
38 * Helper namespace to provide utilities to ease work with Qt.
39 */
40 namespace Gui::QtTools
41 {
42 inline int horizontalAdvance(const QFontMetrics& fm, QChar ch)
43 {
44 return fm.horizontalAdvance(ch);
45 }
46
47 inline int horizontalAdvance(const QFontMetrics& fm, const QString& text, int len = -1)
48 {
49 return fm.horizontalAdvance(text, len);
50 }
51
52 inline bool matches(QKeyEvent* ke, const QKeySequence& ks)
53 {
54 uint searchkey = (ke->modifiers() | ke->key()) & ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
55 return ks == QKeySequence(searchkey);
56 }
57
58 inline QKeySequence::StandardKey deleteKeySequence()
59 {
60 #ifdef FC_OS_MACOSX
61 return QKeySequence::Backspace;
62 #else
63 return QKeySequence::Delete;
64 #endif
65 }
66
67 // clang-format off
68 /**
69 * TreeWalkCallable is a function that takes const QModelIndex& and:
70 * - returns void, if there is no stopping logic;
71 * - returns boolean, if there is logic that should stop tree traversal.
72 */
73 template<typename Func>
74 concept TreeWalkCallable =
75 std::is_invocable_r_v<void, Func, const QModelIndex&> ||
76 std::is_invocable_r_v<bool, Func, const QModelIndex&>;
77 // clang-format on
78
79 /**
80 * @brief Recursively traverses a QAbstractItemModel tree structure.
81 *
82 * The function traverses a tree model starting from a given index, or the root
83 * if no index is provided. For each node, it invokes the provided callable `func`.
84 *
85 * The callable can:
86 * - Return `void`, in which case the traversal continues through all nodes.
87 * - Return `bool`, in which case returning `true` stops further traversal.
88 *
89 * @param[in] model The tree model to traverse.
90 * @param[in] func A callable object applied to each QModelIndex. It can either
91 * return `void` or `bool` (for stopping logic).
92 * @param[in] index The starting index for traversal. If omitted, defaults to the root.
93 */
94 void walkTreeModel(
95 const QAbstractItemModel* model,
96 TreeWalkCallable auto&& func,
97 const QModelIndex& index = {}
98 )
99 {
100 using ReturnType = std::invoke_result_t<decltype(func), const QModelIndex&>;
101
102 if (index.isValid()) {
103 if constexpr (std::is_same_v<ReturnType, void>) {
104 func(index);
105 }
106 else if constexpr (std::is_same_v<ReturnType, bool>) {
107 if (func(index)) {
108 return;
109 }
110 }
111 }
112
113 for (int i = 0; i < model->rowCount(index); ++i) {
114 walkTreeModel(model, func, model->index(i, 0, index));
115 }
116 }
117
118 template<typename T>
119 T valueOr(const QVariant& variant, const T& defaultValue)
120 {
121 return variant.canConvert<T>() ? variant.value<T>() : defaultValue;
122 }
123
124 } // namespace Gui::QtTools
125
126 #endif // GUI_TOOLS_H