Salome HOME
updated copyright message
[modules/shaper.git] / src / ModuleBase / ModuleBase_ListView.h
1 // Copyright (C) 2014-2023  CEA, EDF
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 ModuleBase_ListView_H_
21 #define ModuleBase_ListView_H_
22
23 #include "ModuleBase.h"
24
25 #include <QModelIndex>
26 #include <QObject>
27 #include <QListWidget>
28 #include <QTimer>
29
30 #include <set>
31
32 class QAction;
33 class QListWidget;
34 class QWidget;
35
36
37 /**
38 * Customization of a List Widget to make it to be placed on full width of container
39 */
40 class CustomListWidget : public QListWidget
41 {
42   Q_OBJECT
43 public:
44   /// Constructor
45   /// \param theParent a parent widget
46   CustomListWidget(QWidget* theParent)
47     : QListWidget(theParent)
48   {
49   }
50
51   /// Redefinition of virtual method
52   virtual QSize sizeHint() const
53   {
54     int aHeight = 2 * QFontMetrics(font()).height();
55     QSize aSize = QListWidget::sizeHint();
56     return QSize(aSize.width(), aHeight);
57   }
58
59   /// Redefinition of virtual method
60   virtual QSize minimumSizeHint() const
61   {
62     int aHeight = 4/*2*/ * QFontMetrics(font()).height();
63     QSize aSize = QListWidget::minimumSizeHint();
64     return QSize(aSize.width(), aHeight);
65   }
66
67 signals:
68   void activated();
69
70 protected:
71   virtual void mouseReleaseEvent(QMouseEvent* e) {
72     QListWidget::mouseReleaseEvent(e);
73     emit activated();
74   }
75
76 #ifndef WIN32
77   // The code is necessary only for Linux because
78   //it can not update viewport on widget resize
79 protected:
80   void resizeEvent(QResizeEvent* theEvent)
81   {
82     QListWidget::resizeEvent(theEvent);
83     QTimer::singleShot(5, viewport(), SLOT(repaint()));
84   }
85 #endif
86 };
87
88
89 /**
90 * \ingroup GUI
91 * An extension of QListWidget to provide Undo/Redo functionality
92 */
93 class MODULEBASE_EXPORT ModuleBase_ListView : public QObject
94 {
95 Q_OBJECT
96
97 public:
98   /// Constructor
99   ModuleBase_ListView(QWidget* theParent = 0, const QString& theObjectName = QString(),
100     const QString& theToolTip = QString());
101   /// Destructor
102   virtual ~ModuleBase_ListView() {}
103
104   /// Returns current control
105   /// \return list view instance
106   QListWidget* getControl() const { return myListControl; }
107
108   /// Adds a new list widget item to the end of the list and connect it to the given index
109   /// \param theTextValue value visualized in the view
110   /// \param theIndex an item internal index
111   void addItem(const QString& theTextValue, const int theIndex);
112
113   /// Returns True if the control already contains an item with the given text
114   /// \param theTextValue tex of item to find
115   bool hasItem(const QString& theTextValue) const;
116
117   /// Returns list of internal list view item indices
118   /// \param theIndices an output container for indices
119   void getSelectedIndices(std::set<int>& theIndices);
120
121   /// Selects items with indices
122   /// \param theIndices indices
123   void selectIndices(const std::set<int>& theIndices);
124
125   /// Removes selected items from the list widget
126   void removeSelectedItems();
127
128   /// Remove items contain parameter indices
129   /// \param theIndices an indices
130   void removeItems(std::set<int>& theIndices);
131
132   /// Set selected items if possible
133   /// \param theIndices container of indices to be selected
134   void restoreSelection(const QModelIndexList& theIndices);
135
136   /// Update enable/disable state of context menu actions
137   void updateActionsStatus();
138
139 protected slots:
140   /// Slot for copy command in a list pop-up menu
141   void onCopyItem();
142
143   /// Slot is called on selection of list of selected items
144   void onListSelection();
145
146
147 signals:
148   /// Signal about delete action click
149   void deleteActionClicked();
150
151   void listActivated();
152
153 protected:
154   QListWidget* myListControl; ///< List control
155
156   QAction* myCopyAction; ///< A copy action for pop-up menu in a list control
157   QAction* myDeleteAction; ///< A delete action for pop-up menu in a list control
158 };
159
160 #endif