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
;
46 QcGraphElement( const QSize
& sz
) :
56 void setCurveType( CurveType type
, double curve
= 0.0 ) {
61 QcGraphElement
*prev() { return _prev
; }
62 QcGraphElement
*next() { return _next
; }
74 QcGraphElement
*_prev
;
75 QcGraphElement
*_next
;
78 class QcGraphModel
: public QObject
84 QcGraphModel( QObject
* parent
= 0 ) : QObject(parent
) {}
87 Connection( QcGraphElement
* a_
= 0, QcGraphElement
* b_
= 0 ) : a(a_
), b(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
];
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
) );
116 void appended( QcGraphElement
* );
117 void removed( QcGraphElement
* );
121 QList
<Connection
> _conns
;
122 QList
<QcGraphElement
*> _elems
;
125 class QcGraph
: public QWidget
, QcHelper
, QtCollider::Style::Client
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
);
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
);
167 Q_INVOKABLE
void select( int index
, bool exclusive
= true );
168 Q_INVOKABLE
void deselect( int index
);
169 Q_INVOKABLE
void deselectAll();
194 VariantList
value() const;
195 QcGraphElement
*currentElement() 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())
231 QColor c
= palette().color(QPalette::Text
);
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 ); }
256 void onElementRemoved( QcGraphElement
*e
);
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
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
& );
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
);
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
);
305 QSize _defaultThumbSize
;
306 QPointF _gridMetrics
;
315 SelectionForm _selectionForm
;
319 QSize _largestThumbSize
;
322 Selection () : cached(false), shallMove(false) {}
323 int size() const { return elems
.size(); }
324 int count() const { return elems
.count(); }
326 QList
<SelectedElement
> elems
;
329 QPointF moveOrigin
; // in data domain