You have to solve current level before advance
[vexed] / fieldview.cpp
1 #include <QtCore>
2 #include <QtGui>
3 #include <stdio.h>
4
5 #include "playfield.h"
6 #include "fieldview.h"
7
8 FieldView::FieldView(QWidget *parent) :
9         QWidget(parent)
10 {
11     selX=-1;
12     selY=-1;
13     moving=false;
14     moves=new QList<Animation*>();
15     playField=0;
16
17 }
18
19 void FieldView::mousePressEvent(QMouseEvent *event)
20 {
21     QPoint sel=cell(event->pos());
22
23     selX=sel.x();
24     selY=sel.y();
25     moveX=-1;
26     moveY=-1;
27     moving=true;
28     update();
29 }
30
31 void FieldView::mouseReleaseEvent(QMouseEvent *event)
32 {
33     if(moving)
34     {
35         moving=false;
36         QPoint mov=cell(event->pos());
37         moveX=mov.x();
38         moveY=mov.y();
39         if(moveX==selX && moveY==selY)
40         {
41             moveX=-1;
42             return;
43         }
44     }
45     delete moves;
46     moves=new QList<Animation*>();
47     playField->move(selX, selY, moveX);
48     playMoves();
49     emit updateMoves(playField->moves);
50 }
51
52 PlayField* FieldView::setPlayField(PlayField *pf)
53 {
54         if(playField)
55         {
56                 delete playField;
57         }
58         playField = new PlayField(pf);
59         QObject::connect(playField,SIGNAL(cellMoved(int,int,int,int)),this,SLOT(cellMoved(int,int,int,int)));
60         QObject::connect(playField,SIGNAL(cellGone(int,int)),this,SLOT(cellGone(int,int)));
61
62         updateWidgets();
63         show();
64         emit updateMoves(playField->moves);
65         return playField;
66 }
67
68 void FieldView::cellMoved(int w, int h, int wnew, int hnew)
69 {
70         QRect c=coo(w,h);
71         QRect cnew=coo(wnew,hnew);
72         moves->append(new Move(c,cnew));
73 }
74 void FieldView::cellGone(int w, int h)
75 {
76         QRect c=coo(w,h);
77         moves->append(new Hide(c));
78 }
79 QWidget* FieldView::cellWidget(int w, int h)
80 {
81         return childAt(coo(w,h).topLeft());
82 }
83 void FieldView::playMove()
84 {
85         if(moves->size()>0)
86         {
87                 Animation *move=moves->at(0);
88                 QWidget *item=childAt(move->src.topLeft());
89                 if(!item)
90                 {
91                         QPoint p=cell(move->src.left(),move->src.top());
92                 }
93                 QPropertyAnimation *a=move->getAnimation(item);
94                 moves->removeAt(0);
95                 connect(a,SIGNAL(finished()),this,SLOT(playMove()));
96                 a->start();
97                 delete move;
98         } else if(playField->checkSolved())
99         {
100             emit solved(playField->moves);
101         }
102 }
103
104 void FieldView::playMoves()
105 {
106     playMove();
107 }
108
109 void FieldView::undo()
110 {
111     playField->undo();
112     updateWidgets();
113     emit updateMoves(playField->moves);
114 }
115
116 void FieldView::updateWidgets()
117 {
118     QList<QWidget *> childs=findChildren<QWidget *>();
119     QListIterator<QWidget *> childsI(childs);
120     while(childsI.hasNext())
121     {
122         delete childsI.next();
123     }
124
125     for(int w=0;w<PF::FIELD_WIDTH;w++)
126         for(int h=0;h<PF::FIELD_HEIGHT;h++)
127         {
128         QRect r=coo(w,h,0);
129         int cell=playField->get(w,h);
130         QLabel *cellW;
131         switch(cell)
132         {
133                 case PF::CELL_EMPTY:
134             break;
135                 case PF::CELL_WALL:
136             cellW=new QLabel(this);
137             cellW->setPixmap(iconSet.wallIcon());
138             cellW->setGeometry(r);
139             cellW->setFrameStyle(QFrame::NoFrame);
140             cellW->show();
141             break;
142                 default:
143             cellW=new QLabel(this);
144             cellW->setPixmap(iconSet.icon(cell-2));
145             cellW->setGeometry(r);
146             cellW->setFrameStyle(QFrame::NoFrame);
147             cellW->show();
148         }
149     }
150 }