| 1 | /* | 
|---|
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) | 
|---|
| 3 | * | 
|---|
| 4 | * This program is free software: you can redistribute it and/or modify | 
|---|
| 5 | * it under the terms of the GNU General Public License as published by | 
|---|
| 6 | * the Free Software Foundation, either version 3 of the License, or | 
|---|
| 7 | * (at your option) any later version. | 
|---|
| 8 | * | 
|---|
| 9 | * This program is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | * | 
|---|
| 14 | * You should have received a copy of the GNU General Public License | 
|---|
| 15 | * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
|---|
| 16 | */ | 
|---|
| 17 | #include "customdisablediconengine.h" | 
|---|
| 18 |  | 
|---|
| 19 | #include <QImage> | 
|---|
| 20 | #include <QPainter> | 
|---|
| 21 | #include <QPixmap> | 
|---|
| 22 | #include <QDebug> | 
|---|
| 23 |  | 
|---|
| 24 | CustomDisabledIconEngine::CustomDisabledIconEngine() | 
|---|
| 25 | { | 
|---|
| 26 |  | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | void CustomDisabledIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State /*state*/) | 
|---|
| 30 | { | 
|---|
| 31 | painter->save(); | 
|---|
| 32 | painter->setClipRect(rect); | 
|---|
| 33 | QRect newRect = rect; | 
|---|
| 34 | QPixmap pixmap; | 
|---|
| 35 | if (mode == QIcon::Mode::Disabled) | 
|---|
| 36 | pixmap = mDisabledPixmap; | 
|---|
| 37 | else | 
|---|
| 38 | pixmap = mPixmap; | 
|---|
| 39 | if (pixmap.size().width() < rect.width()) { | 
|---|
| 40 | newRect.setLeft( rect.left()+(rect.width() - pixmap.size().width())/2); | 
|---|
| 41 | newRect.setWidth(pixmap.size().width()); | 
|---|
| 42 | } | 
|---|
| 43 | if (pixmap.size().height() < rect.height()) { | 
|---|
| 44 | newRect.setTop( rect.top()+(rect.height() - pixmap.size().height())/2); | 
|---|
| 45 | newRect.setHeight(pixmap.size().height()); | 
|---|
| 46 | } | 
|---|
| 47 | painter->drawPixmap(newRect,pixmap); | 
|---|
| 48 | painter->restore(); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | QIconEngine *CustomDisabledIconEngine::clone() const | 
|---|
| 52 | { | 
|---|
| 53 | CustomDisabledIconEngine* eng = new CustomDisabledIconEngine(); | 
|---|
| 54 | eng->mPixmap = mPixmap; | 
|---|
| 55 | eng->mDisabledPixmap = mDisabledPixmap; | 
|---|
| 56 | return eng; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | QPixmap CustomDisabledIconEngine::pixmap(const QSize &/*size*/, QIcon::Mode mode, QIcon::State /*state*/) | 
|---|
| 60 | { | 
|---|
| 61 | if (mode == QIcon::Mode::Disabled) | 
|---|
| 62 | return mDisabledPixmap; | 
|---|
| 63 | else | 
|---|
| 64 | return mPixmap; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void CustomDisabledIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode /*mode*/, QIcon::State /*state*/) | 
|---|
| 68 | { | 
|---|
| 69 | setPixmap(pixmap); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void CustomDisabledIconEngine::addFile(const QString &fileName, const QSize &/*size*/, QIcon::Mode /*mode*/, QIcon::State /*state*/) | 
|---|
| 73 | { | 
|---|
| 74 | setPixmap(QPixmap(fileName)); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void CustomDisabledIconEngine::setPixmap(const QPixmap &pixmap) | 
|---|
| 78 | { | 
|---|
| 79 | mPixmap = pixmap; | 
|---|
| 80 | if (pixmap.isNull()) | 
|---|
| 81 | mDisabledPixmap = pixmap; | 
|---|
| 82 | else { | 
|---|
| 83 | QImage oldImage = mPixmap.toImage(); | 
|---|
| 84 | QImage image(mPixmap.size(), QImage::Format_ARGB32); | 
|---|
| 85 | for (int x=0;x<image.width();x++) { | 
|---|
| 86 | for (int y=0;y<image.height();y++) { | 
|---|
| 87 | QColor c = oldImage.pixelColor(x,y); | 
|---|
| 88 | int gray = 0.299 * c.red() + 0.587 * c.green() + 0.114 * c.blue(); | 
|---|
| 89 | QColor c2(gray,gray,gray,c.alpha()); | 
|---|
| 90 | c2 = c2.darker(); | 
|---|
| 91 | image.setPixelColor(x,y,c2); | 
|---|
| 92 | } | 
|---|
| 93 | } | 
|---|
| 94 | mDisabledPixmap = QPixmap::fromImage(image); | 
|---|
| 95 | mDisabledPixmap.setDevicePixelRatio(mPixmap.devicePixelRatioF()); | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|