SciDAVis  1.D4
Graph.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Graph.h
3  Project : SciDAVis
4  --------------------------------------------------------------------
5  Copyright : (C) 2006 by Ion Vasilief, Tilman Benkert
6  Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
7  Description : Graph widget
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 #ifndef GRAPH_H
30 #define GRAPH_H
31 
32 #include <QList>
33 #include <QPointer>
34 #include <QPrinter>
35 #include <QVector>
36 #include <QEvent>
37 #include <QMap>
38 
39 #include <qwt_plot.h>
40 #include <qwt_plot_marker.h>
41 #include <qwt_plot_curve.h>
42 
43 #include "Plot.h"
44 #include "Table.h"
45 #include "AxesDialog.h"
46 #include "PlotToolInterface.h"
47 #include "core/column/Column.h"
48 
49 class QwtPlotCurve;
50 class QwtPlotZoomer;
51 class QwtPieCurve;
52 class Table;
53 class Legend;
54 class ArrowMarker;
55 class ImageMarker;
56 class TitlePicker;
57 class ScalePicker;
58 class CanvasPicker;
59 class ApplicationWindow;
60 class Matrix;
62 class RangeSelectorTool;
63 class DataCurve;
64 class PlotCurve;
65 class QwtErrorPlotCurve;
66 
68 typedef struct{
69  int lCol;
70  int lWidth;
71  int lStyle;
72  int filledArea;
73  int aCol;
74  int aStyle;
75  int symCol;
76  int fillCol;
77  int penWidth;
78  int sSize;
79  int sType;
81 } CurveLayout;
82 
114 class Graph: public QWidget
115 {
116  Q_OBJECT
117 
118  public:
119  Graph (QWidget* parent=0, const char* name=0, Qt::WFlags f=0);
120  ~Graph();
121 
122  enum AxisType{Numeric = 0, Txt = 1, Day = 2, Month = 3, Time = 4, Date = 5, ColHeader = 6, DateTime = 22};
123  enum MarkerType{None = -1, Text = 0, Arrow = 1, Image = 2};
127 
129  QwtPlotZoomer *d_zoomer[2];
133 
134  static int mapToQwtAxis(int axis);
135 
137  QString parentPlotName();
138 
140  void setActiveTool(PlotToolInterface *tool);
143 
144  Grid *grid(){return d_plot->grid();};
145 
146  public slots:
148  Plot* plotWidget() const {return d_plot;};
149  void copy(ApplicationWindow *parent, Graph* g);
150 
152 
153 
154  bool isPiePlot() const {return (c_type.count() == 1 && c_type[0] == Pie);};
155  void plotPie(Table* w,const QString& name, int startRow = 0, int endRow = -1);
157  void plotPie(Table* w,const QString& name, const QPen& pen, int brush, int size, int firstColor, int startRow = 0, int endRow = -1, bool visible = true);
158  void removePie();
159  QString pieLegendText();
160  QString savePieCurveLayout();
162 
163  bool insertCurvesList(Table* w, const QStringList& names, int style, int lWidth, int sSize, int startRow = 0, int endRow = -1);
164  bool insertCurve(Table* w, const QString& name, int style, int startRow = 0, int endRow = -1);
165  bool insertCurve(Table* w, int xcol, const QString& name, int style);
166  bool insertCurve(Table* w, const QString& xColName, const QString& yColName, int style, int startRow = 0, int endRow = -1);
167  void insertPlotItem(QwtPlotItem *i, int type);
168 
170  void showCurve(int index, bool visible = true);
171  int visibleCurves();
172 
174  void removeCurve(int index);
178  void removeCurve(const QString& s);
182  void removeCurves(const QString& s);
183 
184  void updateCurvesData(Table* w, const QString& yColName);
185 
186  int curves() const {return n_curves;};
187  bool validCurvesDataSize() const;
188  double selectedXStartValue();
189  double selectedXEndValue();
190 
191  long curveKey(int curve){return c_keys[curve];}
192  int curveIndex(long key) const {return c_keys.indexOf(key);}
194  int curveIndex(QwtPlotCurve *c) const;
196  int curveIndex(const QString &title) const {return plotItemsList().indexOf(title);}
198  QwtPlotCurve* curve(int index) const;
200  QwtPlotCurve* curve(const QString &title) const {return curve(curveIndex(title));}
201 
203  QStringList analysableCurvesList();
205  QStringList curvesList();
207  QStringList plotItemsList() const;
209  QwtPlotItem* plotItem(int index);
211  int plotItemIndex(QwtPlotItem *it) const;
212 
213  void updateCurveNames(const QString& oldName, const QString& newName, bool updateTableName = true);
214 
215  int curveType(int curveIndex);
217  static bool canConvertTo(QwtPlotCurve *curve, CurveType type);
219 
223  void setCurveType(int curve, CurveType type, bool update=true);
224  void setCurveFullRange(int curveIndex);
225 
227 
228  void print();
229  void setScaleOnPrint(bool on){d_scale_on_print = on;};
230  void printCropmarks(bool on){d_print_cropmarks = on;};
231 
232  void copyImage();
233  QPixmap graphPixmap();
235  void exportToFile(const QString& fileName);
236  void exportSVG(const QString& fname);
237  void exportVector(const QString& fileName, int res = 0, bool color = true,
238  bool keepAspect = true, QPrinter::PageSize pageSize = QPrinter::Custom,
239  QPrinter::Orientation orientation = QPrinter::Portrait);
240  void exportImage(const QString& fileName, int quality = 100, bool transparent = false);
242 
243  void replot(){d_plot->replot();};
244  void updatePlot();
245 
247 
248  bool addErrorBars(const QString& xColName, const QString& yColName, Table *errTable,
249  const QString& errColName, int type = 1, int width = 1, int cap = 8, const QColor& color = QColor(Qt::black),
250  bool through = true, bool minus = true, bool plus = true);
251 
252  bool addErrorBars(const QString& yColName, Table *errTable, const QString& errColName,
253  int type = 1, int width = 1, int cap = 8, const QColor& color = QColor(Qt::black),
254  bool through = true, bool minus = true, bool plus = true);
255 
256  void updateErrorBars(QwtErrorPlotCurve *er, bool xErr,int width, int cap, const QColor& c, bool plus, bool minus, bool through);
257 
261  DataCurve* masterCurve(const QString& xColName, const QString& yColName);
263 
265 
266  void contextMenuEvent(QContextMenuEvent *);
267  void closeEvent(QCloseEvent *e);
268  bool focusNextPrevChild ( bool next );
270 
272  void setScale(int axis, double start, double end, double step = 0.0,
273  int majorTicks = 5, int minorTicks = 5, int type = 0, bool inverted = false);
274  double axisStep(int axis){return d_user_step[axis];};
275 
277 
278  CurveLayout initCurveLayout(int style, int curves = 0);
279  static CurveLayout initCurveLayout();
281  void updateCurveLayout(int index,const CurveLayout *cL);
283  void guessUniqueCurveLayout(int& colorIndex, int& symbolIndex);
285 
287 
288  void zoomed (const QwtDoubleRect &);
289  void zoom(bool on);
290  void zoomOut();
291  bool zoomOn();
293 
294  void setAutoScale();
295  void updateScale();
296 
298 
299  QString saveToString(bool saveAsTemplate = false);
300  QString saveScale();
301  QString saveScaleTitles();
302  QString saveFonts();
303  QString saveMarkers();
304  QString saveCurveLayout(int index);
305  QString saveAxesTitleColors();
306  QString saveAxesColors();
307  QString saveEnabledAxes();
308  QString saveCanvas();
309  QString saveTitle();
310  QString saveAxesTitleAlignement();
311  QString saveEnabledTickLabels();
312  QString saveTicksType();
313  QString saveCurves();
314  QString saveLabelsFormat();
315  QString saveLabelsRotation();
316  QString saveAxesLabelsType();
317  QString saveAxesBaseline();
318  QString saveAxesFormulas();
320 
322 
323  void drawText(bool on);
324  bool drawTextActive(){return drawTextOn;};
325  long insertTextMarker(Legend* mrk);
326 
328  long insertTextMarker(const QStringList& list, int fileVersion);
329  void updateTextMarker(const QString& text,int angle, int bkg,const QFont& fnt,
330  const QColor& textColor, const QColor& backgroundColor);
331 
336  void setTextMarkerDefaults(int f, const QFont &font, const QColor& textCol, const QColor& backgroundCol);
337 
339  void setCopiedMarkerEnds(const QPoint& start, const QPoint& end);
340  void setCopiedTextOptions(int bkg, const QString& text, const QFont& font,
341  const QColor& color, const QColor& bkgColor);
342  void setCopiedArrowOptions(int width, Qt::PenStyle style, const QColor& color,
343  bool start, bool end, int headLength, int headAngle, bool filledHead);
344  void setCopiedImageName(const QString& fn){auxMrkFileName=fn;};
345  QRect copiedMarkerRect(){return QRect(auxMrkStart, auxMrkEnd);};
346  QVector<int> textMarkerKeys(){return d_texts;};
347  Legend* textMarker(long id);
348 
349  void addTimeStamp();
350 
351  void removeLegend();
352  void removeLegendItem(int index);
353  void addLegendItem(const QString& colName);
354  void insertLegend(const QStringList& lst, int fileVersion);
355  Legend *legend();
356  Legend *newLegend();
357  Legend *newLegend(const QString& text);
358  bool hasLegend(){return legendMarkerID >= 0;};
359 
361  QString legendText();
363 
365 
366  ArrowMarker* arrow(long id);
367  void addArrow(ArrowMarker* mrk);
368 
370  void addArrow(QStringList list, int fileVersion);
371  QVector<int> lineMarkerKeys(){return d_lines;};
372 
374  void drawLine(bool on, bool arrow = FALSE);
375  bool drawArrow(){return drawArrowOn;};
376  bool drawLineActive(){return drawLineOn;};
377 
384 
385  void setArrowDefaults(int lineWidth, const QColor& c, Qt::PenStyle style,
386  int headLength, int headAngle, bool fillHead);
387  bool arrowMarkerSelected();
389 
391 
392  ImageMarker* imageMarker(long id);
393  QVector<int> imageMarkerKeys(){return d_images;};
395  ImageMarker* addImage(const QString& fileName);
396 
397  void insertImageMarker(const QStringList& lst, int fileVersion);
398  bool imageMarkerSelected();
399  void updateImageMarker(int x, int y, int width, int height);
401 
403 
404  void removeMarker();
405  void cutMarker();
406  void copyMarker();
407  void pasteMarker();
410 
411  long selectedMarkerKey();
418  void setSelectedMarker(long mrk, bool add=false);
419  QwtPlotMarker* selectedMarkerPtr();
420  bool markerSelected();
422  void deselectMarker();
425 
427 
428  QList<int> axesType();
429 
430  QStringList scalesTitles();
431  void setXAxisTitle(const QString& text);
432  void setYAxisTitle(const QString& text);
433  void setRightAxisTitle(const QString& text);
434  void setTopAxisTitle(const QString& text);
435  void setAxisTitle(int axis, const QString& text);
436  QString axisTitle(int axis) { return d_plot->axisTitle(axis).text(); }
437 
438  QFont axisTitleFont(int axis);
439  void setXAxisTitleFont(const QFont &fnt);
440  void setYAxisTitleFont(const QFont &fnt);
441  void setRightAxisTitleFont(const QFont &fnt);
442  void setTopAxisTitleFont(const QFont &fnt);
443  void setAxisTitleFont(int axis,const QFont &fnt);
444 
445  void setAxisFont(int axis,const QFont &fnt);
446  QFont axisFont(int axis);
447  void initFonts(const QFont &scaleTitleFnt,const QFont &numbersFnt);
448 
449  QColor axisTitleColor(int axis);
450  void setXAxisTitleColor(const QColor& c);
451  void setYAxisTitleColor(const QColor& c);
452  void setRightAxisTitleColor(const QColor& c);
453  void setTopAxisTitleColor(const QColor& c);
454  void setAxesTitleColor(QStringList l);
455 
456  int axisTitleAlignment (int axis);
457  void setXAxisTitleAlignment(int align);
458  void setYAxisTitleAlignment(int align);
459  void setTopAxisTitleAlignment(int align);
460  void setRightAxisTitleAlignment(int align);
461  void setAxesTitlesAlignment(const QStringList& align);
462 
463  QColor axisColor(int axis);
464  QStringList axesColors();
465  void setAxesColors(const QStringList& colors);
466 
467  QColor axisNumbersColor(int axis);
468  QStringList axesNumColors();
469  void setAxesNumColors(const QStringList& colors);
470 
471  void showAxis(int axis, int type, const QString& formatInfo, Table *table, bool axisOn,
472  int majTicksType, int minTicksType, bool labelsOn, const QColor& c, int format,
473  int prec, int rotation, int baselineDist, const QString& formula, const QColor& labelsColor);
474 
475  void enableAxis(int axis, bool on = true);
476  QVector<bool> enabledAxes();
477  void enableAxes(QVector<bool> axesOn);
478  void enableAxes(const QStringList& list);
479 
480  int labelsRotation(int axis);
481  void setAxisLabelRotation(int axis, int rotation);
482 
483  QStringList enabledTickLabels();
484  void setEnabledTickLabels(const QStringList& list);
485 
486  void setAxesLinewidth(int width);
488  void loadAxesLinewidth(int width);
489 
490  void drawAxesBackbones(bool yes);
493  void loadAxesOptions(const QString& s);
494 
495  QList<int> axesBaseline();
496  void setAxesBaseline(const QList<int> &lst);
497  void setAxesBaseline(QStringList &lst);
498 
499  void setMajorTicksType(const QList<int>& lst);
500  void setMajorTicksType(const QStringList& lst);
501 
502  void setMinorTicksType(const QList<int>& lst);
503  void setMinorTicksType(const QStringList& lst);
504 
505  int minorTickLength();
506  int majorTickLength();
507  void setAxisTicksLength(int axis, int majTicksType, int minTicksType,
508  int minLength, int majLength);
509  void setTicksLength(int minLength, int majLength);
510  void changeTicksLength(int minLength, int majLength);
511 
512  void setLabelsNumericFormat(const QStringList& l);
513  void setLabelsNumericFormat(int axis, const QStringList& l);
514  void setLabelsNumericFormat(int axis, int format, int prec = 6, const QString& formula = QString());
515  void setLabelsDateTimeFormat(int axis, int type, const QString& formatInfo);
516  void setLabelsDayFormat(int axis, int format);
517  void setLabelsMonthFormat(int axis, int format);
518 
519  QString axisFormatInfo(int axis);
520  QStringList axesLabelsFormatInfo(){return axesFormatInfo;};
521 
522  void setLabelsTextFormat(int axis, const Column *column, int startRow, int endRow);
523  void setLabelsTextFormat(int axis, Table *table, const QString& columnName);
524  void setLabelsColHeaderFormat(int axis, Table *table);
525 
526  QStringList getAxesFormulas(){return axesFormulas;};
527  void setAxesFormulas(const QStringList& l){axesFormulas = l;};
528  void setAxisFormula(int pos, const QString &f){axesFormulas[pos] = f;};
530 
532 
533  void drawCanvasFrame(bool frameOn, int width);
534  void drawCanvasFrame(const QStringList& frame);
535  void drawCanvasFrame(bool frameOn, int width, const QColor& color);
536  QColor canvasFrameColor();
537  int canvasFrameWidth();
538  bool framed();
540 
542 
543  void setTitle(const QString& t);
544  void setTitleFont(const QFont &fnt);
545  void setTitleColor(const QColor &c);
546  void setTitleAlignment(int align);
547 
548  bool titleSelected();
549  void selectTitle();
550 
551  void removeTitle();
552  void initTitle( bool on, const QFont& fnt);
554 
556 
557  int selectedCurveID();
559  QString selectedCurveTitle();
561 
562  void disableTools();
563 
570  bool enableRangeSelectors(const QObject *status_target=NULL, const char *status_slot="");
571 
573  bool rangeSelectorsEnabled() const { return !d_range_selector.isNull(); }
574 
576 
577  void setMargin (int d);
578  void setFrame(int width = 1, const QColor& color = QColor(Qt::black));
579  void setBackgroundColor(const QColor& color);
580  void setCanvasBackground(const QColor& color);
582 
583  void addFitCurve(QwtPlotCurve *c);
584  void deleteFitCurves();
585  QList<QwtPlotCurve *> fitCurvesList(){return d_fit_curves;};
590  int range(int index, double *start, double *end);
591 
593  void setBarsGap(int curve, int gapPercent, int offset);
594 
596 
597  void showIntensityTable();
599 
601 
602  bool modifyFunctionCurve(ApplicationWindow * parent, int curve, int type, const QStringList &formulas, const QString &var,QList<double> &ranges, int points);
603  bool addFunctionCurve(ApplicationWindow *parent, int type, const QStringList &formulas, const QString& var,
604  QList<double> &ranges, int points, const QString& title = QString::null);
606  bool insertFunctionCurve(ApplicationWindow * parent, const QStringList& func_spec, int points, int fileVersion);
608  QString generateFunctionName(const QString& name = tr("F"));
610 
612  void createTable(const QString& curveName);
613  void createTable(const QwtPlotCurve* curve);
614  void activateGraph();
615 
617 
618  void plotVectorCurve(Table* w, const QStringList& colList, int style, int startRow = 0, int endRow = -1);
619  void updateVectorsLayout(int curve, const QColor& color, int width, int arrowLength, int arrowAngle, bool filled, int position,
620  const QString& xEndColName = QString(), const QString& yEndColName = QString());
622 
624 
625  void openBoxDiagram(Table *w, const QStringList& l, int fileVersion);
626  void plotBoxDiagram(Table *w, const QStringList& names, int startRow = 0, int endRow = -1);
628 
629  bool plotHistogram(Table *w, QStringList names, int startRow=0, int endRow=-1);
630 
631  void setCurveSymbol(int index, const QwtSymbol& s);
632  void setCurvePen(int index, const QPen& p);
633  void setCurveBrush(int index, const QBrush& b);
634  void setCurveStyle(int index, int s);
635 
637 
640  void resizeEvent(QResizeEvent *e);
641  void scaleFonts(double factor);
643 
644  void notifyChanges();
645 
646  void updateSecondaryAxis(int axis);
647  void enableAutoscaling(bool yes){m_autoscale = yes;};
648 
650  void setAutoscaleFonts(bool yes){autoScaleFonts = yes;};
651 
652  static int obsoleteSymbolStyle(int type);
653  static QString penStyleName(Qt::PenStyle style);
654  static Qt::PenStyle getPenStyle(const QString& s);
655  static Qt::PenStyle getPenStyle(int style);
656  static Qt::BrushStyle getBrushStyle(int style);
657  static void showPlotErrorMessage(QWidget *parent, const QStringList& emptyColumns);
658  static QPrinter::PageSize minPageSize(const QPrinter& printer, const QRect& r);
659 
660  void showTitleContextMenu();
661  void copyTitle();
662  void cutTitle();
663 
664  void removeAxisTitle();
665  void cutAxisTitle();
666  void copyAxisTitle();
667  void showAxisTitleMenu(int axis);
668  void showAxisContextMenu(int axis);
669  void hideSelectedAxis();
670  void showGrids();
671 
673  void showGrid();
675  void showGrid(int axis);
676 
677  void showAxisDialog();
678  void showScaleDialog();
679 
681  void plotSpectrogram(Matrix *m, CurveType type);
683  void restoreSpectrogram(ApplicationWindow *app, const QStringList& lst);
684 
685  bool antialiasing(){return d_antialiasing;};
687  void setAntialiasing(bool on = true, bool update = true);
688 
689  void deselect();
690  void print(QPainter *, const QRect &rect, const QwtPlotPrintFilter & = QwtPlotPrintFilter());
691  void printCanvas(QPainter *painter, const QRect &canvasRect,
692  const QwtScaleMap map[QwtPlot::axisCnt], const QwtPlotPrintFilter &pfilter) const;
693 
694 signals:
695  void selectedGraph (Graph*);
696  void closedGraph();
697  void drawTextOff();
698  void drawLineEnded(bool);
699  void cursorInfo(const QString&);
700  void showPlotDialog(int);
701  void createTable(const QString&,const QString&,QList<Column*>);
702 
703  void viewImageDialog();
704  void viewTextDialog();
705  void viewLineDialog();
706  void viewTitleDialog();
707  void modifiedGraph();
708  void hiddenPlot(QWidget*);
709 
711  void showContextMenu();
712  void showCurveContextMenu(int);
713  void showMarkerPopupMenu();
714 
715  void showAxisDialog(int);
716  void axisDblClicked(int);
717  void xAxisTitleDblClicked();
718  void yAxisTitleDblClicked();
720  void topAxisTitleDblClicked();
721 
722  void createIntensityTable(const QString&);
723  void dataRangeChanged();
724  void showFitResults(const QString&);
725 
726  private:
728  QList<QwtPlotCurve *>d_fit_curves;
734  QStringList axesFormulas;
736  QStringList axesFormatInfo;
737  QList <int> axisType;
739  QwtPlotMarker::LineStyle mrklStyle;
740 
742  QVector<double> d_user_step;
744  QVector<int> c_type;
746  QVector<int> c_keys;
748  QVector<int> d_lines;
750  QVector<int> d_images;
752  QVector<int> d_texts;
753 
758  Qt::PenStyle auxMrkStyle;
760 
761  int n_curves;
768 
771 
775  Qt::PenStyle defaultArrowLineStyle;
776 
778  QPointer<SelectionMoveResizer> d_markers_selector;
780  QPointer<RangeSelectorTool> d_range_selector;
783 };
784 #endif // GRAPH_H