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

« back to all changes in this revision

Viewing changes to qwt/src/qwt_text_label.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-07-01 16:08:21 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090701160821-gog44m4w7x2f4u6o
Tags: 5.2.0-1
* New upstream release.
* Add branch pull patch from svn r544.
* Bump Standards-Version to 3.8.2. No changes needed.
* Update installed manpages.
* Use qwt as base name for directory (drop version).
* Add missing warnings patch.
* Rewrite debian/rules: use dh.

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
/*! 
 
218
   Qt paint event
 
219
   \param event Paint event
 
220
*/
 
221
void QwtTextLabel::paintEvent(QPaintEvent *event)
 
222
{
 
223
#if QT_VERSION >= 0x040000
 
224
    QPainter painter(this);
 
225
 
 
226
    if ( !contentsRect().contains( event->rect() ) )
 
227
    {
 
228
        painter.save();
 
229
        painter.setClipRegion( event->region() & frameRect() );
 
230
        drawFrame( &painter );
 
231
        painter.restore();
 
232
    }
 
233
 
 
234
    painter.setClipRegion(event->region() & contentsRect());
 
235
 
 
236
    drawContents( &painter );
 
237
#else // QT_VERSION < 0x040000
 
238
    QFrame::paintEvent(event);
 
239
#endif
 
240
 
 
241
}
 
242
 
 
243
//! Redraw the text and focus indicator
 
244
void QwtTextLabel::drawContents(QPainter *painter)
 
245
{
 
246
    const QRect r = textRect();
 
247
    if ( r.isEmpty() )
 
248
        return;
 
249
 
 
250
    painter->setFont(font());
 
251
#if QT_VERSION < 0x040000
 
252
    painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
 
253
#else
 
254
    painter->setPen(palette().color(QPalette::Active, QPalette::Text));
 
255
#endif
 
256
 
 
257
    drawText(painter, r);
 
258
 
 
259
    if ( hasFocus() )
 
260
    {
 
261
        const int margin = 2;
 
262
 
 
263
        QRect focusRect = contentsRect();
 
264
        focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
 
265
            focusRect.width() - 2 * margin - 2, 
 
266
            focusRect.height() - 2 * margin - 2);
 
267
 
 
268
        QwtPainter::drawFocusRect(painter, this, focusRect);
 
269
    }
 
270
}
 
271
 
 
272
//! Redraw the text
 
273
void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
 
274
{
 
275
    d_data->text.draw(painter, textRect);
 
276
}
 
277
 
 
278
/*! 
 
279
  Calculate the rect for the text in widget coordinates
 
280
  \return Text rect
 
281
*/
 
282
QRect QwtTextLabel::textRect() const
 
283
{
 
284
    QRect r = contentsRect();
 
285
 
 
286
    if ( !r.isEmpty() && d_data->margin > 0 )
 
287
    {
 
288
        r.setRect(r.x() + d_data->margin, r.y() + d_data->margin,
 
289
            r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
 
290
    }
 
291
 
 
292
    if ( !r.isEmpty() )
 
293
    {
 
294
        int indent = d_data->indent;
 
295
        if ( indent <= 0 )
 
296
            indent = defaultIndent();
 
297
 
 
298
        if ( indent > 0 )
 
299
        {
 
300
            const int renderFlags = d_data->text.renderFlags();
 
301
 
 
302
            if ( renderFlags & Qt::AlignLeft )
 
303
                r.setX(r.x() + indent);
 
304
            else if ( renderFlags & Qt::AlignRight )
 
305
                r.setWidth(r.width() - indent);
 
306
            else if ( renderFlags & Qt::AlignTop )
 
307
                r.setY(r.y() + indent);
 
308
            else if ( renderFlags & Qt::AlignBottom )
 
309
                r.setHeight(r.height() - indent);
 
310
        }
 
311
    }
 
312
 
 
313
    return r;
 
314
}
 
315
 
 
316
int QwtTextLabel::defaultIndent() const
 
317
{
 
318
    if ( frameWidth() <= 0 )
 
319
        return 0;
 
320
 
 
321
    QFont fnt;
 
322
    if ( d_data->text.testPaintAttribute(QwtText::PaintUsingTextFont) )
 
323
        fnt = d_data->text.font();
 
324
    else
 
325
        fnt = font();
 
326
 
 
327
    return QFontMetrics(fnt).width('x') / 2;
 
328
}
 
329