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