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