aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/widgets/mlabel.h
blob: 9338f2281641abd6e5e7fc012ac63695caa7ff5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#ifndef MLABEL_H
#define MLABEL_H

#include <mwidgetcontroller.h>
#include <mlabelmodel.h>

class MLabelPrivate;
class MLabelHighlighter;
class QString;

/*!
    \class MLabel
    \brief MLabel provides functionality for displaying text.

    \ingroup widgets

    \section MLabelOverview Overview
        MLabel provides functionality for displaying text. It can display
        plain text as well as rich text. The positioning of the text can be
        changed using setAlignment(). The appearance of the text can be tweaked
        using setTextDirection(), setWordWrap() and setTextElide().

        Following list summarizes the functionality of MLabel:

        - Text direction can be set.
        - Support for auto wrapping and manual line changes for rich text.
          Use "<br>" to do a manual line change.
        - Support for automatic text eliding. If the label is not fitting into
          the given area, the label is truncated, and elision marks "..." are
          added in the end of the label. For truncation, the label has to be at
          least 4 characters long.
        - Rich label supports HTML tags and CSS styling for text formatting
          (such as bold, color, font, italics). See <a href="http://doc.trolltech.com/richtext-html-subset.html">
          Supported HTML Subset</a> for more details.
        - Rich label supports highlighting and execution of links/anchors.
          linkActivated() signal is emitted when a link is clicked.

    \section MLabelUseGuidelines Usage guidelines
        - Simple label is used for plain text on one line such as device buttons.
        - Rich label is used when text styling and formatting are needed.
        - Application decides:
            - Position of the label in the view.
            - Content of the label.
            - Available area for the label.
            - Check that left alignment is OK for the your design, otherwise
              override the alignment. For instance in button the text needs to
              be center aligned.

    \section MLabelVariants Variants
        \li \link MLabelView Default view. \endlink Supports both simple and
            rich text.

    \section MLabelOpenIssues Open issues
        - requirements of localizations:
            - bi-directional languages - do we need to swap the label order in
              the same component?
            - bi-directional languages - aligning from left edge to right edge.
            - bi-directional languages - role of wrapping.
            - different label sub variations required by the localization - do
              we need to have two sub label types per language, one which is
              optimized for the space and another one that uses the whole
              available space.
            - Layouting in whole UI - to which extent our navigation and
              transitions do take into account the localization.

    \section MLabelExamples Examples
        Simple label:
        \code
            //simple label with text
            MLabel* label = new MLabel("Simple label");

            //set/change the text of label
            label->setText("Text");

        \endcode

        Rich label:
        \code
            //rich label with bold text.
            MLabel* label = new MLabel("Rich <b>bold</b> label");

            //manual line changes in rich label
            label->setText("row1<br><i>row2</i><br>row3");
        \endcode

        Rich label with custom text color:
        \code
            QString styledText = "<style>red{color:#FF0000;}</style> <red>Rich</red> label can be <red>styled</red>.";
            MLabel* label = new MLabel(styledText);
        \endcode

        Links in rich label:
        \code
            //create label with a link
            QString text = "Rich label can contain <a href=\"http://www.nokia.com\"> links </a>.";
            MLabel* label = new MLabel(styledText);

            //connect to signal to receive notification when user clicks a link in label
            connect(label, SIGNAL(linkActivated(QString)), this, SLOT(linkActivated(QString)));
        \endcode
    \sa MLabelModel MLabelStyle <a href="http://doc.trolltech.com/richtext-html-subset.html"> Supported HTML Subset</a>
*/
class M_EXPORT MLabel : public MWidgetController
{
    Q_OBJECT
    M_CONTROLLER(MLabel)

    /*!
        \property MLabel::alignment
        \brief Alignmentation of the label.

        See MLabelModel::alignment for details.
    */
    Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)

    /*!
        \property MLabel::wordWrap
        \brief Is word wrapping done or not
    */
    Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap)
        
    /*!
        \property MLabel::WrapMode
        \brief Word wrapping mode of the label.

        Used only if 'wordWrap' is true.
    */
    Q_PROPERTY(QTextOption::WrapMode wrapMode READ wrapMode WRITE setWrapMode)

    /*!
        \property MLabel::textElide
        \brief Text eliding mode of the label.

        See MLabelModel::textElide for details.
    */
    Q_PROPERTY(bool textElide READ textElide WRITE setTextElide)

    /*!
        \property MLabel::text
        \brief Text of the label.

        See MLabelModel::text for details.
    */
    Q_PROPERTY(QString text READ text WRITE setText)

    /*!
        \property MLabel::color
        \brief Color of the label text.

        See MLabelModel::color for details.
    */
    Q_PROPERTY(QColor color READ color WRITE setColor)

