~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.2/src/qwt_text_label.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-04-12 23:25:58 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090412232558-3bl06x785yr8xm8u
Tags: 5.1.2-1
* New upstream release.
* Bump compat/debhelper to 7.
* Bump Standards-Version to 3.8.1. No changes needed.
* Invert Maintainers and Uploaders field.
* Fix lintian warnings:
  - dh_clean _k deprecated.
  - missing dependency on libc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2002   Uwe Rathmann
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
// vim: expandtab
 
11
 
 
12
#include <qpainter.h>
 
13
#include <qevent.h>
 
14
#include "qwt_text.h"
 
15
#include "qwt_painter.h"
 
16
#include "qwt_text_label.h"
 
17
 
 
18
class QwtTextLabel::PrivateData
 
19
{
 
20
public:
 
21
    PrivateData():
 
22
        indent(4),
 
23
        margin(0)
 
24
    {
 
25
    }
 
26
 
 
27
    int indent;
 
28
    int margin;
 
29
    QwtText text;
 
30
};
 
31
 
 
32
/*! 
 
33
  Constructs an empty label.
 
34
  \param parent Parent widget
 
35
*/
 
36
QwtTextLabel::QwtTextLabel(QWidget *parent):
 
37
    QFrame(parent)
 
38
{
 
39
    init();
 
40
}
 
41
 
 
42
#if QT_VERSION < 0x040000
 
43
/*! 
 
44
  Constructs an empty label.
 
45
  \param parent Parent widget
 
46
  \param name Object name
 
47
*/
 
48
QwtTextLabel::QwtTextLabel(QWidget *parent, const char *name):
 
49
    QFrame(parent, name)
 
50
{
 
51
    init();
 
52
}
 
53
#endif
 
54
 
 
55
/*!
 
56
  Constructs a label that displays the text, text
 
57
  \param parent Parent widget
 
58
  \param text Text
 
59
*/
 
60
QwtTextLabel::QwtTextLabel(const QwtText &text, QWidget *parent):
 
61
    QFrame(parent)
 
62
{
 
63
    init();
 
64
    d_data->text = text;
 
65
}
 
66
 
 
67
//! Destructor
 
68
QwtTextLabel::~QwtTextLabel()
 
69
{
 
70
    delete d_data;
 
71
}
 
72
 
 
73
void QwtTextLabel::init()
 
74
{
 
75
    d_data = new PrivateData();
 
76
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
 
77
}
 
78
 
 
79
/*!
 
80
   Change the label's text, keeping all other QwtText attributes
 
81
   \param text New text
 
82
   \param textFormat Format of text
 
83
 
 
84
  \sa QwtText
 
85
*/
 
86
void QwtTextLabel::setText(const QString &text, QwtText::TextFormat textFormat)
 
87
{
 
88
    d_data->text.setText(text, textFormat);
 
89
 
 
90
    update();
 
91
    updateGeometry();
 
92
}
 
93
 
 
94
/*!
 
95
   Change the label's text
 
96
   \param text New text
 
97
*/
 
98
void QwtTextLabel::setText(const QwtText &text)
 
99
{
 
100
    d_data->text = text;
 
101
 
 
102
    update();
 
103
    updateGeometry();
 
104
}
 
105
 
 
106
//! Return the text
 
107
const QwtText &QwtTextLabel::text() const
 
108
{
 
109
    return d_data->text;
 
110
}
 
111
 
 
112
//! Clear the text and all QwtText attributes
 
113
void QwtTextLabel::clear()
 
114
{
 
115
    d_data->text = QwtText();
 
116
 
 
117
    update();
 
118
    updateGeometry();
 
119
}
 
120
 
 
121
//! Return label's text indent in pixels
 
122
int QwtTextLabel::indent() const
 
123
{
 
124
    return d_data->indent;
 
125
}
 
126
 
 
127
/*!
 
128
  Set label's text indent in pixels
 
129
  \param indent Indentation in pixels
 
130
*/
 
131
void QwtTextLabel::setIndent(int indent)
 
132
{
 
133
    if ( indent < 0 )
 
134
        indent = 0;
 
135
 
 
136
    d_data->indent = indent;
 
137
 
 
138
    update();
 
139
    updateGeometry();
 
140
}
 
141
 
 
142
//! Return label's text indent in pixels
 
143
int QwtTextLabel::margin() const
 
144
{
 
145
    return d_data->margin;
 
146
}
 
147
 
 
148
/*!
 
149
  Set label's margin in pixels
 
150
  \param margin Margin in pixels
 
151
*/
 
152
void QwtTextLabel::setMargin(int margin)
 
153
{
 
154
    d_data->margin = margin;
 
155
 
 
156
    update();
 
157
    updateGeometry();
 
158
}
 
159
 
 
160
//! Return label's margin in pixels
 
161
QSize QwtTextLabel::sizeHint() const
 
162
{
 
163
    return minimumSizeHint();
 
164
}
 
165
 
 
166
//! Return a minimum size hint
 
167
QSize QwtTextLabel::minimumSizeHint() const
 
