File indexing completed on 2025-03-09 03:57:05
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2009-09-14 0007 * Description : a class to build the tooltip for a renameparser and its options 0008 * 0009 * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "tooltipcreator.h" 0016 0017 // Qt includes 0018 0019 #include <QRegularExpression> 0020 #include <QPalette> 0021 #include <QApplication> 0022 #include <QIcon> 0023 #include <QImage> 0024 #include <QBuffer> 0025 #include <QByteArray> 0026 0027 0028 // KDE includes 0029 0030 #include <klocalizedstring.h> 0031 0032 // Local includes 0033 0034 #include "modifier.h" 0035 #include "option.h" 0036 #include "parser.h" 0037 0038 namespace Digikam 0039 { 0040 0041 TooltipCreator& TooltipCreator::getInstance() 0042 { 0043 static TooltipCreator m_instance; 0044 return m_instance; 0045 } 0046 0047 QString TooltipCreator::additionalInformation() 0048 { 0049 QStringList infoItems; 0050 infoItems << i18n("To apply a modifier, append it to the option, for instance: [file]{upper}"); 0051 infoItems << i18n("Modifiers can be applied to every renaming option."); 0052 infoItems << i18n("It is possible to assign multiple modifiers to an option, " 0053 "they are applied in the order you assign them."); 0054 infoItems << i18n("Be sure to use the quick access buttons: They might provide " 0055 "additional information about renaming and modifier options."); 0056 infoItems << i18n("The file list can be sorted, just right-click on it to see the sort criteria (album UI only)."); 0057 0058 QString information; 0059 information += QString::fromUtf8("<div style='margin-top:20px;'"); 0060 0061 information += tableStart(90); 0062 information += QString::fromUtf8("<tr><td style='vertical-align:top;'>") + getInfoIconBase64() + QString::fromUtf8("</td>"); 0063 information += QString::fromUtf8("<td><ol>"); 0064 0065 Q_FOREACH (const QString& infoItem, infoItems) 0066 { 0067 // cppcheck-suppress useStlAlgorithm 0068 information += QString::fromUtf8("<li>") + infoItem + QString::fromUtf8("</li>"); 0069 0070 } 0071 0072 information += QString::fromUtf8("</ol></td></tr>"); 0073 information += tableEnd(); 0074 0075 information += QString::fromUtf8("</div>"); 0076 0077 return information; 0078 } 0079 0080 QString TooltipCreator::getInfoIconBase64() const 0081 { 0082 QIcon info = getInfoIcon(); 0083 QImage img = info.pixmap(QSize(48, 48)).toImage(); 0084 QByteArray byteArray; 0085 QBuffer buffer(&byteArray); 0086 img.save(&buffer, "PNG"); 0087 0088 return (QString::fromLatin1("<img src=\"data:image/png;base64,%1\">").arg(QString::fromLatin1(byteArray.toBase64().data()))); 0089 } 0090 0091 QIcon TooltipCreator::getInfoIcon() const 0092 { 0093 return QIcon::fromTheme(QLatin1String("dialog-information")); // image-stack-open 0094 } 0095 0096 QString TooltipCreator::tooltip(Parser* parser) 0097 { 0098 if (!parser) 0099 { 0100 return QString(); 0101 } 0102 0103 QString tooltip; 0104 tooltip += QString::fromUtf8("<html><head><title></title></head>"); 0105 tooltip += QString::fromUtf8("<body>"); 0106 0107 tooltip += tableStart(); 0108 tooltip += createSection(i18n("Options"), parser->options()); 0109 tooltip += createSection(i18n("Modifiers"), parser->modifiers(), true); 0110 tooltip += tableEnd(); 0111 0112 if (!parser->modifiers().isEmpty()) 0113 { 0114 tooltip += additionalInformation(); 0115 } 0116 0117 tooltip += QString::fromUtf8("</body>"); 0118 tooltip += QString::fromUtf8("</html>"); 0119 0120 return tooltip; 0121 } 0122 0123 QString TooltipCreator::tableStart(int widthPercentage) 0124 { 0125 QString w = QString::number(widthPercentage) + QLatin1Char('%'); 0126 0127 return QString::fromUtf8("<table width=\"%1\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">").arg(w); 0128 } 0129 0130 QString TooltipCreator::tableStart() 0131 { 0132 return tableStart(100); 0133 } 0134 0135 QString TooltipCreator::tableEnd() 0136 { 0137 return QString::fromUtf8("</table>"); 0138 } 0139 0140 QString TooltipCreator::markOption(const QString& str) 0141 { 0142 QString result = str; 0143 0144 QRegularExpression optionsRegExp(QLatin1String("\\|\\|(.*)\\|\\|")); 0145 optionsRegExp.setPatternOptions(QRegularExpression::InvertedGreedinessOption); 0146 0147 result.replace(optionsRegExp, QString::fromUtf8("<i><font color=\"%1\">\\1</font></i>") 0148 .arg(qApp->palette().color(QPalette::Link).name())); 0149 return result; 0150 } 0151 0152 QString TooltipCreator::createHeader(const QString& str) 0153 { 0154 QString result; 0155 QString templateStr = QString::fromUtf8("<tr><td style=\"background-color: %1; padding:0.25em;\" colspan=\"2\">" 0156 "<nobr><font color=\"%2\"><center><b>%3" 0157 "</b></center></font></nobr></td></tr>") 0158 .arg(qApp->palette().color(QPalette::Highlight).name()) 0159 .arg(qApp->palette().color(QPalette::HighlightedText).name()); 0160 0161 result += templateStr.arg(str); 0162 0163 return result; 0164 } 0165 0166 QString TooltipCreator::createEntries(const RulesList& data) 0167 { 0168 QString result; 0169 0170 Q_FOREACH (Rule* const t, data) 0171 { 0172 Q_FOREACH (Token* const token, t->tokens()) 0173 { 0174 // cppcheck-suppress useStlAlgorithm 0175 result += QString::fromUtf8("<tr>" 0176 "<td style=\"background-color: %1;\">" 0177 "<font color=\"%2\"><b> %3 </b></font></td>" 0178 "<td> %4 </td></tr>") 0179 .arg(qApp->palette().color(QPalette::Base).name()) 0180 .arg(qApp->palette().color(QPalette::Text).name()) 0181 .arg(markOption(token->id())) 0182 .arg(markOption(token->description())); 0183 } 0184 } 0185 0186 return result; 0187 } 0188 0189 QString TooltipCreator::createSection(const QString& sectionName, const RulesList& data, bool lastSection) 0190 { 0191 if (data.isEmpty()) 0192 { 0193 return QString(); 0194 } 0195 0196 QString result; 0197 0198 result += createHeader(sectionName); 0199 result += createEntries(data); 0200 0201 if (!lastSection) 0202 { 0203 result += QString::fromUtf8("<tr></tr>"); 0204 } 0205 0206 return result; 0207 } 0208 0209 } // namespace Digikam