fix #29, extra options to take in account the on/off state.

Updated/extended sample and README.md
This commit is contained in:
Rick Blommers 2017-11-27 16:32:49 +01:00
parent 5ba074ba00
commit 8c99d8127e
3 changed files with 154 additions and 21 deletions

View File

@ -19,7 +19,58 @@
/// The font-awesome icon painter
class QtAwesomeCharIconPainter: public QtAwesomeIconPainter
{
protected:
QStringList optionKeysForModeAndState( const QString& key, QIcon::Mode mode, QIcon::State state)
{
QString modePostfix;
switch(mode) {
case QIcon::Disabled:
modePostfix = "-disabled";
break;
case QIcon::Active:
modePostfix = "-active";
break;
case QIcon::Selected:
modePostfix = "-selected";
break;
}
QString statePostfix;
if( state == QIcon::Off) {
statePostfix = "-off";
}
// the keys that need to bet tested: key-mode-state | key-mode | key-state | key
QStringList result;
if( !modePostfix.isEmpty() ) {
if( !statePostfix.isEmpty() ) {
result.push_back( key + modePostfix + statePostfix );
}
result.push_back( key + modePostfix );
}
if( !statePostfix.isEmpty() ) {
result.push_back( key + statePostfix );
}
return result;
}
QVariant optionValueForModeAndState( const QString& baseKey, QIcon::Mode mode, QIcon::State state, const QVariantMap& options )
{
foreach( QString key, optionKeysForModeAndState(baseKey, mode, state) ) {
if( options.contains(key)) return options.value(key);
}
return options.value(baseKey);
}
public:
virtual void paint( QtAwesome* awesome, QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state, const QVariantMap& options )
{
Q_UNUSED(mode);
@ -34,7 +85,31 @@ public:
anim->setup( *painter, rect );
}
// get the correct key postfix
QString modePostfix;
switch(mode) {
case QIcon::Disabled:
modePostfix = "-disabled";
break;
case QIcon::Active:
modePostfix = "-active";
break;
case QIcon::Selected:
modePostfix = "-selected";
break;
}
QString statePostFix;
if( state == QIcon::Off) {
statePostFix = "-off";
}
// set the default options
QColor color = optionValueForModeAndState("color", mode, state, options).value<QColor>();
QString text = optionValueForModeAndState("text", mode, state, options).toString();
/*
// set the correct color
QColor color = options.value("color").value<QColor>();
QString text = options.value("text").toString();
@ -58,6 +133,9 @@ public:
text = alt.toString();
}
}
*/
painter->setPen(color);
// add some 'padding' around the icon

View File

@ -10,6 +10,8 @@
#include <QApplication>
#include <QMainWindow>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
@ -19,15 +21,42 @@ int main(int argc, char *argv[])
QtAwesome* awesome = new QtAwesome(&w);
awesome->initFontAwesome();
QVBoxLayout* layout = new QVBoxLayout();
// a simple beer button
QPushButton* beerButton = new QPushButton( "Cheers!");
//=====================
{
QPushButton* beerButton = new QPushButton( "Cheers!");
QVariantMap options;
options.insert("anim", qVariantFromValue( new QtAwesomeAnimation(beerButton) ) );
beerButton->setIcon( awesome->icon( fa::beer, options ) );
layout->addWidget(beerButton);
}
// a simple beer checkbox button
//==============================
{
QPushButton* toggleButton = new QPushButton("Toggle Me");
toggleButton->setCheckable(true);
QVariantMap options;
options.insert("color", QColor(Qt::green) );
options.insert("text-off", QString(fa::squareo) );
options.insert("color-off", QColor(Qt::red) );
toggleButton->setIcon( awesome->icon( fa::checksquareo, options ));
QVariantMap options;
options.insert("anim", qVariantFromValue( new QtAwesomeAnimation(beerButton) ) );
beerButton->setIcon( awesome->icon( fa::beer, options ) );
layout->addWidget(toggleButton);
}
// add the samples
QWidget* samples = new QWidget();
samples->setLayout(layout);
w.setCentralWidget(samples);
w.setCentralWidget( beerButton );
w.show();
return app.exec();

View File

@ -20,8 +20,8 @@ This library has been updated to Font Awesome version **4.7.0**.
* You can find the previous FontAwesome 4 c++11 library in the [c++11 branch](https://github.com/gamecreature/QtAwesome/tree/c++11).
* You can find the previous FontAwesome 3 library in the [fontawesome-3 branch](https://github.com/gamecreature/QtAwesome/tree/fontawesome-3).
**Note about previous c++11**
**Note about previous c++11**
I removed the C++11 requirement. And moved the c++11 code to a c++11 branch.
It's not that I don't like c++11, but the typed enum made the code less flexible then it is now.
@ -58,13 +58,13 @@ You probably want to create a single QtAwesome object for your whole application
Example
--------
```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 (no dashes):
// Next create your icon with the help of the icon-enumeration (no dashes):
QPushButton* beerButton new QPushButton( awesome->icon( fa::beer ), "Cheers!" );
// You can also use 'string' names to access the icons. (The string version omits the 'fa-' or 'icon-' prefix and has no dashes )
@ -75,9 +75,9 @@ QPushButton* coffeeButton new QPushButton( awesome->icon( "coffee" ), "Black ple
QVariantMap options;
options.insert( "color" , QColor(255,0,0) );
QPushButton* musicButton = new QPushButton( awesome->icon( fa::music, options ), "Music" );
QPushButton* musicButton = new QPushButton( awesome->icon( fa::music, options ), "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)
awesome->setDefaultOption( "color-disabled", QColor(0,255,0) );
@ -119,8 +119,8 @@ awesome->give("duplicate", new DuplicateIconPainter() );
Default options:
----------------
The following options are default in the QtAwesome class.
The following options are default in the QtAwesome class.
```c++
setDefaultOption( "color", QColor(50,50,50) );
@ -138,14 +138,40 @@ 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.
It is possible to use another glyph per icon-state. For example to make an icon-unlock symbol switch to locked when selected,
you could supply the following option:
```c++
```c++
options.insert("text-selected", QString( fa::lock ) );
```
Color and text options have the following structure:
`keyname-iconmode-iconstate`
Where iconmode normal is empty
And iconstate On is off.
So the list of items used is:
- color
- color-disabled
- color-active
- color-selected
- color-off
- color-disabled-off
- color-active-off
- color-selected-off
- text
- text-disabled
- text-active
- text-selected
- text-off
- text-disabled-off
- text-active-off
- text-selected-off
License
-------
@ -167,7 +193,7 @@ Contact
Known issues and workarounds
----------------------------
On Mac OS X, placing an qtAwesome icon in QMainWindow menu, doesn't work directly.
On Mac OS X, placing an qtAwesome icon in QMainWindow menu, doesn't work directly.
See the following issue: [https://github.com/gamecreature/QtAwesome/issues/10]
A workaround for this problem is converting it to a Pixmap icon like this:
@ -181,12 +207,12 @@ menuAction->setIcon( awesome->icon(fa::beer).pixmap(32,32) );
Remarks
-------
I've created this project because I needed some nice icons for my own Qt project. After doing a lot of
css/html5 work and being spoiled by the ease of twitter bootstrap with Font Awesome,
I've created this project because I needed some nice icons for my own Qt project. After doing a lot of
css/html5 work and being spoiled by the ease of twitter bootstrap with Font Awesome,
I thought it would be nice to be able to use these icons for my Qt project.
I've slightly changed the code from the original, added some more documentation, but it's still
a work in progress. So feel free to drop me an e-mail for your suggestions and improvements!
a work in progress. So feel free to drop me an e-mail for your suggestions and improvements!
There are still some things todo, like:
@ -196,9 +222,9 @@ There are still some things todo, like:
Thanks go to the contributors of this project!
And of course last but not least,
And of course last but not least,
Many thanks go to Dave Gandy an the other Font Awesome contributors!! [http://fortawesome.github.com/Font-Awesome](http://fortawesome.github.com/Font-Awesome)
Many thanks go to Dave Gandy an the other Font Awesome contributors!! [http://fortawesome.github.com/Font-Awesome](http://fortawesome.github.com/Font-Awesome)
And of course to the Qt team/contributors for supplying this great cross-platform c++ library.
Contributions are welcome! Feel free to fork and send a pull request through Github.