168
{
 
169
    QSize sz = d_data->text.textSize(font());
 
170
 
 
171
    int mw = 2 * (frameWidth() + d_data->margin);
 
172
    int mh = mw;
 
173
 
 
174
    int indent = d_data->indent;
 
175
    if ( indent <= 0 )
 
176
        indent = defaultIndent();
 
177
 
 
178
    if ( indent > 0 )
 
179
    {
 
180
        const int align = d_data->text.renderFlags();
 
181
        if ( align & Qt::AlignLeft || align & Qt::AlignRight )
 
182
            mw += d_data->indent;
 
183
        else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
 
184
            mh += d_data->indent;
 
185
    }
 
186
        
 
187
    sz += QSize(mw, mh);
 
188
 
 
189
    return sz;
 
190
}
 
191
 
 
192
/*!
 
193
   Returns the preferred height for this widget, given the width.
 
194
   \param width Width
 
195
*/
 
196
int QwtTextLabel::heightForWidth(int width) const
 
197
{
 
198
    const int renderFlags = d_data->text.renderFlags();
 
199
 
 
200
    int indent = d_data->indent;
 
201
    if ( indent <= 0 )
 
202
        indent = defaultIndent();
 
203
 
 
204
    width -= 2 * frameWidth();
 
205
    if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
 
206
        width -= indent;
 
207
 
 
208
    int height = d_data->text.heightForWidth(width, font());
 
209
    if ( renderFlags & Qt::AlignTop || renderFlags & Qt::AlignBottom )
 
210
        height += indent;
 
211
 
 
212
    height += 2 * frameWidth();
 
213
 
 
214
    return height;
 
215
}
 
216
 
 
217
//! Qt paint event
 
218
void QwtTextLabel::paintEvent(QPaintEvent *event)
 
219
{
 
220
#if QT_VERSION >= 0x040000
 
221
    QPainter painter(this);
 
222
 
 
223
    if ( !contentsRect().contains( event->rect() ) )
 
224
    {
 
225
        painter.save();
 
226
        painter.setClipRegion( event->region() & frameRect() );
 
227
        drawFrame( &painter );
 
228
        painter.restore();
 
229
    }
 
230
 
 
231
    painter.setClipRegion(event->region() & contentsRect());
 
232
 
 
233
    drawContents( &painter );
 
234
#else // QT_VERSION < 0x040000
 
235
    QFrame::paintEvent(event);
 
236
#endif
 
237
 
 
238
}
 
239
 
 
240
//! Redraw the text and focus indicator
 
241
void QwtTextLabel::drawContents(QPainter *painter)
 
242
{
 
243
    const QRect r = textRect();
 
244
    if ( r.isEmpty() )
 
245
        return;
 
246
 
 
247
    painter->setFont(font());
 
248
#if QT_VERSION < 0x040000
 
249
    painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
 
250
#else
 
251
    painter->setPen(palette().color(QPalette::Active, QPalette::Text));
 
252
#endif
 
253
 
 
254
    drawText(painter, r);
 
255
 
 
256
    if ( hasFocus() )
 
257
    {
 
258
        const int margin = 2;
 
259
 
 
260
        QRect focusRect = contentsRect();
 
261
        focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
 
262
            focusRect.width() - 2 * margin - 2, 
 
263
            focusRect.height() - 2 * margin - 2);
 
264
 
 
265
        QwtPainter::drawFocusRect(painter, this, focusRect);
 
266
    }
 
267
}
 
268
 
 
269
//! Redraw the text
 
270
void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
 
271
{
 
272
    d_data->text.draw(painter, textRect);
 
273
}
 
274
 
 
275
/*! 
 
276
  Calculate the rect for the text in widget coordinates
 
277
  \return Text rect
 
278
*/
 
279
QRect QwtTextLabel::textRect() const
 
280
{
 
281
    QRect r = contentsRect();
 
282
 
 
283
    if ( !r.isEmpty() && d_data->margin > 0 )
 
284
    {
 
285
        r.setRect(r.x() + d_data->margin, r.y() + d_data->margin,
 
286
            r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
 
287
    }
 
288
 
 
289
    if ( !r.isEmpty() )
 
290
    {
 
291
        int indent = d_data->indent;
 
292
        if ( indent <= 0 )
 
293
            indent = defaultIndent();
 
294
 
 
295
        if ( indent > 0 )
 
296
        {
 
297
            const int renderFlags = d_data->text.renderFlags();
 
298
 
 
299
            if ( renderFlags & Qt::AlignLeft )
 
300
                r.setX(r.x() + indent);
 
301
            else if ( renderFlags & Qt::AlignRight )
 
302
                r.setWidth(r.width() - indent);
 
303
            else if ( renderFlags & Qt::AlignTop )
 
304
                r.setY(r.y() + indent);
 
305
            else if ( renderFlags & Qt::AlignBottom )
 
306
                r.setHeight(r.height() - indent);
 
307
        }
 
308
    }
 
309
 
 
310
    return r;
 
311
}
 
312
 
 
313
int QwtTextLabel::defaultIndent() const
 
314
{
 
315
    if ( frameWidth() <= 0 )
 
316
        return 0;
 
317
 
 
318
    QFont fnt;
 
319
    if ( d_data->text.testPaintAttribute(QwtText::PaintUsingTextFont) )
 
320
        fnt = d_data->text.font();
 
321
    else
 
322
        fnt = font();
 
323
 
 
324
    return QFontMetrics(fnt).width('x') / 2;
 
325
}
 
326