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