Commit 06eee4c6 authored by Laurent Montel's avatar Laurent Montel 😁

not necessary to have a private class

parent 39c87134
......@@ -95,178 +95,144 @@ void CsvParser::run()
mReader->read(mDevice);
}
class QCsvModel::Private
{
public:
Private(QCsvModel *model)
: mParent(model)
, mParser(nullptr)
, mDevice(nullptr)
, mRowCount(0)
, mColumnCount(0)
{
}
void columnCountChanged(int columns);
void rowCountChanged(int rows);
void fieldChanged(const QString &data, int row, int column);
void finishedLoading();
QCsvModel *mParent = nullptr;
CsvParser *mParser = nullptr;
QVector<QString> mFieldIdentifiers;
QMap< QPair<int, int>, QString> mFields;
QIODevice *mDevice = nullptr;
int mRowCount = 0;
int mColumnCount = 0;
};
void QCsvModel::Private::columnCountChanged(int columns)
void QCsvModel::columnCountChanged(int columns)
{
mColumnCount = columns;
mFieldIdentifiers.resize(columns);
mFieldIdentifiers[ columns - 1 ] = QStringLiteral("0");
Q_EMIT mParent->layoutChanged();
Q_EMIT layoutChanged();
}
void QCsvModel::Private::rowCountChanged(int rows)
void QCsvModel::rowCountChanged(int rows)
{
mRowCount = rows;
Q_EMIT mParent->layoutChanged();
Q_EMIT layoutChanged();
}
void QCsvModel::Private::fieldChanged(const QString &data, int row, int column)
void QCsvModel::fieldChanged(const QString &data, int row, int column)
{
mFields.insert(QPair<int, int>(row, column), data);
}
void QCsvModel::Private::finishedLoading()
{
Q_EMIT mParent->finishedLoading();
}
QCsvModel::QCsvModel(QObject *parent)
: QAbstractTableModel(parent)
, d(new Private(this))
{
d->mParser = new CsvParser(this);
mParser = new CsvParser(this);
connect(d->mParser, SIGNAL(columnCountChanged(int)),
connect(mParser, SIGNAL(columnCountChanged(int)),
this, SLOT(columnCountChanged(int)), Qt::QueuedConnection);
connect(d->mParser, SIGNAL(rowCountChanged(int)),
connect(mParser, SIGNAL(rowCountChanged(int)),
this, SLOT(rowCountChanged(int)), Qt::QueuedConnection);
connect(d->mParser, SIGNAL(dataChanged(QString,int,int)),
connect(mParser, SIGNAL(dataChanged(QString,int,int)),
this, SLOT(fieldChanged(QString,int,int)), Qt::QueuedConnection);
connect(d->mParser, &CsvParser::ended, this, &QCsvModel::finishedLoading);
connect(mParser, &CsvParser::ended, this, &QCsvModel::finishedLoading);
}
QCsvModel::~QCsvModel()
{
delete d;
}
bool QCsvModel::load(QIODevice *device)
{
d->mDevice = device;
d->mRowCount = 0;
d->mColumnCount = 0;
mDevice = device;
mRowCount = 0;
mColumnCount = 0;
Q_EMIT layoutChanged();
d->mParser->load(device);
mParser->load(device);
return true;
}
void QCsvModel::setTextQuote(QChar textQuote)
{
const bool isRunning = d->mParser->isRunning();
const bool isRunning = mParser->isRunning();
if (isRunning) {
d->mParser->reader()->terminate();
d->mParser->wait();
mParser->reader()->terminate();
mParser->wait();
}
d->mParser->reader()->setTextQuote(textQuote);
mParser->reader()->setTextQuote(textQuote);
if (isRunning) {
load(d->mDevice);
load(mDevice);
}
}
QChar QCsvModel::textQuote() const
{
return d->mParser->reader()->textQuote();
return mParser->reader()->textQuote();
}
void QCsvModel::setDelimiter(const QChar &delimiter)
{
const bool isRunning = d->mParser->isRunning();
const bool isRunning = mParser->isRunning();
if (isRunning) {
d->mParser->reader()->terminate();
d->mParser->wait();
mParser->reader()->terminate();
mParser->wait();
}
d->mParser->reader()->setDelimiter(delimiter);
mParser->reader()->setDelimiter(delimiter);
if (isRunning) {
load(d->mDevice);
load(mDevice);
}
}
QChar QCsvModel::delimiter() const
{
return d->mParser->reader()->delimiter();
return mParser->reader()->delimiter();
}
void QCsvModel::setStartRow(uint startRow)
{
const bool isRunning = d->mParser->isRunning();
const bool isRunning = mParser->isRunning();
if (isRunning) {
d->mParser->reader()->terminate();
d->mParser->wait();
mParser->reader()->terminate();
mParser->wait();
}
d->mParser->reader()->setStartRow(startRow);
mParser->reader()->setStartRow(startRow);
if (isRunning) {
load(d->mDevice);
load(mDevice);
}
}
uint QCsvModel::startRow() const
{
return d->mParser->reader()->startRow();
return mParser->reader()->startRow();
}
void QCsvModel::setTextCodec(QTextCodec *textCodec)
{
const bool isRunning = d->mParser->isRunning();
const bool isRunning = mParser->isRunning();
if (isRunning) {
d->mParser->reader()->terminate();
d->mParser->wait();
mParser->reader()->terminate();
mParser->wait();
}
d->mParser->reader()->setTextCodec(textCodec);
mParser->reader()->setTextCodec(textCodec);
if (isRunning) {
load(d->mDevice);
load(mDevice);
}
}
QTextCodec *QCsvModel::textCodec() const
{
return d->mParser->reader()->textCodec();
return mParser->reader()->textCodec();
}
int QCsvModel::columnCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return d->mColumnCount;
return mColumnCount;
} else {
return 0;
}
......@@ -275,7 +241,7 @@ int QCsvModel::columnCount(const QModelIndex &parent) const
int QCsvModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return d->mRowCount + 1; // +1 for the header row
return mRowCount + 1; // +1 for the header row
} else {
return 0;
}
......@@ -288,23 +254,23 @@ QVariant QCsvModel::data(const QModelIndex &index, int role) const
}
if (index.row() == 0) {
if (index.column() >= d->mFieldIdentifiers.count()) {
if (index.column() >= mFieldIdentifiers.count()) {
return QVariant();
}
if (role == Qt::DisplayRole || role == Qt::EditRole) {
return d->mFieldIdentifiers.at(index.column());
return mFieldIdentifiers.at(index.column());
}
return QVariant();
}
const QPair<int, int> pair(index.row() - 1, index.column());
if (!d->mFields.contains(pair)) {
if (!mFields.contains(pair)) {
return QVariant();
}
const QString data = d->mFields.value(pair);
const QString data = mFields.value(pair);
if (role == Qt::DisplayRole) {
return data;
......@@ -316,8 +282,8 @@ QVariant QCsvModel::data(const QModelIndex &index, int role) const
bool QCsvModel::setData(const QModelIndex &index, const QVariant &data, int role)
{
if (role == Qt::EditRole && index.row() == 0
&& index.column() <= d->mFieldIdentifiers.count()) {
d->mFieldIdentifiers[ index.column() ] = data.toString();
&& index.column() <= mFieldIdentifiers.count()) {
mFieldIdentifiers[ index.column() ] = data.toString();
Q_EMIT dataChanged(index, index);
return true;
......
......@@ -22,7 +22,7 @@
#include <QAbstractTableModel>
#include <QIODevice>
class CsvParser;
class QCsvModel : public QAbstractTableModel
{
Q_OBJECT
......@@ -123,14 +123,19 @@ Q_SIGNALS:
*/
void finishedLoading();
private Q_SLOTS:
void columnCountChanged(int columns);
void rowCountChanged(int rows);
void fieldChanged(const QString &data, int row, int column);
private:
class Private;
Private *const d;
Q_PRIVATE_SLOT(d, void columnCountChanged(int))
Q_PRIVATE_SLOT(d, void rowCountChanged(int))
Q_PRIVATE_SLOT(d, void fieldChanged(const QString &, int, int))
Q_PRIVATE_SLOT(d, void finishedLoading())
CsvParser *mParser = nullptr;
QVector<QString> mFieldIdentifiers;
QMap< QPair<int, int>, QString> mFields;
QIODevice *mDevice = nullptr;
int mRowCount = 0;
int mColumnCount = 0;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment