#include "./syncthingicons.h" #include #include #include #include namespace Data { /*! * \brief Generates the SVG code for the Syncthing icon with the specified colors and status emblem. */ QByteArray makeSyncthingIcon(const GradientColor &gradientColor, StatusEmblem statusEmblem) { // clang-format off static const QString emblems[] = { QString(), QStringLiteral( "" "" "" "" ), QStringLiteral( "" "" "" "" "" ), QStringLiteral( "" "" "" "" "" ), QStringLiteral( "" "" "" "" "" ), QStringLiteral( "" "" ), QStringLiteral( "" "" "" "" ), QStringLiteral( "" "" "" "" ), }; const auto &emblemData = emblems[static_cast(statusEmblem)]; return (QStringLiteral( "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "") % (emblemData.isEmpty() ? QString() : emblemData) % QStringLiteral( "" )).toUtf8(); // clang-format on } /// \cond namespace Detail { template QPixmap renderSvgImage(const SourceType &source, const QSize &size, int margin) { QSvgRenderer renderer(source); QSize renderSize(renderer.defaultSize()); renderSize.scale(size.width() - margin, size.height() - margin, Qt::KeepAspectRatio); QRect renderBounds(QPoint(), size); if (renderSize.width() < renderBounds.width()) { const auto diff = (renderBounds.width() - renderSize.width()) / 2; renderBounds.setX(diff); renderBounds.setWidth(renderSize.width()); } if (renderSize.height() < renderBounds.height()) { const auto diff = (renderBounds.height() - renderSize.height()) / 2; renderBounds.setY(diff); renderBounds.setHeight(renderSize.height()); } QPixmap pm(size); pm.fill(QColor(Qt::transparent)); QPainter painter(&pm); renderer.render(&painter, renderBounds); return pm; } } // namespace Detail /// \endcond /*! * \brief Renders an SVG image to a QPixmap. * \remarks If instantiating QIcon directly from SVG image the icon is not displayed in the tray under Plasma 5. It works * with Tint2, however. */ QPixmap renderSvgImage(const QString &path, const QSize &size, int margin) { return Detail::renderSvgImage(path, size, margin); } /*! * \brief Renders an SVG image to a QPixmap. */ QPixmap renderSvgImage(const QByteArray &contents, const QSize &size, int margin) { return Detail::renderSvgImage(contents, size, margin); } /*! * \brief Returns the font awesome icon with the specified \a iconName and \a color. */ QByteArray loadFontAwesomeIcon(const QString &iconName, const QColor &color, bool solid) { QByteArray result; QFile icon((solid ? QStringLiteral(":/icons/hicolor/scalable/fa/") : QStringLiteral(":/icons/hicolor/scalable/fa-non-solid/")) % iconName % QStringLiteral(".svg")); if (!icon.open(QFile::ReadOnly)) { return result; } result = icon.readAll(); const auto pathBegin = result.indexOf(" 0) { result.insert(pathBegin + 6, QStringLiteral("fill=\"") % color.name(QColor::HexRgb) % QStringLiteral("\" ")); } return result; } StatusIconSettings::StatusIconSettings() : defaultColor({ QStringLiteral("#26B6DB"), QStringLiteral("#0882C8") }) , errorColor({ QStringLiteral("#DB3C26"), QStringLiteral("#C80828") }) , warningColor({ QStringLiteral("#c9ce3b"), QStringLiteral("#ebb83b") }) , idleColor({ QStringLiteral("#2D9D69"), QStringLiteral("#2D9D69") }) , disconnectedColor({ QStringLiteral("#A9A9A9"), QStringLiteral("#58656C") }) { } StatusIcons::StatusIcons(const StatusIconSettings &settings) : disconnected(QIcon(renderSvgImage(makeSyncthingIcon(settings.disconnectedColor, StatusEmblem::None)))) , idling(QIcon(renderSvgImage(makeSyncthingIcon(settings.defaultColor, StatusEmblem::None)))) , scanninig(QIcon(renderSvgImage(makeSyncthingIcon(settings.defaultColor, StatusEmblem::Scanning)))) , notify(QIcon(renderSvgImage(makeSyncthingIcon(settings.warningColor, StatusEmblem::Alert)))) , pause(QIcon(renderSvgImage(makeSyncthingIcon(settings.defaultColor, StatusEmblem::Paused)))) , sync(QIcon(renderSvgImage(makeSyncthingIcon(settings.defaultColor, StatusEmblem::Synchronizing)))) , syncComplete(QIcon(renderSvgImage(makeSyncthingIcon(settings.defaultColor, StatusEmblem::Complete)))) , error(QIcon(renderSvgImage(makeSyncthingIcon(settings.errorColor, StatusEmblem::Alert)))) , errorSync(QIcon(renderSvgImage(makeSyncthingIcon(settings.errorColor, StatusEmblem::Synchronizing)))) , newItem(QIcon(renderSvgImage(makeSyncthingIcon(settings.defaultColor, StatusEmblem::Add)))) { } FontAwesomeIcons::FontAwesomeIcons(const QColor &color, const QSize &size, int margin) : hashtag(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("hashtag"), color), size, margin)) , folderOpen(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("folder-open"), color), size, margin)) , globe(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("globe"), color), size, margin)) , home(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("home"), color), size, margin)) , shareAlt(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("share-alt"), color), size, margin)) , refresh(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("redo"), color), size, margin)) , clock(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("clock"), color), size, margin)) , exchangeAlt(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("exchange-alt"), color), size, margin)) , exclamationTriangle(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("exclamation-triangle"), color), size, margin)) , cogs(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("cogs"), color), size, margin)) , link(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("link"), color), size, margin)) , eye(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("eye"), color), size, margin)) , fileArchive(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("file-archive"), color), size, margin)) , folder(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("folder"), color), size, margin)) , certificate(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("certificate"), color), size, margin)) , networkWired(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("network-wired"), color), size, margin)) , cloudDownloadAlt(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("cloud-download-alt"), color), size, margin)) , cloudUploadAlt(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("cloud-upload-alt"), color), size, margin)) , tag(renderSvgImage(loadFontAwesomeIcon(QStringLiteral("tag"), color), size, margin)) { } IconManager::IconManager(const StatusIconSettings *settings) : m_statusIcons(settings ? *settings : StatusIconSettings()) , m_fontAwesomeIconsForLightTheme(QColor(10, 10, 10), QSize(64, 64), 8) , m_fontAwesomeIconsForDarkTheme(Qt::white, QSize(64, 64), 8) { } IconManager &IconManager::instance(const StatusIconSettings *settingsForFirstTimeSetup) { static IconManager iconManager(settingsForFirstTimeSetup); return iconManager; } } // namespace Data