2 Image Puzzle - A set your pieces straight game
3 Copyright (C) 2009 Timo Härkönen
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "settingsdialog.h"
22 #include "trackerfiles.h"
24 #include <QRadioButton>
25 #include <QPushButton>
27 #include <QVBoxLayout>
28 #include <QHBoxLayout>
30 #include <QFileDialog>
36 SettingsDialog::SettingsDialog(QWidget *parent) :
41 easyButton_ = new QRadioButton(tr("Easy"));
42 easyButton_->setChecked(true);
43 hardButton_ = new QRadioButton(tr("Hard"));
45 buttonLayout_ = new QHBoxLayout;
46 buttonLayout_->addWidget(easyButton_);
47 buttonLayout_->addWidget(hardButton_);
49 buttonGroup_ = new QGroupBox(tr("Difficulty"));
50 buttonGroup_->setLayout(buttonLayout_);
53 items << DEFAULT_IMAGE_TXT << RANDOM_IMAGE_TXT << SELECT_IMAGE_TXT;
55 imageCombo_ = new QComboBox;
56 imageCombo_->addItems(items);
57 imageCombo_->setCurrentIndex(1);
59 selectedImageLabel_ = new QLabel(tr("n/a"));
61 applyButton_ = new QPushButton(tr("Play"));
63 mainLayout_ = new QVBoxLayout;
64 mainLayout_->addWidget(buttonGroup_);
65 mainLayout_->addWidget(imageCombo_);
66 mainLayout_->addWidget(selectedImageLabel_);
67 mainLayout_->addWidget(applyButton_);
69 selectedImageLabel_->setVisible(false);
71 trackerFiles_ = new TrackerFiles(this);
73 setLayout(mainLayout_);
75 connect(easyButton_, SIGNAL(toggled(bool)), this, SLOT(difficultySelectionChanged(bool)));
76 //connect(imageCombo_, SIGNAL(currentIndexChanged(QString)), this, SLOT(imageSelectionChanged(QString)));
77 connect(imageCombo_, SIGNAL(activated(QString)), this, SLOT(imageSelectionChanged(QString)));
78 connect(applyButton_, SIGNAL(clicked()), this, SLOT(applyClicked()));
79 connect(trackerFiles_, SIGNAL(filesRead(QStringList)), this, SLOT(trackerFilesRead(QStringList)));
82 int SettingsDialog::exec()
84 // Making sure that a random image is picked when starting a new game after the first game (fix me)
85 if(imageCombo_->currentText() == RANDOM_IMAGE_TXT) {
86 imageSelectionChanged(RANDOM_IMAGE_TXT);
89 return QDialog::exec();
92 void SettingsDialog::difficultySelectionChanged(bool value)
95 Settings::instance()->setPieceCount(EASY_PIECE_COUNT);
98 Settings::instance()->setPieceCount(HARD_PIECE_COUNT);
102 void SettingsDialog::imageSelectionChanged(const QString &txt)
104 if(txt == RANDOM_IMAGE_TXT) {
105 if(Settings::instance()->localImages().isEmpty()) {
106 trackerFiles_->readFiles();
109 trackerFilesRead(Settings::instance()->localImages());
112 else if(txt == SELECT_IMAGE_TXT) {
113 //qDebug() << "Select image... selected";
115 // Open file selection dialog
116 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
117 QDir::homePath() + QLatin1String("/MyDocs/.images"),
118 tr("Images (*.png *.xpm *.jpg)"));
120 Settings::instance()->setImage(QPixmap(fileName));
121 Settings::instance()->setImagePath(fileName);
123 selectedImageLabel_->setText(fileName);
125 if(!selectedImageLabel_->isVisible() && !fileName.isEmpty()) {
126 selectedImageLabel_->setVisible(true);
130 //qDebug() << "Default image selected";
132 Settings::instance()->setImage(0);
133 Settings::instance()->setImagePath("default");
135 if(selectedImageLabel_->isVisible()) {
136 selectedImageLabel_->setVisible(false);
141 void SettingsDialog::applyClicked()
146 void SettingsDialog::trackerFilesRead(const QStringList &files)
148 if(!files.isEmpty()) {
149 QString path = files.at(qrand() % files.count());
150 path = path.trimmed();
151 Settings::instance()->setImage(QPixmap(path));
152 Settings::instance()->setImagePath(path);
154 if(Settings::instance()->localImages().isEmpty()) {
155 Settings::instance()->setLocalImages(files);
159 Settings::instance()->setImage(QPixmap(":/images/default.jpg"));
160 Settings::instance()->setImagePath("default");
163 if(selectedImageLabel_->isVisible()) {
164 selectedImageLabel_->setVisible(false);