common: win32utils - compile fix
[supercollider.git] / QtCollider / widgets / QcGraph.h
blob2132e24675287aabf9120be12dc6b0edf34aae4d
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"
27 #include "../style/style.hpp"
29 #include <QWidget>
31 struct QcGraphElement {
33 friend class QcGraphModel;
35 enum CurveType {
36 Step = 0,
37 Linear,
38 Sine,
39 Welch,
40 Exponential,
41 Quadratic,
42 Cubic,
43 Curvature
46 QcGraphElement() :
47 curveType( Linear ),
48 curvature( 0.0 ),
49 editable( true ),
50 selected( false ),
51 _prev(0),
52 _next(0)
53 {};
55 void setCurveType( CurveType type, double curve = 0.0 ) {
56 curveType = type;
57 curvature = curve;
60 QcGraphElement *prev() { return _prev; }
61 QcGraphElement *next() { return _next; }
63 QPointF value;
64 QString text;
65 QColor fillColor;
66 CurveType curveType;
67 double curvature;
68 bool editable;
69 bool selected;
71 private:
72 QcGraphElement *_prev;
73 QcGraphElement *_next;
76 class QcGraphModel : public QObject
78 Q_OBJECT
80 public:
82 QcGraphModel( QObject * parent = 0 ) : QObject(parent) {}
84 struct Connection {
85 Connection( QcGraphElement * a_ = 0, QcGraphElement * b_ = 0 ) : a(a_), b(b_) {}
86 QcGraphElement * a;
87 QcGraphElement * b;
90 inline QList<QcGraphElement*> elements() const { return _elems; }
92 inline QcGraphElement *elementAt( int i ) const { return _elems[i]; }
94 inline int elementCount() const { return _elems.count(); }
96 void append( QcGraphElement * e );
98 void removeAt( int i );
100 inline QList<Connection> connections() const { return _conns; }
102 void connect( int xi, int yi ) {
103 QcGraphElement *x = _elems[xi];
104 QcGraphElement *y = _elems[yi];
105 int i;
106 for( i = 0; i < _conns.count(); ++i ) {
107 Connection c = _conns[i];
108 if( (c.a==x && c.b==y) || (c.a==y && c.b==x) ) return;
110 _conns.append( Connection(x,y) );
113 Q_SIGNALS:
114 void appended( QcGraphElement * );
115 void removed( QcGraphElement * );
117 private:
119 QList<Connection> _conns;
120 QList<QcGraphElement*> _elems;
123 class QcGraph : public QWidget, QcHelper, QtCollider::Style::Client
125 Q_OBJECT
126 Q_PROPERTY( VariantList value READ value WRITE setValue )
127 Q_PROPERTY( VariantList strings READ dummyVariantList WRITE setStrings );
128 Q_PROPERTY( int index READ index WRITE setIndex );
129 Q_PROPERTY( float thumbSize READ dummyFloat WRITE setThumbSize );
130 Q_PROPERTY( float thumbWidth READ dummyFloat WRITE setThumbWidth );
131 Q_PROPERTY( float thumbHeight READ dummyFloat WRITE setThumbHeight );
132 Q_PROPERTY( QColor strokeColor READ dummyColor WRITE setStrokeColor );
133 Q_PROPERTY( QColor fillColor READ dummyColor WRITE setFillColor );
134 Q_PROPERTY( QColor gridColor READ dummyColor WRITE setGridColor );
135 Q_PROPERTY( bool drawLines READ dummyBool WRITE setDrawLines );
136 Q_PROPERTY( bool drawRects READ dummyBool WRITE setDrawRects );
137 Q_PROPERTY( bool editable READ dummyBool WRITE setEditable );
138 Q_PROPERTY( double step READ step WRITE setStep );
139 Q_PROPERTY( int selectionForm READ selectionForm WRITE setSelectionForm );
140 Q_PROPERTY( int horizontalOrder READ horizontalOrder WRITE setHorizontalOrder );
141 Q_PROPERTY( float x READ currentX WRITE setCurrentX );
142 Q_PROPERTY( float y READ currentY WRITE setCurrentY );
143 Q_PROPERTY( QPointF grid READ grid WRITE setGrid );
144 Q_PROPERTY( bool gridOn READ dummyBool WRITE setGridOn );
145 Q_PROPERTY( QColor focusColor READ focusColor WRITE setFocusColor );
147 public:
148 Q_INVOKABLE void connectElements( int, VariantList );
149 Q_INVOKABLE void setStringAt( int, const QString & );
150 Q_INVOKABLE void setFillColorAt( int, const QColor & );
151 Q_INVOKABLE void setEditableAt( int, bool );
152 Q_INVOKABLE void setCurves( double curvature );
153 Q_INVOKABLE void setCurves( int type );
154 Q_INVOKABLE void setCurves( const VariantList & curves );
156 public Q_SLOTS:
157 Q_INVOKABLE void select( int index, bool exclusive = true );
158 Q_INVOKABLE void deselect( int index );
159 Q_INVOKABLE void deselectAll();
161 Q_SIGNALS:
162 void action();
163 void metaAction();
165 public:
166 enum SelectionForm {
167 ElasticSelection,
168 RigidSelection
171 enum Order {
172 NoOrder,
173 RigidOrder
176 public:
177 QcGraph();
179 VariantList value() const;
180 int index() const { return _curIndex; }
181 float currentX() const;
182 float currentY() const;
183 QPointF grid() const { return _gridMetrics; }
185 void setValue( const VariantList & );
186 void setStrings( const VariantList &list );
188 void setIndex( int i );
189 void setCurrentX( float );
190 void setCurrentY( float );
191 void setThumbSize( float f ) { _thumbSize = QSize(f,f); update(); }
192 void setThumbWidth( float f ) { _thumbSize.setWidth(f); update(); }
193 void setThumbHeight( float f ) { _thumbSize.setHeight(f); update(); }
194 void setStrokeColor( const QColor & c ) { _strokeColor = c; update(); }
195 void setFillColor( const QColor & c );
197 void setGridColor( const QColor & c ) { _gridColor = c; update(); }
198 void setDrawLines( bool b ) { _drawLines = b; update(); }
199 void setDrawRects( bool b ) { _drawRects = b; update(); }
200 void setEditable( bool b ) { _editable = b; update(); }
202 double step() const { return _step; }
203 void setStep( double );
204 int selectionForm() const { return (int)_selectionForm; }
205 void setSelectionForm( int i ) { _selectionForm = (SelectionForm) i; }
206 int horizontalOrder() const { return (int)_xOrder; }
207 void setHorizontalOrder( int i );
208 void setGrid( const QPointF &pt ) { _gridMetrics = pt; update(); }
209 void setGridOn( bool b ) { _gridOn = b; update(); }
210 QSize sizeHint() const { return QSize( 200,200 ); }
211 QSize minimumSizeHint() const { return QSize( 50,50 ); }
213 private Q_SLOTS:
214 void onElementRemoved( QcGraphElement *e );
216 private:
217 struct SelectedElement {
218 SelectedElement( QcGraphElement *e ) : elem(e) {}
219 bool operator == (const SelectedElement & other) {
220 return elem == other.elem;
223 QcGraphElement *elem;
224 QPointF moveOrigin; // in data domain
227 private:
228 void setAllDeselected();
229 void setIndexSelected( int index, bool selected );
230 void restrictValue( QPointF & );
231 void orderRestrictValue( QcGraphElement *, QPointF &, bool selected );
232 void setValue( QcGraphElement *, const QPointF & );
233 void ensureOrder();
234 void moveFree( QcGraphElement *, const QPointF & );
235 void moveOrderRestricted( QcGraphElement *, const QPointF & );
236 void moveSelected( const QPointF & dValue, SelectionForm, bool fromCache );
237 void addCurve( QPainterPath &, QcGraphElement *e1, QcGraphElement *e2 );
238 QRectF labelRect( QcGraphElement *, const QPointF &, const QRect &, const QFontMetrics & );
239 void paintEvent( QPaintEvent * );
240 void mousePressEvent( QMouseEvent * );
241 void mouseMoveEvent( QMouseEvent * );
242 void keyPressEvent( QKeyEvent * );
243 void doAction( Qt::KeyboardModifiers );
245 QcGraphModel _model;
247 QSize _thumbSize;
248 QColor _strokeColor;
249 QColor _gridColor;
250 QPointF _gridMetrics;
251 bool _gridOn;
252 QColor _focusColor;
254 bool _drawLines;
255 bool _drawRects;
257 bool _editable;
258 double _step;
259 SelectionForm _selectionForm;
260 Order _xOrder;
262 int _curIndex;
264 struct Selection {
265 Selection () : cached(false), shallMove(false) {}
266 int size() { return elems.size(); }
267 int count() { return elems.count(); }
269 QList<SelectedElement> elems;
270 bool cached;
271 bool shallMove;
272 QPointF moveOrigin; // in data domain
273 } _selection;
276 #endif