source: trunk/demos/qtdemo/demoitem.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 9.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the demonstration applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "demoitem.h"
43#include "menumanager.h"
44#include "guide.h"
45#include "colors.h"
46
47QHash<QString, SharedImage *> DemoItem::sharedImageHash;
48QMatrix DemoItem::matrix;
49
50DemoItem::DemoItem(QGraphicsScene *scene, QGraphicsItem *parent) : QGraphicsItem(parent, scene)
51{
52 this->opacity = 1.0;
53 this->locked = false;
54 this->prepared = false;
55 this->neverVisible = false;
56 this->noSubPixeling = false;
57 this->currentAnimation = 0;
58 this->currGuide = 0;
59 this->guideFrame = 0;
60 this->sharedImage = new SharedImage();
61 ++this->sharedImage->refCount;
62}
63
64DemoItem::~DemoItem()
65{
66 if(--this->sharedImage->refCount == 0){
67 if (!this->hashKey.isEmpty())
68 DemoItem::sharedImageHash.remove(this->hashKey);
69 delete this->sharedImage;
70 }
71}
72
73void DemoItem::setNeverVisible(bool never)
74{
75 Q_UNUSED(never);
76/*
77 this->neverVisible = never;
78 if (never){
79 this->setVisible(false);
80 QList<QGraphicsItem *> c = children();
81 for (int i=0; i<c.size(); i++){
82 DemoItem *d = dynamic_cast<DemoItem *>(c[i]); // Don't use dynamic cast because it needs RTTI support.
83 if (d)
84 d->setNeverVisible(true);
85 else{
86 c[i]->setVisible(false);
87 }
88 }
89 }
90*/
91}
92
93void DemoItem::setRecursiveVisible(bool visible){
94 if (visible && this->neverVisible){
95 this->setVisible(false);
96 return;
97 }
98
99 this->setVisible(visible);
100 QList<QGraphicsItem *> c = children();
101 for (int i=0; i<c.size(); i++){
102 // DemoItem *d = dynamic_cast<DemoItem *>(c[i]);
103 // if (d)
104 // d->setRecursiveVisible(visible);
105 // else{
106 c[i]->setVisible(visible);
107 // }
108 }
109}
110
111void DemoItem::useGuide(Guide *guide, float startFrame)
112{
113 this->startFrame = startFrame;
114 this->guideFrame = startFrame;
115 while (this->guideFrame > guide->startLength + guide->length()){
116 if (guide->nextGuide == guide->firstGuide)
117 break;
118
119 guide = guide->nextGuide;
120 }
121 this->currGuide = guide;
122}
123
124void DemoItem::guideAdvance(float distance)
125{
126 this->guideFrame += distance;
127 while (this->guideFrame > this->currGuide->startLength + this->currGuide->length()){
128 this->currGuide = this->currGuide->nextGuide;
129 if (this->currGuide == this->currGuide->firstGuide)
130 this->guideFrame -= this->currGuide->lengthAll();
131 }
132}
133
134void DemoItem::guideMove(float moveSpeed)
135{
136 this->currGuide->guide(this, moveSpeed);
137}
138
139void DemoItem::setPosUsingSheepDog(const QPointF &dest, const QRectF &sceneFence)
140{
141 this->setPos(dest);
142 if (sceneFence.isNull())
143 return;
144
145 // I agree. This is not the optimal way of doing it.
146 // But don't want for use time on it now....
147 float itemWidth = this->boundingRect().width();
148 float itemHeight = this->boundingRect().height();
149 float fenceRight = sceneFence.x() + sceneFence.width();
150 float fenceBottom = sceneFence.y() + sceneFence.height();
151
152 if (this->scenePos().x() < sceneFence.x()) this->moveBy(this->mapFromScene(QPointF(sceneFence.x(), 0)).x(), 0);
153 if (this->scenePos().x() > fenceRight - itemWidth) this->moveBy(this->mapFromScene(QPointF(fenceRight - itemWidth, 0)).x(), 0);
154 if (this->scenePos().y() < sceneFence.y()) this->moveBy(0, this->mapFromScene(QPointF(0, sceneFence.y())).y());
155 if (this->scenePos().y() > fenceBottom - itemHeight) this->moveBy(0, this->mapFromScene(QPointF(0, fenceBottom - itemHeight)).y());
156}
157
158void DemoItem::setGuidedPos(const QPointF &pos)
159{
160 this->guidedPos = pos;
161}
162
163QPointF DemoItem::getGuidedPos()
164{
165 return this->guidedPos;
166}
167
168void DemoItem::switchGuide(Guide *guide)
169{
170 this->currGuide = guide;
171 this->guideFrame = 0;
172}
173
174bool DemoItem::inTransition()
175{
176 if (this->currentAnimation)
177 return this->currentAnimation->running();
178 else
179 return false;
180}
181
182void DemoItem::setMatrix(const QMatrix &matrix)
183{
184 DemoItem::matrix = matrix;
185}
186
187void DemoItem::useSharedImage(const QString &hashKey)
188{
189 this->hashKey = hashKey;