/*************************************************************************** * 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 #include #include #include #include #include #include #include #include #include #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_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); 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_Nonogram) delete m_Nonogram; } /* libqnono::CNonogram * CNewGameDialog::selectedNonogramData() const { QModelIndex selected = ui.picList->selectionModel()->selectedIndexes()[0]; return static_cast(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_Nonogram) return NULL; QModelIndex selected = ui.packageList->selectionModel()->selectedIndexes()[0]; return static_cast(selected.internalPointer()); } QString CNewGameDialog::selectedPackageName() const { if (m_Nonogram) return QString(); libqnono::CCrossPackage *p = selectedPackage(); return p ? p->name() : QString(); } libqnono::CNonogram * CNewGameDialog::takeNonogram() { libqnono::CNonogram * result; 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(selected[0].internalPointer())->takePicture(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"), QString(), tr("QCross Package (*.cpk)")); if (!fileName.isEmpty()) { /* if (!File::exists(QCROSS_STRING_DATAPATH)) QDir::mkpath(QCROSS_STRING_DATAPATH);*/ QString newFileName = QCROSS_STRING_DATAPATH + QDir::separator() + fileName.section(QDir::separator(), -1); if (QFile::copy(fileName, newFileName)) dynamic_cast(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()) { 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(index.internalPointer()); m_PicModel->setHighscore(NULL); m_PicModel->setPackage(package); if (!m_Highscore) m_Highscore = new CHighscore(0); 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()); } }