Skip to content

Commit cf52f7a

Browse files
Merge pull request #56 from Reim-developer/dev
Implement load image from C++ header for show app icon & added Python helper script
2 parents a5d8f7e + b387077 commit cf52f7a

5 files changed

Lines changed: 122 additions & 0 deletions

File tree

resources/1.png

5.5 KB
Loading

src/front_end/include/icon_bytes.hxx

Lines changed: 13 additions & 0 deletions
Large diffs are not rendered by default.

src/front_end/main_window.cxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#include "include/main_window.hxx"
22

33
#include <qgridlayout.h>
4+
#include <qicon.h>
45
#include <qmainwindow.h>
56
#include <qwidget.h>
67

78
#include <memory>
89

10+
#include "../front_end_utils/include/utils.hxx"
911
#include "include/about_widget.hxx"
12+
#include "include/icon_bytes.hxx"
1013
#include "include/main_window_preload.hxx"
1114
#include "include/setting_widget.hxx"
1215
#include "include/table_widget.hxx"
1316

1417
using Lazyboard::front_end::MainWindow;
18+
using Lazyboard::front_end_utils::image_from_bytes;
1519
using std::make_unique;
1620

1721
using Self = MainWindow;
@@ -31,6 +35,7 @@ Self::MainWindow() {
3135
Self *Self::init_main_window() {
3236
main_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
3337
main_window->setWindowTitle("Lazyboard");
38+
main_window->setWindowIcon(image_from_bytes(image_bytes));
3439
main_window_preload->create_default_config(main_window.get());
3540
main_window_preload->read_if_exists_config(main_window.get());
3641
this->front_end_show();

src/front_end_utils/include/utils.hxx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
#define FRONT_END_UTILS_HXX
33

44
#include <qcolor.h>
5+
#include <qicon.h>
6+
#include <qpixmap.h>
7+
#include <qstringview.h>
58

9+
#include <initializer_list>
610
#include <type_traits>
11+
#include <vector>
712

13+
#include "error_types.hxx"
14+
15+
using std::initializer_list;
816
using std::is_same_v;
917
using std::stringstream;
18+
using std::vector;
19+
1020
namespace Lazyboard::front_end_utils {
1121

1222
template <typename T>
@@ -27,6 +37,22 @@ inline constexpr bool is_valid_hex_color(const Args&... args) noexcept {
2737
...);
2838
}
2939

40+
inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) {
41+
QByteArray bytes_array;
42+
bytes_array.reserve(static_cast<int>(data.size()));
43+
44+
for (auto byte : data) {
45+
bytes_array.append(static_cast<char>(byte));
46+
}
47+
48+
QPixmap pixmap;
49+
if (pixmap.loadFromData(bytes_array)) {
50+
return QIcon(pixmap);
51+
}
52+
53+
return QIcon();
54+
}
55+
3056
} // namespace Lazyboard::front_end_utils
3157

3258
#endif

tools/helper.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python3
2+
3+
import argparse
4+
import os
5+
import sys
6+
7+
RED = "\033[1;31m"
8+
WHITE = "\033[0m"
9+
GREEN = "\033[0;32m"
10+
11+
stderr = sys.stderr.write
12+
abort = sys.exit
13+
14+
parser = argparse.ArgumentParser(description = "Helper Tools For Lazyboard Project")
15+
sub_parsers = parser.add_subparsers(dest = "command")
16+
17+
def gen_to_hex(data: bytes) -> str:
18+
return ", ".join(f"0x{b:02x}" for b in data)
19+
20+
def file_name(file_str: str) -> str:
21+
return os.path.basename(file_str)
22+
23+
def file_extension(file: str) -> str:
24+
_, extension = os.path.splitext(file)
25+
26+
return extension
27+
28+
def gen_res(args: argparse.Namespace) -> None:
29+
file_path = args.path
30+
cxx_output = args.output
31+
32+
if not os.path.exists(file_path):
33+
stderr(f"{RED}error:{WHITE} Resource file path {file_path} not found\n")
34+
abort(1)
35+
36+
if not file_extension(cxx_output) == ".cxx":
37+
cxx_output += ".hxx"
38+
39+
try:
40+
with open(file = file_path, mode = "rb") as image_file:
41+
read_bytes = gen_to_hex(image_file.read())
42+
43+
with open(file = cxx_output, mode = "w", encoding = "utf-8") as output:
44+
header_name = file_name(cxx_output).upper().replace(".", "_")
45+
46+
output.write(f"#ifndef {header_name}\n")
47+
output.write(f"#define {header_name}\n\n")
48+
output.write(f"#include <stdint.h>\n\n")
49+
output.write("#include <initializer_list>\n\n")
50+
output.write("using std::initializer_list;\n\n")
51+
output.write(f"const initializer_list<uint8_t> image_bytes = {{\n")
52+
output.write(f" " + read_bytes + "\n")
53+
output.write("};\n")
54+
55+
output.write(f"#endif // {header_name}")
56+
57+
print(f"{GREEN}ok:{WHITE} Generated successfully, can found header file at: {cxx_output}")
58+
59+
except Exception as error:
60+
stderr(f"{RED}error:{WHITE} Exception error: {error}")
61+
62+
def gen_resources_command() -> None:
63+
gen_res_cmd = sub_parsers.add_parser("gen-res", help = "Gen resource to C++ header")
64+
gen_res_cmd.add_argument("--path", help = "Resource file path")
65+
gen_res_cmd.add_argument("--output", help = "C++ source output")
66+
gen_res_cmd.set_defaults(func = gen_res)
67+
68+
def main() -> None:
69+
gen_resources_command()
70+
arguments = parser.parse_args()
71+
72+
if hasattr(arguments, "func"):
73+
arguments.func(arguments)
74+
75+
else:
76+
parser.print_help()
77+
78+
main()

0 commit comments

Comments
 (0)