Added "Show Solution"
[vexed] / fieldview.h
1 #ifndef FIELDVIEW_H
2 #define FIELDVIEW_H
3
4 #include <QtCore>
5 #include <QtGui>
6
7 #include "playfield.h"
8 #include "iconset.h"
9
10 namespace FV
11 {
12     const int FIELD_WIDTH=45; //GUI size
13     const int FIELD_HEIGHT=45;
14 }
15
16 class Animation
17 {
18 public:
19   QPropertyAnimation *a;
20   QRect src;
21   QString name;
22
23   Animation(QRect _src, QString _name):src(_src),name(_name)
24   {
25   }
26   QPropertyAnimation* getAnimation(QWidget *i)
27   {
28           a->setTargetObject(i);
29           return a;
30   }
31 };
32
33 class Move : public Animation
34 {
35 public:
36         Move(QRect _src, QRect _dst):Animation(_src,"Move")
37         {
38                 a=new QPropertyAnimation();
39                 a->setPropertyName("geometry");
40                 a->setDuration(100);
41                 a->setStartValue(_src);
42                 a->setEndValue(_dst);
43         }
44 };
45 class Hide : public Animation
46 {
47 public:
48         Hide(QRect _src): Animation (_src,"Hide")
49         {
50                 a=new QPropertyAnimation();
51                 QRect dst(src);
52                 dst.setWidth(0);
53                 dst.setHeight(0);
54                 a->setPropertyName("geometry");
55                 a->setDuration(100);
56                 a->setStartValue(src);
57                 a->setEndValue(dst);
58         }
59 };
60 class FieldView : public QWidget
61 {
62         Q_OBJECT
63 public:
64         explicit FieldView(QWidget *parent = 0);
65         PlayField* setPlayField(PlayField *pf);
66
67         void showSolution();
68         void stop();
69
70 private:
71         PlayField *playField;
72         int selX, selY;
73         int moveX, moveY;
74         bool moving;
75         QList<Animation*> *moves;
76         QTimer *solutionTimer;
77
78         void playMoves();
79         void move(int x, int y, int dest_x);
80
81         int solutionMove;
82         bool inSolution;
83 signals:
84         void solved(int moves);
85         void updateMoves(int moves);
86         void animationEnd();
87
88 public slots:
89         void cellMoved(int w, int h, int wnew, int hnew);
90         void cellGone(int w, int h);
91         void playMove();
92         void undo();
93         void playSolutionMove();
94
95
96 protected:
97         IconSet iconSet;
98
99         void mousePressEvent(QMouseEvent *event);
100         void mouseReleaseEvent(QMouseEvent *event);
101         virtual void updateWidgets();
102
103         //void paintCell(int x, int y, int cell, QPainter &painter);
104         QWidget *cellWidget(int w, int h);
105         QRect coo(int x, int y, int off=1)
106         {
107                 return QRect(x*FV::FIELD_WIDTH+off,y*FV::FIELD_HEIGHT+off,FV::FIELD_WIDTH-off,FV::FIELD_HEIGHT-off);
108         }
109         QPoint cell(int worldX, int worldY)
110         {
111                 return QPoint(width()/worldX, height()/worldY);
112         }
113         QPoint cell(const QPoint &pos)
114         {
115                 return QPoint(pos.x()/FV::FIELD_WIDTH, pos.y()/FV::FIELD_HEIGHT);
116         }
117
118 };
119
120 #endif // FIELDVIEW_H