]> git.sesse.net Git - pkanalytics/commitdiff
Make the events view scroll the current event into view when playing.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 May 2023 21:02:20 +0000 (23:02 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 May 2023 21:02:26 +0000 (23:02 +0200)
events.cpp
events.h
main.cpp

index 8ad0009d1560f0b78f8097cfd94013ee9cd8d178..24fd649e7f3adfeaea19f41a27e393858ba1df99 100644 (file)
@@ -225,6 +225,19 @@ void EventsModel::set_event_type(unsigned pos, const string &type)
        }
 }
 
+unsigned EventsModel::get_last_event_pos(uint64_t t) const
+{
+       // upper_bound() gives first where e.t > t,
+       // and the one before that is the one we want.
+       auto it = upper_bound(events.begin(), events.end(), t,
+               [](uint64_t t, const Event &e) { return t < e.t; });
+       if (it == events.begin()) {
+               return 0;
+       } else {
+               return distance(events.begin(), it - 1);
+       }
+}
+
 EventsModel::Status EventsModel::get_status_at(uint64_t t)
 {
        Status s;
index b33f0b6e255e5e613d535b5e084247717a2afeac..9804836aaabde417e07ac69fb4d4c52924276e04 100644 (file)
--- a/events.h
+++ b/events.h
@@ -29,6 +29,10 @@ public:
        void delete_event(unsigned row);
        void set_event_type(unsigned row, const std::string &type);
        uint64_t get_time(unsigned row) { return events[row].t; }
+       unsigned get_last_event_pos(uint64_t t) const;  // Last event that happened at or before t.
+       QModelIndex get_last_event_qt(uint64_t t) const {
+               return createIndex(get_last_event_pos(t), 0);
+       }
        std::optional<int> get_player_id(unsigned row) { return events[row].player_id; }
 
        struct Status {
index 104e1e5875dc23e20efe217ea17b5ee5cad69d95..faac256aa15818cbccd280ca6fd991342b29208b 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -161,6 +161,10 @@ void MainWindow::position_changed(uint64_t pos)
        if (!playing) {
                video->pause();  // We only played to get a picture.
        }
+       if (playing) {
+               QModelIndex row = events->get_last_event_qt(video->position());
+               ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
+       }
        update_ui_from_time(pos);
 }