File indexing completed on 2024-04-14 03:59:17
0001 /* 0002 This file is part of the KDE project "KAtomic" 0003 0004 SPDX-FileCopyrightText: 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com> 0005 SPDX-FileCopyrightText: 2010 Brian Croom <brian.s.croom@gmail.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #ifndef FIELD_ITEM_H 0011 #define FIELD_ITEM_H 0012 0013 #include "playfield.h" // for enum PlayField::Direction 0014 0015 #include <KGameRenderedItem> 0016 0017 #include <QGraphicsTextItem> 0018 0019 class KGameRenderer; 0020 class atom; 0021 0022 /** 0023 * Represents item that can be placed in the PlayField. 0024 * Basically it just extends QGraphicsPixmapItem by understanding 0025 * field's cellbased coords. 0026 */ 0027 class FieldItem : public KGameRenderedItem 0028 { 0029 public: 0030 explicit FieldItem( KGameRenderer* renderer, const QString& spriteKey, QGraphicsScene* scene ); 0031 0032 void setFieldX(int x) { m_fieldX = x; } 0033 void setFieldY(int y) { m_fieldY = y; } 0034 void setFieldXY(int x, int y) { m_fieldX = x; m_fieldY = y; } 0035 0036 int fieldX() const { return m_fieldX; } 0037 int fieldY() const { return m_fieldY; } 0038 0039 // enable use of qgraphicsitem_cast 0040 enum { Type = UserType + 1 }; 0041 int type() const override { return Type; } 0042 private: 0043 int m_fieldX; 0044 int m_fieldY; 0045 }; 0046 0047 /** 0048 * FieldItem that knows what atom number it holds 0049 * @see Molecule 0050 */ 0051 class AtomFieldItem : public FieldItem 0052 { 0053 public: 0054 explicit AtomFieldItem(KGameRenderer* renderer, atom at, QGraphicsScene* scene ); 0055 0056 void setAtomNum(int n) { m_atomNum = n; } 0057 int atomNum() const { return m_atomNum; } 0058 0059 /** 0060 * Override so that the bonds (child objects) have their render size 0061 * adjusted too 0062 */ 0063 void setRenderSize(const QSize& renderSize); 0064 0065 /** 0066 * Statically render the atom, for MoleculePreviewItem 0067 */ 0068 static QPixmap renderAtom(KGameRenderer* renderer, atom at, int size); 0069 0070 // enable use of qgraphicsitem_cast 0071 enum { Type = UserType + 2 }; 0072 int type() const override { return Type; } 0073 private: 0074 // from molecule 0075 int m_atomNum; 0076 0077 static QHash<char, QString> s_names; // cryptic_char -> elemName 0078 static QHash<char, QString> s_bondNames; // cryptic_char -> bondName 0079 0080 /** 0081 * Creates hashes for translating atom and bond signatures found in 0082 * level files to corresponding SVG-element names 0083 */ 0084 static void fillNameHashes(); 0085 }; 0086 0087 class QTimeLine; 0088 /** 0089 * FieldItem that represents clickable arrow. 0090 * While showing plays nice fade-in effect 0091 */ 0092 class ArrowFieldItem : public QObject, public FieldItem 0093 { 0094 Q_OBJECT 0095 public: 0096 explicit ArrowFieldItem( KGameRenderer* renderer, PlayField::Direction dir, QGraphicsScene* scene ); 0097 ~ArrowFieldItem() override; 0098 0099 // enable use of qgraphicsitem_cast 0100 enum { Type = UserType + 3 }; 0101 int type() const override { return Type; } 0102 private Q_SLOTS: 0103 void setOpacity( qreal opacity ); 0104 private: 0105 QVariant itemChange( GraphicsItemChange change, const QVariant& value ) override; 0106 0107 /** 0108 * Timeline object to control fade-in animation 0109 */ 0110 QTimeLine *m_timeLine; 0111 }; 0112 0113 class Molecule; 0114 class PlayField; 0115 0116 /** 0117 * QGraphicsItem which displays molecule preview. 0118 */ 0119 class MoleculePreviewItem : public QGraphicsItem 0120 { 0121 public: 0122 explicit MoleculePreviewItem( PlayField* scene ); 0123 ~MoleculePreviewItem() override; 0124 0125 /** 0126 * Sets molecule to display 0127 */ 0128 void setMolecule( const Molecule* mol ); 0129 0130 /** 0131 * Sets item width. Height will be calculated automatically 0132 */ 0133 void setWidth( int width ); 0134 /** 0135 * Sets maximum atom size in rendered molecule preview. 0136 * Usually atom size is calculated so the whole molecule can fit 0137 * in the item. 0138 * In some cases - when item width is big and the molecule is small this 0139 * can lead to preview having a huge molecule with atom size larger than 0140 * in playfield :). That looks not very good, hence this function. 0141 */ 0142 void setMaxAtomSize( int maxSize ); 0143 0144 inline QRectF boundingRect() const override { return QRectF(0,0, m_width, m_width); } // reimp 0145 private: 0146 void paint( QPainter * painter, const QStyleOptionGraphicsItem*, QWidget * widget = nullptr ) override; 0147 0148 KGameRenderer* m_renderer; 0149 int m_width; 0150 int m_atomSize; 0151 int m_maxAtomSize; 0152 const Molecule* m_mol; 0153 }; 0154 0155 #endif