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

« back to all changes in this revision

Viewing changes to src/newui/button.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2004 by Alexander Dymo                                  *
 
3
 *   adymo@kdevelop.org                                                    *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU Library General Public License as       *
 
7
 *   published by the Free Software Foundation; either version 2 of the    *
 
8
 *   License, or (at your option) any later version.                       *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU Library General Public     *
 
16
 *   License along with this program; if not, write to the                 *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
 ***************************************************************************/
 
20
#include "button.h"
 
21
 
 
22
#include <qpainter.h>
 
23
#include <qtooltip.h>
 
24
#include <qstyle.h>
 
25
#include <qapplication.h>
 
26
 
 
27
#include <kdebug.h>
 
28
#include <kiconloader.h>
 
29
 
 
30
#include "buttonbar.h"
 
31
 
 
32
namespace Ideal {
 
33
 
 
34
Button::Button(ButtonBar *parent, const QString text, const QIconSet &icon,
 
35
    const QString &description)
 
36
    :QPushButton(icon, text, parent), m_buttonBar(parent), m_description(description),
 
37
    m_place(parent->place()), m_realText(text), m_realIconSet(icon)
 
38
{
 
39
    hide();
 
40
    setFlat(true);
 
41
    setToggleButton(true);
 
42
    setFocusPolicy(NoFocus);
 
43
    setDescription(m_description);
 
44
    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
 
45
    resize(sizeHint());
 
46
    fixDimensions(Ideal::Bottom);
 
47
    
 
48
    QToolTip::add(this, m_realText);
 
49
}
 
50
 
 
51
Button::~Button()
 
52
{
 
53
//     m_buttonBar->removeButton(this);
 
54
}
 
55
 
 
56
void Button::setDescription(const QString &description)
 
57
{
 
58
    m_description = description;
 
59
    QToolTip::remove(this);
 
60
    QToolTip::add(this, m_description);
 
61
}
 
62
 
 
63
QString Button::description() const
 
64
{
 
65
    return m_description;
 
66
}
 
67
 
 
68
void Button::drawButton(QPainter *p)
 
69
{
 
70
    QRect r = rect();
 
71
    QSize sh = r.size();
 
72
    switch (m_place)
 
73
    {
 
74
        case Ideal::Left:
 
75
        case Ideal::Right:
 
76
            sh.setHeight(r.width());
 
77
            sh.setWidth(r.height());
 
78
            break;
 
79
    }
 
80
 
 
81
    QStyle::SFlags flags = QStyle::Style_Default;
 
82
    if (isEnabled())
 
83
        flags |= QStyle::Style_Enabled;
 
84
    if (hasFocus())
 
85
        flags |= QStyle::Style_HasFocus;
 
86
    if (isDown())
 
87
        flags |= QStyle::Style_Down;
 
88
    if (isOn())
 
89
        flags |= QStyle::Style_On;
 
90
    if (! isFlat() && ! isDown())
 
91
        flags |= QStyle::Style_Raised;
 
92
    if (isDefault())
 
93
        flags |= QStyle::Style_ButtonDefault;
 
94
 
 
95
    QPixmap pm(sh.width(), sh.height());
 
96
    pm.fill(eraseColor());
 
97
    QPainter p2(&pm);
 
98
    
 
99
    style().drawControl(QStyle::CE_PushButton,&p2,this, QRect(0,0,pm.width(),pm.height()), colorGroup(),flags);
 
100
    
 
101
    style().drawControl(QStyle::CE_PushButtonLabel, &p2, this,
 
102
                        QRect(0,0,pm.width(),pm.height()),
 
103
                        colorGroup(), flags, QStyleOption());
 
104
 
 
105
    switch (m_place)
 
106
    {
 
107
        case Ideal::Left:
 
108
                p->rotate(-90);
 
109
                p->drawPixmap(1-pm.width(), 0, pm);
 
110
                break;
 
111
        case Ideal::Right:
 
112
                p->rotate(90);
 
113
                p->drawPixmap(0, 1-pm.height(), pm);
 
114
                break;
 
115
        default:
 
116
                p->drawPixmap(0, 0, pm);
 
117
                break;
 
118
    }
 
119
}
 
120
 
 
121
void Button::drawButtonLabel(QPainter */*p*/)
 
122
{
 
123
}
 
124
 
 
125
ButtonMode Button::mode()
 
126
{
 
127
    return m_buttonBar->mode();
 
128
}
 
129
 
 
130
void Button::setPlace(Ideal::Place place)
 
131
{
 
132
    Place oldPlace = m_place;
 
133
    m_place = place;
 
134
    fixDimensions(oldPlace);
 
135
}
 
136
 
 
137
void Button::fixDimensions(Place oldPlace)
 
138
{
 
139
    switch (m_place)
 
140
    {
 
141
        case Ideal::Left:
 
142
        case Ideal::Right:
 
143
            if ((oldPlace == Ideal::Bottom) || (oldPlace == Ideal::Top))
 
144
            {
 
145
                setFixedWidth(height());
 
146
                setMinimumHeight(sizeHint().width());
 
147
                setMaximumHeight(32767);
 
148
            }
 
149
            break;
 
150
        case Ideal::Top:
 
151
        case Ideal::Bottom:
 
152
            if ((oldPlace == Ideal::Left) || (oldPlace == Ideal::Right))
 
153
            {
 
154
                setFixedHeight(width());
 
155
                setMinimumWidth(sizeHint().height());
 
156
                setMaximumWidth(32767);
 
157
            }
 
158
            break;
 
159
    }
 
160
}
 
161
 
 
162
QSize Button::sizeHint() const
 
163
{
 
164
    return sizeHint(text());
 
165
}
 
166
 
 
167
QSize Button::sizeHint(const QString &text) const
 
168
{
 
169
    constPolish();
 
170
    int w = 0, h = 0;
 
171
 
 
172
    if ( iconSet() && !iconSet()->isNull() && (m_buttonBar->mode() != Text) ) {
 
173
        int iw = iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).width() + 4;
 
174
        int ih = iconSet()->pixmap( QIconSet::Small, QIconSet::Normal ).height();
 
175
        w += iw;
 
176
        h = QMAX( h, ih );
 
177
    }
 
178
    if ( isMenuButton() )
 
179
        w += style().pixelMetric(QStyle::PM_MenuButtonIndicator, this);
 
180
    if ( pixmap() ) {
 
181
        QPixmap *pm = (QPixmap *)pixmap();
 
182
        w += pm->width();
 
183
        h += pm->height();
 
184
    } else if (m_buttonBar->mode() != Icons) {
 
185
        QString s( text );
 
186
        bool empty = s.isEmpty();
 
187
        if ( empty )
 
188
            s = QString::fromLatin1("XXXX");
 
189
        QFontMetrics fm = fontMetrics();
 
190
        QSize sz = fm.size( ShowPrefix, s );
 
191
        if(!empty || !w)
 
192
            w += sz.width();
 
193
        if(!empty || !h)
 
194
            h = QMAX(h, sz.height());
 
195
    }
 
196
 
 
197
    return (style().sizeFromContents(QStyle::CT_ToolButton, this, QSize(w, h)).
 
198
            expandedTo(QApplication::globalStrut()));
 
199
}
 
