Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxToolListWidget.h"
34 
35 #include <QListWidgetItem>
36 #include <QDir>
37 #include <QDropEvent>
38 #include <QMimeData>
39 #include <QAction>
40 #include <QMenu>
41 #include <QDrag>
42 #include "cxEnumConverter.h"
43 #include "cxLogger.h"
44 #include "cxTrackerConfiguration.h"
45 #include "cxLegacySingletons.h"
46 #include "cxTrackingService.h"
47 
48 namespace cx
49 {
50 //---------------------------------------------------------------------------------------------------------------------
51 
53  QListWidget(parent)
54 {
55  connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChangedSlot()));
56 
57  this->setSelectionBehavior(QAbstractItemView::SelectItems);
58  this->setSelectionMode(QAbstractItemView::SingleSelection);
59 }
60 
62 {
63 }
64 
65 void ToolListWidget::populate(QStringList toolsAbsoluteFilePath)
66 {
67  this->clear();
68 
69  foreach(QString tool, toolsAbsoluteFilePath)
70  {
71  this->addTool(tool);
72  }
73  emit listSizeChanged();
74 }
75 
76 void ToolListWidget::addTool(QString absoluteFilePath)
77 {
78  TrackerConfigurationPtr config = trackingService()->getConfiguration();
79  QString name = config->getTool(absoluteFilePath).mName;
80 
81 // QFile file(absoluteFilePath);
82 // QFileInfo info(file);
83 // QListWidgetItem* item = new QListWidgetItem(/*QIcon, */info.dir().dirName());
84  QListWidgetItem* item = new QListWidgetItem(name);
85  item->setData(Qt::ToolTipRole, absoluteFilePath);
86  item->setData(Qt::UserRole, absoluteFilePath);
87  this->addItem(item);
88  emit listSizeChanged();
89 }
90 
91 void ToolListWidget::selectionChangedSlot()
92 {
93  QListWidgetItem* selectedItem = this->currentItem();
94  this->toolSelectedSlot(selectedItem);
95 }
96 
97 void ToolListWidget::toolSelectedSlot(QListWidgetItem* item)
98 {
99  if (!item)
100  return;
101 
102  QString absoluteFilePath = item->data(Qt::UserRole).toString();
103  emit toolSelected(absoluteFilePath);
104 }
105 
106 //---------------------------------------------------------------------------------------------------------------------
107 
109  ToolListWidget(parent)
110 {
111  this->setDragDropMode(QAbstractItemView::DragOnly);
112  this->setDragEnabled(true);
113 }
114 
116 {
117 }
118 
120 {
121  QFontMetrics metric(this->font());
122  int height = metric.lineSpacing() * 15; // approx 15 lines of text
123  return QSize(300,height); // the height here is important: the default is 150, which is too little
124 }
125 
127 {
128  if (event->button() == Qt::LeftButton)
129  startPos = event->pos();
130  QListWidget::mousePressEvent(event);
131 }
132 
134 {
135  if (event->buttons() & Qt::LeftButton)
136  {
137  int distance = (event->pos() - startPos).manhattanLength();
138  if (distance >= 10)
139  this->startDrag();
140  }
141 }
142 
144 {
145  QListWidgetItem *item = currentItem();
146  if (item)
147  {
148  QMimeData *mimeData = new QMimeData;
149  mimeData->setText(item->data(Qt::ToolTipRole).toString());
150  QDrag *drag = new QDrag(this);
151  drag->setMimeData(mimeData);
152 
153  drag->start(Qt::MoveAction);
154  }
155 }
156 
157 void FilteringToolListWidget::filterSlot(QStringList applicationsFilter, QStringList trackingsystemsFilter)
158 {
159  TrackerConfigurationPtr config = trackingService()->getConfiguration();
160  QStringList filteredTools = config->getToolsGivenFilter(applicationsFilter,
161  trackingsystemsFilter);
162 // filteredTools.sort(); // no good: we would like to sort on name, but the list is full paths
163  this->populate(filteredTools);
164 }
165 
166 //---------------------------------------------------------------------------------------------------------------------
167 
169  ToolListWidget(parent)
170 {
171  this->setContextMenuPolicy(Qt::CustomContextMenu);
172 
173  this->viewport()->setAcceptDrops(true);
174  this->setDropIndicatorShown(true);
175  this->setDefaultDropAction(Qt::CopyAction);
176  this->setDragDropMode(QAbstractItemView::DropOnly);
177 
178 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuSlot(const QPoint &)));
179 }
180 
182 {
183 }
184 
185 void ConfigToolListWidget::dragEnterEvent(QDragEnterEvent *event)
186 {
187  QStringList all = this->getTools();
188  if (all.contains(event->mimeData()->text()))
189  event->ignore();
190  else
191  event->accept();
192 }
193 
194 void ConfigToolListWidget::dragMoveEvent(QDragMoveEvent *event)
195 {
196  event->setDropAction(Qt::MoveAction);
197 // event->accept();
198  QStringList all = this->getTools();
199  if (all.contains(event->mimeData()->text()))
200  event->ignore();
201  else
202  event->accept();
203 }
204 
205 void ConfigToolListWidget::dropEvent(QDropEvent *event)
206 {
207 // std:: cout << "received dropEvent: " << event->mimeData()->text() << std::endl;
208  this->addTool(event->mimeData()->text());
209 // addItem(event->mimeData()->text());
210  event->setDropAction(Qt::MoveAction);
211  event->accept();
212  emit userChangedList();
213  emit listSizeChanged();
214 }
215 
217 {
218  QStringList retval;
219 
220  for (int i = 0; i < this->count(); ++i)
221  {
222  QListWidgetItem* item = this->item(i);
223  retval << item->data(Qt::ToolTipRole).toString();
224  }
225 
226  return retval;
227 }
228 
229 void ConfigToolListWidget::configSlot(QStringList toolsAbsoluteFilePath)
230 {
231  this->populate(toolsAbsoluteFilePath);
232 }
233 
234 void ConfigToolListWidget::filterSlot(QStringList trackingsystemFilter)
235 {
236  TrackerConfigurationPtr config = trackingService()->getConfiguration();
237 
238  for (int i = 0; i < this->count(); ++i)
239  {
240  QListWidgetItem* item = this->item(i);
241  QString absoluteFilePath = item->data(Qt::ToolTipRole).toString();
242 
243  QString toolTrackingSystem = config->getTool(absoluteFilePath).mTrackingSystem;
244 
245  QBrush brush = item->foreground();
246  if (!trackingsystemFilter.contains(toolTrackingSystem, Qt::CaseInsensitive) || !config->verifyTool(absoluteFilePath))
247  brush.setColor(Qt::red);
248  else
249  brush.setColor(Qt::black);
250 
251  item->setForeground(brush);
252  }
253 }
254 
255 void ConfigToolListWidget::deleteSlot()
256 {
257  if (!mItemToDelete)
258  {
259  reportDebug("Found no item to delete...");
260  return;
261  }
262  this->deleteItemSlot(mItemToDelete);
263 }
264 
265 void ConfigToolListWidget::deleteItemSlot(QListWidgetItem* item)
266 {
267  delete item;
268  emit userChangedList();
269  emit listSizeChanged();
270 }
271 
272 void ConfigToolListWidget::contextMenuSlot(const QPoint& point)
273 {
274  QWidget* sender = dynamic_cast<QWidget*>(this->sender());
275  QPoint pointGlobal = sender->mapToGlobal(point);
276  QMenu contextMenu(sender);
277 
278  QAction* action = new QAction("Remove", &contextMenu);
279 
280  QListWidgetItem* item = this->itemAt(point);
281  if (!item)
282  {
283  reportDebug("Found no item to delete...");
284  return;
285  }
286  mItemToDelete = item;
287 
288  connect(action, SIGNAL(triggered()), this, SLOT(deleteSlot()));
289  contextMenu.addAction(action);
290 
291  contextMenu.exec(pointGlobal);
292 }
293 
294 //---------------------------------------------------------------------------------------------------------------------
295 }//namespace cx
void addTool(QString absoluteFilePath)
void dragEnterEvent(QDragEnterEvent *event)
void filterSlot(QStringList trackingsystemFilter)
filters the tools on tracking system
Class for displaying tool files that can be dragged and droppedSuperclass, not used directly...
virtual void dropEvent(QDropEvent *event)
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
ConfigToolListWidget(QWidget *parent=NULL)
FilteringToolListWidget(QWidget *parent=NULL)
void populate(QStringList toolsAbsoluteFilePath)
QStringList getTools()
get absolute file path to all tools currently in the list
ToolListWidget(QWidget *parent=NULL)
void filterSlot(QStringList applicationsFilter, QStringList trackingsystemsFilter)
cxLogicManager_EXPORT TrackingServicePtr trackingService()
void dragMoveEvent(QDragMoveEvent *event)
void reportDebug(QString msg)
Definition: cxLogger.cpp:89