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>
30 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
33 * Customization of a List Widget to make it to be placed on full width of container
35 class CustomListWidget : public QListWidget
39 /// \param theParent a parent widget
40 CustomListWidget(QWidget* theParent)
41 : QListWidget(theParent)
45 /// Redefinition of virtual method
46 virtual QSize sizeHint() const
48 int aHeight = 2*QFontMetrics(font()).height();
49 QSize aSize = QListWidget::sizeHint();
50 return QSize(aSize.width(), aHeight);
53 /// Redefinition of virtual method
54 virtual QSize minimumSizeHint() const
56 int aHeight = 4/*2*/*QFontMetrics(font()).height();
57 QSize aSize = QListWidget::minimumSizeHint();
58 return QSize(aSize.width(), aHeight);
62 // The code is necessary only for Linux because
63 //it can not update viewport on widget resize
65 void resizeEvent(QResizeEvent* theEvent)
67 QListWidget::resizeEvent(theEvent);
68 QTimer::singleShot(5, viewport(), SLOT(repaint()));
73 //********************************************************************
74 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
75 const QString& theToolTip)
77 myListControl = new CustomListWidget(theParent);
79 myListControl->setObjectName(theObjectName);
80 myListControl->setToolTip(theToolTip);
81 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
83 myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
84 theParent, this, SLOT(onCopyItem()));
85 myCopyAction->setShortcut(QKeySequence::Copy);
86 myCopyAction->setEnabled(false);
87 myListControl->addAction(myCopyAction);
89 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
90 theParent, this, SIGNAL(deleteActionClicked()));
91 myDeleteAction->setEnabled(false);
92 myListControl->addAction(myDeleteAction);
94 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
95 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
98 //********************************************************************
99 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
101 QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
102 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
103 myListControl->addItem(anItem);
106 //********************************************************************
107 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
109 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
110 foreach(QListWidgetItem* anItem, aItems) {
111 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
112 if (theIndices.find(anIndex) == theIndices.end())
113 theIndices.insert(anIndex);
117 //********************************************************************
118 void ModuleBase_ListView::removeSelectedItems()
120 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
121 foreach(QListWidgetItem* anItem, aItems)
122 myListControl->takeItem(myListControl->row(anItem));
125 //********************************************************************
126 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
128 QList<QListWidgetItem*> aItems;
129 for (int i = 0; i < myListControl->count(); i++) {
130 QListWidgetItem* anItem = myListControl->item(i);
131 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
132 if (theIndices.find(anIndex) != theIndices.end())
133 aItems.append(anItem);
135 foreach(QListWidgetItem* anItem, aItems)
136 myListControl->takeItem(myListControl->row(anItem));
139 //********************************************************************
140 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
142 int aRows = myListControl->model()->rowCount();
144 foreach(QModelIndex aIndex, theIndices) {
145 if (aIndex.row() < aRows)
146 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
148 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
149 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
155 //********************************************************************
156 void ModuleBase_ListView::onCopyItem()
158 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
160 foreach(QListWidgetItem* aItem, aItems) {
163 aRes += aItem->text();
165 if (!aRes.isEmpty()) {
166 QClipboard* aClipboard = QApplication::clipboard();
167 aClipboard->setText(aRes);
171 //********************************************************************
172 void ModuleBase_ListView::onListSelection()
174 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
175 myCopyAction->setEnabled(!aItems.isEmpty());
176 myDeleteAction->setEnabled(!aItems.isEmpty());