Salome HOME
065959ea11027fde1e860ef340ce849913e5218d
[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
32 class QLineEdit;
33 class QLabel;
34 class QPushButton;
35 class QTreeWidgetItem;
36
37 class QTX_EXPORT QtxKeySequenceEdit : public QFrame
38 {
39   Q_OBJECT
40
41 public:
42   QtxKeySequenceEdit(QWidget* = nullptr);
43   virtual ~QtxKeySequenceEdit() = default;
44
45   void           setConfirmedKeySequence(const QKeySequence&);
46   void           setEditedKeySequence(const QKeySequence&);
47   QKeySequence   editedKeySequence() const;
48   bool           isKeySequenceModified() const;
49   void           restoreKeySequence();
50
51   static QString parseEvent(QKeyEvent*);
52   static bool    isValidKey(int);
53
54 signals:
55   void           editingStarted();
56   void           editingFinished();
57   void           restoreFromShortcutMgrClicked();
58
59 private slots:
60   void           onClear();
61   void           onEditingFinished();
62
63 protected:
64   virtual bool   eventFilter(QObject*, QEvent*);
65
66 private:
67   void           initialize();
68
69 private:
70   QLineEdit*     myKeySequenceLineEdit;
71   QString        myConfirmedKeySequenceString;
72
73   // Last valid key sequence string from myKeySequenceLineEdit.
74   QString        myPrevKeySequenceString;
75 };
76
77
78 class QtxShortcutTree;
79 class QtxShortcutTreeItem;
80 class QTextEdit;
81
82
83 class QTX_EXPORT QtxEditKeySequenceDialog : public QDialog
84 {
85   Q_OBJECT
86
87 public:
88   QtxEditKeySequenceDialog(QtxShortcutTree* theParent);
89   QtxEditKeySequenceDialog(const QtxEditKeySequenceDialog&) = delete;
90   QtxEditKeySequenceDialog& operator=(const QtxEditKeySequenceDialog&) = delete;
91   virtual ~QtxEditKeySequenceDialog() = default;
92
93   void setModuleAndActionID(const QString& theModuleID, const QString& theInModuleActionID);
94   const QString& moduleID() const;
95   const QString& inModuleActionID() const;
96
97   void setModuleAndActionName(const QString& theModuleName, const QString& theActionName);
98
99   void setConfirmedKeySequence(const QKeySequence& theSequence);
100   QKeySequence editedKeySequence() const;
101
102   void updateConflictsMessage();
103
104   int exec();
105
106 private slots:
107   void onEditingStarted();
108   void onEditingFinished();
109   void onRestoreFromShortcutMgr();
110   void onConfirm();
111
112 private:
113   QString myModuleID;
114   QString myInModuleActionID;
115   QLabel* myActionName;
116   QtxKeySequenceEdit* myKeySequenceEdit;
117   QTextEdit* myTextEdit;
118 };
119
120
121 class QTX_EXPORT QtxShortcutTree : public QTreeWidget
122 {
123   Q_OBJECT
124
125 public:
126   QtxShortcutTree(
127     std::shared_ptr<SUIT_ShortcutContainer> theContainer = std::shared_ptr<SUIT_ShortcutContainer>(),
128     QWidget* theParent = nullptr
129   );
130   QtxShortcutTree(const QtxShortcutTree&) = delete;
131   QtxShortcutTree& operator=(const QtxShortcutTree&) = delete;
132   virtual ~QtxShortcutTree();
133
134   void setShortcutsFromManager();
135   void setDefaultShortcuts();
136   void applyChangesToShortcutMgr();
137
138   std::shared_ptr<const SUIT_ShortcutContainer> shortcutContainer() const;
139
140 private:
141   void updateItems(bool theHighlightModified, bool theUpdateSyncTrees);
142   std::pair<QtxShortcutTreeItem*, int> findModuleFolderItem(const QString& theModuleID) const;
143
144 private slots:
145   void onItemDoubleClicked(QTreeWidgetItem* theWidgetItem, int theColIdx);
146
147 public:
148   /** Keeps IDs of modules, which will be shown on setShortcutsFromManager(). */
149   std::set<QString> myModuleIDs;
150
151 private:
152   /** Allows to modify plenty of shortcuts and then apply them to SUIT_ShortcutMgr as a batch. */
153   const std::shared_ptr<SUIT_ShortcutContainer> myShortcutContainer;
154
155   QtxEditKeySequenceDialog* myEditDialog;
156
157   /**
158    * Ensures that, if several QtxShortcutTree instances coexist,
159    * all of them are updated when one of them applies pending changes to SUIT_ShortcutMgr.
160    *
161    * Sharing of SUIT_ShortcutContainer allows to keep some trees synchronized even without
162    * applying changes to SUIT_ShortcutMgr. Why? See QtxPagePrefShortcutTreeItem.
163    *
164    * Access is not synchronized in assumption, that all instances live in the same thread.
165   */
166   static std::map<SUIT_ShortcutContainer*, std::set<QtxShortcutTree*>> instances;
167 };
168
169
170 class QtxShortcutTreeItem : public QTreeWidgetItem
171 {
172 private:
173   QtxShortcutTreeItem(const QString& theModuleID, const QString& theInModuleActionID);
174
175   enum ElementIdx {
176     Name = 0,
177     KeySequence = 1, // Empty, if item is used as folder.
178   };
179
180 public:
181   static QtxShortcutTreeItem* createFolderItem(const QString& theModuleID);
182   static QtxShortcutTreeItem* createShortcutItem(const QString& theModuleID, const QString& theInModuleActionID);
183   virtual ~QtxShortcutTreeItem() = default;
184
185   bool isFolder() const;
186   void highlightKeySequenceAsModified(bool theHighlight);
187
188   void setName(const QString& theName);
189   QString name() const;
190
191   void setKeySequence(const QString& theKeySequence);
192   QString keySequence() const;
193
194   const QString myModuleID;
195   const QString myInModuleActionID; // Empty, if item is used as folder.
196 };
197
198 #endif // QTXSHORTCUTTREE_H