1 /*
2 	Copyright (C) 2008, 2009 Andres Cabrera
3 	mantaraya36@gmail.com
4 
5 	This file is part of CsoundQt.
6 
7 	CsoundQt is free software; you can redistribute it
8 	and/or modify it under the terms of the GNU Lesser General Public
9 	License as published by the Free Software Foundation; either
10 	version 2.1 of the License, or (at your option) any later version.
11 
12 	CsoundQt is distributed in the hope that it will be useful,
13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 	GNU Lesser General Public License for more details.
16 
17 	You should have received a copy of the GNU Lesser General Public
18 	License along with Csound; if not, write to the Free Software
19 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 	02111-1307 USA
21 */
22 
23 #ifndef QUTEGRAPH_H
24 #define QUTEGRAPH_H
25 
26 #include "qutewidget.h"
27 #include "csoundengine.h"  //necessary for the CsoundUserData struct
28 #include "selectcolorbutton.h"
29 #include "curve.h"         // necessary for CurveType
30 
31 class Curve;
32 
33 enum GraphType { GRAPH_FTABLE=1, GRAPH_AUDIOSIGNAL=2, GRAPH_SPECTRUM=3 };
34 
35 
36 class QuteGraph : public QuteWidget
37 {
38 	Q_OBJECT
39 
40 //	Q_PROPERTY(bool QCS_grid MEMBER m_grid)
41 //	Q_PROPERTY(bool QCS_logx MEMBER m_logx)
42 //	Q_PROPERTY(bool QCS_logy MEMBER m_logy)
43 
44 public:
45 	QuteGraph(QWidget *parent);
46 
47 	~QuteGraph();
48 
49 	virtual QString getWidgetLine();
50 	virtual QString getWidgetXmlText();
51     virtual QString getWidgetType();
52 	virtual void setWidgetGeometry(int x,int y,int width,int height);
53 	void setValue(double value);
54     void setValue(QString text);
55 
56 	void clearCurves();
57 	void addCurve(Curve *curve);
58 	int getCurveIndex(Curve * curve);
59 	void setCurveData(Curve * curve);
setUd(CsoundUserData * ud)60     void setUd(CsoundUserData *ud) { m_ud = ud; }
showGrid(bool visible)61     void showGrid(bool visible) { m_drawGrid = visible; }
showTableInfo(bool state)62     void showTableInfo(bool state) {
63         m_drawTableInfo = state;
64         setProperty("QCS_showTableInfo", state?"true":"false");
65         if(m_label != nullptr) {
66             if(state)
67                 m_label->show();
68             else
69                 m_label->hide();
70         }
71     }
72     int findCurve(CurveType type, QString text);
73 	virtual void applyInternalProperties();
74     size_t spectrumGetPeak(Curve *curve, double freq, double relativeBandwidth);
75 
76 protected:
77 	CsoundUserData *m_ud;
78 	QLabel * m_label;
79 	QComboBox *m_pageComboBox;
80 	QLineEdit* name2LineEdit;
81 	QDoubleSpinBox *zoomxBox;
82 	QDoubleSpinBox *zoomyBox;
83     QCheckBox *acceptTablesCheckBox;
84     QCheckBox *acceptDisplaysCheckBox;
85     QCheckBox *showSelectorCheckBox;
86     QCheckBox *showGridCheckBox;
87     QCheckBox *showTableInfoCheckBox;
88     QCheckBox *showScrollbarsCheckBox;
89 
90 	QVector<Curve *> curves;
91 	QVector<QVector <QGraphicsLineItem *> > lines;
92 	QVector<QGraphicsPolygonItem *> polygons;
93     QVector<QPainterPath *>painterPaths;
94     QVector<GraphType>graphtypes;
95 
96 	QVector<QVector <QGraphicsLineItem *> > m_gridlines;
97     QVector<QVector <QGraphicsTextItem *> > m_gridTextsX;
98     QVector<QVector <QGraphicsTextItem *> > m_gridTextsY;
99 
100     // speactrum peak
101     QVector<QGraphicsTextItem*> m_spectrumPeakTexts;
102     QVector<QGraphicsRectItem*> m_spectrumPeakMarkers;
103 
104 
105     QPainterPath *gridPath;
106 
107 	virtual void refreshWidget();
108 	virtual void createPropertiesDialog();
109 	virtual void applyProperties();
110     virtual void keyPressEvent(QKeyEvent *ev);
111     virtual void mousePressEvent(QMouseEvent *ev);
112     // virtual void mouseReleaseEvent(QMouseEvent *ev);
113     // virtual void mouseMoveEvent(QMouseEvent *ev);
114 
115 public slots:
116 	void changeCurve(int index);
117 	void indexChanged(int index);
118     void mouseReleased();
119 
120 private:
121 	void drawFtable(Curve * curve, int index);
122     void drawFtablePath(Curve * curve, int index);
123 
124     void drawSpectrum(Curve * curve, int index);
125     void drawSpectrumPath(Curve * curve, int index);
126 
127     void drawSignal(Curve * curve, int index);
128     void drawSignalPath(Curve * curve, int index);
129 
130 	void scaleGraph(int index);
131 	int getTableNumForIndex(int index);
132     int getIndexForTableNum(int GRAPH_FTABLE);
133 	void setInternalValue(double value);
134     void drawGraph(Curve *curve, int index);
135     void showScrollbars(bool show);
136     void freezeSpectrum(bool status);
137 
138     QVector<double> frozenCurve;
139 
140     QGraphicsView* getView(int index);
141 
142     QMutex curveLock;
143 	bool m_grid;
144 	bool m_logx;
145 	bool m_logy;
146     bool m_drawGrid;
147     bool m_drawTableInfo;
148     int m_numticksY;
149     bool m_showScrollbars;
150     double m_showPeakCenterFrequency;
151     double m_showPeakRelativeBandwidth;
152     bool m_showPeak;
153     bool m_showPeakTemp;
154     bool m_frozen;
155     double m_lastPeakFreq;
156     double m_lastTextMarkerY;
157     double m_lastTextMarkerX;
158     QString m_getPeakChannel;
159     MYFLT *m_peakChannelPtr;
160     bool m_mouseDragging;
161     double m_showPeakTempFrequency;
162     double m_dbRange;
163     bool m_enableTables;
164     bool m_enableDisplays;
165 
166 
167 signals:
168     void requestUpdateCurve(Curve *curve);
169 
170 };
171 
172 class StackedLayoutWidget : public QStackedWidget
173 {
174 	Q_OBJECT
175 public:
StackedLayoutWidget(QWidget * parent)176 	StackedLayoutWidget(QWidget* parent) : QStackedWidget(parent)
177 	{
178 		setFrameShape(QFrame::StyledPanel);
179 	}
~StackedLayoutWidget()180 	~StackedLayoutWidget() {}
181 
setWidgetGeometry(int x,int y,int width,int height)182 	void setWidgetGeometry(int x,int y,int width,int height)
183 	{
184 		setGeometry(x,y,width, height);
185 		setMaximumSize(width, height);
186 	}
187 
clearCurves()188 	void clearCurves()
189 	{
190 		QWidget *widget;
191 		widget = currentWidget();
192         while (widget != nullptr) {
193 			removeWidget(widget);
194 			widget = currentWidget();
195 		}
196 	}
197 	/*
198   protected:
199 	virtual void contextMenuEvent(QContextMenuEvent *event)
200 	{emit(popUpMenu(event->globalPos()));}
201 
202   signals:
203 	void popUpMenu(QPoint pos);*/
204 };
205 
206 // ----------------------------------------------------------------------------------
207 
208 
209 // -----------------------------------------------------------------------------------
210 
211 class QuteTableWidget : public QWidget {
212     Q_OBJECT
213 
214 public:
215     QuteTableWidget(QWidget *parent = nullptr)
QWidget(parent)216         : QWidget(parent)
217         , m_tabnum(0)
218         , m_ud(nullptr)
219         , m_running(false)
220         , m_data(nullptr)
221         , m_tabsize(0)
222         , m_margin(8)
223         , m_maxy(1.0)
224         , m_autorange(true)
225         , m_path(nullptr)
226         , m_showGrid(true)
227     {}
228     virtual ~QuteTableWidget() override;
setUserData(CsoundUserData * ud)229     void setUserData(CsoundUserData *ud) { m_ud = ud; }
230     void updateData(int tabnum, bool check=true);
currentTableNumber()231     int currentTableNumber() { return m_tabnum; }
232     void updatePath();
setColor(QColor color)233     void setColor(QColor color) { m_color = color; }
234     void setRange(double maxy=1.0);
showGrid(bool show)235     void showGrid(bool show) { m_showGrid = show; }
236     void reset();
237     void paintGrid(QPainter *painter);
238 
239 protected:
240     virtual void paintEvent(QPaintEvent *event) override;
241 
242 private:
243     int m_tabnum;
244     CsoundUserData *m_ud;
245     bool m_running;
246     QColor m_color;
247     MYFLT *m_data;
248     int m_tabsize;
249     int m_margin;
250     double m_maxy;
251     bool m_autorange;
252     QPainterPath *m_path;
253     QMutex mutex;
254     bool m_showGrid;
255 
256 // public slot:
257 
258 };
259 
260 
261 class QuteTable : public QuteWidget {
262     Q_OBJECT
263 
264 public:
265     QuteTable(QWidget *parent);
266     ~QuteTable();
getWidgetLine()267     virtual QString getWidgetLine() { return QString(""); }
268     virtual QString getWidgetXmlText();
getWidgetType()269     virtual QString getWidgetType() { return QString("BSBTableDisplay"); }
270     // virtual void setWidgetGeometry(int x,int y,int width,int height);
271     virtual void applyInternalProperties();
272     virtual void createPropertiesDialog();
273     virtual void refreshWidget();
274 
275     virtual void setCsoundUserData(CsoundUserData *ud);
276     virtual void setValue(double value);
277     virtual void setValue(QString s);
278     virtual void setColor(QColor color);
279 
280 public slots:
281     void onStop();
282 
283 private:
284     int m_tabnum;
285 
286 protected:
287     // virtual void mousePressEvent(QMouseEvent *event);
288     // virtual void mouseReleaseEvent(QMouseEvent *event);
289 
290     virtual void applyProperties();
291     SelectColorButton *colorButton;
292     QCheckBox *rangeCheckBox;
293     QDoubleSpinBox *rangeSpinBox;
294     QCheckBox *gridCheckBox;
295     QMutex mutex;
296 
297 };
298 
299 class SpectralView : public QGraphicsView {
300     Q_OBJECT
301 
302 public:
303 
SpectralView(QWidget * parent)304     SpectralView(QWidget *parent) : QGraphicsView(parent) {}
305 
306     ~SpectralView();
307     // virtual void mousePressEvent(QMouseEvent *ev);
308     virtual void mouseReleaseEvent(QMouseEvent *ev);
309     // virtual void mouseMoveEvent(QMouseEvent *ev);
310 
311 signals:
312     void mouseReleased();
313 
314 };
315 
316 
317 #endif
318