1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_ListView.h"
21 #include "ModuleBase_Tools.h"
24 #include <QApplication>
29 #include <QResizeEvent>
33 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
35 //********************************************************************
36 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
37 const QString& theToolTip)
39 myListControl = new CustomListWidget(theParent);
41 myListControl->setObjectName(theObjectName);
42 myListControl->setToolTip(theToolTip);
43 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
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);
51 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
52 theParent, this, SIGNAL(deleteActionClicked()));
53 myDeleteAction->setEnabled(false);
54 myListControl->addAction(myDeleteAction);
56 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
57 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
58 connect(myListControl, SIGNAL(activated()), this, SIGNAL(listActivated()));
61 //********************************************************************
62 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
64 QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
65 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
66 myListControl->addItem(anItem);
69 //********************************************************************
70 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
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);
80 //********************************************************************
81 void ModuleBase_ListView::selectIndices(const std::set<int>& theIndices)
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);
93 //********************************************************************
94 void ModuleBase_ListView::removeSelectedItems()
96 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
97 foreach(QListWidgetItem* anItem, aItems)
98 myListControl->takeItem(myListControl->row(anItem));
101 //********************************************************************
102 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
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);
111 foreach(QListWidgetItem* anItem, aItems)
112 myListControl->takeItem(myListControl->row(anItem));
115 //********************************************************************
116 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
118 int aRows = myListControl->model()->rowCount();
120 foreach(QModelIndex aIndex, theIndices) {
121 if (aIndex.row() < aRows)
122 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
124 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
125 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
131 //********************************************************************
132 void ModuleBase_ListView::onCopyItem()
134 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
136 foreach(QListWidgetItem* aItem, aItems) {
139 aRes += aItem->text();
141 if (!aRes.isEmpty()) {
142 QClipboard* aClipboard = QApplication::clipboard();
143 aClipboard->setText(aRes);
147 //********************************************************************
148 void ModuleBase_ListView::onListSelection()
150 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
151 myCopyAction->setEnabled(!aItems.isEmpty());
152 myDeleteAction->setEnabled(!aItems.isEmpty());
155 //********************************************************************
156 bool ModuleBase_ListView::hasItem(const QString& theTextValue) const
158 return myListControl->findItems(theTextValue, Qt::MatchExactly).length() > 0;