Skip to content

Commit c80d840

Browse files
etienneskyNathanW2
authored andcommitted
use separate curves for each raster layer in identify result graph
1 parent 1012d3d commit c80d840

File tree

4 files changed

+93
-30
lines changed

4 files changed

+93
-30
lines changed

src/app/qgsidentifyresultsdialog.cpp

+53-20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include <qwt_plot.h>
5858
#include <qwt_plot_curve.h>
5959
#include <qwt_symbol.h>
60+
#include <qwt_legend.h>
61+
#include "qgsvectorcolorrampv2.h" // for random colors
6062
#endif
6163

6264
QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QWebView( parent )
@@ -303,17 +305,13 @@ QgsIdentifyResultsDialog::QgsIdentifyResultsDialog( QgsMapCanvas *canvas, QWidge
303305
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
304306
mPlot->setAutoFillBackground( false );
305307
mPlot->setAutoDelete( true );
308+
mPlot->insertLegend( new QwtLegend(), QwtPlot::RightLegend );
306309
QSizePolicy sizePolicy = QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
307310
sizePolicy.setHorizontalStretch( 0 );
308311
sizePolicy.setVerticalStretch( 0 );
309312
sizePolicy.setHeightForWidth( mPlot->sizePolicy().hasHeightForWidth() );
310313
mPlot->setSizePolicy( sizePolicy );
311314
mPlot->updateGeometry();
312-
313-
mPlotCurve = new QwtPlotCurve( "" );
314-
mPlotCurve->setSymbol( QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::white ),
315-
QPen( Qt::red, 2 ), QSize( 9, 9 ) ) );
316-
mPlotCurve->attach( mPlot );
317315
#else
318316
delete mPlot;
319317
mPlot = 0;
@@ -347,7 +345,9 @@ QgsIdentifyResultsDialog::~QgsIdentifyResultsDialog()
347345
if ( mActionPopup )
348346
delete mActionPopup;
349347
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
350-
delete mPlotCurve;
348+
foreach ( QgsIdentifyPlotCurve *curve, mPlotCurves )
349+
delete curve;
350+
mPlotCurves.clear();
351351
#endif
352352
}
353353

@@ -600,6 +600,48 @@ void QgsIdentifyResultsDialog::mapLayerActionDestroyed()
600600
}
601601
}
602602

603+
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
604+
QgsIdentifyPlotCurve::QgsIdentifyPlotCurve( const QMap<QString, QString> &attributes,
605+
QwtPlot* plot, const QString &title, QColor color )
606+
{
607+
mPlotCurve = new QwtPlotCurve( title );
608+
609+
if ( color == QColor() )
610+
{
611+
color = QgsVectorRandomColorRampV2::randomColors( 1 )[0];
612+
}
613+
mPlotCurve->setSymbol( QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::white ),
614+
QPen( color, 2 ), QSize( 9, 9 ) ) );
615+
616+
int i = 1;
617+
for ( QMap<QString, QString>::const_iterator it = attributes.begin();
618+
it != attributes.end(); ++it )
619+
{
620+
mPlotCurveXData.append( double( i++ ) );
621+
mPlotCurveYData.append( double( it.value().toDouble() ) );
622+
}
623+
mPlotCurve->setData( mPlotCurveXData, mPlotCurveYData );
624+
625+
mPlotCurve->attach( plot );
626+
627+
plot->setAxisMaxMinor( QwtPlot::xBottom, 0 );
628+
//mPlot->setAxisScale( QwtPlot::xBottom, 1, mPlotCurve->dataSize());
629+
//mPlot->setAxisScale( QwtPlot::yLeft, ymin, ymax );
630+
631+
plot->replot();
632+
plot->setVisible( true );
633+
}
634+
635+
QgsIdentifyPlotCurve::~QgsIdentifyPlotCurve()
636+
{
637+
if ( mPlotCurve )
638+
{
639+
mPlotCurve->detach();
640+
delete mPlotCurve;
641+
}
642+
}
643+
#endif
644+
603645
void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
604646
QString label,
605647
const QMap<QString, QString> &attributes,
@@ -741,20 +783,10 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
741783

742784
// graph
743785
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
744-
i = mPlotCurveXData.count();
745-
for ( QMap<QString, QString>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
786+
if ( attributes.count() > 0 )
746787
{
747-
mPlotCurveXData.append( double( ++i ) );
748-
mPlotCurveYData.append( double( it.value().toDouble() ) );
788+
mPlotCurves.append( new QgsIdentifyPlotCurve( attributes, mPlot, layer->name() ) );
749789
}
750-
mPlotCurve->setData( mPlotCurveXData, mPlotCurveYData );
751-
752-
mPlot->setAxisMaxMinor( QwtPlot::xBottom, 0 );
753-
//mPlot->setAxisScale( QwtPlot::xBottom, 1, mPlotCurve->dataSize());
754-
//mPlot->setAxisScale( QwtPlot::yLeft, ymin, ymax );
755-
756-
mPlot->replot();
757-
mPlot->setVisible( mPlotCurveXData.count() > 0 );
758790
#endif
759791
}
760792

