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

@@ -146,6 +146,32 @@ setDefaultOption( "scale-factor", 0.9 );
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
-------