scide: implement selectionLength for openDocument
[supercollider.git] / QtCollider / widgets / QcGraph.h
blobbd22837cb1a0bf744c130b67da2dba3d08d73bd7
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( const QSize & sz ) :
47 size( sz ),
48 curveType( Linear ),
49 curvature( 0.0 ),
50 editable( true ),
51 selected( false ),
52 _prev(0),
53 _next(0)
54 {};
56 void setCurveType( CurveType type, double curve = 0.0 ) {
57 curveType = type;
58 curvature = curve;
61 QcGraphElement *prev() { return _prev; }
62 QcGraphElement *next() { return _next; }
64 QPointF value;
65 QSize size;
66 QString text;
67 QColor fillColor;
68 CurveType curveType;
69 double curvature;
70 bool editable;
71 bool selected;
73 private:
74 QcGraphElement *_prev;
75 QcGraphElement *_next;
78 class QcGraphModel : public QObject
80 Q_OBJECT
82 public:
84 QcGraphModel( QObject * parent = 0 ) : QObject(parent) {}
86 struct Connection {
87 Connection( QcGraphElement * a_ = 0, QcGraphElement * b_ = 0 ) : a(a_), b(b_) {}
88 QcGraphElement * a;
89 QcGraphElement * b;
92 inline QList<QcGraphElement*> elements() const { return _elems; }
94 inline QcGraphElement *elementAt( int i ) const { return _elems[i]; }
96 inline int elementCount() const { return _elems.count(); }
98 void append( QcGraphElement * e );
100 void removeAt( int i );
102 inline QList<Connection> connections() const { return _conns; }
104 void connect( int xi, int yi ) {
105 QcGraphElement *x = _elems[xi];
106 QcGraphElement *y = _elems[yi];
107 int i;
108 for( i = 0; i < _conns.count(); ++i ) {
109 Connection c = _conns[i];
110 if( (c.a==x && c.b==y) || (c.a==y && c.b==x) ) return;
112 _conns.append( Connection(x,y) );
115 Q_SIGNALS:
116 void appended( QcGraphElement * );
117 void removed( QcGraphElement * );
119 private:
121 QList<Connection> _conns;
122 QList<QcGraphElement*> _elems;
125 class QcGraph : public QWidget, QcHelper, QtCollider::Style::Client
127 Q_OBJECT
128 Q_ENUMS( ElementStyle )
129 Q_PROPERTY( VariantList value READ value WRITE setValue )
130 Q_PROPERTY( VariantList strings READ dummyVariantList WRITE setStrings );
131 Q_PROPERTY( int index READ index WRITE setIndex );
132 Q_PROPERTY( int lastIndex READ lastIndex );
133 Q_PROPERTY( int thumbSize READ dummyInt WRITE setThumbSize );
134 Q_PROPERTY( int thumbWidth READ dummyInt WRITE setThumbWidth );
135 Q_PROPERTY( int thumbHeight READ dummyInt WRITE setThumbHeight );
136 Q_PROPERTY( QColor background READ background WRITE setBackground );
137 Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor );
138 Q_PROPERTY( QColor fillColor READ dummyColor WRITE setFillColor );
139 Q_PROPERTY( QColor gridColor READ gridColor WRITE setGridColor );
140 Q_PROPERTY( QColor focusColor READ focusColor WRITE setFocusColor );
141 Q_PROPERTY( QColor selectionColor READ selectionColor WRITE setSelectionColor );
142 Q_PROPERTY( bool drawLines READ dummyBool WRITE setDrawLines );
143 Q_PROPERTY( bool drawRects READ dummyBool WRITE setDrawRects );
144 Q_PROPERTY( ElementStyle style READ elementStyle WRITE setElementStyle );
145 Q_PROPERTY( bool editable READ dummyBool WRITE setEditable );
146 Q_PROPERTY( double step READ step WRITE setStep );
147 Q_PROPERTY( int selectionForm READ selectionForm WRITE setSelectionForm );
148 Q_PROPERTY( int horizontalOrder READ horizontalOrder WRITE setHorizontalOrder );
149 Q_PROPERTY( float x READ currentX WRITE setCurrentX );
150 Q_PROPERTY( float y READ currentY WRITE setCurrentY );
151 Q_PROPERTY( QPointF grid READ grid WRITE setGrid );
152 Q_PROPERTY( bool gridOn READ dummyBool WRITE setGridOn );
154 public:
155 Q_INVOKABLE void connectElements( int, VariantList );
156 Q_INVOKABLE void setStringAt( int, const QString & );
157 Q_INVOKABLE void setFillColorAt( int, const QColor & );
158 Q_INVOKABLE void setEditableAt( int, bool );
159 Q_INVOKABLE void setThumbHeightAt( int, int );
160 Q_INVOKABLE void setThumbWidthAt( int, int );
161 Q_INVOKABLE void setThumbSizeAt( int, int );
162 Q_INVOKABLE void setCurves( double curvature );
163 Q_INVOKABLE void setCurves( int type );
164 Q_INVOKABLE void setCurves( const VariantList & curves );
166 public Q_SLOTS:
167 Q_INVOKABLE void select( int index, bool exclusive = true );
168 Q_INVOKABLE void deselect( int index );
169 Q_INVOKABLE void deselectAll();
171 Q_SIGNALS:
172 void action();
173 void metaAction();
175 public:
176 enum SelectionForm {
177 ElasticSelection,
178 RigidSelection
181 enum Order {
182 NoOrder,
183 RigidOrder
186 enum ElementStyle {
187 DotElements,
188 RectElements,
191 public:
192 QcGraph();
194 VariantList value() const;
195 QcGraphElement *currentElement() const;
196 int index() const;
197 int lastIndex() const { return _lastIndex; }
198 float currentX() const;
199 float currentY() const;
200 QPointF grid() const { return _gridMetrics; }
202 void setValue( const VariantList & );
203 void setStrings( const VariantList &list );
205 void setIndex( int i );
206 void setCurrentX( float );
207 void setCurrentY( float );
208 void setThumbSize( int f );
209 void setThumbWidth( int f );
210 void setThumbHeight( int f );
212 const QColor & background() const
213 { return _bkg.isValid() ? _bkg : palette().color(QPalette::Base); }
214 void setBackground( const QColor &c ) { _bkg = c; update(); }
216 const QColor & selectionColor() const
217 { return _selectColor.isValid() ? _selectColor : palette().color(QPalette::Highlight); }
218 void setSelectionColor( const QColor &c ) { _selectColor = c; update(); }
220 const QColor & strokeColor() const
221 { return _strokeColor.isValid() ? _strokeColor : palette().color(QPalette::Text); }
222 void setStrokeColor( const QColor & c ) { _strokeColor = c; update(); }
224 void setFillColor( const QColor & c );
226 QColor gridColor() const
228 if(_gridColor.isValid())
229 return _gridColor;
230 else {
231 QColor c = palette().color(QPalette::Text);
232 c.setAlpha(40);
233 return c;
236 void setGridColor( const QColor & c ) { _gridColor = c; update(); }
238 ElementStyle elementStyle() const { return _style; }
239 void setElementStyle(ElementStyle s) { _style = s; _geometryDirty = true; update(); }
240 void setDrawLines( bool b ) { _drawLines = b; update(); }
241 void setDrawRects( bool b ) { _drawRects = b; update(); }
242 void setEditable( bool b ) { _editable = b; update(); }
244 double step() const { return _step; }
245 void setStep( double );
246 int selectionForm() const { return (int)_selectionForm; }
247 void setSelectionForm( int i ) { _selectionForm = (SelectionForm) i; }
248 int horizontalOrder() const { return (int)_xOrder; }
249 void setHorizontalOrder( int i );
250 void setGrid( const QPointF &pt ) { _gridMetrics = pt; update(); }
251 void setGridOn( bool b ) { _gridOn = b; update(); }
252 QSize sizeHint() const { return QSize( 200,200 ); }
253 QSize minimumSizeHint() const { return QSize( 50,50 ); }
255 private Q_SLOTS:
256 void onElementRemoved( QcGraphElement *e );
258 private:
259 struct SelectedElement {
260 SelectedElement( QcGraphElement *e ) : elem(e) {}
261 bool operator == (const SelectedElement & other) {
262 return elem == other.elem;
265 QcGraphElement *elem;
266 QPointF moveOrigin; // in data domain
269 private:
270 void setAllDeselected();
271 void setIndexSelected( int index, bool selected );
272 void restrictValue( QPointF & );
273 void orderRestrictValue( QcGraphElement *, QPointF &, bool selected );
274 void setValue( QcGraphElement *, const QPointF & );
275 void ensureOrder();
276 void moveFree( QcGraphElement *, const QPointF & );
277 void moveOrderRestricted( QcGraphElement *, const QPointF & );
278 void moveSelected( const QPointF & dValue, SelectionForm, bool fromCache );
279 void addCurve( QPainterPath &, QcGraphElement *e1, QcGraphElement *e2 );
280 QSize drawnElementSize( QcGraphElement *e );
281 QRect valueRect();
282 QRectF labelRect( QcGraphElement *, const QPointF &, const QRect &, const QFontMetrics & );
283 void drawDotElement( QcGraphElement *, const QRectF &, const QRect & bounds,
284 const QColor & dotColor, const QColor & circleColor,
285 const QColor & textColor, const QColor & selectColor,
286 const QPalette &, const QFontMetrics &, QPainter * );
287 void drawRectElement( QcGraphElement *, const QRectF &,
288 const QColor & fillColor,
289 const QColor & textColor,
290 const QColor & selectColor,
291 const QPalette &, QPainter * );
292 void paintEvent( QPaintEvent * );
293 void mousePressEvent( QMouseEvent * );
294 void mouseMoveEvent( QMouseEvent * );
295 void keyPressEvent( QKeyEvent * );
296 void doAction( Qt::KeyboardModifiers );
298 QcGraphModel _model;
300 QColor _bkg;
301 QColor _strokeColor;
302 QColor _gridColor;
303 QColor _selectColor;
305 QSize _defaultThumbSize;
306 QPointF _gridMetrics;
307 bool _gridOn;
309 ElementStyle _style;
310 bool _drawLines;
311 bool _drawRects;
313 bool _editable;
314 double _step;
315 SelectionForm _selectionForm;
316 Order _xOrder;
318 bool _geometryDirty;
319 QSize _largestThumbSize;
321 struct Selection {
322 Selection () : cached(false), shallMove(false) {}
323 int size() const { return elems.size(); }
324 int count() const { return elems.count(); }
326 QList<SelectedElement> elems;
327 bool cached;
328 bool shallMove;
329 QPointF moveOrigin; // in data domain
330 } _selection;
332 int _lastIndex;
335 #endif