7 #include "scene2D/adaptor/SScaleValues.hpp" 9 #include <fwRenderQt/data/InitQtPen.hpp> 10 #include <fwRenderQt/Scene2DGraphicsView.hpp> 12 #include <fwServices/macros.hpp> 14 #include <QGraphicsItemGroup> 23 static const ::fwServices::IService::KeyType s_VIEWPORT_INPUT =
"viewport";
49 const ConfigType config = this->
getConfigTree().get_child(
"config.<xmlattr>");
52 SLM_ASSERT(
"'min' atttribute is missing.", config.count(
"min"));
53 SLM_ASSERT(
"'min' atttribute is missing.", config.count(
"max"));
55 m_min = config.get<
double>(
"min");
56 m_max = config.get<
double>(
"max");
59 if (config.count(
"interval"))
61 m_interval = config.get<
float>(
"interval");
65 if (config.count(
"color"))
75 if (config.count(
"fontSize"))
77 m_fontSize = config.get<
float>(
"fontSize");
81 if (config.count(
"showUnit"))
83 const std::string showUnit = config.get<std::string>(
"showUnit");
84 SLM_ASSERT(
"Unsupported value "+showUnit+
" for 'showUnit' attribute (true or false expected).",
85 showUnit ==
"true" || showUnit ==
"false");
86 m_showUnit = (showUnit ==
"true");
90 m_displayedUnit = config.get<std::string>(
"unit",
"");
93 "Please add an 'align' attribute with value 'left', 'right', 'top' or 'bottom'",
94 config.count(
"align"));
97 m_align = config.get<std::string>(
"align");
99 SLM_ASSERT(
"Unsupported value for 'align' attribute.",
100 m_align ==
"left" || m_align ==
"right" || m_align ==
"top" || m_align ==
"bottom");
105 void SScaleValues::buildValues()
107 SLM_ASSERT(
"m_interval can not be equal to 0", m_interval != 0);
111 double val = this->getStartVal();
112 const int range = (int) ceil(this->getEndVal() - val);
113 const int nbValues = (int)(ceil(range/ m_interval)) + 1;
117 if(m_align ==
"left")
121 else if(m_align ==
"right")
131 for(
int i = 0; i < nbValues; ++i, val += m_interval)
133 QGraphicsSimpleTextItem* text =
new QGraphicsSimpleTextItem();
134 text->setText( QString::fromStdString(format).arg(val) );
135 text->setFont( m_font );
136 text->setBrush( m_brush );
137 text->setCacheMode( QGraphicsItem::DeviceCoordinateCache );
139 m_values.push_back( text );
143 for( QGraphicsItem* item : m_values )
145 m_layer->addToGroup( item );
149 m_unit =
new QGraphicsSimpleTextItem();
150 m_unit->setText( QString::fromStdString( m_displayedUnit ) );
151 m_unit->setFont( m_font );
152 m_unit->setBrush( m_brush );
153 m_unit->setCacheMode( QGraphicsItem::DeviceCoordinateCache );
158 m_layer->addToGroup( m_unit );
174 m_layer =
new QGraphicsItemGroup();
176 m_brush = QBrush( m_pen.color() );
177 m_pen.setCosmetic(
true);
179 m_font.setPointSize( m_fontSize );
180 m_font.setLetterSpacing( QFont::AbsoluteSpacing, 0.25 );
181 m_font.setKerning(
true );
182 m_font.setFixedPitch(
true );
190 double SScaleValues::getStartVal()
192 return (
int)( m_min / m_interval ) * m_interval;
196 double SScaleValues::getEndVal()
198 return (
int)( m_max / m_interval ) * m_interval;
208 this->rescaleValues();
213 void SScaleValues::rescaleValues()
215 ::fwRenderQt::data::Viewport::csptr viewport =
216 this->getInput< ::fwRenderQt::data::Viewport>(s_VIEWPORT_INPUT);
218 const double viewportX = viewport->getX();
219 const double viewportWidth = viewport->getWidth();
220 const double viewportHeight = viewport->getHeight();
222 const double viewportSizeRatio = viewportHeight / viewportWidth;
228 double scaleX = m_fontSize;
229 double scaleY = m_fontSize * viewportSizeRatio;
230 scaleY /= viewportWidthRatio;
231 scaleY *= viewInitialSizeRatio;
233 scaleX = scaleX * ratio.first;
234 scaleY = scaleY * ratio.second;
236 QTransform transform;
237 transform.scale( scaleX, scaleY );
243 bool suggestResampling =
false;
246 const size_t valuesSize = m_values.size();
247 float val = getStartVal();
249 if(m_align ==
"left" || m_align ==
"right")
251 const double valueSizeRatio = m_interval / scaleY;
257 if(m_align ==
"left")
259 textPosX = viewportX;
264 textPosX = viewportX + viewportWidth;
267 for(
int i = 0; i < valuesSize; ++i, val += m_interval)
269 valueSize = m_values[i]->boundingRect().height();
274 step = (int)(valueSize / valueSizeRatio) + 1;
279 suggestResampling =
true;
284 m_values[i]->setTransform( transform );
287 coord.first + coeff * size.first * scaleX,
288 coord.second - (m_interval - size.second / 2) * scaleY );
291 m_unit->setTransform( transform );
293 val = viewportHeight * 0.8f;
297 coeff = (m_align ==
"left") ? 1 : -1.5;
300 coord.first + coeff * 2 * size.first * scaleX,
301 coord.second + size.second * scaleY);
305 const double valueSizeRatio = m_interval / scaleX;
309 const double textPosY = (m_align ==
"bottom")
311 : viewportHeight * 0.9;
313 for(
int i = 0; i < valuesSize; ++i, val += m_interval)
315 valueSize = m_values[i]->boundingRect().width();
320 step = (int)(valueSize / valueSizeRatio) + 1;
325 suggestResampling =
true;
331 m_values[i]->setTransform( transform );
334 coord.first - size.first / 2 * scaleX,
335 coord.second - coeff * size.second / 2 * scaleY );
338 m_unit->setTransform( transform );
340 val = viewportHeight * 0.8;
348 coeff = (m_align ==
"left") ? 1 : -1.5;
351 coord.first - size.first * scaleX,
352 coord.second - 1.5 * size.second * scaleY);
355 if( suggestResampling )
357 this->showHideScaleValues();
359 else if( !suggestResampling && step != m_step )
362 this->showHideScaleValues();
368 void SScaleValues::showHideScaleValues()
371 const int size = (int)m_values.size();
372 const double startVal = this->getStartVal();
374 for(
int i = 0; i < size; ++i)
376 value = i * m_interval + startVal;
380 m_values[i]->setVisible( fmod(value, (m_step * m_interval)) == 0.0 );
388 if( _event.getType() == ::fwRenderQt::data::Event::Resize)
Root class for all scene2d adaptors.
::fwRenderQt::data::Axis::sptr m_xAxis
The x Axis.
This class is a helper to define the connections of a service and its data.
std::pair< float, float > Scene2DRatio
<width, height>
FWRENDERQT_API void initializeViewSize()
Initialize the source values used for computing view's size ratio.
SCENE2D_API void stopping() override
Clean the lines vector and remove the layer from the scene.
ViewSizeRatio m_viewInitialSize
FWRENDERQT_API Point2DType mapAdaptorToScene(const Point2DType &_xy, const ::fwRenderQt::data::Axis::sptr &_xAxis, const ::fwRenderQt::data::Axis::sptr &_yAxis) const
FWRENDERQT_API std::shared_ptr< ::fwRenderQt::SRender > getScene2DRender() const
Get the render that manages the IAdaptor.
std::pair< double, double > Point2DType
Point2D coordinate <X, Y>
static FWRENDERQT_API void setPenColor(QPen &_pen, std::string _color)
Set a pen a color.
This bundles contains data and services used to display a 2D Qt scene.
virtual SCENE2D_API ~SScaleValues() noexcept
Basic destructor, do nothing.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
FWRENDERQT_API void initializeViewportSize()
Initialize the source values used for computing viewport's size ratio.
SCENE2D_API void updating() override
Do nothing.
::fwRenderQt::data::Axis::sptr m_yAxis
The y Axis.
float m_opacity
Opacity of the adaptor. Default value set to 1 (opaque).
SCENE2D_API void configuring() override
Configure the service before starting. Apply the configuration to service.
SCENE2D_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated objects signals, this method is used for obj...
FWRENDERQT_API ViewportSizeRatio getViewportSizeRatio() const
Return the ratio between viewport's initial size and its current size.
SScaleValues adaptor. Display values and units on the axis.
FWRENDERQT_API void configureParams()
Parse the xml configuration for Axis, z value and opacity.
SCENE2D_API SScaleValues() noexcept
Constructor, set the x and y spacing to 10.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
SCENE2D_API void starting() override
Initialize the layer, set the pen style to DashLine and call the draw() function. ...
SCENE2D_API void processInteraction(::fwRenderQt::data::Event &_event) override
Manage the given events.
This class manage events on the scene 2D (mouse event, keyboard event , ...).
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.