forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
69 lines (59 loc) · 2.52 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
#include "printerdemo.h"
#include <ctime>
struct InkLevelModel : slint::Model<InkLevel>
{
size_t row_count() const override { return m_data.size(); }
std::optional<InkLevel> row_data(size_t i) const override
{
if (i < row_count())
return { m_data[i] };
return {};
}
std::vector<InkLevel> m_data = { { slint::Color::from_rgb_uint8(255, 255, 0), 0.9 },
{ slint::Color::from_rgb_uint8(0, 255, 255), 0.5 },
{ slint::Color::from_rgb_uint8(255, 0, 255), 0.8 },
{ slint::Color::from_rgb_uint8(0, 0, 0), 0.1 } };
};
int main()
{
auto printer_demo = MainWindow::create();
printer_demo->set_ink_levels(std::make_shared<InkLevelModel>());
printer_demo->on_quit([] { std::exit(0); });
auto printer_queue = std::make_shared<slint::VectorModel<PrinterQueueItem>>();
auto default_queue = printer_demo->global<PrinterQueue>().get_printer_queue();
for (int i = 0; i < default_queue->row_count(); ++i) {
printer_queue->push_back(*default_queue->row_data(i));
}
printer_demo->global<PrinterQueue>().set_printer_queue(printer_queue);
printer_demo->global<PrinterQueue>().on_start_job([=](slint::SharedString name) {
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char time_buf[100] = { 0 };
std::strftime(time_buf, sizeof(time_buf), "%H:%M:%S %d/%m/%Y", std::localtime(&now));
PrinterQueueItem item;
item.status = "WAITING...";
item.progress = 0;
item.title = std::move(name);
item.owner = "[email protected]";
item.pages = 1;
item.size = "100kB";
item.submission_date = time_buf;
printer_queue->push_back(item);
});
printer_demo->global<PrinterQueue>().on_cancel_job(
[=](int index) { printer_queue->erase(int(index)); });
slint::Timer printer_queue_progress_timer(std::chrono::seconds(1), [=]() {
if (printer_queue->row_count() > 0) {
auto top_item = *printer_queue->row_data(0);
top_item.progress += 1;
if (top_item.progress > 100) {
printer_queue->erase(0);
} else {
top_item.status = "PRINTING";
printer_queue->set_row_data(0, top_item);
}
}
});
printer_demo->run();
}