class library: PriorityQueue - implement removeValue, hide array
[supercollider.git] / QtCollider / widgets / QcGraph.h
blob2746cde6365f543aef1ce798e7a4c998a3e89674
1 /************************************************************************
3 * Copyright 2010 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program 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 General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #ifndef QC_GRAPH_H
23 #define QC_GRAPH_H
25 #include "../Common.h"
26 #include "../QcHelper.h"
28 #include <QWidget>
30 struct QcGraphElement {
32 friend class QcGraphModel;
34 enum CurveType {
35 Step,
36 Linear,
37 Sine,
38 Welch,
39 Exponential,
40 Curvature
43 QcGraphElement() :
44 fillColor( QColor(0,0,0) ),
45 curveType( Linear ),
46 curvature( 0.f ),
47 editable( true ),
48 selected( false ),
49 _prev(0),
50 _next(0)
51 {};
53 void setCurveType( QVariant data ) {
54 if( data.type() == QVariant::String ) {
55 QString curveName = data.toString();
56 //printf("curve name: %s\n",curveName.toStdString().c_str());
57 if( curveName == "step" ) curveType = Step;
58 else if( curveName == "linear" ) curveType = Linear;
59 else if( curveName == "exponential" ) curveType = Exponential;
60 else if( curveName == "sine" ) curveType = Sine;
61 else if( curveName == "welch" ) curveType = Welch;
63 else {
64 //printf("curvature: %f\n", data.value<float>());
65 curveType = Curvature;
66 curvature = data.value<float>();
70 QcGraphElement *prev() { return _prev; }
71 QcGraphElement *next() { return _next; }
73 QPointF value;
74 QString text;
75 QColor fillColor;
76 CurveType curveType;
77 float curvature;
78 bool editable;
79 bool selected;
81 private:
82 QcGraphElement *_prev;
83 QcGraphElement *_next;
86 class QcGraphModel : public QObject
88 Q_OBJECT
90 public:
92 QcGraphModel( QObject * parent = 0 ) : QObject(parent) {}
94 struct Connection {
95 Connection( QcGraphElement * a_ = 0, QcGraphElement * b_ = 0 ) : a(a_), b(b_) {}
96 QcGraphElement * a;
97 QcGraphElement * b;
100 inline QList<QcGraphElement*> elements() const { return _elems; }
102 inline QcGraphElement *elementAt( int i ) const { return _elems[i]; }
104 inline int elementCount() const { return _elems.count(); }
106 void append( QcGraphElement * e );
108 void removeAt( int i );
110 inline QList<Connection> connections() const { return _conns; }
112 void connect( int xi, int yi ) {
113 QcGraphElement *x = _elems[xi];
114 QcGraphElement *y = _elems[yi];
115 int i;
116 for( i = 0; i < _conns.count(); ++i ) {
117 Connection c = _conns[i];
118 if( (c.a==x && c.b==y) || (c.a==y && c.b==x) ) return;
120 _conns.append( Connection(x,y) );
123 Q_SIGNALS:
124 void appended( QcGraphElement * );
125 void removed( QcGraphElement * );
127 private:
129 QList<Connection> _conns;
130 QList<QcGraphElement*> _elems;
133 class QcGraph : public QWidget, QcHelper
135 Q_OBJECT
136 Q_PROPERTY( VariantList value READ value WRITE setValue )
137 Q_PROPERTY( VariantList strings READ dummyVariantList WRITE setStrings );
138 Q_PROPERTY( int index READ index WRITE setIndex );
139 Q_PROPERTY( float thumbSize READ dummyFloat WRITE setThumbSize );
140 Q_PROPERTY( float thumbWidth READ dummyFloat WRITE setThumbWidth );
141 Q_PROPERTY( float thumbHeight READ dummyFloat WRITE setThumbHeight );
142 Q_PROPERTY( QColor strokeColor READ dummyColor WRITE setStrokeColor );
143 Q_PROPERTY( QColor fillColor READ dummyColor WRITE setFillColor );
144 Q_PROPERTY( QColor selectionColor READ dummyColor WRITE setSelectionColor );
145 Q_PROPERTY( QColor gridColor READ dummyColor WRITE setGridColor );
146 Q_PROPERTY( bool drawLines READ dummyBool WRITE setDrawLines );
147 Q_PROPERTY( bool drawRects READ dummyBool WRITE setDrawRects );
148 Q_PROPERTY( bool editable READ dummyBool WRITE setEditable );
149 Q_PROPERTY( double step READ step WRITE setStep );
150 Q_PROPERTY( int selectionForm READ selectionForm WRITE setSelectionForm );
151 Q_PROPERTY( int horizontalOrder READ horizontalOrder WRITE setHorizontalOrder );
152 Q_PROPERTY( float x READ currentX WRITE setCurrentX );
153 Q_PROPERTY( float y READ currentY WRITE setCurrentY );
154 Q_PROPERTY( QPointF grid READ grid WRITE setGrid );
155 Q_PROPERTY( bool gridOn READ dummyBool WRITE setGridOn );
157 public:
158 Q_INVOKABLE void connectElements( int, VariantList );
159 Q_INVOKABLE void setCurves( const VariantList & curves );
160 Q_INVOKABLE void setStringAt( int, const QString & );
161 Q_INVOKABLE void setFillColorAt( int, const QColor & );
162 Q_INVOKABLE void setEditableAt( int, bool );
164 public Q_SLOTS:
165 Q_INVOKABLE void select( int index, bool exclusive = true );
166 Q_INVOKABLE void deselect( int index );
167 Q_INVOKABLE void deselectAll();
169 Q_SIGNALS:
170 void action();
171 void metaAction();
173 public:
174 enum SelectionForm {
175 ElasticSelection,
176 RigidSelection
179 enum Order {
180 NoOrder,
181 RigidOrder
184 public:
185 QcGraph();
187 VariantList value() const;
188 int index() const { return _curIndex; }
189 float currentX() const;
190 float currentY() const;
191 QPointF grid() const { return _gridMetrics; }
193 void setValue( const VariantList & );
194 void setStrings( const VariantList &list );
196 void setIndex( int i );
197 void setCurrentX( float );
198 void setCurrentY( float );
199 void setThumbSize( float f ) { _thumbSize = QSize(f,f); update(); }
200 void setThumbWidth( float f ) { _thumbSize.setWidth(f); update(); }
201 void setThumbHeight( float f ) { _thumbSize.setHeight(f); update(); }
202 void setStrokeColor( const QColor & c ) { _strokeColor = c; update(); }
203 void setFillColor( const QColor & c );
205 void setSelectionColor( const QColor & c ) { _selColor = c; update(); }
206 void setGridColor( const QColor & c ) { _gridColor = c; update(); }
207 void setDrawLines( bool b ) { _drawLines = b; update(); }
208 void setDrawRects( bool b ) { _drawRects = b; update(); }
209 void setEditable( bool b ) { _editable = b; update(); }
211 double step() const { return _step; }
212 void setStep( double );
213 int selectionForm() const { return (int)_selectionForm; }
214 void setSelectionForm( int i ) { _selectionForm = (SelectionForm) i; }
215 int horizontalOrder() const { return (int)_xOrder; }
216 void setHorizontalOrder( int i );
217 void setGrid( const QPointF &pt ) { _gridMetrics = pt; update(); }
218 void setGridOn( bool b ) { _gridOn = b; update(); }
219 QSize sizeHint() const { return QSize( 200,200 ); }
220 QSize minimumSizeHint() const { return QSize( 50,50 ); }
222 private Q_SLOTS:
223 void onElementRemoved( QcGraphElement *e );
225 private:
226 struct SelectedElement {
227 SelectedElement( QcGraphElement *e ) : elem(e) {}
228 bool operator == (const SelectedElement & other) {
229 return elem == other.elem;
232 QcGraphElement *elem;
233 QPointF moveOrigin; // in data domain
236 private:
237 void setAllDeselected();
238 void setIndexSelected( int index, bool selected );
239 void restrictValue( QPointF & );
240 void orderRestrictValue( QcGraphElement *, QPointF &, bool selected );
241 void setValue( QcGraphElement *, const QPointF & );
242 void ensureOrder();
243 void moveFree( QcGraphElement *, const QPointF & );
244 void moveOrderRestricted( QcGraphElement *, const QPointF & );
245 void moveSelected( const QPointF & dValue, SelectionForm, bool fromCache );
246 QPointF pos( const QPointF & value );
247 QPointF value( const QPointF & pos );
248 void addCurve( QPainterPath &, QcGraphElement *e1, QcGraphElement *e2 );
249 void paintEvent( QPaintEvent * );
250 void mousePressEvent( QMouseEvent * );
251 void mouseMoveEvent( QMouseEvent * );
252 void keyPressEvent( QKeyEvent * );
253 void doAction( Qt::KeyboardModifiers );
255 QcGraphModel _model;
257 QSize _thumbSize;
258 QColor _strokeColor;
259 QColor _selColor;
260 QColor _gridColor;
261 QPointF _gridMetrics;
262 bool _gridOn;
264 bool _drawLines;
265 bool _drawRects;
267 bool _editable;
268 double _step;
269 SelectionForm _selectionForm;
270 Order _xOrder;
272 int _curIndex;
274 struct Selection {
275 Selection () : cached(false), shallMove(false) {}
276 int size() { return elems.size(); }
277 int count() { return elems.count(); }
279 QList<SelectedElement> elems;
280 bool cached;
281 bool shallMove;
282 QPointF moveOrigin; // in data domain
283 } _selection;
286 #endif