source: trunk/examples/demo/dnd/styledbutton.cpp@ 208

Last change on this file since 208 was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Designer.
5**
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.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "styledbutton.h"
22
23#include <qcolordialog.h>
24#include <qpalette.h>
25#include <qlabel.h>
26#include <qpainter.h>
27#include <qimage.h>
28#include <qpixmap.h>
29#include <qapplication.h>
30#include <qdragobject.h>
31#include <qstyle.h>
32
33StyledButton::StyledButton(QWidget* parent, const char* name)
34 : QButton( parent, name ), pix( 0 ), spix( 0 ), edit( ColorEditor ), s( 0 ), mousePressed( FALSE )
35{
36 setMinimumSize( minimumSizeHint() );
37 setAcceptDrops( TRUE );
38
39 connect( this, SIGNAL(clicked()), SLOT(onEditor()));
40}
41
42StyledButton::StyledButton( const QBrush& b, QWidget* parent, const char* name, WFlags f )
43 : QButton( parent, name, f ), spix( 0 ), s( 0 )
44{
45 col = b.color();
46 pix = b.pixmap();
47 setMinimumSize( minimumSizeHint() );
48}
49
50StyledButton::~StyledButton()
51{
52 if ( pix ) {
53 delete pix;
54 pix = 0;
55 }
56 if ( spix ) {
57 delete spix;
58 spix = 0;
59 }
60}
61
62void StyledButton::setEditor( EditorType e )
63{
64 if ( edit == e )
65 return;
66
67 edit = e;
68 update();
69}
70
71StyledButton::EditorType StyledButton::editor() const
72{
73 return edit;
74}
75
76void StyledButton::setColor( const QColor& c )
77{
78 col = c;
79 update();
80}
81
82void StyledButton::setPixmap( const QPixmap & pm )
83{
84 if ( !pm.isNull() ) {
85 delete pix;
86 pix = new QPixmap( pm );
87 } else {
88 delete pix;
89 pix = 0;
90 }
91 scalePixmap();
92}
93
94QColor StyledButton::color() const
95{
96 return col;
97}
98
99QPixmap* StyledButton::pixmap() const
100{
101 return pix;
102}
103
104bool StyledButton::scale() const
105{
106 return s;
107}
108
109void StyledButton::setScale( bool on )
110{
111 if ( s == on )
112 return;
113
114 s = on;
115 scalePixmap();
116}
117
118QSize StyledButton::sizeHint() const
119{
120 return QSize( 50, 25 );
121}
122
123QSize StyledButton::minimumSizeHint() const
124{
125 return QSize( 50, 25 );
126}
127
128void StyledButton::scalePixmap()
129{
130 delete spix;
131
132 if ( pix ) {
133 spix = new QPixmap( 6*width()/8, 6*height()/8 );
134 QImage img = pix->convertToImage();
135
136 spix->convertFromImage( s? img.smoothScale( 6*width()/8, 6*height()/8 ) : img );
137 } else {
138 spix = 0;
139 }
140
141 update();
142}
143
144void StyledButton::resizeEvent( QResizeEvent* e )
145{
146 scalePixmap();
147 QButton::resizeEvent( e );
148}
149
150void StyledButton::drawButton( QPainter *paint )
151{
152 style().drawPrimitive(QStyle::PE_ButtonBevel, paint, rect(), colorGroup(),
153 isDown() ? QStyle::Style_Sunken : QStyle::Style_Default);
154 drawButtonLabel(paint);
155
156 if (hasFocus())
157 style().drawPrimitive(QStyle::PE_FocusRect, paint,
158 style().subRect(QStyle::SR_PushButtonFocusRect, this),
159 colorGroup(), QStyle::Style_Default);
160}
161
162void StyledButton::drawButtonLabel( QPainter *paint )
163{
164 QColor pen = isEnabled() ?
165 hasFocus() ? palette().active().buttonText() : palette().inactive().buttonText()
166 : palette().disabled().buttonText();
167 paint->setPen( pen );
168
169 if(!isEnabled()) {
170 paint->setBrush( QBrush( colorGroup().button() ) );
171 }
172 else if ( edit == PixmapEditor && spix ) {
173 paint->setBrush( QBrush( col, *spix ) );
174 paint->setBrushOrigin( width()/8, height()/8 );
175 } else
176 paint->setBrush( QBrush( col ) );
177
178 paint->drawRect( width()/8, height()/8, 6*width()/8, 6*height()/8 );
179}
180
181void StyledButton::onEditor()
182{
183 switch (edit) {
184 case ColorEditor: {
185 QColor c = QColorDialog::getColor( palette().active().background(), this );
186 if ( c.isValid() ) {
187 setColor( c );
188 emit changed();
189 }
190 } break;
191 case PixmapEditor: {
192 QPixmap p;
193 /*
194 if ( pixmap() )
195 p = qChoosePixmap( this,*pixmap() );
196 else
197 p = qChoosePixmap( this, QPixmap() );
198 if ( !p.isNull() ) {
199 setPixmap( p );
200 emit changed();
201 }
202 */
203 } break;
204 default:
205 break;
206 }
207}
208
209void StyledButton::mousePressEvent(QMouseEvent* e)
210{
211 QButton::mousePressEvent(e);
212 mousePressed = TRUE;
213 pressPos = e->pos();
214}
215
216void StyledButton::mouseMoveEvent(QMouseEvent* e)
217{
218 QButton::mouseMoveEvent( e );
219#ifndef QT_NO_DRAGANDDROP
220 if ( !mousePressed )
221 return;
222 if ( ( pressPos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
223 if ( edit == ColorEditor ) {
224 QColorDrag *drg = new QColorDrag( col, this );
225 QPixmap pix( 25, 25 );
226 pix.fill( col );
227 QPainter p( &pix );
228 p.drawRect( 0, 0, pix.width(), pix.height() );
229 p.end();
230 drg->setPixmap( pix );
231 mousePressed = FALSE;
232 drg->dragCopy();
233 }
234 else if ( edit == PixmapEditor && pix && !pix->isNull() ) {
235 QImage img = pix->convertToImage();
236 QImageDrag *drg = new QImageDrag( img, this );
237 if(spix)
238 drg->setPixmap( *spix );
239 mousePressed = FALSE;
240 drg->dragCopy();
241 }
242 }
243#endif
244}
245
246#ifndef QT_NO_DRAGANDDROP
247void StyledButton::dragEnterEvent( QDragEnterEvent *e )
248{
249 setFocus();
250 if ( edit == ColorEditor && QColorDrag::canDecode( e ) )
251 e->accept();
252 else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) )
253 e->accept();
254 else
255 e->ignore();
256}
257
258void StyledButton::dragLeaveEvent( QDragLeaveEvent * )
259{
260 if ( hasFocus() )
261 parentWidget()->setFocus();
262}
263
264void StyledButton::dragMoveEvent( QDragMoveEvent *e )
265{
266 if ( edit == ColorEditor && QColorDrag::canDecode( e ) )
267 e->accept();
268 else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) )
269 e->accept();
270 else
271 e->ignore();
272}
273
274void StyledButton::dropEvent( QDropEvent *e )
275{
276 if ( edit == ColorEditor && QColorDrag::canDecode( e ) ) {
277 QColor color;
278 QColorDrag::decode( e, color );
279 setColor(color);
280 emit changed();
281 e->accept();
282 }
283 else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) ) {
284 QImage img;
285 QImageDrag::decode( e, img );
286 QPixmap pm;
287 pm.convertFromImage(img);
288 setPixmap(pm);
289 emit changed();
290 e->accept();
291 } else {
292 e->ignore();
293 }
294}
295#endif // QT_NO_DRAGANDDROP
Note: See TracBrowser for help on using the repository browser.