2022-05-21 10:44:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2022-02-03 21:44:45 +08:00
|
|
|
#include "customdisablediconengine.h"
|
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
CustomDisabledIconEngine::CustomDisabledIconEngine()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
void CustomDisabledIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State /*state*/)
|
2022-02-03 21:44:45 +08:00
|
|
|
{
|
2022-02-05 22:26:02 +08:00
|
|
|
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();
|
2022-02-03 21:44:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QIconEngine *CustomDisabledIconEngine::clone() const
|
|
|
|
{
|
2022-02-03 21:46:53 +08:00
|
|
|
CustomDisabledIconEngine* eng = new CustomDisabledIconEngine();
|
|
|
|
eng->mPixmap = mPixmap;
|
|
|
|
eng->mDisabledPixmap = mDisabledPixmap;
|
|
|
|
return eng;
|
2022-02-03 21:44:45 +08:00
|
|
|
}
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
QPixmap CustomDisabledIconEngine::pixmap(const QSize &/*size*/, QIcon::Mode mode, QIcon::State /*state*/)
|
2022-02-03 21:44:45 +08:00
|
|
|
{
|
|
|
|
if (mode == QIcon::Mode::Disabled)
|
|
|
|
return mDisabledPixmap;
|
|
|
|
else
|
|
|
|
return mPixmap;
|
|
|
|
}
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
void CustomDisabledIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode /*mode*/, QIcon::State /*state*/)
|
2022-02-03 21:44:45 +08:00
|
|
|
{
|
|
|
|
setPixmap(pixmap);
|
|
|
|
}
|
|
|
|
|
2022-05-21 10:44:39 +08:00
|
|
|
void CustomDisabledIconEngine::addFile(const QString &fileName, const QSize &/*size*/, QIcon::Mode /*mode*/, QIcon::State /*state*/)
|
2022-02-03 21:44:45 +08:00
|
|
|
{
|
|
|
|
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<image.width();x++) {
|
|
|
|
for (int y=0;y<image.height();y++) {
|
|
|
|
QColor c = oldImage.pixelColor(x,y);
|
|
|
|
int gray = 0.299 * c.red() + 0.587 * c.green() + 0.114 * c.blue();
|
|
|
|
QColor c2(gray,gray,gray,c.alpha());
|
|
|
|
c2 = c2.darker();
|
|
|
|
image.setPixelColor(x,y,c2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mDisabledPixmap = QPixmap::fromImage(image);
|
2022-04-02 18:42:43 +08:00
|
|
|
mDisabledPixmap.setDevicePixelRatio(mPixmap.devicePixelRatioF());
|
2022-02-03 21:44:45 +08:00
|
|
|
}
|
|
|
|
}
|