1/*******************************************************************
2 *
3 * Copyright 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com>
4 * Copyright 2010 Brian Croom <brian.s.croom@gmail.com>
5 *
6 * This file is part of the KDE project "KAtomic"
7 *
8 * KAtomic is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * KAtomic is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with KAtomic; see the file COPYING. If not, write to
20 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 ********************************************************************/
24#ifndef FIELD_ITEM_H
25#define FIELD_ITEM_H
26
27#include <KGameRenderedItem>
28#include <QGraphicsTextItem>
29
30#include "playfield.h" // for enum PlayField::Direction
31
32class KGameRenderer;
33class atom;
34
35/**
36 * Represents item that can be placed in the PlayField.
37 * Basically it just extends QGraphicsPixmapItem by understanding
38 * field's cellbased coords.
39 */
40class FieldItem : public KGameRenderedItem
41{
42public:
43 explicit FieldItem( KGameRenderer* renderer, const QString& spriteKey, QGraphicsScene* scene );
44
45 void setFieldX(int x) { m_fieldX = x; }
46 void setFieldY(int y) { m_fieldY = y; }
47 void setFieldXY(int x, int y) { m_fieldX = x; m_fieldY = y; }
48
49 int fieldX() const { return m_fieldX; }
50 int fieldY() const { return m_fieldY; }
51
52 // enable use of qgraphicsitem_cast
53 enum { Type = UserType + 1 };
54 virtual int type() const { return Type; }
55private:
56 int m_fieldX;
57 int m_fieldY;
58};
59
60/**
61 * FieldItem that knows what atom number it holds
62 * @see Molecule
63 */
64class AtomFieldItem : public FieldItem
65{
66public:
67 explicit AtomFieldItem( KGameRenderer* renderer, const atom& at, QGraphicsScene* scene );
68
69 void setAtomNum(int n) { m_atomNum = n; }
70 int atomNum() const { return m_atomNum; }
71
72 /**
73 * Override so that the bonds (child objects) have their render size
74 * adjusted too
75 */
76 void setRenderSize(const QSize& renderSize);
77
78 /**
79 * Statically render the atom, for MoleculePreviewItem
80 */
81 static QPixmap renderAtom( KGameRenderer* renderer, const atom& at, int size);
82
83 // enable use of qgraphicsitem_cast
84 enum { Type = UserType + 2 };
85 virtual int type() const { return Type; }
86private:
87 // from molecule
88 int m_atomNum;
89
90 KGameRenderedItem* m_bond;
91
92 static QHash<char, QString> s_names; // cryptic_char -> elemName
93 static QHash<char, QString> s_bondNames; // cryptic_char -> bondName
94
95 /**
96 * Creates hashes for translating atom and bond signatures found in
97 * level files to corresponding SVG-element names
98 */
99 static void fillNameHashes();
100};
101
102class QTimeLine;
103/**
104 * FieldItem that represents clickable arrow.
105 * While showing plays nice fade-in effect
106 */
107class ArrowFieldItem : public QObject, public FieldItem
108{
109 Q_OBJECT
110public:
111 explicit ArrowFieldItem( KGameRenderer* renderer, PlayField::Direction dir, QGraphicsScene* scene );
112 virtual ~ArrowFieldItem();
113
114 // enable use of qgraphicsitem_cast
115 enum { Type = UserType + 3 };
116 virtual int type() const { return Type; }
117private slots:
118 void setOpacity( qreal opacity );
119private:
120 QVariant itemChange( GraphicsItemChange change, const QVariant& value );
121
122 /**
123 * Timeline object to control fade-in animation
124 */
125 QTimeLine *m_timeLine;
126};
127
128class Molecule;
129class PlayField;
130
131/**
132 * QGraphicsItem which displays molecule preview.
133 */
134class MoleculePreviewItem : public QGraphicsItem
135{
136public:
137 explicit MoleculePreviewItem( PlayField* scene );
138 ~MoleculePreviewItem();
139
140 /**
141 * Sets molecule to display
142 */
143 void setMolecule( const Molecule* mol );
144
145 /**
146 * Sets item width. Height will be calculated autmatically
147 */
148 void setWidth( int width );
149 /**
150 * Sets maximum atom size in rendered molecule preview.
151 * Usually atom size is calculated so the whole molecule can fit
152 * in the item.
153 * In some cases - when item width is big and the molecule is small this
154 * can lead to preview having a huge molecule with atom size larger than
155 * in playfield :). That looks not very good, hence this function.
156 */
157 void setMaxAtomSize( int maxSize );
158
159 inline QRectF boundingRect() const { return QRectF(0,0, m_width, m_width); } // reimp
160private:
161 void paint( QPainter * painter, const QStyleOptionGraphicsItem*, QWidget * widget = 0 );
162
163 KGameRenderer* m_renderer;
164 int m_width;
165 int m_atomSize;
166 int m_maxAtomSize;
167 const Molecule* m_mol;
168};
169
170#endif
171