File: Cloud.cpp

package info (click to toggle)
fotowall 0.9-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,280 kB
  • sloc: cpp: 23,275; makefile: 51; sh: 25
file content (339 lines) | stat: -rw-r--r-- 9,899 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
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
/***************************************************************************
 *                                                                         *
 *   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 "Cloud.h"

#include "WordItem.h"

#include <QCoreApplication>
#include <QGraphicsScene>
#include <QProgressDialog>
#include "math.h"

using namespace Wordcloud;

Appearance::Appearance()
  : textGradient(false)
  , minFontSize(8)
  , maxFontSize(72)
  , quadraticFont(false)
{
}

Cloud::Cloud(QObject * parent)
  : QObject(parent)
  , m_scene(0)
  , m_dirty(false)
  , m_placement(MostlyHorizontal)
  , m_accurate(false)
  , m_busyMode(ShowProgress)
{
    // default appearance
    m_appearance.textColors << Qt::lightGray << Qt::darkGray << Qt::darkRed << Qt::black /*### Qt::white */;
    m_appearance.backColor = Qt::transparent;
    m_appearance.font.setFamily("Teen"); // Berylium
}

void Cloud::newCloud(const WordList & words)
{
    // gether stats info over the words
    qDeleteAll(m_wordItems);
    m_wordItems.clear();
    int minCount = 0;
    int maxCount = 0;
    foreach (const Word & word, words) {
        if (word.count < minCount || !minCount)
            minCount = word.count;
        if (word.count > maxCount || !maxCount)
            maxCount = word.count;
    }

    // create items
    foreach (const Word & word, words) {
#if 1
        double rot = ((qrand() % 4) == 1) ? 90 : 0.0;
#else
        double rot = qrand() % 360;
#endif
        WordItem * wordItem = new WordItem(word, m_appearance.font, rot, minCount, maxCount);
        wordItem->show();

        m_wordItems.append(wordItem);
        if (m_scene)
            m_scene->addItem(wordItem);
    }

    // GO!
    m_dirty = true;
    process();
}

void Cloud::regenCloud()
{
    process();
}

void Cloud::randomCloud()
{
    // randomize placement
    m_placement = (Placement)(qrand() % Placement_COUNT);

    // randomize appearance
    m_appearance.quadraticFont = !(qrand() & 0x1);
    m_appearance.textGradient = !(qrand() & 0x1);
    // TODO: randomize more

    process();
}

void Cloud::setScene(QGraphicsScene * scene)
{
    if (scene != m_scene) {
        // set the scene
        removeFromScene();
        m_scene = scene;

        // add existing items to the new scene
        if (m_scene && !m_wordItems.isEmpty())
            foreach (WordItem * word, m_wordItems)
                m_scene->addItem(word);
    }
}

QGraphicsScene * Cloud::scene() const
{
    return m_scene;
}

void Cloud::removeFromScene()
{
    if (!m_scene || m_wordItems.isEmpty())
        return;
    foreach (WordItem * word, m_wordItems)
        m_scene->removeItem(word);
}


void Cloud::setPlacement(Wordcloud::Placement placement)
{
    if (m_placement != placement) {
        m_placement = placement;
        m_dirty = true;
    }
}

Wordcloud::Placement Cloud::placement() const
{
    return m_placement;
}

void Cloud::setAccurate(bool accurate)
{
    if (m_accurate != accurate) {
        m_accurate = accurate;
        m_dirty = true;
    }
}

bool Cloud::accurate() const
{
    return m_accurate;
}

void Cloud::setAppearance(const Wordcloud::Appearance & appearance)
{
    m_appearance = appearance;
    m_dirty = true;
}

Wordcloud::Appearance Cloud::appearance() const
{
    return m_appearance;
}

void Cloud::setBusyMode(Wordcloud::BusyMode mode)
{
    m_busyMode = mode;
}

Wordcloud::BusyMode Cloud::busyMode() const
{
    return m_busyMode;
}

bool Cloud::loadFromXml(QDomElement & cloudElement)
{
    // create all words
    for (QDomElement we = cloudElement.firstChildElement("word"); !we.isNull(); we = we.nextSiblingElement("word")) {
        WordItem * wordItem = new WordItem(we);
        wordItem->show();
        m_wordItems.append(wordItem);
        if (m_scene)
            m_scene->addItem(wordItem);
    }
    return true;
}

void Cloud::saveToXml(QDomElement & cloudElement) const
{
    // dump all Words
    QDomDocument doc = cloudElement.ownerDocument();
    foreach (WordItem * word, m_wordItems) {
        // create the Word element
        QDomElement wordElement = doc.createElement("word");
        cloudElement.appendChild(wordElement);
        word->saveToXml(wordElement);
    }
}

