restructure CCrossPackage load/save, fix qcrossedit compile errors

This commit is contained in:
Stefan Bühler 2012-04-09 13:39:44 +02:00
parent 54e363d50c
commit e99e35c8a7
7 changed files with 85 additions and 35 deletions

View File

@ -24,8 +24,7 @@
namespace libqnono { namespace libqnono {
//public: //public:
CCrossPackage::CCrossPackage() : m_File(NULL) {} CCrossPackage::CCrossPackage() : m_File(NULL), m_headersOnly(false) {}
CCrossPackage::CCrossPackage(QString fileName) : m_FileName(fileName), m_File(NULL) {}
CCrossPackage::~CCrossPackage() { CCrossPackage::~CCrossPackage() {
close(); close();
@ -60,7 +59,7 @@ namespace libqnono {
} }
} }
bool CCrossPackage::readHeader() { bool CCrossPackage::doReadHeader() {
QDataStream in(m_File); QDataStream in(m_File);
in.setVersion(QDataStream::Qt_4_0); in.setVersion(QDataStream::Qt_4_0);
@ -95,10 +94,12 @@ namespace libqnono {
} }
m_Name = stringBuffer; m_Name = stringBuffer;
m_headersOnly = true;
return true; return true;
} }
bool CCrossPackage::readData() { bool CCrossPackage::doReadData() {
if (!m_File) if (!m_File)
return false; return false;
@ -133,10 +134,40 @@ namespace libqnono {
if (i < pictureCount) if (i < pictureCount)
qWarning("damaged package file - invalid picture count"); qWarning("damaged package file - invalid picture count");
m_headersOnly = false;
return true; 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() { bool CCrossPackage::save() {
Q_ASSERT(!m_headersOnly);
QFile file(m_FileName); QFile file(m_FileName);
if (!file.open(QIODevice::WriteOnly)) if (!file.open(QIODevice::WriteOnly))
return false; return false;
@ -154,4 +185,26 @@ namespace libqnono {
file.close(); file.close();
return true; 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;
}
} }

View File

@ -33,29 +33,38 @@ namespace libqnono {
class CCrossPackage { class CCrossPackage {
public: public:
CCrossPackage(); CCrossPackage();
CCrossPackage(QString fileName);
~CCrossPackage(); ~CCrossPackage();
virtual void setFileName(QString & value) { m_FileName = value; } void setFileName(QString & value) { m_FileName = value; }
QString fileName() const { return m_FileName; } QString fileName() const { return m_FileName; }
void setName(QString value) { m_Name = value; } void setName(QString value) { m_Name = value; }
QString name() const { return m_Name; } QString name() const { return m_Name; }
bool open(); QMonoPictureList & pictures() { Q_ASSERT(!m_headersOnly); return m_PictureList; }
void close(); CNonogram * takePicture(int ndx);
bool readHeader(); bool loadPictures();
bool readData(); void unloadPictures();
bool save(); 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: private:
QMonoPictureList m_PictureList; QMonoPictureList m_PictureList;
QString m_FileName; QString m_FileName;
QString m_Name; QString m_Name;
QFile * m_File; QFile * m_File;
bool m_headersOnly; /** valid headers but no pictures */
}; };
} }

View File

@ -45,13 +45,8 @@ namespace libqnono {
QDir workDir(m_DataPath); QDir workDir(m_DataPath);
QStringList fileNames = workDir.entryList(QStringList(m_DataFilter), QDir::Files | QDir::NoDotAndDotDot, QDir::Name); QStringList fileNames = workDir.entryList(QStringList(m_DataFilter), QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
foreach (QString fileName, fileNames) { foreach (QString fileName, fileNames) {
newPackage = new CCrossPackage(workDir.filePath(fileName)); newPackage = CCrossPackage::readHeader(workDir.filePath(fileName));
newPackage->open(); if (newPackage) m_PackageList << newPackage;
if (newPackage->readHeader())
m_PackageList << newPackage;
else
delete newPackage;
newPackage->close();
} }
reset(); reset();

View File

@ -69,16 +69,12 @@ namespace libqnono {
void CCrossPackageModel::setPackage(libqnono::CCrossPackage * package) { void CCrossPackageModel::setPackage(libqnono::CCrossPackage * package) {
if (m_Package) if (m_Package)
m_Package->pictures().clear(); m_Package->unloadPictures();
m_Package = package; m_Package = package;
if (m_Package && m_Package->pictures().isEmpty()) { if (m_Package)
m_Package->open(); m_Package->loadPictures();
m_Package->readHeader();
m_Package->readData();
m_Package->close();
}
reset(); reset();
} }

View File

@ -35,7 +35,9 @@ namespace libqnono {
CNonogram(QSize size); CNonogram(QSize size);
CNonogram(QImage & image); CNonogram(QImage & image);
CNonogram(QDataStream & stream); CNonogram(QDataStream & stream);
CNonogram(const CNonogram &other);
~CNonogram(); ~CNonogram();
CNonogram& operator=(const CNonogram &other);
void loadFromImage(QImage & image); void loadFromImage(QImage & image);
void resize(QSize size); void resize(QSize size);

View File

@ -117,7 +117,7 @@ namespace qcross {
if (selected.isEmpty()) if (selected.isEmpty())
result = NULL; result = NULL;
else { else {
result = static_cast<CCrossPackage *>(selected[0].internalPointer())->pictures().takeAt(nonogramIndex()); result = static_cast<CCrossPackage *>(selected[0].internalPointer())->takePicture(nonogramIndex());
m_PicModel->setPackage(NULL); m_PicModel->setPackage(NULL);
} }
} }

View File

@ -124,15 +124,10 @@ namespace qcrossedit {
if (m_Package) if (m_Package)
delete m_Package; delete m_Package;
m_Package = new CCrossPackage(); m_Package = CCrossPackage::read(fileName);
m_Package->setFileName(fileName); if (m_Package) {
if (!(m_Package->open() && m_Package->readAll())) {
delete m_Package;
m_Package = NULL;
}
else
statusBar()->showMessage(tr("Package \"%1\" opened.").arg(m_Package->name())); statusBar()->showMessage(tr("Package \"%1\" opened.").arg(m_Package->name()));
}
qobject_cast<CCrossPackageModel *>(m_PicListView->model())->setPackage(m_Package); qobject_cast<CCrossPackageModel *>(m_PicListView->model())->setPackage(m_Package);
} }
@ -186,7 +181,7 @@ namespace qcrossedit {
} }
void CMainWindow::editSetTimeout() { 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() { void CMainWindow::editDelete() {