"Post Window" -> "Post window" prevents it being seen as two separate
[supercollider.git] / QtCollider / widgets / QcButton.cpp
blobfe1f6e8e529ed38a7aad2369cc2451b26a6be21b
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 ************************************************************************/
22 #include "QcButton.h"
23 #include "../QcWidgetFactory.h"
25 #ifdef Q_WS_MAC
26 # include <QMacStyle>
27 #endif
29 QC_DECLARE_QWIDGET_FACTORY(QcButton);
31 QcButton::QcButton():
32 QtCollider::Style::Client(this),
33 currentState(0)
35 connect( this, SIGNAL(clicked()), this, SLOT(doAction()) );
38 #ifdef Q_WS_MAC
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);
48 else
49 return QPushButton::hitButton(pos);
51 #endif
53 void QcButton::setStates( const VariantList & statesArg )
55 if( !statesArg.data.count() ) return;
57 states.clear();
59 Q_FOREACH( QVariant var, statesArg.data ) {
60 VariantList stateArg = var.value<VariantList>();
61 int count = stateArg.data.size();
62 State state;
63 if( count >= 1 )
64 state.text = stateArg.data[0].toString();
65 if( count >= 2 )
66 state.textColor = stateArg.data[1].value<QColor>();
67 if( count >= 3 )
68 state.buttonColor = stateArg.data[2].value<QColor>();
69 states.append( state );
71 setState( 0 );
74 void QcButton::setState( int i )
76 int c = states.count();
77 if( !c ) return;
79 currentState = qBound( 0, i, c-1 );
80 setText( states[currentState].text );
81 update();
84 void QcButton::cycleStates()
86 if( states.size() < 2 ) return;
87 int i = currentState + 1;
88 if( i >= states.size() ) i = 0;
89 setState( i );
92 void QcButton::doAction()
94 cycleStates();
95 Q_EMIT( action((int)QApplication::keyboardModifiers()) );
98 void QcButton::paintEvent ( QPaintEvent * )
100 using namespace QtCollider::Style;
101 using QtCollider::Style::RoundRect;
103 QPainter p(this);
104 p.setRenderHint( QPainter::Antialiasing, true );
106 State state;
107 if(states.count()) state = states[currentState];
109 const QPalette &plt = palette();
110 bool sunken = isDown();
111 int radius = 2;
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() );
118 QRect r(rect());
120 RoundRect frame( r, radius );
121 if( sunken )
122 drawSunken( &p, plt, frame, state.buttonColor, focusClr);
123 else
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() );