File: HomeScene.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 (292 lines) | stat: -rw-r--r-- 10,156 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
/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://www.enricoros.com/opensource/fotowall                      *
 *                                                                         *
 *   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 "HomeScene.h"

#include "Canvas/AbstractContent.h"
#include "Frames/StandardFrame.h"
#include "Shared/RenderOpts.h"
#include "3rdparty/pencil/PencilItem.h"
#include "App.h"
#include "Settings.h"

#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QPainter>
#include <QTimer>

#if QT_VERSION >= 0x040600
#include <QPropertyAnimation>
#define ANIMATE_PARAM(object, propName, duration, endValue) \
    {QPropertyAnimation * ani = new QPropertyAnimation(object, propName, object); \
    ani->setEasingCurve(QEasingCurve::OutBack); \
    ani->setDuration(duration); \
    ani->setEndValue(endValue); \
    ani->start(QPropertyAnimation::DeleteWhenStopped);}
#else
#define ANIMATE_PARAM(instance, propName, duration, endValue) \
    instance->setProperty(propName, endValue);
#endif

// uncomment following to draw a subtle shadow behind the labels
#define HOMELABEL_SHADOWED


/** Home Label **/

class HomeLabel : public AbstractContent
{
    public:
        HomeLabel(const QString & title, const QPixmap & pixmap, QGraphicsScene * scene)
          : AbstractContent(scene, 0, true)
          , m_pixmap(pixmap)
        {
            // create the standard frame and set title
            setFrame(new StandardFrame2);
            setFrameTextEnabled(true);
            setFrameTextReadonly(true);
            setFrameText(title);
            setMirrored(false);

            // incremental change over AbstractContent
            setFlag(QGraphicsItem::ItemIsMovable, false);
            setFlag(QGraphicsItem::ItemIsSelectable, false);
            setFlag(QGraphicsItem::ItemIsFocusable, true);
            setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
            const int pixW = pixmap.width();
            const int pixH = pixmap.height();
            resizeContents(QRect(-pixW/2, -pixH/2, pixW, pixH), false);
        }

        QString contentName() const
        {
            return "HomeLabel";
        }

#ifdef HOMELABEL_SHADOWED
        QRectF boundingRect() const
        {
            const QRectF origRect = AbstractContent::boundingRect();
            return origRect.adjusted(0, 0, 0, origRect.height() * 0.2);
        }
#endif

        void focusInEvent(QFocusEvent *event)
        {
            event->accept();
            hoverEnterEvent(0);
        }

        void focusOutEvent(QFocusEvent *event)
        {
            event->accept();
            hoverLeaveEvent(0);
        }

        void hoverEnterEvent(QGraphicsSceneHoverEvent *)
        {
            ANIMATE_PARAM(this, "scale", 300, 1.2);
            //ANIMATE_PARAM(this, "rotation", 300, (-20 + (qrand() % 41)));
        }

        void hoverLeaveEvent(QGraphicsSceneHoverEvent *)
        {
            ANIMATE_PARAM(this, "scale", 300, 1.0);
            //ANIMATE_PARAM(this, "rotation", 300, 0.0);
        }

        void mousePressEvent(QGraphicsSceneMouseEvent * event)
        {
            // use an already existing signal.. FIXME!
            if (event->button() == Qt::LeftButton) {
#if 0
                ANIMATE_PARAM(this, "rotation", 400, 300);
                ANIMATE_PARAM(this, "opacity", 500, 0.0);
                QTimer::singleShot(300, this, SIGNAL(requestEditing()));
#else
                requestEditing();
#endif
            }
        }

        void drawContent(QPainter * painter, const QRect & targetRect, Qt::AspectRatioMode /*ratio*/)
        {
            painter->setRenderHint(QPainter::SmoothPixmapTransform, property("scale").toDouble() == 1.2);
            painter->drawPixmap(targetRect, m_pixmap);
            painter->setRenderHint(QPainter::SmoothPixmapTransform, false);
        }

        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        {
            // draw a 'shadow-like' gradient
#ifdef HOMELABEL_SHADOWED
            QRadialGradient rg(0.5, 0.5, 0.5);
             rg.setCoordinateMode(QGradient::ObjectBoundingMode);
             int opacity = (int)(24.0 * (1.0 - 3 * (property("scale").toDouble() - 1.0)));
             rg.setColorAt(0.0, QColor(32, 32, 32, opacity));
             rg.setColorAt(1.0, Qt::transparent);
            painter->setBrush(rg);
            painter->setPen(Qt::NoPen);
            QRect shadowRect = boundingRect().toRect();
            shadowRect.setTop(boundingRect().bottom() - 2 * 8);
            painter->drawRect(shadowRect);
#endif

            // paint the AbstractContent
            AbstractContent::paint(painter, option, widget);
        }

    private:
        QPixmap m_pixmap;
};


HomeScene::HomeScene(QObject * parent)
  : AbstractScene(parent)
  , m_logoPixmap(":/data/home-logo.png")
#ifdef HAVE_PENCIL_ITEM
  , m_pencil(0)
