From 1e0d452f57c3761983665d1af292e09bb0109f81 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Fri, 2 Sep 2016 22:22:30 -0700 Subject: [PATCH] RecentEntriesView: Generalize draw_header() implementations. --- firmware/application/ais_app.cpp | 30 ++++--------------- firmware/application/ert_app.cpp | 34 ++++++---------------- firmware/application/recent_entries.hpp | 27 +++++++++++++++++- firmware/application/tpms_app.cpp | 38 +++++++------------------ 4 files changed, 50 insertions(+), 79 deletions(-) diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index 1b04bc18..3d7ad098 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -185,30 +185,6 @@ void AISRecentEntry::update(const ais::Packet& packet) { namespace ui { -static const std::array, 2> ais_columns { { - { "MMSI", 9 }, - { "Name/Call", 20 }, -} }; - -template<> -void RecentEntriesView::draw_header( - const Rect& target_rect, - Painter& painter, - const Style& style -) { - auto x = 0; - for(const auto& column : ais_columns) { - const auto width = column.second; - auto text = column.first; - if( width > text.length() ) { - text.append(width - text.length(), ' '); - } - - painter.draw_string({ x, target_rect.pos.y }, style, text); - x += (width * 8) + 8; - } -} - template<> void RecentEntriesView::draw( const Entry& entry, @@ -303,6 +279,12 @@ AISAppView::AISAppView(NavigationView&) { &recent_entry_detail_view, } }); + const std::array columns { { + { "MMSI", 9 }, + { "Name/Call", 20 }, + } }; + recent_entries_view.set_columns(columns); + recent_entry_detail_view.hidden(true); target_frequency_ = initial_target_frequency; diff --git a/firmware/application/ert_app.cpp b/firmware/application/ert_app.cpp index 5bed3782..fac65320 100644 --- a/firmware/application/ert_app.cpp +++ b/firmware/application/ert_app.cpp @@ -75,32 +75,6 @@ void ERTRecentEntry::update(const ert::Packet& packet) { namespace ui { -static const std::array, 4> ert_columns { { - { "ID", 10 }, - { "Tp", 2 }, - { "Consumpt", 10 }, - { "Cnt", 3 }, -} }; - -template<> -void RecentEntriesView::draw_header( - const Rect& target_rect, - Painter& painter, - const Style& style -) { - auto x = 0; - for(const auto& column : ert_columns) { - const auto width = column.second; - auto text = column.first; - if( width > text.length() ) { - text.append(width - text.length(), ' '); - } - - painter.draw_string({ x, target_rect.pos.y }, style, text); - x += (width * 8) + 8; - } -} - template<> void RecentEntriesView::draw( const Entry& entry, @@ -134,6 +108,14 @@ ERTAppView::ERTAppView(NavigationView&) { &recent_entries_view, } }); + const std::array columns { { + { "ID", 10 }, + { "Tp", 2 }, + { "Consumpt", 10 }, + { "Cnt", 3 }, + } }; + recent_entries_view.set_columns(columns); + radio::enable({ initial_target_frequency, sampling_rate, diff --git a/firmware/application/recent_entries.hpp b/firmware/application/recent_entries.hpp index 84afb868..30bf4a8d 100644 --- a/firmware/application/recent_entries.hpp +++ b/firmware/application/recent_entries.hpp @@ -118,6 +118,8 @@ private: namespace ui { +using RecentEntriesColumn = std::pair; + template class RecentEntriesView : public View { public: @@ -132,6 +134,16 @@ public: set_focusable(true); } + template + void set_columns( + const std::array& columns + ) { + _columns.clear(); + for(const auto& column : columns) { + _columns.emplace_back(column); + } + } + void paint(Painter& painter) override { const auto r = screen_rect(); const auto& s = style(); @@ -192,6 +204,7 @@ public: private: Entries& recent; + std::vector> _columns; using EntryKey = typename Entry::Key; EntryKey selected_key = Entry::invalid_key; @@ -226,7 +239,19 @@ private: const Rect& target_rect, Painter& painter, const Style& style - ); + ) { + auto x = 0; + for(const auto& column : _columns) { + const auto width = column.second; + auto text = column.first; + if( width > text.length() ) { + text.append(width - text.length(), ' '); + } + + painter.draw_string({ x, target_rect.pos.y }, style, text); + x += (width * 8) + 8; + } + } void draw( const Entry& entry, diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index 2a13e2f2..25ad2420 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -95,34 +95,6 @@ void TPMSRecentEntry::update(const tpms::Reading& reading) { namespace ui { -static const std::array, 6> tpms_columns { { - { "Tp", 2 }, - { "ID", 8 }, - { "kPa", 3 }, - { "C", 3 }, - { "Cnt", 3 }, - { "Fl", 2 }, -} }; - -template<> -void RecentEntriesView::draw_header( - const Rect& target_rect, - Painter& painter, - const Style& style -) { - auto x = 0; - for(const auto& column : tpms_columns) { - const auto width = column.second; - auto text = column.first; - if( width > text.length() ) { - text.append(width - text.length(), ' '); - } - - painter.draw_string({ x, target_rect.pos.y }, style, text); - x += (width * 8) + 8; - } -} - template<> void RecentEntriesView::draw( const Entry& entry, @@ -176,6 +148,16 @@ TPMSAppView::TPMSAppView(NavigationView&) { &recent_entries_view, } }); + const std::array columns { { + { "Tp", 2 }, + { "ID", 8 }, + { "kPa", 3 }, + { "C", 3 }, + { "Cnt", 3 }, + { "Fl", 2 }, + } }; + recent_entries_view.set_columns(columns); + radio::enable({ tuning_frequency(), sampling_rate,