Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "qclickablelabel.h"
14 
15 #include <QMouseEvent>
16 
17 #include <iostream>
18 
19 QClickableLabel::QClickableLabel(QWidget *parent, Qt::WindowFlags f) : QLabel(parent, f)
20 {
21 }
22 
23 QClickableLabel::QClickableLabel(const QString &text, QWidget *parent, Qt::WindowFlags f) : QLabel(text, parent, f)
24 {
25 }
26 
28 {
29 }
30 
31 void QClickableLabel::AddHotspot(const QString &name, const QRect position)
32 {
33  m_Hotspots.push_back(position);
34  m_HotspotIndexForName.insert(std::make_pair(name, (int)m_Hotspots.size() - 1));
35  m_HotspotNameForIndex.insert(std::make_pair((int)m_Hotspots.size() - 1, name));
36 }
37 
38 void QClickableLabel::RemoveHotspot(const QString &name)
39 {
40  auto iter = m_HotspotIndexForName.find(name);
41 
42  if (iter != m_HotspotIndexForName.end())
43  {
44  RemoveHotspot(iter->second);
45  }
46 }
47 
48 void QClickableLabel::RemoveHotspot(unsigned int hotspotIndex)
49 {
50  if (hotspotIndex < m_Hotspots.size())
51  {
52  m_Hotspots.erase(m_Hotspots.begin() + hotspotIndex);
53  QString name = m_HotspotNameForIndex[hotspotIndex];
54  m_HotspotNameForIndex.erase(hotspotIndex);
55  m_HotspotIndexForName.erase(name);
56  }
57 }
58 
60 {
61  m_Hotspots.clear();
62  m_HotspotIndexForName.clear();
63  m_HotspotNameForIndex.clear();
64 }
65 
67 {
68  unsigned int index = matchingRect(e->pos());
69  if (index < m_Hotspots.size())
70  {
71  emit mouseReleased(index);
73  }
74 }
75 
77 {
78  unsigned int index = matchingRect(e->pos());
79  if (index < m_Hotspots.size())
80  {
81  emit mousePressed(index);
83  }
84 }
85 
86 unsigned int QClickableLabel::matchingRect(const QPoint &p)
87 {
88  unsigned int index(0);
89  for (auto iter = m_Hotspots.begin(); iter != m_Hotspots.end(); ++iter)
90  {
91  if (iter->contains(p))
92  {
93  return index;
94  }
95 
96  ++index;
97  }
98 
99  return index;
100 }
void mouseReleased(const QString &hotspotName)
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)
void mouseReleaseEvent(QMouseEvent *e) override
IndexToNameMapType m_HotspotNameForIndex
~QClickableLabel() override
NameToIndexMapType m_HotspotIndexForName
unsigned int matchingRect(const QPoint &p)
returns index == m_Hotspots.size() if nothing is hit
void RemoveHotspot(const QString &name)