Skip to content

Commit

Permalink
Implement alert viewer
Browse files Browse the repository at this point in the history
Refs: #66
  • Loading branch information
orontee committed Nov 8, 2023
1 parent d27bbdf commit fa465c9
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 85 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Changed

- Alerts were displayed in a standard dialog. A widget dedicated to
alert viewing has been implemented to improve
readability. [#66](https://github.com/orontee/taranis/issues/66)

### Removed

## [ 1.6.0 ] - 2023-11-01
Expand Down
4 changes: 2 additions & 2 deletions po/cs.po
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ msgid "Credits"
msgstr "Poděkování"

#: src/alerts.cc:43
msgid "Alert"
msgstr "Výstraha"
msgid "ALERT"
msgstr "VÝSTRAHA"

#: src/alerts.cc:52
msgid "Start"
Expand Down
4 changes: 2 additions & 2 deletions po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ msgid "Credits"
msgstr "Crédits"

#: src/alerts.cc:43
msgid "Alert"
msgstr "Alerte"
msgid "ALERT"
msgstr "ALERTE"

#: src/alerts.cc:52
msgid "Start"
Expand Down
4 changes: 2 additions & 2 deletions po/pl.po
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ msgid "Credits"
msgstr "Kredyty"

#: src/alerts.cc:43
msgid "Alert"
msgstr "Alert"
msgid "ALERT"
msgstr "ALERT"

#: src/alerts.cc:52
msgid "Start"
Expand Down
2 changes: 1 addition & 1 deletion po/taranis.pot
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ msgid "Credits"
msgstr ""

#: src/alerts.cc:43
msgid "Alert"
msgid "ALERT"
msgstr ""

#: src/alerts.cc:52
Expand Down
150 changes: 105 additions & 45 deletions src/alerts.cc
Original file line number Diff line number Diff line change
@@ -1,72 +1,132 @@
#include "alerts.h"

#include <iomanip>
#include <memory>
#include <sstream>
#include <string>

#include "events.h"
#include "inkview.h"
#include "model.h"
#include "util.h"

#define OK_BUTTON_INDEX 0
#define NEXT_ALERT_BUTTON_INDEX 1

namespace taranis {

static size_t alert_index{0};
static size_t alert_count{0};

inline bool is_next_alert_button_at(int button_index) {
return (alert_index + 1 < alert_count) ? button_index == 1 : false;
}
void AlertsButton::on_clicked() { this->viewer->open(); }

void AlertsButton::open_dialog_maybe() {
alert_count = this->model->alerts.size();
AlertViewer::AlertViewer(std::shared_ptr<Model> model,
std::shared_ptr<Fonts> fonts)
: Widget{0, 0, ScreenWidth(), ScreenHeight()}, model{model}, fonts{fonts} {}

if (alert_count == 0) {
return;
}
if (alert_index >= alert_count) {
void AlertViewer::open() {
if (this->alert_count() == 0 or this->alert_index >= this->alert_count()) {
this->hide();
return;
}
this->visible = true;
const auto event_handler = GetEventHandler();
SendEvent(event_handler, EVT_SHOW, 0, 0);
}

void AlertViewer::hide() {
this->visible = false;
this->alert_index = 0;

const auto event_handler = GetEventHandler();
SendEvent(event_handler, EVT_SHOW, 0, 0);
}

void AlertViewer::do_paint() {
const auto &alert = this->model->alerts.at(this->alert_index);

this->update_title();
this->update_description(alert);

const auto default_font = this->fonts->get_small_font();
SetFont(default_font.get(), BLACK);

const auto &alert = this->model->alerts.at(alert_index);
const auto content_width =
(this->bounding_box.w - 2 * AlertViewer::horizontal_padding);
const auto title_height =
std::max(TextRectHeight(content_width, this->title.c_str(),
ALIGN_CENTER),
2 * default_font->height);
DrawTextRect(AlertViewer::horizontal_padding, AlertViewer::vertical_padding,
content_width, title_height, this->title.c_str(),
ALIGN_CENTER);

const auto dialog_title =
not alert.event.empty() ? alert.event : GetLangText("Alert");
// 😕 Looks like dialog title isn't displayed at all!
DrawLine(0, title_height, ScreenWidth(), title_height, BLACK);

const auto alert_title_start_y = title_height + AlertViewer::vertical_padding;
auto description_start_y = alert_title_start_y;

std::stringstream alert_text;
if (not alert.event.empty()) {
alert_text << alert.event << std::endl << std::endl;
const auto bold_font = this->fonts->get_small_bold_font();
SetFont(bold_font.get(), BLACK);

const auto alert_title = alert.event;
const auto alert_title_height =
TextRectHeight(content_width, alert_title.c_str(), ALIGN_LEFT);
DrawTextRect(AlertViewer::horizontal_padding, alert_title_start_y,
content_width, alert_title_height, alert_title.c_str(),
ALIGN_LEFT);

SetFont(default_font.get(), BLACK);
description_start_y += alert_title_height + AlertViewer::vertical_padding;
}
alert_text << alert.description << std::endl
<< std::endl
<< GetLangText("Start") << " "
<< format_full_date(alert.start_date) << std::endl
<< GetLangText("Duration") << " "
<< format_duration(alert.start_date, alert.end_date) << std::endl;
const auto description_height =
TextRectHeight(content_width, this->description.c_str(), ALIGN_LEFT);
DrawTextRect(AlertViewer::horizontal_padding, description_start_y,
content_width, description_height, this->description.c_str(),
ALIGN_LEFT);
}

if (not alert.sender.empty()) {
alert_text << GetLangText("Source") << " " << alert.sender;
void AlertViewer::update_title() {
this->title = std::string{GetLangText("ALERT")};
if (this->alert_count() > 1) {
this->title += (" " + std::to_string(this->alert_index + 1) + "/" +
std::to_string(this->alert_count()));
}
}

const auto first_button_text = alert_index + 1 < alert_count
? GetLangText("Next alert")
: GetLangText("Ok");
const auto second_button_text =
alert_index + 1 < alert_count ? GetLangText("Ok") : nullptr;
void AlertViewer::update_description(const Alert &alert) {
std::stringstream description_text;
description_text << alert.description << std::endl
<< std::endl
<< GetLangText("Start") << " "
<< format_full_date(alert.start_date) << std::endl
<< GetLangText("Duration") << " "
<< format_duration(alert.start_date, alert.end_date)
<< std::endl;

Dialog(ICON_WARNING, dialog_title.c_str(), alert_text.str().c_str(),
first_button_text, second_button_text, &alert_dialog_handler);
if (not alert.sender.empty()) {
description_text << GetLangText("Source") << " " << alert.sender;
}
this->description = description_text.str();
}

void AlertsButton::alert_dialog_handler(int button_index) {
if (is_next_alert_button_at(button_index)) {
const auto event_handler = GetEventHandler();
++alert_index;
SendEvent(event_handler, EVT_CUSTOM, CustomEvent::display_alert, 0);
} else {
alert_index = 0;
bool AlertViewer::handle_key_press(int key) {
return (key == IV_KEY_PREV) or (key == IV_KEY_NEXT);
}

bool AlertViewer::handle_key_release(int key) {
if (key == IV_KEY_PREV) {
if (this->alert_index != 0) {
--this->alert_index;
this->paint_and_update_screen();
} else {
this->hide();
}
return true;
} else if (key == IV_KEY_NEXT) {
if (this->alert_index + 1 < this->alert_count()) {
++this->alert_index;
this->paint_and_update_screen();
} else {
this->hide();
}
return true;
}
CloseDialog();
return false;
}

} // namespace taranis
54 changes: 47 additions & 7 deletions src/alerts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <inkview.h>
#include <memory>
#include <vector>

#include "button.h"
#include "fonts.h"
Expand All @@ -10,24 +11,63 @@

namespace taranis {

class AlertViewer;

class AlertsButton : public Button {
public:
AlertsButton(int icon_size, std::shared_ptr<Model> model,
std::shared_ptr<Icons> icons, std::shared_ptr<Fonts> fonts)
: Button{icon_size, icons->get("warning")}, model{model},
font{fonts->get_normal_font()} {}
std::shared_ptr<Icons> icons,
std::shared_ptr<AlertViewer> viewer)
: Button{icon_size, icons->get("warning")}, model{model}, viewer{viewer} {
}

bool is_visible() const override { return not this->model->alerts.empty(); }

void open_dialog_maybe();
protected:
void on_clicked() override;

private:
std::shared_ptr<Model> model;
std::shared_ptr<AlertViewer> viewer;
};

class AlertViewer : public Widget {
public:
AlertViewer(std::shared_ptr<Model> model, std::shared_ptr<Fonts> fonts);

void open();

void hide();

protected:
void on_clicked() override { this->open_dialog_maybe(); }
bool is_modal() const override { return true; }

bool is_visible() const override { return this->visible; }

void do_paint() override;

private:
static constexpr int horizontal_padding{25};
static constexpr int vertical_padding{25};

std::shared_ptr<Model> model;
std::shared_ptr<ifont> font;
std::shared_ptr<Fonts> fonts;

bool visible{false};

size_t alert_index{0};

std::string title;
std::string description;

inline size_t alert_count() const { return this->model->alerts.size(); }

void update_title();

void update_description(const Alert &alert);

bool handle_key_press(int key) override;

static void alert_dialog_handler(int button_index);
bool handle_key_release(int key) override;
};
} // namespace taranis
2 changes: 1 addition & 1 deletion src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ int App::handle_custom_event(int param_one, int param_two) {
const std::string location{raw_location->data()};
this->search_location(location);
return 1;
} else if (param_one == CustomEvent::display_alert) {
} else if (param_one == CustomEvent::open_alert_viewer) {
if (this->ui) {
this->ui->display_alert();
return 1;
Expand Down
4 changes: 2 additions & 2 deletions src/events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ std::string taranis::format_custom_event_param(int param) {
if (param == CustomEvent::change_location) {
return "change_location";
}
if (param == CustomEvent::display_alert) {
return "display_alert";
if (param == CustomEvent::open_alert_viewer) {
return "open_alert_viewer";
}
if (param == CustomEvent::open_config_editor) {
return "open_config_editor";
Expand Down
2 changes: 1 addition & 1 deletion src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum CustomEvent {
change_daily_forecast_display,
search_location,
change_location,
display_alert,
open_alert_viewer,
open_config_editor,
refresh_data,
select_location_from_history,
Expand Down
11 changes: 2 additions & 9 deletions src/hourlyforecastbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void HourlyForecastBox::increase_forecast_offset() {
this->forecast_offset = updated_forecast_offset;
BOOST_LOG_TRIVIAL(debug)
<< "Forecast offset increased to " << this->forecast_offset;
this->draw_and_update();
this->paint_and_update_screen();
} else {
this->request_change_display_forecast_display();
}
Expand All @@ -115,19 +115,12 @@ void HourlyForecastBox::decrease_forecast_offset() {
this->forecast_offset = updated_forecast_offset;
BOOST_LOG_TRIVIAL(debug)
<< "Forecast offset decreased to " << this->forecast_offset;
this->draw_and_update();
this->paint_and_update_screen();
} else {
this->request_change_display_forecast_display();
}
}

void HourlyForecastBox::draw_and_update() {
this->paint();

PartialUpdate(this->bounding_box.x, this->bounding_box.y,
this->bounding_box.w, this->bounding_box.h);
}

std::tuple<std::string, int>
HourlyForecastBox::get_date_label_properties(size_t bar_index) const {
const int forecast_index = this->forecast_offset + bar_index;
Expand Down
Loading

0 comments on commit fa465c9

Please sign in to comment.