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"
23 #include <QRadioButton>
24 #include <QPushButton>
26 #include <QVBoxLayout>
27 #include <QHBoxLayout>
29 #include <QFileDialog>
35 SettingsDialog::SettingsDialog(QWidget *parent) :
40 easyButton_ = new QRadioButton(tr("Easy"));
41 easyButton_->setChecked(true);
42 hardButton_ = new QRadioButton(tr("Hard"));
44 buttonLayout_ = new QHBoxLayout;
45 buttonLayout_->addWidget(easyButton_);
46 buttonLayout_->addWidget(hardButton_);
48 buttonGroup_ = new QGroupBox(tr("Difficulty"));
49 buttonGroup_->setLayout(buttonLayout_);
52 items << DEFAULT_IMAGE_TXT << RANDOM_IMAGE_TXT << SELECT_IMAGE_TXT;
54 imageCombo_ = new QComboBox;
55 imageCombo_->addItems(items);
56 imageCombo_->setCurrentIndex(1);
58 selectedImageLabel_ = new QLabel(tr("n/a"));
60 applyButton_ = new QPushButton(tr("Play"));
62 mainLayout_ = new QVBoxLayout;
63 mainLayout_->addWidget(buttonGroup_);
64 mainLayout_->addWidget(imageCombo_);
65 mainLayout_->addWidget(selectedImageLabel_);
66 mainLayout_->addWidget(applyButton_);
68 selectedImageLabel_->setVisible(false);
70 setLayout(mainLayout_);
72 connect(easyButton_, SIGNAL(toggled(bool)), this, SLOT(difficultySelectionChanged(bool)));
73 //connect(imageCombo_, SIGNAL(currentIndexChanged(QString)), this, SLOT(imageSelectionChanged(QString)));
74 connect(imageCombo_, SIGNAL(activated(QString)), this, SLOT(imageSelectionChanged(QString)));
75 connect(applyButton_, SIGNAL(clicked()), this, SLOT(applyClicked()));
78 int SettingsDialog::exec()
80 // Making sure that a random image is picked when starting a new game after the first game (fix me)
81 if(imageCombo_->currentText() == RANDOM_IMAGE_TXT) {
82 imageSelectionChanged(RANDOM_IMAGE_TXT);
85 return QDialog::exec();
88 void SettingsDialog::difficultySelectionChanged(bool value)
91 Settings::instance()->setPieceCount(EASY_PIECE_COUNT);
94 Settings::instance()->setPieceCount(HARD_PIECE_COUNT);
98 void SettingsDialog::imageSelectionChanged(const QString &txt)
100 if(txt == RANDOM_IMAGE_TXT) {
101 qDebug() << "Random image selected";
103 // Get random image from ~/MyDocs/.images/
104 //TODO: images from other directories
106 filters << "*.jpg" << "*.png" << "*.xpm";
108 QDir dir(QDir::homePath() + QLatin1String("/MyDocs/.images"));
109 //dir.setNameFilters(filters);
111 QStringList pics = dir.entryList(filters, QDir::Files | QDir::NoSymLinks);
113 //qDebug() << QString("pics list contains %1 entries").arg(pics.count());
115 if(!pics.isEmpty()) {
116 QString path = QDir::homePath() + QLatin1String("/MyDocs/.images/") + pics.at(qrand() % pics.count());
117 Settings::instance()->setImage(QPixmap(path));
118 Settings::instance()->setImagePath(path);
121 Settings::instance()->setImage(QPixmap(":/images/default.jpg"));
122 Settings::instance()->setImagePath("default");
125 if(selectedImageLabel_->isVisible()) {
126 selectedImageLabel_->setVisible(false);
129 else if(txt == SELECT_IMAGE_TXT) {
130 //qDebug() << "Select image... selected";
132 // Open file selection dialog
133 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
134 QDir::homePath() + QLatin1String("/MyDocs/.images"),
135 tr("Images (*.png *.xpm *.jpg)"));
137 Settings::instance()->setImage(QPixmap(fileName));
138 Settings::instance()->setImagePath(fileName);
140 selectedImageLabel_->setText(fileName);
142 if(!selectedImageLabel_->isVisible() && !fileName.isEmpty()) {
143 selectedImageLabel_->setVisible(true);
147 //qDebug() << "Default image selected";
149 Settings::instance()->setImage(0);
150 Settings::instance()->setImagePath("default");
152 if(selectedImageLabel_->isVisible()) {
153 selectedImageLabel_->setVisible(false);
158 void SettingsDialog::applyClicked()