Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_plot_layout.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (3 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_plot_layout.cpp (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_plot_layout.cpp
r9383 r11158 1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*-*****************************1 /****************************************************************************** 2 2 * Qwt Widget Library 3 3 * Copyright (C) 1997 Josef Wilgen … … 13 13 #include "qwt_scale_widget.h" 14 14 #include "qwt_abstract_legend.h" 15 #include <qscrollbar.h> 16 #include <qmath.h> 17 18 class QwtPlotLayout::LayoutData 19 { 20 public: 21 void init( const QwtPlot *, const QRectF &rect ); 22 23 struct t_legendData 24 { 25 int frameWidth; 26 int hScrollExtent; 27 int vScrollExtent; 28 QSize hint; 29 } legend; 30 31 struct t_titleData 32 { 33 QwtText text; 34 int frameWidth; 35 } title; 36 37 struct t_footerData 38 { 39 QwtText text; 40 int frameWidth; 41 } footer; 42 43 struct t_scaleData 44 { 45 bool isEnabled; 46 const QwtScaleWidget *scaleWidget; 47 QFont scaleFont; 48 int start; 49 int end; 50 int baseLineOffset; 51 double tickOffset; 52 int dimWithoutTitle; 53 } scale[QwtPlot::axisCnt]; 54 55 struct t_canvasData 56 { 57 int contentsMargins[ QwtPlot::axisCnt ]; 58 59 } canvas; 60 }; 61 62 /* 63 Extract all layout relevant data from the plot components 64 */ 65 void QwtPlotLayout::LayoutData::init( const QwtPlot *plot, const QRectF &rect ) 66 { 67 // legend 68 69 if ( plot->legend() ) 70 { 71 legend.frameWidth = plot->legend()->frameWidth(); 72 legend.hScrollExtent = 73 plot->legend()->scrollExtent( Qt::Horizontal ); 74 legend.vScrollExtent = 75 plot->legend()->scrollExtent( Qt::Vertical ); 76 77 const QSize hint = plot->legend()->sizeHint(); 78 79 const int w = qMin( hint.width(), qFloor( rect.width() ) ); 80 81 int h = plot->legend()->heightForWidth( w ); 82 if ( h <= 0 ) 83 h = hint.height(); 84 85 legend.hint = QSize( w, h ); 86 } 87 88 // title 89 90 title.frameWidth = 0; 91 title.text = QwtText(); 92 93 if ( plot->titleLabel() ) 94 { 95 const QwtTextLabel *label = plot->titleLabel(); 96 title.text = label->text(); 97 if ( !( title.text.testPaintAttribute( QwtText::PaintUsingTextFont ) ) ) 98 title.text.setFont( label->font() ); 99 100 title.frameWidth = plot->titleLabel()->frameWidth(); 101 } 102 103 // footer 104 105 footer.frameWidth = 0; 106 footer.text = QwtText(); 107 108 if ( plot->footerLabel() ) 109 { 110 const QwtTextLabel *label = plot->footerLabel(); 111 footer.text = label->text(); 112 if ( !( footer.text.testPaintAttribute( QwtText::PaintUsingTextFont ) ) ) 113 footer.text.setFont( label->font() ); 114 115 footer.frameWidth = plot->footerLabel()->frameWidth(); 116 } 117 118 // scales 119 120 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 121 { 122 if ( plot->axisEnabled( axis ) ) 123 { 124 const QwtScaleWidget *scaleWidget = plot->axisWidget( axis ); 125 126 scale[axis].isEnabled = true; 127 128 scale[axis].scaleWidget = scaleWidget; 129 130 scale[axis].scaleFont = scaleWidget->font(); 131 132 scale[axis].start = scaleWidget->startBorderDist(); 133 scale[axis].end = scaleWidget->endBorderDist(); 134 135 scale[axis].baseLineOffset = scaleWidget->margin(); 136 scale[axis].tickOffset = scaleWidget->margin(); 137 if ( scaleWidget->scaleDraw()->hasComponent( 138 QwtAbstractScaleDraw::Ticks ) ) 139 { 140 scale[axis].tickOffset += 141 scaleWidget->scaleDraw()->maxTickLength(); 142 } 143 144 scale[axis].dimWithoutTitle = scaleWidget->dimForLength( 145 QWIDGETSIZE_MAX, scale[axis].scaleFont ); 146 147 if ( !scaleWidget->title().isEmpty() ) 148 { 149 scale[axis].dimWithoutTitle -= 150 scaleWidget->titleHeightForWidth( QWIDGETSIZE_MAX ); 151 } 152 } 153 else 154 { 155 scale[axis].isEnabled = false; 156 scale[axis].start = 0; 157 scale[axis].end = 0; 158 scale[axis].baseLineOffset = 0; 159 scale[axis].tickOffset = 0.0; 160 scale[axis].dimWithoutTitle = 0; 161 } 162 } 163 164 // canvas 165 166 plot->canvas()->getContentsMargins( 167 &canvas.contentsMargins[ QwtPlot::yLeft ], 168 &canvas.contentsMargins[ QwtPlot::xTop ], 169 &canvas.contentsMargins[ QwtPlot::yRight ], 170 &canvas.contentsMargins[ QwtPlot::xBottom ] ); 171 } 172 173 class QwtPlotLayout::PrivateData 174 { 175 public: 176 PrivateData(): 177 spacing( 5 ) 178 { 179 } 180 181 QRectF titleRect; 182 QRectF footerRect; 183 QRectF legendRect; 184 QRectF scaleRect[QwtPlot::axisCnt]; 185 QRectF canvasRect; 186 187 QwtPlotLayout::LayoutData layoutData; 188 189 QwtPlot::LegendPosition legendPos; 190 double legendRatio; 191 unsigned int spacing; 192 unsigned int canvasMargin[QwtPlot::axisCnt]; 193 bool alignCanvasToScales[QwtPlot::axisCnt]; 194 }; 195 196 /*! 197 \brief Constructor 198 */ 199 200 QwtPlotLayout::QwtPlotLayout() 201 { 202 d_data = new PrivateData; 203 204 setLegendPosition( QwtPlot::BottomLegend ); 205 setCanvasMargin( 4 ); 206 setAlignCanvasToScales( false ); 207 208 invalidate(); 209 } 210 211 //! Destructor 212 QwtPlotLayout::~QwtPlotLayout() 213 { 214 delete d_data; 215 } 216 217 /*! 218 Change a margin of the canvas. The margin is the space 219 above/below the scale ticks. A negative margin will 220 be set to -1, excluding the borders of the scales. 221 222 \param margin New margin 223 \param axis One of QwtPlot::Axis. Specifies where the position of the margin. 224 -1 means margin at all borders. 225 \sa canvasMargin() 226 227 \warning The margin will have no effect when alignCanvasToScale() is true 228 */ 229 230 void QwtPlotLayout::setCanvasMargin( int margin, int axis ) 231 { 232 if ( margin < -1 ) 233 margin = -1; 234 235 if ( axis == -1 ) 236 { 237 for ( axis = 0; axis < QwtPlot::axisCnt; axis++ ) 238 d_data->canvasMargin[axis] = margin; 239 } 240 else if ( axis >= 0 && axis < QwtPlot::axisCnt ) 241 d_data->canvasMargin[axis] = margin; 242 } 243 244 /*! 245 \param axisId Axis index 246 \return Margin around the scale tick borders 247 \sa setCanvasMargin() 248 */ 249 int QwtPlotLayout::canvasMargin( int axisId ) const 250 { 251 if ( axisId < 0 || axisId >= QwtPlot::axisCnt ) 15 #include "qwt_math.h" 16 17 #include <qmargins.h> 18 19 namespace 20 { 21 class LayoutData 22 { 23 public: 24 struct LegendData 25 { 26 void init( const QwtAbstractLegend* legend ) 27 { 28 if ( legend ) 29 { 30 frameWidth = legend->frameWidth(); 31 hScrollExtent = legend->scrollExtent( Qt::Horizontal ); 32 vScrollExtent = legend->scrollExtent( Qt::Vertical ); 33 34 hint = legend->sizeHint(); 35 } 36 } 37 38 QSize legendHint( const QwtAbstractLegend* legend, const QRectF& rect ) const 39 { 40 const int w = qMin( hint.width(), qwtFloor( rect.width() ) ); 41 42 int h = legend->heightForWidth( w ); 43 if ( h <= 0 ) 44 h = hint.height(); 45 46 return QSize( w, h ); 47 } 48 49 int frameWidth; 50 int hScrollExtent; 51 int vScrollExtent; 52 QSize hint; 53 }; 54 55 struct LabelData 56 { 57 void init( const QwtTextLabel* label ) 58 { 59 frameWidth = 0; 60 text = QwtText(); 61 62 if ( label ) 63 { 64 text = label->text(); 65 if ( !( text.testPaintAttribute( QwtText::PaintUsingTextFont ) ) ) 66 text.setFont( label->font() ); 67 68 frameWidth = label->frameWidth(); 69 } 70 } 71 72 QwtText text; 73 int frameWidth; 74 }; 75 76 struct ScaleData 77 { 78 void init( const QwtScaleWidget* axisWidget ) 79 { 80 isVisible = true; 81 82 scaleWidget = axisWidget; 83 scaleFont = axisWidget->font(); 84 85 start = axisWidget->startBorderDist(); 86 end = axisWidget->endBorderDist(); 87 88 baseLineOffset = axisWidget->margin(); 89 90 tickOffset = axisWidget->margin(); 91 if ( axisWidget->scaleDraw()->hasComponent( QwtAbstractScaleDraw::Ticks ) ) 92 tickOffset += axisWidget->scaleDraw()->maxTickLength(); 93 94 dimWithoutTitle = axisWidget->dimForLength( QWIDGETSIZE_MAX, scaleFont ); 95 if ( !axisWidget->title().isEmpty() ) 96 dimWithoutTitle -= axisWidget->titleHeightForWidth( QWIDGETSIZE_MAX ); 97 } 98 99 void reset() 100 { 101 isVisible = false; 102 start = 0; 103 end = 0; 104 baseLineOffset = 0; 105 tickOffset = 0.0; 106 dimWithoutTitle = 0; 107 } 108 109 bool isVisible; 110 const QwtScaleWidget* scaleWidget; 111 QFont scaleFont; 112 int start; 113 int end; 114 int baseLineOffset; 115 double tickOffset; 116 int dimWithoutTitle; 117 }; 118 119 struct CanvasData 120 { 121 void init( const QWidget* canvas ) 122 { 123 const QMargins m = canvas->contentsMargins(); 124 125 contentsMargins[ QwtAxis::YLeft ] = m.left(); 126 contentsMargins[ QwtAxis::XTop ] = m.top(); 127 contentsMargins[ QwtAxis::YRight ] = m.right(); 128 contentsMargins[ QwtAxis::XBottom ] = m.bottom(); 129 } 130 131 int contentsMargins[ QwtAxis::AxisPositions ]; 132 }; 133 134 public: 135 enum Label 136 { 137 Title, 138 Footer, 139 140 NumLabels 141 }; 142 143 LayoutData( const QwtPlot* ); 144 bool hasSymmetricYAxes() const; 145 146 inline ScaleData& axisData( QwtAxisId axisId ) 147 { 148 return m_scaleData[ axisId ]; 149 } 150 151 inline const ScaleData& axisData( QwtAxisId axisId ) const 152 { 153 return m_scaleData[ axisId ]; 154 } 155 156 inline double tickOffset( int axisPos ) const 157 { 158 return axisData( axisPos ).tickOffset; 159 } 160 161 LegendData legendData; 162 LabelData labelData[ NumLabels ]; 163 CanvasData canvasData; 164 165 private: 166 ScaleData m_scaleData[ QwtAxis::AxisPositions ]; 167 }; 168 169 /* 170 Extract all layout relevant data from the plot components 171 */ 172 LayoutData::LayoutData( const QwtPlot* plot ) 173 { 174 legendData.init( plot->legend() ); 175 labelData[ Title ].init( plot->titleLabel() ); 176 labelData[ Footer ].init( plot->footerLabel() ); 177 178 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ ) 179 { 180 { 181 const QwtAxisId axisId( axisPos ); 182 183 ScaleData& scaleData = axisData( axisId ); 184 185 if ( plot->isAxisVisible( axisId ) ) 186 { 187 const QwtScaleWidget* scaleWidget = plot->axisWidget( axisId ); 188 scaleData.init( scaleWidget ); 189 } 190 else 191 { 192 scaleData.reset(); 193 } 194 } 195 } 196 197 canvasData.init( plot->canvas() ); 198 } 199 200 bool LayoutData::hasSymmetricYAxes() const 201 { 202 using namespace QwtAxis; 203 return m_scaleData[ YLeft ].isVisible == m_scaleData[ YRight ].isVisible; 204 } 205 } 206 207 namespace 208 { 209 class LayoutHintData 210 { 211 public: 212 LayoutHintData( const QwtPlot* plot ); 213 214 int alignedSize( const QwtAxisId ) const; 215 216 inline int yAxesWidth() const 217 { 218 using namespace QwtAxis; 219 return axesWidth( YLeft ) + axesWidth( YRight ); 220 } 221 222 inline int yAxesHeight() const 223 { 224 using namespace QwtAxis; 225 return qMax( axesHeight( YLeft ), axesHeight( YRight ) ); 226 } 227 228 inline int xAxesHeight() const 229 { 230 using namespace QwtAxis; 231 return axesHeight( XTop ) + axesHeight( XBottom ); 232 } 233 234 inline int xAxesWidth() const 235 { 236 using namespace QwtAxis; 237 return qMax( axesWidth( XTop ), axesWidth( XBottom ) ); 238 } 239 240 private: 241 242 struct ScaleData 243 { 244 ScaleData() 245 { 246 w = h = minLeft = minRight = tickOffset = 0; 247 } 248 249 int w; 250 int h; 251 int minLeft; 252 int minRight; 253 int tickOffset; 254 255 }; 256 257 const ScaleData& axisData( QwtAxisId axisId ) const 258 { 259 return m_scaleData[ axisId ]; 260 } 261 262 ScaleData& axisData( QwtAxisId axisId ) 263 { 264 return m_scaleData[ axisId ]; 265 } 266 267 inline int axesWidth( int axisPos ) const 268 { 269 return m_scaleData[axisPos].w; 270 } 271 272 inline int axesHeight( int axisPos ) const 273 { 274 return m_scaleData[axisPos].h; 275 } 276 277 int m_canvasBorder[QwtAxis::AxisPositions]; 278 ScaleData m_scaleData[QwtAxis::AxisPositions]; 279 }; 280 281 LayoutHintData::LayoutHintData( const QwtPlot* plot ) 282 { 283 using namespace QwtAxis; 284 285 const QMargins m = plot->canvas()->contentsMargins(); 286 287 int contentsMargins[ 4 ]; 288 contentsMargins[ YLeft ] = m.left(); 289 contentsMargins[ XTop ] = m.top(); 290 contentsMargins[ YRight ] = m.right(); 291 contentsMargins[ XBottom ] = m.bottom(); 292 293 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ ) 294 { 295 m_canvasBorder[axisPos] = contentsMargins[axisPos] + 296 plot->plotLayout()->canvasMargin( axisPos ) + 1; 297 { 298 const QwtAxisId axisId( axisPos ); 299 300 if ( plot->isAxisVisible( axisId ) ) 301 { 302 const QwtScaleWidget* scl = plot->axisWidget( axisId ); 303 304 const QSize hint = scl->minimumSizeHint(); 305 306 ScaleData& sd = axisData( axisId ); 307 sd.w = hint.width(); 308 sd.h = hint.height(); 309 scl->getBorderDistHint( sd.minLeft, sd.minRight ); 310 311 { 312 sd.tickOffset = scl->margin(); 313 if ( scl->scaleDraw()->hasComponent( QwtAbstractScaleDraw::Ticks ) ) 314 sd.tickOffset += qwtCeil( scl->scaleDraw()->maxTickLength() ); 315 } 316 } 317 } 318 } 319 320 for ( int axis = 0; axis < AxisPositions; axis++ ) 321 { 322 const int sz = alignedSize( axis ); 323 324 ScaleData& sd = axisData( axis ); 325 if ( isXAxis( axis ) ) 326 sd.w = sz; 327 else 328 sd.h = sz; 329 } 330 } 331 332 int LayoutHintData::alignedSize( const QwtAxisId axisId ) const 333 { 334 using namespace QwtAxis; 335 336 const ScaleData& sd = axisData( axisId ); 337 338 if ( sd.w && isXAxis( axisId ) ) 339 { 340 int w = sd.w; 341 342 if ( const int leftW = axesWidth( YLeft ) ) 343 { 344 const int shiftLeft = sd.minLeft - m_canvasBorder[YLeft]; 345 if ( shiftLeft > 0 ) 346 w -= qMin( shiftLeft, leftW ); 347 } 348 349 if ( const int rightW = axesWidth( YRight ) ) 350 { 351 const int shiftRight = sd.minRight - m_canvasBorder[YRight]; 352 if ( shiftRight > 0 ) 353 w -= qMin( shiftRight, rightW ); 354 } 355 356 return w; 357 } 358 359 if ( sd.h && isYAxis( axisId ) ) 360 { 361 int h = sd.h; 362 363 if ( axesHeight( XBottom ) ) 364 { 365 const int shiftBottom = sd.minLeft - m_canvasBorder[XBottom]; 366 if ( shiftBottom > 0 ) 367 h -= qMin( shiftBottom, axisData( XBottom ).tickOffset ); 368 } 369 370 if ( axesHeight( XTop ) ) 371 { 372 const int shiftTop = sd.minRight - m_canvasBorder[XTop]; 373 if ( shiftTop > 0 ) 374 h -= qMin( shiftTop, axisData( XTop ).tickOffset ); 375 } 376 377 return h; 378 } 379 252 380 return 0; 253 254 return d_data->canvasMargin[axisId]; 255 } 256 257 /*! 258 \brief Set the align-canvas-to-axis-scales flag for all axes 259 260 \param on True/False 261 \sa setAlignCanvasToScale(), alignCanvasToScale() 262 */ 263 void QwtPlotLayout::setAlignCanvasToScales( bool on ) 264 { 265 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 266 d_data->alignCanvasToScales[axis] = on; 267 } 268 269 /*! 270 Change the align-canvas-to-axis-scales setting. The canvas may: 271 272 - extend beyond the axis scale ends to maximize its size, 273 - align with the axis scale ends to control its size. 274 275 The axisId parameter is somehow confusing as it identifies a border 276 of the plot and not the axes, that are aligned. F.e when QwtPlot::yLeft 277 is set, the left end of the the x-axes ( QwtPlot::xTop, QwtPlot::xBottom ) 278 is aligned. 279 280 \param axisId Axis index 281 \param on New align-canvas-to-axis-scales setting 282 283 \sa setCanvasMargin(), alignCanvasToScale(), setAlignCanvasToScales() 284 \warning In case of on == true canvasMargin() will have no effect 285 */ 286 void QwtPlotLayout::setAlignCanvasToScale( int axisId, bool on ) 287 { 288 if ( axisId >= 0 && axisId < QwtPlot::axisCnt ) 289 d_data->alignCanvasToScales[axisId] = on; 290 } 291 292 /*! 293 Return the align-canvas-to-axis-scales setting. The canvas may: 294 - extend beyond the axis scale ends to maximize its size 295 - align with the axis scale ends to control its size. 296 297 \param axisId Axis index 298 \return align-canvas-to-axis-scales setting 299 \sa setAlignCanvasToScale(), setAlignCanvasToScale(), setCanvasMargin() 300 */ 301 bool QwtPlotLayout::alignCanvasToScale( int axisId ) const 302 { 303 if ( axisId < 0 || axisId >= QwtPlot::axisCnt ) 304 return false; 305 306 return d_data->alignCanvasToScales[ axisId ]; 307 } 308 309 /*! 310 Change the spacing of the plot. The spacing is the distance 311 between the plot components. 312 313 \param spacing New spacing 314 \sa setCanvasMargin(), spacing() 315 */ 316 void QwtPlotLayout::setSpacing( int spacing ) 317 { 318 d_data->spacing = qMax( 0, spacing ); 319 } 320 321 /*! 322 \return Spacing 323 \sa margin(), setSpacing() 324 */ 325 int QwtPlotLayout::spacing() const 326 { 327 return d_data->spacing; 328 } 329 330 /*! 331 \brief Specify the position of the legend 332 \param pos The legend's position. 333 \param ratio Ratio between legend and the bounding rectangle 334 of title, footer, canvas and axes. The legend will be shrunk 335 if it would need more space than the given ratio. 336 The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0 337 it will be reset to the default ratio. 338 The default vertical/horizontal ratio is 0.33/0.5. 339 340 \sa QwtPlot::setLegendPosition() 341 */ 342 343 void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos, double ratio ) 344 { 345 if ( ratio > 1.0 ) 346 ratio = 1.0; 347 348 switch ( pos ) 349 { 350 case QwtPlot::TopLegend: 351 case QwtPlot::BottomLegend: 352 if ( ratio <= 0.0 ) 353 ratio = 0.33; 354 d_data->legendRatio = ratio; 355 d_data->legendPos = pos; 356 break; 357 case QwtPlot::LeftLegend: 358 case QwtPlot::RightLegend: 359 if ( ratio <= 0.0 ) 360 ratio = 0.5; 361 d_data->legendRatio = ratio; 362 d_data->legendPos = pos; 363 break; 364 default: 365 break; 366 } 367 } 368 369 /*! 370 \brief Specify the position of the legend 371 \param pos The legend's position. Valid values are 372 \c QwtPlot::LeftLegend, \c QwtPlot::RightLegend, 373 \c QwtPlot::TopLegend, \c QwtPlot::BottomLegend. 374 375 \sa QwtPlot::setLegendPosition() 376 */ 377 void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos ) 378 { 379 setLegendPosition( pos, 0.0 ); 380 } 381 382 /*! 383 \return Position of the legend 384 \sa setLegendPosition(), QwtPlot::setLegendPosition(), 385 QwtPlot::legendPosition() 386 */ 387 QwtPlot::LegendPosition QwtPlotLayout::legendPosition() const 388 { 389 return d_data->legendPos; 390 } 391 392 /*! 393 Specify the relative size of the legend in the plot 394 \param ratio Ratio between legend and the bounding rectangle 395 of title, footer, canvas and axes. The legend will be shrunk 396 if it would need more space than the given ratio. 397 The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0 398 it will be reset to the default ratio. 399 The default vertical/horizontal ratio is 0.33/0.5. 400 */ 401 void QwtPlotLayout::setLegendRatio( double ratio ) 402 { 403 setLegendPosition( legendPosition(), ratio ); 404 } 405 406 /*! 407 \return The relative size of the legend in the plot. 408 \sa setLegendPosition() 409 */ 410 double QwtPlotLayout::legendRatio() const 411 { 412 return d_data->legendRatio; 413 } 414 415 /*! 416 \brief Set the geometry for the title 417 418 This method is intended to be used from derived layouts 419 overloading activate() 420 421 \sa titleRect(), activate() 422 */ 423 void QwtPlotLayout::setTitleRect( const QRectF &rect ) 424 { 425 d_data->titleRect = rect; 426 } 427 428 /*! 429 \return Geometry for the title 430 \sa activate(), invalidate() 431 */ 432 QRectF QwtPlotLayout::titleRect() const 433 { 434 return d_data->titleRect; 435 } 436 437 /*! 438 \brief Set the geometry for the footer 439 440 This method is intended to be used from derived layouts 441 overloading activate() 442 443 \sa footerRect(), activate() 444 */ 445 void QwtPlotLayout::setFooterRect( const QRectF &rect ) 446 { 447 d_data->footerRect = rect; 448 } 449 450 /*! 451 \return Geometry for the footer 452 \sa activate(), invalidate() 453 */ 454 QRectF QwtPlotLayout::footerRect() const 455 { 456 return d_data->footerRect; 457 } 458 459 /*! 460 \brief Set the geometry for the legend 461 462 This method is intended to be used from derived layouts 463 overloading activate() 464 465 \param rect Rectangle for the legend 466 467 \sa legendRect(), activate() 468 */ 469 void QwtPlotLayout::setLegendRect( const QRectF &rect ) 470 { 471 d_data->legendRect = rect; 472 } 473 474 /*! 475 \return Geometry for the legend 476 \sa activate(), invalidate() 477 */ 478 QRectF QwtPlotLayout::legendRect() const 479 { 480 return d_data->legendRect; 481 } 482 483 /*! 484 \brief Set the geometry for an axis 485 486 This method is intended to be used from derived layouts 487 overloading activate() 488 489 \param axis Axis index 490 \param rect Rectangle for the scale 491 492 \sa scaleRect(), activate() 493 */ 494 void QwtPlotLayout::setScaleRect( int axis, const QRectF &rect ) 495 { 496 if ( axis >= 0 && axis < QwtPlot::axisCnt ) 497 d_data->scaleRect[axis] = rect; 498 } 499 500 /*! 501 \param axis Axis index 502 \return Geometry for the scale 503 \sa activate(), invalidate() 504 */ 505 QRectF QwtPlotLayout::scaleRect( int axis ) const 506 { 507 if ( axis < 0 || axis >= QwtPlot::axisCnt ) 508 { 509 static QRectF dummyRect; 510 return dummyRect; 511 } 512 return d_data->scaleRect[axis]; 513 } 514 515 /*! 516 \brief Set the geometry for the canvas 517 518 This method is intended to be used from derived layouts 519 overloading activate() 520 521 \sa canvasRect(), activate() 522 */ 523 void QwtPlotLayout::setCanvasRect( const QRectF &rect ) 524 { 525 d_data->canvasRect = rect; 526 } 527 528 /*! 529 \return Geometry for the canvas 530 \sa activate(), invalidate() 531 */ 532 QRectF QwtPlotLayout::canvasRect() const 533 { 534 return d_data->canvasRect; 535 } 536 537 /*! 538 Invalidate the geometry of all components. 539 \sa activate() 540 */ 541 void QwtPlotLayout::invalidate() 542 { 543 d_data->titleRect = d_data->footerRect 544 = d_data->legendRect = d_data->canvasRect = QRect(); 545 546 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 547 d_data->scaleRect[axis] = QRect(); 548 } 549 550 /*! 551 \return Minimum size hint 552 \param plot Plot widget 553 554 \sa QwtPlot::minimumSizeHint() 555 */ 556 557 QSize QwtPlotLayout::minimumSizeHint( const QwtPlot *plot ) const 558 { 559 class ScaleData 560 { 561 public: 562 ScaleData() 563 { 564 w = h = minLeft = minRight = tickOffset = 0; 565 } 566 567 int w; 568 int h; 569 int minLeft; 570 int minRight; 571 int tickOffset; 572 } scaleData[QwtPlot::axisCnt]; 573 574 int canvasBorder[QwtPlot::axisCnt]; 575 576 int fw; 577 plot->canvas()->getContentsMargins( &fw, NULL, NULL, NULL ); 578 579 int axis; 580 for ( axis = 0; axis < QwtPlot::axisCnt; axis++ ) 581 { 582 if ( plot->axisEnabled( axis ) ) 583 { 584 const QwtScaleWidget *scl = plot->axisWidget( axis ); 585 ScaleData &sd = scaleData[axis]; 586 587 const QSize hint = scl->minimumSizeHint(); 588 sd.w = hint.width(); 589 sd.h = hint.height(); 590 scl->getBorderDistHint( sd.minLeft, sd.minRight ); 591 sd.tickOffset = scl->margin(); 592 if ( scl->scaleDraw()->hasComponent( QwtAbstractScaleDraw::Ticks ) ) 593 sd.tickOffset += qCeil( scl->scaleDraw()->maxTickLength() ); 594 } 595 596 canvasBorder[axis] = fw + d_data->canvasMargin[axis] + 1; 597 } 598 599 600 for ( axis = 0; axis < QwtPlot::axisCnt; axis++ ) 601 { 602 ScaleData &sd = scaleData[axis]; 603 if ( sd.w && ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop ) ) 604 { 605 if ( ( sd.minLeft > canvasBorder[QwtPlot::yLeft] ) 606 && scaleData[QwtPlot::yLeft].w ) 607 { 608 int shiftLeft = sd.minLeft - canvasBorder[QwtPlot::yLeft]; 609 if ( shiftLeft > scaleData[QwtPlot::yLeft].w ) 610 shiftLeft = scaleData[QwtPlot::yLeft].w; 611 612 sd.w -= shiftLeft; 613 } 614 if ( ( sd.minRight > canvasBorder[QwtPlot::yRight] ) 615 && scaleData[QwtPlot::yRight].w ) 616 { 617 int shiftRight = sd.minRight - canvasBorder[QwtPlot::yRight]; 618 if ( shiftRight > scaleData[QwtPlot::yRight].w ) 619 shiftRight = scaleData[QwtPlot::yRight].w; 620 621 sd.w -= shiftRight; 622 } 623 } 624 625 if ( sd.h && ( axis == QwtPlot::yLeft || axis == QwtPlot::yRight ) ) 626 { 627 if ( ( sd.minLeft > canvasBorder[QwtPlot::xBottom] ) && 628 scaleData[QwtPlot::xBottom].h ) 629 { 630 int shiftBottom = sd.minLeft - canvasBorder[QwtPlot::xBottom]; 631 if ( shiftBottom > scaleData[QwtPlot::xBottom].tickOffset ) 632 shiftBottom = scaleData[QwtPlot::xBottom].tickOffset; 633 634 sd.h -= shiftBottom; 635 } 636 if ( ( sd.minLeft > canvasBorder[QwtPlot::xTop] ) && 637 scaleData[QwtPlot::xTop].h ) 638 { 639 int shiftTop = sd.minRight - canvasBorder[QwtPlot::xTop]; 640 if ( shiftTop > scaleData[QwtPlot::xTop].tickOffset ) 641 shiftTop = scaleData[QwtPlot::xTop].tickOffset; 642 643 sd.h -= shiftTop; 644 } 645 } 646 } 647 648 const QWidget *canvas = plot->canvas(); 649 650 int left, top, right, bottom; 651 canvas->getContentsMargins( &left, &top, &right, &bottom ); 652 653 const QSize minCanvasSize = canvas->minimumSize(); 654 655 int w = scaleData[QwtPlot::yLeft].w + scaleData[QwtPlot::yRight].w; 656 int cw = qMax( scaleData[QwtPlot::xBottom].w, scaleData[QwtPlot::xTop].w ) 657 + left + 1 + right + 1; 658 w += qMax( cw, minCanvasSize.width() ); 659 660 int h = scaleData[QwtPlot::xBottom].h + scaleData[QwtPlot::xTop].h; 661 int ch = qMax( scaleData[QwtPlot::yLeft].h, scaleData[QwtPlot::yRight].h ) 662 + top + 1 + bottom + 1; 663 h += qMax( ch, minCanvasSize.height() ); 664 665 const QwtTextLabel *labels[2]; 666 labels[0] = plot->titleLabel(); 667 labels[1] = plot->footerLabel(); 668 669 for ( int i = 0; i < 2; i++ ) 670 { 671 const QwtTextLabel *label = labels[i]; 672 if ( label && !label->text().isEmpty() ) 673 { 674 // If only QwtPlot::yLeft or QwtPlot::yRight is showing, 675 // we center on the plot canvas. 676 const bool centerOnCanvas = !( plot->axisEnabled( QwtPlot::yLeft ) 677 && plot->axisEnabled( QwtPlot::yRight ) ); 678 679 int labelW = w; 680 if ( centerOnCanvas ) 681 { 682 labelW -= scaleData[QwtPlot::yLeft].w 683 + scaleData[QwtPlot::yRight].w; 684 } 685 686 int labelH = label->heightForWidth( labelW ); 687 if ( labelH > labelW ) // Compensate for a long title 688 { 689 w = labelW = labelH; 690 if ( centerOnCanvas ) 691 { 692 w += scaleData[QwtPlot::yLeft].w 693 + scaleData[QwtPlot::yRight].w; 694 } 695 696 labelH = label->heightForWidth( labelW ); 697 } 698 h += labelH + d_data->spacing; 699 } 700 } 701 702 // Compute the legend contribution 703 704 const QwtAbstractLegend *legend = plot->legend(); 705 if ( legend && !legend->isEmpty() ) 706 { 707 if ( d_data->legendPos == QwtPlot::LeftLegend 708 || d_data->legendPos == QwtPlot::RightLegend ) 709 { 710 int legendW = legend->sizeHint().width(); 711 int legendH = legend->heightForWidth( legendW ); 712 713 if ( legend->frameWidth() > 0 ) 714 w += d_data->spacing; 715 716 if ( legendH > h ) 717 legendW += legend->scrollExtent( Qt::Horizontal ); 718 719 if ( d_data->legendRatio < 1.0 ) 720 legendW = qMin( legendW, int( w / ( 1.0 - d_data->legendRatio ) ) ); 721 722 w += legendW + d_data->spacing; 723 } 724 else // QwtPlot::Top, QwtPlot::Bottom 725 { 726 int legendW = qMin( legend->sizeHint().width(), w ); 727 int legendH = legend->heightForWidth( legendW ); 728 729 if ( legend->frameWidth() > 0 ) 730 h += d_data->spacing; 731 732 if ( d_data->legendRatio < 1.0 ) 733 legendH = qMin( legendH, int( h / ( 1.0 - d_data->legendRatio ) ) ); 734 735 h += legendH + d_data->spacing; 736 } 737 } 738 739 return QSize( w, h ); 740 } 741 742 /*! 743 Find the geometry for the legend 744 745 \param options Options how to layout the legend 746 \param rect Rectangle where to place the legend 747 748 \return Geometry for the legend 749 \sa Options 750 */ 751 752 QRectF QwtPlotLayout::layoutLegend( Options options, 753 const QRectF &rect ) const 754 { 755 const QSize hint( d_data->layoutData.legend.hint ); 756 381 } 382 } 383 384 namespace 385 { 386 class LayoutEngine 387 { 388 public: 389 struct Dimensions 390 { 391 Dimensions() 392 { 393 dimTitle = dimFooter = 0; 394 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ ) 395 m_dimAxes[axisPos] = 0; 396 } 397 398 inline int dimAxis( QwtAxisId axisId ) const 399 { 400 return m_dimAxes[ axisId ]; 401 } 402 403 void setDimAxis( QwtAxisId axisId, int dim ) 404 { 405 m_dimAxes[ axisId ] = dim; 406 } 407 408 inline int dimAxes( int axisPos ) const 409 { 410 return m_dimAxes[ axisPos ]; 411 } 412 413 inline int dimYAxes() const 414 { 415 return dimAxes( QwtAxis::YLeft ) + dimAxes( QwtAxis::YRight ); 416 } 417 418 inline int dimXAxes() const 419 { 420 return dimAxes( QwtAxis::XTop ) + dimAxes( QwtAxis::XBottom ); 421 } 422 423 inline QRectF centered( const QRectF& rect, const QRectF& labelRect ) const 424 { 425 QRectF r = labelRect; 426 r.setX( rect.left() + dimAxes( QwtAxis::YLeft ) ); 427 r.setWidth( rect.width() - dimYAxes() ); 428 429 return r; 430 } 431 432 inline QRectF innerRect( const QRectF& rect ) const 433 { 434 QRectF r( 435 rect.x() + dimAxes( QwtAxis::YLeft ), 436 rect.y() + dimAxes( QwtAxis::XTop ), 437 rect.width() - dimYAxes(), 438 rect.height() - dimXAxes() ); 439 440 if ( r.width() < 0 ) 441 { 442 r.setX( rect.center().x() ); 443 r.setWidth( 0 ); 444 } 445 if ( r.height() < 0 ) 446 { 447 r.setY( rect.center().y() ); 448 r.setHeight( 0 ); 449 } 450 451 return r; 452 } 453 454 int dimTitle; 455 int dimFooter; 456 457 private: 458 int m_dimAxes[QwtAxis::AxisPositions]; 459 }; 460 461 LayoutEngine() 462 : m_legendPos( QwtPlot::BottomLegend ) 463 , m_legendRatio( 1.0 ) 464 , m_spacing( 5 ) 465 { 466 } 467 468 QRectF layoutLegend( QwtPlotLayout::Options, 469 const LayoutData::LegendData&, const QRectF&, const QSize& ) const; 470 471 QRectF alignLegend( const QSize& legendHint, 472 const QRectF& canvasRect, const QRectF& legendRect ) const; 473 474 void alignScales( QwtPlotLayout::Options, 475 const LayoutData&, QRectF& canvasRect, 476 QRectF scaleRect[QwtAxis::AxisPositions] ) const; 477 478 Dimensions layoutDimensions( QwtPlotLayout::Options, 479 const LayoutData&, const QRectF& ) const; 480 481 inline void setSpacing( unsigned int spacing ) { m_spacing = spacing; } 482 inline unsigned int spacing() const { return m_spacing; } 483 484 inline void setAlignCanvas( int axisPos, bool on ) { m_alignCanvas[ axisPos ] = on; } 485 inline bool alignCanvas( int axisPos ) const { return m_alignCanvas[ axisPos ]; } 486 487 inline void setCanvasMargin( int axisPos, int margin ) { m_canvasMargin[ axisPos ] = margin; } 488 inline int canvasMargin( int axisPos ) const { return m_canvasMargin[ axisPos ]; } 489 490 inline void setLegendPos( QwtPlot::LegendPosition pos ) { m_legendPos = pos; } 491 inline QwtPlot::LegendPosition legendPos() const { return m_legendPos; } 492 493 inline void setLegendRatio( double ratio ) { m_legendRatio = ratio; } 494 inline double legendRatio() const { return m_legendRatio; } 495 496 private: 497 int heightForWidth( LayoutData::Label, const LayoutData&, 498 QwtPlotLayout::Options, double width, int axesWidth ) const; 499 500 QwtPlot::LegendPosition m_legendPos; 501 double m_legendRatio; 502 503 unsigned int m_canvasMargin[QwtAxis::AxisPositions]; 504 bool m_alignCanvas[QwtAxis::AxisPositions]; 505 506 unsigned int m_spacing; 507 }; 508 } 509 510 QRectF LayoutEngine::layoutLegend( QwtPlotLayout::Options options, 511 const LayoutData::LegendData& legendData, 512 const QRectF& rect, const QSize& legendHint ) const 513 { 757 514 int dim; 758 if ( d_data->legendPos == QwtPlot::LeftLegend759 || d_data->legendPos == QwtPlot::RightLegend )515 if ( m_legendPos == QwtPlot::LeftLegend 516 || m_legendPos == QwtPlot::RightLegend ) 760 517 { 761 518 // We don't allow vertical legends to take more than 762 519 // half of the available space. 763 520 764 dim = qMin( hint.width(), int( rect.width() *d_data->legendRatio ) );765 766 if ( !( options & IgnoreScrollbars ) ) 767 { 768 if ( hint.height() > rect.height() )521 dim = qMin( legendHint.width(), int( rect.width() * m_legendRatio ) ); 522 523 if ( !( options & QwtPlotLayout::IgnoreScrollbars ) ) 524 { 525 if ( legendHint.height() > rect.height() ) 769 526 { 770 527 // The legend will need additional 771 528 // space for the vertical scrollbar. 772 529 773 dim += d_data->layoutData.legend.hScrollExtent;530 dim += legendData.hScrollExtent; 774 531 } 775 532 } … … 777 534 else 778 535 { 779 dim = qMin( hint.height(), int( rect.height() *d_data->legendRatio ) );780 dim = qMax( dim, d_data->layoutData.legend.vScrollExtent );536 dim = qMin( legendHint.height(), int( rect.height() * m_legendRatio ) ); 537 dim = qMax( dim, legendData.vScrollExtent ); 781 538 } 782 539 783 540 QRectF legendRect = rect; 784 switch ( d_data->legendPos )541 switch ( m_legendPos ) 785 542 { 786 543 case QwtPlot::LeftLegend: 544 { 787 545 legendRect.setWidth( dim ); 788 546 break; 547 } 789 548 case QwtPlot::RightLegend: 549 { 790 550 legendRect.setX( rect.right() - dim ); 791 551 legendRect.setWidth( dim ); 792 552 break; 553 } 793 554 case QwtPlot::TopLegend: 555 { 794 556 legendRect.setHeight( dim ); 795 557 break; 558 } 796 559 case QwtPlot::BottomLegend: 560 { 797 561 legendRect.setY( rect.bottom() - dim ); 798 562 legendRect.setHeight( dim ); 799 563 break; 564 } 800 565 } 801 566 … … 803 568 } 804 569 805 /*! 806 Align the legend to the canvas 807 808 \param canvasRect Geometry of the canvas 809 \param legendRect Maximum geometry for the legend 810 811 \return Geometry for the aligned legend 812 */ 813 QRectF QwtPlotLayout::alignLegend( const QRectF &canvasRect, 814 const QRectF &legendRect ) const 570 QRectF LayoutEngine::alignLegend( const QSize& legendHint, 571 const QRectF& canvasRect, const QRectF& legendRect ) const 815 572 { 816 573 QRectF alignedRect = legendRect; 817 574 818 if ( d_data->legendPos == QwtPlot::BottomLegend819 || d_data->legendPos == QwtPlot::TopLegend )820 { 821 if ( d_data->layoutData.legend.hint.width() < canvasRect.width() )575 if ( m_legendPos == QwtPlot::BottomLegend 576 || m_legendPos == QwtPlot::TopLegend ) 577 { 578 if ( legendHint.width() < canvasRect.width() ) 822 579 { 823 580 alignedRect.setX( canvasRect.x() ); … … 827 584 else 828 585 { 829 if ( d_data->layoutData.legend.hint.height() < canvasRect.height() )586 if ( legendHint.height() < canvasRect.height() ) 830 587 { 831 588 alignedRect.setY( canvasRect.y() ); … … 837 594 } 838 595 839 /*! 840 Expand all line breaks in text labels, and calculate the height 841 of their widgets in orientation of the text. 842 843 \param options Options how to layout the legend 844 \param rect Bounding rectangle for title, footer, axes and canvas. 845 \param dimTitle Expanded height of the title widget 846 \param dimFooter Expanded height of the footer widget 847 \param dimAxes Expanded heights of the axis in axis orientation. 848 849 \sa Options 850 */ 851 void QwtPlotLayout::expandLineBreaks( Options options, const QRectF &rect, 852 int &dimTitle, int &dimFooter, int dimAxes[QwtPlot::axisCnt] ) const 853 { 854 dimTitle = dimFooter = 0; 855 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 856 dimAxes[axis] = 0; 857 858 int backboneOffset[QwtPlot::axisCnt]; 859 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 860 { 861 backboneOffset[axis] = 0; 862 if ( !( options & IgnoreFrames ) ) 863 backboneOffset[axis] += d_data->layoutData.canvas.contentsMargins[ axis ]; 864 865 if ( !d_data->alignCanvasToScales[axis] ) 866 backboneOffset[axis] += d_data->canvasMargin[axis]; 596 int LayoutEngine::heightForWidth( 597 LayoutData::Label labelType, const LayoutData& layoutData, 598 QwtPlotLayout::Options options, 599 double width, int axesWidth ) const 600 { 601 const LayoutData::LabelData& labelData = layoutData.labelData[ labelType ]; 602 603 if ( labelData.text.isEmpty() ) 604 return 0; 605 606 double w = width; 607 608 if ( !layoutData.hasSymmetricYAxes() ) 609 { 610 // center to the canvas 611 w -= axesWidth; 612 } 613 614 int d = qwtCeil( labelData.text.heightForWidth( w ) ); 615 if ( !( options & QwtPlotLayout::IgnoreFrames ) ) 616 d += 2 * labelData.frameWidth; 617 618 return d; 619 } 620 621 LayoutEngine::Dimensions LayoutEngine::layoutDimensions( QwtPlotLayout::Options options, 622 const LayoutData& layoutData, const QRectF& rect ) const 623 { 624 using namespace QwtAxis; 625 626 Dimensions dimensions; 627 628 int backboneOffset[AxisPositions]; 629 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ ) 630 { 631 backboneOffset[axisPos] = 0; 632 if ( !( options & QwtPlotLayout::IgnoreFrames ) ) 633 backboneOffset[axisPos] += layoutData.canvasData.contentsMargins[axisPos]; 634 635 if ( !m_alignCanvas[axisPos] ) 636 backboneOffset[axisPos] += m_canvasMargin[axisPos]; 867 637 } 868 638 … … 880 650 // axis ... . So we loop as long until no size changes. 881 651 882 if ( !( ( options & IgnoreTitle ) || 883 d_data->layoutData.title.text.isEmpty() ) ) 884 { 885 double w = rect.width(); 886 887 if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled 888 != d_data->layoutData.scale[QwtPlot::yRight].isEnabled ) 889 { 890 // center to the canvas 891 w -= dimAxes[QwtPlot::yLeft] + dimAxes[QwtPlot::yRight]; 892 } 893 894 int d = qCeil( d_data->layoutData.title.text.heightForWidth( w ) ); 895 if ( !( options & IgnoreFrames ) ) 896 d += 2 * d_data->layoutData.title.frameWidth; 897 898 if ( d > dimTitle ) 899 { 900 dimTitle = d; 652 if ( !( options & QwtPlotLayout::IgnoreTitle ) ) 653 { 654 const int d = heightForWidth( 655 LayoutData::Title, layoutData, options, 656 rect.width(), dimensions.dimYAxes() ); 657 658 if ( d > dimensions.dimTitle ) 659 { 660 dimensions.dimTitle = d; 901 661 done = false; 902 662 } 903 663 } 904 664 905 if ( !( ( options & IgnoreFooter ) || 906 d_data->layoutData.footer.text.isEmpty() ) ) 907 { 908 double w = rect.width(); 909 910 if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled 911 != d_data->layoutData.scale[QwtPlot::yRight].isEnabled ) 912 { 913 // center to the canvas 914 w -= dimAxes[QwtPlot::yLeft] + dimAxes[QwtPlot::yRight]; 915 } 916 917 int d = qCeil( d_data->layoutData.footer.text.heightForWidth( w ) ); 918 if ( !( options & IgnoreFrames ) ) 919 d += 2 * d_data->layoutData.footer.frameWidth; 920 921 if ( d > dimFooter ) 922 { 923 dimFooter = d; 665 if ( !( options & QwtPlotLayout::IgnoreFooter ) ) 666 { 667 const int d = heightForWidth( 668 LayoutData::Footer, layoutData, options, 669 rect.width(), dimensions.dimYAxes() ); 670 671 if ( d > dimensions.dimFooter ) 672 { 673 dimensions.dimFooter = d; 924 674 done = false; 925 675 } 926 676 } 927 677 928 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 929 { 930 const struct LayoutData::t_scaleData &scaleData = 931 d_data->layoutData.scale[axis]; 932 933 if ( scaleData.isEnabled ) 934 { 935 double length; 936 if ( axis == QwtPlot::xTop || axis == QwtPlot::xBottom ) 937 { 938 length = rect.width() - dimAxes[QwtPlot::yLeft] 939 - dimAxes[QwtPlot::yRight]; 940 length -= scaleData.start + scaleData.end; 941 942 if ( dimAxes[QwtPlot::yRight] > 0 ) 678 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ ) 679 { 680 { 681 const QwtAxisId axisId( axisPos ); 682 683 const LayoutData::ScaleData& scaleData = layoutData.axisData( axisId ); 684 685 if ( scaleData.isVisible ) 686 { 687 double length; 688 if ( isXAxis( axisPos ) ) 689 { 690 length = rect.width() - dimensions.dimYAxes(); 691 length -= scaleData.start + scaleData.end; 692 693 if ( dimensions.dimAxes( YRight ) > 0 ) 694 length -= 1; 695 696 length += qMin( dimensions.dimAxes( YLeft ), 697 scaleData.start - backboneOffset[YLeft] ); 698 699 length += qMin( dimensions.dimAxes( YRight ), 700 scaleData.end - backboneOffset[YRight] ); 701 } 702 else // y axis 703 { 704 length = rect.height() - dimensions.dimXAxes(); 705 length -= scaleData.start + scaleData.end; 943 706 length -= 1; 944 707 945 length += qMin( dimAxes[QwtPlot::yLeft], 946 scaleData.start - backboneOffset[QwtPlot::yLeft] ); 947 length += qMin( dimAxes[QwtPlot::yRight], 948 scaleData.end - backboneOffset[QwtPlot::yRight] ); 949 } 950 else // QwtPlot::yLeft, QwtPlot::yRight 951 { 952 length = rect.height() - dimAxes[QwtPlot::xTop] 953 - dimAxes[QwtPlot::xBottom]; 954 length -= scaleData.start + scaleData.end; 955 length -= 1; 956 957 if ( dimAxes[QwtPlot::xBottom] <= 0 ) 958 length -= 1; 959 if ( dimAxes[QwtPlot::xTop] <= 0 ) 960 length -= 1; 961 962 if ( dimAxes[QwtPlot::xBottom] > 0 ) 963 { 964 length += qMin( 965 d_data->layoutData.scale[QwtPlot::xBottom].tickOffset, 966 double( scaleData.start - backboneOffset[QwtPlot::xBottom] ) ); 967 } 968 if ( dimAxes[QwtPlot::xTop] > 0 ) 969 { 970 length += qMin( 971 d_data->layoutData.scale[QwtPlot::xTop].tickOffset, 972 double( scaleData.end - backboneOffset[QwtPlot::xTop] ) ); 973 } 974 975 if ( dimTitle > 0 ) 976 length -= dimTitle + d_data->spacing; 977 } 978 979 int d = scaleData.dimWithoutTitle; 980 if ( !scaleData.scaleWidget->title().isEmpty() ) 981 { 982 d += scaleData.scaleWidget->titleHeightForWidth( qFloor( length ) ); 983 } 984 985 986 if ( d > dimAxes[axis] ) 987 { 988 dimAxes[axis] = d; 989 done = false; 990 } 991 } 992 } 993 } 994 } 995 996 /*! 997 Align the ticks of the axis to the canvas borders using 998 the empty corners. 999 1000 \param options Layout options 1001 \param canvasRect Geometry of the canvas ( IN/OUT ) 1002 \param scaleRect Geometries of the scales ( IN/OUT ) 1003 1004 \sa Options 1005 */ 1006 1007 void QwtPlotLayout::alignScales( Options options, 1008 QRectF &canvasRect, QRectF scaleRect[QwtPlot::axisCnt] ) const 1009 { 1010 int backboneOffset[QwtPlot::axisCnt]; 1011 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 1012 { 1013 backboneOffset[axis] = 0; 1014 1015 if ( !d_data->alignCanvasToScales[axis] ) 1016 { 1017 backboneOffset[axis] += d_data->canvasMargin[axis]; 1018 } 1019 1020 if ( !( options & IgnoreFrames ) ) 1021 { 1022 backboneOffset[axis] += 1023 d_data->layoutData.canvas.contentsMargins[axis]; 1024 } 1025 } 1026 1027 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 1028 { 1029 if ( !scaleRect[axis].isValid() ) 1030 continue; 1031 1032 const int startDist = d_data->layoutData.scale[axis].start; 1033 const int endDist = d_data->layoutData.scale[axis].end; 1034 1035 QRectF &axisRect = scaleRect[axis]; 1036 1037 if ( axis == QwtPlot::xTop || axis == QwtPlot::xBottom ) 1038 { 1039 const QRectF &leftScaleRect = scaleRect[QwtPlot::yLeft]; 1040 const int leftOffset = 1041 backboneOffset[QwtPlot::yLeft] - startDist; 1042 1043 if ( leftScaleRect.isValid() ) 1044 { 1045 const double dx = leftOffset + leftScaleRect.width(); 1046 if ( d_data->alignCanvasToScales[QwtPlot::yLeft] && dx < 0.0 ) 1047 { 1048 /* 1049 The axis needs more space than the width 1050 of the left scale. 1051 */ 1052 const double cLeft = canvasRect.left(); // qreal -> double 1053 canvasRect.setLeft( qMax( cLeft, axisRect.left() - dx ) ); 708 if ( dimensions.dimAxes( XBottom ) <= 0 ) 709 length -= 1; 710 711 if ( dimensions.dimAxes( XTop ) <= 0 ) 712 length -= 1; 713 714 /* 715 The tick labels of the y axes are always left/right from the 716 backbone/ticks of the x axes - but we have to take care, 717 that the labels don't overlap. 718 */ 719 if ( dimensions.dimAxes( XBottom ) > 0 ) 720 { 721 length += qMin( 722 layoutData.tickOffset( XBottom ), 723 double( scaleData.start - backboneOffset[XBottom] ) ); 724 } 725 726 if ( dimensions.dimAxes( XTop ) > 0 ) 727 { 728 length += qMin( 729 layoutData.tickOffset( XTop ), 730 double( scaleData.end - backboneOffset[XTop] ) ); 731 } 732 733 if ( dimensions.dimTitle > 0 ) 734 length -= dimensions.dimTitle + m_spacing; 735 } 736 737 int d = scaleData.dimWithoutTitle; 738 if ( !scaleData.scaleWidget->title().isEmpty() ) 739 { 740 d += scaleData.scaleWidget->titleHeightForWidth( qwtFloor( length ) ); 741 } 742 743 744 if ( d > dimensions.dimAxis( axisId ) ) 745 { 746 dimensions.setDimAxis( axisId, d ); 747 done = false; 748 } 749 } 750 } 751 } 752 } 753 754 return dimensions; 755 } 756 757 void LayoutEngine::alignScales( QwtPlotLayout::Options options, 758 const LayoutData& layoutData, QRectF& canvasRect, 759 QRectF scaleRect[QwtAxis::AxisPositions] ) const 760 { 761 using namespace QwtAxis; 762 763 int backboneOffset[AxisPositions]; 764 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ ) 765 { 766 backboneOffset[axisPos] = 0; 767 768 if ( !m_alignCanvas[axisPos] ) 769 { 770 backboneOffset[axisPos] += m_canvasMargin[axisPos]; 771 } 772 773 if ( !( options & QwtPlotLayout::IgnoreFrames ) ) 774 { 775 backboneOffset[axisPos] += 776 layoutData.canvasData.contentsMargins[axisPos]; 777 } 778 } 779 780 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ ) 781 { 782 { 783 QRectF& axisRect = scaleRect[axisPos]; 784 if ( !axisRect.isValid() ) 785 continue; 786 787 const QwtAxisId axisId( axisPos ); 788 789 const int startDist = layoutData.axisData( axisId ).start; 790 const int endDist = layoutData.axisData( axisId ).end; 791 792 if ( isXAxis( axisPos ) ) 793 { 794 const QRectF& leftScaleRect = scaleRect[YLeft]; 795 const int leftOffset = backboneOffset[YLeft] - startDist; 796 797 if ( leftScaleRect.isValid() ) 798 { 799 const double dx = leftOffset + leftScaleRect.width(); 800 if ( m_alignCanvas[YLeft] && dx < 0.0 ) 801 { 802 /* 803 The axis needs more space than the width 804 of the left scale. 805 */ 806 const double cLeft = canvasRect.left(); // qreal -> double 807 canvasRect.setLeft( qwtMaxF( cLeft, axisRect.left() - dx ) ); 808 } 809 else 810 { 811 const double minLeft = leftScaleRect.left(); 812 const double left = axisRect.left() + leftOffset; 813 axisRect.setLeft( qwtMaxF( left, minLeft ) ); 814 } 1054 815 } 1055 816 else 1056 817 { 1057 const double minLeft = leftScaleRect.left(); 1058 const double left = axisRect.left() + leftOffset; 1059 axisRect.setLeft( qMax( left, minLeft ) ); 818 if ( m_alignCanvas[YLeft] && leftOffset < 0 ) 819 { 820 canvasRect.setLeft( qwtMaxF( canvasRect.left(), 821 axisRect.left() - leftOffset ) ); 822 } 823 else 824 { 825 if ( leftOffset > 0 ) 826 axisRect.setLeft( axisRect.left() + leftOffset ); 827 } 828 } 829 830 const QRectF& rightScaleRect = scaleRect[YRight]; 831 const int rightOffset = backboneOffset[YRight] - endDist + 1; 832 833 if ( rightScaleRect.isValid() ) 834 { 835 const double dx = rightOffset + rightScaleRect.width(); 836 if ( m_alignCanvas[YRight] && dx < 0 ) 837 { 838 /* 839 The axis needs more space than the width 840 of the right scale. 841 */ 842 const double cRight = canvasRect.right(); // qreal -> double 843 canvasRect.setRight( qwtMinF( cRight, axisRect.right() + dx ) ); 844 } 845 846 const double maxRight = rightScaleRect.right(); 847 const double right = axisRect.right() - rightOffset; 848 axisRect.setRight( qwtMinF( right, maxRight ) ); 849 } 850 else 851 { 852 if ( m_alignCanvas[YRight] && rightOffset < 0 ) 853 { 854 canvasRect.setRight( qwtMinF( canvasRect.right(), 855 axisRect.right() + rightOffset ) ); 856 } 857 else 858 { 859 if ( rightOffset > 0 ) 860 axisRect.setRight( axisRect.right() - rightOffset ); 861 } 862 } 863 } 864 else // y axes 865 { 866 const QRectF& bottomScaleRect = scaleRect[XBottom]; 867 const int bottomOffset = backboneOffset[XBottom] - endDist + 1; 868 869 if ( bottomScaleRect.isValid() ) 870 { 871 const double dy = bottomOffset + bottomScaleRect.height(); 872 if ( m_alignCanvas[XBottom] && dy < 0 ) 873 { 874 /* 875 The axis needs more space than the height 876 of the bottom scale. 877 */ 878 const double cBottom = canvasRect.bottom(); // qreal -> double 879 canvasRect.setBottom( qwtMinF( cBottom, axisRect.bottom() + dy ) ); 880 } 881 else 882 { 883 const double maxBottom = bottomScaleRect.top() + 884 layoutData.tickOffset( XBottom ); 885 const double bottom = axisRect.bottom() - bottomOffset; 886 axisRect.setBottom( qwtMinF( bottom, maxBottom ) ); 887 } 888 } 889 else 890 { 891 if ( m_alignCanvas[XBottom] && bottomOffset < 0 ) 892 { 893 canvasRect.setBottom( qwtMinF( canvasRect.bottom(), 894 axisRect.bottom() + bottomOffset ) ); 895 } 896 else 897 { 898 if ( bottomOffset > 0 ) 899 axisRect.setBottom( axisRect.bottom() - bottomOffset ); 900 } 901 } 902 903 const QRectF& topScaleRect = scaleRect[XTop]; 904 const int topOffset = backboneOffset[XTop] - startDist; 905 906 if ( topScaleRect.isValid() ) 907 { 908 const double dy = topOffset + topScaleRect.height(); 909 if ( m_alignCanvas[XTop] && dy < 0 ) 910 { 911 /* 912 The axis needs more space than the height 913 of the top scale. 914 */ 915 const double cTop = canvasRect.top(); // qreal -> double 916 canvasRect.setTop( qwtMaxF( cTop, axisRect.top() - dy ) ); 917 } 918 else 919 { 920 const double minTop = topScaleRect.bottom() - 921 layoutData.tickOffset( XTop ); 922 923 const double top = axisRect.top() + topOffset; 924 axisRect.setTop( qwtMaxF( top, minTop ) ); 925 } 926 } 927 else 928 { 929 if ( m_alignCanvas[XTop] && topOffset < 0 ) 930 { 931 canvasRect.setTop( qwtMaxF( canvasRect.top(), 932 axisRect.top() - topOffset ) ); 933 } 934 else 935 { 936 if ( topOffset > 0 ) 937 axisRect.setTop( axisRect.top() + topOffset ); 938 } 939 } 940 } 941 } 942 } 943 944 /* 945 The canvas has been aligned to the scale with largest 946 border distances. Now we have to realign the other scale. 947 */ 948 949 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ ) 950 { 951 { 952 const QwtAxisId axisId( axisPos ); 953 954 QRectF& sRect = scaleRect[axisPos]; 955 const LayoutData::ScaleData& axisData = layoutData.axisData( axisId ); 956 957 if ( !sRect.isValid() ) 958 continue; 959 960 if ( isXAxis( axisPos ) ) 961 { 962 if ( m_alignCanvas[YLeft] ) 963 { 964 double y = canvasRect.left() - axisData.start; 965 if ( !( options & QwtPlotLayout::IgnoreFrames ) ) 966 y += layoutData.canvasData.contentsMargins[YLeft]; 967 968 sRect.setLeft( y ); 969 } 970 971 if ( m_alignCanvas[YRight] ) 972 { 973 double y = canvasRect.right() - 1 + axisData.end; 974 if ( !( options & QwtPlotLayout::IgnoreFrames ) ) 975 y -= layoutData.canvasData.contentsMargins[YRight]; 976 977 sRect.setRight( y ); 978 } 979 980 if ( m_alignCanvas[axisPos] ) 981 { 982 if ( axisPos == XTop ) 983 sRect.setBottom( canvasRect.top() ); 984 else 985 sRect.setTop( canvasRect.bottom() ); 1060 986 } 1061 987 } 1062 988 else 1063 989 { 1064 if ( d_data->alignCanvasToScales[QwtPlot::yLeft] && leftOffset < 0 ) 1065 { 1066 canvasRect.setLeft( qMax( canvasRect.left(), 1067 axisRect.left() - leftOffset ) ); 1068 } 1069 else 1070 { 1071 if ( leftOffset > 0 ) 1072 axisRect.setLeft( axisRect.left() + leftOffset ); 1073 } 1074 } 1075 1076 const QRectF &rightScaleRect = scaleRect[QwtPlot::yRight]; 1077 const int rightOffset = 1078 backboneOffset[QwtPlot::yRight] - endDist + 1; 1079 1080 if ( rightScaleRect.isValid() ) 1081 { 1082 const double dx = rightOffset + rightScaleRect.width(); 1083 if ( d_data->alignCanvasToScales[QwtPlot::yRight] && dx < 0 ) 1084 { 1085 /* 1086 The axis needs more space than the width 1087 of the right scale. 1088 */ 1089 const double cRight = canvasRect.right(); // qreal -> double 1090 canvasRect.setRight( qMin( cRight, axisRect.right() + dx ) ); 1091 } 1092 1093 const double maxRight = rightScaleRect.right(); 1094 const double right = axisRect.right() - rightOffset; 1095 axisRect.setRight( qMin( right, maxRight ) ); 1096 } 1097 else 1098 { 1099 if ( d_data->alignCanvasToScales[QwtPlot::yRight] && rightOffset < 0 ) 1100 { 1101 canvasRect.setRight( qMin( canvasRect.right(), 1102 axisRect.right() + rightOffset ) ); 1103 } 1104 else 1105 { 1106 if ( rightOffset > 0 ) 1107 axisRect.setRight( axisRect.right() - rightOffset ); 1108 } 1109 } 1110 } 1111 else // QwtPlot::yLeft, QwtPlot::yRight 1112 { 1113 const QRectF &bottomScaleRect = scaleRect[QwtPlot::xBottom]; 1114 const int bottomOffset = 1115 backboneOffset[QwtPlot::xBottom] - endDist + 1; 1116 1117 if ( bottomScaleRect.isValid() ) 1118 { 1119 const double dy = bottomOffset + bottomScaleRect.height(); 1120 if ( d_data->alignCanvasToScales[QwtPlot::xBottom] && dy < 0 ) 1121 { 1122 /* 1123 The axis needs more space than the height 1124 of the bottom scale. 1125 */ 1126 const double cBottom = canvasRect.bottom(); // qreal -> double 1127 canvasRect.setBottom( qMin( cBottom, axisRect.bottom() + dy ) ); 1128 } 1129 else 1130 { 1131 const double maxBottom = bottomScaleRect.top() + 1132 d_data->layoutData.scale[QwtPlot::xBottom].tickOffset; 1133 const double bottom = axisRect.bottom() - bottomOffset; 1134 axisRect.setBottom( qMin( bottom, maxBottom ) ); 1135 } 1136 } 1137 else 1138 { 1139 if ( d_data->alignCanvasToScales[QwtPlot::xBottom] && bottomOffset < 0 ) 1140 { 1141 canvasRect.setBottom( qMin( canvasRect.bottom(), 1142 axisRect.bottom() + bottomOffset ) ); 1143 } 1144 else 1145 { 1146 if ( bottomOffset > 0 ) 1147 axisRect.setBottom( axisRect.bottom() - bottomOffset ); 1148 } 1149 } 1150 1151 const QRectF &topScaleRect = scaleRect[QwtPlot::xTop]; 1152 const int topOffset = backboneOffset[QwtPlot::xTop] - startDist; 1153 1154 if ( topScaleRect.isValid() ) 1155 { 1156 const double dy = topOffset + topScaleRect.height(); 1157 if ( d_data->alignCanvasToScales[QwtPlot::xTop] && dy < 0 ) 1158 { 1159 /* 1160 The axis needs more space than the height 1161 of the top scale. 1162 */ 1163 const double cTop = canvasRect.top(); // qreal -> double 1164 canvasRect.setTop( qMax( cTop, axisRect.top() - dy ) ); 1165 } 1166 else 1167 { 1168 const double minTop = topScaleRect.bottom() - 1169 d_data->layoutData.scale[QwtPlot::xTop].tickOffset; 1170 const double top = axisRect.top() + topOffset; 1171 axisRect.setTop( qMax( top, minTop ) ); 1172 } 1173 } 1174 else 1175 { 1176 if ( d_data->alignCanvasToScales[QwtPlot::xTop] && topOffset < 0 ) 1177 { 1178 canvasRect.setTop( qMax( canvasRect.top(), 1179 axisRect.top() - topOffset ) ); 1180 } 1181 else 1182 { 1183 if ( topOffset > 0 ) 1184 axisRect.setTop( axisRect.top() + topOffset ); 1185 } 1186 } 1187 } 1188 } 1189 1190 /* 1191 The canvas has been aligned to the scale with largest 1192 border distances. Now we have to realign the other scale. 1193 */ 1194 1195 1196 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 1197 { 1198 QRectF &sRect = scaleRect[axis]; 1199 1200 if ( !sRect.isValid() ) 1201 continue; 1202 1203 if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop ) 1204 { 1205 if ( d_data->alignCanvasToScales[QwtPlot::yLeft] ) 1206 { 1207 double y = canvasRect.left() - d_data->layoutData.scale[axis].start; 1208 if ( !( options & IgnoreFrames ) ) 1209 y += d_data->layoutData.canvas.contentsMargins[ QwtPlot::yLeft ]; 1210 1211 sRect.setLeft( y ); 1212 } 1213 if ( d_data->alignCanvasToScales[QwtPlot::yRight] ) 1214 { 1215 double y = canvasRect.right() - 1 + d_data->layoutData.scale[axis].end; 1216 if ( !( options & IgnoreFrames ) ) 1217 y -= d_data->layoutData.canvas.contentsMargins[ QwtPlot::yRight ]; 1218 1219 sRect.setRight( y ); 1220 } 1221 1222 if ( d_data->alignCanvasToScales[ axis ] ) 1223 { 1224 if ( axis == QwtPlot::xTop ) 1225 sRect.setBottom( canvasRect.top() ); 1226 else 1227 sRect.setTop( canvasRect.bottom() ); 1228 } 990 if ( m_alignCanvas[XTop] ) 991 { 992 double x = canvasRect.top() - axisData.start; 993 if ( !( options & QwtPlotLayout::IgnoreFrames ) ) 994 x += layoutData.canvasData.contentsMargins[XTop]; 995 996 sRect.setTop( x ); 997 } 998 999 if ( m_alignCanvas[XBottom] ) 1000 { 1001 double x = canvasRect.bottom() - 1 + axisData.end; 1002 if ( !( options & QwtPlotLayout::IgnoreFrames ) ) 1003 x -= layoutData.canvasData.contentsMargins[XBottom]; 1004 1005 sRect.setBottom( x ); 1006 } 1007 1008 if ( m_alignCanvas[axisPos] ) 1009 { 1010 if ( axisPos == YLeft ) 1011 sRect.setRight( canvasRect.left() ); 1012 else 1013 sRect.setLeft( canvasRect.right() ); 1014 } 1015 } 1016 } 1017 } 1018 } 1019 1020 class QwtPlotLayout::PrivateData 1021 { 1022 public: 1023 QRectF titleRect; 1024 QRectF footerRect; 1025 QRectF legendRect; 1026 QRectF scaleRects[QwtAxis::AxisPositions]; 1027 QRectF canvasRect; 1028 1029 LayoutEngine engine; 1030 }; 1031 1032 /*! 1033 \brief Constructor 1034 */ 1035 1036 QwtPlotLayout::QwtPlotLayout() 1037 { 1038 m_data = new PrivateData; 1039 1040 setLegendPosition( QwtPlot::BottomLegend ); 1041 setCanvasMargin( 4 ); 1042 setAlignCanvasToScales( false ); 1043 1044 invalidate(); 1045 } 1046 1047 //! Destructor 1048 QwtPlotLayout::~QwtPlotLayout() 1049 { 1050 delete m_data; 1051 } 1052 1053 /*! 1054 Change a margin of the canvas. The margin is the space 1055 above/below the scale ticks. A negative margin will 1056 be set to -1, excluding the borders of the scales. 1057 1058 \param margin New margin 1059 \param axisPos One of QwtAxis::Position. Specifies where the position of the margin. 1060 -1 means margin at all borders. 1061 \sa canvasMargin() 1062 1063 \warning The margin will have no effect when alignCanvasToScale() is true 1064 */ 1065 1066 void QwtPlotLayout::setCanvasMargin( int margin, int axisPos ) 1067 { 1068 if ( margin < -1 ) 1069 margin = -1; 1070 1071 LayoutEngine& engine = m_data->engine; 1072 1073 if ( axisPos == -1 ) 1074 { 1075 for ( axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ ) 1076 engine.setCanvasMargin( axisPos, margin ); 1077 } 1078 else if ( QwtAxis::isValid( axisPos ) ) 1079 { 1080 engine.setCanvasMargin( axisPos, margin ); 1081 } 1082 } 1083 1084 /*! 1085 \param axisPos Axis position 1086 \return Margin around the scale tick borders 1087 \sa setCanvasMargin() 1088 */ 1089 int QwtPlotLayout::canvasMargin( int axisPos ) const 1090 { 1091 if ( !QwtAxis::isValid( axisPos ) ) 1092 return 0; 1093 1094 return m_data->engine.canvasMargin( axisPos ); 1095 } 1096 1097 /*! 1098 \brief Set the align-canvas-to-axis-scales flag for all axes 1099 1100 \param on True/False 1101 \sa setAlignCanvasToScale(), alignCanvasToScale() 1102 */ 1103 void QwtPlotLayout::setAlignCanvasToScales( bool on ) 1104 { 1105 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ ) 1106 m_data->engine.setAlignCanvas( axisPos, on ); 1107 } 1108 1109 /*! 1110 Change the align-canvas-to-axis-scales setting. The canvas may: 1111 1112 - extend beyond the axis scale ends to maximize its size, 1113 - align with the axis scale ends to control its size. 1114 1115 The axisId parameter is somehow confusing as it identifies a border 1116 of the plot and not the axes, that are aligned. F.e when QwtAxis::YLeft 1117 is set, the left end of the the x-axes ( QwtAxis::XTop, QwtAxis::XBottom ) 1118 is aligned. 1119 1120 \param axisPos Axis position 1121 \param on New align-canvas-to-axis-scales setting 1122 1123 \sa setCanvasMargin(), alignCanvasToScale(), setAlignCanvasToScales() 1124 \warning In case of on == true canvasMargin() will have no effect 1125 */ 1126 void QwtPlotLayout::setAlignCanvasToScale( int axisPos, bool on ) 1127 { 1128 if ( QwtAxis::isValid( axisPos ) ) 1129 m_data->engine.setAlignCanvas( axisPos, on ); 1130 } 1131 1132 /*! 1133 Return the align-canvas-to-axis-scales setting. The canvas may: 1134 - extend beyond the axis scale ends to maximize its size 1135 - align with the axis scale ends to control its size. 1136 1137 \param axisPos Axis position 1138 \return align-canvas-to-axis-scales setting 1139 \sa setAlignCanvasToScale(), setAlignCanvasToScale(), setCanvasMargin() 1140 */ 1141 bool QwtPlotLayout::alignCanvasToScale( int axisPos ) const 1142 { 1143 if ( !QwtAxis::isValid( axisPos ) ) 1144 return false; 1145 1146 return m_data->engine.alignCanvas( axisPos ); 1147 } 1148 1149 /*! 1150 Change the spacing of the plot. The spacing is the distance 1151 between the plot components. 1152 1153 \param spacing New spacing 1154 \sa setCanvasMargin(), spacing() 1155 */ 1156 void QwtPlotLayout::setSpacing( int spacing ) 1157 { 1158 m_data->engine.setSpacing( qMax( 0, spacing ) ); 1159 } 1160 1161 /*! 1162 \return Spacing 1163 \sa margin(), setSpacing() 1164 */ 1165 int QwtPlotLayout::spacing() const 1166 { 1167 return m_data->engine.spacing(); 1168 } 1169 1170 /*! 1171 \brief Specify the position of the legend 1172 \param pos The legend's position. 1173 \param ratio Ratio between legend and the bounding rectangle 1174 of title, footer, canvas and axes. The legend will be shrunk 1175 if it would need more space than the given ratio. 1176 The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0 1177 it will be reset to the default ratio. 1178 The default vertical/horizontal ratio is 0.33/0.5. 1179 1180 \sa QwtPlot::setLegendPosition() 1181 */ 1182 1183 void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos, double ratio ) 1184 { 1185 if ( ratio > 1.0 ) 1186 ratio = 1.0; 1187 1188 LayoutEngine& engine = m_data->engine; 1189 1190 switch ( pos ) 1191 { 1192 case QwtPlot::TopLegend: 1193 case QwtPlot::BottomLegend: 1194 { 1195 if ( ratio <= 0.0 ) 1196 ratio = 0.33; 1197 1198 engine.setLegendRatio( ratio ); 1199 engine.setLegendPos( pos ); 1200 1201 break; 1202 } 1203 case QwtPlot::LeftLegend: 1204 case QwtPlot::RightLegend: 1205 { 1206 if ( ratio <= 0.0 ) 1207 ratio = 0.5; 1208 1209 engine.setLegendRatio( ratio ); 1210 engine.setLegendPos( pos ); 1211 1212 break; 1213 } 1214 default: 1215 break; 1216 } 1217 } 1218 1219 /*! 1220 \brief Specify the position of the legend 1221 \param pos The legend's position. Valid values are 1222 \c QwtPlot::LeftLegend, \c QwtPlot::RightLegend, 1223 \c QwtPlot::TopLegend, \c QwtPlot::BottomLegend. 1224 1225 \sa QwtPlot::setLegendPosition() 1226 */ 1227 void QwtPlotLayout::setLegendPosition( QwtPlot::LegendPosition pos ) 1228 { 1229 setLegendPosition( pos, 0.0 ); 1230 } 1231 1232 /*! 1233 \return Position of the legend 1234 \sa setLegendPosition(), QwtPlot::setLegendPosition(), 1235 QwtPlot::legendPosition() 1236 */ 1237 QwtPlot::LegendPosition QwtPlotLayout::legendPosition() const 1238 { 1239 return m_data->engine.legendPos(); 1240 } 1241 1242 /*! 1243 Specify the relative size of the legend in the plot 1244 \param ratio Ratio between legend and the bounding rectangle 1245 of title, footer, canvas and axes. The legend will be shrunk 1246 if it would need more space than the given ratio. 1247 The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0 1248 it will be reset to the default ratio. 1249 The default vertical/horizontal ratio is 0.33/0.5. 1250 */ 1251 void QwtPlotLayout::setLegendRatio( double ratio ) 1252 { 1253 setLegendPosition( legendPosition(), ratio ); 1254 } 1255 1256 /*! 1257 \return The relative size of the legend in the plot. 1258 \sa setLegendPosition() 1259 */ 1260 double QwtPlotLayout::legendRatio() const 1261 { 1262 return m_data->engine.legendRatio(); 1263 } 1264 1265 /*! 1266 \brief Set the geometry for the title 1267 1268 This method is intended to be used from derived layouts 1269 overloading activate() 1270 1271 \sa titleRect(), activate() 1272 */ 1273 void QwtPlotLayout::setTitleRect( const QRectF& rect ) 1274 { 1275 m_data->titleRect = rect; 1276 } 1277 1278 /*! 1279 \return Geometry for the title 1280 \sa activate(), invalidate() 1281 */ 1282 QRectF QwtPlotLayout::titleRect() const 1283 { 1284 return m_data->titleRect; 1285 } 1286 1287 /*! 1288 \brief Set the geometry for the footer 1289 1290 This method is intended to be used from derived layouts 1291 overloading activate() 1292 1293 \sa footerRect(), activate() 1294 */ 1295 void QwtPlotLayout::setFooterRect( const QRectF& rect ) 1296 { 1297 m_data->footerRect = rect; 1298 } 1299 1300 /*! 1301 \return Geometry for the footer 1302 \sa activate(), invalidate() 1303 */ 1304 QRectF QwtPlotLayout::footerRect() const 1305 { 1306 return m_data->footerRect; 1307 } 1308 1309 /*! 1310 \brief Set the geometry for the legend 1311 1312 This method is intended to be used from derived layouts 1313 overloading activate() 1314 1315 \param rect Rectangle for the legend 1316 1317 \sa legendRect(), activate() 1318 */ 1319 void QwtPlotLayout::setLegendRect( const QRectF& rect ) 1320 { 1321 m_data->legendRect = rect; 1322 } 1323 1324 /*! 1325 \return Geometry for the legend 1326 \sa activate(), invalidate() 1327 */ 1328 QRectF QwtPlotLayout::legendRect() const 1329 { 1330 return m_data->legendRect; 1331 } 1332 1333 /*! 1334 \brief Set the geometry for an axis 1335 1336 This method is intended to be used from derived layouts 1337 overloading activate() 1338 1339 \param axisId Axis 1340 \param rect Rectangle for the scale 1341 1342 \sa scaleRect(), activate() 1343 */ 1344 void QwtPlotLayout::setScaleRect( QwtAxisId axisId, const QRectF& rect ) 1345 { 1346 if ( QwtAxis::isValid( axisId ) ) 1347 m_data->scaleRects[axisId] = rect; 1348 } 1349 1350 /*! 1351 \param axisId Axis 1352 \return Geometry for the scale 1353 \sa activate(), invalidate() 1354 */ 1355 QRectF QwtPlotLayout::scaleRect( QwtAxisId axisId ) const 1356 { 1357 if ( QwtAxis::isValid( axisId ) ) 1358 return m_data->scaleRects[axisId]; 1359 1360 return QRectF(); 1361 } 1362 1363 /*! 1364 \brief Set the geometry for the canvas 1365 1366 This method is intended to be used from derived layouts 1367 overloading activate() 1368 1369 \sa canvasRect(), activate() 1370 */ 1371 void QwtPlotLayout::setCanvasRect( const QRectF& rect ) 1372 { 1373 m_data->canvasRect = rect; 1374 } 1375 1376 /*! 1377 \return Geometry for the canvas 1378 \sa activate(), invalidate() 1379 */ 1380 QRectF QwtPlotLayout::canvasRect() const 1381 { 1382 return m_data->canvasRect; 1383 } 1384 1385 /*! 1386 Invalidate the geometry of all components. 1387 \sa activate() 1388 */ 1389 void QwtPlotLayout::invalidate() 1390 { 1391 m_data->titleRect = m_data->footerRect = 1392 m_data->legendRect = m_data->canvasRect = QRectF(); 1393 1394 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ ) 1395 m_data->scaleRects[axisPos] = QRect(); 1396 } 1397 1398 /*! 1399 \return Minimum size hint 1400 \param plot Plot widget 1401 1402 \sa QwtPlot::minimumSizeHint() 1403 */ 1404 QSize QwtPlotLayout::minimumSizeHint( const QwtPlot* plot ) const 1405 { 1406 LayoutHintData hintData( plot ); 1407 1408 const int xAxesWidth = hintData.xAxesWidth(); 1409 const int yAxesHeight = hintData.yAxesHeight(); 1410 1411 const QWidget* canvas = plot->canvas(); 1412 1413 const QMargins m = canvas->contentsMargins(); 1414 const QSize minCanvasSize = canvas->minimumSize(); 1415 1416 int w = hintData.yAxesWidth(); 1417 int cw = xAxesWidth + m.left() + 1 + m.right() + 1; 1418 w += qMax( cw, minCanvasSize.width() ); 1419 1420 int h = hintData.xAxesHeight(); 1421 int ch = yAxesHeight + m.top() + 1 + m.bottom() + 1; 1422 h += qMax( ch, minCanvasSize.height() ); 1423 1424 const QwtTextLabel* labels[2]; 1425 labels[0] = plot->titleLabel(); 1426 labels[1] = plot->footerLabel(); 1427 1428 for ( int i = 0; i < 2; i++ ) 1429 { 1430 const QwtTextLabel* label = labels[i]; 1431 if ( label && !label->text().isEmpty() ) 1432 { 1433 // we center on the plot canvas. 1434 const bool centerOnCanvas = !( plot->isAxisVisible( QwtAxis::YLeft ) 1435 && plot->isAxisVisible( QwtAxis::YRight ) ); 1436 1437 int labelW = w; 1438 if ( centerOnCanvas ) 1439 { 1440 labelW -= hintData.yAxesWidth(); 1441 } 1442 1443 int labelH = label->heightForWidth( labelW ); 1444 if ( labelH > labelW ) // Compensate for a long title 1445 { 1446 w = labelW = labelH; 1447 if ( centerOnCanvas ) 1448 w += hintData.yAxesWidth(); 1449 1450 labelH = label->heightForWidth( labelW ); 1451 } 1452 h += labelH + spacing(); 1453 } 1454 } 1455 1456 // Compute the legend contribution 1457 1458 const QwtAbstractLegend* legend = plot->legend(); 1459 if ( legend && !legend->isEmpty() ) 1460 { 1461 const LayoutEngine& engine = m_data->engine; 1462 1463 if ( engine.legendPos() == QwtPlot::LeftLegend 1464 || engine.legendPos() == QwtPlot::RightLegend ) 1465 { 1466 int legendW = legend->sizeHint().width(); 1467 int legendH = legend->heightForWidth( legendW ); 1468 1469 if ( legend->frameWidth() > 0 ) 1470 w += spacing(); 1471 1472 if ( legendH > h ) 1473 legendW += legend->scrollExtent( Qt::Horizontal ); 1474 1475 if ( engine.legendRatio() < 1.0 ) 1476 legendW = qMin( legendW, int( w / ( 1.0 - engine.legendRatio() ) ) ); 1477 1478 w += legendW + spacing(); 1229 1479 } 1230 1480 else 1231 1481 { 1232 if ( d_data->alignCanvasToScales[QwtPlot::xTop] ) 1233 { 1234 double x = canvasRect.top() - d_data->layoutData.scale[axis].start; 1235 if ( !( options & IgnoreFrames ) ) 1236 x += d_data->layoutData.canvas.contentsMargins[ QwtPlot::xTop ]; 1237 1238 sRect.setTop( x ); 1239 } 1240 if ( d_data->alignCanvasToScales[QwtPlot::xBottom] ) 1241 { 1242 double x = canvasRect.bottom() - 1 + d_data->layoutData.scale[axis].end; 1243 if ( !( options & IgnoreFrames ) ) 1244 x -= d_data->layoutData.canvas.contentsMargins[ QwtPlot::xBottom ]; 1245 1246 sRect.setBottom( x ); 1247 } 1248 1249 if ( d_data->alignCanvasToScales[ axis ] ) 1250 { 1251 if ( axis == QwtPlot::yLeft ) 1252 sRect.setRight( canvasRect.left() ); 1253 else 1254 sRect.setLeft( canvasRect.right() ); 1255 } 1256 } 1257 } 1258 } 1259 1260 /*! 1261 \brief Recalculate the geometry of all components. 1262 1263 \param plot Plot to be layout 1264 \param plotRect Rectangle where to place the components 1265 \param options Layout options 1266 1267 \sa invalidate(), titleRect(), footerRect() 1482 int legendW = qMin( legend->sizeHint().width(), w ); 1483 int legendH = legend->heightForWidth( legendW ); 1484 1485 if ( legend->frameWidth() > 0 ) 1486 h += spacing(); 1487 1488 if ( engine.legendRatio() < 1.0 ) 1489 legendH = qMin( legendH, int( h / ( 1.0 - engine.legendRatio() ) ) ); 1490 1491 h += legendH + spacing(); 1492 } 1493 } 1494 1495 return QSize( w, h ); 1496 } 1497 1498 /*! 1499 \brief Recalculate the geometry of all components. 1500 1501 \param plot Plot to be layout 1502 \param plotRect Rectangle where to place the components 1503 \param options Layout options 1504 1505 \sa invalidate(), titleRect(), footerRect() 1268 1506 legendRect(), scaleRect(), canvasRect() 1269 */ 1270 void QwtPlotLayout::activate( const QwtPlot *plot,1271 const QRectF &plotRect, Options options )1507 */ 1508 void QwtPlotLayout::activate( const QwtPlot* plot, 1509 const QRectF& plotRect, Options options ) 1272 1510 { 1273 1511 invalidate(); … … 1276 1514 1277 1515 // We extract all layout relevant parameters from the widgets, 1278 // and save them to d_data->layoutData. 1279 1280 d_data->layoutData.init( plot, rect ); 1516 // and save them to m_data->layoutData. 1517 1518 LayoutData layoutData( plot ); 1519 1520 QSize legendHint; 1281 1521 1282 1522 if ( !( options & IgnoreLegend ) 1283 1523 && plot->legend() && !plot->legend()->isEmpty() ) 1284 1524 { 1285 d_data->legendRect = layoutLegend( options, rect ); 1286 1287 // subtract d_data->legendRect from rect 1525 legendHint = layoutData.legendData.legendHint( plot->legend(), rect ); 1526 1527 m_data->legendRect = m_data->engine.layoutLegend( 1528 options, layoutData.legendData, rect, legendHint ); 1529 1530 // subtract m_data->legendRect from rect 1288 1531 1289 1532 const QRegion region( rect.toRect() ); 1290 rect = region.subtracted( d_data->legendRect.toRect() ).boundingRect();1291 1292 switch ( d_data->legendPos )1533 rect = region.subtracted( m_data->legendRect.toRect() ).boundingRect(); 1534 1535 switch ( m_data->engine.legendPos() ) 1293 1536 { 1294 1537 case QwtPlot::LeftLegend: 1295 rect.setLeft( rect.left() + d_data->spacing ); 1538 { 1539 rect.setLeft( rect.left() + spacing() ); 1296 1540 break; 1541 } 1297 1542 case QwtPlot::RightLegend: 1298 rect.setRight( rect.right() - d_data->spacing ); 1543 { 1544 rect.setRight( rect.right() - spacing() ); 1299 1545 break; 1546 } 1300 1547 case QwtPlot::TopLegend: 1301 rect.setTop( rect.top() + d_data->spacing ); 1548 { 1549 rect.setTop( rect.top() + spacing() ); 1302 1550 break; 1551 } 1303 1552 case QwtPlot::BottomLegend: 1304 rect.setBottom( rect.bottom() - d_data->spacing ); 1553 { 1554 rect.setBottom( rect.bottom() - spacing() ); 1305 1555 break; 1556 } 1306 1557 } 1307 1558 } … … 1322 1573 | Footer | 1323 1574 +---+-----------+---+ 1324 */ 1575 */ 1325 1576 1326 1577 // title, footer and axes include text labels. The height of each … … 1328 1579 // for the label. A line break in a horizontal text will reduce 1329 1580 // the available width for vertical texts and vice versa. 1330 // expandLineBreaks finds the height/width for title, footer and axes1581 // layoutDimensions finds the height/width for title, footer and axes 1331 1582 // including all line breaks. 1332 1583 1333 int dimTitle, dimFooter, dimAxes[QwtPlot::axisCnt]; 1334 expandLineBreaks( options, rect, dimTitle, dimFooter, dimAxes ); 1335 1336 if ( dimTitle > 0 ) 1337 { 1338 d_data->titleRect.setRect( 1339 rect.left(), rect.top(), rect.width(), dimTitle ); 1340 1341 rect.setTop( d_data->titleRect.bottom() + d_data->spacing ); 1342 1343 if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled != 1344 d_data->layoutData.scale[QwtPlot::yRight].isEnabled ) 1584 using namespace QwtAxis; 1585 1586 const LayoutEngine::Dimensions dimensions = 1587 m_data->engine.layoutDimensions( options, layoutData, rect ); 1588 1589 if ( dimensions.dimTitle > 0 ) 1590 { 1591 QRectF& labelRect = m_data->titleRect; 1592 1593 labelRect.setRect( rect.left(), rect.top(), rect.width(), dimensions.dimTitle ); 1594 1595 rect.setTop( labelRect.bottom() + spacing() ); 1596 1597 if ( !layoutData.hasSymmetricYAxes() ) 1345 1598 { 1346 1599 // if only one of the y axes is missing we align 1347 1600 // the title centered to the canvas 1348 1601 1349 d_data->titleRect.setX( rect.left() + dimAxes[QwtPlot::yLeft] ); 1350 d_data->titleRect.setWidth( rect.width() 1351 - dimAxes[QwtPlot::yLeft] - dimAxes[QwtPlot::yRight] ); 1352 } 1353 } 1354 1355 if ( dimFooter > 0 ) 1356 { 1357 d_data->footerRect.setRect( 1358 rect.left(), rect.bottom() - dimFooter, rect.width(), dimFooter ); 1359 1360 rect.setBottom( d_data->footerRect.top() - d_data->spacing ); 1361 1362 if ( d_data->layoutData.scale[QwtPlot::yLeft].isEnabled != 1363 d_data->layoutData.scale[QwtPlot::yRight].isEnabled ) 1602 labelRect = dimensions.centered( rect, labelRect ); 1603 } 1604 } 1605 1606 if ( dimensions.dimFooter > 0 ) 1607 { 1608 QRectF& labelRect = m_data->footerRect; 1609 1610 labelRect.setRect( rect.left(), rect.bottom() - dimensions.dimFooter, 1611 rect.width(), dimensions.dimFooter ); 1612 1613 rect.setBottom( labelRect.top() - spacing() ); 1614 1615 if ( !layoutData.hasSymmetricYAxes() ) 1364 1616 { 1365 1617 // if only one of the y axes is missing we align 1366 1618 // the footer centered to the canvas 1367 1619 1368 d_data->footerRect.setX( rect.left() + dimAxes[QwtPlot::yLeft] ); 1369 d_data->footerRect.setWidth( rect.width() 1370 - dimAxes[QwtPlot::yLeft] - dimAxes[QwtPlot::yRight] ); 1371 } 1372 } 1373 1374 d_data->canvasRect.setRect( 1375 rect.x() + dimAxes[QwtPlot::yLeft], 1376 rect.y() + dimAxes[QwtPlot::xTop], 1377 rect.width() - dimAxes[QwtPlot::yRight] - dimAxes[QwtPlot::yLeft], 1378 rect.height() - dimAxes[QwtPlot::xBottom] - dimAxes[QwtPlot::xTop] ); 1379 1380 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) 1620 labelRect = dimensions.centered( rect, labelRect ); 1621 } 1622 } 1623 1624 m_data->canvasRect = dimensions.innerRect( rect ); 1625 1626 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ ) 1381 1627 { 1382 1628 // set the rects for the axes 1383 1629 1384 if ( dimAxes[axis] ) 1385 { 1386 int dim = dimAxes[axis]; 1387 QRectF &scaleRect = d_data->scaleRect[axis]; 1388 1389 scaleRect = d_data->canvasRect; 1390 switch ( axis ) 1391 { 1392 case QwtPlot::yLeft: 1393 scaleRect.setX( d_data->canvasRect.left() - dim ); 1394 scaleRect.setWidth( dim ); 1395 break; 1396 case QwtPlot::yRight: 1397 scaleRect.setX( d_data->canvasRect.right() ); 1398 scaleRect.setWidth( dim ); 1399 break; 1400 case QwtPlot::xBottom: 1401 scaleRect.setY( d_data->canvasRect.bottom() ); 1402 scaleRect.setHeight( dim ); 1403 break; 1404 case QwtPlot::xTop: 1405 scaleRect.setY( d_data->canvasRect.top() - dim ); 1406 scaleRect.setHeight( dim ); 1407 break; 1408 } 1409 scaleRect = scaleRect.normalized(); 1630 const int pos = 0; 1631 { 1632 const QwtAxisId axisId( axisPos ); 1633 1634 if ( dimensions.dimAxis( axisId ) ) 1635 { 1636 const int dim = dimensions.dimAxis( axisId ); 1637 1638 const QRectF& canvasRect = m_data->canvasRect; 1639 1640 QRectF& scaleRect = m_data->scaleRects[axisId]; 1641 scaleRect = canvasRect; 1642 1643 switch ( axisPos ) 1644 { 1645 case YLeft: 1646 { 1647 scaleRect.setX( canvasRect.left() - pos - dim ); 1648 scaleRect.setWidth( dim ); 1649 break; 1650 } 1651 case YRight: 1652 { 1653 scaleRect.setX( canvasRect.right() + pos ); 1654 scaleRect.setWidth( dim ); 1655 break; 1656 } 1657 case XBottom: 1658 { 1659 scaleRect.setY( canvasRect.bottom() + pos ); 1660 scaleRect.setHeight( dim ); 1661 break; 1662 } 1663 case XTop: 1664 { 1665 scaleRect.setY( canvasRect.top() - pos - dim ); 1666 scaleRect.setHeight( dim ); 1667 break; 1668 } 1669 } 1670 scaleRect = scaleRect.normalized(); 1671 } 1410 1672 } 1411 1673 } … … 1431 1693 // left/right of the min/max ticks are moved into them. 1432 1694 1433 alignScales( options, d_data->canvasRect, d_data->scaleRect ); 1434 1435 if ( !d_data->legendRect.isEmpty() ) 1695 m_data->engine.alignScales( options, layoutData, 1696 m_data->canvasRect, m_data->scaleRects ); 1697 1698 if ( !m_data->legendRect.isEmpty() ) 1436 1699 { 1437 1700 // We prefer to align the legend to the canvas - not to 1438 1701 // the complete plot - if possible. 1439 1702 1440 d_data->legendRect = alignLegend( d_data->canvasRect, d_data->legendRect ); 1441 } 1442 } 1703 m_data->legendRect = m_data->engine.alignLegend( 1704 legendHint, m_data->canvasRect, m_data->legendRect ); 1705 } 1706 }
Note:
See TracChangeset
for help on using the changeset viewer.