void Cloud::process()
{
    // reapply all the properties to the items here
    if (m_dirty) {
        foreach (WordItem * item, m_wordItems) {

            // randomize the color
            QColor color = m_appearance.textColors.isEmpty() ? Qt::black : m_appearance.textColors.at(qrand() % m_appearance.textColors.size());
            if (m_appearance.textGradient) {
                QLinearGradient lg(0, 0, 0, 1);
                lg.setCoordinateMode(QGradient::StretchToDeviceMode);
                lg.setColorAt(0.0, color.light());
                lg.setColorAt(1.0, color.dark());
                item->setBrush(lg);
            } else
                item->setBrush(color);

        }
    }

    // common constants
    double tBoundingArea = 0.0;
    foreach (WordItem * word, m_wordItems)
        tBoundingArea += word->boundingRect().width() * word->boundingRect().height();
    const double tRatio = 3.0 / 4.0;
    const int tExtWidth = (int)sqrt(tBoundingArea / tRatio);
    const int tExtHeight = (int)((double)tExtWidth * tRatio);

    // random placement
    if (m_placement == RandomPosition) {
        foreach (WordItem * word, m_wordItems)
            word->setPos((qrand() % tExtWidth) - tExtWidth/2, (qrand() % tExtHeight) - tExtHeight/2);
        return;
    }

    // show progress dialog if requested
    QProgressDialog * progress = 0;
    if (m_busyMode == ShowProgress) {
        progress = new QProgressDialog(tr("Placing Words..."), tr("Cancel"), 0, m_wordItems.size(), 0);
        progress->setWindowModality(Qt::WindowModal);
        progress->setValue(0);
        progress->show();
        qApp->processEvents();
    }

    QList<WordItem *> placed;
    foreach (WordItem * word, m_wordItems) {

        // TODO: handle this better
        if (progress && progress->wasCanceled()) {
            delete progress;
            break;
        }

        // randomize the first word inside the area
        if (placed.isEmpty()) {
            word->setPos((qrand() % tExtWidth) - tExtWidth/2, (qrand() % tExtHeight) - tExtHeight/2);
            word->tmpPlacedRect = word->boundingRect().translated(word->pos().x(), word->pos().y());
            placed.append(word);
            continue;
        }

        // place the word outside of other's boudaries
        int pDir = 0, pSide = 1;
        int pX = 0, pY = 1, pCount = 0;
        while (true) {
            switch(pDir) {
            case 0: pY++; break;
            case 1: pX--; break;
            case 2: pY--; break;
            case 3: pX++; break;
            }
            if (++pCount == pSide) {
                if (++pDir == 4)
                    pDir = 0;
                if (pDir == 0 || pDir == 2)
                    ++pSide;
                pCount = 0;
            }

            // find out the list of 'maybe intersecting items'
            int posX = pX * 10;
            int posY = pY * 10;
            const QRectF wordRealRect = word->boundingRect().translated(posX, posY);
            QPainterPath wordRealPath = word->path();
#if QT_VERSION >= 0x040600
            wordRealPath.translate(posX, posY);
#elif QT_VERSION >= 0x040500
            wordRealPath = QTransform::fromTranslate(posX, posY).map(wordRealPath);
#else
            wordRealPath = QTransform().translate(posX, posY).map(wordRealPath);
#endif

            bool intersections = false;
            foreach (WordItem * clItem, placed) {
                // check for boundary intersection
                if (!clItem->tmpPlacedRect.intersects(wordRealRect))
                    continue;

                // check for precise intersection
                if (m_accurate) {
                    QPainterPath placedPath = clItem->path();
#if QT_VERSION >= 0x040600
                    placedPath.translate(clItem->pos());
#elif QT_VERSION >= 0x040500
                    placedPath = QTransform::fromTranslate(clItem->pos().x(), clItem->pos().y()).map(placedPath);
#else
                    placedPath = QTransform().translate(clItem->pos().x(), clItem->pos().y()).map(placedPath);
#endif
                    if (!wordRealPath.intersects(placedPath))
                        continue;
                }

                // there is the intersection
                intersections = true;
                break;
            }

            // place the word
            if (!intersections) {
                word->setPos(posX, posY);
                word->tmpPlacedRect = wordRealRect;
                placed.append(word);
                break;
            }
        }

        if (progress) {
            int value = (100 * placed.size()) / m_wordItems.size();
            value = (value * value) / 100;
            progress->setValue(value);
        }
        if (m_busyMode == ShowPlacement)
            qApp->processEvents();
    }

    // remove the progress dialog (if any)
    delete progress;
    m_dirty = false;
}