From: Steinar H. Gunderson Date: Mon, 1 May 2023 15:09:23 +0000 (+0200) Subject: Show players from the database. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=88759450f6435024a0952783046d10cffffe7f88;p=pkanalytics Show players from the database. --- diff --git a/main.cpp b/main.cpp index b65895d..2e083b8 100644 --- a/main.cpp +++ b/main.cpp @@ -13,6 +13,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include "events.h" +#include "players.h" using namespace std; @@ -30,7 +31,7 @@ string format_timestamp(uint64_t pos) return buf; } -MainWindow::MainWindow(EventsModel *events) : events(events) +MainWindow::MainWindow(EventsModel *events, PlayersModel *players) : events(events), players(players) { player = new QMediaPlayer; //player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv")); @@ -46,6 +47,8 @@ MainWindow::MainWindow(EventsModel *events) : events(events) player->setPosition(events->get_time(current.row())); }); + ui->player_view->setModel(players); + connect(player, &QMediaPlayer::positionChanged, [this](uint64_t pos) { position_changed(pos); }); @@ -191,7 +194,7 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); sqlite3 *db = open_db("ultimate.db"); - MainWindow mainWindow(new EventsModel(db)); + MainWindow mainWindow(new EventsModel(db), new PlayersModel(db)); mainWindow.resize(QSize(1280, 720)); mainWindow.show(); diff --git a/mainwindow.h b/mainwindow.h index a51b88b..079a371 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,13 +6,14 @@ #include "ui_mainwindow.h" class EventsModel; +class PlayersModel; class MainWindow : public QMainWindow { Q_OBJECT public: - MainWindow(EventsModel *events); + MainWindow(EventsModel *events, PlayersModel *players); private: void position_changed(uint64_t pos); @@ -24,6 +25,7 @@ private: Ui::MainWindow *ui; EventsModel *events; + PlayersModel *players; bool seeking = false; bool playing = true; std::optional buffered_seek; diff --git a/mainwindow.ui b/mainwindow.ui index 580fbb4..3c90b22 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -7,7 +7,7 @@ 0 0 1031 - 713 + 754 @@ -128,20 +128,7 @@ - - - - - Qt::Vertical - - - - 20 - 40 - - - - + @@ -420,17 +407,14 @@ - - - Qt::Vertical + + + QAbstractItemView::MultiSelection - - - 20 - 40 - + + QAbstractItemView::SelectRows - + diff --git a/meson.build b/meson.build index a2af988..a466413 100644 --- a/meson.build +++ b/meson.build @@ -9,4 +9,4 @@ qt_files = qt6.preprocess( ui_files: ['mainwindow.ui'], dependencies: qt6deps) -executable('stats', ['main.cpp', 'events.cpp'], qt_files, dependencies: [qt6deps, sqlite3dep]) +executable('stats', ['main.cpp', 'events.cpp', 'players.cpp'], qt_files, dependencies: [qt6deps, sqlite3dep]) diff --git a/players.cpp b/players.cpp new file mode 100644 index 0000000..fce3f7c --- /dev/null +++ b/players.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include "players.h" + +using namespace std; + +string format_timestamp(uint64_t pos); + +PlayersModel::PlayersModel(sqlite3 *db) : db(db) +{ + load_data(); +} + +QVariant PlayersModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) { + return QVariant(); + } + if (orientation == Qt::Horizontal) { + if (section == 0) { + return "#"; + } else if (section == 1) { + return "Name"; + } else { + return QVariant(); + } + } else { + return ""; + } +} + +QVariant PlayersModel::data(const QModelIndex &index, int role) const +{ + if (role != Qt::DisplayRole) { + return QVariant(); + } + if (index.column() == 0) { + return QString::fromUtf8(players[index.row()].number); + } else if (index.column() == 1) { + return QString::fromUtf8(players[index.row()].name); + } + return QVariant(); +} + +void PlayersModel::load_data() +{ + players.clear(); + + // Read the players. + sqlite3_stmt *stmt; + int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player ORDER BY (number+0), number", -1, &stmt, 0); + if (ret != SQLITE_OK) { + fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db)); + abort(); + } + for ( ;; ) { + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + Player p; + p.player_id = sqlite3_column_int(stmt, 0); + p.number = (const char *)sqlite3_column_text(stmt, 1); + p.name = (const char *) sqlite3_column_text(stmt, 2); + players.push_back(p); + } else if (ret == SQLITE_DONE) { + break; + } else { + fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db)); + abort(); + } + } + ret = sqlite3_finalize(stmt); + if (ret != SQLITE_OK) { + fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db)); + abort(); + } +} diff --git a/players.h b/players.h new file mode 100644 index 0000000..f7886f3 --- /dev/null +++ b/players.h @@ -0,0 +1,40 @@ +#ifndef _PLAYERS_H +#define _PLAYERS_H 1 + +#include +#include +#include +#include + +class PlayersModel : public QAbstractTableModel +{ +public: + PlayersModel(sqlite3 *db); + + int rowCount(const QModelIndex &parent) const override + { + return players.size(); + } + int columnCount(const QModelIndex &column) const override + { + return 2; + } + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + QVariant data(const QModelIndex &index, int role) const override; + +// Player get_player(int player_id) const; + +private: + struct Player { + int player_id; + std::string number; + std::string name; + }; + std::vector players; + + sqlite3 *db; + + void load_data(); +}; + +#endif // !defined(_PLAYERS_H)