1 /************************************************************************
3 * Copyright 2010-2012 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 ************************************************************************/
23 #include "../QcWidgetFactory.h"
29 QC_DECLARE_QWIDGET_FACTORY(QcButton
);
32 QtCollider::Style::Client(this),
35 connect( this, SIGNAL(clicked()), this, SLOT(doAction()) );
39 bool QcButton::hitButton( const QPoint
& pos
) const
41 // FIXME: This is a workaround for Qt Bug 15936:
42 // incorrect QPushButton hit area.
44 QMacStyle
*macStyle
= qobject_cast
<QMacStyle
*>(style());
46 if( !macStyle
|| isFlat() )
47 return QAbstractButton::hitButton(pos
);
49 return QPushButton::hitButton(pos
);
53 void QcButton::setStates( const VariantList
& statesArg
)
55 if( !statesArg
.data
.count() ) return;
59 Q_FOREACH( QVariant var
, statesArg
.data
) {
60 VariantList stateArg
= var
.value
<VariantList
>();
61 int count
= stateArg
.data
.size();
64 state
.text
= stateArg
.data
[0].toString();
66 state
.textColor
= stateArg
.data
[1].value
<QColor
>();
68 state
.buttonColor
= stateArg
.data
[2].value
<QColor
>();
69 states
.append( state
);
74 void QcButton::setState( int i
)
76 int c
= states
.count();
79 currentState
= qBound( 0, i
, c
-1 );
80 setText( states
[currentState
].text
);
84 void QcButton::cycleStates()
86 if( states
.size() < 2 ) return;
87 int i
= currentState
+ 1;
88 if( i
>= states
.size() ) i
= 0;
92 void QcButton::doAction()
95 Q_EMIT( action((int)QApplication::keyboardModifiers()) );
98 void QcButton::paintEvent ( QPaintEvent
* )
100 using namespace QtCollider::Style
;
101 using QtCollider::Style::RoundRect
;
104 p
.setRenderHint( QPainter::Antialiasing
, true );
107 if(states
.count()) state
= states
[currentState
];
109 const QPalette
&plt
= palette();
110 bool sunken
= isDown();
113 if(!state
.buttonColor
.isValid()) state
.buttonColor
= plt
.color(QPalette::Button
);
114 if(sunken
) state
.buttonColor
= state
.buttonColor
.darker(120);
116 QColor
focusClr( hasFocus() ? focusColor() : QColor() );
120 RoundRect
frame( r
, radius
);
122 drawSunken( &p
, plt
, frame
, state
.buttonColor
, focusClr
);
124 drawRaised( &p
, plt
, frame
, state
.buttonColor
, focusClr
);
126 p
.setPen( state
.textColor
.isValid() ? state
.textColor
: plt
.color(QPalette::ButtonText
) );
127 if(sunken
) r
.translate(1,1);
128 p
.drawText( r
, Qt::AlignCenter
, text() );