diff --git a/libqnono/ccrosspackage.cpp b/libqnono/ccrosspackage.cpp index b99c9c1..4723182 100644 --- a/libqnono/ccrosspackage.cpp +++ b/libqnono/ccrosspackage.cpp @@ -24,8 +24,7 @@ namespace libqnono { //public: - CCrossPackage::CCrossPackage() : m_File(NULL) {} - CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName), m_File(NULL) {} + CCrossPackage::CCrossPackage() : m_File(NULL), m_headersOnly(false) {} CCrossPackage::~CCrossPackage() { close(); @@ -60,7 +59,7 @@ namespace libqnono { } } - bool CCrossPackage::readHeader() { + bool CCrossPackage::doReadHeader() { QDataStream in(m_File); in.setVersion(QDataStream::Qt_4_0); @@ -95,10 +94,12 @@ namespace libqnono { } m_Name = stringBuffer; + m_headersOnly = true; + return true; } - bool CCrossPackage::readData() { + bool CCrossPackage::doReadData() { if (!m_File) return false; @@ -133,10 +134,40 @@ namespace libqnono { if (i < pictureCount) qWarning("damaged package file - invalid picture count"); + m_headersOnly = false; + return true; } + CNonogram * CCrossPackage::takePicture(int ndx) { + loadPictures(); + CNonogram * result = m_PictureList.takeAt(ndx); + unloadPictures(); + return result; + } + + bool CCrossPackage::loadPictures() { + if (!m_headersOnly) return true; + if (!open() || !doReadHeader() || !doReadData()) { + close(); + return false; + } + close(); + return true; + } + + void CCrossPackage::unloadPictures() { + if (!m_PictureList.empty()) { + foreach (CNonogram * i, m_PictureList) { + delete i; + } + m_PictureList.clear(); + m_headersOnly = true; + } + } + bool CCrossPackage::save() { + Q_ASSERT(!m_headersOnly); QFile file(m_FileName); if (!file.open(QIODevice::WriteOnly)) return false; @@ -154,4 +185,26 @@ namespace libqnono { file.close(); return true; } + + CCrossPackage * CCrossPackage::read(QString fileName) { + CCrossPackage *p = new CCrossPackage(); + p->setFileName(fileName); + if (!p->open() || !p->doReadHeader() || !p->doReadData()) { + delete p; + return NULL; + } + p->close(); + return p; + } + + CCrossPackage * CCrossPackage::readHeader(QString fileName) { + CCrossPackage *p = new CCrossPackage(); + p->setFileName(fileName); + if (!p->open() || !p->doReadHeader()) { + delete p; + return NULL; + } + p->close(); + return p; + } } diff --git a/libqnono/ccrosspackage.h b/libqnono/ccrosspackage.h index bb48f2c..d137f77 100644 --- a/libqnono/ccrosspackage.h +++ b/libqnono/ccrosspackage.h @@ -33,29 +33,38 @@ namespace libqnono { class CCrossPackage { public: CCrossPackage(); - CCrossPackage(QString fileName); ~CCrossPackage(); - virtual void setFileName(QString & value) { m_FileName = value; } + void setFileName(QString & value) { m_FileName = value; } QString fileName() const { return m_FileName; } void setName(QString value) { m_Name = value; } QString name() const { return m_Name; } - bool open(); - void close(); + QMonoPictureList & pictures() { Q_ASSERT(!m_headersOnly); return m_PictureList; } + CNonogram * takePicture(int ndx); - bool readHeader(); - bool readData(); + bool loadPictures(); + void unloadPictures(); bool save(); - QMonoPictureList & pictures() { return m_PictureList; } + static CCrossPackage * read(QString fileName); + static CCrossPackage * readHeader(QString fileName); + + protected: + bool open(); + void close(); + + bool doReadHeader(); + bool doReadData(); + private: QMonoPictureList m_PictureList; QString m_FileName; QString m_Name; QFile * m_File; + bool m_headersOnly; /** valid headers but no pictures */ }; } diff --git a/libqnono/ccrosspackagelistmodel.cpp b/libqnono/ccrosspackagelistmodel.cpp index 8b539eb..0ed035d 100644 --- a/libqnono/ccrosspackagelistmodel.cpp +++ b/libqnono/ccrosspackagelistmodel.cpp @@ -45,13 +45,8 @@ namespace libqnono { QDir workDir(m_DataPath); QStringList fileNames = workDir.entryList(QStringList(m_DataFilter), QDir::Files | QDir::NoDotAndDotDot, QDir::Name); foreach (QString fileName, fileNames) { - newPackage = new CCrossPackage(workDir.filePath(fileName)); - newPackage->open(); - if (newPackage->readHeader()) - m_PackageList << newPackage; - else - delete newPackage; - newPackage->close(); + newPackage = CCrossPackage::readHeader(workDir.filePath(fileName)); + if (newPackage) m_PackageList << newPackage; } reset(); diff --git a/libqnono/ccrosspackagemodel.cpp b/libqnono/ccrosspackagemodel.cpp index e59c28a..fa12d47 100644 --- a/libqnono/ccrosspackagemodel.cpp +++ b/libqnono/ccrosspackagemodel.cpp @@ -69,16 +69,12 @@ namespace libqnono { void CCrossPackageModel::setPackage(libqnono::CCrossPackage * package) { if (m_Package) - m_Package->pictures().clear(); + m_Package->unloadPictures(); m_Package = package; - if (m_Package && m_Package->pictures().isEmpty()) { - m_Package->open(); - m_Package->readHeader(); - m_Package->readData(); - m_Package->close(); - } + if (m_Package) + m_Package->loadPictures(); reset(); } diff --git a/libqnono/cnonogram.h b/libqnono/cnonogram.h index 4c11624..77f1227 100644 --- a/libqnono/cnonogram.h +++ b/libqnono/cnonogram.h @@ -35,7 +35,9 @@ namespace libqnono { CNonogram(QSize size); CNonogram(QImage & image); CNonogram(QDataStream & stream); + CNonogram(const CNonogram &other); ~CNonogram(); + CNonogram& operator=(const CNonogram &other); void loadFromImage(QImage & image); void resize(QSize size); diff --git a/qcross/cnewgamedialog.cpp b/qcross/cnewgamedialog.cpp index 5b1341d..80b7c36 100644 --- a/qcross/cnewgamedialog.cpp +++ b/qcross/cnewgamedialog.cpp @@ -117,7 +117,7 @@ namespace qcross { if (selected.isEmpty()) result = NULL; else { - result = static_cast(selected[0].internalPointer())->pictures().takeAt(nonogramIndex()); + result = static_cast(selected[0].internalPointer())->takePicture(nonogramIndex()); m_PicModel->setPackage(NULL); } } diff --git a/qcrossedit/cmainwindow.cpp b/qcrossedit/cmainwindow.cpp index 975d0c1..746d48d 100644 --- a/qcrossedit/cmainwindow.cpp +++ b/qcrossedit/cmainwindow.cpp @@ -124,15 +124,10 @@ namespace qcrossedit { if (m_Package) delete m_Package; - m_Package = new CCrossPackage(); - m_Package->setFileName(fileName); - - if (!(m_Package->open() && m_Package->readAll())) { - delete m_Package; - m_Package = NULL; - } - else + m_Package = CCrossPackage::read(fileName); + if (m_Package) { statusBar()->showMessage(tr("Package \"%1\" opened.").arg(m_Package->name())); + } qobject_cast(m_PicListView->model())->setPackage(m_Package); } @@ -186,7 +181,7 @@ namespace qcrossedit { } void CMainWindow::editSetTimeout() { - QInputDialog::getInt(this, tr("Set time-out for selected nonogram (in minutes)")); + QInputDialog::getInt(this, tr("Timeout"), tr("Set time-out for selected nonogram (in minutes)")); } void CMainWindow::editDelete() {