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>
27 #include <QListWidget>
31 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
34 * Customization of a List Widget to make it to be placed on full width of container
36 class CustomListWidget : public QListWidget
40 /// \param theParent a parent widget
41 CustomListWidget(QWidget* theParent)
42 : QListWidget(theParent)
46 /// Redefinition of virtual method
47 virtual QSize sizeHint() const
49 int aHeight = 2*QFontMetrics(font()).height();
50 QSize aSize = QListWidget::sizeHint();
51 return QSize(aSize.width(), aHeight);
54 /// Redefinition of virtual method
55 virtual QSize minimumSizeHint() const
57 int aHeight = 4/*2*/*QFontMetrics(font()).height();
58 QSize aSize = QListWidget::minimumSizeHint();
59 return QSize(aSize.width(), aHeight);
63 // The code is necessary only for Linux because
64 //it can not update viewport on widget resize
66 void resizeEvent(QResizeEvent* theEvent)
68 QListWidget::resizeEvent(theEvent);
69 QTimer::singleShot(5, viewport(), SLOT(repaint()));
74 //********************************************************************
75 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
76 const QString& theToolTip)
78 myListControl = new CustomListWidget(theParent);
80 myListControl->setObjectName(theObjectName);
81 myListControl->setToolTip(theToolTip);
82 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
84 myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
85 theParent, this, SLOT(onCopyItem()));
86 myCopyAction->setShortcut(QKeySequence::Copy);
87 myCopyAction->setEnabled(false);
88 myListControl->addAction(myCopyAction);
90 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
91 theParent, this, SIGNAL(deleteActionClicked()));
92 myDeleteAction->setEnabled(false);
93 myListControl->addAction(myDeleteAction);
95 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
96 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
99 //********************************************************************
100 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
102 QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
103 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
104 myListControl->addItem(anItem);
107 //********************************************************************
108 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
110 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
111 foreach(QListWidgetItem* anItem, aItems) {
112 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
113 if (theIndices.find(anIndex) == theIndices.end())
114 theIndices.insert(anIndex);
118 //********************************************************************
119 void ModuleBase_ListView::removeSelectedItems()
121 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
122 foreach(QListWidgetItem* anItem, aItems)
123 myListControl->takeItem(myListControl->row(anItem));
126 //********************************************************************
127 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
129 QList<QListWidgetItem*> aItems;
130 for (int i = 0; i < myListControl->count(); i++) {
131 QListWidgetItem* anItem = myListControl->item(i);
132 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
133 if (theIndices.find(anIndex) != theIndices.end())
134 aItems.append(anItem);
136 foreach(QListWidgetItem* anItem, aItems)
137 myListControl->takeItem(myListControl->row(anItem));
140 //********************************************************************
141 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
143 int aRows = myListControl->model()->rowCount();
145 foreach(QModelIndex aIndex, theIndices) {
146 if (aIndex.row() < aRows)
147 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
149 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
150 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
156 //********************************************************************
157 void ModuleBase_ListView::onCopyItem()
159 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
161 foreach(QListWidgetItem* aItem, aItems) {
164 aRes += aItem->text();
166 if (!aRes.isEmpty()) {
167 QClipboard* aClipboard = QApplication::clipboard();
168 aClipboard->setText(aRes);
172 //********************************************************************
173 void ModuleBase_ListView::onListSelection()
175 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
176 myCopyAction->setEnabled(!aItems.isEmpty());
177 myDeleteAction->setEnabled(!aItems.isEmpty());