Go to the documentation of this file.00001 #ifndef PLOT_CURVE_H
00002 #define PLOT_CURVE_H
00003 #include <QDebug>
00004
00005 #include <qwt_plot.h>
00006
00007 #include <QAction>
00008 #include <QWidget>
00009 #include <QPushButton>
00010 #include <QMenu>
00011
00012 Q_DECLARE_METATYPE(QwtPlot::Axis)
00013 class AxisButton : public QPushButton {
00014 Q_OBJECT
00015 public:
00016 AxisButton(QWidget *parent = 0) : QPushButton(parent),
00017 yLeftAxisAction("Left Y-axis", this), yRightAxisAction("Right Y-axis", this)
00018 {
00019 yLeftAxisAction.setData(QVariant::fromValue(QwtPlot::yLeft));
00020 yRightAxisAction.setData(QVariant::fromValue(QwtPlot::yRight));
00021 setFlat(true);
00022 m.addAction(&yLeftAxisAction);
00023 m.addAction(&yRightAxisAction);
00024 setMenu(&m);
00025
00026 connect(&m, SIGNAL(triggered(QAction*)),
00027 this, SLOT(axisSelected(QAction*)));
00028 axisSelected(&yLeftAxisAction);
00029 }
00030
00031 void setCurrentAxis(QwtPlot::Axis axis){
00032 if (axis == axisOfAction(&yLeftAxisAction)) {
00033 axisSelected(&yLeftAxisAction);
00034 } else if (axis == axisOfAction(&yRightAxisAction)) {
00035 axisOfAction(&yRightAxisAction);
00036 } else {
00037 return;
00038 }
00039 }
00040
00041 private:
00042 QwtPlot::Axis axisOfAction(QAction *action) const {
00043 return action->data().value<QwtPlot::Axis>();
00044 }
00045
00046 private slots:
00047 void axisSelected(QAction *action){
00048 if (text() == action->text()) return;
00049
00050 setText(action->text());
00051 emit axisChanged(axisOfAction(action));
00052 }
00053 signals:
00054 void axisChanged(QwtPlot::Axis axis);
00055
00056 private:
00057 QMenu m;
00058
00059 QAction yLeftAxisAction;
00060 QAction yRightAxisAction;
00061 };
00062
00063 #include <qwt_legend_item.h>
00064 #include "qtcolorpicker.h"
00065
00066 #include <QVBoxLayout>
00067 #include <QLabel>
00068 #include <QPalette>
00069 class LegendItem : public QFrame {
00070 Q_OBJECT
00071 public:
00072 LegendItem(QWidget *parent = 0) : QFrame (parent){
00073 setFrameStyle(QFrame::Panel | QFrame::Raised);
00074 label.setTextFormat(Qt::PlainText);
00075 label.setContentsMargins(6,0,0,0);
00076 cPick.setFlat(true);
00077 cPick.setStandardColors();
00078 cPick.setColorDialogEnabled(true);
00079 setup();
00080
00081
00082 connect(&cPick, SIGNAL(colorChanged(QColor)),
00083 this, SLOT(slot_colorChanged(QColor)));
00084 connect(&m_axisButton, SIGNAL(axisChanged(QwtPlot::Axis)),
00085 this, SIGNAL(axisChanged(QwtPlot::Axis)));
00086 }
00087
00088 void setText(QString title){
00089 label.setText(title);
00090 }
00091
00092 QColor currentColor() const {
00093 return cPick.currentColor();
00094 }
00095
00096 void setCurrentColor(QColor color){
00097 cPick.setCurrentColor(color);
00098 }
00099
00100 QColor color(int index) const {
00101 return cPick.color(index);
00102 }
00103
00104 void setCurrentAxis(QwtPlot::Axis axis){
00105 m_axisButton.setCurrentAxis(axis);
00106 }
00107
00108 QVBoxLayout *layout(){
00109 Q_ASSERT(dynamic_cast<QVBoxLayout*>(QFrame::layout()));
00110 return dynamic_cast<QVBoxLayout*>(QFrame::layout());
00111 }
00112
00113 private:
00114 void setup(){
00115 setLayout(new QVBoxLayout);
00116 layout()->setSpacing(0);
00117 layout()->setContentsMargins(0,0,0,0);
00118
00119 QHBoxLayout *h = new QHBoxLayout();
00120 h->addWidget(&label);
00121 h->addWidget(&cPick);
00122
00123 layout()->addLayout(h);
00124 layout()->addWidget(&m_axisButton);
00125 }
00126
00127 private slots:
00128 void slot_colorChanged(const QColor &color){
00129 QPalette palette;
00130 palette.setColor(QPalette::ButtonText, color);
00131 palette.setColor(QPalette::WindowText, color);
00132 setPalette(palette);
00133 emit colorChanged(color);
00134 }
00135
00136 signals:
00137 void colorChanged(const QColor &color);
00138 void axisChanged(QwtPlot::Axis);
00139
00140 private:
00141 QLabel label;
00142 QtColorPicker cPick;
00143 AxisButton m_axisButton;
00144 };
00145
00146 #include <qwt_plot_curve.h>
00147 #include "cachedplotcurvedatamodel.h"
00148
00149 #include <QPen>
00150 #include <QObject>
00151 class Plot;
00152
00153 class PlotCurve : public QObject, public QwtPlotCurve
00154 {
00155 Q_OBJECT
00156
00157
00158
00159 public:
00160 PlotCurve();
00161 PlotCurve( const QwtText &title );
00162 PlotCurve( const QString &title );
00163 ~PlotCurve();
00164
00165 QWidget * legendItem() const {
00166 return new LegendItem();
00167 }
00168
00169 void updateLegend(QwtLegend *legend) const {
00170 if (!legend) return;
00171 Q_ASSERT(testItemAttribute(QwtPlotItem::Legend));
00172
00173 LegendItem *lgdItem = dynamic_cast<LegendItem*>(legend->find(this));
00174
00175
00176
00177 if (!lgdItem){
00178 lgdItem = dynamic_cast<LegendItem*>(legendItem());
00179 Q_ASSERT(lgdItem);
00180
00181
00182
00183
00184
00185 connect(lgdItem, SIGNAL(colorChanged(QColor)),
00186 this, SLOT(setColor(QColor)), Qt::QueuedConnection);
00187 connect(lgdItem, SIGNAL(axisChanged(QwtPlot::Axis)),
00188 this, SLOT(setAxis(QwtPlot::Axis)), Qt::QueuedConnection);
00189
00190
00191 QList<QColor> usedColors;
00192 foreach(QWidget *wItem, legend->legendItems()){
00193 LegendItem *item = dynamic_cast<LegendItem*>(wItem);
00194 Q_ASSERT(item);
00195 usedColors << item->currentColor();
00196 }
00197
00198 Q_ASSERT(lgdItem->color(0).isValid());
00199 lgdItem->setCurrentColor(lgdItem->color(0));
00200
00201 int i = 0;
00202 while(lgdItem->color(i).isValid()){
00203 if (usedColors.contains(lgdItem->color(i))) {
00204 i++;
00205 continue;
00206 } else {
00207 lgdItem->setCurrentColor(lgdItem->color(i));
00208 break;
00209 }
00210 }
00211
00212
00213 legend->insert(this, lgdItem);
00214
00215 }
00216
00217 Q_ASSERT(lgdItem);
00218 Q_ASSERT(lgdItem->updatesEnabled());
00219
00220 lgdItem->setText(title().text());
00221 }
00222
00223
00224
00225
00226 signals:
00227 void signal_curveAttached(PlotCurve *curve);
00228
00229
00230
00231
00232 public slots:
00233 void itemChanged(){
00234 QwtPlotCurve::itemChanged();
00235 }
00236
00237 void setColor(QColor color){
00238 QPen penCopy(pen());
00239 penCopy.setColor(color);
00240 setPen(penCopy);
00241 }
00242
00243 void setAxis(QwtPlot::Axis axis){
00244 if (axis == QwtPlot::xTop || axis == QwtPlot::xBottom){
00245 setXAxis(axis);
00246 return;
00247 }
00248 if (axis == QwtPlot::yLeft || axis == QwtPlot::yRight){
00249 setYAxis(axis);
00250 return;
00251 }
00252 Q_ASSERT(false);
00253 }
00254
00255
00256
00257
00258 public:
00259 void attach( Plot* plot );
00260 int closestPoint( const QPoint& pos, double* dist ) const;
00261 void setData(const CachedPlotCurveDataModel &dataModel){
00262 qDebug() << "Setting data to plot: " << this;
00263 QwtPlotCurve::setData(dataModel);
00264 CachedPlotCurveDataModel *model = dynamic_cast<CachedPlotCurveDataModel*>(&data());
00265 Q_ASSERT(model);
00266 connect(model, SIGNAL(signal_dataChanged()), this, SLOT(itemChanged()));
00267 }
00268
00269 protected:
00270
00271
00272
00273
00274
00275
00276
00277 };
00278
00279 #endif