Salome HOME
862c80d8610ad936e80ee3670cc5201fc065822c
[modules/gui.git] / src / Qtx / QtxShortcutEdit.h
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef QTXSHORTCUTTREE_H
21 #define QTXSHORTCUTTREE_H
22
23 #include "Qtx.h"
24 #include <QDialog>
25 #include <QFrame>
26 #include <QTreeWidget>
27 #include "SUIT_ShortcutMgr.h"
28 #include <memory>
29 #include <map>
30 #include <set>
31 #include <functional>
32
33
34 class QLineEdit;
35 class QLabel;
36 class QPushButton;
37 class QTreeWidgetItem;
38
39 class QTX_EXPORT QtxKeySequenceEdit : public QFrame
40 {
41   Q_OBJECT
42
43 public:
44   QtxKeySequenceEdit(QWidget* = nullptr);
45   virtual ~QtxKeySequenceEdit() = default;
46
47   void           setConfirmedKeySequence(const QKeySequence&);
48   void           setEditedKeySequence(const QKeySequence&);
49   QKeySequence   editedKeySequence() const;
50   bool           isKeySequenceModified() const;
51   void           restoreKeySequence();
52
53   static QString parseEvent(QKeyEvent*);
54   static bool    isValidKey(int);
55
56 signals:
57   void           editingStarted();
58   void           editingFinished();
59   void           restoreFromShortcutMgrClicked();
60
61 private slots:
62   void           onClear();
63   void           onEditingFinished();
64
65 protected:
66   virtual bool   eventFilter(QObject*, QEvent*);
67
68 private:
69   void           initialize();
70
71 private:
72   QLineEdit*     myKeySequenceLineEdit;
73   QString        myConfirmedKeySequenceString;
74
75   // Last valid key sequence string from myKeySequenceLineEdit.
76   QString        myPrevKeySequenceString;
77 };
78
79
80 class QtxShortcutTree;
81 class QtxShortcutTreeItem;
82 class QtxShortcutTreeFolder;
83 class QtxShortcutTreeAction;
84 class QTextEdit;
85
86
87 class QTX_EXPORT QtxEditKeySequenceDialog : public QDialog
88 {
89   Q_OBJECT
90
91 public:
92   QtxEditKeySequenceDialog(QtxShortcutTree* theParent);
93   QtxEditKeySequenceDialog(const QtxEditKeySequenceDialog&) = delete;
94   QtxEditKeySequenceDialog& operator=(const QtxEditKeySequenceDialog&) = delete;
95   virtual ~QtxEditKeySequenceDialog() = default;
96
97   void setModuleAndActionID(const QString& theModuleID, const QString& theInModuleActionID);
98   const QString& moduleID() const;
99   const QString& inModuleActionID() const;
100
101   void setModuleAndActionName(const QString& theModuleName, const QString& theActionName, const QString& theActionToolTip = "");
102
103   void setConfirmedKeySequence(const QKeySequence& theSequence);
104   QKeySequence editedKeySequence() const;
105
106   void updateConflictsMessage();
107
108   int exec();
109
110 private slots:
111   void onEditingStarted();
112   void onEditingFinished();
113   void onRestoreFromShortcutMgr();
114   void onConfirm();
115
116 private:
117   QString myModuleID;
118   QString myInModuleActionID;
119   QLabel* myActionName;
120   QtxKeySequenceEdit* myKeySequenceEdit;
121   QTextEdit* myTextEdit;
122 };
123
124
125 class QTX_EXPORT QtxShortcutTree : public QTreeWidget
126 {
127   Q_OBJECT
128
129 public:
130   enum ElementIdx {
131     Name = 0,
132     KeySequence = 1, // Empty, if item is folder item.
133   };
134
135   enum class SortKey {
136     ID,
137     Name,
138     ToolTip,
139     KeySequence,
140   };
141
142   enum class SortOrder {
143     Ascending,
144     Descending
145   };
146
147   QtxShortcutTree(
148     std::shared_ptr<SUIT_ShortcutContainer> theContainer = std::shared_ptr<SUIT_ShortcutContainer>(),
149     QWidget* theParent = nullptr
150   );
151   QtxShortcutTree(const QtxShortcutTree&) = delete;
152   QtxShortcutTree& operator=(const QtxShortcutTree&) = delete;
153   virtual ~QtxShortcutTree();
154
155   void setShortcutsFromManager();
156   void setDefaultShortcuts();
157   void applyChangesToShortcutMgr();
158
159   std::shared_ptr<const SUIT_ShortcutContainer> shortcutContainer() const;
160
161   void sort(QtxShortcutTree::SortKey theKey, QtxShortcutTree::SortOrder theOrder);
162
163 private:
164   void updateItems(bool theHighlightModified, bool theUpdateSyncTrees);
165   std::pair<QtxShortcutTreeFolder*, int> findModuleFolderItem(const QString& theModuleID) const;
166
167   std::set<QtxShortcutTreeItem*, std::function<bool(QtxShortcutTreeItem*, QtxShortcutTreeItem*)>> getSortedChildren(QtxShortcutTreeFolder* theParentItem);
168
169   void insertChild(
170     QtxShortcutTreeFolder* theParentItem,
171     std::set<QtxShortcutTreeItem*, std::function<bool(QtxShortcutTreeItem*, QtxShortcutTreeItem*)>>& theSortedChildren,
172     QtxShortcutTreeItem* theChildItem
173   );
174
175 private slots:
176   void onItemDoubleClicked(QTreeWidgetItem* theWidgetItem, int theColIdx);
177
178 public:
179   /** Keeps IDs of modules, which will are shown on setShortcutsFromManager(). */
180   std::set<QString> myModuleIDs;
181
182   static const QList<std::pair<QtxShortcutTree::SortKey, QtxShortcutTree::SortOrder>> DEFAULT_SORT_SCHEMA;
183
184 private:
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;
187
188   QtxEditKeySequenceDialog* myEditDialog;
189
190   QtxShortcutTree::SortKey mySortKey;
191   QtxShortcutTree::SortOrder mySortOrder;
192
193   /**
194    * Ensures that, if several QtxShortcutTree instances coexist,
195    * all of them are updated when one of them applies pending changes to SUIT_ShortcutMgr.
196    *
197    * Sharing of SUIT_ShortcutContainer allows to keep some trees synchronized even without
198    * applying changes to SUIT_ShortcutMgr. Why? See QtxPagePrefShortcutTreeItem.
199    *
200    * Access is not synchronized in assumption, that all instances live in the same thread.
201   */
202   static std::map<SUIT_ShortcutContainer*, std::set<QtxShortcutTree*>> instances;
203 };
204
205
206 class QtxShortcutTreeItem : public QTreeWidgetItem
207 {
208 public:
209   enum Type {
210     Folder = 0,
211     Action = 1,
212   };
213
214 protected:
215   QtxShortcutTreeItem(const QString& theModuleID);
216
217 public:
218   virtual ~QtxShortcutTreeItem() = default;
219   virtual QtxShortcutTreeItem::Type type() const = 0;
220
221   virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang) = 0;
222   QString name() const;
223
224   virtual QString getValue(QtxShortcutTree::SortKey theKey) const = 0;
225
226 public:
227   const QString myModuleID;
228 };
229
230
231 class QtxShortcutTreeFolder : public QtxShortcutTreeItem
232 {
233 public:
234   QtxShortcutTreeFolder(const QString& theModuleID);
235   virtual ~QtxShortcutTreeFolder() = default;
236   virtual QtxShortcutTreeItem::Type type() const { return QtxShortcutTreeItem::Type::Folder; };
237
238   virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang);
239
240   virtual QString getValue(QtxShortcutTree::SortKey theKey) const;
241 };
242
243
244 class QtxShortcutTreeAction : public QtxShortcutTreeItem
245 {
246 private:
247   QtxShortcutTreeAction(const QString& theModuleID, const QString& theInModuleActionID);
248
249 public:
250   static QtxShortcutTreeAction* create(const QString& theModuleID, const QString& theInModuleActionID);
251   virtual ~QtxShortcutTreeAction() = default;
252   virtual QtxShortcutTreeItem::Type type() const { return QtxShortcutTreeItem::Type::Action; };
253
254   virtual void setAssets(std::shared_ptr<const SUIT_ActionAssets> theAssets, const QString& theLang);
255
256   virtual QString getValue(QtxShortcutTree::SortKey theKey) const;
257
258   void setKeySequence(const QString& theKeySequence);
259   QString keySequence() const;
260   void highlightKeySequenceAsModified(bool theHighlight);
261
262   const QString myInModuleActionID;
263 };
264
265 #endif // QTXSHORTCUTTREE_H