Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmitkTextOverlay.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 "QmitkTextOverlay.h"
18 
19 #include "mitkColorProperty.h"
20 #include "mitkProperties.h"
21 #include "mitkPropertyList.h"
22 
23 #include <itkCommand.h>
24 
25 QmitkTextOverlay::QmitkTextOverlay(const char *id) : QmitkOverlay(id), m_ObservedProperty(nullptr), m_ObserverTag(0)
26 {
27  m_Widget = m_Label = new QLabel();
29 }
30 
32 {
33  m_PropertyList->GetProperty(m_Id)->RemoveObserver(m_ObserverTag);
34 }
35 
37 {
38  if (pl.IsNull())
39  return;
40 
41  m_PropertyList = pl;
42 
43  if (m_PropertyList.IsNotNull())
44  {
45  this->SetupCallback(m_PropertyList->GetProperty(m_Id));
46 
47  this->UpdateFontProperties(pl);
49  }
50  else
51  {
52  MITK_ERROR << "invalid propList";
53  }
54 }
55 
57 {
58  std::string text;
59  if (m_PropertyList.IsNull() || !m_PropertyList->GetStringProperty(m_Id, text))
60  {
61  MITK_DEBUG << "Property " << m_Id << " could not be found";
62  }
63  if (text != m_Label->text().toStdString())
64  {
65  m_Label->setText(text.c_str());
66  m_Label->repaint();
67  }
68 }
69 
71 {
72  if (pl.IsNull())
73  return;
74 
75  mitk::PropertyList::Pointer propertyList = pl;
76  QPalette palette = QPalette();
77  QFont font = QFont();
78 
79  // get the desired color of the textOverlays
81  dynamic_cast<mitk::ColorProperty *>(propertyList->GetProperty("overlay.color"));
82 
83  if (colorProp.IsNull())
84  {
85  colorProp = mitk::ColorProperty::New(127.0, 196.0, 232.0);
86  }
87 
88  mitk::Color color = colorProp->GetColor();
89  palette.setColor(QPalette::Foreground, QColor(color[0], color[1], color[2], 255));
90  palette.setColor(QPalette::Window, Qt::transparent);
91  m_Label->setPalette(palette);
92 
93  // get the desired opacity of the overlays
94  // mitk::FloatProperty::Pointer opacityProperty =
95  // dynamic_cast<mitk::FloatProperty*>( propertyList->GetProperty( "overlay.opacity" ) );
96 
97  // if ( opacityProperty.IsNull() )
98  //{
99  // m_Label->setWindowOpacity( 1 );
100  //}
101  // else
102  //{
103  // m_Label->setWindowOpacity( opacityProperty->GetValue() );
104  //}
105 
106  // set the desired font-size of the overlays
107  int fontSize = 0;
108  if (!propertyList->GetIntProperty("overlay.fontSize", fontSize))
109  {
110  fontSize = 9;
111  }
112  font.setPointSize(fontSize);
113 
114  bool useKerning = false;
115  if (!propertyList->GetBoolProperty("overlay.kerning", useKerning))
116  {
117  useKerning = true;
118  }
119  font.setKerning(useKerning);
120 
121  std::string fontFamily = "";
122  if (!propertyList->GetStringProperty("overlay.fontFamily", fontFamily))
123  {
124  fontFamily = "Verdana";
125  }
126  font.setFamily(QString(fontFamily.c_str()));
127 
128  m_Label->setFont(font);
129 }
130 
132 {
133  if (m_ObservedProperty != prop && m_ObserverTag == 0)
134  {
135  if (prop.IsNotNull())
136  {
137  if (m_ObservedProperty.IsNotNull())
138  {
139  m_ObservedProperty->RemoveObserver(m_ObserverTag);
140  }
141 
142  typedef itk::SimpleMemberCommand<QmitkTextOverlay> MemberCommandType;
143  MemberCommandType::Pointer propModifiedCommand;
144  propModifiedCommand = MemberCommandType::New();
145  propModifiedCommand->SetCallbackFunction(this, &QmitkTextOverlay::UpdateDisplayedTextFromProperties);
146  m_ObserverTag = prop->AddObserver(itk::ModifiedEvent(), propModifiedCommand);
147  }
148 
149  m_ObservedProperty = prop;
150  }
151  else
152  {
153  MITK_DEBUG << "invalid property";
154  }
155 }
156 
158 {
159  QFont font = m_Label->font();
160  QFontMetrics fm(font);
161 
162  return fm.size(Qt::TextSingleLine, m_Label->text());
163 }
QSize GetNeededSize() override
itk::SmartPointer< Self > Pointer
void UpdateFontProperties(mitk::PropertyList::Pointer)
internal helper class to determine text-properties
QmitkTextOverlay(const char *id)
Default Constructor.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static Pointer New()
void AddDropShadow(QWidget *widget)
Add drop shadow effect via QGraphicsEffect.
Abstract base class for all overlay-objects in MITK.
Definition: QmitkOverlay.h:53
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
void SetupCallback(mitk::BaseProperty::Pointer prop)
void UpdateDisplayedTextFromProperties()
mitk::PropertyList::Pointer m_PropertyList
The ColorProperty class RGB color property.
QWidget * m_Widget
internal QWidget representing the overlay
Definition: QmitkOverlay.h:120
unsigned long m_ObserverTag
mitk::BaseProperty::Pointer m_ObservedProperty
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
QLabel * m_Label
QLabel internally representing the TextOverlay.
void GenerateData(mitk::PropertyList::Pointer) override
Setup the QLabel with overlay specific information.
virtual ~QmitkTextOverlay()
Default Destructor.
const char * m_Id
ID of the overlay.
Definition: QmitkOverlay.h:111
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.