185 lines
7.1 KiB
C++
185 lines
7.1 KiB
C++
/***************************************************************************
|
|
* 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>
|
|
|
|
#include <libqnono/ccrosspackagelistmodel.h>
|
|
#include <libqnono/ccrosspackage.h>
|
|
#include <libqnono/cnonogram.h>
|
|
#include "cmaskedcrosspackagemodel.h"
|
|
#include "chighscore.h"
|
|
#include "cnewgamedialog.h"
|
|
#include "constants.h"
|
|
#include "common.h"
|
|
|
|
namespace qcross {
|
|
using namespace libqnono;
|
|
|
|
//public:
|
|
CNewGameDialog::CNewGameDialog(QWidget * parent, Qt::WindowFlags f) :
|
|
QDialog(parent, f),
|
|
m_Highscore(NULL),
|
|
m_Problem(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);
|
|
|
|
ui.packageList->setModel(new CCrossPackageListModel(QCROSS_STRING_DATAPATH, "*" QCROSS_STRING_PACKAGE_EXT));
|
|
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_Problem)
|
|
delete m_Problem;
|
|
}
|
|
|
|
/* libqnono::CNonogram * CNewGameDialog::selectedNonogramData() const {
|
|
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();
|
|
}
|
|
|
|
libqnono::CCrossPackage * CNewGameDialog::selectedPackage() const {
|
|
if (m_Problem) return NULL;
|
|
QModelIndex selected = ui.packageList->selectionModel()->selectedIndexes()[0];
|
|
return static_cast<CCrossPackage *>(selected.internalPointer());
|
|
}
|
|
|
|
QString CNewGameDialog::selectedPackageName() const {
|
|
if (m_Problem) return QString();
|
|
libqnono::CCrossPackage *p = selectedPackage();
|
|
return p ? p->name() : QString();
|
|
}
|
|
|
|
NonogramProblem CNewGameDialog::getProblem() {
|
|
if (m_Problem) {
|
|
return *m_Problem;
|
|
}
|
|
else {
|
|
QModelIndexList selected = ui.packageList->selectionModel()->selectedIndexes();
|
|
if (!selected.isEmpty()) {
|
|
return selectedPackage()->getPicture(nonogramIndex());
|
|
}
|
|
}
|
|
|
|
return NonogramProblem();
|
|
}
|
|
|
|
//protected:
|
|
void CNewGameDialog::importPackage() {
|
|
QString fileName = QFileDialog::getOpenFileName(this, tr("Select a package to import"),
|
|
QString(), tr("QCross Package (*.cpk)"));
|
|
if (!fileName.isEmpty()) {
|
|
QDir().mkpath(QCROSS_STRING_DATAPATH);
|
|
|
|
QString packageName = QDir::toNativeSeparators(fileName).section(QDir::separator(), -1);
|
|
QString newFileName = QCROSS_STRING_DATAPATH + QDir::separator() + packageName;
|
|
if (QFile::exists(newFileName)) {
|
|
if (QMessageBox::Ok == QMessageBox::question(this, tr("Overwrite old package"), tr("Overwrite old package %1?").arg(packageName),
|
|
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)) {
|
|
QFile::remove(newFileName);
|
|
} else {
|
|
return;
|
|
}
|
|
}
|
|
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"),
|
|
QString(), tr("Images (*.png *.xpm *.xbm *.jpg)"));
|
|
if (!fileName.isEmpty()) {
|
|
delete m_Problem; m_Problem = 0;
|
|
|
|
QImage image(fileName);
|
|
if (!image.isNull()) {
|
|
m_Problem = new NonogramProblem(NonogramImage(image));
|
|
accept();
|
|
}
|
|
}
|
|
}
|
|
|
|
//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);
|
|
|
|
if (!m_Highscore)
|
|
m_Highscore = new CHighscore(0);
|
|
|
|
m_Highscore->setPackageIdentifier(package->identifier());
|
|
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 &) {
|
|
delete m_Problem; m_Problem = 0;
|
|
ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(selected.isEmpty());
|
|
}
|
|
}
|