public:
    
    /*!
        \brief Constructs label widget.
        \param parent optional parent.
        \param model optional model.
     */
    MLabel(QGraphicsItem *parent = 0, MLabelModel *model = 0);

    /*!
        \brief Constructs a label with a text.
        \param text Label text.
        \param parent Optional parent.
     */
    explicit MLabel(QString const &text, QGraphicsItem *parent = 0);

    /*!
        \brief Destructs label widget.
     */
    virtual ~MLabel();

    /*!
        \brief Set an alignmentation for the text.
        \sa Qt::Alignment
     */
    void setAlignment(Qt::Alignment alignment);

    /*!
        \brief Returns the current alignmentation.
        \return alignment
     */
    Qt::Alignment alignment() const;

    /*!
        \brief Returns wrapping mode for the text in the label.

        If the label doesn't have enough space to show the full text on one 
        line, the text will be wrapped to multiple lines if a mode with
        wrapping behavior is active.        

        \return wrap mode
     */
    
    QTextOption::WrapMode wrapMode() const;

    /*!
        \brief Set word wrapping mode.
        \sa wrapMode
    */
    void setWrapMode(QTextOption::WrapMode wrapMode);

    /*!
        \brief Enable/disable automatic word wrapping.

        If true, wrapping is done according to the mode set with setWrapMode.
     */
    void setWordWrap(bool wrap);

    /*!
        \brief Returns whether the text in label will be wrapped or not.

        If the label doesn't have enough space to show the full text on one
        line, the text will be wrapped to multiple lines.

        \return wrap mode
     */
    bool wordWrap() const;

    /*!
        Returns the current text.
        \return text
     */
    QString text() const;

    /*!
        \brief Enable/disable automatic text eliding.

        If the label doesn't have enough space to show the full text on one
        line, the will be elided with three dots.

        \sa Qt::TextElideMode
     */
    void setTextElide(bool elide);

    /*!
        \brief Returns the elide mode of the label.
        \return elide mode of the label

        \sa Qt::TextElideMode
     */
    bool textElide() const;

    /*!
        \brief Set the font used in MLabel.

        By default MLabel uses font defined in the css file. This method
        can be used to change the font from code.

        Overrides QGraphicsWidget::setFont(const QFont &font)
     */
    void setFont(const QFont &font);

    /*!
        \brief Return the current font.

        The current font is either a font previously set with setFont() or
        if nothing has been manually set the one that has been defined in css
        for this particular label.

        Overrides QGraphicsWidget::font() const.

        \return current font
     */
    QFont font() const;

    /*!
        \brief Set the text color for the MLabel.

        By default MLabel uses color defined in the css file. This method
        can be used to change the color from code. Giving invalid color
        (QColor()) takes font in the css back to use.
     */
    void setColor(const QColor &color);

    /*!
        \brief Returns the color previously set with setColor().

        \return current color
     */
    QColor color() const;

    /*!
        \brief Add highlighter object to mlabel.

        \sa MLabelHighlighter
    */
    void addHighlighter(MLabelHighlighter *highlighter);

    /*!
        \brief Remove highlighter object from mlabel.

        \sa MLabelHighlighter
    */
    void removeHighlighter(MLabelHighlighter *highlighter);

    /*!
        \brief Remove all highlighter objects from mlabel.

        \sa MLabelHighlighter
    */
    void removeAllHighlighters();

public Q_SLOTS:

    /*!
        \brief Set text for the label.
    */
    void setText(const QString &text);

Q_SIGNALS:
    /*!
        \brief A signal which is emitted when an anchor in the label is clicked.

        \a link contains the clicked link. The signal is only emitted when the link
        is defined using anchor "<a href=\"http://www.nokia.com\"> nokia </a>" html
        tag.

     */
    void linkActivated(const QString &link);

protected:

    //! \reimp
    virtual void changeEvent(QEvent *event);
    virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
    virtual void setupModel();
    //! \reimp_end

private:
    Q_DISABLE_COPY(MLabel)
    Q_DECLARE_PRIVATE(MLabel)
};
#endif