File indexing completed on 2024-05-19 16:47:31

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
0006  * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 // clang-format on
0010 
0011 #include "MergeFileInfos.h"
0012 
0013 #include <QApplication>
0014 #include <QIcon>
0015 #include <QPainter>
0016 #include <QPixmap>
0017 #include <QStyle>
0018 
0019 namespace PixMapUtils {
0020 namespace {
0021     QPixmap* s_pm_dir = nullptr;
0022     QPixmap* s_pm_file = nullptr;
0023 
0024     QPixmap* pmNotThere;
0025     QPixmap* pmNew = nullptr;
0026     QPixmap* pmOld;
0027     QPixmap* pmMiddle;
0028 
0029     QPixmap* pmLink;
0030 
0031     QPixmap* pmDirLink;
0032     QPixmap* pmFileLink;
0033 
0034     QPixmap* pmNewLink;
0035     QPixmap* pmOldLink;
0036     QPixmap* pmMiddleLink;
0037 
0038     QPixmap* pmNewDir;
0039     QPixmap* pmMiddleDir;
0040     QPixmap* pmOldDir;
0041 
0042     QPixmap* pmNewDirLink;
0043     QPixmap* pmMiddleDirLink;
0044     QPixmap* pmOldDirLink;
0045 } // namespace
0046 
0047 QPixmap colorToPixmap(const QColor& inColor)
0048 {
0049     QPixmap pm(16, 16);
0050     QPainter p(&pm);
0051     p.setPen(Qt::black);
0052     p.setBrush(inColor);
0053     p.drawRect(0, 0, pm.width(), pm.height());
0054     return pm;
0055 }
0056 
0057 /*
0058     Copy pm2 onto pm1, but preserve the alpha value from pm1 where pm2 is transparent.
0059     Opactiy controls wheather or not pm1 will show through.
0060 */
0061 QPixmap pixCombiner(const QPixmap* pm1, const QPixmap* pm2, const qreal inOpacity = 1)
0062 {
0063     QImage img1 = pm1->toImage().convertToFormat(QImage::Format_ARGB32);
0064     QImage img2 = pm2->toImage().convertToFormat(QImage::Format_ARGB32);
0065     QPainter painter(&img1);
0066 
0067     painter.setOpacity(inOpacity);
0068     painter.drawImage(0, 0, img2);
0069     painter.end();
0070 
0071     return QPixmap::fromImage(img1);
0072 }
0073 
0074 void initPixmaps(const QColor& newest, const QColor& oldest, const QColor& middle, const QColor& notThere)
0075 {
0076     if(s_pm_dir == nullptr || s_pm_file == nullptr)
0077     {
0078 #include "xpm/file.xpm"
0079 #include "xpm/folder.xpm"
0080         const qint32 smallIcon = qApp->style()->pixelMetric(QStyle::PM_SmallIconSize);
0081         s_pm_dir = new QPixmap(QIcon::fromTheme(QStringLiteral("folder")).pixmap(smallIcon));
0082         if(s_pm_dir->size() != QSize(16, 16))
0083         {
0084             delete s_pm_dir;
0085             s_pm_dir = new QPixmap(folder_pm);
0086         }
0087         s_pm_file = new QPixmap(file_pm);
0088     }
0089 
0090     if(pmNew == nullptr)
0091     {
0092 #include "xpm/link_arrow.xpm"
0093 
0094         pmNotThere = new QPixmap;
0095         pmNew = new QPixmap;
0096         pmOld = new QPixmap;
0097         pmMiddle = new QPixmap;
0098 
0099         pmLink = new QPixmap(link_arrow);
0100 
0101         pmDirLink = new QPixmap;
0102         pmFileLink = new QPixmap;
0103 
0104         pmNewLink = new QPixmap;
0105         pmOldLink = new QPixmap;
0106         pmMiddleLink = new QPixmap;
0107 
0108         pmNewDir = new QPixmap;
0109         pmMiddleDir = new QPixmap;
0110         pmOldDir = new QPixmap;
0111 
0112         pmNewDirLink = new QPixmap;
0113         pmMiddleDirLink = new QPixmap;
0114         pmOldDirLink = new QPixmap;
0115     }
0116 
0117     *pmNotThere = colorToPixmap(notThere);
0118     *pmNew = colorToPixmap(newest);
0119     *pmOld = colorToPixmap(oldest);
0120     *pmMiddle = colorToPixmap(middle);
0121 
0122     *pmDirLink = pixCombiner(s_pm_dir, pmLink);
0123     *pmFileLink = pixCombiner(s_pm_file, pmLink);
0124 
0125     *pmNewLink = pixCombiner(pmNew, pmLink);
0126     *pmOldLink = pixCombiner(pmOld, pmLink);
0127     *pmMiddleLink = pixCombiner(pmMiddle, pmLink);
0128 
0129     *pmNewDir = pixCombiner(pmNew, s_pm_dir, 0.5);
0130     *pmMiddleDir = pixCombiner(pmMiddle, s_pm_dir, 0.5);
0131     *pmOldDir = pixCombiner(pmOld, s_pm_dir, 0.5);
0132 
0133     *pmNewDirLink = pixCombiner(pmNewDir, pmLink);
0134     *pmMiddleDirLink = pixCombiner(pmMiddleDir, pmLink);
0135     *pmOldDirLink = pixCombiner(pmOldDir, pmLink);
0136 }
0137 
0138 QPixmap getOnePixmap(e_Age eAge, bool bLink, bool bDir)
0139 {
0140     QPixmap* ageToPm[] = {pmNew, pmMiddle, pmOld, pmNotThere, s_pm_file};
0141     QPixmap* ageToPmLink[] = {pmNewLink, pmMiddleLink, pmOldLink, pmNotThere, pmFileLink};
0142     QPixmap* ageToPmDir[] = {pmNewDir, pmMiddleDir, pmOldDir, pmNotThere, s_pm_dir};
0143     QPixmap* ageToPmDirLink[] = {pmNewDirLink, pmMiddleDirLink, pmOldDirLink, pmNotThere, pmDirLink};
0144 
0145     QPixmap** ppPm = bDir ? (bLink ? ageToPmDirLink : ageToPmDir) : (bLink ? ageToPmLink : ageToPm);
0146 
0147     return *ppPm[eAge];
0148 }
0149 
0150 } // namespace PixMapUtils