mirror of
https://github.com/gamecreature/QtAwesome.git
synced 2025-01-13 16:42:06 +08:00
Added library resource initialization support. Added font method for
creating icon fonts. Improved focus and added custom painter example.
This commit is contained in:
parent
89f9bc2f3f
commit
fd58f84cde
@ -28,6 +28,8 @@
|
|||||||
#include <QFontDatabase>
|
#include <QFontDatabase>
|
||||||
|
|
||||||
|
|
||||||
|
Q_INIT_RESOURCE_EXTERN(QtAwesome);
|
||||||
|
|
||||||
/// The font-awesome icon painter
|
/// The font-awesome icon painter
|
||||||
class QtAwesomeCharIconPainter: public QtAwesomeIconPainter
|
class QtAwesomeCharIconPainter: public QtAwesomeIconPainter
|
||||||
{
|
{
|
||||||
@ -53,10 +55,8 @@ public:
|
|||||||
|
|
||||||
// add some 'padding' around the icon
|
// add some 'padding' around the icon
|
||||||
int drawSize = qRound(rect.height()*options.value("scale-factor").toFloat());
|
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->drawText( rect, options.value("text").toString(), QTextOption( Qt::AlignCenter|Qt::AlignVCenter ) );
|
||||||
painter->restore();
|
painter->restore();
|
||||||
}
|
}
|
||||||
@ -155,6 +155,16 @@ bool QtAwesome::initFontAwesome( )
|
|||||||
|
|
||||||
// only load font-awesome once
|
// only load font-awesome once
|
||||||
if( fontAwesomeFontId < 0 ) {
|
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");
|
QFile res(":/fonts/fontawesome.ttf");
|
||||||
if(!res.open(QIODevice::ReadOnly)) {
|
if(!res.open(QIODevice::ReadOnly)) {
|
||||||
qDebug() << "Font awesome font could not be loaded!";
|
qDebug() << "Font awesome font could not be loaded!";
|
||||||
@ -541,3 +551,14 @@ void QtAwesome::give(const QString& name, QtAwesomeIconPainter* painter)
|
|||||||
painterMap_.insert( name, 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;
|
||||||
|
}
|
||||||
|
@ -308,8 +308,8 @@ enum QtFontAwesomeName {
|
|||||||
|
|
||||||
class QtAwesomeIconPainter;
|
class QtAwesomeIconPainter;
|
||||||
|
|
||||||
/// The main objet for creating icons.
|
/// The main class for managing icons
|
||||||
/// This class requires a 2-phase construction
|
/// 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
|
class QtAwesome : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -333,6 +333,8 @@ public:
|
|||||||
|
|
||||||
void give( const QString& name, QtAwesomeIconPainter* painter );
|
void give( const QString& name, QtAwesomeIconPainter* painter );
|
||||||
|
|
||||||
|
QFont font( int size );
|
||||||
|
|
||||||
/// Returns the font-name that is used as icon-map
|
/// Returns the font-name that is used as icon-map
|
||||||
QString fontName() { return fontName_ ; }
|
QString fontName() { return fontName_ ; }
|
||||||
|
|
||||||
|
78
README.md
78
README.md
@ -36,27 +36,62 @@ Add an accessor to this object. (a global function, membor of your application o
|
|||||||
Example
|
Example
|
||||||
--------
|
--------
|
||||||
|
|
||||||
// You should create a single object of QtAwesome.
|
```c++
|
||||||
QtAwesome* awesome = new QtAwesome( qApp );
|
// You should create a single object of QtAwesome.
|
||||||
awesome->initFontAwesome();
|
QtAwesome* awesome = new QtAwesome( qApp );
|
||||||
|
awesome->initFontAwesome();
|
||||||
|
|
||||||
// Next create your icon with the help of the icon-enumeration: (all dashes are replaced by underscores)
|
// 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!" );
|
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 )
|
// 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!" );
|
QPushButton* coffeeButton new QPushButton( awesome->icon( "coffee" ), "Black please!" );
|
||||||
|
|
||||||
// When you create an icon you can supply some options for your icons:
|
// When you create an icon you can supply some options for your icons:
|
||||||
// The available options can be found at the "Default options"-section
|
// The available options can be found at the "Default options"-section
|
||||||
|
|
||||||
QVariantMap options;
|
QVariantMap options;
|
||||||
options.insert( "color" , QColor(255,0,0) );
|
options.insert( "color" , QColor(255,0,0) );
|
||||||
QPushButton* musicButton = new QPushButton( awesome->icon( icon_music ), "Music" );
|
QPushButton* musicButton = new QPushButton( awesome->icon( icon_music ), "Music" );
|
||||||
|
|
||||||
// You can also change the default options.
|
// You can also change the default options.
|
||||||
// for example if you always would like to have green icons you could call)
|
// for example if you always would like to have green icons you could call)
|
||||||
awesome->setDefaultOption( "color-disabled", QColor(0,255,0) );
|
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:
|
Default options:
|
||||||
@ -64,11 +99,13 @@ Default options:
|
|||||||
|
|
||||||
The following options are default in the QtAwesome class.
|
The following options are default in the QtAwesome class.
|
||||||
|
|
||||||
setDefaultOption( "color", QColor(50,50,50) );
|
```c++
|
||||||
setDefaultOption( "color-disabled", QColor(70,70,70,60));
|
setDefaultOption( "color", QColor(50,50,50) );
|
||||||
setDefaultOption( "color-active", QColor(10,10,10));
|
setDefaultOption( "color-disabled", QColor(70,70,70,60));
|
||||||
setDefaultOption( "color-selected", QColor(10,10,10));
|
setDefaultOption( "color-active", QColor(10,10,10));
|
||||||
setDefaultOption( "scale-factor", 0.9 );
|
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.
|
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.
|
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:
|
There are still some things todo, like:
|
||||||
|
|
||||||
* document the usage of a custom icon painter
|
|
||||||
* document the usage of another icon font
|
* document the usage of another icon font
|
||||||
* add some tests
|
* add some tests
|
||||||
* do some code cleanup
|
* do some code cleanup
|
||||||
|
Loading…
Reference in New Issue
Block a user