diff --git a/gui/fielddelegate.h b/gui/fielddelegate.h index afb0978..b90a3d5 100644 --- a/gui/fielddelegate.h +++ b/gui/fielddelegate.h @@ -9,7 +9,7 @@ class FieldDelegate : public QStyledItemDelegate { public: FieldDelegate(QObject *parent = nullptr); - void setEditorData(QWidget *editor, const QModelIndex &index) const; + void setEditorData(QWidget *editor, const QModelIndex &index) const override; }; } // namespace QtGui diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 8e4a78d..5ebf07c 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -440,13 +440,11 @@ bool MainWindow::openFile(const QString &path, bool readOnly) } // warn before loading a very big file - if (m_file.size() > 10485760) { - if (QMessageBox::warning(this, QApplication::applicationName(), + if (m_file.size() > 10485760 && QMessageBox::warning(this, QApplication::applicationName(), tr("The file you want to load seems to be very big. Do you really want to open it?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) { - m_file.clear(); - return false; - } + m_file.clear(); + return false; } // ask for a password if required @@ -488,18 +486,17 @@ bool MainWindow::openFile(const QString &path, bool readOnly) } // show a message in the error case - if (!msg.isEmpty()) { - m_file.clear(); - m_ui->statusBar->showMessage(msg, 5000); - if (QMessageBox::critical(this, QApplication::applicationName(), msg, QMessageBox::Cancel, QMessageBox::Retry) == QMessageBox::Retry) { - return openFile(path, readOnly); // retry - } else { - return false; - } - } else { + if (msg.isEmpty()) { // show contents return showFile(); } + m_file.clear(); + m_ui->statusBar->showMessage(msg, 5000); + if (QMessageBox::critical(this, QApplication::applicationName(), msg, QMessageBox::Cancel, QMessageBox::Retry) == QMessageBox::Retry) { + return openFile(path, readOnly); // retry + } else { + return false; + } } /*! @@ -618,7 +615,7 @@ void MainWindow::updateWindowTitle() void MainWindow::applyDefaultExpanding(const QModelIndex &parent) { for (int row = 0, rows = m_entryFilterModel->rowCount(parent); row < rows; ++row) { - QModelIndex index = m_entryFilterModel->index(row, 0, parent); + const QModelIndex index = m_entryFilterModel->index(row, 0, parent); if (!index.isValid()) { return; } @@ -735,7 +732,7 @@ bool MainWindow::askForCreatingFile() /*! * \brief Shows an warning if no file is opened. - * \retruns Returns whether the warning has been shown. + * \returns Returns whether the warning has been shown. */ bool MainWindow::showNoFileOpened() { @@ -748,7 +745,7 @@ bool MainWindow::showNoFileOpened() /*! * \brief Shows an warning if no account is selected. - * \retruns Returns whether the warning has been shown. + * \returns Returns whether the warning has been shown. */ bool MainWindow::showNoAccount() { @@ -821,10 +818,8 @@ bool MainWindow::saveFile() return false; } } - } else { - if (!askForCreatingFile()) { - return false; - } + } else if (!askForCreatingFile()) { + return false; } // ask for a password if none is set @@ -858,16 +853,15 @@ bool MainWindow::saveFile() m_ui->statusBar->showMessage(msg, 5000); QMessageBox::critical(this, QApplication::applicationName(), msg); return false; - } else { - if (m_readOnly || m_somethingChanged) { - m_readOnly = false; - m_somethingChanged = false; - updateWindowTitle(); - } - m_recentMgr->addEntry(QString::fromStdString(m_file.path())); - m_ui->statusBar->showMessage(tr("The password list has been saved."), 5000); - return true; } + if (m_readOnly || m_somethingChanged) { + m_readOnly = false; + m_somethingChanged = false; + updateWindowTitle(); + } + m_recentMgr->addEntry(QString::fromStdString(m_file.path())); + m_ui->statusBar->showMessage(tr("The password list has been saved."), 5000); + return true; } /*! @@ -904,13 +898,14 @@ void MainWindow::showContainingDirectory() { if (showNoFileOpened()) { return; - } else if (m_file.path().empty()) { + } + if (m_file.path().empty()) { QMessageBox::warning(this, QApplication::applicationName(), tr("The currently opened file hasn't been saved yet.")); - } else { - const QFileInfo file(QString::fromStdString(m_file.path())); - if (file.dir().exists()) { - DesktopUtils::openLocalFileOrDir(file.dir().absolutePath()); - } + return; + } + const QFileInfo file(QString::fromStdString(m_file.path())); + if (file.dir().exists()) { + DesktopUtils::openLocalFileOrDir(file.dir().absolutePath()); } } @@ -941,30 +936,29 @@ void MainWindow::addEntry(EntryType type) return; } const QModelIndexList selectedIndexes = m_ui->treeView->selectionModel()->selectedRows(0); - if (selectedIndexes.size() == 1) { - const QModelIndex selected = m_entryFilterModel->mapToSource(selectedIndexes.at(0)); - if (m_entryModel->isNode(selected)) { - bool result; - const QString text = QInputDialog::getText(this, type == EntryType::Account ? tr("Add account") : tr("Add category"), - tr("Enter the entry name"), QLineEdit::Normal, tr("new entry"), &result); - if (result) { - if (!text.isEmpty()) { - int row = m_entryModel->rowCount(selected); - m_entryModel->setInsertType(type); - if (m_entryModel->insertRow(row, selected)) { - m_entryModel->setData(m_entryModel->index(row, 0, selected), text, Qt::DisplayRole); - setSomethingChanged(true); - } else { - QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to create new entry.")); - } - } else { - QMessageBox::warning(this, QApplication::applicationName(), tr("You didn't enter text.")); - } - } - return; - } + const QModelIndex selected = selectedIndexes.size() == 1 ? m_entryFilterModel->mapToSource(selectedIndexes.at(0)) : QModelIndex(); + if (!selected.isValid() || !m_entryModel->isNode(selected)) { + QMessageBox::warning(this, QApplication::applicationName(), tr("No node element selected.")); + return; } - QMessageBox::warning(this, QApplication::applicationName(), tr("No node element selected.")); + bool result; + const QString text = QInputDialog::getText(this, type == EntryType::Account ? tr("Add account") : tr("Add category"), + tr("Enter the entry name"), QLineEdit::Normal, tr("new entry"), &result); + if (!result) { + return; + } + if (text.isEmpty()) { + QMessageBox::warning(this, QApplication::applicationName(), tr("You didn't enter text.")); + return; + } + int row = m_entryModel->rowCount(selected); + m_entryModel->setInsertType(type); + if (!m_entryModel->insertRow(row, selected)) { + QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to create new entry.")); + return; + } + m_entryModel->setData(m_entryModel->index(row, 0, selected), text, Qt::DisplayRole); + setSomethingChanged(true); } /*! @@ -976,13 +970,13 @@ void MainWindow::removeEntry() return; } const QModelIndexList selectedIndexes = m_ui->treeView->selectionModel()->selectedRows(0); - if (selectedIndexes.size() == 1) { - const QModelIndex selected = m_entryFilterModel->mapToSource(selectedIndexes.at(0)); - if (!m_entryModel->removeRow(selected.row(), selected.parent())) { - QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to remove the entry.")); - } - } else { + if (selectedIndexes.size() != 1) { QMessageBox::warning(this, QApplication::applicationName(), tr("No entry selected.")); + return; + } + const QModelIndex selected = m_entryFilterModel->mapToSource(selectedIndexes.at(0)); + if (!m_entryModel->removeRow(selected.row(), selected.parent())) { + QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to remove the entry.")); } } @@ -1023,19 +1017,19 @@ void MainWindow::insertRow() return; } const QModelIndexList selectedIndexes = m_ui->tableView->selectionModel()->selectedIndexes(); - if (selectedIndexes.size()) { - int row = m_fieldModel->rowCount(); - for (const QModelIndex &index : selectedIndexes) { - if (index.row() < row) { - row = index.row(); - } - } - if (row < m_fieldModel->rowCount() - 1) { - m_fieldModel->insertRow(row); - } - } else { + if (selectedIndexes.empty()) { QMessageBox::warning( this, windowTitle(), tr("A field has to be selected since new fields are always inserted before the currently selected field.")); + return; + } + int row = m_fieldModel->rowCount(); + for (const QModelIndex &index : selectedIndexes) { + if (index.row() < row) { + row = index.row(); + } + } + if (row < m_fieldModel->rowCount() - 1) { + m_fieldModel->insertRow(row); } } @@ -1052,14 +1046,14 @@ void MainWindow::removeRows() for (const QModelIndex &index : selectedIndexes) { rows << index.row(); } - if (rows.size()) { - for (int i = m_fieldModel->rowCount() - 1; i >= 0; --i) { - if (rows.contains(i)) { - m_fieldModel->removeRow(i); - } - } - } else { + if (rows.empty()) { QMessageBox::warning(this, windowTitle(), tr("No fields have been removed since there are currently no fields selected.")); + return; + } + for (int i = m_fieldModel->rowCount() - 1; i >= 0; --i) { + if (rows.contains(i)) { + m_fieldModel->removeRow(i); + } } } @@ -1088,13 +1082,13 @@ void MainWindow::setFieldType(FieldType fieldType) return; } const QModelIndexList selectedIndexes = m_ui->tableView->selectionModel()->selectedIndexes(); - if (!selectedIndexes.isEmpty()) { - const QVariant typeVariant(static_cast(fieldType)); - for (const QModelIndex &index : selectedIndexes) { - m_fieldModel->setData(index, typeVariant, FieldTypeRole); - } - } else { + if (selectedIndexes.isEmpty()) { QMessageBox::warning(this, windowTitle(), tr("No fields have been changed since there are currently no fields selected.")); + return; + } + const QVariant typeVariant(static_cast(fieldType)); + for (const QModelIndex &index : selectedIndexes) { + m_fieldModel->setData(index, typeVariant, FieldTypeRole); } } @@ -1154,28 +1148,29 @@ void MainWindow::showTreeViewContextMenu() return; } const QModelIndexList selectedIndexes = m_ui->treeView->selectionModel()->selectedRows(0); - if (selectedIndexes.size() == 1) { - QMenu contextMenu(this); - QModelIndex selected = m_entryFilterModel->mapToSource(selectedIndexes.at(0)); - Entry *entry = m_entryModel->entry(selected); - if (entry->type() == EntryType::Node) { - contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), tr("Add account"), this, &MainWindow::addAccount); - contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), tr("Add category"), this, &MainWindow::addCategory); - } - contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), tr("Remove entry"), this, &MainWindow::removeEntry); - if (entry->type() == EntryType::Node) { - auto *nodeEntry = static_cast(entry); - contextMenu.addSeparator(); - auto *action = new QAction(&contextMenu); - action->setCheckable(true); - action->setText(tr("Expanded by default")); - action->setChecked(nodeEntry->isExpandedByDefault()); - connect(action, &QAction::triggered, - std::bind(&EntryModel::setData, m_entryModel, std::cref(selected), QVariant(!nodeEntry->isExpandedByDefault()), DefaultExpandedRole)); - contextMenu.addAction(action); - } - contextMenu.exec(QCursor::pos()); + if (selectedIndexes.size() != 1) { + return; } + QMenu contextMenu(this); + const QModelIndex selected(m_entryFilterModel->mapToSource(selectedIndexes.at(0))); + const Entry *const entry = m_entryModel->entry(selected); + if (entry->type() == EntryType::Node) { + contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), tr("Add account"), this, &MainWindow::addAccount); + contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), tr("Add category"), this, &MainWindow::addCategory); + } + contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), tr("Remove entry"), this, &MainWindow::removeEntry); + if (entry->type() == EntryType::Node) { + const auto *const nodeEntry = static_cast(entry); + contextMenu.addSeparator(); + auto *const action = new QAction(&contextMenu); + action->setCheckable(true); + action->setText(tr("Expanded by default")); + action->setChecked(nodeEntry->isExpandedByDefault()); + connect(action, &QAction::triggered, + std::bind(&EntryModel::setData, m_entryModel, std::cref(selected), QVariant(!nodeEntry->isExpandedByDefault()), DefaultExpandedRole)); + contextMenu.addAction(action); + } + contextMenu.exec(QCursor::pos()); } /*! diff --git a/gui/mainwindow.h b/gui/mainwindow.h index c9b0b95..934f79c 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -49,7 +49,7 @@ class MainWindow : public QMainWindow { public: explicit MainWindow(QSettings &settings, Dialogs::QtSettings *qtSettings = nullptr, QWidget *parent = nullptr); - ~MainWindow(); + ~MainWindow() override; public slots: // file management @@ -71,9 +71,9 @@ public slots: void showUndoView(); protected: - bool eventFilter(QObject *obj, QEvent *event); - void closeEvent(QCloseEvent *event); - void timerEvent(QTimerEvent *event); + bool eventFilter(QObject *obj, QEvent *event) override; + void closeEvent(QCloseEvent *event) override; + void timerEvent(QTimerEvent *event) override; private slots: // file management diff --git a/gui/passwordgeneratordialog.cpp b/gui/passwordgeneratordialog.cpp index af1f35b..5b680d8 100644 --- a/gui/passwordgeneratordialog.cpp +++ b/gui/passwordgeneratordialog.cpp @@ -70,7 +70,6 @@ PasswordGeneratorDialog::PasswordGeneratorDialog(QWidget *parent) */ PasswordGeneratorDialog::~PasswordGeneratorDialog() { - delete m_ui; } /*! diff --git a/gui/passwordgeneratordialog.h b/gui/passwordgeneratordialog.h index 2e0633f..0c93ee4 100644 --- a/gui/passwordgeneratordialog.h +++ b/gui/passwordgeneratordialog.h @@ -6,6 +6,7 @@ #include #include +#include namespace QtGui { @@ -18,7 +19,7 @@ class PasswordGeneratorDialog : public QDialog { public: explicit PasswordGeneratorDialog(QWidget *parent = nullptr); - ~PasswordGeneratorDialog(); + ~PasswordGeneratorDialog() override; private Q_SLOTS: void generateNewPassword(); @@ -27,7 +28,7 @@ private Q_SLOTS: void copyPassword(); private: - Ui::PasswordGeneratorDialog *m_ui; + std::unique_ptr m_ui; std::vector m_charset; Util::OpenSslRandomDevice m_random; }; diff --git a/gui/stacksupport.h b/gui/stacksupport.h index 41d96f9..844a4dd 100644 --- a/gui/stacksupport.h +++ b/gui/stacksupport.h @@ -37,15 +37,14 @@ inline QUndoStack *StackSupport::undoStack() */ inline bool StackSupport::push(CustomUndoCommand *command) { - if (m_undoStack) { - if (command->isNoop()) { - return true; // doing nothing can never fail - } else { - m_undoStack->push(command); - return command->redoResult(); - } + if (!m_undoStack) { + return false; } - return false; + if (command->isNoop()) { + return true; // doing nothing can never fail + } + m_undoStack->push(command); + return command->redoResult(); } /*! diff --git a/gui/undocommands.cpp b/gui/undocommands.cpp index 7f77a9e..9620325 100644 --- a/gui/undocommands.cpp +++ b/gui/undocommands.cpp @@ -132,7 +132,7 @@ FieldModelInsertRowsCommand::FieldModelInsertRowsCommand(FieldModel *model, int , m_row(row) , m_count(count) { - setText(QApplication::translate("undocommands", "insertion of %1 row(s) before row %2", 0, count).arg(count).arg(row + 1)); + setText(QApplication::translate("undocommands", "insertion of %1 row(s) before row %2", nullptr, count).arg(count).arg(row + 1)); } bool FieldModelInsertRowsCommand::internalRedo() @@ -163,9 +163,9 @@ FieldModelRemoveRowsCommand::FieldModelRemoveRowsCommand(FieldModel *model, int , m_count(count) { if (count == 1) { - setText(QApplication::translate("undocommands", "removal of row %1", 0, count).arg(row + 1)); + setText(QApplication::translate("undocommands", "removal of row %1", nullptr, count).arg(row + 1)); } else { - setText(QApplication::translate("undocommands", "removal of the rows %1 to %2", 0, count).arg(row + 1).arg(row + count)); + setText(QApplication::translate("undocommands", "removal of the rows %1 to %2", nullptr, count).arg(row + 1).arg(row + count)); } } diff --git a/gui/undocommands.h b/gui/undocommands.h index bcd240c..b200626 100644 --- a/gui/undocommands.h +++ b/gui/undocommands.h @@ -27,8 +27,8 @@ public: bool redoResult() const; bool undoResult() const; bool isNoop() const; - void redo(); - void undo(); + void redo() override; + void undo() override; protected: void setNoop(bool noop); @@ -81,8 +81,8 @@ public: explicit FieldModelSetValueCommand(FieldModel *model, const QModelIndex &index, const QVariant &value, int role); protected: - bool internalRedo(); - bool internalUndo(); + bool internalRedo() override; + bool internalUndo() override; private: Io::AccountEntry *m_account; @@ -99,8 +99,8 @@ public: explicit FieldModelInsertRowsCommand(FieldModel *model, int row, int count); protected: - bool internalRedo(); - bool internalUndo(); + bool internalRedo() override; + bool internalUndo() override; private: Io::AccountEntry *m_account; @@ -114,8 +114,8 @@ public: explicit FieldModelRemoveRowsCommand(FieldModel *model, int row, int count); protected: - bool internalRedo(); - bool internalUndo(); + bool internalRedo() override; + bool internalUndo() override; private: Io::AccountEntry *m_account; @@ -130,8 +130,8 @@ public: explicit EntryModelSetValueCommand(EntryModel *model, const QModelIndex &index, const QVariant &value, int role); protected: - bool internalRedo(); - bool internalUndo(); + bool internalRedo() override; + bool internalUndo() override; private: EntryModel *m_model; @@ -143,12 +143,10 @@ private: class EntryModelModifyRowsCommand : public CustomUndoCommand { public: - ~EntryModelModifyRowsCommand(); + ~EntryModelModifyRowsCommand() override; protected: explicit EntryModelModifyRowsCommand(EntryModel *model, int row, int count, const QModelIndex &parent); - bool internalRedo() = 0; - bool internalUndo() = 0; bool insert(); bool remove(); EntryModel *m_model; @@ -163,8 +161,8 @@ public: explicit EntryModelInsertRowsCommand(EntryModel *model, int row, int count, const QModelIndex &parent); protected: - bool internalRedo(); - bool internalUndo(); + bool internalRedo() override; + bool internalUndo() override; }; class EntryModelRemoveRowsCommand : public EntryModelModifyRowsCommand { @@ -172,8 +170,8 @@ public: explicit EntryModelRemoveRowsCommand(EntryModel *model, int row, int count, const QModelIndex &parent); protected: - bool internalRedo(); - bool internalUndo(); + bool internalRedo() override; + bool internalUndo() override; }; class EntryModelMoveRowsCommand : public CustomUndoCommand { @@ -182,8 +180,8 @@ public: EntryModel *model, const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild); protected: - bool internalRedo(); - bool internalUndo(); + bool internalRedo() override; + bool internalUndo() override; private: EntryModel *m_model; diff --git a/model/entrymodel.cpp b/model/entrymodel.cpp index d29cdba..d14844f 100644 --- a/model/entrymodel.cpp +++ b/model/entrymodel.cpp @@ -61,11 +61,7 @@ EntryModel::EntryModel(QUndoStack *undoStack, QObject *parent) : */ Entry *EntryModel::entry(const QModelIndex &index) { - if(index.isValid()) { - return static_cast(index.internalPointer()); - } else { - return nullptr; - } + return index.isValid() ? static_cast(index.internalPointer()) : nullptr; } /*! @@ -78,24 +74,24 @@ Entry *EntryModel::entry(const QModelIndex &index) */ QList EntryModel::takeEntries(int row, int count, const QModelIndex &parent) { - QList res; - if(Entry *parentEntry = entry(parent)) { - if(parentEntry->type() == EntryType::Node) { - NodeEntry *parentNodeEntry = static_cast(parentEntry); - int lastIndex = row + count - 1; - const vector &children = parentNodeEntry->children(); - if(lastIndex < 0 || static_cast(lastIndex) >= children.size()) { - lastIndex = children.size() - 1; - } - beginRemoveRows(parent, row, lastIndex); - for(int index = lastIndex; index >= row; --index) { - Entry *child = children.at(index); - child->setParent(nullptr); - res << child; - } - endRemoveRows(); - } + Entry *const parentEntry = entry(parent); + if(!parentEntry || parentEntry->type() != EntryType::Node) { + return QList(); } + QList res; + NodeEntry *const parentNodeEntry = static_cast(parentEntry); + int lastIndex = row + count - 1; + const vector &children = parentNodeEntry->children(); + if(lastIndex < 0 || static_cast(lastIndex) >= children.size()) { + lastIndex = children.size() - 1; + } + beginRemoveRows(parent, row, lastIndex); + for(int index = lastIndex; index >= row; --index) { + Entry *const child = children[index]; + child->setParent(nullptr); + res << child; + } + endRemoveRows(); return res; } @@ -111,42 +107,45 @@ bool EntryModel::insertEntries(int row, const QModelIndex &parent, const QListtype() == EntryType::Node) { - NodeEntry *parentNodeEntry = static_cast(parentEntry); - const vector &children = parentNodeEntry->children(); - if(row < 0 || static_cast(row) > children.size()) { - row = children.size(); - } - beginInsertRows(parent, row, row + entries.size() - 1); - foreach(Entry *entry, entries) { - entry->setParent(parentNodeEntry, row); - ++row; - } - endInsertRows(); - return true; - } + Entry *const parentEntry = entry(parent); + if(!parentEntry || parentEntry->type() != EntryType::Node) { + return false; } - return false; + NodeEntry *const parentNodeEntry = static_cast(parentEntry); + const vector &children = parentNodeEntry->children(); + if(row < 0 || static_cast(row) > children.size()) { + row = children.size(); + } + beginInsertRows(parent, row, row + entries.size() - 1); + for(Entry *const entry : entries) { + entry->setParent(parentNodeEntry, row); + ++row; + } + endInsertRows(); + return true; } QModelIndex EntryModel::index(int row, int column, const QModelIndex &parent) const { - if(parent.isValid()) { - if(Entry *parentEntry = static_cast(parent.internalPointer())) { - switch(parentEntry->type()) { - case EntryType::Node: { - const std::vector &children = static_cast(parentEntry)->children(); - if(row >= 0 && static_cast(row) < children.size()) { - return createIndex(row, column, children.at(row)); - } - break; - } case EntryType::Account: - ; - } + if(!parent.isValid()) { + if(m_rootEntry && row == 0) { + return createIndex(row, column, m_rootEntry); } - } else if(m_rootEntry && row == 0) { - return createIndex(row, column, m_rootEntry); + return QModelIndex(); + } + const auto *const parentEntry = static_cast(parent.internalPointer()); + if(!parentEntry) { + return QModelIndex(); + } + switch(parentEntry->type()) { + case EntryType::Node: { + const std::vector &children = static_cast(parentEntry)->children(); + if(row >= 0 && static_cast(row) < children.size()) { + return createIndex(row, column, children[static_cast(row)]); + } + break; + } case EntryType::Account: + ; } return QModelIndex(); } @@ -166,29 +165,27 @@ QModelIndex EntryModel::index(Entry *entry) const QModelIndex EntryModel::parent(const QModelIndex &child) const { - if(child.isValid()) { - if(Entry *entry = static_cast(child.internalPointer())) { - NodeEntry *parent = entry->parent(); - if(parent && (child.row() >= 0 && static_cast(child.row()) < parent->children().size())) { - return createIndex(parent->index() > 0 ? parent->index() : 0, 0, parent); - } - } + if(!child.isValid()) { + return QModelIndex(); + } + const auto *const entry = static_cast(child.internalPointer()); + if(!entry) { + return QModelIndex(); + } + NodeEntry *const parent = entry->parent(); + if(parent && (child.row() >= 0 && static_cast(child.row()) < parent->children().size())) { + return createIndex(parent->index() > 0 ? parent->index() : 0, 0, parent); } return QModelIndex(); } bool EntryModel::hasChildren(const QModelIndex &parent) const { - if(parent.isValid()) { - if(Entry *entry = static_cast(parent.internalPointer())) { - if(entry->type() == EntryType::Node) { - return static_cast(entry)->children().size(); - } - } - } else { + if(!parent.isValid()) { return true; } - return false; + const auto *const entry = static_cast(parent.internalPointer()); + return entry && entry->type() == EntryType::Node && !static_cast(entry)->children().empty(); } /*! @@ -197,63 +194,65 @@ bool EntryModel::hasChildren(const QModelIndex &parent) const */ bool EntryModel::isNode(const QModelIndex &parent) const { - if(parent.isValid()) { - if(Entry *entry = static_cast(parent.internalPointer())) { - return entry->type() == EntryType::Node; - } + if(!parent.isValid()) { + return false; } - return false; + const auto *const entry = static_cast(parent.internalPointer()); + return entry && entry->type() == EntryType::Node; } QVariant EntryModel::data(const QModelIndex &index, int role) const { - if(index.isValid()) { - if(Entry *entry = static_cast(index.internalPointer())) { - switch(role) { - case Qt::DisplayRole: - case Qt::EditRole: - switch(index.column()) { - case 0: - return QString::fromStdString(entry->label()); - default: - ; - } - break; - case Qt::DecorationRole: - if(index.column() == 0 && entry->type() == EntryType::Node) { - static const QVariant folderIcon = QIcon::fromTheme(QStringLiteral("folder")); - return folderIcon; - } - break; - case SerializedRole: { - stringstream ss(stringstream::in | stringstream::out | stringstream::binary); - ss.exceptions(std::stringstream::failbit | std::stringstream::badbit); - try { - entry->make(ss); - string string = ss.str(); - return QByteArray(string.data(), string.size()); - } catch(...) { - IoUtilities::catchIoFailure(); - return false; - } - } - break; - case DefaultExpandedRole: - return entry->type() == EntryType::Node && static_cast(entry)->isExpandedByDefault(); - default: - ; + if(!index.isValid()) { + return QVariant(); + } + const auto *const entry = static_cast(index.internalPointer()); + if(!entry) { + return QVariant(); + } + switch(role) { + case Qt::DisplayRole: + case Qt::EditRole: + switch(index.column()) { + case 0: + return QString::fromStdString(entry->label()); + default: + ; + } + break; + case Qt::DecorationRole: + if(index.column() == 0 && entry->type() == EntryType::Node) { + static const QVariant folderIcon = QIcon::fromTheme(QStringLiteral("folder")); + return folderIcon; + } + break; + case SerializedRole: { + stringstream ss(stringstream::in | stringstream::out | stringstream::binary); + ss.exceptions(std::stringstream::failbit | std::stringstream::badbit); + try { + entry->make(ss); + const auto str(ss.str()); + return QByteArray(str.data(), str.size()); + } catch(...) { + IoUtilities::catchIoFailure(); + return false; } } + break; + case DefaultExpandedRole: + return entry->type() == EntryType::Node && static_cast(entry)->isExpandedByDefault(); + default: + ; } return QVariant(); } QMap EntryModel::itemData(const QModelIndex &index) const { - QMap roles; - roles.insert(Qt::DisplayRole, data(index, Qt::DisplayRole)); - roles.insert(SerializedRole, data(index, SerializedRole)); - return roles; + return QMap{ + {Qt::DisplayRole, data(index, Qt::DisplayRole)}, + {SerializedRole, data(index, SerializedRole)}, + }; } bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int role) @@ -263,60 +262,65 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro return push(new EntryModelSetValueCommand(this, index, value, role)); } #endif - if(index.isValid()) { - if(Entry *entry = static_cast(index.internalPointer())) { - switch(role) { - case Qt::DisplayRole: - case Qt::EditRole: - switch(index.column()) { - case 0: - entry->setLabel(value.toString().toStdString()); - emit dataChanged(index, index, QVector() << role); - return true; - default: - ; - } + if(!index.isValid()) { + return false; + } + auto *const entry = static_cast(index.internalPointer()); + if(!entry) { + return false; + } + switch(role) { + case Qt::DisplayRole: + case Qt::EditRole: + switch(index.column()) { + case 0: + entry->setLabel(value.toString().toStdString()); + emit dataChanged(index, index, QVector() << role); + return true; + default: + ; + } + break; + case SerializedRole: { + NodeEntry *parent = entry->parent(); + QModelIndex parentIndex = index.parent(); + if(!parent || !parentIndex.isValid()) { break; - case SerializedRole: { - NodeEntry *parent = entry->parent(); - QModelIndex parentIndex = index.parent(); - if(parent && parentIndex.isValid()) { - stringstream ss(stringstream::in | stringstream::out | stringstream::binary); - ss.exceptions(std::stringstream::failbit | std::stringstream::badbit); - QByteArray array = value.toByteArray(); - if(array.size()) { - try { - ss.write(array.data(), array.size()); - Entry *newEntry = Entry::parse(ss); - int row = entry->index(); - beginRemoveRows(parentIndex, row, row); - delete entry; - endRemoveRows(); - beginInsertRows(parentIndex, row, row); - newEntry->setParent(parent, row); - endInsertRows(); - return true; - } catch(...) { - IoUtilities::catchIoFailure(); - } - } - } - } + } + stringstream ss(stringstream::in | stringstream::out | stringstream::binary); + ss.exceptions(std::stringstream::failbit | std::stringstream::badbit); + QByteArray array = value.toByteArray(); + if(array.isEmpty()) { break; - case DefaultExpandedRole: - switch(entry->type()) { - case EntryType::Account: - return false; - case EntryType::Node: - static_cast(entry)->setExpandedByDefault(value.toBool()); - emit dataChanged(index, index, QVector() << role); - return true; - } - break; - default: - ; + } + try { + ss.write(array.data(), array.size()); + Entry *newEntry = Entry::parse(ss); + int row = entry->index(); + beginRemoveRows(parentIndex, row, row); + delete entry; + endRemoveRows(); + beginInsertRows(parentIndex, row, row); + newEntry->setParent(parent, row); + endInsertRows(); + return true; + } catch(...) { + IoUtilities::catchIoFailure(); } } + break; + case DefaultExpandedRole: + switch(entry->type()) { + case EntryType::Account: + return false; + case EntryType::Node: + static_cast(entry)->setExpandedByDefault(value.toBool()); + emit dataChanged(index, index, QVector() << role); + return true; + } + break; + default: + ; } return false; } @@ -388,30 +392,30 @@ bool EntryModel::insertRows(int row, int count, const QModelIndex &parent) return push(new EntryModelInsertRowsCommand(this, row, count, parent)); } #endif - if(parent.isValid()) { - if(Entry *parentEntry = static_cast(parent.internalPointer())) { - if(parentEntry->type() == EntryType::Node) { - beginInsertRows(parent, row, row + count - 1); - for(int end = row + count; row < end; ++row) { - Entry *newEntry; - switch(m_insertType) { - case EntryType::Node: - newEntry = new NodeEntry; - break; - case EntryType::Account: - newEntry = new AccountEntry; - break; - default: - return false; // should never be reached, just to suppress compiler warning - } - newEntry->setParent(static_cast(parentEntry), row); - } - endInsertRows(); - return true; - } - } + if(!parent.isValid()) { + return false; } - return false; + auto *const parentEntry = static_cast(parent.internalPointer()); + if(!parentEntry || parentEntry->type() != EntryType::Node) { + return false; + } + beginInsertRows(parent, row, row + count - 1); + for(int end = row + count; row < end; ++row) { + Entry *newEntry; + switch(m_insertType) { + case EntryType::Node: + newEntry = new NodeEntry; + break; + case EntryType::Account: + newEntry = new AccountEntry; + break; + default: + return false; // should never be reached, just to suppress compiler warning + } + newEntry->setParent(static_cast(parentEntry), row); + } + endInsertRows(); + return true; } bool EntryModel::removeRows(int row, int count, const QModelIndex &parent) @@ -421,17 +425,17 @@ bool EntryModel::removeRows(int row, int count, const QModelIndex &parent) return push(new EntryModelRemoveRowsCommand(this, row, count, parent)); } #endif - if(parent.isValid() && count > 0) { - if(Entry *parentEntry = static_cast(parent.internalPointer())) { - if(parentEntry->type() == EntryType::Node) { - beginRemoveRows(parent, row, row + count - 1); - static_cast(parentEntry)->deleteChildren(row, row + count); - endRemoveRows(); - return true; - } - } + if(!parent.isValid() || count <= 0) { + return false; } - return false; + auto *const parentEntry = static_cast(parent.internalPointer()); + if(!parentEntry || parentEntry->type() != EntryType::Node) { + return false; + } + beginRemoveRows(parent, row, row + count - 1); + static_cast(parentEntry)->deleteChildren(row, row + count); + endRemoveRows(); + return true; } bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) @@ -442,41 +446,42 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co } #endif // check validation of specified arguments - if(sourceParent.isValid() && destinationParent.isValid() - && sourceRow >= 0 && count > 0 - && entry(sourceParent)->type() == EntryType::Node // source and destination parent entries - && entry(destinationParent)->type() == EntryType::Node) { // need to be node entries - // determine the source parent entry and dest parent entry as node entries - NodeEntry *srcParentEntry = static_cast(sourceParent.internalPointer()); - NodeEntry *destParentEntry = static_cast(destinationParent.internalPointer()); - // source rows must be within the valid range - if(static_cast(sourceRow + count) <= srcParentEntry->children().size() - // if source and destination parent are the same the destination child mustn't be in the source range - && (srcParentEntry != destParentEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) { - // do not move a row to one of its own children! -> check before - for(int index = 0; index < count; ++index) { - Entry *toMove = srcParentEntry->children().at(sourceRow + index); - if(toMove->type() == EntryType::Node) { - if(destParentEntry->isIndirectChildOf(static_cast(toMove))) { - return false; - } - } + if(!sourceParent.isValid() || !destinationParent.isValid() + || sourceRow < 0 || count <= 0 + || entry(sourceParent)->type() != EntryType::Node // source and destination parent entries + || entry(destinationParent)->type() != EntryType::Node) { // need to be node entries + return false; + } + // determine the source parent entry and dest parent entry as node entries + auto *const srcParentEntry = static_cast(sourceParent.internalPointer()); + auto *const destParentEntry = static_cast(destinationParent.internalPointer()); + // source rows must be within the valid range + if(static_cast(sourceRow + count) > srcParentEntry->children().size() + // if source and destination parent are the same the destination child mustn't be in the source range + || !(srcParentEntry != destParentEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) { + return false; + } + // do not move a row to one of its own children! -> check before + for(int index = 0; index < count; ++index) { + Entry *toMove = srcParentEntry->children()[static_cast(sourceRow + index)]; + if(toMove->type() == EntryType::Node) { + if(destParentEntry->isIndirectChildOf(static_cast(toMove))) { + return false; } - // actually perform the move operation - beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild); - for(int index = 0; index < count; ++index) { - Entry *toMove = srcParentEntry->children().at(sourceRow + index); - if(srcParentEntry == destParentEntry && sourceRow < destinationChild) { - toMove->setParent(destParentEntry, destinationChild + index - 1); - } else { - toMove->setParent(destParentEntry, destinationChild + index); - } - } - endMoveRows(); - return true; } } - return false; + // actually perform the move operation + beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild); + for(int index = 0; index < count; ++index) { + Entry *toMove = srcParentEntry->children()[static_cast(sourceRow + index)]; + if(srcParentEntry == destParentEntry && sourceRow < destinationChild) { + toMove->setParent(destParentEntry, destinationChild + index - 1); + } else { + toMove->setParent(destParentEntry, destinationChild + index); + } + } + endMoveRows(); + return true; } QStringList EntryModel::mimeTypes() const @@ -497,16 +502,17 @@ QMimeData *EntryModel::mimeData(const QModelIndexList &indexes) const QStringList plainTextParts; QByteArray encoded; QDataStream dataStream(&encoded, QIODevice::WriteOnly); - foreach(const QModelIndex &index, indexes) { - if(index.isValid()) { - Entry *entry = static_cast(index.internalPointer()); - list path = entry->path(); - dataStream << static_cast(path.size()); - for(const string &part : path) { - dataStream << QString::fromStdString(part); - } - plainTextParts << QString::fromStdString(entry->label()); + for(const QModelIndex &index : indexes) { + if(!index.isValid()) { + continue; } + const auto *const entry = static_cast(index.internalPointer()); + const auto path(entry->path()); + dataStream << static_cast(path.size()); + for(const string &part : path) { + dataStream << QString::fromStdString(part); + } + plainTextParts << QString::fromStdString(entry->label()); } data->setData(types.at(0), encoded); data->setText(plainTextParts.join(QStringLiteral(", "))); @@ -533,7 +539,7 @@ bool EntryModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int column = 0; } // decode and insert - QByteArray encoded = data->data(format); + QByteArray encoded(data->data(format)); QDataStream stream(&encoded, QIODevice::ReadOnly); int moved = 0; while(!stream.atEnd()) { @@ -545,12 +551,13 @@ bool EntryModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int stream >> part; path.push_back(part.toStdString()); } - if(Entry *entry = m_rootEntry->entryByPath(path, true)) { - if(NodeEntry *srcParentEntry = entry->parent()) { - if(moveRows(index(srcParentEntry), entry->index(), 1, parent, row)) { - ++moved; - } - } + auto *const entry = m_rootEntry->entryByPath(path, true); + if(!entry) { + continue; + } + auto *const srcParentEntry = entry->parent(); + if(srcParentEntry && moveRows(index(srcParentEntry), entry->index(), 1, parent, row)) { + ++moved; } } return false; diff --git a/model/fieldmodel.cpp b/model/fieldmodel.cpp index 61b93a6..152d9c3 100644 --- a/model/fieldmodel.cpp +++ b/model/fieldmodel.cpp @@ -55,60 +55,62 @@ FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent) : */ void FieldModel::setAccountEntry(AccountEntry *entry) { - if(entry != m_accountEntry) { - beginResetModel(); - if((m_accountEntry = entry)) { - m_fields = &entry->fields(); - } else { - m_fields = nullptr; - } - endResetModel(); + if(entry == m_accountEntry) { + return; } + beginResetModel(); + if((m_accountEntry = entry)) { + m_fields = &entry->fields(); + } else { + m_fields = nullptr; + } + endResetModel(); } QVariant FieldModel::data(const QModelIndex &index, int role) const { - if(index.isValid() && m_fields && index.row() >= 0) { - // return data for existent field - if(static_cast(index.row()) < m_fields->size()) { - switch(role) { - case Qt::DisplayRole: - case Qt::EditRole: - switch(index.column()) { - case 0: - return QString::fromStdString(m_fields->at(index.row()).name()); - case 1: - return (m_passwordVisibility == PasswordVisibility::Always - || role == Qt::EditRole - || m_fields->at(index.row()).type() != FieldType::Password) - ? QString::fromStdString(m_fields->at(index.row()).value()) - : QString(m_fields->at(index.row()).value().size(), QChar(0x2022)); - default: - ; - } - break; - case FieldTypeRole: - return static_cast(m_fields->at(index.row()).type()); + if(!index.isValid() || !m_fields || index.row() < 0) { + return QVariant(); + } + // return data for existent field + if(static_cast(index.row()) < m_fields->size()) { + switch(role) { + case Qt::DisplayRole: + case Qt::EditRole: + switch(index.column()) { + case 0: + return QString::fromStdString((*m_fields)[static_cast(index.row())].name()); + case 1: + return (m_passwordVisibility == PasswordVisibility::Always + || role == Qt::EditRole + || (*m_fields)[static_cast(index.row())].type() != FieldType::Password) + ? QString::fromStdString((*m_fields)[static_cast(index.row())].value()) + : QString((*m_fields)[static_cast(index.row())].value().size(), QChar(0x2022)); default: ; } - // return data for empty field at the end which enables the user to append fields - } else if(static_cast(index.row()) == m_fields->size()) { - switch(role) { - case Qt::DisplayRole: - case Qt::EditRole: - switch(index.column()) { - case 0: - return QString(); - case 1: - return QString(); - default: - ; - } - break; + break; + case FieldTypeRole: + return static_cast((*m_fields)[static_cast(index.row())].type()); + default: + ; + } + // return data for empty field at the end which enables the user to append fields + } else if(static_cast(index.row()) == m_fields->size()) { + switch(role) { + case Qt::DisplayRole: + case Qt::EditRole: + switch(index.column()) { + case 0: + return QString(); + case 1: + return QString(); default: ; } + break; + default: + ; } } return QVariant(); @@ -116,14 +118,16 @@ QVariant FieldModel::data(const QModelIndex &index, int role) const QMap FieldModel::itemData(const QModelIndex &index) const { - static int roles[] = {Qt::EditRole, FieldTypeRole}; - QMap roleMap; - for(int role : roles) { - QVariant variantData = data(index, role); - if (variantData.isValid()) { - roleMap.insert(role, variantData); + static const auto roleMap = [this, index] { + QMap roleMap; + for(const auto role : initializer_list{Qt::EditRole, FieldTypeRole}) { + const auto variantData(data(index, role)); + if (variantData.isValid()) { + roleMap.insert(role, variantData); + } } - } + return roleMap; + }(); return roleMap; } @@ -134,86 +138,86 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro return push(new FieldModelSetValueCommand(this, index, value, role)); } #endif + if(!index.isValid() || !m_fields || index.row() < 0) { + return false; + } QVector roles; - if(index.isValid() && m_fields && index.row() >= 0) { + if(static_cast(index.row()) < m_fields->size()) { // set data for existing field - if(static_cast(index.row()) < m_fields->size()) { - switch(role) { - case Qt::EditRole: - switch(index.column()) { - case 0: - m_fields->at(index.row()).setName(value.toString().toStdString()); - roles << role; - break; - case 1: - m_fields->at(index.row()).setValue(value.toString().toStdString()); - roles << role; - break; - default: - ; - } + switch(role) { + case Qt::EditRole: + switch(index.column()) { + case 0: + m_fields->at(index.row()).setName(value.toString().toStdString()); + roles << role; break; - case FieldTypeRole: { - bool ok; - int fieldType = value.toInt(&ok); - if(ok && Field::isValidType(fieldType)) { - roles << role; - m_fields->at(index.row()).setType(static_cast(fieldType)); - } - break; - } default: - ; - } - // remove last field if empty, showing an empty field at the end to enabled appending new rows is provided by the data method - if(!roles.isEmpty() && static_cast(index.row()) == m_fields->size() - 1 && m_fields->at(index.row()).isEmpty()) { - beginRemoveRows(index.parent(), index.row(), index.row()); - m_fields->pop_back(); - endRemoveRows(); - } - // set data for a new field emplaced at the end of the field list - } else if(static_cast(index.row()) == m_fields->size() && !value.toString().isEmpty()) { - switch(role) { - case Qt::DisplayRole: - case Qt::EditRole: - switch(index.column()) { - case 0: - beginInsertRows(index.parent(), rowCount(), rowCount()); - m_fields->emplace_back(m_accountEntry); - m_fields->back().setName(value.toString().toStdString()); - endInsertRows(); - roles << role; - break; - case 1: - beginInsertRows(index.parent(), rowCount(), rowCount()); - m_fields->emplace_back(m_accountEntry); - m_fields->back().setValue(value.toString().toStdString()); - endInsertRows(); - roles << role; - break; - default: - ; - } + case 1: + m_fields->at(index.row()).setValue(value.toString().toStdString()); + roles << role; break; default: ; } + break; + case FieldTypeRole: { + bool ok; + int fieldType = value.toInt(&ok); + if(ok && Field::isValidType(fieldType)) { + roles << role; + m_fields->at(index.row()).setType(static_cast(fieldType)); + } + break; + } default: + ; + } + // remove last field if empty, showing an empty field at the end to enabled appending new rows is provided by the data method + if(!roles.isEmpty() && static_cast(index.row()) == m_fields->size() - 1 && m_fields->at(index.row()).isEmpty()) { + beginRemoveRows(index.parent(), index.row(), index.row()); + m_fields->pop_back(); + endRemoveRows(); + } + } else if(static_cast(index.row()) == m_fields->size() && !value.toString().isEmpty()) { + // set data for a new field emplaced at the end of the field list + switch(role) { + case Qt::DisplayRole: + case Qt::EditRole: + switch(index.column()) { + case 0: + beginInsertRows(index.parent(), rowCount(), rowCount()); + m_fields->emplace_back(m_accountEntry); + m_fields->back().setName(value.toString().toStdString()); + endInsertRows(); + roles << role; + break; + case 1: + beginInsertRows(index.parent(), rowCount(), rowCount()); + m_fields->emplace_back(m_accountEntry); + m_fields->back().setValue(value.toString().toStdString()); + endInsertRows(); + roles << role; + break; + default: + ; + } + break; + default: + ; } } // return false if nothing could be changed if(roles.isEmpty()) { return false; - } else { - // some roles affect other roles - switch(role) { - case Qt::EditRole: - roles << Qt::DisplayRole; - break; - case FieldTypeRole: - roles << Qt::DisplayRole << Qt::EditRole; - break; - default: - ; - } + } + // some roles affect other roles + switch(role) { + case Qt::EditRole: + roles << Qt::DisplayRole; + break; + case FieldTypeRole: + roles << Qt::DisplayRole << Qt::EditRole; + break; + default: + ; } // emit data changed signal on sucess emit dataChanged(index, index, roles); @@ -267,13 +271,13 @@ bool FieldModel::insertRows(int row, int count, const QModelIndex &parent) return push(new FieldModelInsertRowsCommand(this, row, count)); } #endif - if(!parent.isValid() && row >= 0 && count > 0 && static_cast(row) <= m_fields->size()) { - beginInsertRows(parent, row, row + count - 1); - m_fields->insert(m_fields->begin() + row, count, Field(m_accountEntry)); - endInsertRows(); - return true; + if(parent.isValid() || row < 0 || count <= 0 || static_cast(row + count) > m_fields->size()) { + return false; } - return false; + beginInsertRows(parent, row, row + count - 1); + m_fields->insert(m_fields->begin() + row, static_cast(count), Field(m_accountEntry)); + endInsertRows(); + return true; } bool FieldModel::removeRows(int row, int count, const QModelIndex &parent) @@ -283,21 +287,18 @@ bool FieldModel::removeRows(int row, int count, const QModelIndex &parent) return push(new FieldModelRemoveRowsCommand(this, row, count)); } #endif - if(!parent.isValid() && row >= 0 && count > 0 && static_cast(row + count) <= m_fields->size()) { - beginRemoveRows(parent, row, row + count - 1); - m_fields->erase(m_fields->begin() + row, m_fields->begin() + row + count); - endRemoveRows(); - return true; + if(parent.isValid() || row < 0 || count <= 0 || static_cast(row + count) > m_fields->size()) { + return false; } - return false; + beginRemoveRows(parent, row, row + count - 1); + m_fields->erase(m_fields->begin() + row, m_fields->begin() + row + count); + endRemoveRows(); } bool FieldModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { - if(!QAbstractTableModel::dropMimeData(data, action, row, column, parent)) { - if(data->hasText()) { - return setData(parent, data->text(), Qt::EditRole); - } + if(!QAbstractTableModel::dropMimeData(data, action, row, column, parent) && data->hasText()) { + return setData(parent, data->text(), Qt::EditRole); } return false; } @@ -310,13 +311,14 @@ QStringList FieldModel::mimeTypes() const QMimeData *FieldModel::mimeData(const QModelIndexList &indexes) const { QMimeData *data = QAbstractTableModel::mimeData(indexes); - if(!indexes.isEmpty()) { - QStringList result; - foreach(const QModelIndex &index, indexes) { - result << index.data(Qt::EditRole).toString(); - } - data->setText(result.join(QChar('\n'))); + if(indexes.isEmpty()) { + return data; } + QStringList result; + for(const QModelIndex &index : indexes) { + result << index.data(Qt::EditRole).toString(); + } + data->setText(result.join(QChar('\n'))); return data; } @@ -328,7 +330,7 @@ QMimeData *FieldModel::mimeData(const QModelIndexList &indexes) const const Field *FieldModel::field(size_t row) const { if(m_fields && row < m_fields->size()) { - return &m_fields->at(row); + return &(*m_fields)[row]; } return nullptr; } diff --git a/quickgui/applicationpaths.h b/quickgui/applicationpaths.h index c9465a1..4c03c6a 100644 --- a/quickgui/applicationpaths.h +++ b/quickgui/applicationpaths.h @@ -30,10 +30,12 @@ inline QString ApplicationPaths::path(QStandardPaths::StandardLocation location) { QString path = QStandardPaths::standardLocations(location).value(0); QDir dir(path); - if (!dir.exists()) + if (!dir.exists()) { dir.mkpath(path); - if (!path.isEmpty() && !path.endsWith("/")) + } + if (!path.isEmpty() && !path.endsWith("/")) { path += "/"; + } return path; } } // namespace QtGui