1
/**********************************************************************
2
** Copyright (C) 2000 Trolltech AS. All rights reserved.
4
** This file is part of Qt Designer.
6
** This file may be distributed and/or modified under the terms of the
7
** GNU General Public License version 2 as published by the Free Software
8
** Foundation and appearing in the file LICENSE.GPL included in the
9
** packaging of this file.
11
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12
** licenses may use this file in accordance with the Qt Commercial License
13
** Agreement provided with the Software.
15
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18
** See http://www.trolltech.com/gpl/ for GPL licensing information.
19
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20
** information about Qt Commercial License Agreements.
22
** Contact info@trolltech.com if any conditions of this licensing are
25
**********************************************************************/
27
#include <qvariant.h> // HP-UX compiler needs this here
28
#include "styledbutton.h"
29
#include "formwindow.h"
30
#include "pixmapchooser.h"
31
#include <qcolordialog.h>
37
#include <qapplication.h>
38
#include <qdragobject.h>
41
StyledButton::StyledButton(QWidget* parent, const char* name)
42
: QButton( parent, name ), pix( 0 ), spix( 0 ), s( 0 ), formWindow( 0 ), mousePressed( FALSE )
44
setMinimumSize( minimumSizeHint() );
45
setAcceptDrops( TRUE );
47
connect( this, SIGNAL(clicked()), SLOT(onEditor()));
49
setEditor( ColorEditor );
52
StyledButton::StyledButton( const QBrush& b, QWidget* parent, const char* name, WFlags f )
53
: QButton( parent, name, f ), spix( 0 ), s( 0 ), formWindow( 0 )
57
setMinimumSize( minimumSizeHint() );
60
StyledButton::~StyledButton()
64
void StyledButton::setEditor( EditorType e )
73
StyledButton::EditorType StyledButton::editor() const
78
void StyledButton::setColor( const QColor& c )
84
void StyledButton::setPixmap( const QPixmap & pm )
88
pix = new QPixmap( pm );
96
QColor StyledButton::color() const
101
QPixmap* StyledButton::pixmap() const
106
bool StyledButton::scale() const
111
void StyledButton::setScale( bool on )
120
QSize StyledButton::sizeHint() const
122
return QSize( 50, 25 );
125
QSize StyledButton::minimumSizeHint() const
127
return QSize( 50, 25 );
130
void StyledButton::scalePixmap()
135
spix = new QPixmap( 6*width()/8, 6*height()/8 );
136
QImage img = pix->convertToImage();
138
spix->convertFromImage( s? img.smoothScale( 6*width()/8, 6*height()/8 ) : img );
146
void StyledButton::resizeEvent( QResizeEvent* e )
149
QButton::resizeEvent( e );
152
void StyledButton::drawButton( QPainter *paint )
154
style().drawPrimitive(QStyle::PE_ButtonBevel, paint, rect(), colorGroup(),
155
isDown() ? QStyle::Style_Sunken : QStyle::Style_Raised);
156
drawButtonLabel(paint);
159
style().drawPrimitive(QStyle::PE_FocusRect, paint,
160
style().subRect(QStyle::SR_PushButtonFocusRect, this),
161
colorGroup(), QStyle::Style_Default);
164
void StyledButton::drawButtonLabel( QPainter *paint )
166
QColor pen = isEnabled() ?
167
hasFocus() ? palette().active().buttonText() : palette().inactive().buttonText()
168
: palette().disabled().buttonText();
169
paint->setPen( pen );
172
paint->setBrush( QBrush( colorGroup().button() ) );
174
else if ( edit == PixmapEditor && spix ) {
175
paint->setBrush( QBrush( col, *spix ) );
176
paint->setBrushOrigin( width()/8, height()/8 );
178
paint->setBrush( QBrush( col ) );
180
paint->drawRect( width()/8, height()/8, 6*width()/8, 6*height()/8 );
183
void StyledButton::onEditor()
187
QColor c = QColorDialog::getColor( palette().active().background(), this );
196
p = qChoosePixmap( this, formWindow, *pixmap() );
198
p = qChoosePixmap( this, formWindow, QPixmap() );
209
void StyledButton::mousePressEvent(QMouseEvent* e)
211
QButton::mousePressEvent(e);
216
void StyledButton::mouseMoveEvent(QMouseEvent* e)
218
QButton::mouseMoveEvent( e );
219
#ifndef QT_NO_DRAGANDDROP
222
if ( ( pressPos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
223
if ( edit == ColorEditor ) {
224
QColorDrag *drg = new QColorDrag( col, this );
225
QPixmap pix( 25, 25 );
228
p.drawRect( 0, 0, pix.width(), pix.height() );
230
drg->setPixmap( pix );
231
mousePressed = FALSE;
234
else if ( edit == PixmapEditor && pix && !pix->isNull() ) {
235
QImage img = pix->convertToImage();
236
QImageDrag *drg = new QImageDrag( img, this );
238
drg->setPixmap( *spix );
239
mousePressed = FALSE;
246
#ifndef QT_NO_DRAGANDDROP
247
void StyledButton::dragEnterEvent( QDragEnterEvent *e )
250
if ( edit == ColorEditor && QColorDrag::canDecode( e ) )
252
else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) )
258
void StyledButton::dragLeaveEvent( QDragLeaveEvent * )
261
parentWidget()->setFocus();
264
void StyledButton::dragMoveEvent( QDragMoveEvent *e )
266
if ( edit == ColorEditor && QColorDrag::canDecode( e ) )
268
else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) )
274
void StyledButton::dropEvent( QDropEvent *e )
276
if ( edit == ColorEditor && QColorDrag::canDecode( e ) ) {
278
QColorDrag::decode( e, color );
283
else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) ) {
285
QImageDrag::decode( e, img );
287
pm.convertFromImage(img);
295
#endif // QT_NO_DRAGANDDROP