class library: PriorityQueue - implement removeValue, hide array
[supercollider.git] / QtCollider / widgets / BasicWidgets.cpp
blob4edca9b4245651297698aa338860fb586501991c
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>
27 #include <QKeyEvent>
28 #include <QPainter>
29 #ifdef Q_WS_MAC
30 # include <QMacStyle>
31 #endif
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 )
59 _emitAction = false;
60 clear();
61 Q_FOREACH( QVariant item, items.data )
62 addItem( item.toString() );
63 setCurrentRow( 0 );
64 _emitAction = true;
67 void QcListWidget::setColors( const VariantList & colors ) const
69 int cc = colors.data.count();
70 int ic = 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 )
80 bool b = _emitAction;
81 _emitAction = false;
82 setCurrentRow( row );
83 _emitAction = b;
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 )
110 clear();
111 Q_FOREACH( QVariant item, items.data )
112 addItem( item.toString() );
115 void QcPopUpMenu::doAction( int choice )
117 if( _reactivation || (choice != lastChoice) ) {
118 lastChoice = choice;
119 Q_EMIT( action() );
123 /////////////////////////////// QcButton ///////////////////////////////////////
125 QcWidgetFactory<QcButton> buttonFactory;
127 QcButton::QcButton()
128 : currentState(0), defaultPalette( palette() )
130 connect( this, SIGNAL(clicked()), this, SLOT(doAction()) );
133 #ifdef Q_WS_MAC
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);
143 else
144 return QPushButton::hitButton(pos);
146 #endif
148 void QcButton::setStates( const VariantList & statesArg )
150 if( !statesArg.data.count() ) return;
152 states.clear();
154 Q_FOREACH( QVariant var, statesArg.data ) {
155 VariantList stateArg = var.value<VariantList>();
156 int count = stateArg.data.size();
157 State state;
158 if( count >= 1 )
159 state.text = stateArg.data[0].toString();
160 if( count >= 2 )
161 state.textColor = stateArg.data[1].value<QColor>();
162 if( count >= 3 )
163 state.buttonColor = stateArg.data[2].value<QColor>();
164 states.append( state );
166 setState( 0 );
169 void QcButton::setState( int i )
171 int c = states.count();
172 if( !c ) return;
174 i = qBound( 0, i, c-1 );
176 currentState = i;
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 );
189 setPalette( p );
192 void QcButton::cycleStates()
194 if( states.size() < 2 ) return;
195 int i = currentState + 1;
196 if( i >= states.size() ) i = 0;
197 setState( i );
200 void QcButton::doAction()
202 cycleStates();
203 Q_EMIT( action((int)QApplication::keyboardModifiers()) );
206 class QcCustomPaintedFactory : public QcWidgetFactory<QcCustomPainted>
208 protected:
209 virtual void initialize( QWidgetProxy *p, QcCustomPainted *w )
211 QObject::connect( w, SIGNAL(painting(QPainter*)),
212 p, SLOT(customPaint(QPainter*)) );
216 static QcCustomPaintedFactory customPaintedFactory;