bind State

This commit is contained in:
Marco Martin 2012-02-20 13:32:28 +01:00
parent ed5e2e1981
commit d408978213
2 changed files with 44 additions and 6 deletions

View File

@ -19,15 +19,17 @@
#include "qiconitem.h"
#include <kicon.h>
#include <kiconloader.h>
#include <KIcon>
#include <KIconLoader>
#include <KIconEffect>
#include <QtGui/QPainter>
QIconItem::QIconItem(QDeclarativeItem *parent)
: QDeclarativeItem(parent),
m_smooth(false),
m_group(NoGroup)
m_group(NoGroup),
m_state(DefaultState)
{
setFlag(QGraphicsItem::ItemHasNoContents, false);
}
@ -63,6 +65,7 @@ void QIconItem::setGroup(QIconItem::Group group)
emit groupChanged(group);
emit implicitWidthChanged(implicitWidth());
emit implicitHeightChanged(implicitHeight());
update();
}
QIconItem::Group QIconItem::group() const
@ -70,6 +73,22 @@ QIconItem::Group QIconItem::group() const
return m_group;
}
QIconItem::State QIconItem::state() const
{
return m_state;
}
void QIconItem::setState(QIconItem::State state)
{
if (m_state == state) {
return;
}
m_state = state;
emit stateChanged(state);
update();
}
int QIconItem::implicitWidth() const
{
return KIconLoader::global()->currentSize((KIconLoader::Group)m_group);
@ -108,7 +127,14 @@ void QIconItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->setRenderHint(QPainter::Antialiasing, m_smooth);
painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth);
m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, isEnabled()?QIcon::Normal:QIcon::Disabled);
if (m_state == DefaultState) {
m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, isEnabled()?QIcon::Normal:QIcon::Disabled);
} else {
QPixmap result = m_icon.pixmap(boundingRect().size().toSize());
KIconLoader::global()->iconEffect()->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState);
painter->drawPixmap(0, 0, result);
}
painter->setRenderHint(QPainter::Antialiasing, wasAntiAlias);
painter->setRenderHint(QPainter::SmoothPixmapTransform, wasSmoothTransform);
}

View File

@ -32,7 +32,10 @@ class QIconItem : public QDeclarativeItem
Q_PROPERTY(int implicitWidth READ implicitWidth NOTIFY implicitWidthChanged)
Q_PROPERTY(int implicitHeight READ implicitHeight NOTIFY implicitHeightChanged)
Q_PROPERTY(Group group READ group WRITE setGroup NOTIFY groupChanged)
Q_PROPERTY(State state READ state WRITE setState NOTIFY stateChanged)
Q_ENUMS(Group)
Q_ENUMS(State)
public:
enum Group {
@ -44,8 +47,12 @@ public:
Small, ///Small icons, e.g. for buttons.
Panel, ///Panel (Plasma Taskbar) icons.
Dialog, ///Icons for use in dialog titles, page lists, etc.
LastGroup, ///Last group.
User ///User icons.
};
enum State {
DefaultState, ///The default state.
ActiveState, ///Icon is active.
DisabledState ///Icon is disabled.
};
QIconItem(QDeclarativeItem *parent=0);
@ -57,6 +64,9 @@ public:
void setGroup(Group group);
Group group() const;
QIconItem::State state() const;
void setState(State state);
int implicitWidth() const;
int implicitHeight() const;
@ -69,11 +79,13 @@ Q_SIGNALS:
void implicitWidthChanged(int implicitWidth);
void implicitHeightChanged(int implicitHeight);
void groupChanged(Group group);
void stateChanged(State state);
private:
QIcon m_icon;
bool m_smooth;
Group m_group;
State m_state;
};
#endif