Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "QmitkScalarBar.h"
00019
00020 #include <QPainter>
00021 #include <QPaintEvent>
00022
00023 QmitkScalarBar::QmitkScalarBar(QWidget* parent):
00024 QWidget( parent, Qt::Tool | Qt::FramelessWindowHint ), m_Alignment(vertical)
00025 {
00026 m_NumberOfSubDivisions = 7;
00027
00028 this->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00029 this->SetupGeometry( m_Alignment );
00030 this->setBackgroundRole(QPalette::Base);
00031 this->setAttribute( Qt::WA_TranslucentBackground, true );
00032 this->setAutoFillBackground(false);
00033
00034
00035 this->setAttribute( Qt::WA_X11NetWmWindowTypeUtility, true );
00036
00037
00038
00039 this->setAttribute( Qt::WA_MacAlwaysShowToolWindow, true );
00040
00041 this->setAttribute( Qt::WA_MacShowFocusRect, false );
00042
00043
00044 this->resize( 20,60 );
00045 this->setFixedWidth( 20 );
00046 this->setFixedHeight( 60 );
00047
00048
00049 m_Pen = QPen( Qt::red, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin );
00050 }
00051
00052 QmitkScalarBar::~QmitkScalarBar()
00053 {
00054 }
00055
00056 void QmitkScalarBar::SetupGeometry( alignment align )
00057 {
00058 m_HorizontalLines.clear();
00059 switch ( align )
00060 {
00061 case vertical :
00062 {
00063 for ( unsigned int i=0; i<m_NumberOfSubDivisions; ++i )
00064 {
00065 int y = this->height()/(m_NumberOfSubDivisions-1)*i;
00066 if ( i==0 )
00067 {
00068
00069 y = 1;
00070 }
00071 else if ( i==m_NumberOfSubDivisions-1 )
00072 {
00073
00074 y = this->height() - 1;
00075 }
00076 m_HorizontalLines.push_back( new QLine( QPoint(0,y), QPoint(width(),y) ) );
00077 }
00078
00079 if ( m_HorizontalLines.size() > 0 )
00080 m_VerticalLine = new QLine( QPoint(width()/2,0), QPoint(width()/2,height()) );
00081 break;
00082 }
00083 case horizontal :
00084 {
00085 for ( unsigned int i=0; i<m_NumberOfSubDivisions; ++i )
00086 {
00087 int x = this->width()/(m_NumberOfSubDivisions-1)*i;
00088 if ( i==0 )
00089 {
00090 x = 1;
00091 }
00092 else if ( i==m_NumberOfSubDivisions-1 )
00093 {
00094 x = this->width() - 1;
00095 }
00096 m_HorizontalLines.push_back( new QLine( QPoint(x,0), QPoint(x,height()) ) );
00097 }
00098 if ( m_HorizontalLines.size() > 0 )
00099 m_VerticalLine = new QLine( QPoint(0,height()/2), QPoint(width(),height()/2) );
00100
00101 break;
00102 }
00103 }
00104 }
00105
00106
00107 void QmitkScalarBar::SetScaleFactor( double scale )
00108 {
00109 m_ScaleFactor = scale;
00110
00111
00112 if ( this->parentWidget() != NULL && this->parentWidget()->parentWidget() != NULL )
00113 {
00114
00115 if ( this->height() > this->parentWidget()->parentWidget()->height()*0.7 && m_NumberOfSubDivisions >= 3 )
00116 {
00117 m_NumberOfSubDivisions-=2;
00118 }
00119
00120 else if ( this->height() < this->parentWidget()->parentWidget()->height()*0.4 && ( m_NumberOfSubDivisions < 7 && m_NumberOfSubDivisions > 0 ) )
00121 {
00122 m_NumberOfSubDivisions+=2;
00123 }
00124
00125 if ( m_NumberOfSubDivisions == 1 )
00126 {
00127 this->resize( 0, 0 );
00128 this->setFixedWidth( 0 );
00129 this->setFixedHeight( 0 );
00130 }
00131 else
00132 {
00133 this->resize( 20, (m_NumberOfSubDivisions-1)*10/m_ScaleFactor );
00134 this->setFixedWidth( 20 );
00135 this->setFixedHeight( (m_NumberOfSubDivisions-1)*10/m_ScaleFactor );
00136 this->SetupGeometry(m_Alignment);
00137 }
00138
00139 if ( this->height() > this->parentWidget()->parentWidget()->height()*0.7 && m_NumberOfSubDivisions >= 3 )
00140 SetScaleFactor( scale );
00141
00142 }
00143 }
00144
00145 void QmitkScalarBar::SetNumberOfSubdivisions( unsigned int subs )
00146 {
00147 m_NumberOfSubDivisions = subs;
00148 }
00149
00150 unsigned int QmitkScalarBar::GetNumberOfSubdivisions()
00151 {
00152 return m_NumberOfSubDivisions;
00153 }
00154
00155
00156 void QmitkScalarBar::paintEvent(QPaintEvent* )
00157 {
00158 if ( m_NumberOfSubDivisions > 1 )
00159 {
00160 try
00161 {
00162 QPainter painter(this);
00163 painter.setPen( m_Pen );
00164 painter.setBrush( Qt::SolidPattern );
00165 painter.setRenderHint( QPainter::Antialiasing, true );
00166
00167 painter.drawLine( m_VerticalLine->p1(), m_VerticalLine->p2() );
00168
00169 foreach( QLine* line, m_HorizontalLines )
00170 {
00171 painter.drawLine( line->p1(), line->p2() );
00172 }
00173 }
00174 catch (...)
00175 {
00176 MITK_ERROR << "ScalarBar cannot be drawn.";
00177 }
00178 }
00179 }
00180
00181
00182 void QmitkScalarBar::SetAlignment( alignment align )
00183 {
00184 m_Alignment = align;
00185 this->SetupGeometry( align );
00186 }
00187
00188 void QmitkScalarBar::SetPen( const QPen& pen )
00189 {
00190 m_Pen = pen;
00191 }