Salome HOME
Issue #2948: Synchronize selection for filters controls
[modules/shaper.git] / src / ModuleBase / ModuleBase_ListView.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_ListView.h"
21 #include "ModuleBase_Tools.h"
22
23 #include <QAction>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QWidget>
27
28 #ifndef WIN32
29 #include <QResizeEvent>
30 #include <QTimer>
31 #endif
32
33 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
34
35 //********************************************************************
36 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
37   const QString& theToolTip)
38 {
39   myListControl = new CustomListWidget(theParent);
40
41   myListControl->setObjectName(theObjectName);
42   myListControl->setToolTip(theToolTip);
43   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
44
45   myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
46                           theParent, this, SLOT(onCopyItem()));
47   myCopyAction->setShortcut(QKeySequence::Copy);
48   myCopyAction->setEnabled(false);
49   myListControl->addAction(myCopyAction);
50
51   myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
52                           theParent, this, SIGNAL(deleteActionClicked()));
53   myDeleteAction->setEnabled(false);
54   myListControl->addAction(myDeleteAction);
55
56   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
57   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
58   connect(myListControl, SIGNAL(activated()), this, SIGNAL(listActivated()));
59 }
60
61 //********************************************************************
62 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
63 {
64   QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
65   anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
66   myListControl->addItem(anItem);
67 }
68
69 //********************************************************************
70 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
71 {
72   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
73   foreach(QListWidgetItem* anItem, aItems) {
74     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
75     if (theIndices.find(anIndex) == theIndices.end())
76       theIndices.insert(anIndex);
77   }
78 }
79
80 //********************************************************************
81 void ModuleBase_ListView::removeSelectedItems()
82 {
83   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
84   foreach(QListWidgetItem* anItem, aItems)
85     myListControl->takeItem(myListControl->row(anItem));
86 }
87
88 //********************************************************************
89 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
90 {
91   QList<QListWidgetItem*> aItems;
92   for (int i = 0; i < myListControl->count(); i++) {
93     QListWidgetItem* anItem = myListControl->item(i);
94     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
95     if (theIndices.find(anIndex) != theIndices.end())
96       aItems.append(anItem);
97   }
98   foreach(QListWidgetItem* anItem, aItems)
99     myListControl->takeItem(myListControl->row(anItem));
100 }
101
102 //********************************************************************
103 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
104 {
105   int aRows = myListControl->model()->rowCount();
106   if (aRows > 0) {
107     foreach(QModelIndex aIndex, theIndices) {
108       if (aIndex.row() < aRows)
109         myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
110       else {
111         QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
112         myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
113       }
114     }
115   }
116 }
117
118 //********************************************************************
119 void ModuleBase_ListView::onCopyItem()
120 {
121   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
122   QString aRes;
123   foreach(QListWidgetItem* aItem, aItems) {
124     if (!aRes.isEmpty())
125       aRes += "\n";
126     aRes += aItem->text();
127   }
128   if (!aRes.isEmpty()) {
129     QClipboard* aClipboard = QApplication::clipboard();
130     aClipboard->setText(aRes);
131   }
132 }
133
134 //********************************************************************
135 void ModuleBase_ListView::onListSelection()
136 {
137   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
138   myCopyAction->setEnabled(!aItems.isEmpty());
139   myDeleteAction->setEnabled(!aItems.isEmpty());
140 }
141
142 //********************************************************************
143 bool ModuleBase_ListView::hasItem(const QString& theTextValue) const
144 {
145   return myListControl->findItems(theTextValue, Qt::MatchExactly).length() > 0;
146 }