-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs: #66
- Loading branch information
Showing
13 changed files
with
186 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.