2014-09-18 21:09:55 +00:00
|
|
|
/* stock_icon.cpp
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-04-30 07:47:58 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2014-09-18 21:09:55 +00:00
|
|
|
|
2017-07-25 14:15:18 +00:00
|
|
|
#include <ui/qt/utils/stock_icon.h>
|
2014-09-18 21:09:55 +00:00
|
|
|
|
|
|
|
// Stock icons. Based on gtk/stock_icons.h
|
|
|
|
|
|
|
|
// Toolbar icon sizes:
|
2017-04-05 19:15:27 +00:00
|
|
|
// macOS freestanding: 32x32, 32x32@2x, segmented (inside a button): <= 19x19
|
2014-09-18 21:09:55 +00:00
|
|
|
// Windows: 16x16, 24x24, 32x32
|
|
|
|
// GNOME: 24x24 (default), 48x48
|
|
|
|
|
|
|
|
// References:
|
|
|
|
//
|
2019-07-27 07:54:44 +00:00
|
|
|
// https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
|
|
|
|
// https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
|
2014-09-18 21:09:55 +00:00
|
|
|
//
|
2019-07-27 07:54:44 +00:00
|
|
|
// https://mithatkonar.com/wiki/doku.php/qt/icons
|
2014-09-18 21:09:55 +00:00
|
|
|
//
|
2019-07-27 07:54:44 +00:00
|
|
|
// https://web.archive.org/web/20140829010224/https://developer.apple.com/library/mac/documentation/userexperience/conceptual/applehiguidelines/IconsImages/IconsImages.html
|
|
|
|
// https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/image-size-and-resolution/
|
|
|
|
// https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon/
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/uxguide/vis-icons
|
|
|
|
// https://developer.gnome.org/hig/stable/icons-and-artwork.html.en
|
|
|
|
// https://docs.microsoft.com/en-us/visualstudio/designers/the-visual-studio-image-library
|
2014-09-18 21:09:55 +00:00
|
|
|
|
|
|
|
// To do:
|
2019-06-13 17:25:19 +00:00
|
|
|
// - 32x32, 48x48, 64x64, and unscaled (.svg) icons.
|
2014-09-18 21:09:55 +00:00
|
|
|
// - Indent find & go actions when those panes are open.
|
|
|
|
// - Replace or remove:
|
|
|
|
// WIRESHARK_STOCK_CAPTURE_FILTER x-capture-filter
|
|
|
|
// WIRESHARK_STOCK_DISPLAY_FILTER x-display-filter
|
|
|
|
// GTK_STOCK_SELECT_COLOR x-coloring-rules
|
|
|
|
// GTK_STOCK_PREFERENCES preferences-system
|
|
|
|
// GTK_STOCK_HELP help-contents
|
|
|
|
|
2021-12-23 01:02:50 +00:00
|
|
|
#include <QApplication>
|
2014-09-18 21:09:55 +00:00
|
|
|
#include <QFile>
|
2015-07-10 23:15:55 +00:00
|
|
|
#include <QFontMetrics>
|
2015-09-22 16:08:39 +00:00
|
|
|
#include <QMap>
|
2015-07-10 23:15:55 +00:00
|
|
|
#include <QPainter>
|
2021-04-07 05:40:09 +00:00
|
|
|
#include <QPainterPath>
|
2014-09-18 21:09:55 +00:00
|
|
|
#include <QStyle>
|
2019-06-27 15:57:33 +00:00
|
|
|
#include <QStyleOption>
|
2014-09-18 21:09:55 +00:00
|
|
|
|
2017-12-21 23:06:20 +00:00
|
|
|
static const QString path_pfx_ = ":/stock_icons/";
|
2015-09-22 16:08:39 +00:00
|
|
|
|
|
|
|
// Map FreeDesktop icon names to Qt standard pixmaps.
|
|
|
|
static QMap<QString, QStyle::StandardPixmap> icon_name_to_standard_pixmap_;
|
2014-09-18 21:09:55 +00:00
|
|
|
|
2015-09-02 21:51:33 +00:00
|
|
|
StockIcon::StockIcon(const QString icon_name) :
|
2014-09-18 21:09:55 +00:00
|
|
|
QIcon()
|
|
|
|
{
|
2015-09-22 16:08:39 +00:00
|
|
|
if (icon_name_to_standard_pixmap_.isEmpty()) {
|
|
|
|
fillIconNameMap();
|
2014-09-18 21:09:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 16:08:39 +00:00
|
|
|
// Does our theme contain this icon?
|
|
|
|
// X11 only as per the QIcon documentation.
|
2014-09-18 21:09:55 +00:00
|
|
|
if (hasThemeIcon(icon_name)) {
|
|
|
|
QIcon theme_icon = fromTheme(icon_name);
|
|
|
|
swap(theme_icon);
|
|
|
|
return;
|
2015-09-22 16:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is this is an icon we've manually mapped to a standard pixmap below?
|
|
|
|
if (icon_name_to_standard_pixmap_.contains(icon_name)) {
|
2021-12-23 01:02:50 +00:00
|
|
|
QIcon standard_icon = qApp->style()->standardIcon(icon_name_to_standard_pixmap_[icon_name]);
|
2015-09-22 16:08:39 +00:00
|
|
|
swap(standard_icon);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this one of our locally sourced, cage-free, organic icons?
|
2019-06-16 18:07:13 +00:00
|
|
|
QStringList types = QStringList() << "8x8" << "14x14" << "16x16" << "24x14" << "24x24";
|
2019-06-27 15:57:33 +00:00
|
|
|
QList<QIcon::Mode> icon_modes = QList<QIcon::Mode>()
|
|
|
|
<< QIcon::Disabled
|
|
|
|
<< QIcon::Active
|
|
|
|
<< QIcon::Selected;
|
2015-09-22 16:08:39 +00:00
|
|
|
foreach (QString type, types) {
|
2019-06-13 17:25:19 +00:00
|
|
|
// First, check for a template (mask) icon
|
|
|
|
// Templates should be monochrome as described at
|
|
|
|
// https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/custom-icons/
|
|
|
|
// Transparency is supported.
|
|
|
|
QString icon_path_template = path_pfx_ + QString("%1/%2.template.png").arg(type).arg(icon_name);
|
|
|
|
if (QFile::exists(icon_path_template)) {
|
|
|
|
QIcon mask_icon = QIcon();
|
|
|
|
mask_icon.addFile(icon_path_template);
|
|
|
|
|
|
|
|
foreach(QSize sz, mask_icon.availableSizes()) {
|
|
|
|
QPixmap mask_pm = mask_icon.pixmap(sz);
|
2019-06-27 15:57:33 +00:00
|
|
|
QImage normal_img(sz, QImage::Format_ARGB32);
|
|
|
|
QPainter painter(&normal_img);
|
2021-12-23 01:02:50 +00:00
|
|
|
QBrush br(qApp->palette().color(QPalette::Active, QPalette::WindowText));
|
2019-06-27 15:57:33 +00:00
|
|
|
painter.fillRect(0, 0, sz.width(), sz.height(), br);
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
painter.drawPixmap(0, 0, mask_pm);
|
|
|
|
|
|
|
|
QPixmap normal_pm = QPixmap::fromImage(normal_img);
|
|
|
|
addPixmap(normal_pm, QIcon::Normal, QIcon::On);
|
|
|
|
addPixmap(normal_pm, QIcon::Normal, QIcon::Off);
|
|
|
|
|
|
|
|
QStyleOption opt = {};
|
2021-12-23 01:02:50 +00:00
|
|
|
opt.palette = qApp->palette();
|
2019-06-27 15:57:33 +00:00
|
|
|
foreach (QIcon::Mode icon_mode, icon_modes) {
|
2021-12-23 01:02:50 +00:00
|
|
|
QPixmap mode_pm = qApp->style()->generatedIconPixmap(icon_mode, normal_pm, &opt);
|
2019-06-27 15:57:33 +00:00
|
|
|
addPixmap(mode_pm, icon_mode, QIcon::On);
|
|
|
|
addPixmap(mode_pm, icon_mode, QIcon::Off);
|
2019-06-13 17:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular full-color icons
|
2015-09-22 16:08:39 +00:00
|
|
|
QString icon_path = path_pfx_ + QString("%1/%2.png").arg(type).arg(icon_name);
|
|
|
|
if (QFile::exists(icon_path)) {
|
|
|
|
addFile(icon_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Along with each name check for "<name>.active" and
|
|
|
|
// "<name>.selected" for the Active and Selected modes, and
|
|
|
|
// "<name>.on" to use for the on (checked) state.
|
|
|
|
// XXX Allow more (or all) combinations.
|
|
|
|
QString icon_path_active = path_pfx_ + QString("%1/%2.active.png").arg(type).arg(icon_name);
|
|
|
|
if (QFile::exists(icon_path_active)) {
|
|
|
|
addFile(icon_path_active, QSize(), QIcon::Active, QIcon::On);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString icon_path_selected = path_pfx_ + QString("%1/%2.selected.png").arg(type).arg(icon_name);
|
|
|
|
if (QFile::exists(icon_path_selected)) {
|
|
|
|
addFile(icon_path_selected, QSize(), QIcon::Selected, QIcon::On);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString icon_path_on = path_pfx_ + QString("%1/%2.on.png").arg(type).arg(icon_name);
|
|
|
|
if (QFile::exists(icon_path_on)) {
|
|
|
|
addFile(icon_path_on, QSize(), QIcon::Normal, QIcon::On);
|
2014-09-18 21:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 23:15:55 +00:00
|
|
|
// Create a square icon filled with the specified color.
|
|
|
|
QIcon StockIcon::colorIcon(const QRgb bg_color, const QRgb fg_color, const QString glyph)
|
|
|
|
{
|
|
|
|
QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
|
|
|
|
QIcon color_icon;
|
|
|
|
|
|
|
|
foreach (int size, sizes) {
|
|
|
|
QPixmap pm(size, size);
|
|
|
|
QPainter painter(&pm);
|
|
|
|
QRect border(0, 0, size - 1, size - 1);
|
|
|
|
painter.setPen(fg_color);
|
|
|
|
painter.setBrush(QColor(bg_color));
|
|
|
|
painter.drawRect(border);
|
|
|
|
|
|
|
|
if (!glyph.isEmpty()) {
|
2021-12-23 01:02:50 +00:00
|
|
|
QFont font(qApp->font());
|
2015-07-11 01:42:21 +00:00
|
|
|
font.setPointSizeF(size / 2.0);
|
|
|
|
painter.setFont(font);
|
2015-07-10 23:15:55 +00:00
|
|
|
QRectF bounding = painter.boundingRect(pm.rect(), glyph, Qt::AlignHCenter | Qt::AlignVCenter);
|
|
|
|
painter.drawText(bounding, glyph);
|
|
|
|
}
|
|
|
|
|
|
|
|
color_icon.addPixmap(pm);
|
|
|
|
}
|
|
|
|
return color_icon;
|
|
|
|
}
|
|
|
|
|
2021-04-07 05:40:09 +00:00
|
|
|
// Create a triangle icon filled with the specified color.
|
|
|
|
QIcon StockIcon::colorIconTriangle(const QRgb bg_color, const QRgb fg_color)
|
|
|
|
{
|
|
|
|
QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
|
|
|
|
QIcon color_icon;
|
|
|
|
|
|
|
|
foreach (int size, sizes) {
|
|
|
|
QPixmap pm(size, size);
|
|
|
|
QPainter painter(&pm);
|
|
|
|
QPainterPath triangle;
|
|
|
|
pm.fill();
|
|
|
|
painter.fillRect(0, 0, size-1, size-1, Qt::transparent);
|
|
|
|
painter.setPen(fg_color);
|
|
|
|
painter.setBrush(QColor(bg_color));
|
|
|
|
triangle.moveTo(0, size-1);
|
|
|
|
triangle.lineTo(size-1, size-1);
|
|
|
|
triangle.lineTo((size-1)/2, 0);
|
|
|
|
triangle.closeSubpath();
|
|
|
|
painter.fillPath(triangle, QColor(bg_color));
|
|
|
|
|
|
|
|
color_icon.addPixmap(pm);
|
|
|
|
}
|
|
|
|
return color_icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a cross icon filled with the specified color.
|
|
|
|
QIcon StockIcon::colorIconCross(const QRgb bg_color, const QRgb fg_color)
|
|
|
|
{
|
|
|
|
QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
|
|
|
|
QIcon color_icon;
|
|
|
|
|
|
|
|
foreach (int size, sizes) {
|
|
|
|
QPixmap pm(size, size);
|
|
|
|
QPainter painter(&pm);
|
|
|
|
QPainterPath cross;
|
|
|
|
pm.fill();
|
|
|
|
painter.fillRect(0, 0, size-1, size-1, Qt::transparent);
|
|
|
|
painter.setPen(QPen(QBrush(bg_color), 3));
|
|
|
|
painter.setBrush(QColor(fg_color));
|
|
|
|
cross.moveTo(0, 0);
|
|
|
|
cross.lineTo(size-1, size-1);
|
|
|
|
cross.moveTo(0, size-1);
|
|
|
|
cross.lineTo(size-1, 0);
|
|
|
|
painter.drawPath(cross);
|
|
|
|
|
|
|
|
color_icon.addPixmap(pm);
|
|
|
|
}
|
|
|
|
return color_icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a circle icon filled with the specified color.
|
|
|
|
QIcon StockIcon::colorIconCircle(const QRgb bg_color, const QRgb fg_color)
|
|
|
|
{
|
|
|
|
QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
|
|
|
|
QIcon color_icon;
|
|
|
|
|
|
|
|
foreach (int size, sizes) {
|
|
|
|
QPixmap pm(size, size);
|
|
|
|
QPainter painter(&pm);
|
|
|
|
QRect border(2, 2, size - 3, size - 3);
|
|
|
|
pm.fill();
|
|
|
|
painter.fillRect(0, 0, size-1, size-1, Qt::transparent);
|
|
|
|
painter.setPen(QPen(QBrush(bg_color), 3));
|
|
|
|
painter.setBrush(QColor(fg_color));
|
|
|
|
painter.setBrush(QColor(bg_color));
|
|
|
|
painter.drawEllipse(border);
|
|
|
|
|
|
|
|
color_icon.addPixmap(pm);
|
|
|
|
}
|
|
|
|
return color_icon;
|
|
|
|
}
|
|
|
|
|
2015-09-22 16:08:39 +00:00
|
|
|
void StockIcon::fillIconNameMap()
|
|
|
|
{
|
|
|
|
// Note that some of Qt's standard pixmaps are awful. We shouldn't add an
|
|
|
|
// entry just because a match can be made.
|
|
|
|
icon_name_to_standard_pixmap_["document-open"] = QStyle::SP_DirIcon;
|
|
|
|
icon_name_to_standard_pixmap_["media-playback-pause"] = QStyle::SP_MediaPause;
|
|
|
|
icon_name_to_standard_pixmap_["media-playback-start"] = QStyle::SP_MediaPlay;
|
|
|
|
icon_name_to_standard_pixmap_["media-playback-stop"] = QStyle::SP_MediaStop;
|
|
|
|
}
|