Fraxinus  18.10
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 "cxEnumConverter.h"
22 #include "cxLogger.h"
23 #include "cxTrackerConfiguration.h"
24 #include "cxTrackingService.h"
25 
26 namespace cx
27 {
28 //---------------------------------------------------------------------------------------------------------------------
29 
30 ToolListWidget::ToolListWidget(TrackingServicePtr trackingService, QWidget* parent) :
31  QListWidget(parent),
32  mTrackingService(trackingService)
33 {
34  connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChangedSlot()));
35 
36  this->setSelectionBehavior(QAbstractItemView::SelectItems);
37  this->setSelectionMode(QAbstractItemView::SingleSelection);
38 }
39 
41 {
42 }
43 
44 void ToolListWidget::populate(QStringList toolsAbsoluteFilePath)
45 {
46  this->clear();
47 
48  foreach(QString tool, toolsAbsoluteFilePath)
49  {
50  this->addTool(tool);
51  }
52  emit listSizeChanged();
53 }
54 
55 void ToolListWidget::addTool(QString absoluteFilePath)
56 {
57  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
58  QString name = config->getTool(absoluteFilePath).mName;
59 
60 // QFile file(absoluteFilePath);
61 // QFileInfo info(file);
62 // QListWidgetItem* item = new QListWidgetItem(/*QIcon, */info.dir().dirName());
63  QListWidgetItem* item = new QListWidgetItem(name);
64  item->setData(Qt::ToolTipRole, absoluteFilePath);
65  item->setData(Qt::UserRole, absoluteFilePath);
66  this->addItem(item);
67  emit listSizeChanged();
68 }
69 
70 void ToolListWidget::selectionChangedSlot()
71 {
72  QListWidgetItem* selectedItem = this->currentItem();
73  this->toolSelectedSlot(selectedItem);
74 }
75 
76 void ToolListWidget::toolSelectedSlot(QListWidgetItem* item)
77 {
78  if (!item)
79  return;
80 
81  QString absoluteFilePath = item->data(Qt::UserRole).toString();
82  emit toolSelected(absoluteFilePath);
83 }
84 
85 //---------------------------------------------------------------------------------------------------------------------
86 
88  ToolListWidget(trackingService, parent)
89 {
90  this->setDragDropMode(QAbstractItemView::DragOnly);
91  this->setDragEnabled(true);
92 }
93 
95 {
96 }
97 
99 {
100  QFontMetrics metric(this->font());
101  int height = metric.lineSpacing() * 15; // approx 15 lines of text
102  return QSize(300,height); // the height here is important: the default is 150, which is too little
103 }
104 
106 {
107  if (event->button() == Qt::LeftButton)
108  startPos = event->pos();
109  QListWidget::mousePressEvent(event);
110 }
111 
113 {
114  if (event->buttons() & Qt::LeftButton)
115  {
116  int distance = (event->pos() - startPos).manhattanLength();
117  if (distance >= 10)
118  this->startDrag();
119  }
120 }
121 
123 {
124  QListWidgetItem *item = currentItem();
125  if (item)
126  {
127  QMimeData *mimeData = new QMimeData;
128  mimeData->setText(item->data(Qt::ToolTipRole).toString());
129  QDrag *drag = new QDrag(this);
130  drag->setMimeData(mimeData);
131 
132  drag->start(Qt::MoveAction);
133  }
134 }
135 
136 void FilteringToolListWidget::filterSlot(QStringList applicationsFilter, QStringList trackingsystemsFilter)
137 {
138  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
139  QStringList filteredTools = config->getToolsGivenFilter(applicationsFilter,
140  trackingsystemsFilter);
141 // filteredTools.sort(); // no good: we would like to sort on name, but the list is full paths
142  this->populate(filteredTools);
143 }
144 
145 //---------------------------------------------------------------------------------------------------------------------
146 
148  ToolListWidget(trackingService, parent)
149 {
150  this->setContextMenuPolicy(Qt::CustomContextMenu);
151 
152  this->viewport()->setAcceptDrops(true);
153  this->setDropIndicatorShown(true);
154  this->setDefaultDropAction(Qt::CopyAction);
155  this->setDragDropMode(QAbstractItemView::DropOnly);
156 
157 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuSlot(const QPoint &)));
158 }
159 
161 {
162 }
163 
164 void ConfigToolListWidget::dragEnterEvent(QDragEnterEvent *event)
165 {
166  QStringList all = this->getTools();
167  if (all.contains(event->mimeData()->text()))
168  event->ignore();
169  else
170  event->accept();
171 }
172 
173 void ConfigToolListWidget::dragMoveEvent(QDragMoveEvent *event)
174 {
175  event->setDropAction(Qt::MoveAction);
176 // event->accept();
177  QStringList all = this->getTools();
178  if (all.contains(event->mimeData()->text()))
179  event->ignore();
180  else
181  event->accept();
182 }
183 
184 void ConfigToolListWidget::dropEvent(QDropEvent *event)
185 {
186 // std:: cout << "received dropEvent: " << event->mimeData()->text() << std::endl;
187  this->addTool(event->mimeData()->text());
188 // addItem(event->mimeData()->text());
189  event->setDropAction(Qt::MoveAction);
190  event->accept();
191  emit userChangedList();
192  emit listSizeChanged();
193 }
194 
196 {
197  QStringList retval;
198 
199  for (int i = 0; i < this->count(); ++i)
200  {
201  QListWidgetItem* item = this->item(i);
202  retval << item->data(Qt::ToolTipRole).toString();
203  }
204 
205  return retval;
206 }
207 
208 void ConfigToolListWidget::configSlot(QStringList toolsAbsoluteFilePath)
209 {
210  this->populate(toolsAbsoluteFilePath);
211 }
212 
213 void ConfigToolListWidget::filterSlot(QStringList trackingsystemFilter)
214 {
215  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
216 
217  for (int i = 0; i < this->count(); ++i)
218  {
219  QListWidgetItem* item = this->item(i);
220  QString absoluteFilePath = item->data(Qt::ToolTipRole).toString();
221 
222  QString toolTrackingSystemName = config->getTool(absoluteFilePath).mTrackingSystemName;
223 
224  QBrush brush = item->foreground();
225  if (!trackingsystemFilter.contains(toolTrackingSystemName, Qt::CaseInsensitive) || !config->verifyTool(absoluteFilePath))
226  brush.setColor(Qt::red);
227  else
228  brush.setColor(Qt::black);
229 
230  item->setForeground(brush);
231  }
232 }
233 
234 void ConfigToolListWidget::deleteSlot()
235 {
236  if (!mItemToDelete)
237  {
238  reportDebug("Found no item to delete...");
239  return;
240  }
241  this->deleteItemSlot(mItemToDelete);
242 }
243 
244 void ConfigToolListWidget::deleteItemSlot(QListWidgetItem* item)
245 {
246  delete item;
247  emit userChangedList();
248  emit listSizeChanged();
249 }
250 
251 void ConfigToolListWidget::contextMenuSlot(const QPoint& point)
252 {
253  QWidget* sender = dynamic_cast<QWidget*>(this->sender());
254  QPoint pointGlobal = sender->mapToGlobal(point);
255  QMenu contextMenu(sender);
256 
257  QAction* action = new QAction("Remove", &contextMenu);
258 
259  QListWidgetItem* item = this->itemAt(point);
260  if (!item)
261  {
262  reportDebug("Found no item to delete...");
263  return;
264  }
265  mItemToDelete = item;
266 
267  connect(action, SIGNAL(triggered()), this, SLOT(deleteSlot()));
268  contextMenu.addAction(action);
269 
270  contextMenu.exec(pointGlobal);
271 }
272 
273 //---------------------------------------------------------------------------------------------------------------------
274 }//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.