1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2009-09-14
 * Description : a class to build the tooltip for a renameparser and its options
 *
 * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "tooltipcreator.h"

// Qt includes

#include <QRegularExpression>
#include <QPalette>
#include <QApplication>
#include <QIcon>
#include <QImage>
#include <QBuffer>
#include <QByteArray>


// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "modifier.h"
#include "option.h"
#include "parser.h"

namespace Digikam
{

TooltipCreator& TooltipCreator::getInstance()
{
    static TooltipCreator m_instance;
    return m_instance;
}

QString TooltipCreator::additionalInformation()
{
    QStringList infoItems;
    infoItems << i18n("To apply a modifier, append it to the option, for instance: [file]{upper}");
    infoItems << i18n("Modifiers can be applied to every renaming option.");
    infoItems << i18n("It is possible to assign multiple modifiers to an option, "
                      "they are applied in the order you assign them.");
    infoItems << i18n("Be sure to use the quick access buttons: They might provide "
                      "additional information about renaming and modifier options.");
    infoItems << i18n("The file list can be sorted, just right-click on it to see the sort criteria (album UI only).");

    QString information;
    information += QString::fromUtf8("<div style='margin-top:20px;'");

    information += tableStart(90);
    information += QString::fromUtf8("<tr><td style='vertical-align:top;'>") + getInfoIconBase64() + QString::fromUtf8("</td>");
    information += QString::fromUtf8("<td><ol>");

    for (const QString& infoItem : std::as_const(infoItems))
    {
        // cppcheck-suppress useStlAlgorithm
        information += QString::fromUtf8("<li>") + infoItem + QString::fromUtf8("</li>");

    }

    information += QString::fromUtf8("</ol></td></tr>");
    information += tableEnd();

    information += QString::fromUtf8("</div>");

    return information;
}

QString TooltipCreator::getInfoIconBase64() const
{
    QIcon info = getInfoIcon();
    QImage img = info.pixmap(QSize(48, 48)).toImage();
    QByteArray byteArray;
    QBuffer    buffer(&byteArray);
    img.save(&buffer, "PNG");

    return (QString::fromLatin1("<img src=\"data:image/png;base64,%1\">")
            .arg(QString::fromLatin1(byteArray.toBase64().data())));
}

QIcon TooltipCreator::getInfoIcon() const
{
    return QIcon::fromTheme(QLatin1String("dialog-information"));
}

QString TooltipCreator::tooltip(const Parser* const parser)
{
    if (!parser)
    {
        return QString();
    }

    QString tooltip;
    tooltip += QString::fromUtf8("<html><head><title></title></head>");
    tooltip += QString::fromUtf8("<body>");

    tooltip += tableStart();
    tooltip += createSection(i18n("Options"),   parser->options());
    tooltip += createSection(i18n("Modifiers"), parser->modifiers(), true);
    tooltip += tableEnd();

    if (!parser->modifiers().isEmpty())
    {
        tooltip += additionalInformation();
    }

    tooltip += QString::fromUtf8("</body>");
    tooltip += QString::fromUtf8("</html>");

    return tooltip;
}

QString TooltipCreator::tableStart(int widthPercentage)
{
    QString w = QString::number(widthPercentage) + QLatin1Char('%');

    return QString::fromUtf8("<table width=\"%1\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">").arg(w);
}

QString TooltipCreator::tableStart()
{
    return tableStart(100);
}

QString TooltipCreator::tableEnd()
{
    return QString::fromUtf8("</table>");
}

QString TooltipCreator::markOption(const QString& str)
{
    QString result = str;

    QRegularExpression optionsRegExp(QLatin1String("\\|\\|(.*)\\|\\|"));
    optionsRegExp.setPatternOptions(QRegularExpression::InvertedGreedinessOption);

    result.replace(optionsRegExp, QString::fromUtf8("<i><font color=\"%1\">\\1</font></i>")
                   .arg(qApp->palette().color(QPalette::Link).name()));
    return result;
}

QString TooltipCreator::createHeader(const QString& str)
{
    QString result;
    QString templateStr = QString::fromUtf8("<tr><td style=\"background-color: %1; padding:0.25em;\" colspan=\"2\">"
                                  "<nobr><font color=\"%2\"><center><b>%3"
                                  "</b></center></font></nobr></td></tr>")
                          .arg(qApp->palette().color(QPalette::Highlight).name())
                          .arg(qApp->palette().color(QPalette::HighlightedText).name());

    result += templateStr.arg(str);

    return result;
}

QString TooltipCreator::createEntries(const RulesList& data)
{
    QString result;

    for (Rule* const t : std::as_const(data))<--- Variable 't' can be declared as pointer to const
    {
        for (Token* const token : std::as_const(t->tokens()))<--- Variable 'token' can be declared as pointer to const
        {
            // cppcheck-suppress useStlAlgorithm
            result += QString::fromUtf8("<tr>"
                              "<td style=\"background-color: %1;\">"
                              "<font color=\"%2\"><b>&nbsp;%3&nbsp;</b></font></td>"
                              "<td>&nbsp;%4&nbsp;</td></tr>")
                      .arg(qApp->palette().color(QPalette::Base).name())
                      .arg(qApp->palette().color(QPalette::Text).name())
                      .arg(markOption(token->id()))
                      .arg(markOption(token->description()));
        }
    }

    return result;
}

QString TooltipCreator::createSection(const QString& sectionName, const RulesList& data, bool lastSection)
{
    if (data.isEmpty())
    {
        return QString();
    }

    QString result;

    result += createHeader(sectionName);
    result += createEntries(data);

    if (!lastSection)
    {
        result += QString::fromUtf8("<tr></tr>");
    }

    return result;
}

} // namespace Digikam