200
 
 
201
void Ideal::Button::updateSize()
 
202
{
 
203
    switch (m_place)
 
204
    {
 
205
        case Ideal::Left:
 
206
        case Ideal::Right:
 
207
             setMinimumHeight(sizeHint().width());
 
208
             resize(sizeHint().height(), sizeHint().width());
 
209
            break;
 
210
        case Ideal::Top:
 
211
        case Ideal::Bottom:
 
212
            resize(sizeHint().width(), sizeHint().height());
 
213
            break;
 
214
    }
 
215
}
 
216
 
 
217
QString Button::realText() const
 
218
{
 
219
    return m_realText;
 
220
}
 
221
 
 
222
void Button::setMode(Ideal::ButtonMode mode)
 
223
{
 
224
    switch (mode)
 
225
    {
 
226
        case Text:
 
227
            disableIconSet();
 
228
            enableText();
 
229
            break;
 
230
        case IconsAndText:
 
231
            enableIconSet();
 
232
            enableText();
 
233
            break;
 
234
        case Icons:
 
235
            disableText();
 
236
            enableIconSet();
 
237
            break;
 
238
    }
 
239
}
 
240
 
 
241
void Button::enableIconSet()
 
242
{
 
243
    if (!iconSet())
 
244
    {
 
245
        if (m_realIconSet.isNull())
 
246
            m_realIconSet = SmallIcon("file_new");
 
247
        setIconSet(m_realIconSet);
 
248
    }
 
249
}
 
250
 
 
251
void Button::disableIconSet()
 
252
{
 
253
    setIconSet(QIconSet());
 
254
}
 
255
 
 
256
void Button::disableText()
 
257
{
 
258
    if (text().length() > 0)
 
259
        setText("");
 
260
}
 
261
 
 
262
void Button::enableText()
 
263
{
 
264
    setText(m_realText);
 
265
}
 
266
 
 
267
}
 
268
 
 
269
#include "button.moc"