]> git.sesse.net Git - pkanalytics/commitdiff
Show players from the database.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 May 2023 15:09:23 +0000 (17:09 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 May 2023 15:09:23 +0000 (17:09 +0200)
main.cpp
mainwindow.h
mainwindow.ui
meson.build
players.cpp [new file with mode: 0644]
players.h [new file with mode: 0644]

index b65895d38a278b2edff0bc9b614a231eac5db5d8..2e083b89cca286581b5c4a19ccef331fc4588d72 100644 (file)
--- 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();
 
index a51b88bd3e7ff99f420e4f90884de883275492a7..079a3713b3e8bb873713a4bb55a55aea96d48c63 100644 (file)
@@ -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<uint64_t> buffered_seek;
index 580fbb4eb66d16ea20cfb5f5392228cdcdbdbe44..3c90b220616db544a8a166c1619f96e7b1bb3883 100644 (file)
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>1031</width>
-    <height>713</height>
+    <height>754</height>
    </rect>
   </property>
   <property name="windowTitle">
        </widget>
       </item>
       <item row="0" column="1">
-       <layout class="QVBoxLayout" name="buttons">
-        <item>
-         <spacer name="verticalSpacer_2">
-          <property name="orientation">
-           <enum>Qt::Vertical</enum>
-          </property>
-          <property name="sizeHint" stdset="0">
-           <size>
-            <width>20</width>
-            <height>40</height>
-           </size>
-          </property>
-         </spacer>
-        </item>
+       <layout class="QVBoxLayout" name="buttons" stretch="0,0,0,0,0,0,0,0">
         <item>
          <layout class="QGridLayout" name="player_grid">
           <item row="3" column="0" colspan="2">
          </layout>
         </item>
         <item>
-         <spacer name="verticalSpacer">
-          <property name="orientation">
-           <enum>Qt::Vertical</enum>
+         <widget class="QTableView" name="player_view">
+          <property name="selectionMode">
+           <enum>QAbstractItemView::MultiSelection</enum>
           </property>
-          <property name="sizeHint" stdset="0">
-           <size>
-            <width>20</width>
-            <height>40</height>
-           </size>
+          <property name="selectionBehavior">
+           <enum>QAbstractItemView::SelectRows</enum>
           </property>
-         </spacer>
+         </widget>
         </item>
        </layout>
       </item>
index a2af98863194f1f56caf00b5df23422d23a8a81c..a4664139e8de3bf35b59419e6981745af4d43e17 100644 (file)
@@ -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 (file)
index 0000000..fce3f7c
--- /dev/null
@@ -0,0 +1,78 @@
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <sqlite3.h>
+#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 (file)
index 0000000..f7886f3
--- /dev/null
+++ b/players.h
@@ -0,0 +1,40 @@
+#ifndef _PLAYERS_H
+#define _PLAYERS_H 1
+
+#include <sqlite3.h>
+#include <QAbstractTableModel>
+#include <string>
+#include <vector>
+
+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<Player> players;
+
+       sqlite3 *db;
+
+       void load_data();
+};
+
+#endif  // !defined(_PLAYERS_H)