diff --git a/CMakeLists.txt b/CMakeLists.txt index b881ad0..35e8370 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,8 @@ set(META_ADD_DEFAULT_CPP_UNIT_TEST_APPLICATION OFF) project(${META_PROJECT_NAME}) # add project files -set(HEADER_FILES renderer.h icon.h iconfwd.h) -set(SRC_FILES renderer.cpp) +set(HEADER_FILES renderer.h icon.h iconfwd.h utils.h) +set(SRC_FILES renderer.cpp utils.cpp) set(QT_TESTS renderer) set(DOC_FILES README.md) @@ -68,12 +68,13 @@ list(APPEND RES_FILES "${RES_FILE_PATH}") find_program(PERL perl) set(GENERATED_HEADERS_PATH "${CMAKE_CURRENT_BINARY_DIR}/headers") set(ICONS_HEADER_FILE "${GENERATED_HEADERS_PATH}/private/icons.h") +set(ID_MAPPING_HEADER_FILE "${GENERATED_HEADERS_PATH}/private/idmapping.h") file(MAKE_DIRECTORY "${GENERATED_HEADERS_PATH}/private") -list(APPEND SRC_FILES "${ICONS_HEADER_FILE}") +list(APPEND SRC_FILES "${ICONS_HEADER_FILE}" "${ID_MAPPING_HEADER_FILE}") add_custom_command( - OUTPUT "${ICONS_HEADER_FILE}" + OUTPUT "${ICONS_HEADER_FILE}" "${ID_MAPPING_HEADER_FILE}" COMMAND "${PERL}" "${CMAKE_CURRENT_SOURCE_DIR}/scripts/yaml2enum.pl" "${FORK_AWESOME_ICON_DEFINITIONS}" - "${ICONS_HEADER_FILE}" + "${ICONS_HEADER_FILE}" "${ID_MAPPING_HEADER_FILE}" DEPENDS "${FORK_AWESOME_ICON_DEFINITIONS}" COMMENT "Icon header") diff --git a/icon.h b/icon.h index 989f893..61fd585 100644 --- a/icon.h +++ b/icon.h @@ -6,6 +6,7 @@ namespace QtForkAwesome { enum class Icon : IconBaseType { + Invalid, /**< invalid, used by QtForkAwesome::iconFromId() to indicate failure */ #include "private/icons.h" }; diff --git a/renderer.h b/renderer.h index e810c83..f47c73e 100644 --- a/renderer.h +++ b/renderer.h @@ -14,6 +14,7 @@ QT_FORWARD_DECLARE_CLASS(QPainter) QT_FORWARD_DECLARE_CLASS(QRect) QT_FORWARD_DECLARE_CLASS(QSize) QT_FORWARD_DECLARE_CLASS(QPixmap) +QT_FORWARD_DECLARE_CLASS(QIcon) namespace QtForkAwesome { diff --git a/scripts/yaml2enum.pl b/scripts/yaml2enum.pl index 3b93d8f..e156711 100755 --- a/scripts/yaml2enum.pl +++ b/scripts/yaml2enum.pl @@ -12,11 +12,12 @@ my $parse_yaml die "Unable to locate YAML::XS or YAML: $@" if $@; # read CLI args -my $yaml_file = $ARGV[0] // ''; -my $output_file = $ARGV[1] // ''; +my $yaml_file = $ARGV[0] // ''; +my $enum_output_file = $ARGV[1] // ''; +my $id_output_file = $ARGV[2] // ''; die "The specified file \"$yaml_file\" does not exist.\n" unless -f $yaml_file; -die "No output file specified.\n" unless $output_file; -print "$yaml_file -> $output_file\n"; +die "No enum output file specified.\n" unless $enum_output_file; +die "No ID output file specified.\n" unless $id_output_file; # read input file manually (instead of using LoadFile) to fix screwed indentation open my $yaml_fh, '<', $yaml_file or die "Can't open \"$yaml_file\": $!"; @@ -27,7 +28,8 @@ $yaml =~ s/ - name:/ - name:/g; my $icon_defs = $parse_yaml->($yaml); my $icons = ref $icon_defs eq 'HASH' ? $icon_defs->{icons} : []; die "No icons present in \"$yaml_file\".\n" unless ref $icons eq 'ARRAY' && @$icons; -open my $output, '>:encoding(UTF-8)', $output_file or die "Can't open output file \"$output_file\": $!\n"; +open my $enum_output, '>:encoding(UTF-8)', $enum_output_file or die "Can't open output file \"$enum_output_file\": $!\n"; +open my $id_output, '>:encoding(UTF-8)', $id_output_file or die "Can't open output file \"$id_output_file\": $!\n"; # define function to camelize IDs sub camelize ($s) { $s =~ s/(-)?([\w']+)/\u\L$2/g; $s } @@ -44,5 +46,6 @@ for my $icon (@$icons) { my $category = join ',', @$categories; my $camelized_id = camelize $id; $camelized_id = "Icon$camelized_id" if $camelized_id =~ qr/^\d+.*/; - print $output "$camelized_id = 0x$unicode, /**< $id: name: $name, created: $created, category: $category */\n"; + print $enum_output "$camelized_id = 0x$unicode, /**< $id: name: $name, created: $created, category: $category */\n"; + print $id_output "{ QStringLiteral(\"$id\"), Icon::$camelized_id },\n"; } diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..4abe89f --- /dev/null +++ b/utils.cpp @@ -0,0 +1,29 @@ +#ifndef QT_FORK_AWESOME_UTILS +#define QT_FORK_AWESOME_UTILS + +#include "./utils.h" +#include "./icon.h" + +#include +#include + +namespace QtForkAwesome { + +/*! + * \brief Returns a QtForkAwesome::Icon for the specified icon \a id. + * \param id Specifies the icon ID, that's the "name" used on https://forkaweso.me/Fork-Awesome/icons. + * \returns Returns the QtForkAwesome::Icon which might be QtForkAwesome::Icon::Invalid in case the + * specified \a id is unknown. + */ +Icon iconFromId(const QString &id) +{ + static const auto mapping = QHash({ +#include "private/idmapping.h" + }); + const auto icon = mapping.find(id); + return icon != mapping.cend() ? *icon : Icon::Invalid; +} + +} // namespace QtForkAwesome + +#endif // QT_FORK_AWESOME_UTILS diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..1de8edd --- /dev/null +++ b/utils.h @@ -0,0 +1,20 @@ +#ifndef QT_FORK_AWESOME_UTILS +#define QT_FORK_AWESOME_UTILS + +#include "./global.h" +#include "./iconfwd.h" + +QT_FORWARD_DECLARE_CLASS(QString) + +namespace QtForkAwesome { + +constexpr bool isIconValid(Icon icon) +{ + return static_cast(icon); +} + +QT_FORK_AWESOME_EXPORT Icon iconFromId(const QString &id); + +} // namespace QtForkAwesome + +#endif // QT_FORK_AWESOME_UTILS