feat: add icon assistant

This commit is contained in:
Qsaker 2023-12-05 17:07:05 +08:00
parent 700f16775a
commit 37bfd70e8a
14 changed files with 504 additions and 0 deletions

View File

@ -0,0 +1,14 @@
set(fontawesome_dir "fontawesome-free-6.5.1-desktop")
add_compile_definitions(SAK_FONTAWESOME_DIR="${fontawesome_dir}")
function(sak_import_fontawesome_for_target target)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory fonts
COMMAND
${CMAKE_COMMAND} -E copy_directory_if_different
"${CMAKE_SOURCE_DIR}/resources/font/${fontawesome_dir}"
"${SAK_BINARY_DIR}/${target}/fonts/${fontawesome_dir}"
WORKING_DIRECTORY ${SAK_BINARY_DIR}/${target} VERBATIM)
endfunction(sak_import_fontawesome_for_target)

View File

@ -37,6 +37,7 @@ set(BUILD_SHARED_LIBS OFF)
include(.cmake/sak_common.cmake)
include(.cmake/sak_environment.cmake)
include(.cmake/sak_deploy.cmake)
include(.cmake/sak_font_fontawesome.cmake)
sak_get_last_commit(${CMAKE_CURRENT_SOURCE_DIR} "SAK")
sak_get_last_commit_time(${CMAKE_CURRENT_SOURCE_DIR} "SAK")
@ -86,6 +87,7 @@ sak_import_assistant("number")
sak_import_assistant("string")
sak_import_assistant("broadcast")
sak_import_assistant("filecheck")
sak_import_assistant("fontawesome")
# QtSwissArmyKnife src files
macro(sak_add_src src_dir_name)
@ -136,6 +138,7 @@ endif()
# QtSwissArmyKnife
include_directories(src)
include_directories(3rd/qt/flowlayout)
file(GLOB APP_SOURCES "src/*.h" "src/*.cc" "src/*.ui" ".cmake/*.cmake")
option(SAK_BUILD_FOR_APP_SOTRE "Build for Microsoft/Apple app store." OFF)
@ -153,10 +156,15 @@ if(SAK_IMPORT_MODULE_BLUETOOTH)
include_directories(src/optional/bluetooth)
endif()
file(GLOB_RECURSE SAK_QT_FLOWLAYOUT_SOURCES "${CMAKE_SOURCE_DIR}/3rd/qt/flowlayout/*.cpp"
"${CMAKE_SOURCE_DIR}/3rd/qt/flowlayout/*.h")
list(APPEND SAK_APP_SOURCES ${SAK_QT_FLOWLAYOUT_SOURCES})
sak_add_executable("QtSwissArmyKnife" ${SAK_APP_SOURCES})
sak_auto_execute_deployqt(QtSwissArmyKnife)
sak_tar_target(QtSwissArmyKnife)
sak_set_target_properties(QtSwissArmyKnife)
sak_import_fontawesome_for_target(QtSwissArmyKnife)
target_link_libraries(
QtSwissArmyKnife
PRIVATE glog::glog Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::WebSockets

View File

@ -40,3 +40,4 @@ add_subdirectory(number)
add_subdirectory(string)
add_subdirectory(broadcast)
add_subdirectory(filecheck)
add_subdirectory(fontawesome)

View File

@ -0,0 +1,6 @@
file(GLOB_RECURSE APP_ASSISITANT_OWN_SOURCE "${CMAKE_SOURCE_DIR}/3rd/qt/flowlayout/*.cpp"
"${CMAKE_SOURCE_DIR}/3rd/qt/flowlayout/*.h")
set(APP_ASSISITANT_OWN_LIBS Qt${QT_VERSION_MAJOR}::Widgets)
sak_add_assistant("fontawesome" "FontAwesomeAssistant")
sak_import_fontawesome_for_target(FontAwesomeAssistant)

View File

@ -0,0 +1,22 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#include <QApplication>
#include "sakcommonmainwindow.h"
#include "sakfontawesomeassistant.h"
int main(int argc, char* argv[])
{
QApplication* app = CreateCommonMainWindowApplication<SAKFontAwesomeAssistant>(
argc, argv, QObject::tr("Font Awesome Assistant"), "SAK.FontAwesomeAssistant");
int ret = app->exec();
app->deleteLater();
return ret;
}

View File

@ -0,0 +1,124 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#include "sakfontawesome.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QFontDatabase>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
SAKFontAwesome::SAKFontAwesome(QObject *parent)
: QObject{parent}
{
QFile file(fontAwesomePath() + "/metadata/icon-families.json");
if (file.open(QFile::ReadOnly)) {
QByteArray all = file.readAll();
file.close();
QJsonDocument doc = QJsonDocument::fromJson(all);
QJsonObject obj = doc.object();
QStringList keys = obj.keys();
SAKFontAwesomeIconContext icon;
for (auto &key : keys) {
QJsonObject iconCtx = obj.value(key).toObject();
icon.terms.clear();
icon.unicode = iconCtx.value("unicode").toString().toInt(nullptr, 16);
icon.label = iconCtx.value("label").toString();
QJsonObject search = iconCtx.value("search").toObject();
QJsonArray terms = search.value("terms").toArray();
for (int i = 0; i < terms.count(); i++) {
icon.terms.append(terms.at(i).toString().toUpper());
}
QJsonObject svgs = iconCtx.value("svgs").toObject();
QJsonObject classic = svgs.value("classic").toObject();
QJsonObject family = classic.value("solid").toObject();
if (!family.isEmpty()) {
icon.family = familyToString(FreeSolid);
m_supportedIcons.append(icon);
continue;
}
family = classic.value("brands").toObject();
if (!family.isEmpty()) {
icon.family = familyToString(BrandRegular);
m_supportedIcons.append(icon);
continue;
}
family = classic.value("regular").toObject();
if (!family.isEmpty()) {
icon.family = familyToString(FreeRegular);
m_supportedIcons.append(icon);
continue;
}
}
} else {
qWarning() << "open file failed:" << file.errorString();
}
}
SAKFontAwesome *SAKFontAwesome::instance()
{
static SAKFontAwesome *instance = nullptr;
if (!instance) {
instance = new SAKFontAwesome(qApp);
}
return instance;
}
QList<SAKFontAwesomeIconContext> SAKFontAwesome::supportedIcons() const
{
return m_supportedIcons;
}
void SAKFontAwesome::addApplicationFonts()
{
QString path = fontAwesomePath() + "/otfs/";
QString name = path + QString("Font Awesome 6 Brands-Regular-400.otf");
int fontId = QFontDatabase::addApplicationFont(name);
qInfo() << "Install font:" << QFontDatabase::applicationFontFamilies(fontId);
name = path + QString("Font Awesome 6 Free-Regular-400.otf");
fontId = QFontDatabase::addApplicationFont(name);
qInfo() << "Install font:" << QFontDatabase::applicationFontFamilies(fontId);
name = path + QString("Font Awesome 6 Free-Solid-900.otf");
fontId = QFontDatabase::addApplicationFont(name);
qInfo() << QFontDatabase::applicationFontFamilies(fontId);
}
QString SAKFontAwesome::familyToString(int family)
{
switch (family) {
case BrandRegular:
return QString("Font Awesome 6 Brands Regular");
case FreeRegular:
return QString("Font Awesome 6 Free Regular");
case FreeSolid:
return QString("Font Awesome 6 Free Solid");
default:
qWarning() << "unknown font family:" << family;
return QString();
}
}
QString SAKFontAwesome::fontAwesomePath() const
{
QString path = QCoreApplication::applicationDirPath();
path += "/fonts/" + QString(SAK_FONTAWESOME_DIR);
return path;
}

View File

@ -0,0 +1,37 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#ifndef SAKFONTAWESOME_H
#define SAKFONTAWESOME_H
#include <QList>
#include <QObject>
#include "sakfontawesomedatastruct.h"
class SAKFontAwesome : public QObject
{
Q_OBJECT
private:
explicit SAKFontAwesome(QObject *parent = nullptr);
public:
static SAKFontAwesome *instance();
void addApplicationFonts();
QList<SAKFontAwesomeIconContext> supportedIcons() const;
QString familyToString(int family);
private:
QList<SAKFontAwesomeIconContext> m_supportedIcons;
private:
QString fontAwesomePath() const;
};
#endif // SAKFONTAWESOME_H

View File

@ -0,0 +1,34 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#include "sakfontawesomeassistant.h"
#include "ui_sakfontawesomeassistant.h"
#include "flowlayout.h"
#include "sakfontawesome.h"
#include "sakfontawesomeicon.h"
SAKFontAwesomeAssistant::SAKFontAwesomeAssistant(QWidget *parent)
: QWidget{parent}
, ui(new Ui::SAKFontAwesomeAssistant)
{
ui->setupUi(this);
SAKFontAwesome::instance()->addApplicationFonts();
auto *l = new FlowLayout();
ui->widget->setLayout(l);
auto icons = SAKFontAwesome::instance()->supportedIcons();
for (auto &icon : icons) {
auto iconWidget = new SAKFontAwesomeIcon(icon);
l->addWidget(iconWidget);
connect(ui->lineEditFilter, &QLineEdit::editingFinished, this, [=]() {
iconWidget->setFilter(ui->lineEditFilter->text().trimmed().toUpper());
});
iconWidget->setFilter(ui->lineEditFilter->text().trimmed().toUpper());
}
}

View File

@ -0,0 +1,28 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#ifndef SAKFONTAWESOMEASSISTANT_H
#define SAKFONTAWESOMEASSISTANT_H
#include <QWidget>
namespace Ui {
class SAKFontAwesomeAssistant;
}
class SAKFontAwesomeAssistant : public QWidget
{
Q_OBJECT
public:
explicit SAKFontAwesomeAssistant(QWidget *parent = nullptr);
private:
Ui::SAKFontAwesomeAssistant *ui;
};
#endif // SAKFONTAWESOMEASSISTANT_H

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SAKFontAwesomeAssistant</class>
<widget class="QWidget" name="SAKFontAwesomeAssistant">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>711</width>
<height>356</height>
</rect>
</property>
<property name="windowTitle">
<string>Font Awesome Assistant</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Filter</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEditFilter"/>
</item>
<item row="1" column="0" colspan="2">
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>691</width>
<height>307</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="widget" native="true"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,30 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#ifndef SAKFONTAWESOMEASSISTANTDATASTTRUCT_H
#define SAKFONTAWESOMEASSISTANTDATASTTRUCT_H
#include <QString>
#include <QStringList>
struct SAKFontAwesomeIconContext
{
QString label;
int unicode;
QString family;
QStringList terms;
};
enum SAKFontAwesomeFamily
{
BrandRegular,
FreeRegular,
FreeSolid,
};
#endif // SAKFONTAWESOMEASSISTANTDATASTTRUCT_H

View File

@ -0,0 +1,55 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#include "sakfontawesomeicon.h"
#include "ui_sakfontawesomeicon.h"
#include <QApplication>
#include <QClipboard>
#include <QFont>
SAKFontAwesomeIcon::SAKFontAwesomeIcon(const SAKFontAwesomeIconContext &ctx, QWidget *parent)
: QWidget{parent}
, ui(new Ui::SAKFontAwesomeIcon)
, m_ctx(ctx)
{
ui->setupUi(this);
ui->labelUnicode->setText(QString::number(m_ctx.unicode));
ui->labelIcon->setText(QString(QChar(m_ctx.unicode)));
QFont font = ui->labelIcon->font();
font.setPixelSize(48);
font.setFamily(m_ctx.family);
ui->labelIcon->setFont(font);
setFixedSize(96, 96);
QString toolTip = QString("unicode: %1\n").arg(ctx.unicode);
toolTip.append(QString("label: %1\n").arg(ctx.label));
toolTip.append(QString("terms: %1\n").arg(ctx.terms.join(",")).toLower());
toolTip.append(QString("family: %1").arg(ctx.family));
ui->pushButton->setToolTip(toolTip);
connect(ui->pushButton, &QPushButton::clicked, this, [=]() {
QApplication::clipboard()->setText(toolTip);
});
}
void SAKFontAwesomeIcon::setFilter(const QString &filter)
{
if (filter.isEmpty()) {
show();
return;
}
for (auto &term : m_ctx.terms) {
if (term.contains(filter)) {
show();
return;
}
}
hide();
}

View File

@ -0,0 +1,32 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* code directory.
**************************************************************************************************/
#ifndef SAKFONTAWESOMEICON_H
#define SAKFONTAWESOMEICON_H
#include <QWidget>
#include "sakfontawesomedatastruct.h"
namespace Ui {
class SAKFontAwesomeIcon;
}
class SAKFontAwesomeIcon : public QWidget
{
Q_OBJECT
public:
explicit SAKFontAwesomeIcon(const SAKFontAwesomeIconContext &ctx, QWidget *parent = nullptr);
void setFilter(const QString &filter);
private:
Ui::SAKFontAwesomeIcon *ui;
SAKFontAwesomeIconContext m_ctx;
};
#endif // SAKFONTAWESOMEICON_H

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SAKFontAwesomeIcon</class>
<widget class="QWidget" name="SAKFontAwesomeIcon">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>159</width>
<height>136</height>
</rect>
</property>
<property name="windowTitle">
<string notr="true">Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="2">
<widget class="QLabel" name="labelUnicode">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true"/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Info</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="3">
<widget class="QLabel" name="labelIcon">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true"/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>