@@ -1031,8 +1063,9 @@ void QgsIdentifyResultsDialog::clear()
10311063

10321064
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
10331065
mPlot->setVisible( false );
1034-
mPlotCurveXData.clear();
1035-
mPlotCurveYData.clear();
1066+
foreach ( QgsIdentifyPlotCurve *curve, mPlotCurves )
1067+
delete curve;
1068+
mPlotCurves.clear();
10361069
#endif
10371070

10381071
// keep it visible but disabled, it can switch from disabled/enabled

src/app/qgsidentifyresultsdialog.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ class APP_EXPORT QgsIdentifyResultsWebViewItem: public QObject, public QTreeWidg
9494
QgsIdentifyResultsWebView *mWebView;
9595
};
9696

97+
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
98+
class APP_EXPORT QgsIdentifyPlotCurve
99+
{
100+
public:
101+
102+
QgsIdentifyPlotCurve() { mPlotCurve = 0; }
103+
QgsIdentifyPlotCurve( const QMap<QString, QString> &attributes,
104+
QwtPlot* plot, const QString &title = QString(), QColor color = QColor() ) ;
105+
~QgsIdentifyPlotCurve();
106+
107+
private:
108+
QwtPlotCurve* mPlotCurve;
109+
QVector<double> mPlotCurveXData, mPlotCurveYData;
110+
};
111+
#endif
112+
97113
class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdentifyResultsBase
98114
{
99115
Q_OBJECT
@@ -230,8 +246,9 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
230246
QDockWidget *mDock;
231247

232248
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
233-
QwtPlotCurve* mPlotCurve;
234-
QVector<double> mPlotCurveXData, mPlotCurveYData;
249+
/* QwtPlotCurve* mPlotCurve; */
250+
/* QVector<double> mPlotCurveXData, mPlotCurveYData; */
251+
QVector<QgsIdentifyPlotCurve *> mPlotCurves;
235252
#endif
236253
};
237254

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -309,26 +309,33 @@ QgsStringMap QgsVectorRandomColorRampV2::properties() const
309309
return map;
310310
}
311311

312-
void QgsVectorRandomColorRampV2::updateColors()
312+
QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
313+
int hueMax, int hueMin, int satMax, int satMin, int valMax, int valMin )
313314
{
314315
int h, s, v;
316+
QList<QColor> colors;
315317

316-
mColors.clear();
317318
//start hue at random angle
318319
double currentHueAngle = 360.0 * ( double )rand() / RAND_MAX;
319320

320-
for ( int i = 0; i < mCount; i++ )
321+
for ( int i = 0; i < count; i++ )
321322
{
322323
//increment hue by golden ratio (approx 137.507 degrees)
323324
//as this minimises hue nearness as count increases
324325
//see http://basecase.org/env/on-rainbows for more details
325326
currentHueAngle += 137.50776;
326-
//scale hue to between mHueMax and mHueMin
327-
h = ( fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( mHueMax - mHueMin ) + mHueMin;
328-
s = ( rand() % ( mSatMax - mSatMin + 1 ) ) + mSatMin;
329-
v = ( rand() % ( mValMax - mValMin + 1 ) ) + mValMin;
330-
mColors.append( QColor::fromHsv( h, s, v ) );
327+
//scale hue to between hueMax and hueMin
328+
h = ( fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( hueMax - hueMin ) + hueMin;
329+
s = ( rand() % ( satMax - satMin + 1 ) ) + satMin;
330+
v = ( rand() % ( valMax - valMin + 1 ) ) + valMin;
331+
colors.append( QColor::fromHsv( h, s, v ) );
331332
}
333+
return colors;
334+
}
335+
336+
void QgsVectorRandomColorRampV2::updateColors()
337+
{
338+
mColors = QgsVectorRandomColorRampV2::randomColors( mCount, mHueMax, mHueMin, mSatMax, mSatMin, mValMax, mValMin );
332339
}
333340

334341
/////////////

src/core/symbology-ng/qgsvectorcolorrampv2.h

+6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ class CORE_EXPORT QgsVectorRandomColorRampV2 : public QgsVectorColorRampV2
131131

132132
virtual QgsStringMap properties() const;
133133

134+
/** get a list of random colors
135+
* @note added in 2.4 */
136+
static QList<QColor> randomColors( int count,
137+
int hueMax = DEFAULT_RANDOM_HUE_MAX, int hueMin = DEFAULT_RANDOM_HUE_MIN,
138+
int satMax = DEFAULT_RANDOM_SAT_MAX, int satMin = DEFAULT_RANDOM_SAT_MIN,
139+
int valMax = DEFAULT_RANDOM_VAL_MAX, int valMin = DEFAULT_RANDOM_VAL_MIN );
134140
void updateColors();
135141

136142
int count() const { return mCount; }

0 commit comments

Comments
 (0)