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 ************************************************************************/
25 #include "../Common.h"
26 #include "../QcHelper.h"
30 struct QcGraphElement
{
32 friend class QcGraphModel
;
44 fillColor( QColor(0,0,0) ),
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
;
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
; }
82 QcGraphElement
*_prev
;
83 QcGraphElement
*_next
;
86 class QcGraphModel
: public QObject
92 QcGraphModel( QObject
* parent
= 0 ) : QObject(parent
) {}
95 Connection( QcGraphElement
* a_
= 0, QcGraphElement
* b_
= 0 ) : a(a_
), b(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
];
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
) );
124 void appended( QcGraphElement
* );
125 void removed( QcGraphElement
* );
129 QList
<Connection
> _conns
;
130 QList
<QcGraphElement
*> _elems
;
133 class QcGraph
: public QWidget
, QcHelper
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
);
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 );
165 Q_INVOKABLE
void select( int index
, bool exclusive
= true );
166 Q_INVOKABLE
void deselect( int index
);
167 Q_INVOKABLE
void deselectAll();
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 ); }
223 void onElementRemoved( QcGraphElement
*e
);
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
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
& );
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
);
261 QPointF _gridMetrics
;
269 SelectionForm _selectionForm
;
275 Selection () : cached(false), shallMove(false) {}
276 int size() { return elems
.size(); }
277 int count() { return elems
.count(); }
279 QList
<SelectedElement
> elems
;
282 QPointF moveOrigin
; // in data domain