SGSolve
sgcustomplot.hpp
1 // This file is part of the SGSolve library for stochastic games
2 // Copyright (C) 2019 Benjamin A. Brooks
3 //
4 // SGSolve free software: you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // SGSolve is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see
16 // <http://www.gnu.org/licenses/>.
17 //
18 // Benjamin A. Brooks
19 // ben@benjaminbrooks.net
20 // Chicago, IL
21 
22 #ifndef SGCUSTOMPLOT_HPP
23 #define SGCUSTOMPLOT_HPP
24 
25 #include "qcustomplot.h"
26 #include "sgpoint.hpp"
27 
29 
40 class SGCustomPlot : public QCustomPlot
41 {
42  Q_OBJECT;
43 private:
45  int state;
48 
49  QCPPlotTitle * title;
50  QCPRange nominalXRange;
51  QCPRange nominalYRange;
52  QCPRange realXRange;
53  QCPRange realYRange;
55 
57  QString path;
58 
60  QAction * inspectPointAction;
61 
63  QAction* simulateAction;
64 
66  QAction * savePNGAction;
68  QAction * savePDFAction;
69 
72 
73 public:
75  SGCustomPlot(): SGCustomPlot(0,false) {}
76 
78 
80  SGCustomPlot(int _state, bool _isDetailPlot);
81 
83  QCPPlotTitle * getTitle() {return title;}
84 
86  void adjustRanges();
87 
89  void setRanges(const QCPRange & xrange,
90  const QCPRange & yrange);
91 
93  const QCPRange & getNominalXRange() const
94  { return nominalXRange; }
95 
97  const QCPRange & getNominalYRange() const
98  { return nominalYRange; }
99 
101  void equalizeAxesScales();
102 
104  void setState(int newState)
105  { assert(state>=0); state = newState; }
107  int getState() const
108  { return state; }
109 
110 protected:
112 
113  void resizeEvent(QResizeEvent * event)
114  {
115  adjustRanges();
116  QCustomPlot::resizeEvent(event);
117  }
118 
119 private slots:
121 
123  void ShowContextMenu(const QPoint & pos)
124  {
125  lastContextPos = pos;
126 
127  QPoint globalPos = this->mapToGlobal(pos);
128 
129  QMenu contextMenu;
130  contextMenu.addAction(inspectPointAction);
131  contextMenu.addAction(simulateAction);
132  contextMenu.addSeparator();
133  contextMenu.addAction(savePDFAction);
134  contextMenu.addAction(savePNGAction);
135 
136  contextMenu.exec(globalPos);
137  }
138 
141  {
142  emit inspectPoint(SGPoint(xAxis->pixelToCoord(lastContextPos.x()),
143  yAxis->pixelToCoord(lastContextPos.y())),
145  }
146 
147  void simulationRequested()
148  {
149  emit simulateEquilibrium(SGPoint(xAxis->pixelToCoord(lastContextPos.x()),
150  yAxis->pixelToCoord(lastContextPos.y())),
152 
153  }
154 
156  void savePDF()
157  {
158  QString newPath = QFileDialog::getSaveFileName(this, tr("Save PDF"),
159  path, tr("PDF files (*.pdf)"));
160 
161  if (newPath.isEmpty())
162  return;
163 
164  QFileInfo fi(newPath);
165  path = fi.canonicalPath();
166 
167  savePdf(newPath);
168  }
169 
171  void savePNG()
172  {
173  QString newPath = QFileDialog::getSaveFileName(this, tr("Save PNG"),
174  path, tr("PNG files (*.png)"));
175 
176  if (newPath.isEmpty())
177  return;
178 
179  QFileInfo fi(newPath);
180  path = fi.canonicalPath();
181 
182  savePng(newPath);
183  }
184 
185 
186 private:
188 
189  virtual int heightForWidth(int w) const {return w;}
190 
192 
193  virtual bool hasHeightForWidth() const {return true;}
194 
196  virtual QSize minimumSizeHint() const { return QSize(100,100); }
197 
198 signals:
200  void inspectPoint(SGPoint point,
201  int state,
202  bool isDetailPlot);
203 
206  int state,
207  bool isDetailPlot);
208 
209 };
210 
211 #endif
SGCustomPlot::realYRange
QCPRange realYRange
Definition: sgcustomplot.hpp:53
SGCustomPlot::realXRange
QCPRange realXRange
Definition: sgcustomplot.hpp:52
SGCustomPlot::minimumSizeHint
virtual QSize minimumSizeHint() const
Custom minimum size.
Definition: sgcustomplot.hpp:196
SGCustomPlot::nominalYRange
QCPRange nominalYRange
Definition: sgcustomplot.hpp:51
SGCustomPlot::savePNG
void savePNG()
Saves graph as a PNG.
Definition: sgcustomplot.hpp:171
SGCustomPlot::adjustRanges
void adjustRanges()
Updates ranges when window is resized.
Definition: sgcustomplot.cpp:94
SGCustomPlot::savePDF
void savePDF()
Saves graph as a PDF.
Definition: sgcustomplot.hpp:156
SGCustomPlot::setRanges
void setRanges(const QCPRange &xrange, const QCPRange &yrange)
Sets the nominal ranges.
Definition: sgcustomplot.cpp:70
SGCustomPlot::heightForWidth
virtual int heightForWidth(int w) const
Reimplement heightForWidth.
Definition: sgcustomplot.hpp:189
SGCustomPlot::getState
int getState() const
Gets the state associated with the plot.
Definition: sgcustomplot.hpp:107
SGCustomPlot::lastContextPos
QPoint lastContextPos
Stores the last location at which a context menu was requested.
Definition: sgcustomplot.hpp:71
SGCustomPlot::nominalXRange
QCPRange nominalXRange
Definition: sgcustomplot.hpp:50
SGCustomPlot::getNominalYRange
const QCPRange & getNominalYRange() const
Returns the nominal Y range.
Definition: sgcustomplot.hpp:97
SGCustomPlot::inspectPoint
void inspectPoint(SGPoint point, int state, bool isDetailPlot)
Signal to inspect the given point.
SGCustomPlot::resizeEvent
void resizeEvent(QResizeEvent *event)
Reimplement resizeEvent.
Definition: sgcustomplot.hpp:113
SGCustomPlot::savePDFAction
QAction * savePDFAction
Pointer to the QAction for saving PDF files.
Definition: sgcustomplot.hpp:68
SGCustomPlot::ShowContextMenu
void ShowContextMenu(const QPoint &pos)
Slot for showing context menu.
Definition: sgcustomplot.hpp:123
SGCustomPlot::path
QString path
Save file path.
Definition: sgcustomplot.hpp:57
SGCustomPlot::setState
void setState(int newState)
Sets the state associated with the plot.
Definition: sgcustomplot.hpp:104
SGCustomPlot::isDetailPlot
bool isDetailPlot
Indicates if this is the detail plot.
Definition: sgcustomplot.hpp:47
SGCustomPlot::SGCustomPlot
SGCustomPlot()
Constructor.
Definition: sgcustomplot.hpp:75
SGCustomPlot
A customized version of QCustomPlot.
Definition: sgcustomplot.hpp:41
SGCustomPlot::simulateAction
QAction * simulateAction
Action for forward simulating.
Definition: sgcustomplot.hpp:63
SGCustomPlot::state
int state
Indicates the state that this plot is associated with.
Definition: sgcustomplot.hpp:42
SGPoint
A vector in .
Definition: sgpoint.hpp:35
SGCustomPlot::pointInspected
void pointInspected()
Point inspected.
Definition: sgcustomplot.hpp:140
SGCustomPlot::savePNGAction
QAction * savePNGAction
Pointer to the QAction for saving PNG files.
Definition: sgcustomplot.hpp:66
SGCustomPlot::title
QCPPlotTitle * title
Definition: sgcustomplot.hpp:49
SGCustomPlot::simulateEquilibrium
void simulateEquilibrium(SGPoint point, int state, bool isDetailPlot)
Signal to simulate the given equilibrium forwards.
SGCustomPlot::inspectPointAction
QAction * inspectPointAction
Inspect a point.
Definition: sgcustomplot.hpp:60
SGCustomPlot::getNominalXRange
const QCPRange & getNominalXRange() const
Returns the nominal X range.
Definition: sgcustomplot.hpp:93
SGCustomPlot::hasHeightForWidth
virtual bool hasHeightForWidth() const
Reimplement hasHeighForWidth.
Definition: sgcustomplot.hpp:193
SGCustomPlot::equalizeAxesScales
void equalizeAxesScales()
Normalize ranges.
Definition: sgcustomplot.cpp:79
SGCustomPlot::getTitle
QCPPlotTitle * getTitle()
Returns the title.
Definition: sgcustomplot.hpp:83