1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef QTXSHORTCUTTREE_H
21 #define QTXSHORTCUTTREE_H
26 #include <QTreeWidget>
27 #include "SUIT_ShortcutMgr.h"
37 class QTreeWidgetItem;
39 class QTX_EXPORT QtxKeySequenceEdit : public QFrame
44 QtxKeySequenceEdit(QWidget* = nullptr);
45 virtual ~QtxKeySequenceEdit() = default;
47 void setConfirmedKeySequence(const QKeySequence&);
48 void setEditedKeySequence(const QKeySequence&);
49 QKeySequence editedKeySequence() const;
50 bool isKeySequenceModified() const;
51 void restoreKeySequence();
53 static QString parseEvent(QKeyEvent*);
54 static bool isValidKey(int);
57 void editingStarted();
58 void editingFinished();
59 void restoreFromShortcutMgrClicked();
63 void onEditingFinished();
66 virtual bool eventFilter(QObject*, QEvent*);
72 QLineEdit* myKeySequenceLineEdit;
73 QString myConfirmedKeySequenceString;
75 // Last valid key sequence string from myKeySequenceLineEdit.
76 QString myPrevKeySequenceString;
80 class QtxShortcutTree;
81 class QtxShortcutTreeItem;
82 class QtxShortcutTreeFolder;
83 class QtxShortcutTreeAction;
87 class QTX_EXPORT QtxEditKeySequenceDialog : public QDialog
92 QtxEditKeySequenceDialog(QtxShortcutTree* theParent);
93 QtxEditKeySequenceDialog(const QtxEditKeySequenceDialog&) = delete;
94 QtxEditKeySequenceDialog& operator=(const QtxEditKeySequenceDialog&) = delete;
95 virtual ~QtxEditKeySequenceDialog() = default;
97 void setModuleAndActionID(const QString& theModuleID, const QString& theInModuleActionID);
98 const QString& moduleID() const;
99 const QString& inModuleActionID() const;
101 void setModuleAndActionName(const QString& theModuleName, const QString& theActionName, const QString& theActionToolTip = "");
103 void setConfirmedKeySequence(const QKeySequence& theSequence);
104 QKeySequence editedKeySequence() const;
106 void updateConflictsMessage();
111 void onEditingStarted();
112 void onEditingFinished();
113 void onRestoreFromShortcutMgr();
118 QString myInModuleActionID;
119 QLabel* myActionName;
120 QtxKeySequenceEdit* myKeySequenceEdit;
121 QTextEdit* myTextEdit;
125 class QTX_EXPORT QtxShortcutTree : public QTreeWidget
132 KeySequence = 1, // Empty, if item is folder item.
142 enum class SortOrder {
148 std::shared_ptr<SUIT_ShortcutContainer> theContainer = std::shared_ptr<SUIT_ShortcutContainer>(),
149 QWidget* theParent = nullptr
151 QtxShortcutTree(const QtxShortcutTree&) = delete;
152 QtxShortcutTree& operator=(const QtxShortcutTree&) = delete;
153 virtual ~QtxShortcutTree();
155 void setShortcutsFromManager();
156 void setDefaultShortcuts();
157 void applyChangesToShortcutMgr();
159 std::shared_ptr<const SUIT_ShortcutContainer> shortcutContainer() const;
161 void sort(QtxShortcutTree::SortKey theKey, QtxShortcutTree::SortOrder theOrder);
164 void updateItems(bool theHighlightModified, bool theUpdateSyncTrees);
165 std::pair<QtxShortcutTreeFolder*, int> findModuleFolderItem(const QString& theModuleID) const;
167 std::set<QtxShortcutTreeItem*, std::function<bool(QtxShortcutTreeItem*, QtxShortcutTreeItem*)>> getSortedChildren(QtxShortcutTreeFolder* theParentItem);
170 QtxShortcutTreeFolder* theParentItem,
171 std::set<QtxShortcutTreeItem*, std::function<bool(QtxShortcutTreeItem*, QtxShortcutTreeItem*)>>& theSortedChildren,
172 QtxShortcutTreeItem* theChildItem
176 void onItemDoubleClicked(QTreeWidgetItem* theWidgetItem, int theColIdx);
179 /** Keeps IDs of modules, which will are shown on setShortcutsFromManager(). */
180 std::set<QString> myModuleIDs;
182 static const QList<std::pair<QtxShortcutTree::SortKey, QtxShortcutTree::SortOrder>> DEFAULT_SORT_SCHEMA;
185 /** Allows to modify plenty of shortcuts and then apply them to SUIT_ShortcutMgr as a batch. */
186 const std::shared_ptr<SUIT_ShortcutContainer> myShortcutContainer;
188 QtxEditKeySequenceDialog* myEditDialog;
190 QtxShortcutTree::SortKey mySortKey;
191 QtxShortcutTree::SortOrder mySortOrder;
194 * Ensures that, if several QtxShortcutTree instances coexist,
195 * all of them are updated when one of them applies pending changes to SUIT_ShortcutMgr.
197 * Sharing of SUIT_ShortcutContainer allows to keep some trees synchronized even without
198 * applying changes to SUIT_ShortcutMgr. Why? See QtxPagePrefShortcutTreeItem.
200 * Access is not synchronized in assumption, that all instances live in the same thread.
202 static std::map<SUIT_ShortcutContainer*, std::set<QtxShortcutTree*>> instances;
206 class QtxShortcutTreeItem : public QTreeWidgetItem
215 QtxShortcutTreeItem(const QString& theModuleID);
218 virtual ~QtxShortcutTreeItem() = default;
219 virtual QtxShortcutTreeItem::Type type() const = 0;
221 virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang) = 0;
222 QString name() const;
224 virtual QString getValue(QtxShortcutTree::SortKey theKey) const = 0;
227 const QString myModuleID;
231 class QtxShortcutTreeFolder : public QtxShortcutTreeItem
234 QtxShortcutTreeFolder(const QString& theModuleID);
235 virtual ~QtxShortcutTreeFolder() = default;
236 virtual QtxShortcutTreeItem::Type type() const { return QtxShortcutTreeItem::Type::Folder; };
238 virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang);
240 virtual QString getValue(QtxShortcutTree::SortKey theKey) const;
244 class QtxShortcutTreeAction : public QtxShortcutTreeItem
247 QtxShortcutTreeAction(const QString& theModuleID, const QString& theInModuleActionID);
250 static QtxShortcutTreeAction* create(const QString& theModuleID, const QString& theInModuleActionID);
251 virtual ~QtxShortcutTreeAction() = default;
252 virtual QtxShortcutTreeItem::Type type() const { return QtxShortcutTreeItem::Type::Action; };
254 virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang);
256 virtual QString getValue(QtxShortcutTree::SortKey theKey) const;
258 void setKeySequence(const QString& theKeySequence);
259 QString keySequence() const;
260 void highlightKeySequenceAsModified(bool theHighlight);
262 const QString myInModuleActionID;
265 #endif // QTXSHORTCUTTREE_H