1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModuleBase_ListView.h"
22 #include "ModuleBase_Tools.h"
25 #include <QApplication>
30 #include <QResizeEvent>
34 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
36 //********************************************************************
37 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
38 const QString& theToolTip)
40 myListControl = new CustomListWidget(theParent);
42 myListControl->setObjectName(theObjectName);
43 myListControl->setToolTip(theToolTip);
44 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
46 myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
47 theParent, this, SLOT(onCopyItem()));
48 myCopyAction->setShortcut(QKeySequence::Copy);
49 myCopyAction->setEnabled(false);
50 myListControl->addAction(myCopyAction);
52 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
53 theParent, this, SIGNAL(deleteActionClicked()));
54 myDeleteAction->setEnabled(false);
55 myListControl->addAction(myDeleteAction);
57 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
58 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
59 connect(myListControl, SIGNAL(activated()), this, SIGNAL(listActivated()));
62 //********************************************************************
63 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
65 QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
66 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
67 myListControl->addItem(anItem);
70 //********************************************************************
71 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
73 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
74 foreach(QListWidgetItem* anItem, aItems) {
75 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
76 if (theIndices.find(anIndex) == theIndices.end())
77 theIndices.insert(anIndex);
81 //********************************************************************
82 void ModuleBase_ListView::removeSelectedItems()
84 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
85 foreach(QListWidgetItem* anItem, aItems)
86 myListControl->takeItem(myListControl->row(anItem));
89 //********************************************************************
90 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
92 QList<QListWidgetItem*> aItems;
93 for (int i = 0; i < myListControl->count(); i++) {
94 QListWidgetItem* anItem = myListControl->item(i);
95 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
96 if (theIndices.find(anIndex) != theIndices.end())
97 aItems.append(anItem);
99 foreach(QListWidgetItem* anItem, aItems)
100 myListControl->takeItem(myListControl->row(anItem));
103 //********************************************************************
104 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
106 int aRows = myListControl->model()->rowCount();
108 foreach(QModelIndex aIndex, theIndices) {
109 if (aIndex.row() < aRows)
110 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
112 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
113 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
119 //********************************************************************
120 void ModuleBase_ListView::onCopyItem()
122 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
124 foreach(QListWidgetItem* aItem, aItems) {
127 aRes += aItem->text();
129 if (!aRes.isEmpty()) {
130 QClipboard* aClipboard = QApplication::clipboard();
131 aClipboard->setText(aRes);
135 //********************************************************************
136 void ModuleBase_ListView::onListSelection()
138 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
139 myCopyAction->setEnabled(!aItems.isEmpty());
140 myDeleteAction->setEnabled(!aItems.isEmpty());
143 //********************************************************************
144 bool ModuleBase_ListView::hasItem(const QString& theTextValue) const
146 return myListControl->findItems(theTextValue, Qt::MatchExactly).length() > 0;