From fd58f84cdeaf4db3f288b9f24426eaf7355dd7fc Mon Sep 17 00:00:00 2001 From: Rick Blommers Date: Fri, 19 Apr 2013 13:09:36 +0200 Subject: [PATCH] Added library resource initialization support. Added font method for creating icon fonts. Improved focus and added custom painter example. --- QtAwesome/QtAwesome.cpp | 27 ++++++++++++-- QtAwesome/QtAwesome.h | 6 ++-- README.md | 78 ++++++++++++++++++++++++++++++----------- 3 files changed, 85 insertions(+), 26 deletions(-) diff --git a/QtAwesome/QtAwesome.cpp b/QtAwesome/QtAwesome.cpp index 85186db..2e8b572 100644 --- a/QtAwesome/QtAwesome.cpp +++ b/QtAwesome/QtAwesome.cpp @@ -28,6 +28,8 @@ #include +Q_INIT_RESOURCE_EXTERN(QtAwesome); + /// The font-awesome icon painter class QtAwesomeCharIconPainter: public QtAwesomeIconPainter { @@ -53,10 +55,8 @@ public: // add some 'padding' around the icon int drawSize = qRound(rect.height()*options.value("scale-factor").toFloat()); - QFont font(awesome->fontName()); - font.setPixelSize(drawSize); // use pixel size - painter->setFont( font ); + painter->setFont( awesome->font(drawSize) ); painter->drawText( rect, options.value("text").toString(), QTextOption( Qt::AlignCenter|Qt::AlignVCenter ) ); painter->restore(); } @@ -155,6 +155,16 @@ bool QtAwesome::initFontAwesome( ) // only load font-awesome once if( fontAwesomeFontId < 0 ) { + + // The macro below internally calls "qInitResources_QtAwesome()". this initializes + // the resource system. For a .pri project this isn't required, but when building and using a + // static library the resource need to initialized first. + /// + // I've checked th qInitResource_* code and calling this method mutliple times shouldn't be any problem + // (More info about this subject: http://qt-project.org/wiki/QtResources) + Q_INIT_RESOURCE(QtAwesome); + + // load the font file QFile res(":/fonts/fontawesome.ttf"); if(!res.open(QIODevice::ReadOnly)) { qDebug() << "Font awesome font could not be loaded!"; @@ -541,3 +551,14 @@ void QtAwesome::give(const QString& name, QtAwesomeIconPainter* painter) painterMap_.insert( name, painter ); } +/// Creates/Gets the icon font with a given size in pixels. This can be usefull to use a label for displaying icons +/// Example: +/// +/// QLabel* label = new QLabel( QChar( icon_group ) ); +/// label->setFont( awesome->font(16) ) +QFont QtAwesome::font( int size ) +{ + QFont font( fontName_); + font.setPixelSize(size); + return font; +} diff --git a/QtAwesome/QtAwesome.h b/QtAwesome/QtAwesome.h index 6589ee0..5a51ac9 100644 --- a/QtAwesome/QtAwesome.h +++ b/QtAwesome/QtAwesome.h @@ -308,8 +308,8 @@ enum QtFontAwesomeName { class QtAwesomeIconPainter; -/// The main objet for creating icons. -/// This class requires a 2-phase construction +/// The main class for managing icons +/// This class requires a 2-phase construction. You must first create the class and then initialize it via an init* method class QtAwesome : public QObject { Q_OBJECT @@ -333,6 +333,8 @@ public: void give( const QString& name, QtAwesomeIconPainter* painter ); + QFont font( int size ); + /// Returns the font-name that is used as icon-map QString fontName() { return fontName_ ; } diff --git a/README.md b/README.md index e5c5d2a..95a0c88 100644 --- a/README.md +++ b/README.md @@ -36,27 +36,62 @@ Add an accessor to this object. (a global function, membor of your application o Example -------- - // You should create a single object of QtAwesome. - QtAwesome* awesome = new QtAwesome( qApp ); - awesome->initFontAwesome(); +```c++ +// You should create a single object of QtAwesome. +QtAwesome* awesome = new QtAwesome( qApp ); +awesome->initFontAwesome(); - // Next create your icon with the help of the icon-enumeration: (all dashes are replaced by underscores) - QPushButton* beerButton new QPushButton( awesome->icon( icon_beer ), "Cheers!" ); +// Next create your icon with the help of the icon-enumeration: (all dashes are replaced by underscores) +QPushButton* beerButton new QPushButton( awesome->icon( icon_beer ), "Cheers!" ); - // You can also use 'string' names to access the icons. (The string version omits the 'icon-' prefix ) - QPushButton* coffeeButton new QPushButton( awesome->icon( "coffee" ), "Black please!" ); +// You can also use 'string' names to access the icons. (The string version omits the 'icon-' prefix ) +QPushButton* coffeeButton new QPushButton( awesome->icon( "coffee" ), "Black please!" ); - // When you create an icon you can supply some options for your icons: - // The available options can be found at the "Default options"-section +// When you create an icon you can supply some options for your icons: +// The available options can be found at the "Default options"-section - QVariantMap options; - options.insert( "color" , QColor(255,0,0) ); - QPushButton* musicButton = new QPushButton( awesome->icon( icon_music ), "Music" ); +QVariantMap options; +options.insert( "color" , QColor(255,0,0) ); +QPushButton* musicButton = new QPushButton( awesome->icon( icon_music ), "Music" ); - // You can also change the default options. - // for example if you always would like to have green icons you could call) - awesome->setDefaultOption( "color-disabled", QColor(0,255,0) ); +// You can also change the default options. +// for example if you always would like to have green icons you could call) +awesome->setDefaultOption( "color-disabled", QColor(0,255,0) ); +// You can also directly render a label with this font +QLabel* label = new QLabel( QChar( icon_group ) ); +label->setFont( awesome->font(16) ); + +``` + +Example custom painter +---------------------- + +This example registers a custom painter for supporting a duplicate icon (it draws 2 plus marks) + +```c++ +class DuplicateIconPainter : public QtAwesomeIconPainter +{ +public: + virtual void paint( QtAwesome* awesome, QPainter* painter, const QRect& rectIn, QIcon::Mode mode, QIcon::State state, const QVariantMap& options ) + { + int drawSize = qRound(rectIn.height()*0.5); + int offset = rectIn.height() / 4; + QChar chr = QChar( icon_plus ); + + painter->setFont( awesome->font( drawSize ) ); + + painter->setPen( QColor(100,100,100) ); + painter->drawText( QRect( QPoint(offset*2, offset*2), QSize(drawSize, drawSize) ), chr , QTextOption( Qt::AlignCenter|Qt::AlignVCenter ) ); + + painter->setPen( QColor(50,50,50) ); + painter->drawText( QRect( QPoint(rectIn.width()-drawSize-offset, rectIn.height()-drawSize-offset), QSize(drawSize, drawSize) ), chr , QTextOption( Qt::AlignCenter|Qt::AlignVCenter ) ); + + } +}; + +awesome->give("duplicate", new DuplicateIconPainter() ); +``` Default options: @@ -64,11 +99,13 @@ Default options: The following options are default in the QtAwesome class. - setDefaultOption( "color", QColor(50,50,50) ); - setDefaultOption( "color-disabled", QColor(70,70,70,60)); - setDefaultOption( "color-active", QColor(10,10,10)); - setDefaultOption( "color-selected", QColor(10,10,10)); - setDefaultOption( "scale-factor", 0.9 ); +```c++ +setDefaultOption( "color", QColor(50,50,50) ); +setDefaultOption( "color-disabled", QColor(70,70,70,60)); +setDefaultOption( "color-active", QColor(10,10,10)); +setDefaultOption( "color-selected", QColor(10,10,10)); +setDefaultOption( "scale-factor", 0.9 ); +``` When creating an icon, it first populates the options-map with the default options from the QtAwesome object. After that the options are expanded/overwritten by the options supplied to the icon. @@ -103,7 +140,6 @@ a work in progress. So feel free to drop me an e-mail for your suggestions and i There are still some things todo, like: - * document the usage of a custom icon painter * document the usage of another icon font * add some tests * do some code cleanup