Added support for import data from file.
parent
ace78877a3
commit
fe5eac9cd9
@ -0,0 +1,93 @@
|
||||
#include "csvimporter.h"
|
||||
#include <QFile>
|
||||
#include <QVariant>
|
||||
|
||||
CsvImporter::CsvImporter(const QMetaObject *metaObject, QObject *parent)
|
||||
:QObject(parent),
|
||||
IImporter(metaObject)
|
||||
{
|
||||
m_parsed = false;
|
||||
m_currentRec = 1;
|
||||
m_error = false;
|
||||
}
|
||||
|
||||
void CsvImporter::setImportFile(const QString &fileName)
|
||||
{
|
||||
m_fileName = fileName;
|
||||
}
|
||||
|
||||
int CsvImporter::recordCount()
|
||||
{
|
||||
if (!m_parsed)
|
||||
{
|
||||
parseFile();
|
||||
}
|
||||
|
||||
return m_lines.count() - 1;
|
||||
}
|
||||
|
||||
QSharedPointer<QObject> CsvImporter::nextRecord()
|
||||
{
|
||||
if (!m_parsed)
|
||||
{
|
||||
parseFile();
|
||||
}
|
||||
|
||||
QObject *entity = m_metaObject->newInstance();
|
||||
|
||||
if (entity == NULL || m_currentRec > recordCount())
|
||||
{
|
||||
++m_currentRec;
|
||||
return QSharedPointer<QObject>();
|
||||
}
|
||||
|
||||
QStringList props = m_header.split(m_separator);
|
||||
QString line = m_lines[m_currentRec];
|
||||
QStringList values = line.split(m_separator);
|
||||
|
||||
for (int i = 0; i < props.size(); i++) {
|
||||
QString property = props[i];
|
||||
QString value = values[i];
|
||||
if (!entity->setProperty(property.toStdString().c_str(), QVariant(value)))
|
||||
{
|
||||
m_error = true;
|
||||
emit noSuchProperty(property);
|
||||
|
||||
++m_currentRec;
|
||||
return QSharedPointer<QObject>();
|
||||
}
|
||||
}
|
||||
|
||||
++m_currentRec;
|
||||
|
||||
return QSharedPointer<QObject>(entity);
|
||||
}
|
||||
|
||||
bool CsvImporter::isError()
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
void CsvImporter::parseFile()
|
||||
{
|
||||
QFile file(m_fileName);
|
||||
|
||||
if (!file.open(QFile::ReadOnly | QFile::Text))
|
||||
{
|
||||
m_error = true;
|
||||
emit parseError();
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray data = file.readAll();
|
||||
QString strData(data);
|
||||
|
||||
m_lines = strData.split("\n");
|
||||
m_header = m_lines[0];
|
||||
m_parsed = true;
|
||||
}
|
||||
|
||||
void CsvImporter::setSeparator(const QString &separator)
|
||||
{
|
||||
m_separator = separator;
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
#ifndef CSVIMPORTER_H
|
||||
#define CSVIMPORTER_H
|
||||
|
||||
#include "iimporter.h"
|
||||
#include <QStringList>
|
||||
#include <QObject>
|
||||
|
||||
class CsvImporter : public QObject, public IImporter
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CsvImporter(const QMetaObject *metaObject, QObject *parent = NULL);
|
||||
|
||||
// IImporter interface
|
||||
public:
|
||||
void setImportFile(const QString &fileName);
|
||||
int recordCount();
|
||||
QSharedPointer<QObject> nextRecord();
|
||||
bool isError();
|
||||
|
||||
void setSeparator(const QString &separator);
|
||||
|
||||
signals:
|
||||
void parseError();
|
||||
void noSuchProperty(QString propName);
|
||||
|
||||
private:
|
||||
void parseFile();
|
||||
|
||||
QString m_header;
|
||||
QString m_separator;
|
||||
QString m_fileName;
|
||||
QStringList m_lines;
|
||||
bool m_parsed;
|
||||
bool m_error;
|
||||
int m_currentRec;
|
||||
};
|
||||
|
||||
#endif // CSVIMPORTER_H
|
@ -0,0 +1,22 @@
|
||||
#ifndef IIMPORTER_H
|
||||
#define IIMPORTER_H
|
||||
|
||||
#include <QMetaObject>
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
|
||||
class IImporter
|
||||
{
|
||||
public:
|
||||
explicit IImporter(const QMetaObject *metaObject) { m_metaObject = metaObject; }
|
||||
|
||||
virtual void setImportFile(const QString &fileName) = 0;
|
||||
virtual int recordCount() = 0;
|
||||
virtual QSharedPointer<QObject> nextRecord() = 0;
|
||||
virtual bool isError() = 0;
|
||||
|
||||
protected:
|
||||
const QMetaObject *m_metaObject;
|
||||
};
|
||||
|
||||
#endif // IIMPORTER_H
|
Loading…
Reference in New Issue