Fixed showSolution
[vexed] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "preferences.h"
4 #include "howtoplay.h"
5 #include "needadvance.h"
6
7 MainWindow::MainWindow(QWidget *parent) :
8     QMainWindow(parent),
9     levelNo(0),
10     ui(new Ui::MainWindow)
11
12 {
13         ui->setupUi(this);
14 #ifdef Q_WS_MAEMO_5
15         setAttribute(Qt::WA_Maemo5AutoOrientation);
16         QDesktopWidget* q=QApplication::desktop();
17         connect(q,SIGNAL(resized(int)),this,SLOT(reorient()));
18 #endif
19         QDir packDir(":/packs");
20         QStringList packList=packDir.entryList();
21
22         QListIterator<QString> packNamesI(packList);
23         while(packNamesI.hasNext())
24         {
25                 QString packName=packNamesI.next().section(".",0,0);
26                 packNames+=packName;
27         }
28
29         pack=0;
30         loadPack(Settings().savedPackName(),Settings().savedLevelNo());
31 }
32
33 MainWindow::~MainWindow()
34 {
35     delete ui;
36 }
37
38 void MainWindow::changeEvent(QEvent *e)
39 {
40     QMainWindow::changeEvent(e);
41     switch (e->type()) {
42     case QEvent::LanguageChange:
43         ui->retranslateUi(this);
44         break;
45     default:
46         break;
47     }
48 }
49
50 void MainWindow::reorient()
51 {
52
53     ui->buttonsPortrait->removeWidget(ui->prevLevel);
54     ui->buttonsPortrait->removeWidget(ui->nextLevel);
55     ui->buttonsPortrait->removeWidget(ui->reload);
56     ui->buttonsPortrait->removeWidget(ui->undo);
57     ui->buttonsPortrait->removeWidget(ui->solve);
58
59     ui->undoLandscape->removeWidget(ui->undo);
60     ui->solveLandscape->removeWidget(ui->solve);
61     ui->buttonsLandscape->removeWidget(ui->prevLevel);
62     ui->buttonsLandscape->removeWidget(ui->nextLevel);
63     ui->buttonsLandscape->removeWidget(ui->reload);
64
65
66     QDesktopWidget* q=QApplication::desktop();
67     if(q->height()>q->width())
68     {
69         // Portrait mode
70
71         ui->buttonsPortrait->addWidget(ui->prevLevel);
72         ui->buttonsPortrait->addWidget(ui->reload);
73         ui->buttonsPortrait->addWidget(ui->nextLevel);
74         ui->buttonsPortrait->addWidget(ui->undo);
75         ui->buttonsPortrait->addWidget(ui->solve);
76         //ui->buttonsPortrait->layout();
77     } else
78     {
79         ui->buttonsLandscape->insertWidget(0,ui->nextLevel,1,Qt::AlignLeft);
80         ui->buttonsLandscape->insertWidget(0,ui->reload,1,Qt::AlignLeft);
81         ui->buttonsLandscape->insertWidget(0,ui->prevLevel,1,Qt::AlignLeft);
82         ui->undoLandscape->addWidget(ui->undo);
83         ui->solveLandscape->addWidget(ui->solve);
84         //ui->buttonsLandscape->layout();
85     }
86 }
87
88 void MainWindow::loadLevel()
89 {
90     PlayField *pf=pack->levelAt(levelNo);
91     ui->playwidget->setPlayField(pf);
92     QString title;
93     QTextStream(&title)<<pf->title<<" ("<<levelNo+1<<" of "<<pack->levels.size()<<")";
94     levelHiscore=Settings().getHighscore(pack->getTitle(),pf->title);
95     ui->levelTitle->setText(title);
96     movesChanged(0);
97 }
98
99 void MainWindow::loadPack(const QString& packName, int levelToLoad)
100 {
101         if(pack) delete pack;
102         QString fileName(":/packs/");
103         fileName.append(packName).append(".ini");
104         pack=new LevelPack(fileName);
105         levelNo=levelToLoad;
106         loadLevel();
107 }
108
109 void MainWindow::movesChanged(int moves)
110 {
111     PlayField *pf=pack->levelAt(levelNo);
112     const QString sol=pf->getSolution();
113     int total=sol.length()/2;
114     //int score=Settings().getHighscore(pack->getTitle(),pf->title);
115     QString q;
116     QTextStream movesBanner(&q);
117     movesBanner<<moves<<"/"<<total;
118     if(levelHiscore!=HS::NO_SCORE)
119         movesBanner<<"(<b>"<<levelHiscore<<"</b>)";
120     ui->moves->setText(q);
121 }
122
123 void MainWindow::solved(int moves)
124 {
125     if(inSolution)
126     {
127         inSolution=false;
128         return;
129     }
130     PlayField *pf=pack->levelAt(levelNo);
131     Settings().saveHiscore(pack->getTitle(),pf->title, moves);
132     levelHiscore=moves;
133     loadNextLevel();
134 }
135
136 void MainWindow::openPreferences()
137 {
138     Preferences pref(packNames,pack->getTitle(),this);
139     if(pref.exec()==QDialog::Accepted)
140     {
141         loadPack(pref.getSelectedPack(),0);
142     }
143 }
144
145 void MainWindow::howToPlay()
146 {
147     HowToPlay h(this);
148     h.exec();
149 }
150 // save current pack and level to config
151 // they are used to restore on start
152 // TODO: save playfield (and undo?)
153 void MainWindow::saveState()
154 {
155     Settings().saveGame(pack->getTitle(),levelNo);
156 }
157
158 void MainWindow::loadNextLevel()
159 {
160     if(levelHiscore == HS::NO_SCORE)
161     {
162         needadvance().exec();
163         return;
164     }
165     if(levelNo<(pack->size()-1))
166     {
167         levelNo++;
168         loadLevel();
169     }
170 }
171
172 void MainWindow::solve()
173 {
174     // reset current level
175     loadLevel();
176     // and show solution
177     QMessageBox confirm;
178     confirm.setText(tr("Reset the level and show the solution?"));
179     confirm.setInformativeText(tr("If you agree current level will be reloaded and solution will be shown"));
180     confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
181     int confirmation=confirm.exec();
182     if(confirmation==QMessageBox::Yes)
183     {
184         inSolution=true;
185         ui->playwidget->showSolution();
186     }
187 }