Salome HOME
#2309 Possibility to hide faces
[modules/shaper.git] / src / ModuleBase / ModuleBase_ListView.cpp
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 #include "ModuleBase_ListView.h"
22 #include "ModuleBase_Tools.h"
23
24 #include <QAction>
25 #include <QApplication>
26 #include <QClipboard>
27 #include <QListWidget>
28 #include <QWidget>
29
30 #ifndef WIN32
31 #include <QResizeEvent>
32 #include <QTimer>
33 #endif
34
35 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
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 public:
43   /// Constructor
44   /// \param theParent a parent widget
45   CustomListWidget(QWidget* theParent)
46     : QListWidget(theParent)
47   {
48   }
49
50   /// Redefinition of virtual method
51   virtual QSize sizeHint() const
52   {
53     int aHeight = 2*QFontMetrics(font()).height();
54     QSize aSize = QListWidget::sizeHint();
55     return QSize(aSize.width(), aHeight);
56   }
57
58   /// Redefinition of virtual method
59   virtual QSize minimumSizeHint() const
60   {
61     int aHeight = 4/*2*/*QFontMetrics(font()).height();
62     QSize aSize = QListWidget::minimumSizeHint();
63     return QSize(aSize.width(), aHeight);
64   }
65
66 #ifndef WIN32
67 // The code is necessary only for Linux because
68 //it can not update viewport on widget resize
69 protected:
70   void resizeEvent(QResizeEvent* theEvent)
71   {
72     QListWidget::resizeEvent(theEvent);
73     QTimer::singleShot(5, viewport(), SLOT(repaint()));
74   }
75 #endif
76 };
77
78 //********************************************************************
79 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
80   const QString& theToolTip)
81 {
82   myListControl = new CustomListWidget(theParent);
83
84   myListControl->setObjectName(theObjectName);
85   myListControl->setToolTip(theToolTip);
86   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
87
88   myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
89                           theParent, this, SLOT(onCopyItem()));
90   myCopyAction->setShortcut(QKeySequence::Copy);
91   myCopyAction->setEnabled(false);
92   myListControl->addAction(myCopyAction);
93
94   myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
95                           theParent, this, SIGNAL(deleteActionClicked()));
96   myDeleteAction->setEnabled(false);
97   myListControl->addAction(myDeleteAction);
98
99   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
100   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
101 }
102
103 //********************************************************************
104 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
105 {
106   QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
107   anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
108   myListControl->addItem(anItem);
109 }
110
111 //********************************************************************
112 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
113 {
114   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
115   foreach(QListWidgetItem* anItem, aItems) {
116     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
117     if (theIndices.find(anIndex) == theIndices.end())
118       theIndices.insert(anIndex);
119   }
120 }
121
122 //********************************************************************
123 void ModuleBase_ListView::removeSelectedItems()
124 {
125   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
126   foreach(QListWidgetItem* anItem, aItems)
127     myListControl->takeItem(myListControl->row(anItem));
128 }
129
130 //********************************************************************
131 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
132 {
133   QList<QListWidgetItem*> aItems;
134   for (int i = 0; i < myListControl->count(); i++) {
135     QListWidgetItem* anItem = myListControl->item(i);
136     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
137     if (theIndices.find(anIndex) != theIndices.end())
138       aItems.append(anItem);
139   }
140   foreach(QListWidgetItem* anItem, aItems)
141     myListControl->takeItem(myListControl->row(anItem));
142 }
143
144 //********************************************************************
145 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
146 {
147   int aRows = myListControl->model()->rowCount();
148   if (aRows > 0) {
149     foreach(QModelIndex aIndex, theIndices) {
150       if (aIndex.row() < aRows)
151         myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
152       else {
153         QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
154         myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
155       }
156     }
157   }
158 }
159
160 //********************************************************************
161 void ModuleBase_ListView::onCopyItem()
162 {
163   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
164   QString aRes;
165   foreach(QListWidgetItem* aItem, aItems) {
166     if (!aRes.isEmpty())
167       aRes += "\n";
168     aRes += aItem->text();
169   }
170   if (!aRes.isEmpty()) {
171     QClipboard* aClipboard = QApplication::clipboard();
172     aClipboard->setText(aRes);
173   }
174 }
175
176 //********************************************************************
177 void ModuleBase_ListView::onListSelection()
178 {
179   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
180   myCopyAction->setEnabled(!aItems.isEmpty());
181   myDeleteAction->setEnabled(!aItems.isEmpty());
182 }