diff --git a/src/core/CsvParser.cpp b/src/core/CsvParser.cpp index 87b3f340..288cd040 100644 --- a/src/core/CsvParser.cpp +++ b/src/core/CsvParser.cpp @@ -15,10 +15,12 @@ * along with this program. If not, see . */ +#include "CsvParser.h" + #include #include + #include "core/Tools.h" -#include "CsvParser.h" CsvParser::CsvParser() : m_ch(0) @@ -61,16 +63,14 @@ bool CsvParser::parse(QFile *device) { m_statusMsg += QObject::tr("NULL device\n"); return false; } - if (!readFile(device)) { + if (!readFile(device)) return false; - } return parseFile(); } bool CsvParser::readFile(QFile *device) { - if (device->isOpen()) { + if (device->isOpen()) device->close(); - } device->open(QIODevice::ReadOnly); if (!Tools::readAllFromDevice(device, m_array)) { @@ -78,14 +78,14 @@ bool CsvParser::readFile(QFile *device) { m_isFileLoaded = false; } else { - device->close(); + device->close(); - m_array.replace("\r\n", "\n"); - m_array.replace("\r", "\n"); - if (0 == m_array.size()) { - m_statusMsg += QObject::tr("File empty\n"); - } - m_isFileLoaded = true; + m_array.replace("\r\n", "\n"); + m_array.replace("\r", "\n"); + if (0 == m_array.size()) { + m_statusMsg += QObject::tr("File empty\n"); + } + m_isFileLoaded = true; } return m_isFileLoaded; } @@ -117,11 +117,9 @@ void CsvParser::clear() { bool CsvParser::parseFile() { parseRecord(); - while (!m_isEof) - { - if (!skipEndline()) { + while (!m_isEof) { + if (!skipEndline()) appendStatusMsg(QObject::tr("malformed string")); - } m_currRow++; m_currCol = 1; parseRecord(); @@ -131,43 +129,36 @@ bool CsvParser::parseFile() { } void CsvParser::parseRecord() { - csvrow row; + CsvRow row; if (isComment()) { skipLine(); return; } - else { - do { - parseField(row); - getChar(m_ch); - } while (isSeparator(m_ch) && !m_isEof); + do { + parseField(row); + getChar(m_ch); + } while (isSeparator(m_ch) && !m_isEof); - if (!m_isEof) { - ungetChar(); - } - if (isEmptyRow(row)) { - row.clear(); - return; - } - m_table.push_back(row); - if (m_maxCols < row.size()) { - m_maxCols = row.size(); - } - m_currCol++; + if (!m_isEof) + ungetChar(); + if (isEmptyRow(row)) { + row.clear(); + return; } + m_table.push_back(row); + if (m_maxCols < row.size()) + m_maxCols = row.size(); + m_currCol++; } -void CsvParser::parseField(csvrow& row) { +void CsvParser::parseField(CsvRow& row) { QString field; peek(m_ch); - if (!isTerminator(m_ch)) - { - if (isQualifier(m_ch)) { + if (!isTerminator(m_ch)) { + if (isQualifier(m_ch)) parseQuoted(field); - } - else { - parseSimple(field); - } + else + parseSimple(field); } row.push_back(field); } @@ -175,14 +166,12 @@ void CsvParser::parseField(csvrow& row) { void CsvParser::parseSimple(QString &s) { QChar c; getChar(c); - while ((isText(c)) && (!m_isEof)) - { + while ((isText(c)) && (!m_isEof)) { s.append(c); getChar(c); } - if (!m_isEof) { + if (!m_isEof) ungetChar(); - } } void CsvParser::parseQuoted(QString &s) { @@ -190,25 +179,21 @@ void CsvParser::parseQuoted(QString &s) { getChar(m_ch); parseEscaped(s); //getChar(m_ch); - if (!isQualifier(m_ch)) { + if (!isQualifier(m_ch)) appendStatusMsg(QObject::tr("missing closing quote")); - } } void CsvParser::parseEscaped(QString &s) { parseEscapedText(s); - while (processEscapeMark(s, m_ch)) { + while (processEscapeMark(s, m_ch)) parseEscapedText(s); - } - if (!m_isEof) { + if (!m_isEof) ungetChar(); - } } void CsvParser::parseEscapedText(QString &s) { getChar(m_ch); - while ((!isQualifier(m_ch)) && !m_isEof) - { + while ((!isQualifier(m_ch)) && !m_isEof) { s.append(m_ch); getChar(m_ch); } @@ -218,30 +203,25 @@ bool CsvParser::processEscapeMark(QString &s, QChar c) { QChar buf; peek(buf); QChar c2; - //escape-character syntax, e.g. \" - if (true == m_isBackslashSyntax) - { + if (true == m_isBackslashSyntax) { + //escape-character syntax, e.g. \" if (c != '\\') { return false; } //consume (and append) second qualifier getChar(c2); - if (m_isEof){ + if (m_isEof) { c2='\\'; s.append('\\'); return false; - } - else { + } else { s.append(c2); return true; } - } - //double quote syntax, e.g. "" - else - { - if (!isQualifier(c)) { + } else { + //double quote syntax, e.g. "" + if (!isQualifier(c)) return false; - } peek(c2); if (!m_isEof) { //not EOF, can read one char if (isQualifier(c2)) { @@ -255,13 +235,12 @@ bool CsvParser::processEscapeMark(QString &s, QChar c) { } void CsvParser::fillColumns() { - //fill the rows with lesser columns with empty fields - - for (int i=0; i 0) { - csvrow r = m_table.at(i); - for (int j=0; j 100) + return m_statusMsg.section('\n', 0, 4) + .append("\n[...]\n").append(QObject::tr("More messages, skipped!")); return m_statusMsg; } diff --git a/src/core/CsvParser.h b/src/core/CsvParser.h index 77c6d36e..f7c043a3 100644 --- a/src/core/CsvParser.h +++ b/src/core/CsvParser.h @@ -23,8 +23,8 @@ #include #include -typedef QStringList csvrow; -typedef QList csvtable; +typedef QStringList CsvRow; +typedef QList CsvTable; class CsvParser { @@ -36,19 +36,19 @@ public: bool isFileLoaded(); //reparse the same buffer (device is not opened again) bool reparse(); - void setCodec(const QString s); - void setComment(const QChar c); - void setFieldSeparator(const QChar c); - void setTextQualifier(const QChar c); + void setCodec(const QString &s); + void setComment(const QChar &c); + void setFieldSeparator(const QChar &c); + void setTextQualifier(const QChar &c); void setBackslashSyntax(bool set); - int getFileSize() const; - int getCsvRows() const; - int getCsvCols() const; + int getFileSize() const; + int getCsvRows() const; + int getCsvCols() const; QString getStatus() const; - const csvtable getCsvTable() const; + const CsvTable getCsvTable() const; protected: - csvtable m_table; + CsvTable m_table; private: QByteArray m_array; @@ -72,22 +72,22 @@ private: void ungetChar(); void peek(QChar &c); void fillColumns(); - bool isTerminator(const QChar c) const; - bool isSeparator(const QChar c) const; - bool isQualifier(const QChar c) const; + bool isTerminator(const QChar &c) const; + bool isSeparator(const QChar &c) const; + bool isQualifier(const QChar &c) const; bool processEscapeMark(QString &s, QChar c); bool isText(QChar c) const; bool isComment(); - bool isCRLF(const QChar c) const; - bool isSpace(const QChar c) const; - bool isTab(const QChar c) const; - bool isEmptyRow(csvrow row) const; + bool isCRLF(const QChar &c) const; + bool isSpace(const QChar &c) const; + bool isTab(const QChar &c) const; + bool isEmptyRow(CsvRow row) const; bool parseFile(); void parseRecord(); - void parseField(csvrow& row); - void parseSimple(QString& s); - void parseQuoted(QString& s); - void parseEscaped(QString& s); + void parseField(CsvRow &row); + void parseSimple(QString &s); + void parseQuoted(QString &s); + void parseEscaped(QString &s); void parseEscapedText(QString &s); bool readFile(QFile *device); void reset(); diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 2a88d8a1..86319e91 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -196,6 +196,9 @@ DatabaseWidget::Mode DatabaseWidget::currentMode() const if (currentWidget() == nullptr) { return DatabaseWidget::None; } + else if (currentWidget() == m_csvImportWizard) { + return DatabaseWidget::ImportMode; + } else if (currentWidget() == m_mainWidget) { return DatabaseWidget::ViewMode; } diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index 87d14a18..651b9d34 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -61,6 +61,7 @@ public: enum Mode { None, + ImportMode, ViewMode, EditMode, LockedMode diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index aa862465..47edaf48 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -425,6 +425,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) break; } case DatabaseWidget::EditMode: + case DatabaseWidget::ImportMode: case DatabaseWidget::LockedMode: { const QList entryActions = m_ui->menuEntries->actions(); for (QAction* action : entryActions) { diff --git a/src/gui/csvImport/CsvImportWidget.cpp b/src/gui/csvImport/CsvImportWidget.cpp index 9e53d5af..1e3c81a8 100644 --- a/src/gui/csvImport/CsvImportWidget.cpp +++ b/src/gui/csvImport/CsvImportWidget.cpp @@ -17,15 +17,17 @@ #include "CsvImportWidget.h" #include "ui_CsvImportWidget.h" -#include "gui/MessageBox.h" #include #include +#include "gui/MessageBox.h" +#include "gui/MessageWidget.h" + //I wanted to make the CSV import GUI future-proof, so if one day you need entries //to have a new field, all you have to do is uncomment a row or two here, and the GUI will follow: //dynamic generation of comboBoxes, labels, placement and so on. Try it for immense fun! -const QStringList CsvImportWidget::m_columnheader = QStringList() +const QStringList CsvImportWidget::m_columnHeader = QStringList() << QObject::tr("Group") << QObject::tr("Title") << QObject::tr("Username") @@ -46,10 +48,7 @@ CsvImportWidget::CsvImportWidget(QWidget *parent) { m_ui->setupUi(this); - QFont font = m_ui->labelHeadline->font(); - font.setBold(true); - font.setPointSize(font.pointSize() + 2); - m_ui->labelHeadline->setFont(font); + m_ui->messageWidget->setHidden(true); m_ui->comboBoxCodec->addItems(QStringList() <<"UTF-8" <<"Windows-1252" <<"UTF-16" <<"UTF-16LE"); m_ui->comboBoxFieldSeparator->addItems(QStringList() <<"," <<";" <<"-" <<":" <<"."); @@ -59,10 +58,10 @@ CsvImportWidget::CsvImportWidget(QWidget *parent) m_ui->tableViewFields->setSelectionMode(QAbstractItemView::NoSelection); m_ui->tableViewFields->setFocusPolicy(Qt::NoFocus); - for (int i=0; isetFixedWidth(label->minimumSizeHint().width()); - font = label->font(); + QFont font = label->font(); font.setBold(false); label->setFont(font); @@ -76,14 +75,14 @@ CsvImportWidget::CsvImportWidget(QWidget *parent) connect(combo, SIGNAL(currentIndexChanged(int)), m_comboMapper, SLOT(map())); //layout labels and combo fields in column-first order - int combo_rows = 1+(m_columnheader.count()-1)/2; - int x=i%combo_rows; - int y= 2*(i/combo_rows); + int combo_rows = 1+(m_columnHeader.count()-1)/2; + int x = i%combo_rows; + int y = 2*(i/combo_rows); m_ui->gridLayout_combos->addWidget(label, x, y); m_ui->gridLayout_combos->addWidget(combo, x, y+1); } - m_parserModel->setHeaderLabels(m_columnheader); + m_parserModel->setHeaderLabels(m_columnHeader); m_ui->tableViewFields->setModel(m_parserModel); connect(m_ui->spinBoxSkip, SIGNAL(valueChanged(int)), SLOT(skippedChanged(int))); @@ -101,10 +100,9 @@ CsvImportWidget::CsvImportWidget(QWidget *parent) void CsvImportWidget::comboChanged(int comboId) { QComboBox* currentSender = qobject_cast(m_comboMapper->mapping(comboId)); - if (currentSender->currentIndex() != -1) { - //here is the line that actually updates the GUI table + if (currentSender->currentIndex() != -1) + //this line is the one that actually updates GUI table m_parserModel->mapColumns(currentSender->currentIndex(), comboId); - } updateTableview(); } @@ -127,7 +125,7 @@ void CsvImportWidget::updateTableview() { m_ui->tableViewFields->resizeRowsToContents(); m_ui->tableViewFields->resizeColumnsToContents(); - for (int c=0; ctableViewFields->horizontalHeader()->count(); ++c) { + for (int c = 0; c < m_ui->tableViewFields->horizontalHeader()->count(); ++c) { m_ui->tableViewFields->horizontalHeader()->setSectionResizeMode( c, QHeaderView::Stretch); } @@ -137,12 +135,12 @@ void CsvImportWidget::updatePreview() { m_ui->labelSizeRowsCols->setText(m_parserModel->getFileInfo()); m_ui->spinBoxSkip->setValue(0); - m_ui->spinBoxSkip->setMaximum(m_parserModel->rowCount()-1); + m_ui->spinBoxSkip->setMaximum(m_parserModel->rowCount() - 1); int i; QStringList list(tr("Not present in CSV file")); - for (i=1; igetCsvCols(); i++) { + for (i = 1; i < m_parserModel->getCsvCols(); i++) { QString s = QString(tr("Column ")) + QString::number(i); list << s; } @@ -150,12 +148,10 @@ void CsvImportWidget::updatePreview() { i=1; Q_FOREACH (QComboBox* b, m_combos) { - if (i < m_parserModel->getCsvCols()) { + if (i < m_parserModel->getCsvCols()) b->setCurrentIndex(i); - } - else { + else b->setCurrentIndex(0); - } ++i; } } @@ -166,7 +162,7 @@ void CsvImportWidget::load(const QString& filename, Database* const db) { m_ui->labelFilename->setText(filename); Group* group = m_db->rootGroup(); group->setUuid(Uuid::random()); - group->setNotes(tr("Imported from CSV file\nOriginal data: ") + filename); + group->setNotes(tr("Imported from CSV file").append("\n").append(tr("Original data: ")) + filename); parse(); } @@ -181,47 +177,48 @@ void CsvImportWidget::parse() { } void CsvImportWidget::showReport() { - MessageBox::warning(this, tr("Syntax error"), tr("While parsing file...\n") - .append(m_parserModel->getStatus()), QMessageBox::Ok, QMessageBox::Ok); +// MessageBox::warning(this, tr("Syntax error"), tr("While parsing file...\n") +// .append(m_parserModel->getStatus()), QMessageBox::Ok, QMessageBox::Ok); + m_ui->messageWidget->showMessage(tr("Syntax error while parsing file.").append("\n") + .append(m_parserModel->getStatus()), MessageWidget::Warning); } void CsvImportWidget::writeDatabase() { - checkGroupNames(); - for (int r=0; rrowCount(); r++) { + setRootGroup(); + for (int r = 0; r < m_parserModel->rowCount(); r++) //use the validity of second column as a GO/NOGO hint for all others fields if (m_parserModel->data(m_parserModel->index(r, 1)).isValid()) { Entry* entry = new Entry(); entry->setUuid(Uuid::random()); entry->setGroup(splitGroups(m_parserModel->data(m_parserModel->index(r, 0)).toString())); - entry->setTitle( m_parserModel->data(m_parserModel->index(r, 1)).toString()); - entry->setUsername( m_parserModel->data(m_parserModel->index(r, 2)).toString()); - entry->setPassword( m_parserModel->data(m_parserModel->index(r, 3)).toString()); - entry->setUrl( m_parserModel->data(m_parserModel->index(r, 4)).toString()); - entry->setNotes( m_parserModel->data(m_parserModel->index(r, 5)).toString()); + entry->setTitle(m_parserModel->data(m_parserModel->index(r, 1)).toString()); + entry->setUsername(m_parserModel->data(m_parserModel->index(r, 2)).toString()); + entry->setPassword(m_parserModel->data(m_parserModel->index(r, 3)).toString()); + entry->setUrl(m_parserModel->data(m_parserModel->index(r, 4)).toString()); + entry->setNotes(m_parserModel->data(m_parserModel->index(r, 5)).toString()); } - } QBuffer buffer; buffer.open(QBuffer::ReadWrite); KeePass2Writer writer; writer.writeDatabase(&buffer, m_db); - if (writer.hasError()) { + if (writer.hasError()) MessageBox::warning(this, tr("Error"), tr("CSV import: writer has errors:\n") .append((writer.errorString())), QMessageBox::Ok, QMessageBox::Ok); - } Q_EMIT editFinished(true); } -void CsvImportWidget::checkGroupNames() { +void CsvImportWidget::setRootGroup() { QString groupLabel; QStringList groupList; - bool is_root = false - , is_empty = false - , is_label = false; - for (int r=0; rrowCount(); r++) { + bool is_root = false; + bool is_empty = false; + bool is_label = false; + + for (int r = 0; r < m_parserModel->rowCount(); r++) { groupLabel = m_parserModel->data(m_parserModel->index(r, 0)).toString(); //check if group name is either "root", "" (empty) or some other label groupList = groupLabel.split("/", QString::SkipEmptyParts); @@ -234,18 +231,13 @@ void CsvImportWidget::checkGroupNames() { groupList.clear(); } - if ((not is_label and (is_empty xor is_root)) - or (is_label and not is_root)) { + if ((not is_label and (is_empty xor is_root)) or (is_label and not is_root)) m_db->rootGroup()->setName("Root"); - } - else if ((is_empty and is_root) - or (is_label and not is_empty and is_root)) { + else if ((is_empty and is_root) or (is_label and not is_empty and is_root)) m_db->rootGroup()->setName("CSV IMPORTED"); - } - else { + else //SHOULD NEVER GET HERE m_db->rootGroup()->setName("ROOT_FALLBACK"); - } } Group *CsvImportWidget::splitGroups(QString label) { @@ -254,9 +246,8 @@ Group *CsvImportWidget::splitGroups(QString label) { QStringList groupList = label.split("/", QString::SkipEmptyParts); //skip the creation of a subgroup of Root with the same name - if (m_db->rootGroup()->name() == "Root" && groupList.first() == "Root") { + if (m_db->rootGroup()->name() == "Root" && groupList.first() == "Root") groupList.removeFirst(); - } for (const QString& groupName : groupList) { Group *children = hasChildren(current, groupName); @@ -265,8 +256,7 @@ Group *CsvImportWidget::splitGroups(QString label) { brandNew->setParent(current); brandNew->setName(groupName); current = brandNew; - } - else { + } else { Q_ASSERT(children != nullptr); current = children; } @@ -277,9 +267,8 @@ Group *CsvImportWidget::splitGroups(QString label) { Group* CsvImportWidget::hasChildren(Group* current, QString groupName) { //returns the group whose name is "groupName" and is child of "current" group for (Group * group : current->children()) { - if (group->name() == groupName) { + if (group->name() == groupName) return group; - } } return nullptr; } diff --git a/src/gui/csvImport/CsvImportWidget.h b/src/gui/csvImport/CsvImportWidget.h index f8798b56..1a71924e 100644 --- a/src/gui/csvImport/CsvImportWidget.h +++ b/src/gui/csvImport/CsvImportWidget.h @@ -26,10 +26,10 @@ #include #include +#include "core/Metadata.h" #include "format/KeePass2Writer.h" #include "gui/csvImport/CsvParserModel.h" #include "keys/PasswordKey.h" -#include "core/Metadata.h" namespace Ui { @@ -42,7 +42,7 @@ class CsvImportWidget : public QWidget public: explicit CsvImportWidget(QWidget *parent = nullptr); - virtual ~CsvImportWidget(); + ~CsvImportWidget(); void load(const QString& filename, Database* const db); Q_SIGNALS: @@ -54,7 +54,7 @@ private Q_SLOTS: void comboChanged(int comboId); void skippedChanged(int rows); void writeDatabase(); - void checkGroupNames(); + void setRootGroup(); void reject(); private: @@ -67,7 +67,7 @@ private: Database *m_db; KeePass2Writer m_writer; - static const QStringList m_columnheader; + static const QStringList m_columnHeader; void configParser(); void updatePreview(); void updateTableview(); diff --git a/src/gui/csvImport/CsvImportWidget.ui b/src/gui/csvImport/CsvImportWidget.ui index 5df2aa1a..8816c577 100644 --- a/src/gui/csvImport/CsvImportWidget.ui +++ b/src/gui/csvImport/CsvImportWidget.ui @@ -14,7 +14,33 @@ - + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 758 + 24 + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + false + + + + @@ -377,17 +403,7 @@ - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - false - - - - + @@ -409,50 +425,7 @@ - - - - - - - 0 - 0 - - - - Import CSV fields - - - - - - - - 0 - 0 - - - - filename - - - - - - - - 0 - 0 - - - - size, rows, columns - - - - - - + @@ -501,24 +474,69 @@ - - - - Qt::Vertical - - - QSizePolicy::Fixed - - - - 20 - 27 - - - + + + + + + + + + + 0 + 0 + + + + + 11 + 75 + true + + + + Import CSV fields + + + + + + + + 0 + 0 + + + + filename + + + + + + + + 0 + 0 + + + + size, rows, columns + + + + + + + MessageWidget + QWidget +
gui/MessageWidget.h
+ 1 +
+
diff --git a/src/gui/csvImport/CsvImportWizard.cpp b/src/gui/csvImport/CsvImportWizard.cpp index f84a2295..0114fcf3 100644 --- a/src/gui/csvImport/CsvImportWizard.cpp +++ b/src/gui/csvImport/CsvImportWizard.cpp @@ -16,8 +16,10 @@ */ #include "CsvImportWizard.h" + #include #include + #include "gui/MessageBox.h" @@ -49,7 +51,7 @@ void CsvImportWizard::load(const QString& filename, Database* database) void CsvImportWizard::keyFinished(bool accepted) { if (!accepted) { - Q_EMIT(importFinished(false)); + emit(importFinished(false)); return; } @@ -61,12 +63,12 @@ void CsvImportWizard::keyFinished(bool accepted) if (!result) { MessageBox::critical(this, tr("Error"), tr("Unable to calculate master key")); - Q_EMIT importFinished(false); + emit(importFinished(false)); return; } } void CsvImportWizard::parseFinished(bool accepted) { - Q_EMIT(importFinished(accepted)); + emit(importFinished(accepted)); } diff --git a/src/gui/csvImport/CsvImportWizard.h b/src/gui/csvImport/CsvImportWizard.h index 5a83a19d..75d10bb9 100644 --- a/src/gui/csvImport/CsvImportWizard.h +++ b/src/gui/csvImport/CsvImportWizard.h @@ -18,10 +18,11 @@ #ifndef KEEPASSX_CSVIMPORTWIZARD_H #define KEEPASSX_CSVIMPORTWIZARD_H +#include "CsvImportWidget.h" + #include #include -#include "CsvImportWidget.h" #include "core/Database.h" #include "gui/ChangeMasterKeyWidget.h" #include "gui/DialogyWidget.h" @@ -34,7 +35,7 @@ class CsvImportWizard : public DialogyWidget public: explicit CsvImportWizard(QWidget *parent = nullptr); - virtual ~CsvImportWizard(); + ~CsvImportWizard(); void load(const QString& filename, Database *database); Q_SIGNALS: diff --git a/src/gui/csvImport/CsvParserModel.cpp b/src/gui/csvImport/CsvParserModel.cpp index 3bc6c834..ba5d20d9 100644 --- a/src/gui/csvImport/CsvParserModel.cpp +++ b/src/gui/csvImport/CsvParserModel.cpp @@ -42,39 +42,33 @@ bool CsvParserModel::parse() { m_columnMap.clear(); if (CsvParser::isFileLoaded()) { r = CsvParser::reparse(); - } - else { + } else { QFile csv(m_filename); r = CsvParser::parse(&csv); } - for (int i=0; i= getCsvCols()) { + if (csvColumn >= getCsvCols()) m_columnMap[dbColumn] = 0; //map to the empty column - } - else { + else m_columnMap[dbColumn] = csvColumn; - } endResetModel(); } @@ -82,8 +76,8 @@ void CsvParserModel::setSkippedRows(int skipped) { m_skipped = skipped; QModelIndex topLeft = createIndex(skipped,0); QModelIndex bottomRight = createIndex(m_skipped+rowCount(), columnCount()); - Q_EMIT dataChanged(topLeft, bottomRight); - Q_EMIT layoutChanged(); + emit dataChanged(topLeft, bottomRight); + emit layoutChanged(); } void CsvParserModel::setHeaderLabels(QStringList l) { @@ -91,45 +85,37 @@ void CsvParserModel::setHeaderLabels(QStringList l) { } int CsvParserModel::rowCount(const QModelIndex &parent) const { - if (parent.isValid()) { + if (parent.isValid()) return 0; - } return getCsvRows(); } int CsvParserModel::columnCount(const QModelIndex &parent) const { - if (parent.isValid()) { + if (parent.isValid()) return 0; - } return m_columnHeader.size(); } QVariant CsvParserModel::data(const QModelIndex &index, int role) const { - if ( (index.column() >= m_columnHeader.size()) + if ((index.column() >= m_columnHeader.size()) || (index.row()+m_skipped >= rowCount()) - || !index.isValid() ) - { + || !index.isValid()) { return QVariant(); } - - if (role == Qt::DisplayRole) { + if (role == Qt::DisplayRole) return m_table.at(index.row()+m_skipped).at(m_columnMap[index.column()]); - } return QVariant(); } QVariant CsvParserModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::DisplayRole) { if (orientation == Qt::Horizontal) { - if ( (section < 0) || (section >= m_columnHeader.size())) { + if ((section < 0) || (section >= m_columnHeader.size())) return QVariant(); - } return m_columnHeader.at(section); - } - else if (orientation == Qt::Vertical) { - if (section+m_skipped >= rowCount()) { + } else if (orientation == Qt::Vertical) { + if (section+m_skipped >= rowCount()) return QVariant(); - } return QString::number(section+1); } } diff --git a/src/gui/csvImport/CsvParserModel.h b/src/gui/csvImport/CsvParserModel.h index ae7bf6e2..ff4f410d 100644 --- a/src/gui/csvImport/CsvParserModel.h +++ b/src/gui/csvImport/CsvParserModel.h @@ -20,8 +20,9 @@ #include #include -#include "core/Group.h" + #include "core/CsvParser.h" +#include "core/Group.h" class CsvParserModel : public QAbstractTableModel, public CsvParser { @@ -29,7 +30,7 @@ class CsvParserModel : public QAbstractTableModel, public CsvParser public: explicit CsvParserModel(QObject *parent = nullptr); - virtual ~CsvParserModel(); + ~CsvParserModel(); void setFilename(const QString& filename); QString getFileInfo(); bool parse(); @@ -37,10 +38,10 @@ public: void setHeaderLabels(QStringList l); void mapColumns(int csvColumn, int dbColumn); - virtual int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - virtual int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; - virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; public Q_SLOTS: void setSkippedRows(int skipped); diff --git a/tests/TestCsvParser.h b/tests/TestCsvParser.h index efdd96ab..975d86a9 100644 --- a/tests/TestCsvParser.h +++ b/tests/TestCsvParser.h @@ -62,8 +62,8 @@ private Q_SLOTS: private: QFile file; CsvParser* parser; - csvtable t; - void dumpRow(csvtable table, int row); + CsvTable t; + void dumpRow(CsvTable table, int row); }; #endif // KEEPASSX_TESTCSVPARSER_H