aboutsummaryrefslogtreecommitdiff
path: root/src/views/mlabelview_p.h
blob: 9e6fb4d739c4a278040651e014bf22edb90deb64 (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
/***************************************************************************
**
** 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 MLABELVIEW_P_H
#define MLABELVIEW_P_H

#include <QTextOption>
#include <QTextDocument>
#include <QStaticText>
#include "private/mwidgetview_p.h"

class MLabel;
class MLabelView;
class QGraphicsSceneResizeEvent;
class QGestureEvent;
class QTapAndHoldGesture;

class MLabelViewSimple
{
public:
    MLabelViewSimple(MLabelViewPrivate *viewPrivate);
    virtual ~MLabelViewSimple();

    virtual void drawContents(QPainter *painter, const QSizeF &size);
    virtual bool resizeEvent(QGraphicsSceneResizeEvent *event);
    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
    virtual void setupModel();
    virtual bool updateData(const QList<const char *>& modifications);
    virtual bool isRich();
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    virtual void cancelEvent(MCancelEvent *event);
    virtual void longPressEvent(QGestureEvent *event, QTapAndHoldGesture* gesture);
    virtual void orientationChangeEvent(MOrientationChangeEvent *event);
    virtual void applyStyle();
    void initializeStaticText();

    /**
     * \param width Available width in pixels for the text.
     * \return      Text that should be used for the rendering. Dependent on the available
     *              width, the wrapping-policies, eliding-settings and multilength-status
     *              adjustments are done.
     */
    QString textToRender(qreal width) const;

    /**
     * \param text  Text that should be rendered.
     * \param width Available width in pixels for the text to render.
     * \return      -1.0 (= unrestricted) is returned if no wrapping must be done. If
     *              a wrapping is done it is assured that the returned width is not
     *              larger than \a width.
     */
    qreal restrictedTextWidth(const QString &text, qreal width) const;

    /**
     * \return True, if the wrap-mode indicates that a wrapping should be done.
     */
    bool wrap() const;

    /**
     * \return Text flag for the current wrap-mode specified by the model.
     */
    Qt::TextFlag textFlagForWrapMode() const;
    
    /**
     * Helper method for initializeStaticText(): Adjusts the member variable
     * 'textOffset' dependent on the used alignment, as QStaticText does not support
     * text alignment within a bounding rectangle.
     */
    void adjustTextOffset();

    /**
     * \return The required size of the label to fit into the given width. If the width
     *         is smaller than 0 no restriction for the width is given.
     */
    QSizeF sizeForWidth(qreal width) const;

    /**
     * \return Size for the constraint \a constraint. If the width or height
     *         of the constraint is < 0 it will be replaced by QWIDGETSIZE_MAX.
     */
    QSizeF sizeForConstraint(const QSizeF &constraint) const;

    void markDirty();

    MLabelViewPrivate *viewPrivate;

    QSizeF preferredSize;
    QPointF textOffset;
    QRectF paintingRect;
    bool dirty;
    QStaticText staticText;
    QString unconstraintText;
};

class MLabelViewRich : public MLabelViewSimple
{

public:
    /**
     * Caches parts of the textdocument inside a pixmap to
     * increase the performance. Several tiles might be used
     * as there are size limitations for pixmaps.
     */
    struct Tile {
        qreal y;
        QString pixmapCacheKey;
        QSize size;
    };

    MLabelViewRich(MLabelViewPrivate *viewPrivate);
    virtual ~MLabelViewRich();

    virtual void drawContents(QPainter *painter, const QSizeF &size);
    virtual bool shouldElide() const;
    virtual bool resizeEvent(QGraphicsSceneResizeEvent *event);
    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
    virtual void setupModel();
    virtual bool updateData(const QList<const char *>& modifications);
    virtual bool isRich();
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    virtual void cancelEvent(MCancelEvent *event);
    virtual void longPressEvent(QGestureEvent *event, QTapAndHoldGesture* gesture);
    virtual void orientationChangeEvent(MOrientationChangeEvent *event);

    virtual void applyStyle();

    void ensureDocumentIsReady();
    int cursorPositionOfLastVisibleCharacter();
    void updateRichTextEliding();
    void updateHighlighting();
    QString wrapTextWithSpanTag(const QString &text) const;

    /**
     * Assures that the tiles are initialized for the given size.
     * If enough pixmap cache is available the member 'tiles'
     * represents the list of all tiles.
     */
    void initTiles(const QSize &size);

    /**
     * Creates \a count tiles with a size of \a size
     * and applies them to the member 'tiles'.
     */
    void createTiles(int count, const QSize &size);

    /**
     * Releases the cache for all tiles. The member 'tiles' will be set empty.
     */
    void cleanupTiles();

    /**
     * \param painter Painter where the tiles are drawn into.
     * \param pos     Position of the tile relative to the painter.
     * \param size    Size of the MLabelView widget.
     */
    void drawTiles(QPainter *painter, const QPointF &pos, const QSizeF &size);

    /**
     * If the scene-position has been changed the vertical position of the
     * tiles (stored in the member 'tiles') probably must be adjusted if the
     * tile got completely invisible.
     */
    void updateTilesPosition();

    /**
     * Updates the pixmap of the tile with the content of the textdocument
     * from the position indicated by the tile.
     * \return False, if the updating has been failed because of a limited cache.
     */
    bool updateTilePixmap(const Tile &tile);

    /**
     * \return True, if all tiles indicated by the member 'tiles' are cached
     *         inside QPixmapCache.
     */
    bool isTilesCacheValid() const;

    Tile* topTile();
    Tile* bottomTile();

    QRectF textBoundaries() const;

    mutable QTextDocument textDocument;
    bool textDocumentDirty;
    QPoint pixmapOffset;
    int mouseDownCursorPos;

    int tileHeight;
    QString tileCacheKey;
    M::Orientation tileOrientation;
    QList<Tile> tiles;
};

class MLabelViewPrivate : public MWidgetViewPrivate
{
    Q_DECLARE_PUBLIC(MLabelView)

public:
    MLabelViewPrivate();
    virtual ~MLabelViewPrivate();

    const MLabelModel *model() const;
    const MLabelStyle *style() const;
    const QRectF boundingRect() const;

    //Should label be rendered as rich text
    bool displayAsRichText(QString text, Qt::TextFormat textFormat, int numberOfHighlighters) const;
    
    // need define this for there are overload functions in controller
    MLabel *controller;

    MLabelViewSimple *impl;
    QTextOption textOptions;
};

#endif