Fraxinus  22.09
An IGT application
cxToolListWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxToolListWidget.h"
13 
14 #include <QListWidgetItem>
15 #include <QDir>
16 #include <QDropEvent>
17 #include <QMimeData>
18 #include <QAction>
19 #include <QMenu>
20 #include <QDrag>
21 #include "cxLogger.h"
22 #include "cxTrackerConfiguration.h"
23 #include "cxTrackingService.h"
24 
25 namespace cx
26 {
27 //---------------------------------------------------------------------------------------------------------------------
28 
29 ToolListWidget::ToolListWidget(TrackingServicePtr trackingService, QWidget* parent) :
30  QListWidget(parent),
31  mTrackingService(trackingService)
32 {
33  connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChangedSlot()));
34 
35  this->setSelectionBehavior(QAbstractItemView::SelectItems);
36  this->setSelectionMode(QAbstractItemView::SingleSelection);
37 }
38 
40 {
41 }
42 
43 void ToolListWidget::populate(QStringList toolsAbsoluteFilePath)
44 {
45  this->clear();
46 
47  foreach(QString tool, toolsAbsoluteFilePath)
48  {
49  this->addTool(tool);
50  }
51  emit listSizeChanged();
52 }
53 
54 void ToolListWidget::addTool(QString absoluteFilePath)
55 {
56  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
57  QString name = config->getTool(absoluteFilePath).mName;
58 
59 // QFile file(absoluteFilePath);
60 // QFileInfo info(file);
61 // QListWidgetItem* item = new QListWidgetItem(/*QIcon, */info.dir().dirName());
62  QListWidgetItem* item = new QListWidgetItem(name);
63  item->setData(Qt::ToolTipRole, absoluteFilePath);
64  item->setData(Qt::UserRole, absoluteFilePath);
65  this->addItem(item);
66  emit listSizeChanged();
67 }
68 
69 void ToolListWidget::selectionChangedSlot()
70 {
71  QListWidgetItem* selectedItem = this->currentItem();
72  this->toolSelectedSlot(selectedItem);
73 }
74 
75 void ToolListWidget::toolSelectedSlot(QListWidgetItem* item)
76 {
77  if (!item)
78  return;
79 
80  QString absoluteFilePath = item->data(Qt::UserRole).toString();
81  emit toolSelected(absoluteFilePath);
82 }
83 
84 //---------------------------------------------------------------------------------------------------------------------
85 
87  ToolListWidget(trackingService, parent)
88 {
89  this->setDragDropMode(QAbstractItemView::DragOnly);
90  this->setDragEnabled(true);
91 }
92 
94 {
95 }
96 
98 {
99  QFontMetrics metric(this->font());
100  int height = metric.lineSpacing() * 15; // approx 15 lines of text
101  return QSize(300,height); // the height here is important: the default is 150, which is too little
102 }
103 
105 {
106  if (event->button() == Qt::LeftButton)
107  startPos = event->pos();
108  QListWidget::mousePressEvent(event);
109 }
110 
112 {
113  if (event->buttons() & Qt::LeftButton)
114  {
115  int distance = (event->pos() - startPos).manhattanLength();
116  if (distance >= 10)
117  this->startDrag();
118  }
119 }
120 
122 {
123  QListWidgetItem *item = currentItem();
124  if (item)
125  {
126  QMimeData *mimeData = new QMimeData;
127  mimeData->setText(item->data(Qt::ToolTipRole).toString());
128  QDrag *drag = new QDrag(this);
129  drag->setMimeData(mimeData);
130 
131  drag->start(Qt::MoveAction);
132  }
133 }
134 
135 void FilteringToolListWidget::filterSlot(QStringList applicationsFilter, QStringList trackingsystemsFilter)
136 {
137  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
138  QStringList filteredTools = config->getToolsGivenFilter(applicationsFilter,
139  trackingsystemsFilter);
140 // filteredTools.sort(); // no good: we would like to sort on name, but the list is full paths
141  this->populate(filteredTools);
142 }
143 
144 //---------------------------------------------------------------------------------------------------------------------
145 
147  ToolListWidget(trackingService, parent)
148 {
149  this->setContextMenuPolicy(Qt::CustomContextMenu);
150 
151  this->viewport()->setAcceptDrops(true);
152  this->setDropIndicatorShown(true);
153  this->setDefaultDropAction(Qt::CopyAction);
154  this->setDragDropMode(QAbstractItemView::DropOnly);
155 
156 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuSlot(const QPoint &)));
157 }
158 
160 {
161 }
162 
163 void ConfigToolListWidget::dragEnterEvent(QDragEnterEvent *event)
164 {
165  QStringList all = this->getTools();
166  if (all.contains(event->mimeData()->text()))
167  event->ignore();
168  else
169  event->accept();
170 }
171 
172 void ConfigToolListWidget::dragMoveEvent(QDragMoveEvent *event)
173 {
174  event->setDropAction(Qt::MoveAction);
175 // event->accept();
176  QStringList all = this->getTools();
177  if (all.contains(event->mimeData()->text()))
178  event->ignore();
179  else
180  event->accept();
181 }
182 
183 void ConfigToolListWidget::dropEvent(QDropEvent *event)
184 {
185 // std:: cout << "received dropEvent: " << event->mimeData()->text() << std::endl;
186  this->addTool(event->mimeData()->text());
187 // addItem(event->mimeData()->text());
188  event->setDropAction(Qt::MoveAction);
189  event->accept();
190  emit userChangedList();
191  emit listSizeChanged();
192 }
193 
195 {
196  QStringList retval;
197 
198  for (int i = 0; i < this->count(); ++i)
199  {
200  QListWidgetItem* item = this->item(i);
201  retval << item->data(Qt::ToolTipRole).toString();
202  }
203 
204  return retval;
205 }
206 
207 void ConfigToolListWidget::configSlot(QStringList toolsAbsoluteFilePath)
208 {
209  this->populate(toolsAbsoluteFilePath);
210 }
211 
212 void ConfigToolListWidget::filterSlot(QStringList trackingsystemFilter)
213 {
214  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
215 
216  for (int i = 0; i < this->count(); ++i)
217  {
218  QListWidgetItem* item = this->item(i);
219  QString absoluteFilePath = item->data(Qt::ToolTipRole).toString();
220 
221  QString toolTrackingSystemName = config->getTool(absoluteFilePath).mTrackingSystemName;
222 
223  QBrush brush = item->foreground();
224  if (!trackingsystemFilter.contains(toolTrackingSystemName, Qt::CaseInsensitive) || !config->verifyTool(absoluteFilePath))
225  brush.setColor(Qt::red);
226  else
227  brush.setColor(Qt::black);
228 
229  item->setForeground(brush);
230  }
231 }
232 
233 void ConfigToolListWidget::deleteSlot()
234 {
235  if (!mItemToDelete)
236  {
237  reportDebug("Found no item to delete...");
238  return;
239  }
240  this->deleteItemSlot(mItemToDelete);
241 }
242 
243 void ConfigToolListWidget::deleteItemSlot(QListWidgetItem* item)
244 {
245  delete item;
246  emit userChangedList();
247  emit listSizeChanged();
248 }
249 
250 void ConfigToolListWidget::contextMenuSlot(const QPoint& point)
251 {
252  QWidget* sender = dynamic_cast<QWidget*>(this->sender());
253  QPoint pointGlobal = sender->mapToGlobal(point);
254  QMenu contextMenu(sender);
255 
256  QAction* action = new QAction("Remove", &contextMenu);
257 
258  QListWidgetItem* item = this->itemAt(point);
259  if (!item)
260  {
261  reportDebug("Found no item to delete...");
262  return;
263  }
264  mItemToDelete = item;
265 
266  connect(action, SIGNAL(triggered()), this, SLOT(deleteSlot()));
267  contextMenu.addAction(action);
268 
269  contextMenu.exec(pointGlobal);
270 }
271 
272 //---------------------------------------------------------------------------------------------------------------------
273 }//namespace cx
void addTool(QString absoluteFilePath)
void dragEnterEvent(QDragEnterEvent *event)
void filterSlot(QStringList trackingsystemFilter)
filters the tools on tracking system
boost::shared_ptr< class TrackingService > TrackingServicePtr
Class for displaying tool files that can be dragged and droppedSuperclass, not used directly...
virtual void dropEvent(QDropEvent *event)
ToolListWidget(TrackingServicePtr trackingService, QWidget *parent=NULL)
FilteringToolListWidget(TrackingServicePtr trackingService, QWidget *parent=NULL)
ConfigToolListWidget(TrackingServicePtr trackingService, QWidget *parent=NULL)
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
void listSizeChanged()
emitted whenever the count changes
void toolSelected(QString absoluteFilePath)
void mousePressEvent(QMouseEvent *event)
void configSlot(QStringList toolsAbsoluteFilePath)
adds all input tools to the list
void userChangedList()
emitted whenever the user changes the list
void mouseMoveEvent(QMouseEvent *event)
virtual QSize minimumSizeHint() const
void populate(QStringList toolsAbsoluteFilePath)
QStringList getTools()
get absolute file path to all tools currently in the list
TrackingServicePtr mTrackingService
void filterSlot(QStringList applicationsFilter, QStringList trackingsystemsFilter)
void dragMoveEvent(QDragMoveEvent *event)
void reportDebug(QString msg)
Definition: cxLogger.cpp:68
Namespace for all CustusX production code.