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 #include <QResizeEvent>
35 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
38 * Customization of a List Widget to make it to be placed on full width of container
40 class CustomListWidget : public QListWidget
44 /// \param theParent a parent widget
45 CustomListWidget(QWidget* theParent)
46 : QListWidget(theParent)
50 /// Redefinition of virtual method
51 virtual QSize sizeHint() const
53 int aHeight = 2*QFontMetrics(font()).height();
54 QSize aSize = QListWidget::sizeHint();
55 return QSize(aSize.width(), aHeight);
58 /// Redefinition of virtual method
59 virtual QSize minimumSizeHint() const
61 int aHeight = 4/*2*/*QFontMetrics(font()).height();
62 QSize aSize = QListWidget::minimumSizeHint();
63 return QSize(aSize.width(), aHeight);
67 // The code is necessary only for Linux because
68 //it can not update viewport on widget resize
70 void resizeEvent(QResizeEvent* theEvent)
72 QListWidget::resizeEvent(theEvent);
73 QTimer::singleShot(5, viewport(), SLOT(repaint()));
78 //********************************************************************
79 ModuleBase_ListView::ModuleBase_ListView(QWidget* theParent, const QString& theObjectName,
80 const QString& theToolTip)
82 myListControl = new CustomListWidget(theParent);
84 myListControl->setObjectName(theObjectName);
85 myListControl->setToolTip(theToolTip);
86 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
88 myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
89 theParent, this, SLOT(onCopyItem()));
90 myCopyAction->setShortcut(QKeySequence::Copy);
91 myCopyAction->setEnabled(false);
92 myListControl->addAction(myCopyAction);
94 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
95 theParent, this, SIGNAL(deleteActionClicked()));
96 myDeleteAction->setEnabled(false);
97 myListControl->addAction(myDeleteAction);
99 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
100 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
103 //********************************************************************
104 void ModuleBase_ListView::addItem(const QString& theTextValue, const int theIndex)
106 QListWidgetItem* anItem = new QListWidgetItem(theTextValue, myListControl);
107 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, theIndex);
108 myListControl->addItem(anItem);
111 //********************************************************************
112 void ModuleBase_ListView::getSelectedIndices(std::set<int>& theIndices)
114 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
115 foreach(QListWidgetItem* anItem, aItems) {
116 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
117 if (theIndices.find(anIndex) == theIndices.end())
118 theIndices.insert(anIndex);
122 //********************************************************************
123 void ModuleBase_ListView::removeSelectedItems()
125 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
126 foreach(QListWidgetItem* anItem, aItems)
127 myListControl->takeItem(myListControl->row(anItem));
130 //********************************************************************
131 void ModuleBase_ListView::removeItems(std::set<int>& theIndices)
133 QList<QListWidgetItem*> aItems;
134 for (int i = 0; i < myListControl->count(); i++) {
135 QListWidgetItem* anItem = myListControl->item(i);
136 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
137 if (theIndices.find(anIndex) != theIndices.end())
138 aItems.append(anItem);
140 foreach(QListWidgetItem* anItem, aItems)
141 myListControl->takeItem(myListControl->row(anItem));
144 //********************************************************************
145 void ModuleBase_ListView::restoreSelection(const QModelIndexList& theIndices)
147 int aRows = myListControl->model()->rowCount();
149 foreach(QModelIndex aIndex, theIndices) {
150 if (aIndex.row() < aRows)
151 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
153 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
154 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
160 //********************************************************************
161 void ModuleBase_ListView::onCopyItem()
163 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
165 foreach(QListWidgetItem* aItem, aItems) {
168 aRes += aItem->text();
170 if (!aRes.isEmpty()) {
171 QClipboard* aClipboard = QApplication::clipboard();
172 aClipboard->setText(aRes);
176 //********************************************************************
177 void ModuleBase_ListView::onListSelection()
179 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
180 myCopyAction->setEnabled(!aItems.isEmpty());
181 myDeleteAction->setEnabled(!aItems.isEmpty());