File indexing completed on 2025-04-27 03:58:31

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2014-09-12
0007  * Description : A label with an active url
0008  *
0009  * SPDX-FileCopyrightText: 2014-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "dactivelabel.h"
0016 
0017 // Qt includes
0018 
0019 #include <QByteArray>
0020 #include <QBuffer>
0021 
0022 namespace Digikam
0023 {
0024 
0025 DActiveLabel::DActiveLabel(const QUrl& url, const QString& imgPath, QWidget* const parent)
0026     : QLabel(parent)
0027 {
0028     setContentsMargins(QMargins());
0029     setScaledContents(false);
0030     setOpenExternalLinks(true);
0031     setTextFormat(Qt::RichText);
0032     setFocusPolicy(Qt::NoFocus);
0033     setTextInteractionFlags(Qt::LinksAccessibleByMouse);
0034     setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
0035 
0036     if (imgPath.isEmpty())
0037     {
0038         updateData(url, QImage());
0039     }
0040     else
0041     {
0042         updateData(url, QImage(imgPath));
0043     }
0044 }
0045 
0046 DActiveLabel::~DActiveLabel()
0047 {
0048 }
0049 
0050 void DActiveLabel::updateData(const QUrl& url, const QImage& img)
0051 {
0052     QByteArray byteArray;
0053     QBuffer    buffer(&byteArray);
0054     img.save(&buffer, "PNG");
0055     setText(QString::fromLatin1("<a href=\"%1\">%2</a>")
0056             .arg(url.url())
0057             .arg(QString::fromLatin1("<img src=\"data:image/png;base64,%1\">")
0058             .arg(QString::fromLatin1(byteArray.toBase64().data()))));
0059 }
0060 
0061 } // namespace Digikam
0062 
0063 #include "moc_dactivelabel.cpp"