scide: introspection - store strings in boost.flyweight
[supercollider.git] / QtCollider / style / routines.hpp
blob36d4ae76db65fec37c80a15c5664c83e4be961b9
1 #ifndef QC_STYLE_ROUTINES_H
2 #define QC_STYLE_ROUTINES_H
4 #include <QPainter>
5 #include <QPalette>
7 namespace QtCollider {
8 namespace Style {
9 #if 0
10 namespace ReliefType {
11 struct Hover;
12 struct Raised;
13 struct Flat;
14 struct Sunken;
16 using namespace ReliefType;
18 namespace ShapeType {
19 struct Rect;
20 struct RoundRect;
21 struct LeftRoundRect;
22 struct RightRoundRect;
23 struct TopRoundRect;
24 struct BottomRoundRect;
25 struct Ellipse;
27 using namespace ShapeType;
29 // drawShape //
31 template<typename Shape>
32 void drawShape( QPainter *, const QRect & );
34 template<typename Shape>
35 void drawShape( QPainter *, const QRect &, qreal param1 );
37 template<>
38 void drawShape <Rect> ( QPainter *p, const QRect &r )
40 p->drawRect(r);
43 template<>
44 void drawShape <Ellipse> ( QPainter *p, const QRect &r )
46 p->drawEllipse(r);
49 template<>
50 void drawShape <RoundRect> ( QPainter *p, const QRect &r, qreal radius )
52 p->drawRoundedRect( r, radius, radius, Qt::RelativeSize );
55 // drawReliefShape //
57 template<typename ShapeT> struct Shape
59 template<typename RectT> void draw();
61 #endif
63 struct Rect
65 Rect( QRectF rect ) : _rect(rect) {}
66 template<typename RectT> inline void draw( QPainter * p, const RectT &r ) const {
67 p->drawRect(r);
69 QRectF _rect;
72 struct RoundRect
74 RoundRect( QRectF rect, qreal radius ) : _rect(rect), _radius(radius) {}
75 template<typename RectT> inline void draw( QPainter * p, const RectT &r ) const {
76 p->drawRoundedRect(r, _radius, _radius, Qt::AbsoluteSize );
78 QRectF _rect;
79 qreal _radius;
82 struct Ellipse
84 Ellipse( QRectF rect ) : _rect(rect) {}
85 template<typename RectT> inline void draw( QPainter * p, const RectT &r ) const {
86 p->drawEllipse(r);
88 QRectF _rect;
91 template<typename ShapeT> void drawRaised (QPainter *p, const QPalette &plt, const ShapeT &shape,
92 const QColor &color, const QColor &focusColor = QColor())
94 p->save();
96 bool focus = focusColor.isValid();
98 QRectF r( shape._rect );
100 p->setBrush(Qt::NoBrush);
102 // focus indication
104 QPen pen;
105 pen.setWidth(2);
106 if( focus ) {
107 pen.setColor(focusColor);
108 p->setPen(pen);
109 shape.draw( p, r.adjusted(1,1,-1,-1) );
110 r.adjust(2,2,-2,-2);
112 else {
113 r.adjust(1,1,-1,-1);
116 // edge
118 QColor edgeClr( focus ? plt.color(QPalette::Light) : plt.color(QPalette::Dark) );
119 if(focus) edgeClr.setAlpha(100);
120 else edgeClr.setAlpha( color.alpha() );
121 pen.setColor(edgeClr);
122 p->setPen(pen);
123 shape.draw( p, r );
125 // center
127 double pos = shape._rect.height();
128 pos = (pos - 3) / pos;
130 QLinearGradient lgrad( shape._rect.topLeft(), shape._rect.bottomLeft() );
131 lgrad.setColorAt( 0, color.lighter(110) );
132 lgrad.setColorAt( pos, color.darker(105) );
133 lgrad.setColorAt( 1, color.darker(115) );
135 p->setPen(Qt::NoPen);
136 p->setBrush( QBrush(lgrad) );
137 shape.draw( p, r );
139 p->restore();
142 template<typename ShapeT> void drawSunken (QPainter *p, const QPalette &plt, const ShapeT &shape,
143 const QColor &color, const QColor &focusColor = QColor())
145 p->save();
147 QPen pen;
148 pen.setWidth(2);
150 // outer border
152 QColor c = focusColor.isValid() ? focusColor : plt.color(QPalette::Window).lighter(105);
153 pen.setColor(c);
154 p->setPen(pen);
155 p->setBrush(Qt::NoBrush);
156 shape.draw( p, shape._rect.adjusted(1,1,-1,-1) );
158 // inner border
160 QColor c1( plt.color(QPalette::Shadow) );
161 c1.setAlpha( 180 );
162 pen.setColor(c1);
163 p->setPen(pen);
164 p->setBrush(Qt::NoBrush);
165 shape.draw( p, shape._rect.adjusted(2,2,-2,-2) );
167 // shadow
169 QColor c2=color; c2.setAlpha(150);
170 p->setPen(Qt::NoPen);
171 p->setBrush(c2);
172 shape.draw( p, shape._rect.adjusted(2,2,-2,-2) );
174 // background
176 p->setBrush(color);
177 shape.draw( p, shape._rect.adjusted(2,3,-2,-2) );
179 p->restore();
182 template<typename RectT> RectT sunkenContentsRect ( const RectT & r )
184 return r.adjusted(1, 1,-1,-1 );
187 template<typename RectT>
188 qreal xValue ( qreal x, const RectT & bounds )
190 return (x - bounds.x()) / bounds.width();
193 template<typename RectT>
194 qreal yValue ( qreal y, const RectT & bounds )
196 return (bounds.y() + bounds.height() - y) / bounds.height();
199 template<typename RectT, typename SizeT>
200 qreal xValue ( qreal x, const RectT & bounds, const SizeT & margin )
202 return margin.width() < bounds.width() ?
203 (x - bounds.x() - (margin.width() * 0.5)) /
204 (bounds.width() - margin.width())
205 : 0.0;
208 template<typename RectT, typename SizeT>
209 qreal yValue ( qreal y, const RectT & bounds, const SizeT & margin )
211 return margin.height() < bounds.height() ?
212 (bounds.y() + bounds.height() - y - (margin.height() * 0.5)) /
213 (bounds.height() - margin.height())
214 : 0.0;
217 template<typename RectT, typename PointT>
218 QPointF value ( const PointT & pos, const RectT & bounds )
220 return QPointF( xValue(pos.x(), bounds), yValue(pos.y(), bounds) );
223 template<typename RectT, typename SizeT, typename PointT>
224 QPointF value ( const PointT & pos, const RectT & bounds, const SizeT & margin )
226 return QPointF( xValue(pos.x(), bounds, margin), yValue(pos.y(), bounds, margin) );
229 template<typename RectT, typename SizeT>
230 RectT marginsRect ( const RectT & bounds, const SizeT & margin )
232 RectT r;
233 r.setSize( SizeT( qMax((qreal)bounds.width() - margin.width(), qreal(0.0)),
234 qMax((qreal)bounds.height() - margin.height(), qreal(0.0)) ) );
235 r.moveCenter( bounds.center() );
236 return r;
239 template<typename RectT, typename SizeT>
240 RectT rect ( const QPointF & value, const RectT & bounds, const SizeT & size )
242 qreal x = value.x() * (bounds.width() - size.width()) + bounds.left();
243 qreal y = bounds.top() + bounds.height() - size.height() - value.y() * (bounds.height() - size.height());
244 return RectT( x, y, size.width(), size.height() );
247 template<typename RectT, typename SizeT>
248 QPointF pos ( const QPointF & value, const RectT & bounds, const SizeT & margin )
250 qreal x = value.x() * (bounds.width() - margin.width()) + bounds.left() + margin.x() * 0.5;
251 qreal y = - value.y() * (bounds.height() - margin.height()) + bounds.top() + bounds.height() - margin.y() * 0.5;
252 return QPointF(x,y);
255 template<typename RectT>
256 QPointF pos ( const QPointF & value, const RectT & bounds )
258 qreal x = value.x() * bounds.width() + bounds.left();
259 qreal y = - value.y() * bounds.height() + bounds.top() + bounds.height();
260 return QPointF(x,y);
263 } // namespace QtCollider
264 } // namespace Style
266 #endif