Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
qclickablelabel.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "qclickablelabel.h"
18 
19 #include <QMouseEvent>
20 
21 #include <iostream>
22 
23 QClickableLabel::QClickableLabel(QWidget *parent, Qt::WindowFlags f) : QLabel(parent, f)
24 {
25 }
26 
27 QClickableLabel::QClickableLabel(const QString &text, QWidget *parent, Qt::WindowFlags f) : QLabel(text, parent, f)
28 {
29 }
30 
32 {
33 }
34 
35 void QClickableLabel::AddHotspot(const QString &name, const QRect position)
36 {
37  m_Hotspots.push_back(position);
38  m_HotspotIndexForName.insert(std::make_pair(name, (int)m_Hotspots.size() - 1));
39  m_HotspotNameForIndex.insert(std::make_pair((int)m_Hotspots.size() - 1, name));
40 }
41 
42 void QClickableLabel::RemoveHotspot(const QString &name)
43 {
44  auto iter = m_HotspotIndexForName.find(name);
45 
46  if (iter != m_HotspotIndexForName.end())
47  {
48  RemoveHotspot(iter->second);
49  }
50 }
51 
52 void QClickableLabel::RemoveHotspot(unsigned int hotspotIndex)
53 {
54  if (hotspotIndex < m_Hotspots.size())
55  {
56  m_Hotspots.erase(m_Hotspots.begin() + hotspotIndex);
57  QString name = m_HotspotNameForIndex[hotspotIndex];
58  m_HotspotNameForIndex.erase(hotspotIndex);
59  m_HotspotIndexForName.erase(name);
60  }
61 }
62 
64 {
65  m_Hotspots.clear();
66  m_HotspotIndexForName.clear();
67  m_HotspotNameForIndex.clear();
68 }
69 
71 {
72  unsigned int index = matchingRect(e->pos());
73  if (index < m_Hotspots.size())
74  {
75  emit mouseReleased(index);
77  }
78 }
79 
81 {
82  unsigned int index = matchingRect(e->pos());
83  if (index < m_Hotspots.size())
84  {
85  emit mousePressed(index);
87  }
88 }
89 
90 unsigned int QClickableLabel::matchingRect(const QPoint &p)
91 {
92  unsigned int index(0);
93  for (auto iter = m_Hotspots.begin(); iter != m_Hotspots.end(); ++iter)
94  {
95  if (iter->contains(p))
96  {
97  return index;
98  }
99 
100  ++index;
101  }
102 
103  return index;
104 }
void mouseReleased(const QString &hotspotName)
virtual void mousePressEvent(QMouseEvent *e) override
RectVectorType m_Hotspots
void mousePressed(const QString &hotspotName)
void AddHotspot(const QString &name, const QRect position)
QClickableLabel(QWidget *parent, Qt::WindowFlags f=nullptr)
virtual void mouseReleaseEvent(QMouseEvent *e) override
IndexToNameMapType m_HotspotNameForIndex
NameToIndexMapType m_HotspotIndexForName
unsigned int matchingRect(const QPoint &p)
returns index == m_Hotspots.size() if nothing is hit
void RemoveHotspot(const QString &name)
virtual ~QClickableLabel()