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 {
//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;
}
}

View File

@ -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 */
};
}

View File

@ -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();

View File

@ -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();
}

View File

@ -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);

View File

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

View File

@ -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<CCrossPackageModel *>(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() {