Salome HOME
Issue #2513: Provide selection for selector in undocked window
[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 <QWidget>
28
29 #ifndef WIN32
30 #include <QResizeEvent>
31 #include <QTimer>
32 #endif
33
34 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
35
36 //********************************************************************
37 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
38   const QString& theToolTip)
39 {
40   myListControl = new CustomListWidget(theParent);
41
42   myListControl->setObjectName(theObjectName);
43   myListControl->setToolTip(theToolTip);
44   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
45
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);
51
52   myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
53                           theParent, this, SIGNAL(deleteActionClicked()));
54   myDeleteAction->setEnabled(false);
55   myListControl->addAction(myDeleteAction);
56
57   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
58   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
59   connect(myListControl, SIGNAL(activated()), this, SIGNAL(listActivated()));
60 }
61
62 //********************************************************************
63 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
64 {
65   QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
66   anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
67   myListControl->addItem(anItem);
68 }
69
70 //********************************************************************
71 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
72 {
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);
78   }
79 }
80
81 //********************************************************************
82 void ModuleBase_ListView::removeSelectedItems()
83 {
84   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
85   foreach(QListWidgetItem* anItem, aItems)
86     myListControl->takeItem(myListControl->row(anItem));
87 }
88
89 //********************************************************************
90 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
91 {
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);
98   }
99   foreach(QListWidgetItem* anItem, aItems)
100     myListControl->takeItem(myListControl->row(anItem));
101 }
102
103 //********************************************************************
104 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
105 {
106   int aRows = myListControl->model()->rowCount();
107   if (aRows > 0) {
108     foreach(QModelIndex aIndex, theIndices) {
109       if (aIndex.row() < aRows)
110         myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
111       else {
112         QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
113         myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
114       }
115     }
116   }
117 }
118
119 //********************************************************************
120 void ModuleBase_ListView::onCopyItem()
121 {
122   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
123   QString aRes;
124   foreach(QListWidgetItem* aItem, aItems) {
125     if (!aRes.isEmpty())
126       aRes += "\n";
127     aRes += aItem->text();
128   }
129   if (!aRes.isEmpty()) {
130     QClipboard* aClipboard = QApplication::clipboard();
131     aClipboard->setText(aRes);
132   }
133 }
134
135 //********************************************************************
136 void ModuleBase_ListView::onListSelection()
137 {
138   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
139   myCopyAction->setEnabled(!aItems.isEmpty());
140   myDeleteAction->setEnabled(!aItems.isEmpty());
141 }
142
143 //********************************************************************
144 bool ModuleBase_ListView::hasItem(const QString& theTextValue) const
145 {
146   return myListControl->findItems(theTextValue, Qt::MatchExactly).length() > 0;
147 }