From cd6fac395056d794f9d89766593b748e79fe9149 Mon Sep 17 00:00:00 2001 From: S David <2100425+s-daveb@users.noreply.github.com> Date: Sun, 5 May 2024 19:53:08 -0400 Subject: [PATCH] Fix reversed return value logic in QJsonModel::load methods --- .clang-tidy | 4 ++-- QJsonModel.cpp | 17 ++++++++++------- include/QJsonModel.hpp | 7 ++++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index de1731a..452f3c9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -66,10 +66,10 @@ CheckOptions: - { key: readability-identifier-naming.PrivateMemberCase, value: camelBack } # Constants and Enum Values - - { key: readability-identifier-naming.ConstantPrefix, value: 'k' } + - { key: readability-identifier-naming.ConstantPrefix, value: 'k_' } - { key: readability-identifier-naming.ConstantCase, value: CamelBack } - - { key: readability-identifier-naming.EnumConstantPrefix, value: '' } + - { key: readability-identifier-naming.EnumConstantPrefix, value: 'k' } - { key: readability-identifier-naming.EnumConstantCase, value: CamelCase } # Constant Expression diff --git a/QJsonModel.cpp b/QJsonModel.cpp index b6b75c7..b3058c3 100644 --- a/QJsonModel.cpp +++ b/QJsonModel.cpp @@ -32,7 +32,9 @@ #include namespace { + using FieldPermissions = QJsonModel::FieldPermissions; +using ErrorFlag = QJsonModel::ErrorFlag; constexpr int kKeyColumn = 0; constexpr int kValueColumn = 1; @@ -86,27 +88,28 @@ QJsonModel::~QJsonModel() delete rootItem; } -bool QJsonModel::load(const QString& fileName) +ErrorFlag QJsonModel::load(const QString& fileName) { QFile file(fileName); - bool success = false; + ErrorFlag result = false; + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { - success = load(&file); + result = load(&file); file.close(); } else { - success = false; + result = true; } - return success; + return result; } -bool QJsonModel::load(QIODevice* device) +ErrorFlag QJsonModel::load(QIODevice* device) { QTextStream content(device); return loadJson(content.readAll().toUtf8()); } -bool QJsonModel::loadJson(const QByteArray& json) +ErrorFlag QJsonModel::loadJson(const QByteArray& json) { auto const& jdoc = QJsonDocument::fromJson(json); diff --git a/include/QJsonModel.hpp b/include/QJsonModel.hpp index b284697..aebad88 100644 --- a/include/QJsonModel.hpp +++ b/include/QJsonModel.hpp @@ -69,9 +69,10 @@ class QJsonModel : public QAbstractItemModel { ~QJsonModel() override; - bool load(const QString& fileName); - bool load(QIODevice* file); - bool loadJson(const QByteArray& json); + using ErrorFlag = bool; + ErrorFlag load(const QString& fileName); + ErrorFlag load(QIODevice* file); + ErrorFlag loadJson(const QByteArray& json); QVariant data(const QModelIndex& index, int role) const override; bool setData( const QModelIndex& index, const QVariant& value,