#endif
{
    // create the 3 buttons
#ifndef NO_WORDCLOUD_APPLIANCE
    HomeLabel * newWordcloud = new HomeLabel(tr("Wordcloud"), QPixmap(":/data/home-newwordcloud.png"), this);
     connect(newWordcloud, SIGNAL(requestEditing()), this, SIGNAL(startWordcloud()));
#else
    HomeLabel * newWordcloud = new HomeLabel(tr("coming soon"), QPixmap(":/data/home-newwordcloud.png"), this);
     newWordcloud->setEnabled(false);
#if QT_VERSION >= 0x040500
     newWordcloud->setOpacity(0.2);
#endif
#endif
     newWordcloud->setZValue(0.0);
     m_labels.append(newWordcloud);

    HomeLabel * newCanvas = new HomeLabel(tr("Create"), QPixmap(":/data/home-newcanvas.png"), this);
     connect(newCanvas, SIGNAL(requestEditing()), this, SIGNAL(startCanvas()));
     newCanvas->setZValue(1.0);
     m_labels.append(newCanvas);

    HomeLabel * wizard = new HomeLabel(tr("coming soon"), QPixmap(":/data/home-wizard.png"), this);
     connect(wizard, SIGNAL(requestEditing()), this, SIGNAL(startWizard()));
     wizard->setEnabled(false);
#if QT_VERSION >= 0x040500
     wizard->setOpacity(0.2);
#endif
     wizard->setZValue(0.0);
     m_labels.append(wizard);

#ifdef HAVE_PENCIL_ITEM
     // create the pencil item the first time
     if (App::settings->firstTime() || !(qrand() % 20))
        QTimer::singleShot(1000, this, SLOT(slotCreatePencil()));
#endif
}

HomeScene::~HomeScene()
{
    qDeleteAll(m_labels);
}

void HomeScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    // draw a gray shade as background
    painter->setCompositionMode(QPainter::CompositionMode_Source);
    QLinearGradient lg(0, 0, 0, sceneHeight());
    lg.setColorAt(0.0, QColor(192, 192, 192, RenderOpts::ARGBWindow ? 200 : 255));
    lg.setColorAt(1.0, QColor(128, 128, 128, RenderOpts::ARGBWindow ? 200 : 255));
    painter->fillRect(rect, lg);
    painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
#if 0
    QRadialGradient rg(sceneWidth() / 2, 0, 200, sceneWidth() / 2, -100);
    rg.setColorAt(0.0, Qt::white);
    rg.setColorAt(1.0, Qt::transparent);
    painter->fillRect(rect, rg);
#endif
}

void HomeScene::drawForeground(QPainter *painter, const QRectF &rect)
{
    // draw top-center logo
    if (m_logoRect.isValid() && m_logoRect.intersects(rect.toRect()))
        painter->drawPixmap(m_logoRect.topLeft(), m_logoPixmap);
}

void HomeScene::keyPressEvent(QKeyEvent *event)
{
    AbstractScene::keyPressEvent(event);
    emit keyPressed(event->key());
}

void HomeScene::resize(const QSize & size)
{
    // resize but ensure a minimum size
    AbstractScene::resize(size.expandedTo(QSize(600, 200)));

    // grid placement
    const int margin = 20;
    const int count = m_labels.size();
    const int width = sceneWidth() - margin * 2;
    const int height = sceneHeight() - margin;
    const int rows = (int)sqrt((qreal)count);
    const int cols = (int)ceil((qreal)count / (qreal)rows);
    int rIdx = 0, cIdx = 0;
    for (int i = 0; i < count; i++) {
        double xPos = margin + (int)(((qreal)cIdx + 0.50) * (qreal)width / (qreal)cols);
        double yPos = margin + (int)(((qreal)rIdx + 0.55) * (qreal)height / (qreal)rows);
        if (cols == 3 && cIdx != 1)
            yPos += 4;
        m_labels[i]->setPos(QPointF(xPos, yPos) - m_labels[i]->boundingRect().center());
        if (++cIdx >= cols) {
            cIdx = 0;
            ++rIdx;
        }
    }

    // logo rectangle
    if (!m_logoPixmap.isNull()) {
        int top = 0;
        if (m_labels.size() < 2)
            top = (height - rows * 200) / 10;
        else
            top = qMax((qreal)0, (m_labels[1]->sceneBoundingRect().top() - m_logoPixmap.height()) / 2);
        m_logoRect = QRect((sceneWidth() - m_logoPixmap.width()) / 2, top, m_logoPixmap.width(), m_logoPixmap.height());
    }

    // pencil item
#ifdef HAVE_PENCIL_ITEM
    if (m_pencil) {
        QRectF pRect = m_pencil->boundingRect();
        m_pencil->setPos(QPointF((sceneWidth() - pRect.width()) / 2, 9 * (sceneHeight() - pRect.height()) / 10));
    }
#endif
}

bool HomeScene::sceneSelectable() const
{
    return false;
}

void HomeScene::slotCreatePencil()
{
#ifdef HAVE_PENCIL_ITEM
    if (!m_pencil) {
        m_pencil = new PencilItem(":/data/home-art.svg");
        QRectF pRect = m_pencil->boundingRect();
        m_pencil->setZValue(999);
        m_pencil->setPos(QPointF((sceneWidth() - pRect.width()) / 2, 9 * (sceneHeight() - pRect.height()) / 10));
        addItem(m_pencil);
    }
#endif
}