Skip to content

Commit

Permalink
Refactor introducing stack widget
Browse files Browse the repository at this point in the history
Refs: #77
  • Loading branch information
orontee committed Nov 27, 2023
1 parent 638f349 commit 9a4ac54
Show file tree
Hide file tree
Showing 16 changed files with 448 additions and 185 deletions.
14 changes: 7 additions & 7 deletions src/alerts.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class AlertViewer : public ModalWidget {

void do_paint() override;

bool handle_key_press(int key) override;

bool handle_key_release(int key) override;

int handle_pointer_event(int event_type, int pointer_pos_x,
int pointer_pos_y) override;

private:
static constexpr int horizontal_padding{25};
static constexpr int vertical_padding{25};
Expand Down Expand Up @@ -77,12 +84,5 @@ class AlertViewer : public ModalWidget {
void display_previous_alert_maybe();

void display_next_alert_maybe();

bool handle_key_press(int key) override;

bool handle_key_release(int key) override;

int handle_pointer_event(int event_type, int pointer_pos_x,
int pointer_pos_y) override;
};
} // namespace taranis
4 changes: 2 additions & 2 deletions src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ void App::setup() {
}
this->set_task_parameters();

this->ui = std::make_unique<Ui>(this->config, this->model);

this->application_state = std::make_unique<ApplicationState>(this->model);
this->application_state->restore();

Expand All @@ -105,6 +103,8 @@ void App::setup() {
<< "Current location restored from application state";
}
this->load_config();

this->ui = std::make_unique<Ui>(this->config, this->model);
}

