/
BasicWidgets.cpp
218 lines (179 loc) · 5.48 KB
/
BasicWidgets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/************************************************************************
*
* Copyright 2010 Jakob Leben (jakob.leben@gmail.com)
*
* This file is part of SuperCollider Qt GUI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
************************************************************************/
#include "BasicWidgets.h"
#include "../QcWidgetFactory.h"
#include "../Common.h"
#include <QApplication>
#include <QKeyEvent>
#include <QPainter>
#ifdef Q_WS_MAC
# include <QMacStyle>
#endif
QC_DECLARE_QWIDGET_FACTORY( QcDefaultWidget );
QC_DECLARE_QWIDGET_FACTORY( QcHLayoutWidget );
QC_DECLARE_QWIDGET_FACTORY( QcVLayoutWidget );
QC_DECLARE_QWIDGET_FACTORY( QLabel );
QC_DECLARE_QWIDGET_FACTORY( QcTextField );
QC_DECLARE_QWIDGET_FACTORY( QcCheckBox );
//////////////////////////// QcListWidget //////////////////////////////////////
class QcListWidgetFactory : public QcWidgetFactory<QcListWidget>
{
void initialize( QWidgetProxy *p, QcListWidget *l ) {
p->setMouseEventWidget( l->viewport() );
}
};
QC_DECLARE_FACTORY( QcListWidget, QcListWidgetFactory );
QcListWidget::QcListWidget() : _emitAction(true)
{
connect( this, SIGNAL( currentItemChanged( QListWidgetItem*, QListWidgetItem* ) ),
this, SLOT( onCurrentItemChanged() ) );
}
void QcListWidget::setItems( const VariantList & items )
{
_emitAction = false;
clear();
Q_FOREACH( QVariant item, items.data )
addItem( item.toString() );
setCurrentRow( 0 );
_emitAction = true;
}
void QcListWidget::setColors( const VariantList & colors ) const
{
int cc = colors.data.count();
int ic = count();
for( int i=0; i<cc && i < ic; ++i ) {
QListWidgetItem *it = item(i);
QColor color( colors.data[i].value<QColor>() );
if( color.isValid() ) it->setBackground( color );
}
}
void QcListWidget::setCurrentRowWithoutAction( int row )
{
bool b = _emitAction;
_emitAction = false;
setCurrentRow( row );
_emitAction = b;
}
void QcListWidget::onCurrentItemChanged()
{
if( _emitAction ) Q_EMIT( action() );
}
void QcListWidget::keyPressEvent( QKeyEvent *e )
{
QListWidget::keyPressEvent( e );
if( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
Q_EMIT( returnPressed() );
}
////////////////////////// QcPopUpMenu /////////////////////////////////////////
QC_DECLARE_QWIDGET_FACTORY(QcPopUpMenu);
QcPopUpMenu::QcPopUpMenu()
: lastChoice( -1 ), _reactivation(false)
{
connect( this, SIGNAL(activated(int)), this, SLOT(doAction(int)) );
}
void QcPopUpMenu::setItems( const VariantList & items )
{
clear();
Q_FOREACH( QVariant item, items.data )
addItem( item.toString() );
}
void QcPopUpMenu::doAction( int choice )
{
if( _reactivation || (choice != lastChoice) ) {
lastChoice = choice;
Q_EMIT( action() );
}
}
/////////////////////////////// QcButton ///////////////////////////////////////
QC_DECLARE_QWIDGET_FACTORY(QcButton);
QcButton::QcButton()
: currentState(0), defaultPalette( palette() )
{
connect( this, SIGNAL(clicked()), this, SLOT(doAction()) );
}
#ifdef Q_WS_MAC
bool QcButton::hitButton( const QPoint & pos ) const
{
// FIXME: This is a workaround for Qt Bug 15936:
// incorrect QPushButton hit area.
QMacStyle *macStyle = qobject_cast<QMacStyle *>(style());
if( !macStyle || isFlat() )
return QAbstractButton::hitButton(pos);
else
return QPushButton::hitButton(pos);
}
#endif
void QcButton::setStates( const VariantList & statesArg )
{
if( !statesArg.data.count() ) return;
states.clear();
Q_FOREACH( QVariant var, statesArg.data ) {
VariantList stateArg = var.value<VariantList>();
int count = stateArg.data.size();
State state;
if( count >= 1 )
state.text = stateArg.data[0].toString();
if( count >= 2 )
state.textColor = stateArg.data[1].value<QColor>();
if( count >= 3 )
state.buttonColor = stateArg.data[2].value<QColor>();
states.append( state );
}
setState( 0 );
}
void QcButton::setState( int i )
{
int c = states.count();
if( !c ) return;
i = qBound( 0, i, c-1 );
currentState = i;
State state = states[i];
setText( state.text );
QPalette p ( defaultPalette );
if( state.textColor.isValid() )
p.setColor( QPalette::ButtonText, state.textColor );
if( state.buttonColor.isValid() )
p.setColor( QPalette::Button, state.buttonColor );
setPalette( p );
}
void QcButton::cycleStates()
{
if( states.size() < 2 ) return;
int i = currentState + 1;
if( i >= states.size() ) i = 0;
setState( i );
}
void QcButton::doAction()
{
cycleStates();
Q_EMIT( action((int)QApplication::keyboardModifiers()) );
}
/////////////////////////////////////////////////////////////////////////
class QcCustomPaintedFactory : public QcWidgetFactory<QcCustomPainted>
{
protected:
virtual void initialize( QWidgetProxy *p, QcCustomPainted *w )
{
QObject::connect( w, SIGNAL(painting(QPainter*)),
p, SLOT(customPaint(QPainter*)) );
}
};
QC_DECLARE_FACTORY( QcCustomPainted, QcCustomPaintedFactory );