File: WordItem.cpp

package info (click to toggle)
fotowall 0.9-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,832 kB
  • ctags: 2,804
  • sloc: cpp: 23,275; makefile: 47; sh: 25
file content (215 lines) | stat: -rw-r--r-- 7,462 bytes parent folder | download | duplicates (3)
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
/***************************************************************************
 *                                                                         *
 *   This file is part of the Wordcloud project,                           *
 *       http://www.enricoros.com/opensource/wordcloud                     *
 *                                                                         *
 *   Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "WordItem.h"

#include <QApplication>
#include <QPainterPathStroker>
#include <QPainter>
#include <QPen>
#include <QStyle>
#include <QStyleOptionFocusRect>
#include <QStyleOptionGraphicsItem>

using namespace Wordcloud;

WordItem::WordItem(const Word & word, const QFont & font, double rotation,
                   int minCount, int maxCount, QGraphicsItem * parent)
  : QAbstractGraphicsShapeItem(parent)
  , m_font(font)
  , m_rotation(rotation)
  , m_string(word.commonString)
{
    // customize item
    setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
#if QT_VERSION >= 0x040600
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
#endif
    setPen(Qt::NoPen);

    // adapt font to the relative count
    const double maxFontSize = 96;
    double gamma = (double)(word.count - minCount) / (double)(maxCount - minCount);
    int fontSize = 8 + (int)(maxFontSize * gamma);
    m_font.setPointSize(fontSize);

    regeneratePath();
}

WordItem::WordItem(QDomElement & wordElement, QGraphicsItem * parent)
    : QAbstractGraphicsShapeItem(parent)
    , m_rotation((qreal)0.0)
{
    // customize item
    setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
#if QT_VERSION >= 0x040600
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
#endif
    setPen(Qt::NoPen);

    // restore geometry
    QDomElement domElement;
    domElement = wordElement.firstChildElement("pos");
    qreal px = domElement.firstChildElement("x").text().toDouble();
    qreal py = domElement.firstChildElement("y").text().toDouble();
    setPos(px, py);

    // restore font
    domElement = wordElement.firstChildElement("font");
    QString family = domElement.firstChildElement("family").text();
    if (!family.isEmpty())
        m_font.setFamily(family);
    int pointSize = domElement.firstChildElement("pointSize").text().toInt();
    if (pointSize > 0)
        m_font.setPointSize(pointSize);

    // SIMPLE restore brush
    domElement = wordElement.firstChildElement("color");
    QColor col(domElement.text());
    setBrush(col.isValid() ? col : Qt::black);

    // rotation
    m_rotation = wordElement.firstChildElement("rotation").text().toDouble();

    // word
    m_string = wordElement.firstChildElement("string").text();

    // DO PATH!
    regeneratePath();
}

void WordItem::saveToXml(QDomElement & wordElement) const
{
    QDomDocument doc = wordElement.ownerDocument();
    QDomElement domElement;

    // save geometry
    domElement = doc.createElement("pos");
    QDomElement xElement = doc.createElement("x");
    QDomElement yElement = doc.createElement("y");
    xElement.appendChild(doc.createTextNode(QString::number(pos().x())));
    yElement.appendChild(doc.createTextNode(QString::number(pos().y())));
    domElement.appendChild(xElement);
    domElement.appendChild(yElement);
    wordElement.appendChild(domElement);

    // save font
    domElement = doc.createElement("font");
    QDomElement fElement = doc.createElement("family");
    QDomElement psElement = doc.createElement("pointSize");
    fElement.appendChild(doc.createTextNode(m_font.family()));
    psElement.appendChild(doc.createTextNode(QString::number(m_font.pointSize())));
    domElement.appendChild(fElement);
    domElement.appendChild(psElement);
    wordElement.appendChild(domElement);

    // SIMPLE save brush
    if (!brush().color().isValid())
        qWarning("WordItem::saveToXml: implement complex brush xml dump");
    domElement = doc.createElement("color");
    domElement.appendChild(doc.createTextNode(brush().color().name()));
    wordElement.appendChild(domElement);

    // rotation
    if (m_rotation != 0.0) {
        domElement = doc.createElement("rotation");
        domElement.appendChild(doc.createTextNode(QString::number(m_rotation)));
        wordElement.appendChild(domElement);
    }

    // word
    domElement = doc.createElement("string");
    domElement.appendChild(doc.createTextNode(m_string));
    wordElement.appendChild(domElement);
}

QVariant WordItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionHasChanged)
        emit moved();
    return QAbstractGraphicsShapeItem::itemChange(change, value);
}

void WordItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    // draw the word
    painter->save();
    painter->setPen(pen());
    painter->setBrush(brush());
    painter->drawPath(m_path);

    // draw the standard selection
    if (option->state & QStyle::State_Selected) {
        QStyleOptionFocusRect opt;
        opt.rect = boundingRect().toRect();
        opt.backgroundColor = Qt::white;
        QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, painter, 0);
    }
    painter->restore();
}

QPainterPath WordItem::strokedPath() const
{
    // use path as-is if no pen or null
    const QPen & pen = this->pen();
    if (m_path == QPainterPath() || pen.widthF() <= 0.0)
        return m_path;

    // return enlarged (stroked) path
    QPainterPathStroker ps;
    ps.setCapStyle(pen.capStyle());
    ps.setWidth(pen.widthF());
    ps.setJoinStyle(pen.joinStyle());
    ps.setMiterLimit(pen.miterLimit());
    QPainterPath fullPath = ps.createStroke(m_path);
    fullPath.addPath(m_path);
    return fullPath;
}

void WordItem::regeneratePath()
{
    // make the path from the text
    QPainterPath path;
    path.addText(0, 0, m_font, m_string);

    // rotate the text if needed
    if (m_rotation != 0.0) {
        QTransform pathForm;
        // perspective rotation
        //pathForm.rotate(-60 + (qrand() % 120), Qt::XAxis);
        //pathForm.rotate(-60 + (qrand() % 120), Qt::YAxis);
        // planar rotation
        pathForm.rotate(m_rotation, Qt::ZAxis);
        path = pathForm.map(path);
    }

    // center the path
    QPointF pathCenter = path.boundingRect().center();
#if QT_VERSION >= 0x040600
    path.translate(-pathCenter);
#elif QT_VERSION >= 0x040500
    path = QTransform::fromTranslate(-pathCenter.x(), -pathCenter.y()).map(path);
#else
    path = QTransform().translate(-pathCenter.x(), -pathCenter.y()).map(path);
#endif

    // update constants
    prepareGeometryChange();
    m_path = path;
    if (pen().widthF() <= 0.001)
        m_boudingRect = m_path.boundingRect();
    else
        m_boudingRect = strokedPath().boundingRect();
    update();
}