Add mapping from icon names specified as QString to QtForkAwesome::Icon

This commit is contained in:
Martchus 2021-09-11 16:09:28 +02:00
parent f3ae226c8c
commit ed6f8198d0
6 changed files with 66 additions and 11 deletions

View File

@ -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")

1
icon.h
View File

@ -6,6 +6,7 @@
namespace QtForkAwesome {
enum class Icon : IconBaseType {
Invalid, /**< invalid, used by QtForkAwesome::iconFromId() to indicate failure */
#include "private/icons.h"
};

View File

@ -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 {

View File

@ -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";
}

29
utils.cpp Normal file
View File

@ -0,0 +1,29 @@
#ifndef QT_FORK_AWESOME_UTILS
#define QT_FORK_AWESOME_UTILS
#include "./utils.h"
#include "./icon.h"
#include <QHash>
#include <QString>
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<QString, Icon>({
#include "private/idmapping.h"
});
const auto icon = mapping.find(id);
return icon != mapping.cend() ? *icon : Icon::Invalid;
}
} // namespace QtForkAwesome
#endif // QT_FORK_AWESOME_UTILS

20
utils.h Normal file
View File

@ -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<IconBaseType>(icon);
}
QT_FORK_AWESOME_EXPORT Icon iconFromId(const QString &id);
} // namespace QtForkAwesome
#endif // QT_FORK_AWESOME_UTILS