Salome HOME
updated copyright message
[modules/shaper.git] / src / ModuleBase / ModuleBase_ListView.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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::selectIndices(const std::set<int>& theIndices)
82 {
83   myListControl->clearSelection();
84   for (int i = 0; i < myListControl->count(); i++) {
85     QListWidgetItem* anItem = myListControl->item(i);
86     int aId = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
87     if (theIndices.find(aId) != theIndices.end()) {
88       anItem->setSelected(true);
89     }
90   }
91 }
92
93 //********************************************************************
94 void ModuleBase_ListView::removeSelectedItems()
95 {
96   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
97   foreach(QListWidgetItem* anItem, aItems)
98     myListControl->takeItem(myListControl->row(anItem));
99 }
100
101 //********************************************************************
102 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
103 {
104   QList<QListWidgetItem*> aItems;
105   for (int i = 0; i < myListControl->count(); i++) {
106     QListWidgetItem* anItem = myListControl->item(i);
107     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
108     if (theIndices.find(anIndex) != theIndices.end())
109       aItems.append(anItem);
110   }
111   foreach(QListWidgetItem* anItem, aItems)
112     myListControl->takeItem(myListControl->row(anItem));
113 }
114
115 //********************************************************************
116 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
117 {
118   int aRows = myListControl->model()->rowCount();
119   if (aRows > 0) {
120     foreach(QModelIndex aIndex, theIndices) {
121       if (aIndex.row() < aRows)
122         myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
123       else {
124         QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
125         myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
126       }
127     }
128   }
129 }
130
131 //********************************************************************
132 void ModuleBase_ListView::onCopyItem()
133 {
134   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
135   QString aRes;
136   foreach(QListWidgetItem* aItem, aItems) {
137     if (!aRes.isEmpty())
138       aRes += "\n";
139     aRes += aItem->text();
140   }
141   if (!aRes.isEmpty()) {
142     QClipboard* aClipboard = QApplication::clipboard();
143     aClipboard->setText(aRes);
144   }
145 }
146
147 //********************************************************************
148 void ModuleBase_ListView::onListSelection()
149 {
150   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
151   myCopyAction->setEnabled(!aItems.isEmpty());
152   myDeleteAction->setEnabled(!aItems.isEmpty());
153 }
154
155 //********************************************************************
156 bool ModuleBase_ListView::hasItem(const QString& theTextValue) const
157 {
158   return myListControl->findItems(theTextValue, Qt::MatchExactly).length() > 0;
159 }