Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added resources/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/front_end/include/icon_bytes.hxx

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/front_end/main_window.cxx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#include "include/main_window.hxx"

#include <qgridlayout.h>
#include <qicon.h>
#include <qmainwindow.h>
#include <qwidget.h>

#include <memory>

#include "../front_end_utils/include/utils.hxx"
#include "include/about_widget.hxx"
#include "include/icon_bytes.hxx"
#include "include/main_window_preload.hxx"
#include "include/setting_widget.hxx"
#include "include/table_widget.hxx"

using Lazyboard::front_end::MainWindow;
using Lazyboard::front_end_utils::image_from_bytes;
using std::make_unique;

using Self = MainWindow;
Expand All @@ -31,6 +35,7 @@ Self::MainWindow() {
Self *Self::init_main_window() {
main_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
main_window->setWindowTitle("Lazyboard");
main_window->setWindowIcon(image_from_bytes(image_bytes));
main_window_preload->create_default_config(main_window.get());
main_window_preload->read_if_exists_config(main_window.get());
this->front_end_show();
Expand Down
26 changes: 26 additions & 0 deletions src/front_end_utils/include/utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
#define FRONT_END_UTILS_HXX

#include <qcolor.h>
#include <qicon.h>
#include <qpixmap.h>
#include <qstringview.h>

#include <initializer_list>
#include <type_traits>
#include <vector>

#include "error_types.hxx"

using std::initializer_list;
using std::is_same_v;
using std::stringstream;
using std::vector;

namespace Lazyboard::front_end_utils {

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

inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) {
QByteArray bytes_array;
bytes_array.reserve(static_cast<int>(data.size()));

for (auto byte : data) {
bytes_array.append(static_cast<char>(byte));
}

QPixmap pixmap;
if (pixmap.loadFromData(bytes_array)) {
return QIcon(pixmap);
}

return QIcon();
}

} // namespace Lazyboard::front_end_utils

#endif
78 changes: 78 additions & 0 deletions tools/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/python3

import argparse
import os
import sys

RED = "\033[1;31m"
WHITE = "\033[0m"
GREEN = "\033[0;32m"

stderr = sys.stderr.write
abort = sys.exit

parser = argparse.ArgumentParser(description = "Helper Tools For Lazyboard Project")
sub_parsers = parser.add_subparsers(dest = "command")

def gen_to_hex(data: bytes) -> str:
return ", ".join(f"0x{b:02x}" for b in data)

def file_name(file_str: str) -> str:
return os.path.basename(file_str)

def file_extension(file: str) -> str:
_, extension = os.path.splitext(file)

return extension

def gen_res(args: argparse.Namespace) -> None:
file_path = args.path
cxx_output = args.output

if not os.path.exists(file_path):
stderr(f"{RED}error:{WHITE} Resource file path {file_path} not found\n")
abort(1)

if not file_extension(cxx_output) == ".cxx":
cxx_output += ".hxx"

try:
with open(file = file_path, mode = "rb") as image_file:
read_bytes = gen_to_hex(image_file.read())

with open(file = cxx_output, mode = "w", encoding = "utf-8") as output:
header_name = file_name(cxx_output).upper().replace(".", "_")

output.write(f"#ifndef {header_name}\n")
output.write(f"#define {header_name}\n\n")
output.write(f"#include <stdint.h>\n\n")
output.write("#include <initializer_list>\n\n")
output.write("using std::initializer_list;\n\n")
output.write(f"const initializer_list<uint8_t> image_bytes = {{\n")
output.write(f" " + read_bytes + "\n")
output.write("};\n")

output.write(f"#endif // {header_name}")

print(f"{GREEN}ok:{WHITE} Generated successfully, can found header file at: {cxx_output}")

except Exception as error:
stderr(f"{RED}error:{WHITE} Exception error: {error}")

def gen_resources_command() -> None:
gen_res_cmd = sub_parsers.add_parser("gen-res", help = "Gen resource to C++ header")
gen_res_cmd.add_argument("--path", help = "Resource file path")
gen_res_cmd.add_argument("--output", help = "C++ source output")
gen_res_cmd.set_defaults(func = gen_res)

def main() -> None:
gen_resources_command()
arguments = parser.parse_args()

if hasattr(arguments, "func"):
arguments.func(arguments)

else:
parser.print_help()

main()