void App::show() {
Expand Down
2 changes: 1 addition & 1 deletion src/events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ std::string taranis::format_custom_event_param(int param) {
if (param == CustomEvent::select_location_from_list) {
return "select_location_from_list";
}

if (param == CustomEvent::toggle_current_location_favorite) {
return "toggle_current_location_favorite";
}

if (param == CustomEvent::model_daily_forecast_display_changed) {
return "model_daily_forecast_display_changed";
}
Expand Down
139 changes: 139 additions & 0 deletions src/forecast.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "forecast.h"

#include "dailyforecastbox.h"
#include "events.h"

namespace taranis {

ForecastStack::ForecastStack(int pos_x, int pos_y, int width, int height,
std::shared_ptr<Model> model,
std::shared_ptr<Icons> icons,
std::shared_ptr<Fonts> fonts)
: Stack{}, model{model} {
this->hourly_forecast_box = std::make_shared<HourlyForecastBox>(
pos_x, pos_y, width, height, model, icons, fonts);

auto daily_forecast_box = std::make_shared<DailyForecastBox>(
pos_x, pos_y, width, height, model, icons, fonts);

this->add("hourly-forecast", this->hourly_forecast_box);
this->add("daily-forecast", daily_forecast_box);

this->swipe_detector.set_bounding_box(this->get_bounding_box());

auto forecast_to_display = this->model->display_daily_forecast
? "daily-forecast"
: "hourly-forecast";
this->set_current_widget_maybe(forecast_to_display);
}

int ForecastStack::handle_pointer_event(int event_type, int pointer_pos_x,
int pointer_pos_y) {
if (this->handle_possible_swipe(event_type, pointer_pos_x, pointer_pos_y)) {
return 1;
}
return Stack::handle_pointer_event(event_type, pointer_pos_x, pointer_pos_y);
}

bool ForecastStack::handle_possible_swipe(int event_type, int pointer_pos_x,
int pointer_pos_y) {
const auto swipe = this->swipe_detector.guess_event_swipe_type(
event_type, pointer_pos_x, pointer_pos_y);
if (swipe != SwipeType::no_swipe) {
const auto current_forecast_name = this->current_widget_name();
if (current_forecast_name == "daily-forecast") {
if (swipe == SwipeType::left_swipe) {
this->hourly_forecast_box->set_min_forecast_offset();
} else if (swipe == SwipeType::right_swipe) {
this->hourly_forecast_box->set_max_forecast_offset();
}
this->request_forecast_switch();
return true;
} else if (current_forecast_name == "hourly-forecast") {
if (swipe == SwipeType::left_swipe) {
if (this->hourly_forecast_box->get_forecast_offset() ==
this->hourly_forecast_box->get_max_forecast_offset()) {
this->request_forecast_switch();
} else {
this->hourly_forecast_box->increase_forecast_offset();
}
return true;
} else if (swipe == SwipeType::right_swipe) {
if (this->hourly_forecast_box->get_forecast_offset() ==
this->hourly_forecast_box->get_min_forecast_offset()) {
this->request_forecast_switch();
} else {
this->hourly_forecast_box->decrease_forecast_offset();
}
return true;
}
}
}
return false;
}

bool ForecastStack::handle_key_press(int key) {
this->skip_next_key_release = false;
return (key == IV_KEY_PREV or key == IV_KEY_NEXT);
}

bool ForecastStack::handle_key_repeat(int key) {
const auto current_forecast_name = this->current_widget_name();
if (current_forecast_name == "hourly-forecast") {
this->prepare_child_switch_by_key(key);
this->request_forecast_switch();
}
return true;
}

bool ForecastStack::handle_key_release(int key) {
const auto current_forecast_name = this->current_widget_name();
if (current_forecast_name == "hourly-forecast") {
if ((key == IV_KEY_PREV and
this->hourly_forecast_box->get_forecast_offset() ==
this->hourly_forecast_box->get_min_forecast_offset()) or
(key == IV_KEY_NEXT and
this->hourly_forecast_box->get_forecast_offset() ==
this->hourly_forecast_box->get_max_forecast_offset())) {
this->prepare_child_switch_by_key(key);
this->request_forecast_switch();
} else {
this->hourly_forecast_box->handle_key_release(key);
}
} else {
if (not this->skip_next_key_release) {
this->prepare_child_switch_by_key(key);
this->request_forecast_switch();
}
}
return true;
;
}

void ForecastStack::prepare_child_switch_by_key(int key) {
const auto current_forecast_name = this->current_widget_name();
if (current_forecast_name == "daily-forecast") {
if (key == IV_KEY_PREV) {
this->hourly_forecast_box->set_max_forecast_offset();
} else if (key == IV_KEY_NEXT) {
this->hourly_forecast_box->set_min_forecast_offset();
}
}
this->skip_next_key_release = true;
// since this switch was triggered by a "key press" event
}

void ForecastStack::request_forecast_switch() const {
const auto event_handler = GetEventHandler();
SendEvent(event_handler, EVT_CUSTOM,
CustomEvent::change_daily_forecast_display, 0);
}

void ForecastStack::switch_forecast() {
auto forecast_to_display = this->model->display_daily_forecast
? "daily-forecast"
: "hourly-forecast";
this->set_current_widget(forecast_to_display);
}

} // namespace taranis
46 changes: 46 additions & 0 deletions src/forecast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <memory>

#include "fonts.h"
#include "hourlyforecastbox.h"
#include "icons.h"
#include "model.h"
#include "stack.h"
#include "swipe.h"

namespace taranis {
struct ForecastStack : public Stack {
ForecastStack(int pos_x, int pos_y, int width, int height,
std::shared_ptr<Model> model, std::shared_ptr<Icons> icons,
std::shared_ptr<Fonts> fonts);

void switch_forecast();

int handle_pointer_event(int event_type, int pointer_pos_x,
int pointer_pos_y) override;

bool handle_key_press(int key) override;

bool handle_key_repeat(int key) override;

bool handle_key_release(int key) override;

private:
std::shared_ptr<Model> model;

std::shared_ptr<HourlyForecastBox> hourly_forecast_box;
// to call specific methods to reset forecast offset

bool skip_next_key_release{false};

SwipeDetector swipe_detector;

void prepare_child_switch_by_key(int key);

void request_forecast_switch() const;

bool handle_possible_swipe(int event_type, int pointer_pos_x,
int pointer_pos_y);
};
} // namespace taranis
32 changes: 11 additions & 21 deletions src/hourlyforecastbox.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "hourlyforecastbox.h"

#include "opencv2/imgproc/imgproc.hpp"
#include <boost/log/trivial.hpp>
#include <cmath>
#include <cstdlib>
Expand All @@ -12,8 +13,6 @@
#include <opencv2/opencv.hpp>
#include <sstream>

#include "events.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "units.h"
#include "util.h"

Expand Down Expand Up @@ -67,14 +66,6 @@ bool HourlyForecastBox::handle_key_press(int key) {
return (key == IV_KEY_PREV or key == IV_KEY_NEXT);
}

bool HourlyForecastBox::handle_key_repeat(int key) {
if (key == IV_KEY_PREV or key == IV_KEY_NEXT) {
this->request_change_daily_forecast_display();
return true;
}
return false;
}

bool HourlyForecastBox::handle_key_release(int key) {
if (key == IV_KEY_PREV) {
this->decrease_forecast_offset();
Expand All @@ -86,6 +77,16 @@ bool HourlyForecastBox::handle_key_release(int key) {
return false;
}

size_t HourlyForecastBox::get_forecast_offset() const {
return this->forecast_offset;
}

size_t HourlyForecastBox::get_min_forecast_offset() const { return 0; }

size_t HourlyForecastBox::get_max_forecast_offset() const {
return this->model->hourly_forecast.size() - HourlyForecastBox::visible_bars;
}

void HourlyForecastBox::set_min_forecast_offset() {
if (this->forecast_offset == 0) {
return;
Expand Down Expand Up @@ -117,8 +118,6 @@ void HourlyForecastBox::increase_forecast_offset() {
BOOST_LOG_TRIVIAL(debug)
<< "Forecast offset increased to " << this->forecast_offset;
this->paint_and_update_screen();
} else {
this->request_change_daily_forecast_display();
}
}

Expand All @@ -136,8 +135,6 @@ void HourlyForecastBox::decrease_forecast_offset() {
BOOST_LOG_TRIVIAL(debug)
<< "Forecast offset decreased to " << this->forecast_offset;
this->paint_and_update_screen();
} else {
this->request_change_daily_forecast_display();
}
}

Expand Down Expand Up @@ -375,13 +372,6 @@ void HourlyForecastBox::draw_precipitation_histogram() const {
}
}

void HourlyForecastBox::request_change_daily_forecast_display() {
BOOST_LOG_TRIVIAL(debug) << "Requesting a change of forecast display";
const auto event_handler = GetEventHandler();
SendEvent(event_handler, EVT_CUSTOM,
CustomEvent::change_daily_forecast_display, 0);
}

std::map<int, std::vector<unsigned char>> rotated_icons;

const ibitmap *HourlyForecastBox::rotate_direction_icon(int degree) {
Expand Down
18 changes: 10 additions & 8 deletions src/hourlyforecastbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class HourlyForecastBox : public Widget {
std::shared_ptr<Model> model, std::shared_ptr<Icons> icons,
std::shared_ptr<Fonts> fonts);

size_t get_forecast_offset() const;

size_t get_min_forecast_offset() const;

size_t get_max_forecast_offset() const;

void set_min_forecast_offset();

void set_max_forecast_offset();
Expand All @@ -27,6 +33,10 @@ class HourlyForecastBox : public Widget {

void do_paint() override;

bool handle_key_press(int key) override;

bool handle_key_release(int key) override;

private:
static constexpr int vertical_padding{25};
static constexpr int icon_size{100};
Expand Down Expand Up @@ -60,12 +70,6 @@ class HourlyForecastBox : public Widget {

size_t forecast_offset{0};

bool handle_key_press(int key) override;

bool handle_key_repeat(int key) override;

bool handle_key_release(int key) override;

std::tuple<std::string, int>
get_date_label_properties(size_t bar_index) const;

Expand All @@ -79,8 +83,6 @@ class HourlyForecastBox : public Widget {

void draw_precipitation_histogram() const;

void request_change_daily_forecast_display();

const ibitmap *rotate_direction_icon(int degree);
};

Expand Down
Loading

0 comments on commit 9a4ac54

Please sign in to comment.