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 ************************************************************************/
22 #include "BasicWidgets.h"
23 #include "../QcWidgetFactory.h"
24 #include "../Common.h"
26 #include <QApplication>
33 QcWidgetFactory
<QcDefaultWidget
> defaultWidgetFactory
;
34 QcWidgetFactory
<QcHLayoutWidget
> hLayoutWidgetFactory
;
35 QcWidgetFactory
<QcVLayoutWidget
> vLayoutWidgetFactory
;
36 QcWidgetFactory
<QLabel
> labelFactory
;
37 QcWidgetFactory
<QcTextField
> textFieldFactory
;
38 QcWidgetFactory
<QcCheckBox
> checkBoxFactory
;
40 //////////////////////////// QcListWidget //////////////////////////////////////
42 class QcListWidgetFactory
: public QcWidgetFactory
<QcListWidget
>
44 void initialize( QWidgetProxy
*p
, QcListWidget
*l
) {
45 p
->setMouseEventWidget( l
->viewport() );
49 QcListWidgetFactory listWidgetFactory
;
51 QcListWidget::QcListWidget() : _emitAction(true)
53 connect( this, SIGNAL( currentItemChanged( QListWidgetItem
*, QListWidgetItem
* ) ),
54 this, SLOT( onCurrentItemChanged() ) );
57 void QcListWidget::setItems( const VariantList
& items
)
61 Q_FOREACH( QVariant item
, items
.data
)
62 addItem( item
.toString() );
67 void QcListWidget::setColors( const VariantList
& colors
) const
69 int cc
= colors
.data
.count();
71 for( int i
=0; i
<cc
&& i
< ic
; ++i
) {
72 QListWidgetItem
*it
= item(i
);
73 QColor
color( colors
.data
[i
].value
<QColor
>() );
74 if( color
.isValid() ) it
->setBackground( color
);
78 void QcListWidget::setCurrentRowWithoutAction( int row
)
86 void QcListWidget::onCurrentItemChanged()
88 if( _emitAction
) Q_EMIT( action() );
91 void QcListWidget::keyPressEvent( QKeyEvent
*e
)
93 QListWidget::keyPressEvent( e
);
94 if( e
->key() == Qt::Key_Return
)
95 Q_EMIT( returnPressed() );
98 ////////////////////////// QcPopUpMenu /////////////////////////////////////////
100 QcWidgetFactory
<QcPopUpMenu
> popUpMenuFactory
;
102 QcPopUpMenu::QcPopUpMenu()
103 : lastChoice( -1 ), _reactivation(false)
105 connect( this, SIGNAL(activated(int)), this, SLOT(doAction(int)) );
108 void QcPopUpMenu::setItems( const VariantList
& items
)
111 Q_FOREACH( QVariant item
, items
.data
)
112 addItem( item
.toString() );
115 void QcPopUpMenu::doAction( int choice
)
117 if( _reactivation
|| (choice
!= lastChoice
) ) {
123 /////////////////////////////// QcButton ///////////////////////////////////////
125 QcWidgetFactory
<QcButton
> buttonFactory
;
128 : currentState(0), defaultPalette( palette() )
130 connect( this, SIGNAL(clicked()), this, SLOT(doAction()) );
134 bool QcButton::hitButton( const QPoint
& pos
) const
136 // FIXME: This is a workaround for Qt Bug 15936:
137 // incorrect QPushButton hit area.
139 QMacStyle
*macStyle
= qobject_cast
<QMacStyle
*>(style());
141 if( !macStyle
|| isFlat() )
142 return QAbstractButton::hitButton(pos
);
144 return QPushButton::hitButton(pos
);
148 void QcButton::setStates( const VariantList
& statesArg
)
150 if( !statesArg
.data
.count() ) return;
154 Q_FOREACH( QVariant var
, statesArg
.data
) {
155 VariantList stateArg
= var
.value
<VariantList
>();
156 int count
= stateArg
.data
.size();
159 state
.text
= stateArg
.data
[0].toString();
161 state
.textColor
= stateArg
.data
[1].value
<QColor
>();
163 state
.buttonColor
= stateArg
.data
[2].value
<QColor
>();
164 states
.append( state
);
169 void QcButton::setState( int i
)
171 int c
= states
.count();
174 i
= qBound( 0, i
, c
-1 );
178 State state
= states
[i
];
180 setText( state
.text
);
182 QPalette
p ( defaultPalette
);
184 if( state
.textColor
.isValid() )
185 p
.setColor( QPalette::ButtonText
, state
.textColor
);
186 if( state
.buttonColor
.isValid() )
187 p
.setColor( QPalette::Button
, state
.buttonColor
);
192 void QcButton::cycleStates()
194 if( states
.size() < 2 ) return;
195 int i
= currentState
+ 1;
196 if( i
>= states
.size() ) i
= 0;
200 void QcButton::doAction()
203 Q_EMIT( action((int)QApplication::keyboardModifiers()) );
206 class QcCustomPaintedFactory
: public QcWidgetFactory
<QcCustomPainted
>
209 virtual void initialize( QWidgetProxy
*p
, QcCustomPainted
*w
)
211 QObject::connect( w
, SIGNAL(painting(QPainter
*)),
212 p
, SLOT(customPaint(QPainter
*)) );
216 static QcCustomPaintedFactory customPaintedFactory
;