Skip to content

Instantly share code, notes, and snippets.

@mnafees
Last active May 16, 2019 18:07
Show Gist options
  • Save mnafees/9570760 to your computer and use it in GitHub Desktop.
Save mnafees/9570760 to your computer and use it in GitHub Desktop.
Font Awesome for Qt apps
// Qt
#include <QCoreApplication>
#include <QStringList>
#include <QFile>
#include <QHash>
#include <QTextStream>
#include <QtDebug>
int main( int argc, char *argv[] )
{
QCoreApplication a( argc, argv );
if ( a.arguments().contains( "--cssfilepath" ) ) {
if ( a.arguments().size() < 3 ) {
qCritical() << "Please provide the css file path after \"--cssfilepath\"";
return -1;
}
QString mode = "c++";
if ( a.arguments().contains( "--mode" ) ) {
if ( a.arguments().size() < 5 ) {
qCritical() << "Please provide a mode to output the file in your preferred format.";
return -1;
} else {
mode = a.arguments().at( a.arguments().indexOf( "--mode" ) + 1 );
if ( mode != "c++" && mode != "qml" ) {
qCritical() << "Invalid mode entered. Enter \'c++\' for generating header file or \'qml\' for generating javascript file";
return -1;
}
}
}
QString cssFilePath = a.arguments().at( a.arguments().indexOf( "--cssfilepath" ) + 1 );
QFile cssFile( cssFilePath );
if ( cssFile.open( QIODevice::ReadOnly ) ) {
QHash<QString, QString> hash;
QString line, name, unicode;
QStringList otherNames;
while( !cssFile.atEnd() ) {
line = cssFile.readLine();
if ( line.contains( ":before," ) ) {
otherNames << line.split( "." ).at( 1 ).split( ":" ).at( 0 ).split( "," ).at( 0 );
} else if ( line.contains( ":before {" ) ) {
name = line.split( "." ).at( 1 ).split( ":" ).at( 0 );
} else if ( line.contains( "content: " ) ) {
unicode = "\\uf" + line.split( " " ).at( 3 ).split( "\"" ).at( 1 ).split( "\\f" ).at( 1 );
}
if ( name != "" && unicode != "" ) {
hash.insert( name, unicode );
if ( otherNames.size() != 0 ) {
for ( int i = 0; i < otherNames.size(); ++i ) {
hash.insert( otherNames.at( i ), unicode );
}
otherNames.clear();
}
name = "";
unicode = "";
}
}
cssFile.close();
QString outputPath;
if ( a.arguments().contains( "--outputpath" ) ) {
if ( a.arguments().size() < 5 ) {
qDebug() << "Please provide the css file path after \"--outputpath\"";
return -1;
}
outputPath = a.arguments().at( a.arguments().indexOf( "--outputpath" ) + 1 );
outputPath = outputPath.endsWith( "/" ) ? outputPath : outputPath + "/";
}
QFile classFile;
if ( mode == "c++" ) {
classFile.setFileName( outputPath + "FontAwesomeIconNames.h" );
} else if ( mode == "qml" ) {
classFile.setFileName( outputPath + "FontAwesomeIconNames.js" );
}
if ( classFile.exists() ) {
classFile.resize( 0 );
}
classFile.open( QIODevice::WriteOnly );
QTextStream stream( &classFile );
if ( mode == "c++" ) {
stream << "#ifndef FONTAWESOMEICONNAMES_H\n"
<< "#define FONTAWESOMEICONNAMES_H\n"
<< "\n"
<< "//Qt\n"
<< "#include <QString>\n"
<< "\n"
<< "class FontAwesomeIconNames\n"
<< "{\n"
<< "public:\n"
<< " FontAwesomeIconNames() {}\n"
<< " static QString unicode( QString iconName )\n"
<< " {\n";
for ( int i = 0; i < hash.size(); ++i ) {
stream << " if ( iconName == \"" << hash.keys().at( i ) << "\" ) {\n"
<< " return QString::fromUtf8( \"" << hash.values().at( i ) << "\" );\n"
<< " }\n";
}
stream << " }\n"
<< "};\n"
<< "\n"
<< "#endif\n";
} else if ( mode == "qml" ) {
stream << "var Icon = {\n";
for ( int i = 0; i < hash.size(); ++i ) {
stream << " \"" << hash.keys().at( i ) << "\" : \"" << hash.values().at( i ) << "\"";
if ( i < hash.size() - 1 ) {
stream << ",\n";
}
}
stream << "\n};";
}
classFile.close();
qDebug() << "Output file succesfully generated.";
return 0;
} else {
qCritical() << "Could not open css file. " << cssFile.errorString();
return -1;
}
} else {
qCritical() << "usage: ./FontAwesomeClassGenerator --cssfilepath /path/to/fontawesome.css --mode [c++|qml] [--outputpath /path/to/save/outputfile]";
return -1;
}
return a.exec();
}
#include <QApplication>
#include <QFontDatabase>
int main( int argc, char *argv[] )
{
QApplication app( argc, argv );
QFontDatabase::addApplicationFont( "/path/to/fontawesome.ttf" );
return app.exec();
}
// someClass here, inherits QWidget
void someClass::myMethod()
{
setFont( "FontAwesome" );
QLabel *label = new QLabel;
label->setText( FontAwesomeIconNamesUtility::unicode( "fa-square" ) ); // replace fa-square with any icon name you want
label->show();
}
// using it in the paintEvent() just for painting some icons
void someClass::paintEvent( QPaintEvent * )
{
QPainter painter( this );
painter.setFont( "FontAwesome" );
painter.drawText( 100, 100, 100, 100, Qt::AlignCenter, FontAwesomeIconNamesUtility::unicode( "fa-square" ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment