1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : Plot2d_PlotItems.cxx
23 // Author : Natalia ERMOLAEVA, Open CASCADE S.A.S. (natalia.donis@opencascade.com)
25 #include "Plot2d_PlotItems.h"
26 #include "Plot2d_Object.h"
27 #include "Plot2d_ViewFrame.h"
34 #include <QStyleOption>
35 #include <QPaintEvent>
39 #include <qwt_painter.h>
40 #include <qwt_scale_map.h>
41 #include <qwt_legend.h>
42 #include <qwt_legend_label.h>
43 #include <qwt_plot_dict.h>
45 const int SPACING = 10;
46 const int SYMBOL_SIZE = 13;
48 const char* yAxisLeft[] = {
65 const char* yAxisRight[] = {
83 Constructor of Plot2d_QwtLegendLabel
85 Plot2d_QwtLegendLabel::Plot2d_QwtLegendLabel( QWidget* parent ) :
86 QwtLegendLabel( parent ),
87 myYAxisIdentifierMode( IM_None ),
89 mySymbol( new QwtSymbol() ),
93 myYAxisLeftIcon = QPixmap(yAxisLeft);
94 myYAxisRightIcon = QPixmap(yAxisRight);
95 int anIconWidth = qMax( myYAxisLeftIcon.width(), myYAxisRightIcon.width() );
96 setSpacing( SPACING );
98 mySpacingCollapsed = spacing();
99 mySpacingExpanded = anIconWidth + mySpacingCollapsed;
103 Destructor of Plot2d_QwtLegendLabel
105 Plot2d_QwtLegendLabel::~Plot2d_QwtLegendLabel()
111 Set Y axis identifier displaying mode
113 void Plot2d_QwtLegendLabel::setYAxisIdentifierMode( const int theMode )
115 myYAxisIdentifierMode = theMode;
116 setSpacing( theMode == IM_None ? mySpacingCollapsed : mySpacingExpanded );
120 Paint the identifier to a given rect.
121 \param painter Painter
122 \param rect Rect where to paint
124 void Plot2d_QwtLegendLabel::drawIdentifier( QPainter* painter, const QRect& rect )
126 if ( rect.isEmpty() )
129 if( myPen.style() != Qt::NoPen ) {
131 painter->setPen(myPen);
132 QwtPainter::drawLine( painter, rect.left() - 2, rect.center().y() + mySymbolType * 4,
133 rect.right() + 2, rect.center().y() + mySymbolType * 4 );
136 if ( mySymbol->style() != QwtSymbol::NoSymbol )
139 symbolRect.setSize( QSize( SYMBOL_SIZE, SYMBOL_SIZE ) );
140 symbolRect.moveCenter( QPoint( rect.center().x(), rect.center().y() - mySymbolType * 4 ) );
142 painter->setBrush( mySymbol->brush() );
143 painter->setPen( mySymbol->pen() );
144 mySymbol->drawSymbol( painter, symbolRect );
147 if( myYAxisIdentifierMode != IM_None ) {
148 QPixmap aPixmap( myYAxisIdentifierMode == IM_Left ? yAxisLeft : yAxisRight );
150 painter->drawPixmap( rect.topRight() + QPoint( mySpacingExpanded/2, mySpacingExpanded/2 ), aPixmap );
156 Update highliting on the item.
158 void Plot2d_QwtLegendLabel::updateHighlit() {
159 QwtText txt = text();
161 QColor highlightColor = Plot2d_Object::selectionColor();
162 if( highlightColor != txt.backgroundBrush().color() ) {
163 txt.setBackgroundBrush( highlightColor );
167 else if( this->parent() ) {
168 if(QWidget* parent = qobject_cast<QWidget*>( this->parent()->parent() ) ) {
169 QPalette aPal = parent->palette();
170 if( aPal.color( QPalette::Background) != txt.backgroundBrush().color() ) {
171 txt.setBackgroundBrush( aPal.color( QPalette::Background ) );
179 Sets selected property.
181 void Plot2d_QwtLegendLabel::setSelected(const bool on) {
186 Gets selected property.
188 bool Plot2d_QwtLegendLabel::isSelected() const {
194 Draw text of the item.
196 void Plot2d_QwtLegendLabel::drawText( QPainter * painter, const QRectF &rect ) {
197 painter->setPen( isSelected() ? Plot2d_Object::highlightedLegendTextColor() :
198 getColorFromPalette( QPalette::Text) );
200 QwtTextLabel::drawText( painter, rect );
206 void Plot2d_QwtLegendLabel::setSymbol( const QwtSymbol* theSymbol )
209 mySymbol = new QwtSymbol( theSymbol->style(), theSymbol->brush(),
210 theSymbol->pen(), theSymbol->size() );
214 Sets symbol type 0(marker on line) or 1(marker above line).
216 void Plot2d_QwtLegendLabel::setSymbolType( const int theType )
218 mySymbolType = theType;
224 void Plot2d_QwtLegendLabel::setPen (const QPen& thePen )
230 Redefined method paintEvent of QwtLegendLabel
232 void Plot2d_QwtLegendLabel::paintEvent( QPaintEvent *e )
234 const QRect cr = contentsRect();
237 QPainter painter( this );
238 painter.setClipRegion( e->region() );
242 qDrawWinButton( &painter, 0, 0, width(), height(),
247 painter.setClipRect( cr );
249 drawContents( &painter );
252 if ( !icon().isNull() )
254 if ( itemMode() != QwtLegendData::ReadOnly ) {
255 iconRect.setX( iconRect.x() + ButtonFrame );
256 iconRect.setSize( QSize( icon().size().width() + spacing() ,
257 icon().size().height() + spacing() ) );
258 iconRect.moveCenter( QPoint( iconRect.center().x(), cr.center().y() ) );
262 drawIdentifier( &painter, iconRect );
268 Get color from the legend pallete by 'role' flag.
270 QColor Plot2d_QwtLegendLabel::getColorFromPalette(QPalette::ColorRole role) {
271 QWidget* pw = parentWidget();
272 QColor col = palette().color( role );
274 if ( qobject_cast<QwtLegend*>( pw ) ) {
275 col = pw->palette().color(role );
278 pw = pw->parentWidget();
283 * Internal class to store deviation data on the curve.
285 class Plot2d_QwtPlotCurve::Plot2d_DeviationData {
287 Plot2d_DeviationData(const double *min, const double *max,const QList<int>& idx)
289 foreach(int index,idx) {
290 myMin[index] = min[index];
291 myMax[index] = max[index];
294 ~Plot2d_DeviationData(){}
298 return qwtMin(myMin.size(), myMax.size());
300 bool values(size_t i, double &min, double &max) {
301 if(myMin.contains((int)i) && myMax.contains((int)i)) { //!< TODO: conversion from size_t to int
309 QMap<int,double> myMin;
310 QMap<int,double> myMax;
315 Constructor of Plot2d_QwtPlotCurve
317 Plot2d_QwtPlotCurve::Plot2d_QwtPlotCurve( const QString& title,
318 QwtPlot::Axis yAxis /*const int index*/ ) :
319 QwtPlotCurve( title ),
320 Plot2d_SelectableItem(),
322 myYAxisIdentifierEnabled( false ),
328 Destructor of Plot2d_QwtPlotCurve
330 Plot2d_QwtPlotCurve::~Plot2d_QwtPlotCurve()
332 clearDeviationData();
336 Enable / disable Y axis identifier
338 void Plot2d_QwtPlotCurve::setYAxisIdentifierEnabled( const bool on )
340 myYAxisIdentifierEnabled = on;
344 Redefined method, which updates legend of the curve
346 void Plot2d_QwtPlotCurve::updateLegend( const QwtPlotItem* thePlotItem,
347 const QList<QwtLegendData>& theLegendData )
349 if ( !thePlotItem || !thePlotItem->plot() )
352 if ( !testItemAttribute( QwtPlotItem::Legend ) )
355 QwtPlotCurve::updateLegend( thePlotItem, theLegendData );
357 const QVariant itemInfo = thePlotItem->plot()->itemToInfo( const_cast< QwtPlotItem *>( thePlotItem ) );
358 if( QwtLegend* legend = dynamic_cast<QwtLegend*>( thePlotItem->plot()->legend() ) ) {
359 if( QWidget* widget = legend->legendWidget( itemInfo ) ) {
360 QwtLegendLabel* label = dynamic_cast<QwtLegendLabel*>( widget );
361 if( Plot2d_QwtLegendLabel* anItem = (Plot2d_QwtLegendLabel*)label ) {
362 int aMode = Plot2d_QwtLegendLabel::IM_None;
363 if( myYAxisIdentifierEnabled )
364 aMode = myYAxis == QwtPlot::yRight ?
365 Plot2d_QwtLegendLabel::IM_Right :
366 Plot2d_QwtLegendLabel::IM_Left;
367 anItem->setYAxisIdentifierMode( aMode );
369 anItem->setSymbol( legendSymbol() );
370 if( Plot2d_Plot2d* plot = dynamic_cast<Plot2d_Plot2d*>( thePlotItem->plot() ) )
371 anItem->setSymbolType( plot->getLegendSymbolType() );
372 anItem->setPen( legendPen() );
374 anItem->setSelected( isSelected() );
375 anItem->updateHighlit();
383 Redefined method, which updates and calls QwtPlot::autoRefresh() for the parent plot
385 void Plot2d_QwtPlotCurve::itemChanged()
388 updateLegend( this, legendData() );
390 QwtPlotItem::itemChanged();
394 Redefined method, which draw a set of points of a curve.
396 void Plot2d_QwtPlotCurve::drawSeries( QPainter *painter,
397 const QwtScaleMap &xMap,
398 const QwtScaleMap &yMap,
399 const QRectF &canvasRect,
400 int from, int to) const
403 to = (int)dataSize() - 1; //!< TODO: conversion from size_t to int
404 QwtPlotCurve::drawSeries(painter, xMap, yMap, canvasRect, from, to);
406 //draw deviation data
407 if(hasDeviationData()) {
409 int lineW = deviationMarkerLineWidth();
410 int tickSz = deviationMarkerTickSize() + qRound(double(lineW)/2);
411 double min, max, xi, yi;
412 int xp, ytop, ybtm, tickl, tickr;
413 QColor c = isSelected() ? Plot2d_Object::selectionColor() : deviationMarkerColor();
414 QPen p = QPen(c, lineW, Qt::SolidLine);
416 for (int i = from; i <= to; i++) {
417 if(!myDeviationData->values(i,min,max)) continue;
418 const QPointF sample = data()->sample( i );
421 xp = xMap.transform(xi);
422 ytop = yMap.transform(yi + max);
423 ybtm = yMap.transform(yi - min);
426 painter->drawLine(tickl,ytop,tickr,ytop);
427 painter->drawLine(xp,ytop,xp,ybtm);
428 painter->drawLine(tickl,ybtm,tickr,ybtm);
435 * Return color of the deviation marker.
437 QColor Plot2d_QwtPlotCurve::deviationMarkerColor() const {
440 QVariant var = plot()->property(PLOT2D_DEVIATION_COLOR);
442 c = var.value<QColor>();
447 * Return line width of the deviation marker.
449 int Plot2d_QwtPlotCurve::deviationMarkerLineWidth() const {
452 QVariant var = plot()->property(PLOT2D_DEVIATION_LW);
460 * Return tick size of the deviation marker.
462 int Plot2d_QwtPlotCurve::deviationMarkerTickSize() const {
465 QVariant var = plot()->property(PLOT2D_DEVIATION_TS);
473 * Sets deviation data for the plot item.
475 void Plot2d_QwtPlotCurve::setDeviationData(const double* min, const double* max,const QList<int> &idx) {
476 clearDeviationData();
477 myDeviationData = new Plot2d_DeviationData(min,max,idx);
481 * Return true if deviation is assigned to the plot item,
484 bool Plot2d_QwtPlotCurve::hasDeviationData() const {
485 return myDeviationData != 0;
489 * Remove deviation data from the plot item.
491 void Plot2d_QwtPlotCurve::clearDeviationData()
494 delete myDeviationData;
503 Plot2d_SelectableItem::Plot2d_SelectableItem():
505 myLegendPen( QPen() ),
506 myLegendSymbol( new QwtSymbol() )
513 Plot2d_SelectableItem::~Plot2d_SelectableItem()
518 Sets selected property.
520 void Plot2d_SelectableItem::setSelected( const bool on) {
525 Return selected property.
527 bool Plot2d_SelectableItem::isSelected() const {
532 Sets legend pen property.
534 void Plot2d_SelectableItem::setLegendPen( const QPen & p) {
539 Return legend pen property.
541 QPen Plot2d_SelectableItem::legendPen() const {
546 Sets legend symbol property.
548 void Plot2d_SelectableItem::setLegendSymbol( const QwtSymbol* s ) {
549 myLegendSymbol->setStyle( s->style() );
550 myLegendSymbol->setBrush( s->brush() );
551 myLegendSymbol->setPen( s->pen() );
552 myLegendSymbol->setSize( s->size() );
556 Sets legend symbol property.
558 QwtSymbol* Plot2d_SelectableItem::legendSymbol() const {
559 return new QwtSymbol( myLegendSymbol->style(), myLegendSymbol->brush(),
560 myLegendSymbol->pen(), myLegendSymbol->size() );
566 Plot2d_HistogramQwtItem::Plot2d_HistogramQwtItem( const QwtText& theTitle )
567 : QwtPlotItem( theTitle )
575 Plot2d_HistogramQwtItem::Plot2d_HistogramQwtItem( const QString& theTitle )
576 : QwtPlotItem( QwtText( theTitle ) )
584 Plot2d_HistogramQwtItem::~Plot2d_HistogramQwtItem()
589 Initialization of object
591 void Plot2d_HistogramQwtItem::init()
594 myAttributes = Plot2d_HistogramQwtItem::Auto;
596 setItemAttribute( QwtPlotItem::AutoScale, true );
597 setItemAttribute( QwtPlotItem::Legend, true );
603 Sets base line to object
606 void Plot2d_HistogramQwtItem::setBaseline( double theRef )
608 if ( myReference != theRef ) {
609 myReference = theRef;
615 Returns base line of object
617 double Plot2d_HistogramQwtItem::baseline() const
625 void Plot2d_HistogramQwtItem::setData( const QwtIntervalSeriesData& theData )
627 myData.setSamples( theData.samples() );
632 Returns data from object
634 const QwtIntervalSeriesData& Plot2d_HistogramQwtItem::data() const
642 void Plot2d_HistogramQwtItem::setColor( const QColor& theColor )
644 if ( myColor != theColor ) {
651 Returns color from object
653 QColor Plot2d_HistogramQwtItem::color() const
659 Returns bounding rect of object
661 QwtDoubleRect Plot2d_HistogramQwtItem::boundingRect() const
663 QwtDoubleRect aRect = myData.boundingRect();
664 if ( !aRect.isValid() )
667 if ( myAttributes & Xfy ) {
668 aRect = QwtDoubleRect( aRect.y(), aRect.x(),
669 aRect.height(), aRect.width() );
670 if ( aRect.left() > myReference )
671 aRect.setLeft( myReference );
672 else if ( aRect.right() < myReference )
673 aRect.setRight( myReference );
676 if ( aRect.bottom() < myReference )
677 aRect.setBottom( myReference );
678 else if ( aRect.top() > myReference )
679 aRect.setTop( myReference );
685 Returns type of plot object
687 int Plot2d_HistogramQwtItem::rtti() const
689 return QwtPlotItem::Rtti_PlotHistogram;
693 Sets histogram attributes
695 void Plot2d_HistogramQwtItem::setHistogramAttribute( HistogramAttribute theAttr,
698 if ( testHistogramAttribute( theAttr ) != isOn ) {
700 myAttributes |= theAttr;
702 myAttributes &= ~theAttr;
709 Tests histogram attributes
711 bool Plot2d_HistogramQwtItem::testHistogramAttribute( HistogramAttribute theAttr ) const
713 return myAttributes & theAttr;
717 Draws histogram object
719 void Plot2d_HistogramQwtItem::draw( QPainter* thePainter,
720 const QwtScaleMap& theXMap,
721 const QwtScaleMap& theYMap,
722 const QRectF& ) const
724 thePainter->setPen( QPen( myColor ) );
726 const int x0 = theXMap.transform( baseline() );
727 const int y0 = theYMap.transform( baseline() );
729 for ( int i = 0; i < (int)myData.size(); i++ ) {
730 if ( myAttributes & Plot2d_HistogramQwtItem::Xfy ) {
731 const int x2 = theXMap.transform( myData.sample(i).value );
734 int y1 = theYMap.transform( myData.sample( i ).interval.minValue() );
735 int y2 = theYMap.transform( myData.sample( i ).interval.maxValue() );
739 if ( i < (int)myData.size() - 2 ) {
740 const int yy1 = theYMap.transform( myData.sample(i+1).interval.minValue() );
741 const int yy2 = theYMap.transform( myData.sample(i+1).interval.maxValue() );
742 if ( y2 == qwtMin( yy1, yy2 ) ) {
743 const int xx2 = theXMap.transform( myData.sample(i+1).interval.minValue() );
744 if ( xx2 != x0 && ( ( xx2 < x0 && x2 < x0 ) ||
745 ( xx2 > x0 && x2 > x0 ) ) ) {
746 // One pixel distance between neighboured bars
751 drawBar( thePainter, Qt::Horizontal, QRect( x0, y1, x2 - x0, y2 - y1 ) );
754 const int y2 = theYMap.transform( myData.sample( i ).value );
757 int x1 = theXMap.transform( myData.sample( i ).interval.minValue() );
758 int x2 = theXMap.transform( myData.sample( i ).interval.maxValue() );
762 if ( i < (int)myData.size() - 2 ) {
763 const int xx1 = theXMap.transform( myData.sample(i+1).interval.minValue() );
764 const int xx2 = theXMap.transform( myData.sample(i+1).interval.maxValue() );
765 if ( x2 == qwtMin( xx1, xx2 ) ) {
766 const int yy2 = theYMap.transform( myData.sample(i+1).value );
767 if ( yy2 != y0 && ( ( yy2 < y0 && y2 < y0 ) ||
768 ( yy2 > y0 && y2 > y0 ) ) ) {
769 // One pixel distance between neighboured bars
774 drawBar( thePainter, Qt::Vertical, QRect( x1, y0, x2 - x1, y2 - y0 ) );
780 Draws single bar of histogram
782 void Plot2d_HistogramQwtItem::drawBar( QPainter* thePainter,
784 const QRect& theRect ) const
788 const QColor color( thePainter->pen().color() );
789 QRect r = theRect.normalized();
791 const int factor = 125;
792 const QColor light( color.light( factor ) );
793 const QColor dark( color.dark( factor ) );
795 thePainter->setBrush( color );
796 thePainter->setPen( Qt::NoPen );
797 QwtPainter::drawRect( thePainter, r.x() + 1, r.y() + 1,
798 r.width() - 2, r.height() - 2 );
799 thePainter->setBrush( Qt::NoBrush );
801 thePainter->setPen( QPen( light, 2 ) );
802 QwtPainter::drawLine( thePainter, r.left() + 1, r.top() + 2,
803 r.right() + 1, r.top() + 2 );
805 thePainter->setPen( QPen( dark, 2 ) );
806 QwtPainter::drawLine( thePainter, r.left() + 1, r.bottom(),
807 r.right() + 1, r.bottom() );
808 thePainter->setPen( QPen( light, 1 ) );
810 QwtPainter::drawLine( thePainter, r.left(), r.top() + 1,
811 r.left(), r.bottom() );
812 QwtPainter::drawLine( thePainter, r.left() + 1, r.top() + 2,
813 r.left() + 1, r.bottom() - 1 );
814 thePainter->setPen( QPen( dark, 1 ) );
816 QwtPainter::drawLine( thePainter, r.right() + 1, r.top() + 1,
817 r.right() + 1, r.bottom() );
818 QwtPainter::drawLine(thePainter, r.right(), r.top() + 2,
819 r.right(), r.bottom() - 1 );
820 thePainter->restore();
826 Plot2d_HistogramItem::Plot2d_HistogramItem( const QwtText& theTitle )
827 : Plot2d_HistogramQwtItem( theTitle ),
828 Plot2d_SelectableItem(),
836 Plot2d_HistogramItem::Plot2d_HistogramItem( const QString& theTitle )
837 : Plot2d_HistogramQwtItem( theTitle ),
845 Plot2d_HistogramItem::~Plot2d_HistogramItem()
850 Get histogram bar items
852 QList<QRect> Plot2d_HistogramItem::getBars() const
858 Set to legend item symbol with color of item
860 void Plot2d_HistogramItem::updateLegend( const QwtPlotItem* thePlotItem,
861 const QList<QwtLegendData>& theLegendData )
863 if ( !thePlotItem || !thePlotItem->plot() )
866 Plot2d_HistogramQwtItem::updateLegend( thePlotItem, theLegendData );
868 const QVariant itemInfo = thePlotItem->plot()->itemToInfo( const_cast< QwtPlotItem *>( thePlotItem ) );
869 QwtLegend *legend = dynamic_cast<QwtLegend *>( thePlotItem->plot()->legend() );
870 QWidget* widget = legend->legendWidget( itemInfo );
872 if ( !widget || !widget->inherits( "QwtLegendLabel" ) )
875 QwtLegendLabel* label = dynamic_cast<QwtLegendLabel*>( widget );
876 if( Plot2d_QwtLegendLabel* anItem = (Plot2d_QwtLegendLabel*)( label ) ) {
877 QFontMetrics aFMetrics( anItem->font() );
878 int aSize = aFMetrics.height();
879 QPen aPen( legendPen().color() );
880 aPen.setStyle(Qt::NoPen);
881 anItem->setPen(aPen);
882 QwtSymbol* aSymbol = new QwtSymbol( QwtSymbol::Rect, QBrush( legendPen().color() ),
883 aPen, QSize( aSize, aSize ) );
884 anItem->setSymbol( aSymbol );
885 if( Plot2d_Plot2d* plot = dynamic_cast<Plot2d_Plot2d*>( thePlotItem->plot() ) )
886 anItem->setSymbolType( plot->getLegendSymbolType() );
887 anItem->setSelected( isSelected() );
888 anItem->updateHighlit();
895 Draws histogram object
897 void Plot2d_HistogramItem::draw( QPainter* thePainter,
898 const QwtScaleMap& theXMap,
899 const QwtScaleMap& theYMap,
900 const QRectF& ) const
902 // nds: clear list of bar items
903 Plot2d_HistogramItem* anItem = (Plot2d_HistogramItem*)this;
904 anItem->myBarItems.clear();
906 thePainter->setPen( QPen( color() ) );
907 const int x0 = theXMap.transform( baseline() );
908 const int y0 = theYMap.transform( baseline() );
910 const QwtIntervalSeriesData& iData = data();
912 for ( int i = 0; i < (int)iData.size(); i++ ) {
913 if ( testHistogramAttribute( Plot2d_HistogramItem::Xfy ) ) {
914 const int x2 = theXMap.transform( iData.sample(i).value );
917 int y1 = theYMap.transform( iData.sample(i).interval.minValue() );
918 int y2 = theYMap.transform( iData.sample(i).interval.maxValue() );
922 if ( i < (int)iData.size() - 2 ) {
923 const int yy1 = theYMap.transform( iData.sample(i+1).interval.minValue() );
924 const int yy2 = theYMap.transform( iData.sample(i+1).interval.maxValue() );
925 if ( y2 == qwtMin( yy1, yy2 ) ) {
926 const int xx2 = theXMap.transform( iData.sample(i+1).interval.minValue() );
927 if ( xx2 != x0 && ( ( xx2 < x0 && x2 < x0 ) ||
928 ( xx2 > x0 && x2 > x0 ) ) ) {
929 // One pixel distance between neighboured bars
934 // nds: draw rect with the other lower rects
935 QRect aRect( x0, y1, x2 - x0, y2 - y1 );
936 drawRectAndLowers( thePainter, Qt::Horizontal, aRect );
937 anItem->myBarItems.append( aRect );
940 const int y2 = theYMap.transform( iData.sample(i).value );
943 int x1 = theXMap.transform( iData.sample(i).interval.minValue() );
944 int x2 = theXMap.transform( iData.sample(i).interval.maxValue() );
948 if ( i < (int)iData.size() - 2 ) {
949 const int xx1 = theXMap.transform( iData.sample(i+1).interval.minValue() );
950 const int xx2 = theXMap.transform( iData.sample(i+1).interval.maxValue() );
951 if ( x2 == qwtMin( xx1, xx2 ) ) {
952 const int yy2 = theYMap.transform( iData.sample(i+1).value );
953 if ( yy2 != y0 && ( ( yy2 < y0 && y2 < y0 ) ||
954 ( yy2 > y0 && y2 > y0 ) ) ) {
955 // One pixel distance between neighboured bars
960 // nds: draw rect with the other lower rects
961 QRect aRect(x1, y0, x2 - x1, y2 - y0 );
962 drawRectAndLowers( thePainter, Qt::Vertical, aRect );
963 anItem->myBarItems.append( aRect );
969 Set/clear "cross items" option
971 void Plot2d_HistogramItem::setCrossItems( bool theCross )
973 myCrossed = theCross;
977 Get "cross items" option
979 bool Plot2d_HistogramItem::isCrossItems() const
985 Draws bar of histogram and on it bars of histograms with lower height.
987 void Plot2d_HistogramItem::drawRectAndLowers( QPainter* thePainter,
988 Qt::Orientation /*theOr*/,
989 const QRect& theRect ) const
991 QRect aRect = theRect;
992 // theRect has inversed coordinates on Y axis.
993 // The top of the rect is bottom in standard QRect coordinates,
994 // and it bottom is the top.
995 if ( myCrossed )//&& theOr == Qt::Horizontal )
996 aRect.setTop( getCrossedTop( theRect ) );
998 drawBar( thePainter, Qt::Horizontal, aRect );
1001 void Plot2d_HistogramItem::itemChanged()
1004 updateLegend( this, legendData() );
1006 QwtPlotItem::itemChanged();
1011 Returns top value of the given rect in the context of other bars.
1013 It's necessary to remember, that \a theRect has inverted coordinate Y.
1015 int Plot2d_HistogramItem::getCrossedTop( const QRect& theRect ) const
1017 int aRes = theRect.top();
1018 QwtPlot* aPlot = plot();
1019 // int aHeight = theRect.height();
1021 QwtPlotItemList anItems = aPlot->itemList();
1022 QwtPlotItemIterator anIt = anItems.begin(), aLast = anItems.end();
1023 Plot2d_HistogramItem* anItem;
1024 QList<QRect> aRects;
1025 for ( ; anIt != aLast; anIt++ ) {
1026 if ( !((*anIt)->rtti() == QwtPlotItem::Rtti_PlotHistogram) )
1028 anItem = dynamic_cast<Plot2d_HistogramItem*>( *anIt );
1029 if( !anItem || anItem == this )
1032 aRects = anItem->getBars();
1033 for ( int i = 0, aSize = aRects.size(); i < aSize; i++ ) {
1034 if ( qMax( theRect.x(), aRects[i].x() ) <=
1035 qMin( theRect.left(), aRects[i].left() ) ) {
1036 if ( theRect.bottom() < aRects[i].bottom() )
1037 if ( aRects[i].bottom() < aRes )
1038 aRes = aRects[i].bottom();
1046 QwtGraphic Plot2d_HistogramItem::legendIcon( int /*index*/, const QSizeF &size ) const
1048 return defaultIcon( color(), size );
1051 Plot2d_QwtLegend::Plot2d_QwtLegend( QWidget *parent ):
1054 setAutoFillBackground(true);
1057 Plot2d_QwtLegend::~Plot2d_QwtLegend()
1062 Redefined method, which create a widget to be inserted into the legend.
1064 QWidget *Plot2d_QwtLegend::createWidget( const QwtLegendData& /*data*/ ) const
1066 Plot2d_QwtLegendLabel *label = new Plot2d_QwtLegendLabel();
1067 label->setItemMode( defaultItemMode() );
1069 connect( label, SIGNAL( clicked() ), SLOT( itemClicked() ) );
1070 connect( label, SIGNAL( checked( bool ) ), SLOT( itemChecked( bool ) ) );