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"
27 #include "../style/style.hpp"
31 struct QcGraphElement
{
33 friend class QcGraphModel
;
55 void setCurveType( CurveType type
, double curve
= 0.0 ) {
60 QcGraphElement
*prev() { return _prev
; }
61 QcGraphElement
*next() { return _next
; }
72 QcGraphElement
*_prev
;
73 QcGraphElement
*_next
;
76 class QcGraphModel
: public QObject
82 QcGraphModel( QObject
* parent
= 0 ) : QObject(parent
) {}
85 Connection( QcGraphElement
* a_
= 0, QcGraphElement
* b_
= 0 ) : a(a_
), b(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
];
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
) );
114 void appended( QcGraphElement
* );
115 void removed( QcGraphElement
* );
119 QList
<Connection
> _conns
;
120 QList
<QcGraphElement
*> _elems
;
123 class QcGraph
: public QWidget
, QcHelper
, QtCollider::Style::Client
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
);
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
);
157 Q_INVOKABLE
void select( int index
, bool exclusive
= true );
158 Q_INVOKABLE
void deselect( int index
);
159 Q_INVOKABLE
void deselectAll();
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 ); }
214 void onElementRemoved( QcGraphElement
*e
);
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
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
& );
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
);
250 QPointF _gridMetrics
;
259 SelectionForm _selectionForm
;
265 Selection () : cached(false), shallMove(false) {}
266 int size() { return elems
.size(); }
267 int count() { return elems
.count(); }
269 QList
<SelectedElement
> elems
;
272 QPointF moveOrigin
; // in data domain