diff --git a/.clang-tidy b/.clang-tidy index fd21b3531..543d5ca53 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -40,6 +40,7 @@ Checks: -cppcoreguidelines-pro-type-union-access, -cppcoreguidelines-pro-type-vararg, -cppcoreguidelines-special-member-functions, + -cppcoreguidelines-use-enum-class, -hicpp-avoid-c-arrays, -hicpp-avoid-goto, -hicpp-braces-around-statements, diff --git a/.github/actions/cmake/action.yml b/.github/actions/cmake/action.yml index e822fb209..04c549162 100644 --- a/.github/actions/cmake/action.yml +++ b/.github/actions/cmake/action.yml @@ -66,6 +66,7 @@ runs: -B '${{github.workspace}}/build' \ -DCMAKE_BUILD_TYPE=Release \ -DQF_BUILD_QML_PLUGINS=ON \ + -DQF_WITH_LIBSHV=ON \ -DBUILD_TESTING=OFF \ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ -DUSE_QT6=${{ inputs.use_qt6 }} \ diff --git a/.github/actions/run-linter/action.yml b/.github/actions/run-linter/action.yml index 93baab419..7d5cd38b4 100644 --- a/.github/actions/run-linter/action.yml +++ b/.github/actions/run-linter/action.yml @@ -20,11 +20,18 @@ runs: modules: qtserialport qtmultimedia additional_cmake_args: -DCMAKE_GLOBAL_AUTOGEN_TARGET=ON -DCMAKE_AUTOGEN_ORIGIN_DEPENDS=OFF + # https://bugs.launchpad.net/ubuntu/+source/clazy/+bug/2086665/comments/4 + - name: Workaround for clazy + shell: bash + run: | + sudo apt-get remove gcc-14 + sudo apt-get autoremove + # workaround for clang-tidy false positive - uses: mjp41/workaround8649@c8550b715ccdc17f89c8d5c28d7a48eeff9c94a8 if: runner.os == 'Linux' with: - os: ubuntu-latest + os: ubuntu-24.04 - name: Build autogenerated stuff shell: bash diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index d41591f4c..312183ca7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -8,8 +8,8 @@ on: jobs: clang-tidy: - name: clang-tidy / Ubuntu 22.04 - runs-on: ubuntu-22.04 + name: clang-tidy / Ubuntu 24.04 + runs-on: ubuntu-24.04 env: CC: clang CXX: clang++ @@ -26,8 +26,8 @@ jobs: exclude_files_pattern: ^quickevent/app/quickevent/plugins/Receipts/src/thirdparty/qrcodegen\.cpp$ clazy: - name: clazy / Ubuntu 22.04 - runs-on: ubuntu-22.04 + name: clazy / Ubuntu 24.04 + runs-on: ubuntu-24.04 env: CC: clang CXX: clang++ diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index ea681f520..fa02121e1 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -37,6 +37,7 @@ jobs: -B '${{github.workspace}}/build' \ -DCMAKE_BUILD_TYPE=Release \ -DQF_BUILD_QML_PLUGINS=ON \ + -DQF_WITH_LIBSHV=ON \ -DBUILD_TESTING=OFF \ -DUSE_QT6=ON \ -DCOMMIT_SHA=${{ env.COMMIT_SHA }} \ diff --git a/.gitmodules b/.gitmodules index 6e2bec6bb..5006631e0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "3rdparty/necrolog"] path = 3rdparty/necrolog url = https://github.com/fvacek/necrolog.git +[submodule "3rdparty/libshv"] + path = 3rdparty/libshv + url = https://github.com/silicon-heaven/libshv.git diff --git a/3rdparty/libshv b/3rdparty/libshv new file mode 160000 index 000000000..b1c3721d5 --- /dev/null +++ b/3rdparty/libshv @@ -0,0 +1 @@ +Subproject commit b1c3721d53f7390598e8f4fd8e5a3b225b58e5d0 diff --git a/CMakeLists.txt b/CMakeLists.txt index e511bd8f6..a59ce7871 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.18.4) set(QF_BUILD_QML_PLUGINS ON CACHE BOOL "Build with QML Plugins support") +set(QF_WITH_LIBSHV OFF CACHE BOOL "Build with libshv") project(quickbox LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) @@ -34,6 +35,9 @@ endif (WIN32) if (NOT TARGET libnecrolog) add_subdirectory(3rdparty/necrolog) endif() +if (QF_WITH_LIBSHV) + add_subdirectory(3rdparty/libshv) +endif() set(USE_QT6 ON) find_package(Qt6 REQUIRED COMPONENTS Core Widgets Gui Sql Qml Xml LinguistTools PrintSupport Svg SerialPort Multimedia Network) diff --git a/libqf/libqfcore/src/sql/qxrecchng.cpp b/libqf/libqfcore/src/sql/qxrecchng.cpp index fc0ae36b2..b534d9ed5 100644 --- a/libqf/libqfcore/src/sql/qxrecchng.cpp +++ b/libqf/libqfcore/src/sql/qxrecchng.cpp @@ -48,6 +48,7 @@ QString QxRecChng::recopToString(RecOp op) case RecOp::Update: return "Update"; case RecOp::Delete: return "Delete"; } + return QString(); } } diff --git a/libqf/libqfcore/src/sql/qxsql.cpp b/libqf/libqfcore/src/sql/qxsql.cpp index 844ffbf2d..45e55d111 100644 --- a/libqf/libqfcore/src/sql/qxsql.cpp +++ b/libqf/libqfcore/src/sql/qxsql.cpp @@ -1,6 +1,8 @@ #include "qxsql.h" #include +#include +#include #include #include @@ -11,6 +13,49 @@ namespace qf::core::sql { //================================================================ // QxSqlApi //================================================================ +namespace { + +class Transaction +{ +public: + Transaction(QSqlDatabase db) : m_db(db) { + if (!m_db.transaction()) { + qfWarning() << "BEGIN transaction error:" << m_db.lastError().text(); + throw std::runtime_error("BEGIN transaction error"); + } + } + ~Transaction() { + if (m_inTransaction) { + m_db.rollback(); + } + } + void commit() { + if (!m_db.commit()) { + qfWarning() << "COMMIT transaction error:" << m_db.lastError().text(); + throw std::runtime_error("COMMIT transaction error"); + } + m_inTransaction = false; + } +private: + QSqlDatabase m_db; + bool m_inTransaction = true; +}; +} +void QxSqlApi::transaction(const QString &query, const QVariantList ¶ms, QSqlDatabase db) +{ + Transaction transaction(db); + qf::core::sql::Query q(db); + q.prepare(query, qf::core::Exception::Throw); + for (const auto ¶m : params) { + auto m = param.toMap(); + for (const auto &[k, v] : m.asKeyValueRange()) { + q.bindValue(':' + k, v); + } + q.exec(qf::core::Exception::Throw); + } + transaction.commit(); +} + qint64 QxSqlApi::createRecord(const QString &table, const Record &record, const QString &issuer) { Q_UNUSED(issuer) @@ -63,7 +108,7 @@ bool QxSqlApi::updateRecord(const QString &table, qint64 id, const Record &recor .arg(table, keyVals.join(", "), QString::number(id)); ExecResult res = exec(qs, record); - return res.rowsAffected == 1; + return res.numRowsAffected == 1; } bool QxSqlApi::deleteRecord(const QString &table, qint64 id, const QString &issuer) @@ -72,7 +117,7 @@ bool QxSqlApi::deleteRecord(const QString &table, qint64 id, const QString &issu QString qs = QString("DELETE FROM %1 WHERE id = %2").arg(table, QString::number(id)); ExecResult res = exec(qs, {}); - return res.rowsAffected == 1; + return res.numRowsAffected == 1; } QList QxSqlApi::listOneOrMoreRecords(const QString &table, const std::optional &fields, const std::optional &id, const std::optional &limit) @@ -106,18 +151,15 @@ QList QxSqlApi::listOneOrMoreRecords(const QString &table, const std::op } //================================================================ -// QxSql +// QxSqlApiImpl //================================================================ -QxSql::QxSql(const QSqlDatabase &db) - : m_db(db) -{ - -} - -QueryResult QxSql::query(const QString &query, const QVariantMap ¶ms) +QueryResult QxSqlApiImpl::query(const QString &query, const QVariantMap ¶ms) { + // qDebug() << query << params; QSqlQuery q(m_db); - q.prepare(query); + if (!q.prepare(query)) { + throw qf::core::Exception(q.lastError().text()); + } for (const auto &[key, val] : params.asKeyValueRange()) { q.bindValue(QStringLiteral(":%1").arg(key), val); } @@ -126,7 +168,7 @@ QueryResult QxSql::query(const QString &query, const QVariantMap ¶ms) } QueryResult result; for (int i = 0; i < q.record().count(); ++i) { - result.columns.append(q.record().fieldName(i).toLower()); + result.fields.append(DbField{.name = q.record().fieldName(i).toLower()}); } while (q.next()) { QList row; @@ -138,10 +180,12 @@ QueryResult QxSql::query(const QString &query, const QVariantMap ¶ms) return result; } -ExecResult QxSql::exec(const QString &query, const QVariantMap ¶ms) +ExecResult QxSqlApiImpl::exec(const QString &query, const QVariantMap ¶ms) { QSqlQuery q(m_db); - q.prepare(query); + if (!q.prepare(query)) { + throw qf::core::Exception(q.lastError().text()); + } for (const auto &[key, val] : params.asKeyValueRange()) { q.bindValue(QStringLiteral(":%1").arg(key), val); } @@ -149,44 +193,129 @@ ExecResult QxSql::exec(const QString &query, const QVariantMap ¶ms) throw qf::core::Exception(q.lastError().text()); } ExecResult result; - result.rowsAffected = q.numRowsAffected(); + result.numRowsAffected = q.numRowsAffected(); + if (auto id = q.lastInsertId().toLongLong(); id > 0) { + result.lastInsertId = id; + } return result; } -qint64 QxSql::createRecord(const QString &table, const Record &record, const QString &issuer) +void QxSqlApiImpl::transaction(const QString &query, const QVariantList ¶ms) +{ + QxSqlApi::transaction(query, params, m_db); +} + +//================================================================ +// QxSql +//================================================================ +QxSql::QxSql(const QString &issuer, const QSqlDatabase &db, QObject *parent) + : QObject(parent) + , m_issuer(issuer) + , m_sqlApi(db) { - auto id = QxSqlApi::createRecord(table, record, issuer); - emit dbRecChng(qf::core::sql::QxRecChng{.table = table, +} + +QueryResult QxSql::query(const QString &query, const QVariantMap ¶ms) +{ + return m_sqlApi.query(query, params); +} + +ExecResult QxSql::exec(const QString &query, const QVariantMap ¶ms) +{ + return m_sqlApi.exec(query, params); +} + +QList QxSql::listRecords(const QString &table, const std::optional &fields, const std::optional &fromId, const std::optional &limit) +{ + return m_sqlApi.listRecords(table, fields, fromId, limit); +} + +void QxSql::transaction(const QString &query, const QVariantList ¶ms) +{ + m_sqlApi.transaction(query, params); +} + +qint64 QxSql::createRecord(const QString &table, const Record &record, QObject *source) +{ + auto id = m_sqlApi.createRecord(table, record, m_issuer); + emit recChng(qf::core::sql::QxRecChng{.table = table, .id = id, .record = record, .op = qf::core::sql::RecOp::Insert, - .issuer = issuer}); + .issuer = m_issuer}, + source); return id; } -bool QxSql::updateRecord(const QString &table, qint64 id, const Record &record, const QString &issuer) +std::optional QxSql::readRecord(const QString &table, qint64 id, const std::optional &fields) { - auto ok = QxSqlApi::updateRecord(table, id, record, issuer); + return m_sqlApi.readRecord(table, id, fields); +} + +bool QxSql::updateRecord(const QString &table, qint64 id, const Record &record, QObject *source) +{ + auto ok = m_sqlApi.updateRecord(table, id, record, m_issuer); if (ok) { - emit dbRecChng(qf::core::sql::QxRecChng{.table = table, + emit recChng(qf::core::sql::QxRecChng{.table = table, .id = id, .record = record, .op = qf::core::sql::RecOp::Update, - .issuer = issuer}); + .issuer = m_issuer}, + source); } return ok; } -bool QxSql::deleteRecord(const QString &table, qint64 id, const QString &issuer) +bool QxSql::deleteRecord(const QString &table, qint64 id, QObject *source) { - auto ok = QxSqlApi::deleteRecord(table, id, issuer); + auto ok = m_sqlApi.deleteRecord(table, id, m_issuer); if (ok) { - emit dbRecChng(qf::core::sql::QxRecChng{.table = table, + emit recChng(qf::core::sql::QxRecChng{.table = table, .id = id, .record = {}, .op = qf::core::sql::RecOp::Delete, - .issuer = issuer}); + .issuer = m_issuer}, + source); } return ok; } + +void QxSql::emitRecInserted(const QString &table, qint64 id, const QVariantMap &record, QObject *source) +{ + emit recChng(qf::core::sql::QxRecChng{ + .table = table, + .id = id, + .record = record, + .op = qf::core::sql::RecOp::Insert, + .issuer = m_issuer + }, source); +} + +void QxSql::emitRecUpdated(const QString &table, qint64 id, const QVariantMap &record, QObject *source) +{ + emit recChng(qf::core::sql::QxRecChng{ + .table = table, + .id = id, + .record = record, + .op = qf::core::sql::RecOp::Update, + .issuer = m_issuer + }, source); +} + +void QxSql::emitRecDeleted(const QString &table, qint64 id, QObject *source) +{ + emit recChng(qf::core::sql::QxRecChng{ + .table = table, + .id = id, + .record = {}, + .op = qf::core::sql::RecOp::Delete, + .issuer = m_issuer + }, source); +} + +void QxSql::emitRecChng(const core::sql::QxRecChng &recchng, QObject *source) +{ + emit recChng(recchng, source); +} + } diff --git a/libqf/libqfcore/src/sql/qxsql.h b/libqf/libqfcore/src/sql/qxsql.h index 51e7c6fd0..04d493eeb 100644 --- a/libqf/libqfcore/src/sql/qxsql.h +++ b/libqf/libqfcore/src/sql/qxsql.h @@ -15,9 +15,14 @@ namespace qf::core::sql { using Record = QVariantMap; +struct DbField +{ + QString name; +}; + struct QFCORE_DECL_EXPORT QueryResult { - QStringList columns; + QList fields; QList> rows; std::optional record(int i) const @@ -27,8 +32,8 @@ struct QFCORE_DECL_EXPORT QueryResult } Record r; const auto &row = rows[i]; - for (int j = 0; j < columns.size(); ++j) { - r[columns[j]] = row.value(j); + for (int j = 0; j < fields.size(); ++j) { + r[fields[j].name] = row.value(j); } return r; } @@ -36,7 +41,8 @@ struct QFCORE_DECL_EXPORT QueryResult struct QFCORE_DECL_EXPORT ExecResult { - qint64 rowsAffected = 0; + qint64 numRowsAffected = 0; + std::optional lastInsertId = 0; }; class QFCORE_DECL_EXPORT QxSqlApi @@ -54,11 +60,12 @@ class QFCORE_DECL_EXPORT QxSqlApi { return listOneOrMoreRecords(table, fields, fromId, limit); } + void transaction(const QString &query, const QVariantList ¶ms, QSqlDatabase db); - virtual qint64 createRecord(const QString &table, const Record &record, const QString &issuer); - virtual std::optional readRecord(const QString &table, qint64 id, const std::optional &fields = std::nullopt); - virtual bool updateRecord(const QString &table, qint64 id, const Record &record, const QString &issuer); - virtual bool deleteRecord(const QString &table, qint64 id, const QString &issuer); + qint64 createRecord(const QString &table, const Record &record, const QString &issuer); + std::optional readRecord(const QString &table, qint64 id, const std::optional &fields = std::nullopt); + bool updateRecord(const QString &table, qint64 id, const Record &record, const QString &issuer); + bool deleteRecord(const QString &table, qint64 id, const QString &issuer); protected: QList listOneOrMoreRecords( const QString &table, @@ -67,23 +74,48 @@ class QFCORE_DECL_EXPORT QxSqlApi const std::optional &limit); }; -class QFCORE_DECL_EXPORT QxSql : public QObject, public QxSqlApi +class QxSqlApiImpl : public QxSqlApi { - Q_OBJECT public: - QxSql(const QSqlDatabase &db = QSqlDatabase()); - ~QxSql() override = default; - - Q_SIGNAL void dbRecChng(const qf::core::sql::QxRecChng &recchng); + QxSqlApiImpl(QSqlDatabase db) : m_db(db) {} QueryResult query(const QString &query, const QVariantMap ¶ms) override; ExecResult exec(const QString &query, const QVariantMap ¶ms) override; - - qint64 createRecord(const QString &table, const Record &record, const QString &issuer) override; - bool updateRecord(const QString &table, qint64 id, const Record &record, const QString &issuer) override; - bool deleteRecord(const QString &table, qint64 id, const QString &issuer) override; + void transaction(const QString &query, const QVariantList ¶ms); private: QSqlDatabase m_db; }; +class QFCORE_DECL_EXPORT QxSql : public QObject +{ + Q_OBJECT +public: + QxSql(const QString &issuer, const QSqlDatabase &db = {}, QObject *parent = nullptr); + ~QxSql() override = default; + + Q_SIGNAL void recChng(const qf::core::sql::QxRecChng &recchng, QObject *source); + + QueryResult query(const QString &query, const QVariantMap ¶ms); + ExecResult exec(const QString &query, const QVariantMap ¶ms); + QList listRecords( + const QString &table, + const std::optional &fields = std::nullopt, + const std::optional &fromId = std::nullopt, + const std::optional &limit = std::nullopt); + void transaction(const QString &query, const QVariantList ¶ms); + + qint64 createRecord(const QString &table, const Record &record, QObject *source); + std::optional readRecord(const QString &table, qint64 id, const std::optional &fields = std::nullopt); + bool updateRecord(const QString &table, qint64 id, const Record &record, QObject *source); + bool deleteRecord(const QString &table, qint64 id, QObject *source); + + void emitRecInserted(const QString &table, qint64 id, const QVariantMap &record, QObject *source); + void emitRecUpdated(const QString &table, qint64 id, const QVariantMap &record, QObject *source); + void emitRecDeleted(const QString &table, qint64 id, QObject *source); + void emitRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); +private: + QString m_issuer; + QxSqlApiImpl m_sqlApi; +}; + } // namespace qf::core::sql diff --git a/libqf/libqfcore/src/utils/table.cpp b/libqf/libqfcore/src/utils/table.cpp index bd7a7a759..deadd881b 100644 --- a/libqf/libqfcore/src/utils/table.cpp +++ b/libqf/libqfcore/src/utils/table.cpp @@ -1311,6 +1311,7 @@ QVariant Table::sumValue(int field_ix) const } return ret; } + namespace { void setDomElementText(QDomDocument &owner_doc, QDomElement &el, const QString &str) { @@ -1325,6 +1326,7 @@ void setDomElementText(QDomDocument &owner_doc, QDomElement &el, const QString & } } } + QDomElement Table::toHtmlElement(QDomDocument &owner_doc, const QString & col_names, TextExportOptions opts) const { QList ixs; diff --git a/libqf/libqfcore/src/utils/treetable.cpp b/libqf/libqfcore/src/utils/treetable.cpp index c18b2fdee..5641dea4f 100644 --- a/libqf/libqfcore/src/utils/treetable.cpp +++ b/libqf/libqfcore/src/utils/treetable.cpp @@ -581,8 +581,8 @@ QVariantMap TreeTable::keyvals(int row_ix) const { return rows().value(row_ix).toMap().value(KEY_KEYVALS).toMap(); } - -static inline QString line_indent(const QString &ind, int level) +namespace { +QString line_indent(const QString &ind, int level) { QString s; for(int i=0; i Application::readDbRecord(const QString &table, qint64 id, const std::optional &fields) const -{ - using namespace qf::core::sql; - QxSql sql; - return sql.readRecord(table, id, fields); -} - -bool Application::updateDbRecord(const QString &table, qint64 id, const QVariantMap &record, QObject *source) -{ - using namespace qf::core::sql; - QxSql sql; - connect(&sql, &QxSql::dbRecChng, this, [this, source](const qf::core::sql::QxRecChng &recchng) { emit qxRecChng(recchng, source); }); - return sql.updateRecord(table, id, record, uuidString()); -} - -bool Application::deleteDbRecord(const QString &table, qint64 id, QObject *source) -{ - using namespace qf::core::sql; - QxSql sql; - connect(&sql, &QxSql::dbRecChng, this, [this, source](const qf::core::sql::QxRecChng &recchng) { emit qxRecChng(recchng, source); }); - return sql.deleteRecord(table, id, uuidString()); -} + if (m_qxSql == nullptr) { + m_qxSql = new qf::core::sql::QxSql(uuidString(), {}, this); + } + return m_qxSql; +} + +// qint64 Application::createDbRecord(const QString &table, const QVariantMap &record, QObject *source) +// { +// using namespace qf::core::sql; +// QxSql sql; +// connect(&sql, &QxSql::dbRecChng, this, [this, source](const qf::core::sql::QxRecChng &recchng) { emit qxRecChng(recchng, source); }); +// return sql.createRecord(table, record, uuidString()); +// } + +// std::optional Application::readDbRecord(const QString &table, qint64 id, const std::optional &fields) const +// { +// using namespace qf::core::sql; +// QxSql sql; +// return sql.readRecord(table, id, fields); +// } + +// bool Application::updateDbRecord(const QString &table, qint64 id, const QVariantMap &record, QObject *source) +// { +// using namespace qf::core::sql; +// QxSql sql; +// connect(&sql, &QxSql::dbRecChng, this, [this, source](const qf::core::sql::QxRecChng &recchng) { emit qxRecChng(recchng, source); }); +// return sql.updateRecord(table, id, record, uuidString()); +// } + +// bool Application::deleteDbRecord(const QString &table, qint64 id, QObject *source) +// { +// using namespace qf::core::sql; +// QxSql sql; +// connect(&sql, &QxSql::dbRecChng, this, [this, source](const qf::core::sql::QxRecChng &recchng) { emit qxRecChng(recchng, source); }); +// return sql.deleteRecord(table, id, uuidString()); +// } QString Application::versionString() const { return QCoreApplication::applicationVersion(); } -void Application::emitDbRecInserted(const QString &table, qint64 id, const QVariantMap &record, QObject *source) -{ - emit qxRecChng(qf::core::sql::QxRecChng{ - .table = table, - .id = id, - .record = record, - .op = qf::core::sql::RecOp::Insert, - .issuer = uuidString() - }, source); -} - -void Application::emitDbRecUpdated(const QString &table, qint64 id, const QVariantMap &record, QObject *source) -{ - emit qxRecChng(qf::core::sql::QxRecChng{ - .table = table, - .id = id, - .record = record, - .op = qf::core::sql::RecOp::Update, - .issuer = uuidString() - }, source); -} - -void Application::emitDbRecDeleted(const QString &table, qint64 id, QObject *source) -{ - emit qxRecChng(qf::core::sql::QxRecChng{ - .table = table, - .id = id, - .record = {}, - .op = qf::core::sql::RecOp::Delete, - .issuer = uuidString() - }, source); -} - -void Application::emitQxRecChng(const core::sql::QxRecChng &recchng, QObject *source) -{ - emit qxRecChng(recchng, source); -} - Application *Application::instance(bool must_exist) { auto *ret = qobject_cast(Super::instance()); diff --git a/libqf/libqfgui/src/framework/application.h b/libqf/libqfgui/src/framework/application.h index 6b3893a8c..513007e7d 100644 --- a/libqf/libqfgui/src/framework/application.h +++ b/libqf/libqfgui/src/framework/application.h @@ -12,6 +12,8 @@ class QQmlEngine; +namespace qf::core::sql { class QxSql; } + namespace qf { namespace gui { namespace framework { @@ -28,15 +30,13 @@ class QFGUI_DECL_EXPORT Application : public QApplication explicit Application(int & argc, char ** argv); ~Application() override = default; - Q_SIGNAL void qxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); - qint64 createDbRecord(const QString &table, const QVariantMap &record, QObject *source); - std::optional readDbRecord(const QString &table, qint64 id, const std::optional &fields = std::nullopt) const; - bool updateDbRecord(const QString &table, qint64 id, const QVariantMap &record, QObject *source); - bool deleteDbRecord(const QString &table, qint64 id, QObject *source); - void emitDbRecInserted(const QString &table, qint64 id, const QVariantMap &record, QObject *source); - void emitDbRecUpdated(const QString &table, qint64 id, const QVariantMap &record, QObject *source); - void emitDbRecDeleted(const QString &table, qint64 id, QObject *source); - void emitQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); + qf::core::sql::QxSql *qxSql(); + + // Q_SIGNAL void qxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); + // qint64 createDbRecord(const QString &table, const QVariantMap &record, QObject *source); + // std::optional readDbRecord(const QString &table, qint64 id, const std::optional &fields = std::nullopt) const; + // bool updateDbRecord(const QString &table, qint64 id, const QVariantMap &record, QObject *source); + // bool deleteDbRecord(const QString &table, qint64 id, QObject *source); Q_INVOKABLE QString versionString() const; public: @@ -53,6 +53,7 @@ class QFGUI_DECL_EXPORT Application : public QApplication static QString uuidString(); protected: MainWindow* m_frameWork = nullptr; + qf::core::sql::QxSql *m_qxSql = nullptr; }; }}} diff --git a/libqf/libqfgui/src/framework/datadialogwidget.h b/libqf/libqfgui/src/framework/datadialogwidget.h index 37c4c1d91..ec4cca8e0 100644 --- a/libqf/libqfgui/src/framework/datadialogwidget.h +++ b/libqf/libqfgui/src/framework/datadialogwidget.h @@ -26,7 +26,7 @@ class QFGUI_DECL_EXPORT DataDialogWidget : public DialogWidget qf::gui::model::DataDocument* dataDocument(bool throw_exc = qf::core::Exception::Throw); - Q_SLOT virtual bool load(const QVariant &id = QVariant(), int mode = qf::gui::model::DataDocument::ModeEdit); + virtual bool load(const QVariant &id = QVariant(), int mode = qf::gui::model::DataDocument::ModeEdit); bool acceptDialogDone(int result) Q_DECL_OVERRIDE; diff --git a/libqf/libqfgui/src/framework/dialogwidget.cpp b/libqf/libqfgui/src/framework/dialogwidget.cpp index f46cd8f23..8d5769997 100644 --- a/libqf/libqfgui/src/framework/dialogwidget.cpp +++ b/libqf/libqfgui/src/framework/dialogwidget.cpp @@ -9,13 +9,13 @@ using namespace qf::gui::framework; -DialogWidget::DialogWidget(QWidget *parent) : - Super(parent), IPersistentSettings(this) +DialogWidget::DialogWidget(QWidget *parent) + : Super(parent) + , IPersistentSettings(this) { } -DialogWidget::~DialogWidget() -= default; +DialogWidget::~DialogWidget() = default; bool DialogWidget::acceptDialogDone(int result) { @@ -23,12 +23,7 @@ bool DialogWidget::acceptDialogDone(int result) Q_UNUSED(result); return true; } -/* -QVariant DialogWidget::acceptDialogDone_qml(const QVariant &result) -{ - return acceptDialogDone(result.toBool()); -} -*/ + void DialogWidget::settleDownInDialog_qml(const QVariant &dlg) { auto *o = dlg.value(); diff --git a/libqf/libqfgui/src/framework/dialogwidget.h b/libqf/libqfgui/src/framework/dialogwidget.h index 13893c8a0..611be6b95 100644 --- a/libqf/libqfgui/src/framework/dialogwidget.h +++ b/libqf/libqfgui/src/framework/dialogwidget.h @@ -32,7 +32,7 @@ class QFGUI_DECL_EXPORT DialogWidget : public Frame, public IPersistentSettings typedef Frame Super; public: explicit DialogWidget(QWidget *parent = nullptr); - ~DialogWidget() Q_DECL_OVERRIDE; + ~DialogWidget() override; QF_PROPERTY_IMPL(QString, t, T, itle) QF_PROPERTY_IMPL(QString, i, I, conSource) diff --git a/libqf/libqfgui/src/framework/dockwidget.h b/libqf/libqfgui/src/framework/dockwidget.h index 1364ec5ee..bc4067236 100644 --- a/libqf/libqfgui/src/framework/dockwidget.h +++ b/libqf/libqfgui/src/framework/dockwidget.h @@ -22,12 +22,12 @@ class QFGUI_DECL_EXPORT DockWidget : public QDockWidget, public framework::IPers public: explicit DockWidget(const QString &window_title, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); explicit DockWidget(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()) : DockWidget(QString(), parent, flags) {} - ~DockWidget() Q_DECL_OVERRIDE; + ~DockWidget() override; // visibilityChanged() exists already in QDockWidget //Q_SIGNAL void visibleChanged(bool visible); protected: - bool event(QEvent *ev) Q_DECL_OVERRIDE; + bool event(QEvent *ev) override; //void showEvent(QShowEvent *ev) Q_DECL_OVERRIDE; private: void setQmlWidget(QWidget *w); diff --git a/libqf/libqfgui/src/framework/ipersistentsettings.cpp b/libqf/libqfgui/src/framework/ipersistentsettings.cpp index c639c374c..2d66f0235 100644 --- a/libqf/libqfgui/src/framework/ipersistentsettings.cpp +++ b/libqf/libqfgui/src/framework/ipersistentsettings.cpp @@ -5,7 +5,7 @@ #include #include -using namespace qf::gui::framework; +namespace qf::gui::framework { IPersistentSettings::IPersistentSettings(QObject *controlled_object) : m_controlledObject(controlled_object) @@ -39,7 +39,8 @@ QString IPersistentSettings::persistentSettingsPath() return m_persistentSettingsPath; } -static void callMethodRecursively(QObject *obj, const char *method_name) +namespace { +void callMethodRecursively(QObject *obj, const char *method_name) { if(!obj) return; @@ -48,7 +49,7 @@ static void callMethodRecursively(QObject *obj, const char *method_name) QMetaMethod mm = obj->metaObject()->method(ix); mm.invoke(obj); } - Q_FOREACH(auto *o, obj->children()) { + for (auto *o : obj->children()) { //static int level = 0; //level++; //QString indent = QString(level, ' '); @@ -57,6 +58,7 @@ static void callMethodRecursively(QObject *obj, const char *method_name) //level--; } } +} void IPersistentSettings::loadPersistentSettingsRecursively() { @@ -103,28 +105,24 @@ QString IPersistentSettings::rawPersistentSettingsPath() QString persistent_id = persistentSettingsId(); QStringList raw_path; if(!persistent_id.isEmpty()) { - for(QObject *obj=m_controlledObject->parent(); obj!=nullptr; obj=obj->parent()) { + for(QObject* obj = m_controlledObject->parent(); obj != nullptr; obj = obj->parent()) { auto *ps = dynamic_cast(obj); if(ps) { QString pp = ps->rawPersistentSettingsPath(); - if(!pp.isEmpty()) + if(!pp.isEmpty()) { raw_path.insert(0, pp); - //qfWarning() << "reading property 'persistentSettingsId' error" << obj << "casted to IPersistentSettings" << ps; - //qfWarning() << "\tcorrect value should be:" << parent_id; + } break; } - QVariant vid = obj->property("persistentSettingsId"); - QString parent_id = vid.toString(); - if(!parent_id.isEmpty()) { - raw_path.insert(0, parent_id); - } - - // reading property using QQmlProperty is crashing my app Qt 5.3.1 commit a83826dad0f62d7a96f5a6093240e4c8f7f2e06e - //QQmlProperty p(obj, "persistentSettingsId"); - //QVariant v2 = p.read(); + QVariant vid = obj->property("persistentSettingsId"); + QString parent_id = vid.toString(); + if(!parent_id.isEmpty()) { + raw_path.insert(0, parent_id); + } } raw_path.append(persistent_id); } return raw_path.join('/'); } +} diff --git a/libqf/libqfgui/src/framework/ipersistentsettings.h b/libqf/libqfgui/src/framework/ipersistentsettings.h index 239dae588..272bd7efd 100644 --- a/libqf/libqfgui/src/framework/ipersistentsettings.h +++ b/libqf/libqfgui/src/framework/ipersistentsettings.h @@ -7,9 +7,7 @@ class QObject; -namespace qf { -namespace gui { -namespace framework { +namespace qf::gui::framework { class QFGUI_DECL_EXPORT IPersistentSettings { @@ -38,6 +36,6 @@ class QFGUI_DECL_EXPORT IPersistentSettings QString m_persistentSettingsPath; }; -}}} +} #endif diff --git a/libqf/libqfgui/src/model/sqldatadocument.h b/libqf/libqfgui/src/model/sqldatadocument.h index ee56ac5b5..2a2b755e1 100644 --- a/libqf/libqfgui/src/model/sqldatadocument.h +++ b/libqf/libqfgui/src/model/sqldatadocument.h @@ -3,9 +3,7 @@ #include "datadocument.h" #include "sqltablemodel.h" -namespace qf { -namespace gui { -namespace model { +namespace qf::gui::model { class QFGUI_DECL_EXPORT SqlDataDocument : public DataDocument { @@ -21,7 +19,7 @@ class QFGUI_DECL_EXPORT SqlDataDocument : public DataDocument qf::core::sql::QueryBuilder queryBuilder(); void setQueryBuilder(const qf::core::sql::QueryBuilder &qb); protected: - SqlTableModel* createModel(QObject *parent) Q_DECL_OVERRIDE; + SqlTableModel* createModel(QObject *parent) override; ///! load model persistent storage via model bool loadData() Q_DECL_OVERRIDE; @@ -35,5 +33,5 @@ class QFGUI_DECL_EXPORT SqlDataDocument : public DataDocument */ }; -}}} +} diff --git a/libqf/libqfgui/src/model/sqltablemodel.cpp b/libqf/libqfgui/src/model/sqltablemodel.cpp index 794a0b83e..e2d3d25dc 100644 --- a/libqf/libqfgui/src/model/sqltablemodel.cpp +++ b/libqf/libqfgui/src/model/sqltablemodel.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include @@ -162,8 +164,7 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) int serial_ix = -1; bool serial_ix_explicitly_set = false; int primary_ix = -1; - //QSqlIndex pri_ix = ti.primaryIndex(); - //bool has_blob_field = false; + QVariantMap qx_record; for (const auto &fld : row_ref.fields()) { i++; if(fld.tableId() != table_id) @@ -198,6 +199,7 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) new_fld.setValue(v); //qfInfo() << "\t\t" << "val is QString:" << (v.metaType().id() == QMetaType::QString); rec.append(new_fld); + qx_record[fld.shortName().toLower()] = v; } } @@ -213,43 +215,26 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) } else { qs = sqldrv->sqlStatement(QSqlDriver::InsertStatement, table, rec, false); - //qs = fixSerialDefaultValue(qs, serial_ix, rec); } - if(qs.isEmpty()) + if(qs.isEmpty()) { continue; - /* - qfDebug() << "\texecuting prepared query:" << qs; - bool ok = q.prepare(qs); - if(!ok) { - qfError() << "Cannot prepare query:" << qs; } - else { - for(int i=0; i= 0 && !serial_ix_explicitly_set) { - QVariant v = q.lastInsertId(); - qfDebug() << "\tsetting serial index:" << serial_ix << "to generated value:" << v; - if(v.isValid()) { - row_ref.setValue(serial_ix, v); + auto insert_id = q.lastInsertId(); + qfDebug() << "\tsetting serial index:" << serial_ix << "to generated value:" << insert_id; + if(insert_id.isValid()) { + row_ref.setValue(serial_ix, insert_id); row_ref.setDirty(serial_ix, false); if (auto *app = qobject_cast(QApplication::instance()); app) { - app->emitDbRecInserted(table_id, v.value(), record_to_map(rec), this); + app->qxSql()->emitRecInserted(table_id, insert_id.value(), record_to_map(rec), this); } } else { @@ -283,7 +268,7 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) QSqlDriver *sqldrv = sql_conn.driver(); for (const auto &table_id : tableIds(m_table.fields())) { qfDebug() << "\ttableid:" << table_id; - //table = conn.fullTableNameToQtDriverTableName(table); + QVariantMap qx_record; QSqlRecord edit_rec; int i = -1; for (const auto &fld : row_ref.fields()) { @@ -301,12 +286,13 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) //qfDebug() << "\ttableid:" << tableid << "fullTableName:" << fld.fullTableName(); qfDebug() << "\tdirty field" << fld.name() << "type:" << fld.type().id() << "orig val:" << row_ref.origValue(i).toString() << "new val:" << v.toString(); //qfDebug().noSpace() << "\tdirty value: '" << v.toString() << "' isNull(): " << v.isNull() << " type(): " << v.type(); - QSqlField sqlfld(fld.shortName(), fld.type()); + QSqlField sqlfld(fld.shortName().toLower(), fld.type()); sqlfld.setValue(v); //if(sqlfld.type() == QVariant::ByteArray) // has_blob_field = true; qfDebug() << "\tfield is null: " << sqlfld.isNull(); edit_rec.append(sqlfld); + qx_record[fld.shortName()] = v; } if(!edit_rec.isEmpty()) { qfDebug() << "updating table edits:" << table_id; @@ -315,6 +301,7 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) query_str += " "; QSqlRecord where_rec; qfDebug() << "looking for primary index of table:" << table_id; + std::optional id_pri_key_value; for(const auto &fld_name : sql_conn.primaryIndexFieldNames(table_id)) { QString full_fld_name = table_id + '.' + fld_name; qfDebug() << "\t checking value of field:" << full_fld_name; @@ -328,6 +315,9 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) sqlfld.setValue(row_ref.origValue(fld_ix)); qfDebug() << "\tpri index field" << full_fld_name << "type:" << sqlfld.metaType().id() << "orig val:" << row_ref.origValue(fld_ix) << "current val:" << row_ref.value(fld_ix); where_rec.append(sqlfld); + if (auto id = sqlfld.value().toInt(); id > 0) { + id_pri_key_value = id; + } } QF_ASSERT(!where_rec.isEmpty(), QString("pri keys values not generated for table '%1'").arg(table_id), @@ -348,7 +338,7 @@ bool SqlTableModel::postRow(int row_no, bool throw_exc) if (num_rows_affected == 1) { if (where_rec.count() == 1) { if (auto *app = qobject_cast(QApplication::instance()); app) { - app->emitDbRecUpdated(table_id, where_rec.value(0).value(), record_to_map(edit_rec), this); + app->qxSql()->emitRecUpdated(table_id, where_rec.value(0).value(), record_to_map(edit_rec), this); } } } @@ -457,7 +447,7 @@ bool SqlTableModel::removeTableRow(int row_no, bool throw_exc) if (num_rows_affected == 1) { if (where_rec.count() == 1) { if (auto *app = qobject_cast(QApplication::instance()); app) { - app->emitDbRecDeleted(table_id, where_rec.value(0).value(), this); + app->qxSql()->emitRecDeleted(table_id, where_rec.value(0).value(), this); } } } else { diff --git a/libqf/libqfgui/src/model/sqltablemodel.h b/libqf/libqfgui/src/model/sqltablemodel.h index 439b87c6f..ce9e2313f 100644 --- a/libqf/libqfgui/src/model/sqltablemodel.h +++ b/libqf/libqfgui/src/model/sqltablemodel.h @@ -10,12 +10,10 @@ #include #include -namespace qf { -namespace gui { -namespace sql { -class Connection; -} -namespace model { +namespace qf::core::sql { struct QxRecChng; } +namespace qf::gui::sql { class Connection; } + +namespace qf::gui::model { class QFGUI_DECL_EXPORT SqlTableModel : public TableModel { @@ -100,5 +98,5 @@ class QFGUI_DECL_EXPORT SqlTableModel : public TableModel QMap m_foreignKeyDependencies; }; -}}} +} diff --git a/libqf/libqfgui/src/model/tablemodel.cpp b/libqf/libqfgui/src/model/tablemodel.cpp index 2fff88940..1eebdbfca 100644 --- a/libqf/libqfgui/src/model/tablemodel.cpp +++ b/libqf/libqfgui/src/model/tablemodel.cpp @@ -672,7 +672,7 @@ QColor TableModel::contrastTextColor(const QColor &background_color) return (qGray(background_color.rgb()) < 128)? QColor(Qt::white) : QColor(Qt::black); } -void TableModel::handleQxRecChng(const core::sql::QxRecChng &recchng, QObject *source) +void TableModel::applyQxRecChng(const core::sql::QxRecChng &recchng, QObject *source) { // qfInfo() << __FUNCTION__ << source; if (source == this) { @@ -685,12 +685,28 @@ void TableModel::handleQxRecChng(const core::sql::QxRecChng &recchng, QObject *s return i; } } + qfMessage() << "cannot find table row with id:" << id; } else { - qfWarning() << "cannot find table column:" << (table_name + ".id"); + // not every change is addressing every table model + qfMessage() << metaObject()->className() << objectName() << "cannot find table column:" << (table_name + ".id"); } return -1; }; - if (recchng.op == qf::core::sql::RecOp::Update) { + if (recchng.op == qf::core::sql::RecOp::Insert) { + if (int row = find_row_by_id(recchng.table, recchng.id); row >= 0) { + // row exists already + return; + } + if (auto fld_ix = table().fields().fieldIndex(recchng.table + ".id"); fld_ix >= 0) { + beginInsertRows({}, 0, 0); + auto row = insertTableRow(0); + tableRowRef(row).setValue(fld_ix, recchng.id); + endInsertRows(); + qfMessage() << "reloading inserted row:" << row << "table:" << recchng.table; + reloadRow(row); + } + } + else if (recchng.op == qf::core::sql::RecOp::Update) { if (int row = find_row_by_id(recchng.table, recchng.id); row >= 0) { for (const auto &[k, v] : recchng.record.asKeyValueRange()) { if (auto col_ix = columnIndexOfTableField(recchng.table + '.' + k); col_ix.has_value()) { @@ -707,12 +723,18 @@ void TableModel::handleQxRecChng(const core::sql::QxRecChng &recchng, QObject *s } } else { // calculated column - qfInfo() << "reloading calculated field, row:" << row << (recchng.table + '.' + k) << "-->" << v; + qfMessage() << "reloading calculated field, row:" << row << (recchng.table + '.' + k) << "-->" << v; reloadRow(row); } } } } + else if (recchng.op == qf::core::sql::RecOp::Delete) { + if (int row = find_row_by_id(recchng.table, recchng.id); row >= 0) { + qfMessage() << "deleteing row:" << row << (recchng.table + ".id") << recchng.id; + removeRowNoOverload(row, !qf::core::Exception::Throw); + } + } } QVariant TableModel::rawValueToEdit(int column_index, const QVariant &val) const diff --git a/libqf/libqfgui/src/model/tablemodel.h b/libqf/libqfgui/src/model/tablemodel.h index fc2c4b805..fded27d00 100644 --- a/libqf/libqfgui/src/model/tablemodel.h +++ b/libqf/libqfgui/src/model/tablemodel.h @@ -209,7 +209,7 @@ class QFGUI_DECL_EXPORT TableModel : public QAbstractTableModel static QColor contrastTextColor(const QColor &background_color); - void handleQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); + void applyQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); protected: virtual void checkColumns(); void createColumnsFromTableFields(); diff --git a/libqf/libqfgui/src/tableview.cpp b/libqf/libqfgui/src/tableview.cpp index 3dabbcadf..17e40e82a 100644 --- a/libqf/libqfgui/src/tableview.cpp +++ b/libqf/libqfgui/src/tableview.cpp @@ -633,40 +633,37 @@ void TableView::paste() void TableView::setValueInSelection_helper(const QVariant &new_val) { + auto *sql_m = qobject_cast(tableModel()); + if (!sql_m) { + return; + } QModelIndexList lst = selectedIndexes(); QMap row_selections; foreach(const QModelIndex &ix, lst) { row_selections[ix.row()] << ix; } - QList selected_row_indexes = row_selections.keys(); - if(selected_row_indexes.count() == 1) { - int row_ix = selected_row_indexes.value(0); - foreach(const QModelIndex &ix, row_selections.value(row_ix)) { - model()->setData(ix, new_val); - } - } - else if(selected_row_indexes.count() > 1) { - qfc::sql::Connection conn; - auto *sql_m = qobject_cast(tableModel()); - if(sql_m) { - try { - conn = sql_m->sqlConnection(); - qfc::sql::Transaction transaction(conn); - QF_TIME_SCOPE(QString("Saving %1 rows").arg(selected_row_indexes.count())); - foreach(int row_ix, selected_row_indexes) { - foreach(const QModelIndex &ix, row_selections.value(row_ix)) { - model()->setData(ix, new_val); - } - sql_m->postRow(toTableModelRowNo(row_ix), qf::core::Exception::Throw); - } - transaction.commit(); - } - catch(qfc::Exception &e) { - dialogs::MessageBox::showException(this, e); + auto *proxy_m = qobject_cast(lastProxyModel()); + Q_ASSERT(proxy_m); + auto is_dynamic_sort_filter = proxy_m->dynamicSortFilter(); + proxy_m->setDynamicSortFilter(false); + qfc::sql::Connection conn; + try { + conn = sql_m->sqlConnection(); + qfc::sql::Transaction transaction(conn); + QF_TIME_SCOPE(QString("Saving %1 rows").arg(selected_row_indexes.count())); + for(const auto &[row_ix, row_indexes] : row_selections.asKeyValueRange()) { + for(const auto &ix : row_indexes) { + proxy_m->setData(ix, new_val); } + sql_m->postRow(toTableModelRowNo(row_ix), qf::core::Exception::Throw); } - update(); + transaction.commit(); + } + catch(qfc::Exception &e) { + dialogs::MessageBox::showException(this, e); } + proxy_m->setDynamicSortFilter(is_dynamic_sort_filter); + update(); } void TableView::setValueInSelection() @@ -1154,7 +1151,7 @@ void TableView::rowExternallySaved(const QVariant &id) qf::core::sql::Query q; bool ok = q.exec(query_str); if (!ok) { - qfInfo() << "Query:" << query_str; + qfMessage() << "Query:" << query_str; qfWarning() << "SQL error:" << q.lastErrorText(); return; } diff --git a/libquickevent/libquickeventgui/src/reportoptionsdialog.h b/libquickevent/libquickeventgui/src/reportoptionsdialog.h index e25ef7ee2..e13fd6dad 100644 --- a/libquickevent/libquickeventgui/src/reportoptionsdialog.h +++ b/libquickevent/libquickeventgui/src/reportoptionsdialog.h @@ -87,13 +87,13 @@ class QUICKEVENTGUI_DECL_EXPORT ReportOptionsDialog : public QDialog, public qf: explicit ReportOptionsDialog(QWidget *parent = nullptr); ~ReportOptionsDialog() override; - int exec() Q_DECL_OVERRIDE; + int exec() override; void setStartListPrintVacantsVisible(bool b); void setStartListForRelays(); - QString persistentSettingsPath() Q_DECL_OVERRIDE; - bool setPersistentSettingsId(const QString &id) Q_DECL_OVERRIDE; + QString persistentSettingsPath() override; + bool setPersistentSettingsId(const QString &id) override; Q_SIGNAL void persistentSettingsIdChanged(const QString &id); void setOptions(const Options &options); @@ -129,7 +129,7 @@ class QUICKEVENTGUI_DECL_EXPORT ReportOptionsDialog : public QDialog, public qf: static QString sqlWhereExpression(const Options &opts, const int stage_id); static QString getClassesForStartNumber(const int number, const int stage_id); protected: - //void showEvent(QShowEvent *event) Q_DECL_OVERRIDE; + //void showEvent(QShowEvent *event) override; private: Ui::ReportOptionsDialog *ui; }; diff --git a/quickevent/app/quickevent/CMakeLists.txt b/quickevent/app/quickevent/CMakeLists.txt index 5368f5fe7..a12ccb291 100644 --- a/quickevent/app/quickevent/CMakeLists.txt +++ b/quickevent/app/quickevent/CMakeLists.txt @@ -99,15 +99,15 @@ add_executable(quickevent plugins/Event/src/services/serviceswidget.cpp plugins/Event/src/services/servicewidget.cpp plugins/Event/src/services/servicewidget.ui - plugins/Event/src/services/qx/qxclientservice.cpp - plugins/Event/src/services/qx/qxclientservice.h - plugins/Event/src/services/qx/qxclientservicewidget.cpp - plugins/Event/src/services/qx/qxclientservicewidget.h - plugins/Event/src/services/qx/qxclientservicewidget.ui - plugins/Event/src/services/qx/qxlateregistrationswidget.h - plugins/Event/src/services/qx/qxlateregistrationswidget.cpp - plugins/Event/src/services/qx/qxlateregistrationswidget.ui - plugins/Event/src/services/qx/runchangedialog.h plugins/Event/src/services/qx/runchangedialog.cpp plugins/Event/src/services/qx/runchangedialog.ui + + plugins/Event/src/services/qx/qxnode.cpp + plugins/Event/src/services/qx/sqlapinode.cpp + plugins/Event/src/services/qx/sqlapi.cpp plugins/Event/src/services/qx/sqlapi.h + plugins/Event/src/services/qx/nodes.cpp + plugins/Event/src/services/qx/qxeventservice.cpp plugins/Event/src/services/qx/qxeventservice.h + plugins/Event/src/services/qx/qxeventservicewidget.cpp plugins/Event/src/services/qx/qxeventservicewidget.h plugins/Event/src/services/qx/qxeventservicewidget.ui + plugins/Event/src/services/qx/qxlateentrieswidget.h plugins/Event/src/services/qx/qxlateentrieswidget.cpp plugins/Event/src/services/qx/qxlateentrieswidget.ui + plugins/Event/src/services/qx/lateentrydialog.h plugins/Event/src/services/qx/lateentrydialog.cpp plugins/Event/src/services/qx/lateentrydialog.ui plugins/Event/src/services/qx/runchange.h plugins/Event/src/services/qx/runchange.cpp plugins/Event/src/services/punchingtest/punchingtestservice.h plugins/Event/src/services/punchingtest/punchingtestservice.cpp plugins/Event/src/services/punchingtest/punchingtestservicewidget.h plugins/Event/src/services/punchingtest/punchingtestservicewidget.cpp @@ -128,10 +128,11 @@ add_executable(quickevent plugins/Receipts/src/receiptssettings.cpp plugins/Receipts/src/receiptssettingspage.cpp plugins/Receipts/src/receiptssettingspage.ui - plugins/Receipts/src/thirdparty/qrcodegen.cpp plugins/Receipts/src/receiptswidget.cpp plugins/Receipts/src/receiptswidget.ui + plugins/Receipts/src/thirdparty/qrcodegen.cpp + plugins/Relays/src/addlegdialogwidget.cpp plugins/Relays/src/addlegdialogwidget.ui plugins/Relays/src/relaydocument.cpp @@ -274,6 +275,10 @@ qt6_add_lupdate(quickevent TS_FILES ${TS_FILES}) target_sources(quickevent PRIVATE ${QM_FILES}) target_include_directories(quickevent PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src) target_link_libraries(quickevent PUBLIC libquickeventcore libquickeventgui libqfgui libsiut) +if (QF_WITH_LIBSHV) + target_link_libraries(quickevent PUBLIC libshviotqt) + target_compile_definitions(quickevent PRIVATE QF_WITH_LIBSHV) +endif() # Extract version from appversion.h file(READ "${CMAKE_CURRENT_SOURCE_DIR}/src/appversion.h" APP_VERSION_H) diff --git a/quickevent/app/quickevent/plugins/CardReader/src/cardreaderplugin.cpp b/quickevent/app/quickevent/plugins/CardReader/src/cardreaderplugin.cpp index 11148b97a..7bd6ec96f 100644 --- a/quickevent/app/quickevent/plugins/CardReader/src/cardreaderplugin.cpp +++ b/quickevent/app/quickevent/plugins/CardReader/src/cardreaderplugin.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -280,7 +281,7 @@ int CardReaderPlugin::saveCardToSql(const quickevent::core::si::ReadCard &read_c {"runIdAssignError", read_card.runIdAssignError()}, {"data", qf::core::Utils::qvariantToJson(read_card.data())}, }; - auto id = app->createDbRecord("cards", rec, this); + auto id = app->qxSql()->createRecord("cards", rec, this); return id; } catch (const std::exception &e) { @@ -333,7 +334,7 @@ int CardReaderPlugin::savePunchRecordToSql(const quickevent::core::si::PunchReco {"timeMs", punch.timems()}, {"runTimeMs", punch.runtimems_isset()? punch.runtimems(): QVariant()}, }; - auto id = app->createDbRecord("punches", rec, this); + auto id = app->qxSql()->createRecord("punches", rec, this); return id; } catch (const std::exception &e) { @@ -406,7 +407,7 @@ void CardReaderPlugin::updateCheckedCardValuesSql(int card_id, const quickevent: {"notStart", false}, {"penaltyTimeMs", {}}, }; - app->updateDbRecord("runs", run_id, rec, this); + app->qxSql()->updateRecord("runs", run_id, rec, this); } if (auto missing_codes = checked_card.missingCodes(); !missing_codes.isEmpty()) { QStringList missing_str; @@ -417,7 +418,7 @@ void CardReaderPlugin::updateCheckedCardValuesSql(int card_id, const quickevent: QVariantMap rec { {"runIdAssignError", tr("Missing codes: %1").arg(missing_str.join(','))}, }; - app->updateDbRecord("cards", card_id, rec, this); + app->qxSql()->updateRecord("cards", card_id, rec, this); } } catch (const std::exception &e) { @@ -463,7 +464,7 @@ bool CardReaderPlugin::saveCardAssignedRunnerIdSql(int card_id, int run_id) {"runId", run_id}, {"runIdAssignTS", QDateTime::currentDateTime()}, }; - app->updateDbRecord("cards", card_id, rec, this); + app->qxSql()->updateRecord("cards", card_id, rec, this); return true; } catch (const std::exception &e) { @@ -540,7 +541,7 @@ void CardReaderPlugin::setStartTime(int relay_id, int leg, int start_time) { {"startTimeMs", start_time}, }; auto run_id = q.value(0).toInt(); - app->updateDbRecord("runs", run_id, rec, this); + app->qxSql()->updateRecord("runs", run_id, rec, this); } catch (const std::exception &e) { qfError() << "setStartTime(): Update runs error, query:" << e.what(); diff --git a/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.cpp b/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.cpp index 45ec6ea0d..c5e9f37b0 100644 --- a/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.cpp +++ b/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.cpp @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -111,6 +112,7 @@ class Model : public quickevent::gui::og::SqlTableModel Model::Model(QObject *parent) : Super(parent) { + setIdColumnName("cards.id"); clearColumns(col_COUNT); setColumn(col_cards_id, ColumnDefinition("cards.id", "id").setReadOnly(true)); setColumn(col_cards_siId, ColumnDefinition("cards.siId", tr("SI")).setReadOnly(true).setCastType(qMetaTypeId())); @@ -261,7 +263,7 @@ CardReaderWidget::CardReaderWidget(QWidget *parent) } }, Qt::QueuedConnection); - connect(qf::gui::framework::Application::instance(), &qf::gui::framework::Application::qxRecChng, this, &CardReaderWidget::onQxRecChng, Qt::QueuedConnection); + connect(qf::gui::framework::Application::instance()->qxSql(), &qf::core::sql::QxSql::recChng, this, &CardReaderWidget::onQxRecChng, Qt::QueuedConnection); } CardReaderWidget::~CardReaderWidget() @@ -474,7 +476,7 @@ void CardReaderWidget::settleDownInPartWidget(::PartWidget *part_widget) a_tools->addActionInto(a); } } - connect(getPlugin(), &Event::EventPlugin::dbEventNotify, this, &CardReaderWidget::onDbEventNotify, Qt::QueuedConnection); + // connect(getPlugin(), &Event::EventPlugin::dbEventNotify, this, &CardReaderWidget::onDbEventNotify, Qt::QueuedConnection); } void CardReaderWidget::reset() @@ -520,20 +522,20 @@ void CardReaderWidget::reload() void CardReaderWidget::onQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source) { if(isVisible()) { - m_cardsModel->handleQxRecChng(recchng, source); + m_cardsModel->applyQxRecChng(recchng, source); } } -void CardReaderWidget::onDbEventNotify(const QString &domain, int connection_id, const QVariant &data) -{ - Q_UNUSED(connection_id) - if(domain == QLatin1String(Event::EventPlugin::DBEVENT_CARD_READ)) { - int card_id = data.toInt(); - if(isVisible()) { - updateTableView(card_id); - } - } -} +// void CardReaderWidget::onDbEventNotify(const QString &domain, int connection_id, const QVariant &data) +// { +// Q_UNUSED(connection_id) +// if(domain == QLatin1String(Event::EventPlugin::DBEVENT_CARD_READ)) { +// int card_id = data.toInt(); +// if(isVisible()) { +// updateTableView(card_id); +// } +// } +// } void CardReaderWidget::createActions() { @@ -817,7 +819,7 @@ void CardReaderWidget::assignRunnerToSelectedCard() QVariantMap rec { {"isRunning", true}, }; - app->updateDbRecord("runs", run_id, rec, this); + app->qxSql()->updateRecord("runs", run_id, rec, this); // QString qs = "UPDATE runs SET isRunning=true WHERE competitorId=" QF_IARG(competitor_id) " AND stageId=" QF_IARG(stage_id); // q.execThrow(qs); } @@ -834,7 +836,7 @@ void CardReaderWidget::assignRunnerToSelectedCard() QVariantMap rec { {"siId", si_id}, }; auto run_id = q.value(0).toInt(); if (n++ == 0 || use_si_in_next_stages) { - app->updateDbRecord("runs", run_id, rec, this); + app->qxSql()->updateRecord("runs", run_id, rec, this); } } // QString qs = "UPDATE runs SET siId=" QF_IARG(si_id) " WHERE competitorId=" QF_IARG(competitor_id) " AND stageId=" QF_IARG(stage_id); diff --git a/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.h b/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.h index 03b7a6aa5..8911758d9 100644 --- a/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.h +++ b/quickevent/app/quickevent/plugins/CardReader/src/cardreaderwidget.h @@ -68,8 +68,8 @@ class CardReaderWidget : public QFrame Q_SLOT void reload(); private: - void onQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); - void onDbEventNotify(const QString &domain, int connection_id, const QVariant &data); + void onQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); + // void onDbEventNotify(const QString &domain, int connection_id, const QVariant &data); void appendLog(NecroLog::Level level, const QString &msg); void processDriverInfo(NecroLog::Level level, const QString &msg); void logDriverRawData(const QByteArray &data); diff --git a/quickevent/app/quickevent/plugins/Classes/src/classeswidget.cpp b/quickevent/app/quickevent/plugins/Classes/src/classeswidget.cpp index 8fc4af4be..a15b6c468 100644 --- a/quickevent/app/quickevent/plugins/Classes/src/classeswidget.cpp +++ b/quickevent/app/quickevent/plugins/Classes/src/classeswidget.cpp @@ -392,7 +392,8 @@ void ClassesWidget::importCourses(const QList &course_defs, con reload(); } -static QString normalize_course_name(const QString &course_name) +namespace { +QString normalize_course_name(const QString &course_name) { QString ret = qf::core::Collator::toAscii7(QLocale::Czech, course_name, false); ret.replace(' ', QString()); @@ -402,6 +403,7 @@ static QString normalize_course_name(const QString &course_name) ret.replace('-', '+'); return ret; } +} void ClassesWidget::import_ocad_txt() { @@ -568,7 +570,8 @@ void ClassesWidget::import_ocad_v8() } } -static QString element_text(const QDomElement &parent, const QString &tag_name) +namespace { +QString element_text(const QDomElement &parent, const QString &tag_name) { QDomElement el = parent.firstChildElement(tag_name); if(el.isNull()) @@ -576,13 +579,14 @@ static QString element_text(const QDomElement &parent, const QString &tag_name) return el.text(); } -static QString dump_element(const QDomElement &el) +QString dump_element(const QDomElement &el) { QString ret; QTextStream s(&ret); el.save(s, QDomNode::EncodingFromDocument); return ret; } +} void ClassesWidget::import_ocad_iofxml_2() { diff --git a/quickevent/app/quickevent/plugins/Classes/src/editcourseswidget.cpp b/quickevent/app/quickevent/plugins/Classes/src/editcourseswidget.cpp index 05d6ebe35..98cbc1029 100644 --- a/quickevent/app/quickevent/plugins/Classes/src/editcourseswidget.cpp +++ b/quickevent/app/quickevent/plugins/Classes/src/editcourseswidget.cpp @@ -151,7 +151,7 @@ void EditCoursesWidget::updateQuery() .join("courses.id", "classdefs.courseId", qf::core::sql::QueryBuilder::INNER_JOIN) .join("classdefs.classId", "classes.id") .join("classes.id", "competitors.classId") - .joinRestricted("competitors.id", "runs.competitorId", "runs.isRunning") + .joinRestricted("competitors.id", "runs.competitorId", "runs.isRunning AND runs.stageId=" QF_IARG(m_stageId)) .where("classdefs.stageId=" QF_IARG(m_stageId)) .groupBy("courses.id") .orderBy("courses.name"); diff --git a/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.cpp b/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.cpp index 58d837025..cae50bfde 100644 --- a/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.cpp +++ b/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.cpp @@ -1,5 +1,6 @@ #include "competitordocument.h" +#include #include #include @@ -9,6 +10,7 @@ #include #include +#include #include #include @@ -92,7 +94,7 @@ bool CompetitorDocument::saveData() getPlugin()->emitDbEvent(Event::EventPlugin::DBEVENT_RUN_CHANGED, QVariantList {run_id, rec}); // new update API auto *app = qf::gui::framework::Application::instance(); - app->emitDbRecInserted( "runs", run_id, rec, this); + app->qxSql()->emitRecInserted( "runs", run_id, rec, this); } } } @@ -103,7 +105,7 @@ bool CompetitorDocument::saveData() auto *app = qf::gui::framework::Application::instance(); QVariantMap rec { {"siId", siid()}, }; for (const auto &[run_id, _] : old_records.asKeyValueRange()) { - app->updateDbRecord("runs", run_id, rec, this); + app->qxSql()->updateRecord("runs", run_id, rec, this); } // int competitor_id = dataId().toInt(); // qf::core::sql::Query q(sqlModel()->connectionName()); @@ -175,7 +177,7 @@ bool CompetitorDocument::dropData() getPlugin()->emitDbEvent(Event::EventPlugin::DBEVENT_RUN_CHANGED, QVariantList {run_id, {}}); // new update API auto *app = qf::gui::framework::Application::instance(); - app->emitDbRecDeleted("runs", run_id, this); + app->qxSql()->emitRecDeleted("runs", run_id, this); } } } @@ -212,4 +214,101 @@ QVariant CompetitorDocument::siid() const return value(SIID); } +QList CompetitorDocument::possibleStartTimesMs(int run_id) +{ + using namespace qf::core::sql; + + int class_id; + int stage_id; + int start_time; + QString club_abbr; + { + qf::core::sql::QueryBuilder qb; + qb.select2("runs", "competitorId, stageId, startTimeMs") + .select2("competitors", "classId, registration") + .from("runs") + .joinRestricted("runs.competitorId", "competitors.id", "runs.id=" QF_IARG(run_id), qf::core::sql::QueryBuilder::INNER_JOIN) + .where("runs.isRunning"); + Query q; + q.exec(qb.toString(), qf::core::Exception::Throw); + if (q.next()) { + class_id = q.value("classId").toInt(); + stage_id = q.value("stageId").toInt(); + start_time = q.value("startTimeMs").toInt(); + club_abbr = q.value("registration").toString().mid(0, 3).toUpper(); + } + else { + return {}; + } + } + + Classes::ClassDef class_def; + class_def.load(class_id, stage_id, false); + + if (class_def.classInterval == 0) { + return {}; + } + + struct S { + QString club; + int start_time = 0; + + bool operator<(const S &o) const { return start_time < o.start_time; } + }; + QList runs; + { + qf::core::sql::QueryBuilder qb; + qb.select2("runs", "startTimeMs") + .select2("competitors", "registration") + .from("runs") + .joinRestricted("runs.competitorId", "competitors.id", "competitors.classId=" QF_IARG(class_id), qf::core::sql::QueryBuilder::INNER_JOIN) + .where("runs.stageId=" QF_IARG(stage_id)) + .where("runs.isRunning") + .orderBy("runs.startTimeMs"); + //qfInfo() << qb.toString(); + Query q; + q.exec(qb.toString(), qf::core::Exception::Throw); + while (q.next()) { + //auto start_time = q.value("startTimeMs").toInt(); + //if (start_time < class_def.classStartFirst) { + // continue; + //} + runs << S { + .club = q.value("registration").toString().mid(0, 3).toUpper(), + .start_time = q.value("startTimeMs").toInt() + }; + } + std::sort(runs.begin(), runs.end()); + } + QList ret; + auto start_interval_ms = class_def.classInterval * 60 * 1000; + for (auto stime = class_def.classStartFirst; stime <= class_def.classStartLast; stime += class_def.classInterval) { + S r { .club = {}, .start_time = stime }; + auto [lo, up] = std::equal_range(runs.begin(), runs.end(), r); + if (lo == up || stime == start_time) { + // stime does not exist or it is my current start_time, should be inserted before up + // check next club + // qfInfo() << "free stime:" << quickevent::core::og::TimeMs(stime).toString(); + if (up != runs.end()) { + // qfInfo() << "next club:" << up->club; + if (up->club == club_abbr && (up->start_time - stime) <= start_interval_ms) { + // qfInfo() << "same next club:" << club_abbr; + continue; + } + } + // check prev club + if (up != runs.begin()) { + up--; + // qfInfo() << "prev club:" << up->club; + if (up->club == club_abbr && (stime - up->start_time) <= start_interval_ms) { + // qfInfo() << "same prev club:" << club_abbr; + continue; + } + } + ret << stime; + } + } + return ret; +} + diff --git a/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.h b/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.h index a394edbe4..8894d63d3 100644 --- a/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.h +++ b/quickevent/app/quickevent/plugins/Competitors/src/competitordocument.h @@ -1,5 +1,4 @@ -#ifndef COMPETITORS_COMPETITORDOCUMENT_H -#define COMPETITORS_COMPETITORDOCUMENT_H +#pragma once #include @@ -15,13 +14,13 @@ class CompetitorDocument : public qf::gui::model::SqlDataDocument public: CompetitorDocument(QObject *parent = nullptr); - //bool isSaveSiidToRuns() const {return m_saveSiidToRuns;} void setEmitDbEventsOnSave(bool b) {m_isEmitDbEventsOnSave = b;} - //void setSiid(const QVariant &siid, bool save_siid_to_runs); void setSiid(const QVariant &siid); QVariant siid() const; const QVector& runsIds() const {return m_runsIds;} + + static QList possibleStartTimesMs(int run_id); protected: bool loadData() override; bool saveData() override; @@ -33,4 +32,3 @@ class CompetitorDocument : public qf::gui::model::SqlDataDocument } -#endif // COMPETITORS_COMPETITORDOCUMENT_H diff --git a/quickevent/app/quickevent/plugins/Competitors/src/competitorwidget.cpp b/quickevent/app/quickevent/plugins/Competitors/src/competitorwidget.cpp index 854300cf1..9b58bee11 100644 --- a/quickevent/app/quickevent/plugins/Competitors/src/competitorwidget.cpp +++ b/quickevent/app/quickevent/plugins/Competitors/src/competitorwidget.cpp @@ -376,97 +376,7 @@ QString CompetitorWidget::guessClassFromRegistration(const QString ®istration QList CompetitorWidget::possibleStartTimesMs(int run_id) { - int class_id; - int stage_id; - int start_time; - QString club_abbr; - { - qf::core::sql::QueryBuilder qb; - qb.select2("runs", "competitorId, stageId, startTimeMs") - .select2("competitors", "classId, registration") - .from("runs") - .joinRestricted("runs.competitorId", "competitors.id", "runs.id=" QF_IARG(run_id), qf::core::sql::QueryBuilder::INNER_JOIN) - .where("runs.isRunning"); - qfs::Query q; - q.exec(qb.toString(), qf::core::Exception::Throw); - if (q.next()) { - class_id = q.value("classId").toInt(); - stage_id = q.value("stageId").toInt(); - start_time = q.value("startTimeMs").toInt(); - club_abbr = q.value("registration").toString().mid(0, 3).toUpper(); - } - else { - return {}; - } - } - - Classes::ClassDef class_def; - class_def.load(class_id, stage_id, false); - - if (class_def.classInterval == 0) { - return {}; - } - - struct S { - QString club; - int start_time = 0; - - bool operator<(const S &o) const { return start_time < o.start_time; } - }; - QList runs; - { - qf::core::sql::QueryBuilder qb; - qb.select2("runs", "startTimeMs") - .select2("competitors", "registration") - .from("runs") - .joinRestricted("runs.competitorId", "competitors.id", "competitors.classId=" QF_IARG(class_id), qf::core::sql::QueryBuilder::INNER_JOIN) - .where("runs.stageId=" QF_IARG(stage_id)) - .where("runs.isRunning") - .orderBy("runs.startTimeMs"); - //qfInfo() << qb.toString(); - qfs::Query q; - q.exec(qb.toString(), qf::core::Exception::Throw); - while (q.next()) { - //auto start_time = q.value("startTimeMs").toInt(); - //if (start_time < class_def.classStartFirst) { - // continue; - //} - runs << S { - .club = q.value("registration").toString().mid(0, 3).toUpper(), - .start_time = q.value("startTimeMs").toInt() - }; - } - std::sort(runs.begin(), runs.end()); - } - QList ret; - auto start_interval_ms = class_def.classInterval * 60 * 1000; - for (auto stime = class_def.classStartFirst; stime <= class_def.classStartLast; stime += class_def.classInterval) { - S r { .club = {}, .start_time = stime }; - auto [lo, up] = std::equal_range(runs.begin(), runs.end(), r); - if (lo == up || stime == start_time) { - // stime does not exist or it is my current start_time, should be inserted before up - // check next club - // qfInfo() << "free stime:" << quickevent::core::og::TimeMs(stime).toString(); - if (up != runs.end()) { - qfInfo() << "next club:" << up->club; - if (up->club == club_abbr && (up->start_time - stime) <= start_interval_ms) { - // qfInfo() << "same next club:" << club_abbr; - continue; - } - } - // check prev club - if (up != runs.begin()) { - up--; - // qfInfo() << "prev club:" << up->club; - if (up->club == club_abbr && (stime - up->start_time) <= start_interval_ms) { - // qfInfo() << "same prev club:" << club_abbr; - continue; - } - } - ret << stime; - } - } - return ret; + return Competitors::CompetitorDocument::possibleStartTimesMs(run_id); } void CompetitorWidget::onRegistrationSelected(const QVariantMap &values) diff --git a/quickevent/app/quickevent/plugins/Event/qml/DbSchema.qml b/quickevent/app/quickevent/plugins/Event/qml/DbSchema.qml index c06846d00..dff9e5d07 100644 --- a/quickevent/app/quickevent/plugins/Event/qml/DbSchema.qml +++ b/quickevent/app/quickevent/plugins/Event/qml/DbSchema.qml @@ -40,8 +40,8 @@ Schema { defaultValue: false notNull: true }, - Field { name: 'drawingConfig'; type: String {} }, - Field { name: 'qxApiToken'; type: String {} } + Field { name: 'drawingConfig'; type: String {} } + // Field { name: 'qxApiToken'; type: String {} } ] indexes: [ Index {fields: ['id']; primary: true } @@ -494,22 +494,31 @@ Schema { fields: [ Field { name: 'id'; type: Serial { primaryKey: true } }, Field { name: 'stage_id'; type: Int { } }, - Field { name: 'change_id'; type: Int { } }, - Field { name: 'data_id'; type: Int { } }, + Field { name: 'foreign_table'; type: String { } }, + Field { name: 'foreign_id'; type: Int { } }, Field { name: 'data_type'; type: String { } }, Field { name: 'data'; type: String { } }, Field { name: 'orig_data'; type: String { } comment: 'Store data overriden by change here to enable change rollback.' }, - Field { name: 'source'; type: String { } }, Field { name: 'user_id'; type: String { } }, Field { name: 'status'; type: String { } }, Field { name: 'status_message'; type: String { } }, - Field { name: 'created'; type: DateTime { } }, + Field { name: 'created'; type: DateTime { } + notNull: true + defaultValue: 'CURRENT_TIMESTAMP' + }, Field { name: 'lock_number'; type: Int { } } ] indexes: [ - Index {fields: ['stage_id', 'change_id']; unique: true } + Index {fields: ['stage_id', 'data_type', 'foreign_id']; unique: false }, + Index {fields: ['stage_id', 'status']; unique: false }, + Index {fields: ['stage_id', 'foreign_id'] + // The WHERE clause means the index only contains rows that satisfy where condition + where: "status = 'Pending' AND data_type = 'LateEntry' AND foreign_table='runs' AND foreign_id IS NOT NULL" + comment: 'Only one pending late entry can be present for single runs.id (foreign_id).' + unique: true + } ] } ] diff --git a/quickevent/app/quickevent/plugins/Event/qml/sql/def/Field.qml b/quickevent/app/quickevent/plugins/Event/qml/sql/def/Field.qml index 6d43184fe..c3fae0e09 100644 --- a/quickevent/app/quickevent/plugins/Event/qml/sql/def/Field.qml +++ b/quickevent/app/quickevent/plugins/Event/qml/sql/def/Field.qml @@ -21,6 +21,8 @@ QtObject { def += ' DEFAULT '; if(typeof defaultValue === 'boolean' && driver_name.endsWith("SQLITE")) def += (defaultValue)? 1: 0; + else if(defaultValue === 'CURRENT_TIMESTAMP') + def += defaultValue; else if(typeof defaultValue === 'string') def += "'" + defaultValue + "'"; else diff --git a/quickevent/app/quickevent/plugins/Event/qml/sql/def/Index.qml b/quickevent/app/quickevent/plugins/Event/qml/sql/def/Index.qml index bc72921c5..12986f898 100644 --- a/quickevent/app/quickevent/plugins/Event/qml/sql/def/Index.qml +++ b/quickevent/app/quickevent/plugins/Event/qml/sql/def/Index.qml @@ -9,6 +9,7 @@ QtObject property bool primary: false property bool unique: false property ForeignKeyReference references: null + property string where property string comment function indexName(cnt, table_name) @@ -55,21 +56,29 @@ QtObject else if(primary) { ret += '\tCONSTRAINT ' + indexName(constr_no, options.tableName) + ' PRIMARY KEY (' + fields.join(', ') + ')'; } - else if(unique) { + else if(unique && !where) { ret += '\tCONSTRAINT ' + indexName(constr_no, options.tableName) + ' UNIQUE (' + fields.join(', ') + ')'; } } return ret; } - function createSqlIndexScript(options) + function createSqlIndexScript(index_name, table_name, options) { - var ret = ''; if(fields) { - if(!primary && !unique && !references) { - ret = '(' + fields.join(', ') + ')'; + if (where) { + return 'CREATE ' + + (unique? 'UNIQUE ': ' ') + +'INDEX ' + index_name + + ' ON ' + table_name + ' (' + fields.join(', ') + ')' + + ' WHERE ' + where; + } + else if(!primary && !unique && !references) { + return 'CREATE INDEX ' + index_name + + ' ON ' + table_name + + ' (' + fields.join(', ') + ')'; } } - return ret; + return ''; } } diff --git a/quickevent/app/quickevent/plugins/Event/qml/sql/def/Table.qml b/quickevent/app/quickevent/plugins/Event/qml/sql/def/Table.qml index ab5b750ca..a50ad1766 100644 --- a/quickevent/app/quickevent/plugins/Event/qml/sql/def/Table.qml +++ b/quickevent/app/quickevent/plugins/Event/qml/sql/def/Table.qml @@ -20,8 +20,8 @@ QtObject { var field_defs = []; options.tableName = name; var fieldNames = [] - for(var i=0; i maximumCardCheckAdvanceSec() const; bool isOneTenthSecResults() const; diff --git a/quickevent/app/quickevent/plugins/Event/src/eventplugin.cpp b/quickevent/app/quickevent/plugins/Event/src/eventplugin.cpp index 32e500ba3..90640eb72 100644 --- a/quickevent/app/quickevent/plugins/Event/src/eventplugin.cpp +++ b/quickevent/app/quickevent/plugins/Event/src/eventplugin.cpp @@ -12,12 +12,13 @@ #include "services/serviceswidget.h" #include "services/emmaclient.h" -#include "services/qx/qxclientservice.h" +#include "services/qx/qxeventservice.h" #include "services/punchingtest/punchingtestservice.h" #include #include #include +#include #include @@ -35,12 +36,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include @@ -73,23 +74,6 @@ using qff::getPlugin; namespace Event { -class DbEventPayload : public QVariantMap -{ -private: - typedef QVariantMap Super; - - QF_VARIANTMAP_FIELD(QString, e, setE, ventName) - QF_VARIANTMAP_FIELD(QString, d, setD, omain) - QF_VARIANTMAP_FIELD(int, c, setc, onnectionId) - QF_VARIANTMAP_FIELD(QVariant, d, setD, ata) -public: - DbEventPayload(const QVariantMap &data = QVariantMap()) : QVariantMap(data) {} - - static DbEventPayload fromJson(const QByteArray &json); - - QByteArray toJson() const; -}; - DbEventPayload DbEventPayload::fromJson(const QByteArray &json) { QJsonParseError error; @@ -195,7 +179,7 @@ EventPlugin::EventPlugin(QObject *parent) setEventOpen(!event_name.isEmpty()); }); connect(this, &Event::EventPlugin::dbEventNotify, this, &Event::EventPlugin::onDbEventNotify, Qt::QueuedConnection); - connect(qf::gui::framework::Application::instance(), &qf::gui::framework::Application::qxRecChng, this, &EventPlugin::onRecChng); + connect(qf::gui::framework::Application::instance()->qxSql(), &qf::core::sql::QxSql::recChng, this, &EventPlugin::onRecChng); } void EventPlugin::initEventConfig() @@ -435,7 +419,7 @@ void EventPlugin::onInstalled() auto *emma_client = new services::EmmaClient(this); services::Service::addService(emma_client); - auto shvapi_client = new services::qx::QxClientService(this); + auto shvapi_client = new services::qx::QxEventService(this); services::Service::addService(shvapi_client); auto *punching_test = new services::PunchingTestService(this); @@ -673,7 +657,7 @@ void EventPlugin::onDbEvent(const QString &name, QSqlDriver::NotificationSource qfWarning() << "RecChng loopback detected, issuer:" << recchng.issuer; return; } - qf::gui::framework::Application::instance()->emitQxRecChng(recchng, this); + qf::gui::framework::Application::instance()->qxSql()->emitRecChng(recchng, this); return; } qfMessage() << "emitting domain:" << domain << "data:" << data; @@ -1366,8 +1350,8 @@ void EventPlugin::deleteEvent(const QString &event_name) .arg(event_name, q.lastErrorText())); } } - -static void cloneDbConnection(qfs::Connection &dst, const qfs::Connection &src) +namespace { +void cloneDbConnection(qfs::Connection &dst, const qfs::Connection &src) { dst.setHostName(src.hostName()); dst.setPort(src.port()); @@ -1375,7 +1359,7 @@ static void cloneDbConnection(qfs::Connection &dst, const qfs::Connection &src) dst.setPassword(src.password()); dst.setDatabaseName(src.databaseName()); } - +} bool EventPlugin::importEventFromFile(const QString &src_file, const QString &dest_event_name) { qfLogFuncFrame(); diff --git a/quickevent/app/quickevent/plugins/Event/src/eventplugin.h b/quickevent/app/quickevent/plugins/Event/src/eventplugin.h index 50a8e077e..a29badbe4 100644 --- a/quickevent/app/quickevent/plugins/Event/src/eventplugin.h +++ b/quickevent/app/quickevent/plugins/Event/src/eventplugin.h @@ -26,6 +26,23 @@ namespace Event { static constexpr auto START_LIST_IOFXML3_FILE = "startlist-iof3.xml"; static constexpr auto RESULTS_IOFXML3_FILE = "results-iof3.xml"; +class DbEventPayload : public QVariantMap +{ +private: + typedef QVariantMap Super; + + QF_VARIANTMAP_FIELD(QString, e, setE, ventName) + QF_VARIANTMAP_FIELD(QString, d, setD, omain) + QF_VARIANTMAP_FIELD(int, c, setc, onnectionId) + QF_VARIANTMAP_FIELD(QVariant, d, setD, ata) +public: + DbEventPayload(const QVariantMap &data = QVariantMap()) : QVariantMap(data) {} + + static DbEventPayload fromJson(const QByteArray &json); + + QByteArray toJson() const; +}; + class EventPlugin : public qf::gui::framework::Plugin { Q_OBJECT @@ -60,7 +77,6 @@ class EventPlugin : public qf::gui::framework::Plugin static constexpr auto DBEVENT_PUNCH_RECEIVED = "punchReceived"; static constexpr auto DBEVENT_REGISTRATIONS_IMPORTED = "registrationsImported"; static constexpr auto DBEVENT_STAGE_START_CHANGED = "stageStartChanged"; - static constexpr auto DBEVENT_QX_CHANGE_RECEIVED = "qxChangeReceived"; static constexpr auto DBEVENT_QX_RECCHNG = "qxRecChng"; Q_INVOKABLE void initEventConfig(); @@ -151,8 +167,7 @@ class EventPlugin : public qf::gui::framework::Plugin bool importEventFromFile(const QString &src_file, const QString &dest_event_name); bool convertSqlEvent(const QString &from_event, const QString &to_event); void deleteEvent(const QString &event_name); - QString copyEventSchema(qf::core::sql::Connection &imp_conn, qf::core::sql::Connection &exp_conn, - const QString &dest_schema_name); + QString copyEventSchema(qf::core::sql::Connection &imp_conn, qf::core::sql::Connection &exp_conn, const QString &dest_schema_name); void onServiceDockVisibleChanged(bool on = true); private: diff --git a/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclient.cpp b/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclient.cpp index 3cc6cb3d7..3fe2f6dec 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclient.cpp +++ b/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclient.cpp @@ -304,7 +304,7 @@ void OFeedClient::onDbEventNotify(const QString &domain, int connection_id, cons auto lst = data.toList(); int run_id = lst.value(0).toInt(); auto dirty_vals = lst.value(1).toMap(); - qfInfo() << serviceName().toStdString() + " DB event RUN CHANGED received, run_id: " << run_id << ", dirty keys: " << dirty_vals.keys().join(", "); + qfInfo() << serviceName().toStdString() + " DB event RUN CHANGED received, run_id: " << run_id << ", dirty keys: " << dirty_vals.keys().join(", "); // clazy:exclude=container-anti-pattern if (!dirty_vals.isEmpty()) { static const QSet relevant_fields = { // Run fields (finishTimeMs and timeMs are covered by DBEVENT_CARD_PROCESSED_AND_ASSIGNED) @@ -319,7 +319,7 @@ void OFeedClient::onDbEventNotify(const QString &domain, int connection_id, cons QStringLiteral("classid"), }; bool has_relevant = false; - for (const auto &key : dirty_vals.keys()) { + for (const auto &[key, _] : dirty_vals.asKeyValueRange()) { if (relevant_fields.contains(key.section('.', -1).toLower())) { has_relevant = true; break; @@ -758,7 +758,7 @@ void OFeedClient::testConnection(const QString &host_url, const QByteArray response_bytes = reply->readAll(); const QString response_text = QString::fromUtf8(response_bytes).trimmed(); - auto callback_error = [&](const QString &msg) { + auto callback_error = [=](const QString &msg) { const QString status_text = http_status > 0 ? QStringLiteral("HTTP %1").arg(http_status) : QStringLiteral("HTTP status unavailable"); const QString full_message = response_text.isEmpty() ? QStringLiteral("%1: %2").arg(status_text, msg) @@ -1373,7 +1373,7 @@ void OFeedClient::markChangelogEntryAsProcessed(int protocolId) variables["processedByType"] = QString("SYSTEM"); variables["processedBySource"] = QString("IT"); - sendGraphQLRequest(mutation, variables, [this, protocolId](QJsonObject data) { + sendGraphQLRequest(mutation, variables, [protocolId](QJsonObject data) { if (data.isEmpty()) { qfWarning() << serviceName().toStdString() + " Failed to mark changelog entry " << protocolId << " as processed"; } else { @@ -1522,7 +1522,7 @@ void OFeedClient::processNewRunner(int ofeed_competitor_id) // Save emits db event doc.save(); - auto competitor_id = doc.value("competitors.id"); + // auto competitor_id = doc.value("competitors.id"); unused // Get runs.id for current stage int current_stage = getPlugin()->currentStageId(); @@ -1563,8 +1563,8 @@ void OFeedClient::storeChange(const QJsonObject &change) QString firstname = competitor["firstname"].isString() ? competitor["firstname"].toString() : no_data; QString lastname = competitor["lastname"].isString() ? competitor["lastname"].toString() : no_data; - QJsonDocument change_json_doc(change); - QString change_json = QString::fromUtf8(change_json_doc.toJson(QJsonDocument::Compact)); + // QJsonDocument change_json_doc(change); unused + // QString change_json = QString::fromUtf8(change_json_doc.toJson(QJsonDocument::Compact)); int change_id = change["id"].toInt(); auto created = QDateTime::fromString(change["createdAt"].toString(), Qt::ISODate); diff --git a/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclientwidget.h b/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclientwidget.h index b91980d60..242409451 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclientwidget.h +++ b/quickevent/app/quickevent/plugins/Event/src/services/ofeed/ofeedclientwidget.h @@ -40,9 +40,9 @@ class OFeedClientWidget : public qf::gui::framework::DialogWidget QString defaultReceiptEventLink() const; OFeedClient* service(); bool saveSettings(); + bool acceptDialogDone(int result) override; private: Ui::OFeedClientWidget *ui; - bool acceptDialogDone(int result) override; bool m_isTestConnectionRunning = false; bool m_isImageRefreshRunning = false; QString m_lastAutoReceiptEventLink; diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.cpp new file mode 100644 index 000000000..b21873fd7 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.cpp @@ -0,0 +1,410 @@ +#include "lateentrydialog.h" +#include "ui_lateentrydialog.h" + +#include "qxeventservice.h" +#include "runchange.h" + +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace quickevent::core::og; + +namespace Event::services::qx { + +LateEntryDialog::LateEntryDialog(int change_id, int stage_id, const LateEntry &late_entry, const QString &status, const QString &status_message, QWidget *parent) + : Super(parent) + , ui(new Ui::LateEntryDialog) + , m_changeId(change_id) + , m_stageId(stage_id) + , m_lateEntryId(late_entry.id) + , m_status(qxChangeStatusFromString(status)) +{ + Q_ASSERT(change_id > 0); + + ui->setupUi(this); + + ui->edStatusMessage->setPlainText(status_message); + + ui->btAccept->setDisabled(true); + ui->btReject->setDisabled(true); + + connect(ui->chkForce, &QCheckBox::checkStateChanged, this, [this]() { + updateButtonsEnabled(); + }); + connect(ui->btAccept, &QPushButton::clicked, this, [this]() { + resolveChangesAndClose(true); + done(QDialog::Accepted); + }); + connect(ui->btAcceptAndEdit, &QPushButton::clicked, this, [this]() { + resolveChangesAndClose(true); + emit editCompetitor(m_competitorId); + done(QDialog::Accepted); + }); + connect(ui->btReject, &QPushButton::clicked, this, [this]() { + resolveChangesAndClose(false); + done(QDialog::Accepted); + }); + connect(ui->btSendMessage, &QPushButton::clicked, this, &LateEntryDialog::updateQxChangeMessage); + + ui->edNote->setText(late_entry.note.value_or(QString())); + + ui->edChangeId->setValue(change_id); + + auto update_background = [](QWidget *widget) { + bool is_set = false; + if (auto *le = qobject_cast(widget)) { + is_set = !le->text().isEmpty(); + } + else if (auto *spin_box = qobject_cast(widget)) { + is_set = spin_box->value() > 0; + } + if (is_set) { + widget->setStyleSheet("background: palegreen"); + } + }; + + ui->grpFirstName->setChecked(late_entry.first_name.has_value()); + ui->edFirstName->setText(late_entry.first_name.value_or(QString())); + update_background(ui->edFirstName); + + ui->grpLastName->setChecked(late_entry.last_name.has_value()); + ui->edLastName->setText(late_entry.last_name.value_or(QString())); + update_background(ui->edLastName); + + ui->grpRegistration->setChecked(late_entry.registration.has_value()); + ui->edRegistration->setText(late_entry.registration.value_or(QString())); + update_background(ui->edRegistration); + + ui->grpSiCard->setChecked(late_entry.si_id.has_value()); + ui->edSiCard->setValue(late_entry.si_id.value_or(0)); + update_background(ui->edSiCard); + + ui->grpStartTime->setChecked(late_entry.start_time_ms.has_value()); + ui->edStartTime->setText(late_entry.start_time_ms.has_value()? TimeMs(late_entry.start_time_ms.value()).toString(): QString{}); + update_background(ui->edStartTime); + + connect(ui->edStartTime, &QLineEdit::textEdited, this, &LateEntryDialog::checkStartTimeIsValid); + + if (auto *run_id = std::get_if(&late_entry.id)) { + auto id = run_id->id; + ui->edRunId->setValue(id); + loadOrigValues(id); + checkStartTimeIsValid(); + } + else if (auto *id = std::get_if(&late_entry.id)) { + auto class_id = id->id; + loadClassName(class_id); + } + + if (late_entry.paid.value_or(false)) { + ui->lblPaid->setText(tr("Paid")); + ui->lblPaid->setStyleSheet("background:green;color:white"); + } else { + ui->lblPaid->setText(tr("Not Paid")); + ui->lblPaid->setStyleSheet("background:red;color:white"); + } + + checkDuplicitRegistration(); + checkDuplicitName(); + lockChange(); +} + +LateEntryDialog::~LateEntryDialog() +{ + delete ui; +} + +QxEventService *LateEntryDialog::service() +{ + auto *svc = qobject_cast(Service::serviceByName(QxEventService::serviceId())); + Q_ASSERT(svc); + return svc; +} + +void LateEntryDialog::setMessage(const QString &msg, bool error) +{ + if (msg.isEmpty()) { + ui->lblError->setStyleSheet({}); + } + else if (error) { + ui->lblError->setStyleSheet("background: salmon"); + } + else { + ui->lblError->setStyleSheet({}); + } + ui->lblError->setText(msg); +} + +void LateEntryDialog::loadOrigValues(int run_id) +{ + Q_ASSERT(run_id > 0); + + qf::core::sql::Query q; + q.execThrow(QStringLiteral("SELECT classes.name AS class_name, competitors.id AS competitor_id" + " FROM runs" + " JOIN competitors ON competitors.id=runs.competitorId AND runs.id=%1" + " LEFT JOIN classes ON competitors.classId=classes.id") + .arg(run_id) + ); + if (q.next()) { + ui->edClassName->setText(q.value("class_name").toString()); + m_competitorId = q.value("competitor_id").toInt(); + } + + Competitors::CompetitorDocument doc; + doc.load(m_competitorId, qf::gui::model::DataDocument::ModeView); + + m_origValues.first_name = doc.value("firstName").toString(); + m_origValues.last_name = doc.value("lastName").toString(); + m_origValues.registration = doc.value("registration").toString(); + { + qf::core::sql::Query q; + q.execThrow(QStringLiteral("SELECT startTimeMs" + " FROM runs" + " WHERE id=%1") + .arg(run_id) + ); + if (q.next()) { + m_origValues.start_time_ms = q.value("startTimeMs").toInt(); + } + } + m_origValues.si_id = doc.value("siId").toInt(); + + ui->edFirstNameOrig->setText(m_origValues.first_name); + ui->edLastNameOrig->setText(m_origValues.last_name); + ui->edRegistrationOrig->setText(m_origValues.registration); + ui->edStartTimeOrig->setText(TimeMs(m_origValues.start_time_ms).toString()); + ui->edSiCardOrig->setValue(m_origValues.si_id); +} + +void LateEntryDialog::loadClassName(int class_id) +{ + qf::core::sql::Query q; + q.execThrow(QStringLiteral("SELECT name FROM classes WHERE id=%1") + .arg(class_id) ); + if (q.next()) { + ui->edClassName->setText(q.value(0).toString()); + } +} + +void LateEntryDialog::unlockChange() const +{ + if (m_lockNumber > 0 || ui->chkForce->isChecked()) { + qf::core::sql::Query q; + q.execThrow(QStringLiteral("UPDATE qxchanges SET lock_number=NULL WHERE id=%1") + .arg(m_changeId) ); + } +} + +void LateEntryDialog::lockChange() +{ + auto lock_number = QxEventService::currentConnectionId(); + qf::core::sql::Query q; + q.execThrow(QStringLiteral("UPDATE qxchanges SET lock_number=%1 WHERE id=%2 AND (lock_number IS NULL OR lock_number=%1)") + .arg(lock_number) + .arg(m_changeId) + ); + if (q.numRowsAffected() == 0) { + m_lockNumber = 0; + setMessage(tr("Change is already locked by other user."), true); + } + else { + m_lockNumber = lock_number; + } + updateButtonsEnabled(); +} + +void LateEntryDialog::updateButtonsEnabled() +{ + ui->btAccept->setEnabled(m_status == QxChangeStatus::Pending && (ui->chkForce->isChecked() || m_lockNumber > 0)); + ui->btReject->setEnabled(m_status == QxChangeStatus::Pending && (ui->chkForce->isChecked() || m_lockNumber > 0)); +} + +void LateEntryDialog::resolveChangesAndClose(bool is_accepted) +{ + using namespace qf::gui::model; + + if (is_accepted) { + auto class_id = classId(); + bool is_insert = class_id.has_value() && !m_setIsRunning; + Competitors::CompetitorDocument doc; + doc.load(m_competitorId, is_insert? DataDocument::ModeInsert: DataDocument::ModeEdit); + if (is_insert) { + doc.setValue("classId", class_id.value()); + } + if (ui->grpFirstName->isChecked()) { + doc.setValue("firstName", ui->edFirstName->text()); + } + if (ui->grpLastName->isChecked()) { + doc.setValue("lastName", ui->edLastName->text()); + } + if (ui->grpRegistration->isChecked()) { + doc.setValue("registration", ui->edRegistration->text()); + } + if (ui->grpSiCard->isChecked()) { + if (is_insert) { + doc.setValue("siId", ui->edSiCard->value()); + } + } + doc.save(); + m_competitorId = doc.dataId().toInt(); + qf::core::sql::Record rec; + if (ui->grpSiCard->isChecked()) { + rec["siId"] = ui->edSiCard->value(); + } + if (ui->grpStartTime->isChecked()) { + rec["starttimems"] = TimeMs::fromString(ui->edStartTime->text()).msec(); + } + if (m_setIsRunning) { + rec["isRunning"] = true; + } + if (!rec.isEmpty()) { + auto competitor_id = doc.value("id").toInt(); + Q_ASSERT(competitor_id > 0); + int run_id = 0; + { + qf::core::sql::Query q; + q.execThrow(QStringLiteral("SELECT id FROM runs WHERE competitorId=%1 AND stageId=%2") + .arg(competitor_id) + .arg(m_stageId) + ); + if (q.next()) { + run_id = q.value(0).toInt(); + } + } + Q_ASSERT(run_id > 0); + qf::gui::framework::Application::instance()->qxSql()->updateRecord("runs", run_id, rec, this); + } + } + + qf::core::sql::Record rec { + {"status", is_accepted? "Accepted": "Rejected"}, + {"status_message", ui->edStatusMessage->toPlainText()}, + {"orig_data", qf::core::Utils::qvariantToJson(is_accepted? m_origValues.toVariantMap(): QVariantMap{})}, + }; + qf::gui::framework::Application::instance()->qxSql()->updateRecord("qxchanges", m_changeId, rec, this); +} + +void LateEntryDialog::updateQxChangeMessage() +{ + qf::core::sql::Record rec { + { "status_message", ui->edStatusMessage->toPlainText() }, + }; + qf::gui::framework::Application::instance()->qxSql()->updateRecord("qxchanges", m_changeId, rec, this); +} + +void LateEntryDialog::checkDuplicitRegistration() +{ + auto class_id = classId(); + if (!ui->grpRegistration->isChecked() || !class_id.has_value()) { + return; + } + auto reg = ui->edRegistration->text().trimmed().toUpper(); + qf::core::sql::Query q; + q.execThrow(QStringLiteral("SELECT id, firstName, lastName FROM competitors WHERE registration='%1' AND classId=%2") + .arg(reg).arg(class_id.value()) ); + if (q.next()) { + setMessage(tr("Competitor %1 %2 %3 is registered in this class already, run will be updated.") + .arg(q.value("firstName").toString()) + .arg(q.value("lastName").toString()) + .arg(reg) + , true); + auto competitor_id = q.value("id").toInt(); + Q_ASSERT(competitor_id > 0); + changeEventEntryToStageEntry(competitor_id); + } +} + +void LateEntryDialog::checkDuplicitName() +{ + auto class_id = classId(); + if (!ui->grpFirstName->isChecked() || !ui->grpLastName->isChecked() || !class_id.has_value()) { + return; + } + auto first_name = ui->edFirstName->text().trimmed(); + auto last_name = ui->edLastName->text().trimmed(); + qf::core::sql::Query q; + q.execThrow(QStringLiteral("SELECT id, registration FROM competitors WHERE firstName='%1' AND lastName='%2' AND classId=%3") + .arg(first_name) + .arg(last_name) + .arg(class_id.value())); + if (q.next()) { + setMessage(tr("Competitor %1 %2 %3 is registered in this class already, run will be updated.") + .arg(first_name) + .arg(last_name) + .arg(q.value("registration").toString()) + , true); + auto competitor_id = q.value("id").toInt(); + Q_ASSERT(competitor_id > 0); + changeEventEntryToStageEntry(competitor_id); + } +} + +void LateEntryDialog::changeEventEntryToStageEntry(int competitor_id) +{ + m_setIsRunning = true; + m_competitorId = competitor_id; + + ui->grpFirstName->setChecked(false); + ui->grpLastName->setChecked(false); + ui->grpRegistration->setChecked(false); +} + +void LateEntryDialog::checkStartTimeIsValid() +{ + if (!ui->grpStartTime->isChecked()) { + ui->edStartTime->setStyleSheet({}); + return; + } + if (auto run_id = runId(); run_id.has_value()) { + auto stime = TimeMs::fromString(ui->edStartTime->text()); + if (stime.isValid()) { + auto possible_stimes = Competitors::CompetitorDocument::possibleStartTimesMs(run_id.value()); + if (possible_stimes.contains(stime.msec())) { + ui->edStartTime->setStyleSheet({}); + } else { + ui->edStartTime->setStyleSheet("background: red"); + } + } + } +} + +void LateEntryDialog::done(int result) +{ + unlockChange(); + Super::done(result); +} + +std::optional LateEntryDialog::runId() const +{ + if (auto *id = std::get_if(&m_lateEntryId)) { + return id->id; + } + return std::nullopt; +} + +std::optional LateEntryDialog::classId() const +{ + if (auto *id = std::get_if(&m_lateEntryId)) { + return id->id; + } + return std::nullopt; +} + +} // namespace Event::services::qx diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.h new file mode 100644 index 000000000..fee286b76 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.h @@ -0,0 +1,66 @@ +#ifndef LATEENTRYDIALOG_H +#define LATEENTRYDIALOG_H + +#include "runchange.h" + +#include + +class QNetworkReply; + +namespace Event::services::qx { + +namespace Ui { +class LateEntryDialog; +} + +struct LateEntry; +class QxEventService; + +class LateEntryDialog : public QDialog +{ + Q_OBJECT + using Super = QDialog; +public: + explicit LateEntryDialog(int change_id, int stage_id, const LateEntry &late_entry, const QString &status, const QString &status_message, QWidget *parent = nullptr); + ~LateEntryDialog() override; + + Q_SIGNAL void editCompetitor(int competitor_id); + + void done(int result) override; +private: + std::optional runId() const; + std::optional classId() const; + QxEventService* service(); + void setMessage(const QString &msg, bool error); + + void loadOrigValues(int run_id); + void loadClassName(int class_id); + + void lockChange(); + void unlockChange() const; + void updateButtonsEnabled(); + + void resolveChangesAndClose(bool is_accepted); + void updateQxChangeMessage(); + + void checkDuplicitRegistration(); + void checkDuplicitName(); + void checkStartTimeIsValid(); + void changeEventEntryToStageEntry(int competitor_id); +private: + Ui::LateEntryDialog *ui; + int m_changeId = 0; + int m_stageId = 0; + LateEntryForeignId m_lateEntryId = RunId{}; + int m_competitorId = 0; + int m_lockNumber = 0; + QxChangeStatus m_status = QxChangeStatus::Rejected; + OrigRunRecord m_origValues; + bool m_setIsRunning = false; +}; + + +} // namespace Event::services::qx + + +#endif // LATEENTRYDIALOG_H diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.ui b/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.ui similarity index 79% rename from quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.ui rename to quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.ui index c18f157b3..6e2cee6ef 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.ui +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/lateentrydialog.ui @@ -1,19 +1,19 @@ - Event::services::qx::RunChangeDialog - + Event::services::qx::LateEntryDialog + 0 0 712 - 569 + 696 Dialog - + @@ -101,35 +101,6 @@ - - - - Lock number - - - - - - - - 0 - 0 - - - - Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter - - - true - - - None - - - 999999999 - - - @@ -142,7 +113,11 @@ - + + + true + + @@ -170,7 +145,11 @@ - + + + true + + @@ -198,7 +177,11 @@ - + + + true + + @@ -290,6 +273,38 @@ + + + + Start time + + + true + + + + + + true + + + + + + + -> + + + Qt::AlignmentFlag::AlignCenter + + + + + + + + + @@ -308,6 +323,40 @@ + + + + + + Message + + + + + + + Qt::Orientation::Horizontal + + + + 40 + 20 + + + + + + + + Send + + + + + + + + @@ -322,18 +371,23 @@ - - - - - Message - - - - - - - + + + + 0 + 0 + + + + QFrame::Shape::Box + + + Not Paid + + + Qt::AlignmentFlag::AlignCenter + + @@ -351,7 +405,7 @@ - 40 + 0 20 @@ -378,6 +432,13 @@ + + + + Accept and Edit + + + @@ -386,7 +447,6 @@ edClassName edRunId edChangeId - edLockNumber grpFirstName edFirstNameOrig edFirstName @@ -403,6 +463,7 @@ edSiCardRent edNote edStatusMessage + btSendMessage chkForce btAccept btReject @@ -413,7 +474,7 @@ btCancel clicked() - Event::services::qx::RunChangeDialog + Event::services::qx::LateEntryDialog reject() diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/nodes.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/nodes.cpp new file mode 100644 index 000000000..6a4db4533 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/nodes.cpp @@ -0,0 +1,51 @@ +#include "nodes.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include + +using namespace qf::core::sql; +using namespace shv::chainpack; + +namespace Event::services::qx { + +//========================================================= +// DotAppNode +//========================================================= +namespace { +auto METH_NAME = "name"; +} +const std::vector &DotAppNode::metaMethods() +{ + static std::vector meta_methods { + methods::DIR, + methods::LS, + {Rpc::METH_PING, MetaMethod::Flag::None, {}, "RpcValue", AccessLevel::Browse}, + {METH_NAME, MetaMethod::Flag::IsGetter, {}, "RpcValue", AccessLevel::Browse}, + }; + return meta_methods; +} + +RpcValue DotAppNode::callMethod(const StringViewList &shv_path, const std::string &method, const shv::chainpack::RpcValue ¶ms, const shv::chainpack::RpcValue &user_id) +{ + qfLogFuncFrame() << shv_path.join('/') << method; + //eyascore::utils::UserId user_id = eyascore::utils::UserId::makeUserName(QString::fromStdString(rq.userId().toMap().value("userName").toString())); + if(shv_path.empty()) { + if(method == Rpc::METH_PING) { + return nullptr; + } + if(method == METH_NAME) { + return "QuickEvent"; + } + } + return Super::callMethod(shv_path, method, params, user_id); +} + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/nodes.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/nodes.h new file mode 100644 index 000000000..58cf1125e --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/nodes.h @@ -0,0 +1,22 @@ +#pragma once + +#include "qxnode.h" + +#include + +namespace Event::services::qx { + +class DotAppNode : public QxNode +{ + Q_OBJECT + + using Super = QxNode; +public: + explicit DotAppNode(shv::iotqt::node::ShvNode *parent) : Super(".app", parent) {} +private: + //shv::chainpack::RpcValue callMethodRq(const shv::chainpack::RpcRequest &rq) override; + const std::vector &metaMethods() override; + shv::chainpack::RpcValue callMethod(const StringViewList &shv_path, const std::string &method, const shv::chainpack::RpcValue ¶ms, const shv::chainpack::RpcValue &user_id) override; +}; + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.cpp deleted file mode 100644 index e9a04902e..000000000 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.cpp +++ /dev/null @@ -1,187 +0,0 @@ -#include "qxclientservicewidget.h" -#include "ui_qxclientservicewidget.h" -#include "qxclientservice.h" - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -using qf::gui::framework::getPlugin; - -namespace Event::services::qx { - -QxClientServiceWidget::QxClientServiceWidget(QWidget *parent) - : Super(parent) - , ui(new Ui::QxClientServiceWidget) -{ - setPersistentSettingsId("QxClientServiceWidget"); - ui->setupUi(this); - connect(ui->edServerUrl, &QLineEdit::textChanged, this, &QxClientServiceWidget::updateOCheckListPostUrl); - connect(ui->edApiToken, &QLineEdit::textChanged, this, &QxClientServiceWidget::updateOCheckListPostUrl); - - setMessage(""); - - auto *svc = service(); - Q_ASSERT(svc); - auto *event_plugin = getPlugin(); - auto current_stage = event_plugin->currentStageId(); - auto settings = svc->settings(); - ui->edServerUrl->setText(settings.exchangeServerUrl()); - ui->edApiToken->setText(svc->apiToken()); - ui->edCurrentStage->setValue(current_stage); - connect(ui->btTestConnection, &QAbstractButton::clicked, this, &QxClientServiceWidget::testConnection); - connect(ui->btExportEventInfo, &QAbstractButton::clicked, this, &QxClientServiceWidget::exportEventInfo); - connect(ui->btExportStartList, &QAbstractButton::clicked, this, &QxClientServiceWidget::exportStartList); - connect(ui->btExportRuns, &QAbstractButton::clicked, this, &QxClientServiceWidget::exportRuns); -} - -QxClientServiceWidget::~QxClientServiceWidget() -{ - delete ui; -} - -void QxClientServiceWidget::setMessage(const QString &msg, MessageType msg_type) -{ - if (msg.isEmpty()) { - ui->lblStatus->setStyleSheet({}); - } - else { - switch (msg_type) { - case MessageType::Ok: - ui->lblStatus->setStyleSheet("background: lightgreen"); - break; - case MessageType::Error: - ui->lblStatus->setStyleSheet("background: salmon"); - break; - case MessageType::Progress: - ui->lblStatus->setStyleSheet("background: orange"); - break; - } - } - ui->lblStatus->setText(msg); -} - -bool QxClientServiceWidget::acceptDialogDone(int result) -{ - if(result == QDialog::Accepted) { - if(!saveSettings()) { - return false; - } - } - return true; -} - -QxClientService *QxClientServiceWidget::service() -{ - auto *svc = qobject_cast(Service::serviceByName(QxClientService::serviceId())); - QF_ASSERT(svc, QxClientService::serviceId() + " doesn't exist", return nullptr); - return svc; -} - -bool QxClientServiceWidget::saveSettings() -{ - auto *svc = service(); - if(svc) { - auto ss = svc->settings(); - ss.setExchangeServerUrl(ui->edServerUrl->text()); - svc->setSettings(ss); - auto *event_plugin = getPlugin(); - - auto current_stage = event_plugin->currentStageId(); - auto stage_data = event_plugin->stageData(current_stage); - stage_data.setQxApiToken(ui->edApiToken->text()); - event_plugin->setStageData(current_stage, stage_data); - } - return true; -} - -void QxClientServiceWidget::updateOCheckListPostUrl() -{ - auto url = QStringLiteral("%1/api/event/current/oc").arg(ui->edServerUrl->text()); - ui->edOChecklistUrl->setText(url); - ui->edOChecklistUrlHeader->setText(QStringLiteral("qx-api-token=%1").arg(ui->edApiToken->text())); -} - -void QxClientServiceWidget::testConnection() -{ - auto *svc = service(); - Q_ASSERT(svc); - auto *reply = svc->getRemoteEventInfo(ui->edServerUrl->text(), ui->edApiToken->text()); - connect(reply, &QNetworkReply::finished, this, [this, reply]() { - if (reply->error() == QNetworkReply::NetworkError::NoError) { - auto data = reply->readAll(); - auto doc = QJsonDocument::fromJson(data); - EventInfo event_info(doc.toVariant().toMap()); - ui->edEventId->setValue(event_info.id()); - setMessage(tr("Connected OK")); - } - else { - setMessage(tr("Connection error: %1").arg(reply->errorString()), MessageType::Error); - } - reply->deleteLater(); - }); -} - -void QxClientServiceWidget::exportEventInfo() -{ - auto *svc = service(); - Q_ASSERT(svc); - auto *reply = svc->postEventInfo(ui->edServerUrl->text(), ui->edApiToken->text()); - connect(reply, &QNetworkReply::finished, this, [this, reply]() { - auto data = reply->readAll(); - if (reply->error() == QNetworkReply::NetworkError::NoError) { - auto doc = QJsonDocument::fromJson(data); - EventInfo event_info(doc.toVariant().toMap()); - ui->edEventId->setValue(event_info.id()); - setMessage(tr("Event info updated OK")); - } - else { - setMessage(tr("Event info update error: %1\n%2").arg(reply->errorString()).arg(QString::fromUtf8(data)), MessageType::Error); - } - reply->deleteLater(); - }); -} - -void QxClientServiceWidget::exportStartList() -{ - auto *svc = service(); - Q_ASSERT(svc); - saveSettings(); - setMessage(tr("Start list export started ..."), MessageType::Progress); - svc->postStartListIofXml3(this, [this](auto err) { - if (err.isEmpty()) { - setMessage(tr("Start list exported Ok")); - } - else { - setMessage(err, MessageType::Error); - } - }); -} - -void QxClientServiceWidget::exportRuns() -{ - auto *svc = service(); - Q_ASSERT(svc); - saveSettings(); - setMessage(tr("Runs export started ..."), MessageType::Progress); - svc->postRuns(this, [this](auto err) { - if (err.isEmpty()) { - setMessage(tr("Runs exported Ok")); - } - else { - setMessage(err, MessageType::Error); - } - }); -} - -} - diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservice.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservice.cpp similarity index 53% rename from quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservice.cpp rename to quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservice.cpp index e80607c83..305f718cc 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservice.cpp +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservice.cpp @@ -1,5 +1,7 @@ -#include "qxclientservice.h" -#include "qxclientservicewidget.h" +#include "qxeventservice.h" +#include "qxeventservicewidget.h" +#include "nodes.h" +#include "sqlapinode.h" #include "../../eventplugin.h" #include "../../../../Runs/src/runsplugin.h" @@ -9,6 +11,10 @@ #include #include +#include +#include +#include + #include #include #include @@ -24,6 +30,10 @@ #include #include +using namespace shv::chainpack; +using namespace shv::iotqt::rpc; +using namespace shv::iotqt::node; + using namespace qf::core; using namespace qf::gui; using namespace qf::gui::dialogs; @@ -33,89 +43,92 @@ using Event::EventPlugin; using Runs::RunsPlugin; namespace Event::services::qx { -//=============================================== -// QxClientServiceSettings -//=============================================== -// QString QxClientServiceSettings::eventKey() const -// { -// auto *event_plugin = getPlugin(); -// auto *cfg = event_plugin->eventConfig(); -// auto key = cfg->apiKey(); -// auto current_stage = cfg->currentStageId(); -// return QStringLiteral("%1%2").arg(key).arg(current_stage); -// } //=============================================== // QxClientService //=============================================== -QxClientService::QxClientService(QObject *parent) - : Super(QxClientService::serviceId(), parent) +QxEventService::QxEventService(QObject *parent) + : Super(QxEventService::serviceId(), parent) + , m_rootNode(new shv::iotqt::node::ShvRootNode(this)) { - auto *event_plugin = getPlugin(); - connect(event_plugin, &Event::EventPlugin::dbEventNotify, this, &QxClientService::onDbEventNotify, Qt::QueuedConnection); + new DotAppNode(m_rootNode); + new SqlApiNode(m_rootNode); + + connect(m_rootNode, &shv::iotqt::node::ShvNode::sendRpcMessage, this, &QxEventService::sendRpcMessage); } -QString QxClientService::serviceDisplayName() const +QString QxEventService::serviceDisplayName() const { - return tr("QE Exchange"); + return tr("QX Event"); } -QString QxClientService::serviceId() +QString QxEventService::serviceId() { return QStringLiteral("qx"); } -void QxClientService::run() { +void QxEventService::run() { + using namespace shv::iotqt::rpc; + auto ss = settings(); - auto *reply = getRemoteEventInfo(ss.exchangeServerUrl(), apiToken()); - connect(reply, &QNetworkReply::finished, this, [this, reply, ss]() { - if (reply->error() == QNetworkReply::NetworkError::NoError) { - auto data = reply->readAll(); - auto doc = QJsonDocument::fromJson(data); - EventInfo event_info(doc.toVariant().toMap()); - setStatusMessage(event_info.name() + (event_info.stage_count() > 1? QStringLiteral(" E%1").arg(event_info.stage()): QString())); - m_eventId = event_info.id(); - connectToSSE(m_eventId); - if (!m_pollChangesTimer) { - m_pollChangesTimer = new QTimer(this); - connect(m_pollChangesTimer, &QTimer::timeout, this, &QxClientService::pollQxChanges); - } - pollQxChanges(); - m_pollChangesTimer->start(10000); - Super::run(); - } - else { - qfWarning() << "Cannot run QX service, network error:" << reply->errorString(); - } - }); + + delete m_rpcConnection; + m_eventId = 0; + + auto api_token = apiToken(); + if (api_token.isEmpty()) { + setStatus(Status::Stopped); + setStatusMessage(tr("API token is not set.")); + } + + m_rpcConnection = new DeviceConnection("QuickEvent", this); + m_rpcConnection->setConnectionString(ss.shvBrokerUrl()); + if (ss.isExportDatabase()) { + RpcValue::Map opts; + RpcValue::Map device; + device["deviceId"] = api_token.toStdString(); + opts["device"] = device; + m_rpcConnection->setConnectionOptions(opts); + } + + connect(m_rpcConnection, &ClientConnection::brokerConnectedChanged, this, &QxEventService::onBrokerConnectedChanged); + connect(m_rpcConnection, &ClientConnection::socketError, this, &QxEventService::onBrokerSocketError); + connect(m_rpcConnection, &ClientConnection::brokerLoginError, this, &QxEventService::onBrokerLoginError); + connect(m_rpcConnection, &ClientConnection::rpcMessageReceived, this, &QxEventService::onRpcMessageReceived); + + // connect(::qx::SqlApi::instance(), &::qx::SqlApi::recchng, this, &QxEventService::sendRecchgShvSignal); + + m_rpcConnection->open(); } -void QxClientService::stop() +void QxEventService::stop() { - disconnectSSE(); + delete m_rpcConnection; + m_rpcConnection = nullptr; + m_eventId = 0; if (m_pollChangesTimer) { m_pollChangesTimer->stop(); } Super::stop(); } -qf::gui::framework::DialogWidget *QxClientService::createDetailWidget() +qf::gui::framework::DialogWidget *QxEventService::createDetailWidget() { - auto *w = new QxClientServiceWidget(); + auto *w = new QxEventServiceWidget(); return w; } -void QxClientService::loadSettings() +void QxEventService::loadSettings() { Super::loadSettings(); auto ss = settings(); - if (ss.exchangeServerUrl().isEmpty()) { - ss.setExchangeServerUrl("http://localhost:8000"); + if (ss.shvBrokerUrl().isEmpty()) { + ss.setShvBrokerUrl("tcp://localhost?user=test&password=test"); } m_settings = ss; } -void QxClientService::onDbEventNotify(const QString &domain, int connection_id, const QVariant &data) +void QxEventService::onDbEventNotify(const QString &domain, int connection_id, const QVariant &data) { Q_UNUSED(connection_id) Q_UNUSED(data) @@ -162,7 +175,7 @@ void QxClientService::onDbEventNotify(const QString &domain, int connection_id, } } -QNetworkAccessManager *QxClientService::networkManager() +QNetworkAccessManager *QxEventService::networkManager() { if (!m_networkManager) { m_networkManager = new QNetworkAccessManager(this); @@ -170,7 +183,7 @@ QNetworkAccessManager *QxClientService::networkManager() return m_networkManager; } -QNetworkReply *QxClientService::getRemoteEventInfo(const QString &qxhttp_host, const QString &api_token) +QNetworkReply *QxEventService::getRemoteEventInfo(const QString &qxhttp_host, const QString &api_token) { auto *nm = networkManager(); QNetworkRequest request; @@ -181,7 +194,7 @@ QNetworkReply *QxClientService::getRemoteEventInfo(const QString &qxhttp_host, c return nm->get(request); } -QNetworkReply *QxClientService::postEventInfo(const QString &qxhttp_host, const QString &api_token) +QNetworkReply *QxEventService::postEventInfo(const QString &qxhttp_host, const QString &api_token) { auto *nm = networkManager(); QNetworkRequest request; @@ -197,7 +210,7 @@ QNetworkReply *QxClientService::postEventInfo(const QString &qxhttp_host, const return nm->post(request, data); } -void QxClientService::postStartListIofXml3(QObject *context, std::function call_back) +void QxEventService::postStartListIofXml3(QObject *context, std::function call_back) { auto *ep = getPlugin(); int current_stage = ep->currentStageId(); @@ -208,7 +221,7 @@ void QxClientService::postStartListIofXml3(QObject *context, std::function call_back) +void QxEventService::postRuns(QObject *context, std::function call_back) { auto *ep = getPlugin(); int current_stage = ep->currentStageId(); @@ -220,9 +233,9 @@ void QxClientService::postRuns(QObject *context, std::function c } } -void QxClientService::getHttpJson(const QString &path, const QUrlQuery &query, QObject *context, const std::function &call_back) +void QxEventService::getHttpJson(const QString &path, const QUrlQuery &query, QObject *context, const std::function &call_back) { - auto url = exchangeServerUrl(); + auto url = shvBrokerUrl(); url.setPath(path); url.setQuery(query); // qfInfo() << url.toString(); @@ -249,9 +262,9 @@ void QxClientService::getHttpJson(const QString &path, const QUrlQuery &query, Q }); } -QNetworkReply* QxClientService::getQxChangesReply(int from_id) +QNetworkReply* QxEventService::getQxChangesReply(int from_id) { - auto url = exchangeServerUrl(); + auto url = shvBrokerUrl(); url.setPath(QStringLiteral("/api/event/%1/changes").arg(eventId())); url.setQuery(QStringLiteral("from_id=%1").arg(from_id)); @@ -261,32 +274,29 @@ QNetworkReply* QxClientService::getQxChangesReply(int from_id) return networkManager()->get(request); } -int QxClientService::eventId() const +int QxEventService::eventId() const { - if (m_eventId == 0) { - throw qf::core::Exception(tr("Event ID is not loaded, service is not probably running.")); - } + // if (m_eventId == 0) { + // throw qf::core::Exception(tr("Event ID is not loaded, service is not probably running.")); + // } return m_eventId; } -QByteArray QxClientService::apiToken() const +QString QxEventService::apiToken() const { - // API token must not be cached to enable service point - // always to current stage event on qxhttpd auto *event_plugin = getPlugin(); - auto current_stage = event_plugin->currentStageId(); - return event_plugin->stageData(current_stage).qxApiToken().toUtf8(); + return event_plugin->eventConfig()->qxApiToken(); } -QUrl QxClientService::exchangeServerUrl() const +QUrl QxEventService::shvBrokerUrl() const { auto ss = settings(); - return QUrl(ss.exchangeServerUrl()); + return QUrl(ss.shvBrokerUrl()); } -void QxClientService::postFileCompressed(std::optional path, std::optional name, QByteArray data, QObject *context , std::function call_back) +void QxEventService::postFileCompressed(std::optional path, std::optional name, QByteArray data, QObject *context , std::function call_back) { - auto url = exchangeServerUrl(); + auto url = shvBrokerUrl(); url.setPath(path.value_or("/api/event/current/file")); if (name.has_value()) { @@ -294,7 +304,7 @@ void QxClientService::postFileCompressed(std::optional path, std::optio } QNetworkRequest request; request.setUrl(url); - request.setRawHeader(QX_API_TOKEN, apiToken()); + request.setRawHeader(QX_API_TOKEN, apiToken().toUtf8()); request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/zip")); auto zdata = zlibCompress(data); QNetworkReply *reply = networkManager()->post(request, zdata); @@ -312,7 +322,7 @@ void QxClientService::postFileCompressed(std::optional path, std::optio }); } -void QxClientService::uploadSpecFile(SpecFile file, QByteArray data, QObject *context, const std::function &call_back) +void QxEventService::uploadSpecFile(SpecFile file, QByteArray data, QObject *context, const std::function &call_back) { switch (file) { case SpecFile::StartListIofXml3: @@ -324,7 +334,7 @@ void QxClientService::uploadSpecFile(SpecFile file, QByteArray data, QObject *co } } -QByteArray QxClientService::zlibCompress(QByteArray data) +QByteArray QxEventService::zlibCompress(QByteArray data) { QByteArray compressedData = qCompress(data); // strip the 4-byte length put on by qCompress @@ -333,19 +343,19 @@ QByteArray QxClientService::zlibCompress(QByteArray data) return compressedData; } -void QxClientService::httpPostJson(const QString &path, const QString &query, QVariantMap json, QObject *context, const std::function &call_back) +void QxEventService::httpPostJson(const QString &path, const QString &query, QVariantMap json, QObject *context, const std::function &call_back) { if (!isRunning()) { return; } - auto url = exchangeServerUrl(); + auto url = shvBrokerUrl(); url.setPath(path); url.setQuery(query); QNetworkRequest request; request.setUrl(url); - request.setRawHeader(QX_API_TOKEN, apiToken()); + request.setRawHeader(QX_API_TOKEN, apiToken().toUtf8()); request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json")); auto data = QJsonDocument::fromVariant(json).toJson(QJsonDocument::Compact); qfInfo() << "HTTP POST JSON:" << url.toString() << "data:" << QString::fromUtf8(data); @@ -373,114 +383,7 @@ void QxClientService::httpPostJson(const QString &path, const QString &query, QV } } -void QxClientService::connectToSSE(int event_id) -{ - Q_UNUSED(event_id); - // auto url = exchangeServerUrl(); - // url.setPath(QStringLiteral("/api/event/%1/run/changes/sse").arg(event_id)); - // QNetworkRequest request(url); - // request.setRawHeader(QByteArray("Accept"), QByteArray("text/event-stream")); - // request.setHeader(QNetworkRequest::UserAgentHeader, "QuickEvent"); - // request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); - // request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork); // Events shouldn't be cached - - // qfInfo() << "Connecting to SSE:" << url.toString(); - // m_replySSE = networkManager()->get(request); - // qfInfo() << "Connected"; - // connect(m_replySSE, &QNetworkReply::readyRead, this, [this]() { - // auto data = m_replySSE->readAll(); - // qfInfo() << "DATA:" << data.toStdString(); - // }); - // connect(m_replySSE, &QNetworkReply::finished, this, [this]() { - // qfInfo() << "SSE finished:" << m_replySSE->errorString(); - // }); -} - -void QxClientService::disconnectSSE() -{ - if (m_replySSE) { - qfInfo() << "Disconnecting SSE:" << m_replySSE; - m_replySSE->deleteLater(); - m_replySSE = nullptr; - } -} - -void QxClientService::pollQxChanges() -{ - auto event_plugin = getPlugin(); - if(!getPlugin()->isEventOpen()) { - return; - } - int stage_id = event_plugin->currentStageId(); - try { - int max_change_id = 0; - qf::core::sql::Query q; - q.execThrow("SELECT MAX(change_id) FROM qxchanges WHERE stage_id=" + QString::number(event_plugin->currentStageId())); - if (q.next()) { - max_change_id = q.value(0).toInt(); - } - auto *reply = getQxChangesReply(max_change_id + 1); - connect(reply, &QNetworkReply::finished, this, [reply, stage_id]() { - QString err; - if(reply->error()) { - err = reply->errorString(); - qfWarning() << "Load qxchanges error:" << err; - } - else { - QJsonParseError err; - auto data = reply->readAll(); - auto json = QJsonDocument::fromJson(data, &err); - if (err.error != QJsonParseError::NoError) { - qfWarning() << "Parse qxchanges error:" << err.errorString(); - } - else { - auto records = json.array().toVariantList(); - qf::core::sql::Query q; - q.prepare("INSERT INTO qxchanges (data_type, data, data_id, source, user_id, status, status_message, stage_id, change_id, created)" - " VALUES (:data_type, :data, :data_id, :source, :user_id, :status, :status_message, :stage_id, :change_id, :created)" - " RETURNING id"); - for (const auto &v : records) { - auto rec = v.toMap(); - auto ba = QJsonDocument::fromVariant(rec.value("data")).toJson(QJsonDocument::Compact); - auto data = QString::fromUtf8(ba); - q.bindValue(":data_type", rec.value("data_type")); - q.bindValue(":data", data); - q.bindValue(":data_id", rec.value("data_id")); - q.bindValue(":source", rec.value("source")); - q.bindValue(":user_id", rec.value("user_id")); - q.bindValue(":status", rec.value("status")); - q.bindValue(":status_message", rec.value("status_message")); - q.bindValue(":stage_id", stage_id); - auto change_id = rec.value("id").toInt(); - Q_ASSERT(change_id > 0); - q.bindValue(":change_id", change_id); - auto created = QDateTime::fromString(rec.value("created").toString(), Qt::ISODate); - qfDebug() << "created:" << created.toString(Qt::ISODate); - q.bindValue(":created", created); - // may fail on prikey violation if more clients are inserting simultaneously - if (q.exec()) { - if (q.next()) { - auto id = q.value(0).toInt(); - qfDebug() << "insert ID:" << id; - getPlugin()->emitDbEvent(Event::EventPlugin::DBEVENT_QX_CHANGE_RECEIVED, id, true); - } - } - else { - qfInfo() << "sql error:" << q.lastErrorText(); - - } - } - } - } - reply->deleteLater(); - }); - } - catch (const qf::core::Exception &e) { - qfWarning() << "Load qxchanges error:" << e.message(); - } -} - -EventInfo QxClientService::eventInfo() const +EventInfo QxEventService::eventInfo() const { auto *event_plugin = getPlugin(); auto *event_config = event_plugin->eventConfig(); @@ -537,35 +440,135 @@ EventInfo QxClientService::eventInfo() const // qfInfo() << qf::core::Utils::qvariantToJson(ei, false); return ei; } -/* -namespace { -auto query_to_json_csv(QSqlQuery &q) +//namespace { +//auto query_to_json_csv(QSqlQuery &q) +//{ +// QVariantList csv; +// { +// // QStringList columns{"name", "control_count", "length", "climb", "start_time", "interval", "start_slot_count"}; +// QStringList columns; +// auto rec = q.record(); +// for (auto i = 0; i < rec.count(); ++i) { +// columns << rec.field(i).name(); +// } +// csv.insert(csv.length(), columns); +// } +// while (q.next()) { +// QVariantList values; +// auto rec = q.record(); +// for (auto i = 0; i < rec.count(); ++i) { +// values << q.value(i); +// } +// csv.insert(csv.length(), values); +// } +// return csv; +//} +//} +int QxEventService::currentConnectionId() { - QVariantList csv; - { - // QStringList columns{"name", "control_count", "length", "climb", "start_time", "interval", "start_slot_count"}; - QStringList columns; - auto rec = q.record(); - for (auto i = 0; i < rec.count(); ++i) { - columns << rec.field(i).name(); - } - csv.insert(csv.length(), columns); + return qf::core::sql::Connection::forName().connectionId(); +} + +const char *QxEventService::eventctlShvPath() const +{ + return "qx/qxeventd/eventctl"; +} + +void QxEventService::onBrokerConnectedChanged(bool is_connected) +{ + if(is_connected) { + auto *event_plugin = getPlugin(); + auto current_stage = event_plugin->currentStageId(); + auto stage_data = event_plugin->stageData(current_stage); + auto api_token = event_plugin->eventConfig()->qxApiToken().toStdString(); + auto *rpc_call = shv::iotqt::rpc::RpcCall::create(m_rpcConnection) + ->setShvPath(eventctlShvPath()) + ->setMethod("openEventApiKey") + ->setParams(RpcValue(api_token)); + connect(rpc_call, &shv::iotqt::rpc::RpcCall::maybeResult, this, [this](const ::shv::chainpack::RpcValue &result, const shv::chainpack::RpcError &error) { + if (error.isValid()) { + setStatus(Status::Stopped); + setStatusMessage(tr("Client info discovery error: %1").arg(error.toString())); + } + else { + const auto &info = result.asList(); + m_eventId = info.value(0).toInt(); + m_eventMountPoint = info.value(1).to(); + setStatus(Status::Running); + setStatusMessage(tr("Event ID: %1").arg(m_eventId)); + subscribeChanges(); + } + }); + rpc_call->start(); + } else { + setStatus(Status::Stopped); } - while (q.next()) { - QVariantList values; - auto rec = q.record(); - for (auto i = 0; i < rec.count(); ++i) { - values << q.value(i); + +} + +void QxEventService::onBrokerSocketError(const QString &err) +{ + qfWarning() << "onBrokerSocketError:" << err; + setStatusMessage(tr("Broker socket error: %1").arg(err)); +} + +void QxEventService::onBrokerLoginError(const shv::chainpack::RpcError &err) +{ + qfWarning() << "onBrokerLoginError:" << err.toString(); + setStatusMessage(tr("Broker login error: %1").arg(err.toString())); +} + +void QxEventService::onRpcMessageReceived(const shv::chainpack::RpcMessage &msg) +{ +// shvLogFuncFrame() << msg.toCpon(); + if(msg.isRequest()) { + RpcRequest rq(msg); + if (rq.shvPath().asString().starts_with(".broker/")) { + // ignore broker discovery messages + return; } - csv.insert(csv.length(), values); + qfMessage() << "RPC request received:" << rq.toPrettyString(); + m_rootNode->handleRpcRequest(rq); + } + else if(msg.isResponse()) { + RpcResponse rp(msg); + qfMessage() << "RPC response received:" << rp.toPrettyString(); + } + else if(msg.isSignal()) { + RpcSignal nt(msg); + qfMessage() << "RPC signal received:" << nt.toPrettyString(); } - return csv; } + +void QxEventService::sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg) +{ + if(m_rpcConnection && m_rpcConnection->isBrokerConnected()) { + m_rpcConnection->sendRpcMessage(rpc_msg); + } } -*/ -int QxClientService::currentConnectionId() + +void QxEventService::subscribeChanges() { - return qf::core::sql::Connection::forName().connectionId(); + // Q_ASSERT(m_rpcConnection); + // QString shv_path = "test"; + // QString signal_name = shv::chainpack::Rpc::SIG_VAL_CHANGED; + // auto *rpc_call = RpcCall::createSubscriptionRequest(m_rpcConnection, shv_path, signal_name); + // connect(rpc_call, &RpcCall::maybeResult, this, [shv_path, signal_name](const ::shv::chainpack::RpcValue &result, const shv::chainpack::RpcError &error) { + // if(error.isValid()) { + // qfError() << "Signal:" << signal_name << "on SHV path:" << shv_path << "subscribe error:" << error.toString(); + // } + // else { + // qfMessage() << "Signal:" << signal_name << "on SHV path:" << shv_path << "subscribed successfully" << result.toCpon(); + // } + // }); + // rpc_call->start(); } +// void QxEventService::sendRecchgShvSignal(const qf::core::sql::QxRecChng &chng) +// { +// if (isRunning()) { +// m_rpcConnection->sendShvSignal("sql", "recchng", ::qx::qxRecChngToRpcValue(chng)); +// } +// } + } // namespace Event::services::qx diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservice.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservice.h similarity index 66% rename from quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservice.h rename to quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservice.h index 636a54c4d..e78831898 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservice.h +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservice.h @@ -7,15 +7,22 @@ class QNetworkReply; class QUrlQuery; class QTimer; +namespace shv::iotqt::node { class ShvNodeTree; class ShvRootNode; } +namespace shv::iotqt::rpc { class DeviceConnection; } +namespace shv::chainpack { class RpcMessage; class RpcError; } +namespace qf::core::sql { struct QxRecChng; } + namespace Event::services::qx { -class QxClientServiceSettings : public ServiceSettings +class QxEventServiceSettings : public ServiceSettings { using Super = ServiceSettings; - QF_VARIANTMAP_FIELD2(QString, e, setE, xchangeServerUrl, "http://localhost:8000") + QF_VARIANTMAP_FIELD2(QString, s, setS, hvBrokerUrl, "tcp://localhost?user=qe&password=test") + QF_VARIANTMAP_FIELD2(bool, is, set, LocalBroker, false) + QF_VARIANTMAP_FIELD2(bool, is, set, ExportDatabase, false) public: - QxClientServiceSettings(const QVariantMap &o = QVariantMap()) : Super(o) {} + QxEventServiceSettings(const QVariantMap &o = QVariantMap()) : Super(o) {} }; class EventInfo : public QVariantMap @@ -34,7 +41,7 @@ class EventInfo : public QVariantMap EventInfo(const QVariantMap &data = QVariantMap()) : QVariantMap(data) {} }; -class QxClientService : public Service +class QxEventService : public Service { Q_OBJECT @@ -42,14 +49,14 @@ class QxClientService : public Service public: static constexpr auto QX_API_TOKEN = "qx-api-token"; public: - QxClientService(QObject *parent); + QxEventService(QObject *parent); static QString serviceId(); QString serviceDisplayName() const override; void run() override; void stop() override; - QxClientServiceSettings settings() const {return QxClientServiceSettings(m_settings);} + QxEventServiceSettings settings() const {return QxEventServiceSettings(m_settings);} void onDbEventNotify(const QString &domain, int connection_id, const QVariant &data); QNetworkAccessManager* networkManager(); @@ -63,11 +70,20 @@ class QxClientService : public Service QNetworkReply* getQxChangesReply(int from_id); - QByteArray apiToken() const; + QString apiToken() const; static int currentConnectionId(); - QUrl exchangeServerUrl() const; + QUrl shvBrokerUrl() const; int eventId() const; +private: // shv + void onBrokerConnectedChanged(bool is_connected); + void onRpcMessageReceived(const shv::chainpack::RpcMessage &msg); + void sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg); + void onBrokerSocketError(const QString &err); + void onBrokerLoginError(const shv::chainpack::RpcError &err); + + const char* eventctlShvPath() const; + void subscribeChanges(); private: void loadSettings() override; qf::gui::framework::DialogWidget *createDetailWidget() override; @@ -78,16 +94,15 @@ class QxClientService : public Service void httpPostJson(const QString &path, const QString &query, QVariantMap json, QObject *context = nullptr, const std::function &call_back = nullptr); - void connectToSSE(int event_id); - void disconnectSSE(); - - void pollQxChanges(); - EventInfo eventInfo() const; +private: // shv + shv::iotqt::rpc::DeviceConnection *m_rpcConnection = nullptr; + shv::iotqt::node::ShvRootNode *m_rootNode; private: QNetworkAccessManager *m_networkManager = nullptr; QNetworkReply *m_replySSE = nullptr; int m_eventId = 0; + QString m_eventMountPoint; QTimer *m_pollChangesTimer = nullptr; }; diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.cpp new file mode 100644 index 000000000..4d7fe6171 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.cpp @@ -0,0 +1,191 @@ +#include "qxeventservicewidget.h" +#include "ui_qxeventservicewidget.h" + +#include "qxeventservice.h" + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using qf::gui::framework::getPlugin; + +namespace Event::services::qx { + +QxEventServiceWidget::QxEventServiceWidget(QWidget *parent) + : Super(parent) + , ui(new Ui::QxEventServiceWidget) +{ + setPersistentSettingsId("QxEventServiceWidget"); + ui->setupUi(this); + + setMessage(""); + + auto *svc = service(); + Q_ASSERT(svc); + auto *event_plugin = getPlugin(); + auto current_stage = event_plugin->currentStageId(); + auto settings = svc->settings(); + ui->edServerUrl->setText(settings.shvBrokerUrl()); + ui->chkExportDatabase->setChecked(settings.isExportDatabase()); + ui->edApiToken->setText(svc->apiToken()); + ui->edCurrentStage->setValue(current_stage); + ui->edEventId->setValue(svc->eventId()); + connect(ui->btTestConnection, &QAbstractButton::clicked, this, &QxEventServiceWidget::testConnection); + + connect(ui->btQxQxOrg, &QRadioButton::toggled, this, &QxEventServiceWidget::setConnectionType); + connect(ui->btLocalhost, &QRadioButton::toggled, this, &QxEventServiceWidget::setConnectionType); + ui->btQxQxOrg->setChecked(!settings.isLocalBroker()); +} + +QxEventServiceWidget::~QxEventServiceWidget() +{ + delete ui; +} + +void QxEventServiceWidget::setMessage(const QString &msg, MessageType msg_type) +{ + if (msg.isEmpty()) { + ui->lblStatus->setStyleSheet({}); + } + else { + switch (msg_type) { + case MessageType::Ok: + ui->lblStatus->setStyleSheet("background: lightgreen"); + break; + case MessageType::Error: + ui->lblStatus->setStyleSheet("background: salmon"); + break; + case MessageType::Progress: + ui->lblStatus->setStyleSheet("background: orange"); + break; + } + } + ui->lblStatus->setText(msg); +} + +bool QxEventServiceWidget::acceptDialogDone(int result) +{ + // delete testing connection if any + delete findChild(); + if(result == QDialog::Accepted) { + if(!saveSettings()) { + return false; + } + } + return true; +} + +QxEventService *QxEventServiceWidget::service() +{ + auto *svc = qobject_cast(Service::serviceByName(QxEventService::serviceId())); + QF_ASSERT(svc, QxEventService::serviceId() + " doesn't exist", return nullptr); + return svc; +} + +bool QxEventServiceWidget::saveSettings() +{ + auto *svc = service(); + if(svc) { + auto ss = svc->settings(); + bool is_localhost = ui->btLocalhost->isChecked(); + if (is_localhost) { + ss.setShvBrokerUrl(ui->edServerUrl->text()); + } + ss.setLocalBroker(is_localhost); + ss.setExportDatabase(ui->chkExportDatabase->isChecked()); + svc->setSettings(ss); + + auto *event_plugin = getPlugin(); + auto *event_config = event_plugin->eventConfig(); + event_config->setQxApiToken(ui->edApiToken->text()); + } + return true; +} + +void QxEventServiceWidget::testConnection() +{ + using namespace shv::iotqt::rpc; + using namespace shv::chainpack; + + delete findChild(); + + auto *rpc = new DeviceConnection("QuickEventTest", this); + rpc->setConnectionString(ui->edServerUrl->text()); + if (ui->chkExportDatabase->isChecked()) { + RpcValue::Map opts; + RpcValue::Map device; + device["deviceId"] = ui->edApiToken->text().toStdString(); + opts["device"] = device; + rpc->setConnectionOptions(opts); + } + + connect(rpc, &ClientConnection::brokerConnectedChanged, this, [this, rpc](bool is_connected) { + if (is_connected) { + setMessage(tr("Broker connected OK")); + auto *rpc_call = shv::iotqt::rpc::RpcCall::create(rpc) + ->setShvPath(".broker/currentClient") + ->setMethod("info"); + connect(rpc_call, &shv::iotqt::rpc::RpcCall::maybeResult, this, [this](const ::shv::chainpack::RpcValue &result, const shv::chainpack::RpcError &error) { + if (error.isValid()) { + setMessage(tr("Client info discovery error: %1").arg(error.toString()), MessageType::Error); + } + else { + const auto &info = result.asMap(); + auto mount_point = info.value("mountPoint").to(); + auto event_id = mount_point.section('/', -1, -1).toInt(); + ui->edEventId->setValue(event_id); + setMessage(tr("Event mounted at: %1, event id: %2").arg(mount_point).arg(event_id)); + } + }); + rpc_call->start(); + } + }); + connect(rpc, &ClientConnection::socketError, this, [this](const QString &error) { + setMessage(tr("Connection error: %1").arg(error), MessageType::Error); + }); + connect(rpc, &ClientConnection::brokerLoginError, this, [this](const auto &error) { + setMessage(tr("Login error: %1").arg(QString::fromStdString(error.toString())), MessageType::Error); + }); + rpc->open(); +} + +void QxEventServiceWidget::setConnectionType() +{ + bool is_localhost = ui->btLocalhost->isChecked(); + if (is_localhost) { + ui->lblUrl->setVisible(true); + ui->edServerUrl->setVisible(true); + } else { + ui->lblUrl->setVisible(false); + ui->edServerUrl->setVisible(false); + } +} + +QString QxEventServiceWidget::brokerUrl() const +{ + bool is_localhost = ui->btLocalhost->isChecked(); + if (is_localhost) { + return ui->edServerUrl->text(); + } + auto url = QStringLiteral("tcp://%1?user=qe&%2=%3"); + url = url.arg("qxqx.org"); + url = url.arg("password"); + url = url.arg("2ddf6394ea6"); + return url; +} + +} + diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.h similarity index 52% rename from quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.h rename to quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.h index f1b108a30..0ae762934 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.h +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.h @@ -5,32 +5,31 @@ namespace Event::services::qx { namespace Ui { -class QxClientServiceWidget; +class QxEventServiceWidget; } -class QxClientService; +class QxEventService; -class QxClientServiceWidget : public qf::gui::framework::DialogWidget +class QxEventServiceWidget : public qf::gui::framework::DialogWidget { Q_OBJECT using Super = qf::gui::framework::DialogWidget; public: - explicit QxClientServiceWidget(QWidget *parent = nullptr); - ~QxClientServiceWidget() override; + explicit QxEventServiceWidget(QWidget *parent = nullptr); + ~QxEventServiceWidget() override; + + bool acceptDialogDone(int result) override; private: enum class MessageType { Ok, Error, Progress }; void setMessage(const QString &msg = {}, MessageType msg_type = MessageType::Ok); - QxClientService* service(); + QxEventService* service(); bool saveSettings(); - void updateOCheckListPostUrl(); void testConnection(); - void exportEventInfo(); - void exportStartList(); - void exportRuns(); + void setConnectionType(); + QString brokerUrl() const; private: - Ui::QxClientServiceWidget *ui; - bool acceptDialogDone(int result) override; + Ui::QxEventServiceWidget *ui; }; } diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.ui b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.ui similarity index 65% rename from quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.ui rename to quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.ui index d8824707e..b6bc76c50 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxclientservicewidget.ui +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxeventservicewidget.ui @@ -1,7 +1,7 @@ - Event::services::qx::QxClientServiceWidget - + Event::services::qx::QxEventServiceWidget + 0 @@ -14,43 +14,62 @@ SHV API service + + + + SHV broker connection + + + + + + + 0 + 0 + + + + Url + + + + + + + localhost + + + true + + + + + + + qxqx.org + + + false + + + + + + + + + - - - - Exchange server url - - - - - - - - - - Fill this url into HTTP POST synchonization method in O-CheckList - - - API token - - - - - - - - - - Fill this url into HTTP POST synchonization method in O-CheckList - + + - Current stage + Export database - - + + false @@ -60,12 +79,15 @@ true - - 1 + + Not loaded + + + 0 - + Fill this url into HTTP POST synchonization method in O-CheckList @@ -75,8 +97,11 @@ + + + - + false @@ -87,52 +112,41 @@ true - Not loaded + Not defined + + + 0 0 - - + + Fill this url into HTTP POST synchonization method in O-CheckList - OCheckList Url - - - - - - - true + API token - - + + Fill this url into HTTP POST synchonization method in O-CheckList - Header - - - - - - - true + Current stage - - + + Qt::Orientation::Horizontal @@ -145,31 +159,17 @@ - - - - Export event info - - - - + Test connection - - - - Export start list - - - - - + + - Export runs + Archive event @@ -200,10 +200,6 @@ - - edServerUrl - edOChecklistUrl - diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.cpp new file mode 100644 index 000000000..49b59528e --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.cpp @@ -0,0 +1,339 @@ +#include "qxlateentrieswidget.h" +#include "ui_qxlateentrieswidget.h" + +#include "qxeventservice.h" +#include "lateentrydialog.h" +#include "runchange.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace qfm = qf::gui::model; +namespace qfs = qf::core::sql; +namespace qfw = qf::gui; +using qf::gui::framework::getPlugin; + +namespace Event::services::qx { + +namespace { +enum Columns { + col_id = 0, + col_stage_id, + col_data_type, + col_foreign_id, + col_foreign_table, + col_data, + col_orig_data, + col_status, + col_status_message, + col_user_id, + col_created, + col_lock_number, + col_COUNT +}; + +constexpr auto COL_ID = "id"; +constexpr auto COL_STAGE_ID = "stage_id"; +constexpr auto COL_DATA_TYPE = "data_type"; +constexpr auto COL_FOREIGN_ID = "foreign_id"; +constexpr auto COL_FOREIGN_TABLE = "foreign_table"; +constexpr auto COL_DATA = "data"; +constexpr auto COL_ORIG_DATA = "orig_data"; +constexpr auto COL_STATUS = "status"; +constexpr auto COL_STATUS_MESSAGE = "status_message"; +constexpr auto COL_USER_ID = "user_id"; +constexpr auto COL_CREATED = "created"; +constexpr auto COL_LOCK_NUMBER = "lock_number"; + +class LateEntriesModel : public qfm::SqlTableModel +{ +public: + using qfm::SqlTableModel::SqlTableModel; + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override + { + if (role == Qt::BackgroundRole) { + if (index.column() == col_data) { + auto data = index.data().toString(); + auto qxdata = QxChangeData::fromJson(data); + if (auto le = qxdata.lateEntry; le.has_value()) { + if (le.value().paid.value_or(false)) { + return QColor("lightgreen"); + } + } + } + else if (index.column() == col_status) { + auto status = qxChangeStatusFromString(index.data().toString()); + switch (status) { + case QxChangeStatus::Pending: return {}; + case QxChangeStatus::Accepted: return QColor("lightgreen"); + case QxChangeStatus::Rejected: return QColor("salmon"); + } + } + } + return qfm::SqlTableModel::data(index, role); + } +}; +} // namespace + +QxLateEntriesWidget::QxLateEntriesWidget(QWidget *parent) : + QWidget(parent), + ui(new Ui::QxLateEntriesWidget) +{ + ui->setupUi(this); + + ui->tableView->setReadOnly(true); + ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui->tableView, &qfw::TableView::customContextMenuRequested, this, &QxLateEntriesWidget::onTableCustomContextMenuRequest); + connect(ui->tableView, &qfw::TableView::doubleClicked, this, &QxLateEntriesWidget::onTableDoubleClicked); + + ui->tableView->setPersistentSettingsId("tblQxLateEntries"); + ui->tableView->setInsertRowEnabled(false); + ui->tableView->setCloneRowEnabled(false); + ui->tableView->setRemoveRowEnabled(false); + ui->tableView->setDirtyRowsMenuSectionEnabled(false); + + ui->toolbar->setTableView(ui->tableView); + m_model = new LateEntriesModel(this); + //m->setObjectName("classes.classesModel"); + + m_model->clearColumns(col_COUNT); + m_model->setColumn(col_id, qf::gui::model::TableModel::ColumnDefinition(COL_ID).setReadOnly(true).setAlignment(Qt::AlignRight)); + m_model->setColumn(col_stage_id, qf::gui::model::TableModel::ColumnDefinition(COL_STAGE_ID).setReadOnly(true).setAlignment(Qt::AlignRight)); + m_model->setColumn(col_data_type, qf::gui::model::SqlTableModel::ColumnDefinition(COL_DATA_TYPE).setReadOnly(true)); + m_model->setColumn(col_foreign_id, qf::gui::model::SqlTableModel::ColumnDefinition(COL_FOREIGN_ID).setReadOnly(true).setAlignment(Qt::AlignRight)); + m_model->setColumn(col_foreign_table, qf::gui::model::SqlTableModel::ColumnDefinition(COL_FOREIGN_TABLE).setReadOnly(true)); + m_model->setColumn(col_data, qf::gui::model::SqlTableModel::ColumnDefinition(COL_DATA).setReadOnly(true)); + m_model->setColumn(col_orig_data, qf::gui::model::SqlTableModel::ColumnDefinition(COL_ORIG_DATA).setReadOnly(true)); + m_model->setColumn(col_status, qf::gui::model::SqlTableModel::ColumnDefinition(COL_STATUS).setReadOnly(true)); + m_model->setColumn(col_status_message, qf::gui::model::SqlTableModel::ColumnDefinition(COL_STATUS_MESSAGE).setReadOnly(true)); + m_model->setColumn(col_user_id, qf::gui::model::SqlTableModel::ColumnDefinition(COL_USER_ID).setReadOnly(true)); + m_model->setColumn(col_created, qf::gui::model::SqlTableModel::ColumnDefinition(COL_CREATED).setReadOnly(true)); + m_model->setColumn(col_lock_number, qf::gui::model::SqlTableModel::ColumnDefinition(COL_LOCK_NUMBER).setReadOnly(true)); + ui->tableView->setTableModel(m_model); + + ui->tableView->setColumnHidden(col_stage_id, true); + + showMessage(tr("QxEvent service is disabled"), true); + setEnabled(false); + + auto *svc = qxEventService(); + connect(svc, &Service::statusChanged, this, [this](Service::Status new_status){ + switch (new_status) { + case Service::Status::Unknown: + case Service::Status::Stopped: + showMessage(tr("QxEvent service is disabled"), true); + setEnabled(false); + break; + case Service::Status::Running: + showMessage({}); + setEnabled(true); + reload(); + break; + } + }); + + { + auto *lst = ui->lstType; + lst->addItem("All"); + lst->addItem("RunUpdateRequest"); + lst->addItem("RunUpdated"); + lst->addItem("OcChange"); + lst->addItem("RadioPunch"); + lst->addItem("CardReadout"); + lst->setCurrentIndex(0); + connect(lst, &QComboBox::currentIndexChanged, this, &QxLateEntriesWidget::reload); + } + connect(ui->chkPending, &QCheckBox::checkStateChanged, this, &QxLateEntriesWidget::reload); + connect(ui->chkAccepted, &QCheckBox::checkStateChanged, this, &QxLateEntriesWidget::reload); + connect(ui->chkRejected, &QCheckBox::checkStateChanged, this, &QxLateEntriesWidget::reload); + + connect(ui->btAll, &QPushButton::clicked, this, [this]() { + QSignalBlocker sb1(ui->lstType); + ui->lstType->setCurrentIndex(0); + + QSignalBlocker sb3(ui->chkPending); + ui->chkPending->setChecked(true); + QSignalBlocker sb5(ui->chkAccepted); + ui->chkAccepted->setChecked(true); + QSignalBlocker sb6(ui->chkRejected); + ui->chkRejected->setChecked(true); + + reload(); + }); + connect(qf::gui::framework::Application::instance()->qxSql(), &qf::core::sql::QxSql::recChng, this, &QxLateEntriesWidget::onQxRecChng, Qt::QueuedConnection); +} + +QxLateEntriesWidget::~QxLateEntriesWidget() +{ + delete ui; +} + +void QxLateEntriesWidget::onVisibleChanged(bool is_visible) +{ + if (is_visible && isEnabled()) { + reload(); + } +} + +QxEventService *QxLateEntriesWidget::qxEventService() +{ + auto *svc = qobject_cast(Event::services::Service::serviceByName(QxEventService::serviceId())); + Q_ASSERT(svc); + return svc; +} + +void QxLateEntriesWidget::resizeColumns() +{ + auto *tv = ui->tableView; + tv->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents); +} + +void QxLateEntriesWidget::showMessage(const QString &msg, bool is_error) +{ + if (msg.isEmpty()) { + ui->lblErrorMsg->hide(); + } + else { + if (is_error) { + ui->lblErrorMsg->setStyleSheet("background: salmon"); + } + else { + ui->lblErrorMsg->setStyleSheet("background: lightgreen"); + } + ui->lblErrorMsg->show(); + } + ui->lblErrorMsg->setText(msg); +} + +void QxLateEntriesWidget::reload() +{ + if(!isEnabled()) { + return; + } + auto event_plugin = getPlugin(); + if(!getPlugin()->isEventOpen()) { + return; + } + int stage_id = event_plugin->currentStageId(); + qfs::QueryBuilder qb; + qb.select2("qxchanges", "*") + .from("qxchanges") + .where("stage_id=" + QString::number(stage_id)) + .orderBy("id"); + QStringList status_cond_list; + if (ui->chkPending->isChecked()) { + status_cond_list << "status='Pending'"; + } + if (ui->chkAccepted->isChecked()) { + status_cond_list << "status='Accepted'"; + } + if (ui->chkRejected->isChecked()) { + status_cond_list << "status='Rejected'"; + } + if (!status_cond_list.isEmpty()) { + qb.where('(' + status_cond_list.join(" OR ") + ')'); + } + if (auto ix = ui->lstType->currentIndex(); ix > 0) { + auto data_type = ui->lstType->currentText(); + qb.where(QStringLiteral("data_type='%1'").arg(data_type)); + } + qfDebug() << qb.toString(); + m_model->setQueryBuilder(qb, false); + m_model->reload(); +} + +void QxLateEntriesWidget::applyQxChange(int sql_id) +{ + qfDebug() << "reloading qxchanges row id:" << sql_id << "col id:" << COL_ID; + if(sql_id <= 0) { + return; + } + + auto qb = m_model->queryBuilder(); + qb.where(QStringLiteral("id=%1").arg(sql_id)); + qf::core::sql::Query q; + q.execThrow(qb.toString()); + if (!q.next()) { + // inserted row is filtered out + return; + } + + m_model->insertRow(0); + m_model->setValue(0, COL_ID, sql_id); + int cnt = m_model->reloadRow(0); + if(cnt != 1) { + qfWarning() << "Inserted qx change row id:" << sql_id << "reloaded in" << cnt << "instances."; + return; + } +} + +void QxLateEntriesWidget::onTableCustomContextMenuRequest(const QPoint &pos) +{ + QAction a_edit_competitor(tr("Edit competitor"), nullptr); + QList lst; + lst << &a_edit_competitor; + QAction *a = QMenu::exec(lst, ui->tableView->viewport()->mapToGlobal(pos)); + if(a == &a_edit_competitor) { + if (auto ix = ui->tableView->indexAt(pos); ix.isValid()) { + if (auto t = ix.sibling(ix.row(), col_foreign_table).data().toString(); t == "runs") { + auto run_id = ix.sibling(ix.row(), col_foreign_id).data().toInt(); + qf::core::sql::Query q; + q.execThrow(QStringLiteral("SELECT competitorId FROM runs WHERE id=%1") + .arg(run_id) + ); + if (q.next()) { + auto competitor_id = q.value(0).toInt(); + RunsWidget::showEditCompetitorDialog(competitor_id, this); + return; + } + } + } + QMessageBox::information(this, tr("Edit competitor"), tr("No competitor found for this run.")); + } +} + +void QxLateEntriesWidget::onTableDoubleClicked(const QModelIndex &ix) +{ + auto row = ui->tableView->toTableModelIndex(ix).row(); + auto data = m_model->value(row, COL_DATA).toString(); + auto qxdata = QxChangeData::fromJson(data); + if (qxdata.lateEntry.has_value()) { + auto status = m_model->value(row, COL_STATUS).toString(); + auto status_message = m_model->value(row, COL_STATUS_MESSAGE).toString(); + auto change_id = m_model->value(row, COL_ID).toInt(); + auto stage_id = m_model->value(row, COL_STAGE_ID).toInt(); + LateEntryDialog dlg(change_id, stage_id, qxdata.lateEntry.value(), status, status_message, this); + connect(&dlg, &LateEntryDialog::editCompetitor, this, [this](int competitor_id) { + RunsWidget::showEditCompetitorDialog(competitor_id, this); + }, Qt::QueuedConnection); + dlg.exec(); + ui->tableView->reloadRow(row); + } +} + +void QxLateEntriesWidget::onQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source) +{ + if (recchng.table == "qxchanges" && recchng.record.contains("status")) { + // status affect filter, cannot be solved by applyQxRecChng + m_model->reload(); + return; + } + m_model->applyQxRecChng(recchng, source); +} + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.h similarity index 52% rename from quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.h rename to quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.h index 6c972c739..85dd3265e 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.h +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.h @@ -2,40 +2,40 @@ #include +namespace qf::core::sql { struct QxRecChng; } namespace qf::gui::model { class SqlTableModel; } namespace Event::services::qx { namespace Ui { -class QxLateRegistrationsWidget; +class QxLateEntriesWidget; } -class QxClientService; +class QxEventService; -class QxLateRegistrationsWidget : public QWidget +class QxLateEntriesWidget : public QWidget { Q_OBJECT public: - explicit QxLateRegistrationsWidget(QWidget *parent = nullptr); - ~QxLateRegistrationsWidget() override; + explicit QxLateEntriesWidget(QWidget *parent = nullptr); + ~QxLateEntriesWidget() override; - void onDbEventNotify(const QString &domain, int connection_id, const QVariant &payload); void onVisibleChanged(bool is_visible); private: - QxClientService* service(); + QxEventService* qxEventService(); void reload(); - void addQxChangeRow(int sql_id); + void applyQxChange(int sql_id); void resizeColumns(); void showMessage(const QString &msg, bool is_error = false); - void applyCurrentChange(); void onTableCustomContextMenuRequest(const QPoint &pos); void onTableDoubleClicked(const QModelIndex &ix); + void onQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); private: - Ui::QxLateRegistrationsWidget *ui; + Ui::QxLateEntriesWidget *ui; qf::gui::model::SqlTableModel *m_model; }; diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.ui b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.ui similarity index 85% rename from quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.ui rename to quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.ui index 07bf69ed4..9ac4e2a67 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.ui +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateentrieswidget.ui @@ -1,7 +1,7 @@ - Event::services::qx::QxLateRegistrationsWidget - + Event::services::qx::QxLateEntriesWidget + 0 @@ -11,7 +11,7 @@ - Form + Late entries @@ -87,13 +87,6 @@ 0 - - - - Locked - - - @@ -108,13 +101,6 @@ - - - - Null - - - diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.cpp deleted file mode 100644 index f0747e77a..000000000 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxlateregistrationswidget.cpp +++ /dev/null @@ -1,307 +0,0 @@ -#include "qxlateregistrationswidget.h" -#include "ui_qxlateregistrationswidget.h" - -#include "qxclientservice.h" -#include "runchangedialog.h" -#include "runchange.h" - -#include - -#include -#include -#include -#include - -#include -#include - -namespace qfm = qf::gui::model; -namespace qfs = qf::core::sql; -namespace qfw = qf::gui; -using qf::gui::framework::getPlugin; - -namespace Event::services::qx { - -constexpr auto COL_ID = "id"; -constexpr auto COL_CHANGE_ID = "change_id"; -constexpr auto COL_DATA_TYPE = "data_type"; -constexpr auto COL_DATA_ID = "data_id"; -constexpr auto COL_DATA = "data"; -constexpr auto COL_ORIG_DATA = "orig_data"; -constexpr auto COL_STATUS = "status"; -constexpr auto COL_STATUS_MESSAGE = "status_message"; -constexpr auto COL_SOURCE = "source"; -constexpr auto COL_USER_ID = "user_id"; -constexpr auto COL_CREATED = "created"; -constexpr auto COL_LOCK_NUMBER = "lock_number"; - -constexpr auto STATUS_PENDING = "Pending"; -constexpr auto STATUS_LOCKED = "Locked"; - -constexpr auto DATA_TYPE_RUN_UPDATE_REQUEST = "RunUpdateRequest"; - -constexpr auto SOURCE_WWW = "www"; - -QxLateRegistrationsWidget::QxLateRegistrationsWidget(QWidget *parent) : - QWidget(parent), - ui(new Ui::QxLateRegistrationsWidget) -{ - ui->setupUi(this); - - ui->tableView->setReadOnly(true); - ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); - connect(ui->tableView, &qfw::TableView::customContextMenuRequested, this, &QxLateRegistrationsWidget::onTableCustomContextMenuRequest); - connect(ui->tableView, &qfw::TableView::doubleClicked, this, &QxLateRegistrationsWidget::onTableDoubleClicked); - - ui->tableView->setPersistentSettingsId("tblQxLateRegistrations"); - ui->tableView->setInsertRowEnabled(false); - ui->tableView->setCloneRowEnabled(false); - ui->tableView->setRemoveRowEnabled(false); - ui->tableView->setDirtyRowsMenuSectionEnabled(false); - - ui->toolbar->setTableView(ui->tableView); - m_model = new qfm::SqlTableModel(this); - //m->setObjectName("classes.classesModel"); - m_model->addColumn(COL_ID).setReadOnly(true).setAlignment(Qt::AlignLeft); - m_model->addColumn(COL_STATUS, tr("Status")); - m_model->addColumn(COL_DATA_TYPE, tr("Type")); - m_model->addColumn(COL_DATA_ID, tr("Data ID")).setAlignment(Qt::AlignLeft); - m_model->addColumn(COL_SOURCE, tr("Source")); - m_model->addColumn(COL_USER_ID, tr("User")); - m_model->addColumn(COL_STATUS_MESSAGE, tr("Status message")); - m_model->addColumn(COL_CREATED, tr("Created")); - m_model->addColumn(COL_CHANGE_ID, tr("Change ID")); - m_model->addColumn(COL_LOCK_NUMBER, tr("Lock")); - m_model->addColumn(COL_DATA, tr("Data"));//.setToolTip(tr("Locked for drawing")); - m_model->addColumn(COL_ORIG_DATA, tr("Orig data"));//.setToolTip(tr("Locked for drawing")); - ui->tableView->setTableModel(m_model); - - showMessage({}); - setEnabled(false); - - auto *svc = service(); - connect(svc, &Service::statusChanged, this, [this](Service::Status new_status){ - switch (new_status) { - case Service::Status::Unknown: - case Service::Status::Stopped: - setEnabled(false); - break; - case Service::Status::Running: - setEnabled(true); - reload(); - break; - } - }); - - connect(getPlugin(), &Event::EventPlugin::dbEventNotify, this, &QxLateRegistrationsWidget::onDbEventNotify, Qt::QueuedConnection); - - { - auto *lst = ui->lstType; - lst->addItem("All"); - lst->addItem("RunUpdateRequest"); - lst->addItem("RunUpdated"); - lst->addItem("OcChange"); - lst->addItem("RadioPunch"); - lst->addItem("CardReadout"); - lst->setCurrentIndex(0); - connect(lst, &QComboBox::currentIndexChanged, this, &QxLateRegistrationsWidget::reload); - } - connect(ui->chkNull, &QCheckBox::checkStateChanged, this, &QxLateRegistrationsWidget::reload); - connect(ui->chkPending, &QCheckBox::checkStateChanged, this, &QxLateRegistrationsWidget::reload); - connect(ui->chkLocked, &QCheckBox::checkStateChanged, this, &QxLateRegistrationsWidget::reload); - connect(ui->chkAccepted, &QCheckBox::checkStateChanged, this, &QxLateRegistrationsWidget::reload); - connect(ui->chkRejected, &QCheckBox::checkStateChanged, this, &QxLateRegistrationsWidget::reload); - - connect(ui->btAll, &QPushButton::clicked, this, [this]() { - QSignalBlocker sb1(ui->lstType); - ui->lstType->setCurrentIndex(0); - - QSignalBlocker sb2(ui->chkNull); - ui->chkNull->setChecked(true); - QSignalBlocker sb3(ui->chkPending); - ui->chkPending->setChecked(true); - QSignalBlocker sb4(ui->chkLocked); - ui->chkLocked->setChecked(true); - QSignalBlocker sb5(ui->chkAccepted); - ui->chkAccepted->setChecked(true); - QSignalBlocker sb6(ui->chkRejected); - ui->chkRejected->setChecked(true); - - reload(); - }); -} - -QxLateRegistrationsWidget::~QxLateRegistrationsWidget() -{ - delete ui; -} - -void QxLateRegistrationsWidget::onDbEventNotify(const QString &domain, int connection_id, const QVariant &payload) -{ - Q_UNUSED(connection_id) - if(domain == QLatin1String(Event::EventPlugin::DBEVENT_QX_CHANGE_RECEIVED)) { - int sql_id = payload.toInt(); - addQxChangeRow(sql_id); - } -} - -void QxLateRegistrationsWidget::onVisibleChanged(bool is_visible) -{ - if (is_visible && isEnabled()) { - reload(); - } -} - -QxClientService *QxLateRegistrationsWidget::service() -{ - auto *svc = qobject_cast(Event::services::Service::serviceByName(QxClientService::serviceId())); - Q_ASSERT(svc); - return svc; -} - -void QxLateRegistrationsWidget::resizeColumns() -{ - auto *tv = ui->tableView; - tv->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents); -} - -void QxLateRegistrationsWidget::showMessage(const QString &msg, bool is_error) -{ - if (msg.isEmpty()) { - ui->lblErrorMsg->hide(); - } - else { - if (is_error) { - ui->lblErrorMsg->setStyleSheet("background: salmon"); - } - else { - ui->lblErrorMsg->setStyleSheet("background: lightgreen"); - } - ui->lblErrorMsg->show(); - } - ui->lblErrorMsg->setText(msg); -} - -void QxLateRegistrationsWidget::reload() -{ - if(!isEnabled()) { - return; - } - auto event_plugin = getPlugin(); - if(!getPlugin()->isEventOpen()) { - return; - } - int stage_id = event_plugin->currentStageId(); - qfs::QueryBuilder qb; - qb.select2("qxchanges", "*") - .from("qxchanges") - .where("stage_id=" + QString::number(stage_id)) - .orderBy("id"); - QStringList status_cond_list; - if (ui->chkNull->isChecked()) { - status_cond_list << "status IS NULL"; - } - if (ui->chkPending->isChecked()) { - status_cond_list << "status='Pending'"; - } - if (ui->chkLocked->isChecked()) { - status_cond_list << "status LIKE 'Locked%'"; - } - if (ui->chkAccepted->isChecked()) { - status_cond_list << "status='Accepted'"; - } - if (ui->chkRejected->isChecked()) { - status_cond_list << "status='Rejected'"; - } - if (!status_cond_list.isEmpty()) { - qb.where('(' + status_cond_list.join(" OR ") + ')'); - } - if (auto ix = ui->lstType->currentIndex(); ix > 0) { - auto data_type = ui->lstType->currentText(); - qb.where(QStringLiteral("data_type='%1'").arg(data_type)); - } - qfDebug() << qb.toString(); - m_model->setQueryBuilder(qb, false); - m_model->reload(); -} - -void QxLateRegistrationsWidget::addQxChangeRow(int sql_id) -{ - qfDebug() << "reloading qxchanges row id:" << sql_id << "col id:" << COL_ID; - if(sql_id <= 0) { - return; - } - - auto qb = m_model->queryBuilder(); - qb.where(QStringLiteral("id=%1").arg(sql_id)); - qf::core::sql::Query q; - q.execThrow(qb.toString()); - if (!q.next()) { - // inserted row is filtered out - return; - } - - m_model->insertRow(0); - m_model->setValue(0, COL_ID, sql_id); - int cnt = m_model->reloadRow(0); - if(cnt != 1) { - qfWarning() << "Inserted qx change row id:" << sql_id << "reloaded in" << cnt << "instances."; - return; - } -} - - -void QxLateRegistrationsWidget::applyCurrentChange() -{ - auto row = ui->tableView->currentIndex().row(); - if (row < 0) { - return; - } - auto status = m_model->value(row, COL_STATUS).toString(); - if (status != STATUS_PENDING) { - return; - } - //auto run_id = m_model->value(row, COL_RUN_ID).toInt(); - //int competitor_id = 0; - // int result = getPlugin()->editCompetitor(competitor_id, run_id == 0? Mode::Insert: Mode::Edit); - -} - -void QxLateRegistrationsWidget::onTableCustomContextMenuRequest(const QPoint &pos) -{ - QAction a_neco(tr("Neco"), nullptr); - QList lst; - lst << &a_neco; - QAction *a = QMenu::exec(lst, ui->tableView->viewport()->mapToGlobal(pos)); - if(a == &a_neco) { - //printSelectedCards(); - } -} - -void QxLateRegistrationsWidget::onTableDoubleClicked(const QModelIndex &ix) -{ - auto row = ix.row(); - auto data_type = m_model->value(row, COL_DATA_TYPE).toString(); - if (data_type != DATA_TYPE_RUN_UPDATE_REQUEST) { - return; - } - auto source = m_model->value(row, COL_SOURCE).toString(); - if (source != SOURCE_WWW) { - return; - } - auto change_id = m_model->value(row, COL_CHANGE_ID).toInt(); - auto status = m_model->value(row, COL_STATUS).toString(); - auto lock_number = m_model->value(row, COL_LOCK_NUMBER).toInt(); - auto data_id = m_model->value(row, COL_DATA_ID).toInt(); - auto data = m_model->value(row, COL_DATA).toString().toUtf8(); - auto change_rec = QJsonDocument::fromJson(data).toVariant().toMap(); - auto run_change = RunChange::fromVariantMap(change_rec.value(DATA_TYPE_RUN_UPDATE_REQUEST).toMap()); - if (status == STATUS_PENDING || status == STATUS_LOCKED) { - RunChangeDialog dlg(change_id, data_id, lock_number, run_change, this); - dlg.exec(); - ui->tableView->reloadRow(row); - } -} - -} - diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxnode.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxnode.cpp new file mode 100644 index 000000000..dfcea61fd --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxnode.cpp @@ -0,0 +1,36 @@ +#include "qxnode.h" + +#include +#include + +#include + +using namespace shv::chainpack; + +namespace Event::services::qx { + +QxNode::QxNode(const std::string &name, shv::iotqt::node::ShvNode *parent) + : Super(name, parent) +{ + +} + +size_t QxNode::methodCount(const StringViewList &shv_path) +{ + if(shv_path.empty()) { + return metaMethods().size(); + } + return Super::methodCount(shv_path); +} + +const MetaMethod *QxNode::metaMethod(const StringViewList &shv_path, size_t ix) +{ + if(shv_path.empty()) { + if(metaMethods().size() <= ix) + QF_EXCEPTION("Invalid method index: " + QString::number(ix) + " of: " + QString::number(metaMethods().size())); + return &(metaMethods()[ix]); + } + return Super::metaMethod(shv_path, ix); +} + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/qxnode.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxnode.h new file mode 100644 index 000000000..79e42c174 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/qxnode.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace Event::services::qx { + +class QxNode : public shv::iotqt::node::ShvNode +{ + Q_OBJECT + + using Super = shv::iotqt::node::ShvNode; +public: + explicit QxNode(const std::string &name, shv::iotqt::node::ShvNode *parent); + + size_t methodCount(const StringViewList &shv_path) override; + const shv::chainpack::MetaMethod* metaMethod(const StringViewList &shv_path, size_t ix) override; +protected: + //shv::chainpack::RpcValue callMethodRq(const shv::chainpack::RpcRequest &rq) override; + //shv::chainpack::RpcValue callMethod(const StringViewList &shv_path, const std::string &method, const shv::chainpack::RpcValue ¶ms, const shv::chainpack::RpcValue &user_id) override; + virtual const std::vector &metaMethods() = 0; +}; + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.cpp index c9a84e23b..8e4cdd5c2 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.cpp +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.cpp @@ -1,5 +1,7 @@ #include "runchange.h" +#include + #include #include @@ -11,17 +13,25 @@ namespace Event::services::qx { // return ret; // } -RunChange RunChange::fromVariantMap(const QVariantMap &map) +LateEntry LateEntry::fromVariantMap(const QVariantMap &map) { - RunChange ret; + LateEntry ret; - if (auto v = map.value("class_name"); v.isValid()) { ret.class_name = v.toString(); } - if (auto v = map.value("first_name"); v.isValid()) { ret.first_name = v.toString(); } - if (auto v = map.value("last_name"); v.isValid()) { ret.last_name = v.toString(); } + auto id_map = map.value("id").toMap(); + if (auto id = id_map.value("RunId").toInt(); id > 0) { + ret.id = RunId{id}; + } + if (auto id = id_map.value("ClassId").toInt(); id > 0) { + ret.id = ClassId{id}; + } + if (auto v = map.value("firstname"); v.isValid()) { ret.first_name = v.toString(); } + if (auto v = map.value("lastname"); v.isValid()) { ret.last_name = v.toString(); } if (auto v = map.value("registration"); v.isValid()) { ret.registration = v.toString(); } - if (auto v = map.value("si_id"); v.isValid()) { ret.si_id = v.toInt(); } + if (auto v = map.value("starttimems"); v.isValid()) { ret.start_time_ms = v.toInt(); } + if (auto v = map.value("siid"); v.isValid()) { ret.si_id = v.toInt(); } // if (auto v = map.value("si_id_rent"); v.isValid()) { ret.si_id_rent = v.toBool(); } - ret.note = map.value("note").toString(); + if (auto v = map.value("note"); v.isValid()) { ret.note = v.toString(); } + if (auto v = map.value("paid"); v.isValid()) { ret.paid = v.toBool(); } return ret; } @@ -29,13 +39,37 @@ RunChange RunChange::fromVariantMap(const QVariantMap &map) QVariantMap OrigRunRecord::toVariantMap() const { QVariantMap ret; - ret["first_name"] = first_name; - ret["last_name"] = last_name; + ret["firstname"] = first_name; + ret["lastname"] = last_name; ret["registration"] = registration; - ret["si_id"] = si_id; + ret["siid"] = si_id; + ret["starttimems"] = start_time_ms; return ret; } -} // namespace Event::services::qx +QxChangeData QxChangeData::fromJson(const QString &json) +{ + QxChangeData ret; + auto map = QJsonDocument::fromJson(json.toUtf8()).toVariant().toMap(); + if (auto v = map.value(DATA_TYPE_LATE_ENTRY); v.isValid()) { + ret.lateEntry = LateEntry::fromVariantMap(v.toMap()); + } + return ret; +} +QxChangeStatus qxChangeStatusFromString(const QString &status) +{ + if (status.compare(QStringLiteral("Pending"), Qt::CaseInsensitive) == 0) { + return QxChangeStatus::Pending; + } + if (status.compare(QStringLiteral("Accepted"), Qt::CaseInsensitive) == 0) { + return QxChangeStatus::Accepted; + } + if (status.compare(QStringLiteral("Rejected"), Qt::CaseInsensitive) == 0) { + return QxChangeStatus::Rejected; + } + qfWarning() << "Unknown late entry status:" << status; + return QxChangeStatus::Rejected; +} +} // namespace Event::services::qx diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.h index 83ab57062..6fe5aef7a 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.h +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/runchange.h @@ -4,20 +4,39 @@ #include #include +#include +#include + namespace Event::services::qx { -struct RunChange +constexpr auto DATA_TYPE_LATE_ENTRY = "LateEntry"; + +struct RunId { int id = 0; }; +struct ClassId { int id = 0; }; + +using LateEntryForeignId = std::variant; + +struct LateEntry { - std::optional class_name; + LateEntryForeignId id = RunId{}; std::optional first_name; std::optional last_name; std::optional registration; std::optional si_id; + std::optional start_time_ms; // std::optional si_id_rent; - QString note; + std::optional note; + std::optional paid; + + // static LateEntry fromJsonString(const QString &json); + static LateEntry fromVariantMap(const QVariantMap &map); +}; + +struct QxChangeData +{ + std::optional lateEntry; - // static RunChange fromJsonString(const QString &json); - static RunChange fromVariantMap(const QVariantMap &map); + static QxChangeData fromJson(const QString &json); }; struct OrigRunRecord @@ -25,7 +44,8 @@ struct OrigRunRecord QString first_name; QString last_name; QString registration; - int si_id; + int start_time_ms = 0; + int si_id = 0; // bool si_id_rent; QVariantMap toVariantMap() const; @@ -38,27 +58,28 @@ enum class DataType { RadioPunch, CardReadout, }; -enum class ChangeStatus { +enum class QxChangeStatus { Pending, - Locked, Accepted, Rejected, }; -struct EventChange -{ - int64_t id; - QString source; - DataType data_type; - int64_t data_id; - QVariant data; - QString user_id; - ChangeStatus status; - QString status_message; - QDateTime created; - int64_t lock_number; +QxChangeStatus qxChangeStatusFromString(const QString &status); - QVariantMap toVariantMap() const; -}; +// struct EventChange +// { +// int64_t id; +// QString source; +// DataType data_type; +// int64_t data_id; +// QVariant data; +// QString user_id; +// QxChangeStatus status; +// QString status_message; +// QDateTime created; +// int64_t lock_number; + +// QVariantMap toVariantMap() const; +// }; // struct ChangeRecord // { @@ -74,6 +95,3 @@ struct EventChange // }; } // namespace Event::services::qx - - - diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.cpp deleted file mode 100644 index c44c38a94..000000000 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.cpp +++ /dev/null @@ -1,301 +0,0 @@ -#include "runchangedialog.h" -#include "ui_runchangedialog.h" - -#include "qxclientservice.h" -#include "runchange.h" - -#include - -#include -#include - -#include -#include -#include -#include -#include - -namespace Event::services::qx { - -RunChangeDialog::RunChangeDialog(int change_id, int run_id, int lock_number, const RunChange &run_change, QWidget *parent) - : QDialog(parent) - , ui(new Ui::RunChangeDialog) - , m_changeId(change_id) - , m_runId(run_id) -{ - ui->setupUi(this); - - ui->btAccept->setDisabled(true); - ui->btReject->setDisabled(true); - - connect(ui->chkForce, &QCheckBox::checkStateChanged, this, [this]() { - ui->btAccept->setDisabled(!ui->chkForce->isChecked()); - ui->btReject->setDisabled(!ui->chkForce->isChecked()); - }); - connect(ui->btAccept, &QPushButton::clicked, this, [this]() { - resolveChanges(true); - }); - connect(ui->btReject, &QPushButton::clicked, this, [this]() { - resolveChanges(false); - }); - - ui->edClassName->setText(run_change.class_name.value_or(QString())); - ui->edNote->setText(run_change.note); - - ui->edRunId->setValue(run_id); - ui->edChangeId->setValue(change_id); - ui->edLockNumber->setValue(lock_number); - - ui->grpFirstName->setChecked(run_change.first_name.has_value()); - ui->edFirstName->setText(run_change.first_name.has_value()? run_change.first_name.value(): QString()); - - ui->grpLastName->setChecked(run_change.last_name.has_value()); - ui->edLastName->setText(run_change.last_name.has_value()? run_change.last_name.value(): QString()); - - ui->grpRegistration->setChecked(run_change.registration.has_value()); - ui->edRegistration->setText(run_change.registration.has_value()? run_change.registration.value(): QString()); - - ui->grpSiCard->setChecked(run_change.si_id.has_value()); - ui->edSiCard->setValue(run_change.si_id.has_value()? run_change.si_id.value(): 0); - - if (run_id > 0) { - loadOrigValues(); - } - else { - loadClassId(); - } - lockChange(); -} - -RunChangeDialog::~RunChangeDialog() -{ - delete ui; -} - -QxClientService *RunChangeDialog::service() -{ - auto *svc = qobject_cast(Service::serviceByName(QxClientService::serviceId())); - Q_ASSERT(svc); - return svc; -} - -void RunChangeDialog::setMessage(const QString &msg, bool error) -{ - if (msg.isEmpty()) { - ui->lblError->setStyleSheet({}); - } - else if (error) { - ui->lblError->setStyleSheet("background: salmon"); - } - else { - ui->lblError->setStyleSheet({}); - } - ui->lblError->setText(msg); -} - -void RunChangeDialog::loadOrigValues() -{ - Q_ASSERT(m_runId > 0); - - qf::core::sql::Query q; - q.execThrow(QStringLiteral("SELECT classes.name AS class_name, competitors.id AS competitor_id" - " FROM runs" - " JOIN competitors ON competitors.id=runs.competitorId AND runs.id=%1" - " LEFT JOIN classes ON competitors.classId=classes.id") - .arg(m_runId) - ); - if (q.next()) { - ui->edClassName->setText(q.value("class_name").toString()); - m_competitorId = q.value("competitor_id").toInt(); - } - - Competitors::CompetitorDocument doc; - doc.load(m_competitorId, qf::gui::model::DataDocument::ModeView); - - m_origValues.first_name = doc.value("firstName").toString(); - m_origValues.last_name = doc.value("lastName").toString(); - m_origValues.registration = doc.value("registration").toString(); - m_origValues.si_id = doc.value("siId").toInt(); - - ui->edFirstNameOrig->setText(m_origValues.first_name); - ui->edLastNameOrig->setText(m_origValues.last_name); - ui->edRegistrationOrig->setText(m_origValues.registration); - ui->edSiCardOrig->setValue(m_origValues.si_id); -} - -void RunChangeDialog::loadClassId() -{ - auto class_name = ui->edClassName->text(); - if (class_name.isEmpty()) { - return; - } - qf::core::sql::Query q; - q.execThrow(QStringLiteral("SELECT id" - " FROM classes" - " WHERE name='%1'") - .arg(class_name) - ); - if (q.next()) { - m_classId = q.value("id").toInt(); - } -} - -void RunChangeDialog::lockChange() -{ - // NIY - // auto *svc = service(); - // auto *nm = svc->networkManager(); - - // auto path = QStringLiteral("/api/event/%1/changes").arg(svc->eventId()); - // QUrlQuery query; - // query.addQueryItem("from_id", QString::number(m_changeId)); - // query.addQueryItem("limit", QString::number(1)); - // svc->getHttpJson(path, query, this, [this](auto data, auto error) { - // if (!error.isEmpty()) { - // setMessage(error, true); - // return; - // } - // auto rec = data.toList().value(0).toMap(); - // }); - - - // QNetworkRequest request; - // auto url = svc->exchangeServerUrl(); - // // qfInfo() << "url " << url.toString(); - // url.setPath("/api/event/current/changes/lock-change"); - - // QUrlQuery query; - // query.addQueryItem("change_id", QString::number(m_changeId)); - // auto connection_id = QxClientService::currentConnectionId(); - // query.addQueryItem("lock_number", QString::number(connection_id)); - // url.setQuery(query); - // qfInfo() << "GET " << url.toString(); - - // request.setUrl(url); - // request.setRawHeader(QxClientService::QX_API_TOKEN, svc->apiToken()); - // auto *reply = nm->get(request); - // connect(reply, &QNetworkReply::finished, this, [this, reply, connection_id]() { - // auto data = reply->readAll(); - // if (reply->error() == QNetworkReply::NetworkError::NoError) { - // m_lockNumber = data.toInt(); - // ui->edLockNumber->setValue(m_lockNumber); - // if (m_lockNumber == connection_id) { - // ui->btAccept->setDisabled(false); - // ui->btReject->setDisabled(false); - - // qf::core::sql::Query q; - // q.execThrow(QStringLiteral("UPDATE qxchanges SET lock_number=%1, status='Locked' WHERE id=%2") - // .arg(connection_id) - // .arg(m_changeId) - // ); - // } - // else { - // setMessage(tr("Change is locked already by other client: %1, current client id:.%2").arg(m_lockNumber).arg(connection_id), false); - // } - // } - // else { - // setMessage(tr("Lock change error: %1\n%2").arg(reply->errorString()).arg(QString::fromUtf8(data)), true); - // } - // reply->deleteLater(); - // }); -} - -void RunChangeDialog::resolveChanges(bool is_accepted) -{ - if (is_accepted) { - applyLocalChanges(is_accepted); - } - auto *svc = service(); - auto *nm = svc->networkManager(); - QNetworkRequest request; - auto url = svc->exchangeServerUrl(); - // qfInfo() << "url " << url.toString(); - url.setPath("/api/event/current/changes/resolve-change"); - - QUrlQuery query; - query.addQueryItem("change_id", QString::number(m_changeId)); - query.addQueryItem("lock_number", QString::number(m_lockNumber)); - query.addQueryItem("accepted", is_accepted? "true": "false"); - query.addQueryItem("status_message", ui->edStatusMessage->text()); - url.setQuery(query); - - request.setUrl(url); - request.setRawHeader(QxClientService::QX_API_TOKEN, svc->apiToken()); - auto *reply = nm->get(request); - connect(reply, &QNetworkReply::finished, this, [this, reply]() { - if (reply->error() == QNetworkReply::NetworkError::NoError) { - accept(); - } - else { - QMessageBox::warning(this, - QCoreApplication::applicationName(), - tr("Update change error: %1").arg(reply->errorString())); - } - reply->deleteLater(); - }); -} - -void RunChangeDialog::applyLocalChanges(bool is_accepted) -{ - using namespace qf::gui::model; - - bool is_insert = m_runId == 0; - Competitors::CompetitorDocument doc; - doc.load(m_competitorId, is_insert? DataDocument::ModeInsert: DataDocument::ModeEdit); - if (is_insert) { - doc.setValue("classId", m_classId); - } - if (ui->grpFirstName->isChecked()) { - doc.setValue("firstName", ui->edFirstName->text()); - } - if (ui->grpLastName->isChecked()) { - doc.setValue("lastName", ui->edLastName->text()); - } - if (ui->grpRegistration->isChecked()) { - doc.setValue("registration", ui->edRegistration->text()); - } - if (ui->grpSiCard->isChecked()) { - if (is_insert) { - doc.setValue("siId", ui->edSiCard->value()); - } - else { - qf::core::sql::Query q; - q.execThrow(QStringLiteral("UPDATE runs SET siId=%1 WHERE id=%2") - .arg(ui->edSiCard->value()) - .arg(m_runId) - ); - } - } - doc.save(); - { - qf::core::sql::Query q; - q.execThrow(QStringLiteral("UPDATE qxchanges SET status='%1' WHERE id=%2") - .arg(is_accepted? "Accepted": "Rejected") - .arg(m_changeId) - ); - } - { - auto dc_str = qf::core::Utils::qvariantToJson(m_origValues.toVariantMap()); - QString qs = "UPDATE qxchanges SET orig_data=:orig_data WHERE id=:id"; - qf::core::sql::Query q; - q.prepare(qs, qf::core::Exception::Throw); - q.bindValue(":orig_data", dc_str); - q.bindValue(":id", m_changeId); - q.exec(qf::core::Exception::Throw); - } -} - -bool RunChangeDialog::checkHttpError(QNetworkReply *reply) -{ - if (reply->error() != QNetworkReply::NetworkError::NoError) { - setMessage(tr("Http error: %1\n%2") - .arg(reply->request().url().toString()) - .arg(reply->errorString()), true); - return false; - } - return true; -} - -} // namespace Event::services::qx - - diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.h deleted file mode 100644 index d704a1d48..000000000 --- a/quickevent/app/quickevent/plugins/Event/src/services/qx/runchangedialog.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef RUNCHANGEDIALOG_H -#define RUNCHANGEDIALOG_H - -#include "runchange.h" - -#include - -class QNetworkReply; - -namespace Event::services::qx { - -namespace Ui { -class RunChangeDialog; -} - -struct RunChange; -class QxClientService; - -class RunChangeDialog : public QDialog -{ - Q_OBJECT - -public: - explicit RunChangeDialog(int change_id, int run_id, int lock_number, const RunChange &run_change, QWidget *parent = nullptr); - ~RunChangeDialog() override; - -private: - QxClientService* service(); - void setMessage(const QString &msg, bool error); - - void loadOrigValues(); - void loadClassId(); - - void lockChange(); - - void resolveChanges(bool is_accepted); - void applyLocalChanges(bool is_accepted); - - bool checkHttpError(QNetworkReply *reply); -private: - Ui::RunChangeDialog *ui; - int m_changeId = 0; - int m_runId = 0; - int m_competitorId = 0; - int m_classId = 0; - int m_lockNumber = 0; - OrigRunRecord m_origValues; -}; - - -} // namespace Event::services::qx - - -#endif // RUNCHANGEDIALOG_H diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapi.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapi.cpp new file mode 100644 index 000000000..25a68b6fc --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapi.cpp @@ -0,0 +1,262 @@ +#include "sqlapi.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace shv::chainpack; + +namespace Event::services::qx { + +RpcValue dbFieldToRpcValue(const DbField &fld) +{ + RpcValue::Map ret; + ret["name"] = fld.name.toStdString(); + return RpcValue(std::move(ret)); +} + +RpcValue execResultToRpcValue(const ExecResult &res) +{ + RpcValue::Map ret; + ret["numRowsAffected"] = res.numRowsAffected; + ret["lastInsertId"] = res.lastInsertId.has_value()? RpcValue(res.lastInsertId.value()): RpcValue(nullptr); + return ret; +} + +RpcValue queryResultToRpcValue(const QueryResult &res) +{ + RpcValue::Map ret; + RpcValue::List flds; + for(const auto &fld : res.fields) { + flds.push_back(dbFieldToRpcValue(fld)); + } + RpcValue::List rpc_rows; + for (const auto &row : res.rows) { + RpcValue::List rpc_row; + for (const auto &cell : row) { + rpc_row.push_back(shv::coreqt::rpc::qVariantToRpcValue(cell)); + } + rpc_rows.push_back(std::move(rpc_row)); + } + ret["fields"] = flds; + ret["rows"] = std::move(rpc_rows); + return ret; +} + +namespace { +RpcValue::List toShvRecordList(const QList &records) +{ + RpcValue::List ret; + for (const auto &rec : records) { + ret.push_back(shv::coreqt::rpc::qVariantToRpcValue(rec)); + } + return ret; +} +QStringList toQStringList(const std::vector &sl) +{ + QStringList qsl; + for (const auto &s : sl) { + qsl << QString::fromStdString(s); + } + return qsl; +} +} + +SqlQueryAndParams SqlQueryAndParams::fromRpcValue(const shv::chainpack::RpcValue &rv) +{ + const auto &lst = rv.asList(); + auto sql_query = QString::fromStdString(lst.valref(0).asString()); + const auto &sql_params = lst.valref(1); + return SqlQueryAndParams { .query = sql_query, .params = shv::coreqt::rpc::rpcValueToQVariant(sql_params).toMap() }; +} + +RpcValue qxRecChngToRpcValue(const qf::core::sql::QxRecChng &chng) +{ + auto m = chng.toVariantMap(); + return shv::coreqt::rpc::qVariantToRpcValue(m); +} + +//============================================== +// SqlApi +//============================================== +SqlApi::SqlApi(QObject *parent) + : QObject{parent} +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + connect(qxsql, &qf::core::sql::QxSql::recChng, this, &SqlApi::recChng); +} + +// void SqlApi::emitRecChng(const qf::core::sql::QxRecChng &chng) +// { +// qfInfo() << "REC_CHNG:" << qxRecChngToRpcValue(chng).toCpon(); +// emit recchng(chng); +// } + +// namespace { + +// class Transaction +// { +// public: +// Transaction(QSqlDatabase db) : m_db(db) { +// if (!m_db.transaction()) { +// qfWarning() << "BEGIN transaction error:" << m_db.lastError().text(); +// throw std::runtime_error("BEGIN transaction error"); +// } +// } +// ~Transaction() { +// if (m_inTransaction) { +// m_db.rollback(); +// } +// } +// void commit() { +// if (!m_db.commit()) { +// qfWarning() << "COMMIT transaction error:" << m_db.lastError().text(); +// throw std::runtime_error("COMMIT transaction error"); +// } +// m_inTransaction = false; +// } +// private: +// QSqlDatabase m_db; +// bool m_inTransaction = true; +// }; + +// void bindParams(qf::core::sql::Query &q, const Record ¶ms) +// { +// for (const auto &[k, v] : params.asKeyValueRange()) { +// q.bindValue(':' + k, v); +// } +// } + +// QueryResult sqlQuery(const SqlQueryAndParams ¶ms) +// { +// qf::core::sql::Query q; +// q.prepare(params.query, qf::core::Exception::Throw); +// bindParams(q, params.params); +// q.exec(qf::core::Exception::Throw); + +// QueryResult ret; +// QSqlRecord rec = q.record(); +// for (int i = 0; i < rec.count(); ++i) { +// QSqlField fld = rec.field(i); +// DbField rfld; +// rfld.name = fld.name(); +// // rfld.name.replace("__", "."); +// ret.fields.push_back(rfld); +// } +// while(q.next()) { +// QueryResult::Row row; +// for (int i = 0; i < rec.count(); ++i) { +// row.push_back(q.value(i)); +// //shvError() << v << v.isNull() << jsv.toVariant() << jsv.toVariant().isNull(); +// } +// ret.rows.insert(ret.rows.size(), row); +// } +// return ret; +// } + +// ExecResult sqlExec(const SqlQueryAndParams ¶ms) +// { +// qf::core::sql::Query q; +// q.prepare(params.query, qf::core::Exception::Throw); +// bindParams(q, params.params); +// q.exec(qf::core::Exception::Throw); + +// ExecResult ret; +// ret.numRowsAffected = q.numRowsAffected(); +// ret.lastInsertId = q.lastInsertId().toInt(); +// return ret; +// } + +// } + +ExecResult SqlApi::exec(const SqlQueryAndParams ¶ms) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + return qxsql->exec(params.query, params.params); +} + +QueryResult SqlApi::query(const SqlQueryAndParams ¶ms) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + return qxsql->query(params.query, params.params); +} + +void SqlApi::transaction(const std::string &query, const shv::chainpack::RpcValue::List ¶ms) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + QVariantList qparams; + for (const auto &p : params) { + qparams << shv::coreqt::rpc::rpcValueToQVariant(p); + } + qxsql->transaction(QString::fromStdString(query), qparams); +} + +RpcValue::List SqlApi::list(const std::string &table, const std::vector &fields, std::optional ids_above, std::optional limit) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + auto records = qxsql->listRecords(QString::fromStdString(table), toQStringList(fields), ids_above, limit); + return toShvRecordList(records); +} +namespace { +std::string to_lower(const std::string &s) +{ + std::string result; + result.reserve(s.size()); + + std::ranges::transform(s, std::back_inserter(result), + [](unsigned char c) { return std::tolower(c); }); + + return result; +} + +[[maybe_unused]] Record normalizeFieldNames(const Record &rec) +{ + Record ret; + for (const auto &[k, v] : rec.asKeyValueRange()) { + ret[QString::fromStdString(to_lower(k.toStdString()))] = v; + } + return ret; +} +} +int64_t SqlApi::create(const std::string &table, const RpcValue::Map &record) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + return qxsql->createRecord(QString::fromStdString(table), shv::coreqt::rpc::rpcValueToQVariant(record).toMap(), this); +} + +std::optional SqlApi::read(const std::string &table, int64_t id, const std::vector &fields) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + if (auto rec = qxsql->readRecord(QString::fromStdString(table), id, toQStringList(fields)); rec.has_value()) { + return shv::coreqt::rpc::qVariantToRpcValue(rec.value()).asMap(); + } + return {}; +} + +bool SqlApi::update(const std::string &table, int64_t id, const RpcValue::Map &record) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + return qxsql->updateRecord(QString::fromStdString(table), id, shv::coreqt::rpc::rpcValueToQVariant(record).toMap(), this); +} + +bool SqlApi::drop(const std::string &table, int64_t id) +{ + auto *qxsql = qf::gui::framework::Application::instance()->qxSql(); + return qxsql->deleteRecord(QString::fromStdString(table), id, this); +} + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapi.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapi.h new file mode 100644 index 000000000..4e3294683 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapi.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include + +#include +#include + +namespace qf::core::sql { struct QxRecChng; } + +namespace Event::services::qx { + +using DbField = qf::core::sql::DbField; +using ExecResult = qf::core::sql::ExecResult; +using QueryResult = qf::core::sql::QueryResult; + +shv::chainpack::RpcValue dbFieldToRpcValue(const DbField &fld); +shv::chainpack::RpcValue execResultToRpcValue(const ExecResult &res); +shv::chainpack::RpcValue queryResultToRpcValue(const QueryResult &res); + +using Record = QVariantMap; + +struct SqlQueryAndParams +{ + QString query; + Record params; + + static SqlQueryAndParams fromRpcValue(const shv::chainpack::RpcValue &rv); +}; + +shv::chainpack::RpcValue qxRecChngToRpcValue(const qf::core::sql::QxRecChng &chng); + + +class SqlApi : public QObject +{ + Q_OBJECT +public: + explicit SqlApi(QObject *parent = nullptr); + + Q_SIGNAL void recChng(const qf::core::sql::QxRecChng &recchng, QObject *source); + + ExecResult exec(const SqlQueryAndParams ¶ms); + QueryResult query(const SqlQueryAndParams ¶ms); + void transaction(const std::string &query, const shv::chainpack::RpcValue::List ¶ms); + shv::chainpack::RpcValue::List list(const std::string &table, const std::vector &fields, std::optional ids_above, std::optional limit); + int64_t create(const std::string &table, const shv::chainpack::RpcValue::Map &record); + std::optional read(const std::string &table, int64_t id, const std::vector &fields); + bool update(const std::string &table, int64_t id, const shv::chainpack::RpcValue::Map &record); + bool drop(const std::string &table, int64_t id); +}; + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapinode.cpp b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapinode.cpp new file mode 100644 index 000000000..063e5d516 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapinode.cpp @@ -0,0 +1,137 @@ +#include "sqlapinode.h" +#include "sqlapi.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +using namespace shv::chainpack; +using namespace shv::coreqt::data; + +namespace Event::services::qx { + +SqlApiNode::SqlApiNode(shv::iotqt::node::ShvNode *parent) + : Super("sql", parent) + , m_sqlApi(new SqlApi(this)) +{ + connect(m_sqlApi, &SqlApi::recChng, this, [this](const qf::core::sql::QxRecChng &recchng, QObject *source) { + Q_UNUSED(source); + RpcSignal sig; + sig.setSignal("recchng"); + sig.setShvPath(shvPath().asString()); + sig.setParams(shv::coreqt::rpc::qVariantToRpcValue(recchng.toVariantMap())); + qfInfo() << "emit recchng:" << recchng.table; + emitSendRpcMessage(sig); + }); + +} + +namespace { +auto METH_QUERY = "query"; +auto METH_EXEC = "exec"; +auto METH_TRANSACTION = "transaction"; +auto METH_LIST = "list"; +auto METH_CREATE = "create"; +auto METH_READ = "read"; +auto METH_UPDATE = "update"; +auto METH_DELETE = "delete"; +} + +const std::vector &SqlApiNode::metaMethods() +{ + static std::vector meta_methods { + methods::DIR, + methods::LS, + {METH_QUERY, MetaMethod::Flag::LargeResultHint, "[s:query,{s|i|b|t|n}:params]", "{{s:name}:fields,[[s|i|b|t|n]]:rows}", AccessLevel::Read}, + {METH_EXEC, MetaMethod::Flag::None, "[s:query,{s|i|b|t|n}:params]", "{i:rows_affected,i|n:insert_id}", AccessLevel::Write}, + {METH_TRANSACTION, MetaMethod::Flag::None, "[s:query,[{s|i|b|t|n}]:params]", "n", AccessLevel::Write}, + {METH_LIST, MetaMethod::Flag::LargeResultHint, "{s:table,[s]|n:fields,i|n:ids_above,i|n:limit}", "[{s|i|b|t|n}]", AccessLevel::Write}, + {METH_CREATE, MetaMethod::Flag::None, "{s:table,{s|i|b|t|n}:record}", "i", AccessLevel::Write}, + {METH_READ, MetaMethod::Flag::None, "{s:table,i:id,{s}|n:fields}", "{s|i|b|t|n}|n", AccessLevel::Read}, + {METH_UPDATE, MetaMethod::Flag::None, "{s:table,i:id,{s|i|b|t|n}:record}", "b", AccessLevel::Write}, + {METH_DELETE, MetaMethod::Flag::None, "{s:table,i:id}", "b", AccessLevel::Write}, + }; + return meta_methods; +} + +RpcValue SqlApiNode::callMethod(const StringViewList &shv_path, const std::string &method, const shv::chainpack::RpcValue ¶ms, const shv::chainpack::RpcValue &user_id) +{ + qfLogFuncFrame() << shv_path.join('/') << method; + //eyascore::utils::UserId user_id = eyascore::utils::UserId::makeUserName(QString::fromStdString(rq.userId().toMap().value("userName").toString())); + if(shv_path.empty()) { + if(method == METH_EXEC) { + auto res = m_sqlApi->exec(SqlQueryAndParams::fromRpcValue(params)); + return execResultToRpcValue(res); + } + if(method == METH_QUERY) { + auto res = m_sqlApi->query(SqlQueryAndParams::fromRpcValue(params)); + return queryResultToRpcValue(res); + } + if(method == METH_TRANSACTION) { + auto sql_query = params.asList().valref(0).asString(); + const auto &sql_params = params.asList().valref(0); + m_sqlApi->transaction(sql_query, sql_params.asList()); + return RpcValue(nullptr); + } + if(method == METH_LIST) { + const auto &map = params.asMap(); + const auto &table = map.value("table").asString(); + std::vector fields; + for (const auto &fn : map.valref("fields").asList()) { + fields.push_back(fn.asString()); + } + auto ids_above = map.contains("ids_above")? std::optional(map.value("ids_above").toInt64()): std::optional(); + auto limit = map.contains("limit")? std::optional(map.value("limit").toInt64()): std::optional(); + auto res = m_sqlApi->list(table, fields, ids_above, limit); + return res; + } + if(method == METH_CREATE) { + const auto &map = params.asMap(); + const auto &table = map.value("table").asString(); + const auto &record = map.valref("record").asMap(); + auto res = m_sqlApi->create(table, record); + return res; + } + if(method == METH_READ) { + const auto &map = params.asMap(); + const auto &table = map.value("table").asString(); + auto id = map.value("id").toInt64(); + std::vector fields; + for (const auto &fn : map.valref("fields").asList()) { + fields.push_back(fn.asString()); + } + auto res = m_sqlApi->read(table, id, fields); + if (res.has_value()) { + return res.value(); + } + return RpcValue(nullptr); + } + if(method == METH_UPDATE) { + const auto &map = params.asMap(); + const auto &table = map.value("table").asString(); + auto id = map.value("id").toInt64(); + const auto &record = map.valref("record").asMap(); + auto res = m_sqlApi->update(table, id, record); + return res; + } + if(method == METH_DELETE) { + const auto &map = params.asMap(); + const auto &table = map.value("table").asString(); + auto id = map.valref("id").toInt(); + auto res = m_sqlApi->drop(table, id); + return res; + } + } + return Super::callMethod(shv_path, method, params, user_id); +} + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapinode.h b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapinode.h new file mode 100644 index 000000000..d461ae689 --- /dev/null +++ b/quickevent/app/quickevent/plugins/Event/src/services/qx/sqlapinode.h @@ -0,0 +1,28 @@ +#pragma once + +#include "qxnode.h" + +class QSqlQuery; +class QSqlRecord; + +namespace shv::coreqt::data { class RpcSqlResult; } + +namespace Event::services::qx { + +class SqlApi; + +class SqlApiNode : public QxNode +{ + Q_OBJECT + + using Super = QxNode; +public: + explicit SqlApiNode(shv::iotqt::node::ShvNode *parent); +protected: + const std::vector &metaMethods() override; + shv::chainpack::RpcValue callMethod(const StringViewList &shv_path, const std::string &method, const shv::chainpack::RpcValue ¶ms, const shv::chainpack::RpcValue &user_id) override; +private: + SqlApi *m_sqlApi = nullptr; +}; + +} diff --git a/quickevent/app/quickevent/plugins/Event/src/services/serviceswidget.cpp b/quickevent/app/quickevent/plugins/Event/src/services/serviceswidget.cpp index e4c9a604a..324dd0424 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/serviceswidget.cpp +++ b/quickevent/app/quickevent/plugins/Event/src/services/serviceswidget.cpp @@ -35,12 +35,7 @@ void ServicesWidget::reload() Service *svc = Service::serviceAt(i); auto *sw = new ServiceWidget(); - sw->setStatus(svc->status()); - connect(svc, &Service::statusChanged, sw, &ServiceWidget::setStatus); - sw->setServiceId(svc->serviceId(), svc->serviceDisplayName()); - sw->setMessage(svc->statusMessage()); - connect(svc, &Service::statusMessageChanged, sw, &ServiceWidget::setMessage); - connect(sw, &ServiceWidget::setRunningRequest, svc, &Service::setRunning); + sw->setService(svc); ly2->addWidget(sw); } diff --git a/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.cpp b/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.cpp index dd4bf8233..3069eb848 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.cpp +++ b/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.cpp @@ -45,10 +45,15 @@ void ServiceWidget::setStatus(Service::Status st) } } -void ServiceWidget::setServiceId(const QString &id, const QString &display_name) +void ServiceWidget::setService(Service *service) { - m_serviceId = id; - ui->lblServiceName->setText(display_name.isEmpty()? id: display_name); + m_serviceId = service->serviceId(); + ui->lblServiceName->setText(service->serviceDisplayName()); + setStatus(service->status()); + connect(service, &Service::statusChanged, this, &ServiceWidget::setStatus); + setMessage(service->statusMessage()); + connect(service, &Service::statusMessageChanged, this, &ServiceWidget::setMessage); + connect(this, &ServiceWidget::setRunningRequest, service, &Service::setRunning); } QString ServiceWidget::serviceId() const diff --git a/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.h b/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.h index f659cafde..4d589d2fe 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.h +++ b/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.h @@ -21,7 +21,7 @@ class ServiceWidget : public QWidget ~ServiceWidget(); void setStatus(Service::Status st); - void setServiceId(const QString &id, const QString &display_name); + void setService(Service *service); QString serviceId() const; void setMessage(const QString &m); diff --git a/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.ui b/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.ui index cdff60fa1..510d437a7 100644 --- a/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.ui +++ b/quickevent/app/quickevent/plugins/Event/src/services/servicewidget.ui @@ -87,6 +87,9 @@ neco neco + + true + diff --git a/quickevent/app/quickevent/plugins/Event/src/stage.h b/quickevent/app/quickevent/plugins/Event/src/stage.h index f52c79399..ec50622c9 100644 --- a/quickevent/app/quickevent/plugins/Event/src/stage.h +++ b/quickevent/app/quickevent/plugins/Event/src/stage.h @@ -16,7 +16,6 @@ class StageData : public QVariantMap QF_VARIANTMAP_KEY_FIELD(bool, useallmaps, is, set, UseAllMaps) QF_VARIANTMAP_KEY_FIELD(QVariantMap, drawingconfig, d, setD, rawingConfig) - QF_VARIANTMAP_KEY_FIELD(QString, qxapitoken, qx, setQx, ApiToken) public: StageData(const QVariantMap &data = QVariantMap()); diff --git a/quickevent/app/quickevent/plugins/Runs/src/runsplugin.cpp b/quickevent/app/quickevent/plugins/Runs/src/runsplugin.cpp index b5e3ccbe7..53f34f054 100644 --- a/quickevent/app/quickevent/plugins/Runs/src/runsplugin.cpp +++ b/quickevent/app/quickevent/plugins/Runs/src/runsplugin.cpp @@ -11,7 +11,7 @@ // #include "../../Competitors/src/competitorwidget.h" #include "../../CardReader/src/cardreaderplugin.h" #include "../../Event/src/eventplugin.h" -#include "../../Event/src/services/qx/qxlateregistrationswidget.h" +#include "../../Event/src/services/qx/qxlateentrieswidget.h" #include #include @@ -143,15 +143,15 @@ void RunsPlugin::onInstalled() } { auto *dw = new qff::DockWidget(nullptr); - dw->setObjectName("qxLateRegistrationsDockWidget"); - dw->setPersistentSettingsId("Runs/qxLateRegistrations"); - dw->setWindowTitle(tr("Late registrations")); - auto *ew = new Event::services::qx::QxLateRegistrationsWidget(); + dw->setObjectName("qxLateEntriesDockWidget"); + dw->setPersistentSettingsId("Runs/qxLateEntries"); + dw->setWindowTitle(tr("Late entries")); + auto *ew = new Event::services::qx::QxLateEntriesWidget(); dw->setWidget(ew); fwk->addDockWidget(Qt::RightDockWidgetArea, dw); dw->hide(); - connect(dw, &qff::DockWidget::visibilityChanged, ew, &Event::services::qx::QxLateRegistrationsWidget::onVisibleChanged); + connect(dw, &qff::DockWidget::visibilityChanged, ew, &Event::services::qx::QxLateEntriesWidget::onVisibleChanged); auto *a = dw->toggleViewAction(); // a->setShortcut(QKeySequence("ctrl+shift+E")); @@ -1298,7 +1298,7 @@ qf::core::sql::QueryBuilder RunsPlugin::runsQuery(int stage_id, int class_id, bo qfs::QueryBuilder qb; qb.select2("runs", "*") .select2("classes", "name") - .select2("competitors", "id, iofId, registration, licence, ranking, siId, note") + .select2("competitors", "id, firstName, lastName, iofId, registration, licence, ranking, siId, note") .select("COALESCE(lastName, '') || ' ' || COALESCE(firstName, '') AS competitorName") .select("lentcards.siid IS NOT NULL AS cardInLentTable") diff --git a/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.cpp b/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.cpp index 858272e09..b6409de8f 100644 --- a/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.cpp +++ b/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.cpp @@ -1,13 +1,15 @@ #include "runstablemodel.h" +#include "../../Event/src/eventplugin.h" + #include #include -#include "../../Event/src/eventplugin.h" #include #include #include +#include #include #include #include @@ -50,7 +52,7 @@ RunsTableModel::RunsTableModel(QObject *parent) setColumn(col_competitors_note, ColumnDefinition("competitors.note", tr("Note"))); connect(this, &RunsTableModel::dataChanged, this, &RunsTableModel::onDataChanged, Qt::QueuedConnection); - connect(qf::gui::framework::Application::instance(), &qf::gui::framework::Application::qxRecChng, this, &RunsTableModel::onQxRecChng, Qt::QueuedConnection); + connect(qf::gui::framework::Application::instance()->qxSql(), &qf::core::sql::QxSql::recChng, this, &RunsTableModel::onQxRecChng, Qt::QueuedConnection); } Qt::ItemFlags RunsTableModel::flags(const QModelIndex &index) const @@ -101,6 +103,12 @@ QVariant RunsTableModel::data(const QModelIndex &index, int role) const QVariant RunsTableModel::value(int row_ix, int column_ix) const { + if(column_ix == col_competitorName) { + qf::core::utils::TableRow row = tableRow(row_ix); + auto first_name = row.value(QStringLiteral("firstName")).toString(); + auto last_name = row.value(QStringLiteral("lastName")).toString(); + return last_name + ' ' + first_name; + } if(column_ix == col_runFlags) { qf::core::utils::TableRow row = tableRow(row_ix); bool mis_punch = row.value(QStringLiteral("runs.misPunch")).toBool(); @@ -355,7 +363,11 @@ void RunsTableModel::onDataChanged(const QModelIndex &top_left, const QModelInde void RunsTableModel::onQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source) { - handleQxRecChng(recchng, source); + if (recchng.op == qf::core::sql::RecOp::Insert) { + // inserts might be in filterred out class or stage, this cannot be solved by applyQxRecChng + return; + } + applyQxRecChng(recchng, source); } bool RunsTableModel::postRow(int row_no, bool throw_exc) diff --git a/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.h b/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.h index ed54e71b6..08bc5a67c 100644 --- a/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.h +++ b/quickevent/app/quickevent/plugins/Runs/src/runstablemodel.h @@ -1,5 +1,4 @@ -#ifndef RUNSTABLEMODEL_H -#define RUNSTABLEMODEL_H +#pragma once #include @@ -67,4 +66,3 @@ class RunsTableModel : public quickevent::gui::og::SqlTableModel void onQxRecChng(const qf::core::sql::QxRecChng &recchng, QObject *source); }; -#endif // RUNSTABLEMODEL_H diff --git a/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.cpp b/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.cpp index c40d4b517..9c0661303 100644 --- a/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.cpp +++ b/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.cpp @@ -8,9 +8,9 @@ #include "cardflagsdialog.h" #include -#include #include #include +#include #include #include @@ -20,6 +20,8 @@ #include #include +#include + #include #include #include @@ -433,4 +435,3 @@ void RunsTableWidget::onBadTableDataInput(const QString &message) qf::gui::dialogs::MessageBox::showError(this, message); } - diff --git a/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.h b/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.h index 8ba2e0964..263ddbd5c 100644 --- a/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.h +++ b/quickevent/app/quickevent/plugins/Runs/src/runstablewidget.h @@ -7,6 +7,7 @@ class RunsTableItemDelegate; class CourseItemDelegate; namespace qf::gui { class TableView; } +namespace qf::core::sql { struct QxRecChng; } namespace Ui { class RunsTableWidget; diff --git a/quickevent/app/quickevent/plugins/Runs/src/runswidget.cpp b/quickevent/app/quickevent/plugins/Runs/src/runswidget.cpp index 53793196f..c5d2e0cd3 100644 --- a/quickevent/app/quickevent/plugins/Runs/src/runswidget.cpp +++ b/quickevent/app/quickevent/plugins/Runs/src/runswidget.cpp @@ -1243,6 +1243,18 @@ void RunsWidget::editCompetitor_helper(const QVariant &id, int mode, int siid) } } +void RunsWidget::showEditCompetitorDialog(int competitor_id, QWidget *parent) +{ + auto *w = new CompetitorWidget(); + w->setWindowTitle(tr("Edit Competitor")); + qfd::Dialog dlg(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, parent); + dlg.setDefaultButton(QDialogButtonBox::Ok); + + dlg.setCentralWidget(w); + w->load(competitor_id, qf::gui::model::DataDocument::ModeEdit); + dlg.exec(); +} + void RunsWidget::updateClassComboBox() { m_cbxClasses->blockSignals(true); diff --git a/quickevent/app/quickevent/plugins/Runs/src/runswidget.h b/quickevent/app/quickevent/plugins/Runs/src/runswidget.h index 6e882180b..df6e7fd9b 100644 --- a/quickevent/app/quickevent/plugins/Runs/src/runswidget.h +++ b/quickevent/app/quickevent/plugins/Runs/src/runswidget.h @@ -13,19 +13,11 @@ class QLabel; class QToolButton; namespace qf { -namespace core { -namespace model { -class SqlTableModel; -} -} -namespace gui { -class ForeignKeyComboBox; -} +namespace core { namespace model { class SqlTableModel; } } +namespace gui { class ForeignKeyComboBox; } } -namespace Event { -class EventPlugin; -} +namespace Event { class EventPlugin; } namespace Ui { class RunsWidget; @@ -59,6 +51,7 @@ class RunsWidget : public QFrame void export_results_stage_csv(); public: + static void showEditCompetitorDialog(int competitor_id, QWidget *parent); void editCompetitor(const QVariant &id, int mode) {editCompetitor_helper(id, mode, 0);} void editCompetitors(int mode); void editCompetitorOnPunch(int siid); @@ -71,7 +64,6 @@ class RunsWidget : public QFrame void onDrawRemoveClicked(); void onCbxStageCurrentIndexChanged(); private: - /** * @brief runnersInClubsHistogram * @return list of runs.id for each club sorted by their count, longest list of runners is first diff --git a/quickevent/app/quickevent/src/appversion.h b/quickevent/app/quickevent/src/appversion.h index d62b2834f..a08b78be1 100644 --- a/quickevent/app/quickevent/src/appversion.h +++ b/quickevent/app/quickevent/src/appversion.h @@ -1,4 +1,4 @@ #pragma once -#define APP_VERSION "3.5.8" +#define APP_VERSION "3.6.0" diff --git a/quickevent/app/quickevent/src/main.cpp b/quickevent/app/quickevent/src/main.cpp index 9d6040ced..04d873e64 100644 --- a/quickevent/app/quickevent/src/main.cpp +++ b/quickevent/app/quickevent/src/main.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -197,7 +198,8 @@ int main(int argc, char *argv[]) main_window.show(); emit main_window.applicationLaunched(); - QObject::connect(&app, &Application::qxRecChng, &app, [](const qf::core::sql::QxRecChng &recchng) { +#ifdef QT_DEBUG + QObject::connect(app.qxSql(), &qf::core::sql::QxSql::recChng, &app, [](const qf::core::sql::QxRecChng &recchng) { auto dump_map = [](const QVariantMap &m) { QStringList rows; for (const auto &[k, v] : m.asKeyValueRange()) { @@ -210,6 +212,7 @@ int main(int argc, char *argv[]) << "id:" << recchng.id << "record:" << dump_map(recchng.record); }); +#endif int ret = Application::exec();