#include "customdisablediconengine.h" #include #include #include #include CustomDisabledIconEngine::CustomDisabledIconEngine() { } void CustomDisabledIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) { painter->save(); painter->setClipRect(rect); QRect newRect = rect; QPixmap pixmap; if (mode == QIcon::Mode::Disabled) pixmap = mDisabledPixmap; else pixmap = mPixmap; if (pixmap.size().width() < rect.width()) { newRect.setLeft( rect.left()+(rect.width() - pixmap.size().width())/2); newRect.setWidth(pixmap.size().width()); } if (pixmap.size().height() < rect.height()) { newRect.setTop( rect.top()+(rect.height() - pixmap.size().height())/2); newRect.setHeight(pixmap.size().height()); } painter->drawPixmap(newRect,pixmap); painter->restore(); } QIconEngine *CustomDisabledIconEngine::clone() const { CustomDisabledIconEngine* eng = new CustomDisabledIconEngine(); eng->mPixmap = mPixmap; eng->mDisabledPixmap = mDisabledPixmap; return eng; } QPixmap CustomDisabledIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) { if (mode == QIcon::Mode::Disabled) return mDisabledPixmap; else return mPixmap; } void CustomDisabledIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state) { setPixmap(pixmap); } void CustomDisabledIconEngine::addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state) { setPixmap(QPixmap(fileName)); } void CustomDisabledIconEngine::setPixmap(const QPixmap &pixmap) { mPixmap = pixmap; if (pixmap.isNull()) mDisabledPixmap = pixmap; else { QImage oldImage = mPixmap.toImage(); QImage image(mPixmap.size(), QImage::Format_ARGB32); for (int x=0;x