1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#ifndef CC_QCUSTOMPLOT_HEADER
#define CC_QCUSTOMPLOT_HEADER
//QCustomPlot
#include <qcustomplot.h>
//System
#include <assert.h>
/*********************************/
/*** Custom QCustomPlot wigets ***/
/*********************************/
//! QCustomPlot: vertical bar with text along side
class QCPBarsWithText : public QCPBars
{
Q_OBJECT
public:
QCPBarsWithText(QCPAxis* keyAxis, QCPAxis* valueAxis) : QCPBars(keyAxis,valueAxis), m_textOnTheLeft(false) {}
void setText(QString text) { m_text = QStringList(text); }
void appendText(QString text) { m_text.append(text); }
void setTextAlignment(bool left) { m_textOnTheLeft = left; }
protected:
QStringList m_text;
bool m_textOnTheLeft;
// reimplemented virtual draw method
virtual void draw(QCPPainter *painter)
{
if (!mKeyAxis || !mValueAxis) { qDebug() << Q_FUNC_INFO << "invalid key or value axis"; return; }
//switch to standard display
QCPBars::draw(painter);
int fontHeight = painter->fontMetrics().height();
if (!mData->isEmpty())
{
double& key = mData->begin()->key;
double& value = mData->begin()->value;
QPointF P = coordsToPixels(key, value);
//apply a small shift
int margin = 5; //in pixels
if (m_textOnTheLeft)
margin = -margin;
P.setX(P.x() + margin);
//we draw at the 'base' line
P.setY(P.y() + fontHeight);
for (int i=0; i<m_text.size(); ++i)
{
QPointF Pstart = P;
if (m_textOnTheLeft)
Pstart.setX(P.x() - painter->fontMetrics().width(m_text[i]));
painter->drawText(Pstart,m_text[i]);
P.setY(P.y() + fontHeight);
}
}
}
};
//! QCustomPlot: colored histogram
class QCPColoredBars : public QCPBars
{
Q_OBJECT
public:
class QCPColoredBarData : public QCPBarData
{
public:
QCPColoredBarData()
: QCPBarData()
, color(Qt::blue)
{}
QColor color;
};
typedef QMap<double, QCPColoredBarData> QCPColoredBarDataMap;
QCPColoredBars(QCPAxis *keyAxis, QCPAxis *valueAxis) : QCPBars(keyAxis,valueAxis) {}
void setData(const QVector<double> &key, const QVector<double> &value)
{
//no colors? we switch to the standard QCPBars object
m_coloredData.clear();
QCPBars::setData(key,value);
}
void setData(const QVector<double> &key, const QVector<double> &value, const QVector<QColor>& colors)
{
Q_ASSERT(colors.size() == key.size());
mData->clear(); //we duplicate the structures so that other stuff in QCPBarData works!
int n = qMin(key.size(), value.size());
for (int i=0; i<n; ++i)
{
QCPColoredBarData newData;
newData.key = key[i];
newData.value = value[i];
if (colors.size() > i)
newData.color = colors[i];
m_coloredData.insertMulti(newData.key, newData);
mData->insertMulti(newData.key, newData);
}
}
inline QRect rect() const { return clipRect(); }
// reimplemented virtual methods:
virtual void clearData() { QCPBars::clearData(); m_coloredData.clear(); }
protected:
// reimplemented virtual draw method
virtual void draw(QCPPainter *painter)
{
//no colors?
if (m_coloredData.empty())
{
//switch to standard display
QCPBars::draw(painter);
}
if (!mKeyAxis || !mValueAxis) { qDebug() << Q_FUNC_INFO << "invalid key or value axis"; return; }
QCPColoredBarDataMap::const_iterator it;
for (it = m_coloredData.constBegin(); it != m_coloredData.constEnd(); ++it)
{
// skip bar if not visible in key axis range:
if (it.key()+mWidth*0.5 < mKeyAxis.data()->range().lower || it.key()-mWidth*0.5 > mKeyAxis.data()->range().upper)
continue;
QPolygonF barPolygon = getBarPolygon(it.key(), it.value().value);
// draw bar fill:
if (mainBrush().style() != Qt::NoBrush && mainBrush().color().alpha() != 0)
{
QBrush brush = mainBrush();
brush.setColor(it.value().color);
applyFillAntialiasingHint(painter);
painter->setPen(Qt::NoPen);
painter->setBrush(brush);
painter->drawPolygon(barPolygon);
}
// draw bar line:
if (mainPen().style() != Qt::NoPen && mainPen().color().alpha() != 0)
{
QPen pen = mainPen();
pen.setColor(it.value().color);
applyDefaultAntialiasingHint(painter);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
painter->drawPolyline(barPolygon);
}
}
}
QCPColoredBarDataMap m_coloredData;
};
//! QCustomPlot: selectable cursor interface
class QCPSelectableCursor : public QCPAbstractPlottable
{
Q_OBJECT
public:
//! Default constructor
explicit QCPSelectableCursor(QCPAxis *keyAxis, QCPAxis *valueAxis)
: QCPAbstractPlottable(keyAxis, valueAxis)
, mCurrentVal(0)
, mMinVal(0)
, mMaxVal(0)
, mLastPos(-1,-1)
, mLastRadius(0)
{}
//! Returns whether the item is "selectable" when the mouse is clicked at a given position
inline virtual bool isSelectable(QPoint click) const
{
if (mLastPos.x() < 0 || mLastPos.y() < 0)
return false;
QPoint d = mLastPos - click;
return (d.x()*d.x() + d.y()*d.y() <= mLastRadius*mLastRadius);
}
// getters
inline double currentVal() const { return mCurrentVal; }
inline double minVal() const { return mMinVal; }
inline double maxVal() const { return mMaxVal; }
inline void range(double& minVal, double& maxVal) const { minVal = mMinVal; maxVal = mMaxVal; }
// setters
inline void setCurrentVal(double val) { mCurrentVal = std::max(std::min(val,mMaxVal),mMinVal); }
inline void setRange(double minVal, double maxVal) { mMinVal = minVal; mMaxVal = maxVal; }
//! Converts a pixel value (X) to the equivalent key
inline double pixelToKey(int pixX) const { return keyAxis() ? keyAxis()->pixelToCoord(pixX) : 0; }
//! Converts a pixel value (Y) to the equivalent value
inline double pixelToValue(int pixY) const { return valueAxis() ? valueAxis()->pixelToCoord(pixY) : 0; }
// reimplemented virtual methods:
virtual void clearData() {}
virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const { return -1; } //we don't use the QCP internal selection mechanism!
protected:
// reimplemented virtual methods:
virtual void drawLegendIcon(QCPPainter *painter, const QRectF &rect) const {}
virtual QCPRange getKeyRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const { foundRange = false; return QCPRange(); }
virtual QCPRange getValueRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const { foundRange = false; return QCPRange(); }
// property members:
double mCurrentVal;
double mMinVal,mMaxVal;
QPoint mLastPos;
int mLastRadius;
};
//! QCustomPlot: greyed areas
class QCPHiddenArea : public QCPSelectableCursor
{
Q_OBJECT
public:
explicit QCPHiddenArea(bool leftSide, QCPAxis *keyAxis, QCPAxis *valueAxis)
: QCPSelectableCursor(keyAxis, valueAxis)
, mLeftSide(leftSide)
{
mPen = QPen(QColor(80, 80, 80),Qt::SolidLine); // dark grey
mPen.setWidth(2);
mSelectedPen = mPen;
mBrush = QBrush(Qt::white,Qt::SolidPattern); // white
mSelectedBrush = mBrush;
}
protected:
// reimplemented virtual methods:
virtual void draw(QCPPainter *painter)
{
if (!keyAxis())
return;
QRect rect = clipRect();
double currentPosd = keyAxis()->coordToPixel(mCurrentVal);
if (mLeftSide)
{
int x2 = static_cast<int>(ceil(currentPosd));
assert(x2 >= rect.x());
rect.setWidth(x2 - rect.x());
}
else
{
int x1 = static_cast<int>(floor(currentPosd));
assert(x1 >= rect.x());
int newWidth = rect.width() - (x1 - rect.x());
rect.setX(x1);
rect.setWidth(newWidth);
}
// draw greyed rect
if ( (mLeftSide && mCurrentVal > mMinVal)
|| (!mLeftSide && mCurrentVal < mMaxVal) )
{
applyFillAntialiasingHint(painter);
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(QColor(128, 128, 128, 128),Qt::SolidPattern)); // semi-transparent grey
painter->drawPolygon(rect);
}
//draw circle (handle)
if (mainPen().style() != Qt::NoPen && mainPen().color().alpha() != 0)
{
//circle
QPoint C(mLeftSide ? rect.x()+rect.width() : rect.x(), rect.y()+rect.height()/2);
int r = rect.height() / 10;
painter->setPen(mainPen());
painter->setBrush(mainBrush());
painter->drawEllipse(C,r,r);
painter->setPen(QPen(QColor(128, 128, 128, 128),Qt::SolidLine)); // semi-transparent grey
painter->drawLine(C+QPoint(0,r),C-QPoint(0,r));
//save last circle position
mLastPos = C;
mLastRadius = r;
}
else
{
//no circle
mLastPos = QPoint(-1,-1);
mLastRadius = 0;
}
}
//! Whether the cursor is displayed on the left side or not
bool mLeftSide;
};
//! QCustomPlot: small arrows at the bottom
class QCPArrow : public QCPSelectableCursor
{
Q_OBJECT
public:
explicit QCPArrow(QCPAxis *keyAxis, QCPAxis *valueAxis)
: QCPSelectableCursor(keyAxis, valueAxis)
{
mPen.setColor(QColor(128, 128, 0)); // dark yellow
mPen.setStyle(Qt::SolidLine);
mPen.setWidth(2);
mSelectedPen = mPen;
mBrush.setColor(QColor(255, 255, 0, 196)); // semi-transparent yellow
mBrush.setStyle(Qt::SolidPattern);
mSelectedBrush = mBrush;
}
//! Sets triangle 'inside' color
void setColor(int r, int g, int b)
{
mBrush.setColor(QColor(r, g, b, 196)); // semi-transparent color
mSelectedBrush = mBrush;
}
protected:
// reimplemented virtual methods:
virtual void draw(QCPPainter *painter)
{
if (!keyAxis())
return;
QRect rect = clipRect();
int currentPos = static_cast<int>(keyAxis()->coordToPixel(mCurrentVal));
int r = rect.height() / 10;
// draw dashed line
{
QPen pen(QColor(128, 128, 128, 128),Qt::DashLine);
pen.setWidth(1);
painter->setPen(pen); // semi-transparent grey
painter->drawLine(QPoint(currentPos,rect.y()+2*r), QPoint(currentPos,rect.y()+rect.height()));
}
//draw triangle(handle)
if (mainPen().style() != Qt::NoPen && mainPen().color().alpha() != 0)
{
//QPoint O(currentPos,rect.y() + rect.height() - r);
//QPoint T[3] = { O - QPoint(0,r), O + QPoint(r,r), O + QPoint(-r,r) };
QPoint O(currentPos,rect.y() + r);
QPoint T[3] = { O + QPoint(0,r), O - QPoint(r,r), O - QPoint(-r,r) };
painter->setPen(mainPen());
painter->setBrush(mainBrush());
painter->drawPolygon(T,3);
//save last circle position
mLastPos = O;
mLastRadius = r;
}
else
{
//no circle
mLastPos = QPoint(-1,-1);
mLastRadius = 0;
}
}
};
#endif //CC_QCUSTOMPLOT_HEADER
|