~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to kdevdesigner/designer/styledbutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
** 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.
14
 
**
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.
17
 
**
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.
21
 
**
22
 
** Contact info@trolltech.com if any conditions of this licensing are
23
 
** not clear to you.
24
 
**
25
 
**********************************************************************/
26
 
 
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>
32
 
#include <qpalette.h>
33
 
#include <qlabel.h>
34
 
#include <qpainter.h>
35
 
#include <qimage.h>
36
 
#include <qpixmap.h>
37
 
#include <qapplication.h>
38
 
#include <qdragobject.h>
39
 
#include <qstyle.h>
40
 
 
41
 
StyledButton::StyledButton(QWidget* parent, const char* name)
42
 
    : QButton( parent, name ), pix( 0 ), spix( 0 ), s( 0 ), formWindow( 0 ), mousePressed( FALSE )
43
 
{
44
 
    setMinimumSize( minimumSizeHint() );
45
 
    setAcceptDrops( TRUE );
46
 
 
47
 
    connect( this, SIGNAL(clicked()), SLOT(onEditor()));
48
 
 
49
 
    setEditor( ColorEditor );
50
 
}
51
 
 
52
 
StyledButton::StyledButton( const QBrush& b, QWidget* parent, const char* name, WFlags f )
53
 
    : QButton( parent, name, f ), spix( 0 ), s( 0 ), formWindow( 0 )
54
 
{
55
 
    col = b.color();
56
 
    pix = b.pixmap();
57
 
    setMinimumSize( minimumSizeHint() );
58
 
}
59
 
 
60
 
StyledButton::~StyledButton()
61
 
{
62
 
}
63
 
 
64
 
void StyledButton::setEditor( EditorType e )
65
 
{
66
 
    if ( edit == e )
67
 
        return;
68
 
 
69
 
    edit = e;
70
 
    update();
71
 
}
72
 
 
73
 
StyledButton::EditorType StyledButton::editor() const
74
 
{
75
 
    return edit;
76
 
}
77
 
 
78
 
void StyledButton::setColor( const QColor& c )
79
 
{
80
 
    col = c;
81
 
    update();
82
 
}
83
 
 
84
 
void StyledButton::setPixmap( const QPixmap & pm )
85
 
{
86
 
    if ( !pm.isNull() ) {
87
 
        delete pix;
88
 
        pix = new QPixmap( pm );
89
 
    } else {
90
 
        delete pix;
91
 
        pix = 0;
92
 
    }
93
 
    scalePixmap();
94
 
}
95
 
 
96
 
QColor StyledButton::color() const
97
 
{
98
 
    return col;
99
 
}
100
 
 
101
 
QPixmap* StyledButton::pixmap() const
102
 
{
103
 
    return pix;
104
 
}
105
 
 
106
 
bool StyledButton::scale() const
107
 
{
108
 
    return s;
109
 
}
110
 
 
111
 
void StyledButton::setScale( bool on )
112
 
{
113
 
    if ( s == on )
114
 
        return;
115
 
 
116
 
    s = on;
117
 
    scalePixmap();
118
 
}
119
 
 
120
 
QSize StyledButton::sizeHint() const
121
 
{
122
 
    return QSize( 50, 25 );
123
 
}
124
 
 
125
 
QSize StyledButton::minimumSizeHint() const
126
 
{
127
 
    return QSize( 50, 25 );
128
 
}
129
 
 
130
 
void StyledButton::scalePixmap()
131
 
{
132
 
    delete spix;
133
 
 
134
 
    if ( pix ) {
135
 
        spix = new QPixmap( 6*width()/8, 6*height()/8 );
136
 
        QImage img = pix->convertToImage();
137
 
 
138
 
        spix->convertFromImage( s? img.smoothScale( 6*width()/8, 6*height()/8 ) : img );
139
 
    } else {
140
 
        spix = 0;
141
 
    }
142
 
 
143
 
    update();
144
 
}
145
 
 
146
 
void StyledButton::resizeEvent( QResizeEvent* e )
147
 
{
148
 
    scalePixmap();
149
 
    QButton::resizeEvent( e );
150
 
}
151
 
 
152
 
void StyledButton::drawButton( QPainter *paint )
153
 
{
154
 
    style().drawPrimitive(QStyle::PE_ButtonBevel, paint, rect(), colorGroup(),
155
 
                          isDown() ? QStyle::Style_Sunken : QStyle::Style_Raised);
156
 
    drawButtonLabel(paint);
157
 
 
158
 
    if (hasFocus())
159
 
        style().drawPrimitive(QStyle::PE_FocusRect, paint,
160
 
                              style().subRect(QStyle::SR_PushButtonFocusRect, this),
161
 
                              colorGroup(), QStyle::Style_Default);
162
 
}
163
 
 
164
 
void StyledButton::drawButtonLabel( QPainter *paint )
165
 
{
166
 
    QColor pen = isEnabled() ?
167
 
                 hasFocus() ? palette().active().buttonText() : palette().inactive().buttonText()
168
 
                 : palette().disabled().buttonText();
169
 
    paint->setPen( pen );
170
 
 
171
 
    if(!isEnabled()) {
172
 
        paint->setBrush( QBrush( colorGroup().button() ) );
173
 
    }
174
 
    else if ( edit == PixmapEditor && spix ) {
175
 
        paint->setBrush( QBrush( col, *spix ) );
176
 
        paint->setBrushOrigin( width()/8, height()/8 );
177
 
    } else
178
 
        paint->setBrush( QBrush( col ) );
179
 
 
180
 
    paint->drawRect( width()/8, height()/8, 6*width()/8, 6*height()/8 );
181
 
}
182
 
 
183
 
void StyledButton::onEditor()
184
 
{
185
 
    switch (edit) {
186
 
    case ColorEditor: {
187
 
        QColor c = QColorDialog::getColor( palette().active().background(), this );
188
 
        if ( c.isValid() ) {
189
 
            setColor( c );
190
 
            emit changed();
191
 
        }
192
 
    } break;
193
 
    case PixmapEditor: {
194
 
        QPixmap p;
195
 
        if ( pixmap() )
196
 
                p = qChoosePixmap( this, formWindow, *pixmap() );
197
 
        else
198
 
                p = qChoosePixmap( this, formWindow, QPixmap() );
199
 
        if ( !p.isNull() ) {
200
 
            setPixmap( p );
201
 
            emit changed();
202
 
        }
203
 
    } break;
204
 
    default:
205
 
        break;
206
 
    }
207
 
}
208
 
 
209
 
void StyledButton::mousePressEvent(QMouseEvent* e)
210
 
{
211
 
    QButton::mousePressEvent(e);
212
 
    mousePressed = TRUE;
213
 
    pressPos = e->pos();
214
 
}
215
 
 
216
 
void 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
247
 
void 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
 
 
258
 
void StyledButton::dragLeaveEvent( QDragLeaveEvent * )
259
 
{
260
 
    if ( hasFocus() )
261
 
        parentWidget()->setFocus();
262
 
}
263
 
 
264
 
void 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
 
 
274
 
void 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