Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
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 (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 "QmitkTextOverlay.h"
14 
15 #include "mitkColorProperty.h"
16 #include "mitkProperties.h"
17 #include "mitkPropertyList.h"
18 
19 #include <itkCommand.h>
20 
21 QmitkTextOverlay::QmitkTextOverlay(const char *id) : QmitkOverlay(id), m_ObservedProperty(nullptr), m_ObserverTag(0)
22 {
23  m_Widget = m_Label = new QLabel();
25 }
26 
28 {
29  m_PropertyList->GetProperty(m_Id)->RemoveObserver(m_ObserverTag);
30 }
31 
33 {
34  if (pl.IsNull())
35  return;
36 
37  m_PropertyList = pl;
38 
39  if (m_PropertyList.IsNotNull())
40  {
41  this->SetupCallback(m_PropertyList->GetProperty(m_Id));
42 
43  this->UpdateFontProperties(pl);
45  }
46  else
47  {
48  MITK_ERROR << "invalid propList";
49  }
50 }
51 
53 {
54  std::string text;
55  if (m_PropertyList.IsNull() || !m_PropertyList->GetStringProperty(m_Id, text))
56  {
57  MITK_DEBUG << "Property " << m_Id << " could not be found";
58  }
59  if (text != m_Label->text().toStdString())
60  {
61  m_Label->setText(text.c_str());
62  m_Label->repaint();
63  }
64 }
65 
67 {
68  if (pl.IsNull())
69  return;
70 
71  mitk::PropertyList::Pointer propertyList = pl;
72  QPalette palette = QPalette();
73  QFont font = QFont();
74 
75  // get the desired color of the textOverlays
77  dynamic_cast<mitk::ColorProperty *>(propertyList->GetProperty("overlay.color"));
78 
79  if (colorProp.IsNull())
80  {
81  colorProp = mitk::ColorProperty::New(127.0, 196.0, 232.0);
82  }
83 
84  mitk::Color color = colorProp->GetColor();
85  palette.setColor(QPalette::Foreground, QColor(color[0], color[1], color[2], 255));
86  palette.setColor(QPalette::Window, Qt::transparent);
87  m_Label->setPalette(palette);
88 
89  // get the desired opacity of the overlays
90  // mitk::FloatProperty::Pointer opacityProperty =
91  // dynamic_cast<mitk::FloatProperty*>( propertyList->GetProperty( "overlay.opacity" ) );
92 
93  // if ( opacityProperty.IsNull() )
94  //{
95  // m_Label->setWindowOpacity( 1 );
96  //}
97  // else
98  //{
99  // m_Label->setWindowOpacity( opacityProperty->GetValue() );
100  //}
101 
102  // set the desired font-size of the overlays
103  int fontSize = 0;
104  if (!propertyList->GetIntProperty("overlay.fontSize", fontSize))
105  {
106  fontSize = 9;
107  }
108  font.setPointSize(fontSize);
109 
110  bool useKerning = false;
111  if (!propertyList->GetBoolProperty("overlay.kerning", useKerning))
112  {
113  useKerning = true;
114  }
115  font.setKerning(useKerning);
116 
117  std::string fontFamily = "";
118  if (!propertyList->GetStringProperty("overlay.fontFamily", fontFamily))
119  {
120  fontFamily = "Verdana";
121  }
122  font.setFamily(QString(fontFamily.c_str()));
123 
124  m_Label->setFont(font);
125 }
126 
128 {
129  if (m_ObservedProperty != prop && m_ObserverTag == 0)
130  {
131  if (prop.IsNotNull())
132  {
133  if (m_ObservedProperty.IsNotNull())
134  {
135  m_ObservedProperty->RemoveObserver(m_ObserverTag);
136  }
137 
138  typedef itk::SimpleMemberCommand<QmitkTextOverlay> MemberCommandType;
139  MemberCommandType::Pointer propModifiedCommand;
140  propModifiedCommand = MemberCommandType::New();
141  propModifiedCommand->SetCallbackFunction(this, &QmitkTextOverlay::UpdateDisplayedTextFromProperties);
142  m_ObserverTag = prop->AddObserver(itk::ModifiedEvent(), propModifiedCommand);
143  }
144 
145  m_ObservedProperty = prop;
146  }
147  else
148  {
149  MITK_DEBUG << "invalid property";
150  }
151 }
152 
154 {
155  QFont font = m_Label->font();
156  QFontMetrics fm(font);
157 
158  return fm.size(Qt::TextSingleLine, m_Label->text());
159 }
QSize GetNeededSize() override
void UpdateFontProperties(mitk::PropertyList::Pointer)
internal helper class to determine text-properties
QmitkTextOverlay(const char *id)
Default Constructor.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
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:49
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
void SetupCallback(mitk::BaseProperty::Pointer prop)
void UpdateDisplayedTextFromProperties()
~QmitkTextOverlay() override
Default Destructor.
mitk::PropertyList::Pointer m_PropertyList
The ColorProperty class RGB color property.
QWidget * m_Widget
internal QWidget representing the overlay
Definition: QmitkOverlay.h:116
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.
const char * m_Id
ID of the overlay.
Definition: QmitkOverlay.h:107