You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
6.8 KiB
296 lines
6.8 KiB
15 years ago
|
/**********************************************************************
|
||
|
** Copyright (C) 2000 Trolltech AS. All rights reserved.
|
||
|
**
|
||
14 years ago
|
** This file is part of TQt Designer.
|
||
15 years ago
|
**
|
||
|
** This file may be distributed and/or modified under the terms of the
|
||
|
** GNU General Public License version 2 as published by the Free Software
|
||
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
||
|
** packaging of this file.
|
||
|
**
|
||
14 years ago
|
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
|
||
|
** licenses may use this file in accordance with the TQt Commercial License
|
||
15 years ago
|
** Agreement provided with the Software.
|
||
|
**
|
||
|
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||
|
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||
|
**
|
||
|
** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
||
|
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
||
14 years ago
|
** information about TQt Commercial License Agreements.
|
||
15 years ago
|
**
|
||
|
** Contact info@trolltech.com if any conditions of this licensing are
|
||
|
** not clear to you.
|
||
|
**
|
||
|
**********************************************************************/
|
||
|
|
||
15 years ago
|
#include <tqvariant.h> // HP-UX compiler needs this here
|
||
15 years ago
|
#include "styledbutton.h"
|
||
|
#include "formwindow.h"
|
||
|
#include "pixmapchooser.h"
|
||
15 years ago
|
#include <tqcolordialog.h>
|
||
13 years ago
|
#include <tqpalette.h>
|
||
15 years ago
|
#include <tqlabel.h>
|
||
|
#include <tqpainter.h>
|
||
|
#include <tqimage.h>
|
||
|
#include <tqpixmap.h>
|
||
|
#include <tqapplication.h>
|
||
|
#include <tqdragobject.h>
|
||
|
#include <tqstyle.h>
|
||
|
|
||
14 years ago
|
StyledButton::StyledButton(TQWidget* parent, const char* name)
|
||
|
: TQButton( parent, name ), pix( 0 ), spix( 0 ), s( 0 ), formWindow( 0 ), mousePressed( FALSE )
|
||
15 years ago
|
{
|
||
13 years ago
|
setMinimumSize( minimumSizeHint() );
|
||
15 years ago
|
setAcceptDrops( TRUE );
|
||
|
|
||
15 years ago
|
connect( this, TQT_SIGNAL(clicked()), TQT_SLOT(onEditor()));
|
||
15 years ago
|
|
||
|
setEditor( ColorEditor );
|
||
|
}
|
||
|
|
||
14 years ago
|
StyledButton::StyledButton( const TQBrush& b, TQWidget* parent, const char* name, WFlags f )
|
||
|
: TQButton( parent, name, f ), spix( 0 ), s( 0 ), formWindow( 0 )
|
||
15 years ago
|
{
|
||
|
col = b.color();
|
||
|
pix = b.pixmap();
|
||
13 years ago
|
setMinimumSize( minimumSizeHint() );
|
||
15 years ago
|
}
|
||
|
|
||
|
StyledButton::~StyledButton()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void StyledButton::setEditor( EditorType e )
|
||
|
{
|
||
|
if ( edit == e )
|
||
|
return;
|
||
|
|
||
|
edit = e;
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
StyledButton::EditorType StyledButton::editor() const
|
||
|
{
|
||
|
return edit;
|
||
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::setColor( const TQColor& c )
|
||
15 years ago
|
{
|
||
|
col = c;
|
||
|
update();
|
||
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::setPixmap( const TQPixmap & pm )
|
||
15 years ago
|
{
|
||
|
if ( !pm.isNull() ) {
|
||
|
delete pix;
|
||
15 years ago
|
pix = new TQPixmap( pm );
|
||
15 years ago
|
} else {
|
||
|
delete pix;
|
||
|
pix = 0;
|
||
|
}
|
||
|
scalePixmap();
|
||
|
}
|
||
|
|
||
15 years ago
|
TQColor StyledButton::color() const
|
||
15 years ago
|
{
|
||
|
return col;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQPixmap* StyledButton::pixmap() const
|
||
15 years ago
|
{
|
||
|
return pix;
|
||
|
}
|
||
|
|
||
|
bool StyledButton::scale() const
|
||
|
{
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
void StyledButton::setScale( bool on )
|
||
|
{
|
||
|
if ( s == on )
|
||
|
return;
|
||
|
|
||
|
s = on;
|
||
|
scalePixmap();
|
||
|
}
|
||
|
|
||
13 years ago
|
TQSize StyledButton::sizeHint() const
|
||
15 years ago
|
{
|
||
15 years ago
|
return TQSize( 50, 25 );
|
||
15 years ago
|
}
|
||
|
|
||
13 years ago
|
TQSize StyledButton::minimumSizeHint() const
|
||
15 years ago
|
{
|
||
15 years ago
|
return TQSize( 50, 25 );
|
||
15 years ago
|
}
|
||
|
|
||
|
void StyledButton::scalePixmap()
|
||
|
{
|
||
|
delete spix;
|
||
|
|
||
|
if ( pix ) {
|
||
15 years ago
|
spix = new TQPixmap( 6*width()/8, 6*height()/8 );
|
||
|
TQImage img = pix->convertToImage();
|
||
15 years ago
|
|
||
|
spix->convertFromImage( s? img.smoothScale( 6*width()/8, 6*height()/8 ) : img );
|
||
|
} else {
|
||
|
spix = 0;
|
||
|
}
|
||
|
|
||
|
update();
|
||
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::resizeEvent( TQResizeEvent* e )
|
||
15 years ago
|
{
|
||
|
scalePixmap();
|
||
15 years ago
|
TQButton::resizeEvent( e );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::drawButton( TQPainter *paint )
|
||
15 years ago
|
{
|
||
13 years ago
|
style().tqdrawPrimitive(TQStyle::PE_ButtonBevel, paint, rect(), colorGroup(),
|
||
15 years ago
|
isDown() ? TQStyle::Style_Sunken : TQStyle::Style_Raised);
|
||
15 years ago
|
drawButtonLabel(paint);
|
||
|
|
||
|
if (hasFocus())
|
||
13 years ago
|
style().tqdrawPrimitive(TQStyle::PE_FocusRect, paint,
|
||
|
style().subRect(TQStyle::SR_PushButtonFocusRect, this),
|
||
13 years ago
|
colorGroup(), TQStyle::Style_Default);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::drawButtonLabel( TQPainter *paint )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQColor pen = isEnabled() ?
|
||
13 years ago
|
hasFocus() ? palette().active().buttonText() : palette().inactive().buttonText()
|
||
|
: palette().disabled().buttonText();
|
||
15 years ago
|
paint->setPen( pen );
|
||
|
|
||
|
if(!isEnabled()) {
|
||
13 years ago
|
paint->setBrush( TQBrush( colorGroup().button() ) );
|
||
15 years ago
|
}
|
||
|
else if ( edit == PixmapEditor && spix ) {
|
||
15 years ago
|
paint->setBrush( TQBrush( col, *spix ) );
|
||
15 years ago
|
paint->setBrushOrigin( width()/8, height()/8 );
|
||
|
} else
|
||
15 years ago
|
paint->setBrush( TQBrush( col ) );
|
||
15 years ago
|
|
||
|
paint->drawRect( width()/8, height()/8, 6*width()/8, 6*height()/8 );
|
||
|
}
|
||
|
|
||
|
void StyledButton::onEditor()
|
||
|
{
|
||
|
switch (edit) {
|
||
|
case ColorEditor: {
|
||
13 years ago
|
TQColor c = TQColorDialog::getColor( palette().active().background(), this );
|
||
15 years ago
|
if ( c.isValid() ) {
|
||
|
setColor( c );
|
||
|
emit changed();
|
||
|
}
|
||
|
} break;
|
||
|
case PixmapEditor: {
|
||
15 years ago
|
TQPixmap p;
|
||
15 years ago
|
if ( pixmap() )
|
||
|
p = qChoosePixmap( this, formWindow, *pixmap() );
|
||
|
else
|
||
15 years ago
|
p = qChoosePixmap( this, formWindow, TQPixmap() );
|
||
15 years ago
|
if ( !p.isNull() ) {
|
||
|
setPixmap( p );
|
||
|
emit changed();
|
||
|
}
|
||
|
} break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::mousePressEvent(TQMouseEvent* e)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQButton::mousePressEvent(e);
|
||
15 years ago
|
mousePressed = TRUE;
|
||
|
pressPos = e->pos();
|
||
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::mouseMoveEvent(TQMouseEvent* e)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQButton::mouseMoveEvent( e );
|
||
14 years ago
|
#ifndef TQT_NO_DRAGANDDROP
|
||
15 years ago
|
if ( !mousePressed )
|
||
|
return;
|
||
15 years ago
|
if ( ( pressPos - e->pos() ).manhattanLength() > TQApplication::startDragDistance() ) {
|
||
15 years ago
|
if ( edit == ColorEditor ) {
|
||
15 years ago
|
TQColorDrag *drg = new TQColorDrag( col, this );
|
||
|
TQPixmap pix( 25, 25 );
|
||
15 years ago
|
pix.fill( col );
|
||
15 years ago
|
TQPainter p( &pix );
|
||
15 years ago
|
p.drawRect( 0, 0, pix.width(), pix.height() );
|
||
|
p.end();
|
||
|
drg->setPixmap( pix );
|
||
|
mousePressed = FALSE;
|
||
|
drg->dragCopy();
|
||
|
}
|
||
|
else if ( edit == PixmapEditor && pix && !pix->isNull() ) {
|
||
15 years ago
|
TQImage img = pix->convertToImage();
|
||
|
TQImageDrag *drg = new TQImageDrag( img, this );
|
||
15 years ago
|
if(spix)
|
||
|
drg->setPixmap( *spix );
|
||
|
mousePressed = FALSE;
|
||
|
drg->dragCopy();
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
|
||
14 years ago
|
#ifndef TQT_NO_DRAGANDDROP
|
||
15 years ago
|
void StyledButton::dragEnterEvent( TQDragEnterEvent *e )
|
||
15 years ago
|
{
|
||
|
setFocus();
|
||
15 years ago
|
if ( edit == ColorEditor && TQColorDrag::canDecode( e ) )
|
||
15 years ago
|
e->accept();
|
||
15 years ago
|
else if ( edit == PixmapEditor && TQImageDrag::canDecode( e ) )
|
||
15 years ago
|
e->accept();
|
||
|
else
|
||
|
e->ignore();
|
||
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::dragLeaveEvent( TQDragLeaveEvent * )
|
||
15 years ago
|
{
|
||
|
if ( hasFocus() )
|
||
14 years ago
|
parentWidget()->setFocus();
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::dragMoveEvent( TQDragMoveEvent *e )
|
||
15 years ago
|
{
|
||
15 years ago
|
if ( edit == ColorEditor && TQColorDrag::canDecode( e ) )
|
||
15 years ago
|
e->accept();
|
||
15 years ago
|
else if ( edit == PixmapEditor && TQImageDrag::canDecode( e ) )
|
||
15 years ago
|
e->accept();
|
||
|
else
|
||
|
e->ignore();
|
||
|
}
|
||
|
|
||
15 years ago
|
void StyledButton::dropEvent( TQDropEvent *e )
|
||
15 years ago
|
{
|
||
15 years ago
|
if ( edit == ColorEditor && TQColorDrag::canDecode( e ) ) {
|
||
|
TQColor color;
|
||
|
TQColorDrag::decode( e, color );
|
||
15 years ago
|
setColor(color);
|
||
|
emit changed();
|
||
|
e->accept();
|
||
|
}
|
||
15 years ago
|
else if ( edit == PixmapEditor && TQImageDrag::canDecode( e ) ) {
|
||
|
TQImage img;
|
||
|
TQImageDrag::decode( e, img );
|
||
|
TQPixmap pm;
|
||
15 years ago
|
pm.convertFromImage(img);
|
||
|
setPixmap(pm);
|
||
|
emit changed();
|
||
|
e->accept();
|
||
|
} else {
|
||
|
e->ignore();
|
||
|
}
|
||
|
}
|
||
14 years ago
|
#endif // TQT_NO_DRAGANDDROP
|