2010-06-16 12:53:56 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2008 by Oliver Groß *
|
|
|
|
* z.o.gross@gmx.de *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QInputDialog>
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
#include <libqnono/ccrosspackagelistmodel.h>
|
|
|
|
#include <libqnono/ccrosspackage.h>
|
|
|
|
#include <libqnono/cnonogram.h>
|
2010-06-16 12:53:56 +00:00
|
|
|
#include "cmaskedcrosspackagemodel.h"
|
|
|
|
#include "chighscore.h"
|
|
|
|
#include "cnewgamedialog.h"
|
|
|
|
#include "constants.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
namespace qcross {
|
2011-12-21 11:49:33 +00:00
|
|
|
using namespace libqnono;
|
2010-06-16 12:53:56 +00:00
|
|
|
|
|
|
|
//public:
|
|
|
|
CNewGameDialog::CNewGameDialog(QWidget * parent, Qt::WindowFlags f) :
|
|
|
|
QDialog(parent, f),
|
2011-12-21 11:49:33 +00:00
|
|
|
m_Highscore(NULL),
|
2010-06-16 12:53:56 +00:00
|
|
|
m_Nonogram(NULL)
|
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
|
|
|
|
|
|
|
QPushButton * importButton = new QPushButton(tr("Import package..."), this);
|
|
|
|
QPushButton * openPicFileButton = new QPushButton(tr("Open picture file..."), this);
|
|
|
|
|
|
|
|
connect(importButton, SIGNAL(clicked()), this, SLOT(importPackage()));
|
|
|
|
connect(openPicFileButton, SIGNAL(clicked()), this, SLOT(openPictureFile()));
|
|
|
|
|
|
|
|
ui.buttonBox->addButton(importButton, QDialogButtonBox::ActionRole);
|
|
|
|
ui.buttonBox->addButton(openPicFileButton, QDialogButtonBox::ActionRole);
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
ui.packageList->setModel(new CCrossPackageListModel(QCROSS_STRING_DATAPATH, "*" QCROSS_STRING_PACKAGE_EXT));
|
2010-06-16 12:53:56 +00:00
|
|
|
m_PicModel = new CMaskedCrossPackageModel();
|
|
|
|
m_PicProxyModel = new CMaskedCrossPackageProxyModel();
|
|
|
|
m_PicProxyModel->setSourceModel(m_PicModel);
|
|
|
|
m_PicModel->setHighscore(m_Highscore);
|
|
|
|
ui.picList->setModel(m_PicProxyModel);
|
|
|
|
|
|
|
|
// ui.picList->header()->setResizeMode(1, QHeaderView::Fixed);
|
|
|
|
// ui.picList->header()->setResizeMode(0, QHeaderView::Stretch);
|
|
|
|
|
|
|
|
connect(ui.unfinishedCheck, SIGNAL(toggled(bool)), m_PicProxyModel, SLOT(setUnfinishedFilter(bool)));
|
|
|
|
|
|
|
|
connect(ui.packageList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
|
|
|
|
this, SLOT(handlePackageSelectionChanged(const QItemSelection &, const QItemSelection &)));
|
|
|
|
|
|
|
|
connect(ui.picList, SIGNAL(activated(QModelIndex)), this, SLOT(accept()));
|
|
|
|
connect(ui.picList->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
|
|
|
|
this, SLOT(handlePictureSelectionChanged(QItemSelection,QItemSelection)));
|
|
|
|
}
|
|
|
|
|
|
|
|
CNewGameDialog::~CNewGameDialog() {
|
|
|
|
if (m_Highscore)
|
|
|
|
delete m_Highscore;
|
|
|
|
|
|
|
|
if (m_Nonogram)
|
|
|
|
delete m_Nonogram;
|
|
|
|
}
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
/* libqnono::CNonogram * CNewGameDialog::selectedNonogramData() const {
|
2010-06-16 12:53:56 +00:00
|
|
|
QModelIndex selected = ui.picList->selectionModel()->selectedIndexes()[0];
|
|
|
|
return static_cast<CNonogram *>(selected.internalPointer());
|
|
|
|
}*/
|
|
|
|
|
|
|
|
int CNewGameDialog::nonogramIndex() const {
|
|
|
|
QModelIndex selected = m_PicProxyModel->mapToSource(ui.picList->selectionModel()->selectedIndexes()[0]);
|
|
|
|
return selected.row();
|
|
|
|
}
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
libqnono::CCrossPackage * CNewGameDialog::selectedPackage() const {
|
2010-06-16 12:53:56 +00:00
|
|
|
QModelIndex selected = ui.packageList->selectionModel()->selectedIndexes()[0];
|
|
|
|
return static_cast<CCrossPackage *>(selected.internalPointer());
|
|
|
|
}
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
libqnono::CNonogram * CNewGameDialog::takeNonogram() {
|
|
|
|
libqnono::CNonogram * result;
|
2010-06-16 12:53:56 +00:00
|
|
|
|
|
|
|
if (m_Nonogram) {
|
|
|
|
result = m_Nonogram;
|
|
|
|
m_Nonogram = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QModelIndexList selected = ui.packageList->selectionModel()->selectedIndexes();
|
|
|
|
if (selected.isEmpty())
|
|
|
|
result = NULL;
|
|
|
|
else {
|
|
|
|
result = static_cast<CCrossPackage *>(selected[0].internalPointer())->pictures().takeAt(nonogramIndex());
|
|
|
|
m_PicModel->setPackage(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHighscore * CNewGameDialog::takeHighscore() {
|
|
|
|
if (m_Nonogram)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CHighscore * result = m_Highscore;
|
|
|
|
m_Highscore = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//protected:
|
|
|
|
void CNewGameDialog::importPackage() {
|
|
|
|
QString fileName = QFileDialog::getOpenFileName(this, tr("Select a package to import"),
|
|
|
|
QDir::homePath(), tr("QCross Package (*.cpk)"));
|
|
|
|
if (!fileName.isEmpty()) {
|
2011-12-21 11:49:33 +00:00
|
|
|
/* if (!File::exists(QCROSS_STRING_DATAPATH))
|
|
|
|
QDir::mkpath(QCROSS_STRING_DATAPATH);*/
|
|
|
|
|
2010-06-16 12:53:56 +00:00
|
|
|
QString newFileName = QCROSS_STRING_DATAPATH + QDir::separator() + fileName.section(QDir::separator(), -1);
|
|
|
|
if (QFile::copy(fileName, newFileName))
|
|
|
|
dynamic_cast<CCrossPackageListModel *>(ui.packageList->model())->update();
|
|
|
|
else
|
|
|
|
qCritical("could not copy file %s to %s", fileName.toAscii().data(), newFileName.toAscii().data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNewGameDialog::openPictureFile() {
|
|
|
|
QString fileName = QFileDialog::getOpenFileName(this, tr("Select a picture to open"),
|
|
|
|
QDir::homePath(), tr("Images (*.png *.xpm *.xbm *.jpg)"));
|
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
if (m_Nonogram)
|
|
|
|
delete m_Nonogram;
|
|
|
|
|
|
|
|
QImage image(fileName);
|
|
|
|
if (!image.isNull()) {
|
|
|
|
m_Nonogram = new CNonogram();
|
|
|
|
m_Nonogram->loadFromImage(image);
|
|
|
|
m_Nonogram->updateNumbers();
|
|
|
|
accept();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_Nonogram = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//private:
|
|
|
|
void CNewGameDialog::handlePackageSelectionChanged(const QItemSelection & selected, const QItemSelection &) {
|
|
|
|
QModelIndex index = selected.indexes()[0];
|
|
|
|
|
|
|
|
CCrossPackage * package = static_cast<CCrossPackage *>(index.internalPointer());
|
|
|
|
m_PicModel->setHighscore(NULL);
|
|
|
|
m_PicModel->setPackage(package);
|
|
|
|
|
2011-12-21 11:49:33 +00:00
|
|
|
if (!m_Highscore)
|
|
|
|
m_Highscore = new CHighscore(0);
|
|
|
|
|
2010-06-16 12:53:56 +00:00
|
|
|
m_Highscore->setFileName(getHighscoreFileName(package->fileName()));
|
|
|
|
if (!m_Highscore->open()) {
|
|
|
|
qDebug("opening highscore file failed. will create a new one when nonogram is solved");
|
|
|
|
m_Highscore->clear();
|
|
|
|
for (int i = 0; i < package->pictures().size(); i++)
|
|
|
|
m_Highscore->append(0);
|
|
|
|
}
|
|
|
|
m_PicModel->setHighscore(m_Highscore);
|
|
|
|
}
|
|
|
|
void CNewGameDialog::handlePictureSelectionChanged(const QItemSelection & selected, const QItemSelection &) {
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(selected.isEmpty());
|
|
|
|
}
|
|
|
|
}
|