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 SUIT_SHORTCUTTREE_H
21 #define SUIT_SHORTCUTTREE_H
26 #include <QTreeWidget>
27 #include "SUIT_ShortcutMgr.h"
37 class QTreeWidgetItem;
39 class SUIT_EXPORT SUIT_KeySequenceEdit : public QFrame
44 SUIT_KeySequenceEdit(QWidget* = nullptr);
45 virtual ~SUIT_KeySequenceEdit() = 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 SUIT_ShortcutTree;
81 class SUIT_ShortcutTreeItem;
82 class SUIT_ShortcutTreeFolder;
83 class SUIT_ShortcutTreeAction;
87 class SUIT_EXPORT SUIT_EditKeySequenceDialog : public QDialog
92 SUIT_EditKeySequenceDialog(SUIT_ShortcutTree* theParent);
93 SUIT_EditKeySequenceDialog(const SUIT_EditKeySequenceDialog&) = delete;
94 SUIT_EditKeySequenceDialog& operator=(const SUIT_EditKeySequenceDialog&) = delete;
95 virtual ~SUIT_EditKeySequenceDialog() = 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 SUIT_KeySequenceEdit* myKeySequenceEdit;
121 QTextEdit* myTextEdit;
125 class SUIT_EXPORT SUIT_ShortcutTree : 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 SUIT_ShortcutTree(const SUIT_ShortcutTree&) = delete;
152 SUIT_ShortcutTree& operator=(const SUIT_ShortcutTree&) = delete;
153 virtual ~SUIT_ShortcutTree();
155 void setShortcutsFromManager();
156 void setDefaultShortcuts();
157 void applyChangesToShortcutMgr();
159 std::shared_ptr<const SUIT_ShortcutContainer> shortcutContainer() const;
161 void sort(SUIT_ShortcutTree::SortKey theKey, SUIT_ShortcutTree::SortOrder theOrder);
164 void updateItems(bool theHighlightModified, bool theUpdateSyncTrees);
165 std::pair<SUIT_ShortcutTreeFolder*, int> findModuleFolderItem(const QString& theModuleID) const;
167 std::set<SUIT_ShortcutTreeItem*, std::function<bool(SUIT_ShortcutTreeItem*, SUIT_ShortcutTreeItem*)>> getSortedChildren(SUIT_ShortcutTreeFolder* theParentItem);
170 SUIT_ShortcutTreeFolder* theParentItem,
171 std::set<SUIT_ShortcutTreeItem*, std::function<bool(SUIT_ShortcutTreeItem*, SUIT_ShortcutTreeItem*)>>& theSortedChildren,
172 SUIT_ShortcutTreeItem* 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<SUIT_ShortcutTree::SortKey, SUIT_ShortcutTree::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 SUIT_EditKeySequenceDialog* myEditDialog;
190 SUIT_ShortcutTree::SortKey mySortKey;
191 SUIT_ShortcutTree::SortOrder mySortOrder;
194 * Ensures that, if several SUIT_ShortcutTree 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 SUIT_PagePrefShortcutTreeItem.
200 * Access is not synchronized in assumption, that all instances live in the same thread.
202 static std::map<SUIT_ShortcutContainer*, std::set<SUIT_ShortcutTree*>> instances;
206 class SUIT_ShortcutTreeItem : public QTreeWidgetItem
215 SUIT_ShortcutTreeItem(const QString& theModuleID);
218 virtual ~SUIT_ShortcutTreeItem() = default;
219 virtual SUIT_ShortcutTreeItem::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(SUIT_ShortcutTree::SortKey theKey) const = 0;
227 const QString myModuleID;
231 class SUIT_ShortcutTreeFolder : public SUIT_ShortcutTreeItem
234 SUIT_ShortcutTreeFolder(const QString& theModuleID);
235 virtual ~SUIT_ShortcutTreeFolder() = default;
236 virtual SUIT_ShortcutTreeItem::Type type() const { return SUIT_ShortcutTreeItem::Type::Folder; };
238 virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang);
240 virtual QString getValue(SUIT_ShortcutTree::SortKey theKey) const;
244 class SUIT_ShortcutTreeAction : public SUIT_ShortcutTreeItem
247 SUIT_ShortcutTreeAction(const QString& theModuleID, const QString& theInModuleActionID);
250 static SUIT_ShortcutTreeAction* create(const QString& theModuleID, const QString& theInModuleActionID);
251 virtual ~SUIT_ShortcutTreeAction() = default;
252 virtual SUIT_ShortcutTreeItem::Type type() const { return SUIT_ShortcutTreeItem::Type::Action; };
254 virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang);
256 virtual QString getValue(SUIT_ShortcutTree::SortKey theKey) const;
258 void setKeySequence(const QString& theKeySequence);
259 QString keySequence() const;
260 void highlightKeySequenceAsModified(bool theHighlight);
262 const QString myInModuleActionID;
265 #endif // SUIT_SHORTCUTTREE_H