00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision$ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 #include "qclickablelabel.h" 00019 00020 #include <QMouseEvent> 00021 00022 #include <iostream> 00023 00024 QClickableLabel::QClickableLabel( QWidget* parent, Qt::WFlags f ) 00025 :QLabel(parent, f) 00026 { 00027 00028 } 00029 00030 00031 QClickableLabel::QClickableLabel( const QString& text, QWidget* parent, Qt::WFlags f ) 00032 :QLabel(text, parent, f) 00033 { 00034 00035 } 00036 00037 00038 QClickableLabel::~QClickableLabel() 00039 { 00040 00041 } 00042 00043 void QClickableLabel::AddHotspot( const QString& name, const QRect position ) 00044 { 00045 m_Hotspots.push_back( position ); 00046 m_HotspotIndexForName.insert( std::make_pair( name, (int)m_Hotspots.size()-1 ) ); 00047 m_HotspotNameForIndex.insert( std::make_pair( (int)m_Hotspots.size()-1, name ) ); 00048 } 00049 00050 00051 void QClickableLabel::RemoveHotspot( const QString& name ) 00052 { 00053 NameToIndexMapType::iterator iter = m_HotspotIndexForName.find( name ); 00054 00055 if ( iter != m_HotspotIndexForName.end() ) 00056 { 00057 RemoveHotspot( iter->second ); 00058 } 00059 } 00060 00061 void QClickableLabel::RemoveHotspot( unsigned int hotspotIndex ) 00062 { 00063 if ( hotspotIndex < m_Hotspots.size() ) 00064 { 00065 m_Hotspots.erase( m_Hotspots.begin() + hotspotIndex ); 00066 QString name = m_HotspotNameForIndex[hotspotIndex]; 00067 m_HotspotNameForIndex.erase( hotspotIndex ); 00068 m_HotspotIndexForName.erase( name ); 00069 } 00070 } 00071 00072 void QClickableLabel::RemoveAllHotspots() 00073 { 00074 m_Hotspots.clear(); 00075 m_HotspotIndexForName.clear(); 00076 m_HotspotNameForIndex.clear(); 00077 } 00078 00079 void QClickableLabel::mousePressEvent( QMouseEvent* e ) 00080 { 00081 unsigned int index = matchingRect( e->pos() ); 00082 if ( index < m_Hotspots.size() ) 00083 { 00084 emit mouseReleased( index ); 00085 emit mouseReleased( m_HotspotNameForIndex[index] ); 00086 } 00087 } 00088 00089 void QClickableLabel::mouseReleaseEvent( QMouseEvent* e ) 00090 { 00091 unsigned int index = matchingRect( e->pos() ); 00092 if ( index < m_Hotspots.size() ) 00093 { 00094 emit mousePressed( index ); 00095 emit mousePressed( m_HotspotNameForIndex[index] ); 00096 } 00097 } 00098 00099 unsigned int QClickableLabel::matchingRect( const QPoint& p ) 00100 { 00101 unsigned int index(0); 00102 for ( RectVectorType::iterator iter = m_Hotspots.begin(); 00103 iter != m_Hotspots.end(); 00104 ++iter ) 00105 { 00106 if ( iter->contains(p) ) 00107 { 00108 return index; 00109 } 00110 00111 ++index; 00112 } 00113 00114 return index; 00115 } 00116