PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ui/qt/utils/stock_icon.cpp

https://gitlab.com/jvelando/wireshark
C++ | 253 lines | 165 code | 33 blank | 55 comment | 9 complexity | 93395e7edf53c7a1a9c78acefed2b2a7 MD5 | raw file
  1. /* stock_icon.cpp
  2. *
  3. * Wireshark - Network traffic analyzer
  4. * By Gerald Combs <gerald@wireshark.org>
  5. * Copyright 1998 Gerald Combs
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. */
  9. #include <ui/qt/utils/stock_icon.h>
  10. // Stock icons. Based on gtk/stock_icons.h
  11. // Toolbar icon sizes:
  12. // macOS freestanding: 32x32, 32x32@2x, segmented (inside a button): <= 19x19
  13. // Windows: 16x16, 24x24, 32x32
  14. // GNOME: 24x24 (default), 48x48
  15. // References:
  16. //
  17. // https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
  18. // https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
  19. //
  20. // https://mithatkonar.com/wiki/doku.php/qt/icons
  21. //
  22. // https://web.archive.org/web/20140829010224/https://developer.apple.com/library/mac/documentation/userexperience/conceptual/applehiguidelines/IconsImages/IconsImages.html
  23. // https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/image-size-and-resolution/
  24. // https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon/
  25. // https://docs.microsoft.com/en-us/windows/win32/uxguide/vis-icons
  26. // https://developer.gnome.org/hig/stable/icons-and-artwork.html.en
  27. // https://docs.microsoft.com/en-us/visualstudio/designers/the-visual-studio-image-library
  28. // To do:
  29. // - 32x32, 48x48, 64x64, and unscaled (.svg) icons.
  30. // - Indent find & go actions when those panes are open.
  31. // - Replace or remove:
  32. // WIRESHARK_STOCK_CAPTURE_FILTER x-capture-filter
  33. // WIRESHARK_STOCK_DISPLAY_FILTER x-display-filter
  34. // GTK_STOCK_SELECT_COLOR x-coloring-rules
  35. // GTK_STOCK_PREFERENCES preferences-system
  36. // GTK_STOCK_HELP help-contents
  37. #include <QApplication>
  38. #include <QFile>
  39. #include <QFontMetrics>
  40. #include <QMap>
  41. #include <QPainter>
  42. #include <QPainterPath>
  43. #include <QStyle>
  44. #include <QStyleOption>
  45. static const QString path_pfx_ = ":/stock_icons/";
  46. // Map FreeDesktop icon names to Qt standard pixmaps.
  47. static QMap<QString, QStyle::StandardPixmap> icon_name_to_standard_pixmap_;
  48. StockIcon::StockIcon(const QString icon_name) :
  49. QIcon()
  50. {
  51. if (icon_name_to_standard_pixmap_.isEmpty()) {
  52. fillIconNameMap();
  53. }
  54. // Does our theme contain this icon?
  55. // X11 only as per the QIcon documentation.
  56. if (hasThemeIcon(icon_name)) {
  57. QIcon theme_icon = fromTheme(icon_name);
  58. swap(theme_icon);
  59. return;
  60. }
  61. // Is this is an icon we've manually mapped to a standard pixmap below?
  62. if (icon_name_to_standard_pixmap_.contains(icon_name)) {
  63. QIcon standard_icon = qApp->style()->standardIcon(icon_name_to_standard_pixmap_[icon_name]);
  64. swap(standard_icon);
  65. return;
  66. }
  67. // Is this one of our locally sourced, cage-free, organic icons?
  68. QStringList types = QStringList() << "8x8" << "14x14" << "16x16" << "24x14" << "24x24";
  69. QList<QIcon::Mode> icon_modes = QList<QIcon::Mode>()
  70. << QIcon::Disabled
  71. << QIcon::Active
  72. << QIcon::Selected;
  73. foreach (QString type, types) {
  74. // First, check for a template (mask) icon
  75. // Templates should be monochrome as described at
  76. // https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/custom-icons/
  77. // Transparency is supported.
  78. QString icon_path_template = path_pfx_ + QString("%1/%2.template.png").arg(type).arg(icon_name);
  79. if (QFile::exists(icon_path_template)) {
  80. QIcon mask_icon = QIcon();
  81. mask_icon.addFile(icon_path_template);
  82. foreach(QSize sz, mask_icon.availableSizes()) {
  83. QPixmap mask_pm = mask_icon.pixmap(sz);
  84. QImage normal_img(sz, QImage::Format_ARGB32);
  85. QPainter painter(&normal_img);
  86. QBrush br(qApp->palette().color(QPalette::Active, QPalette::WindowText));
  87. painter.fillRect(0, 0, sz.width(), sz.height(), br);
  88. painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  89. painter.drawPixmap(0, 0, mask_pm);
  90. QPixmap normal_pm = QPixmap::fromImage(normal_img);
  91. addPixmap(normal_pm, QIcon::Normal, QIcon::On);
  92. addPixmap(normal_pm, QIcon::Normal, QIcon::Off);
  93. QStyleOption opt = {};
  94. opt.palette = qApp->palette();
  95. foreach (QIcon::Mode icon_mode, icon_modes) {
  96. QPixmap mode_pm = qApp->style()->generatedIconPixmap(icon_mode, normal_pm, &opt);
  97. addPixmap(mode_pm, icon_mode, QIcon::On);
  98. addPixmap(mode_pm, icon_mode, QIcon::Off);
  99. }
  100. }
  101. continue;
  102. }
  103. // Regular full-color icons
  104. QString icon_path = path_pfx_ + QString("%1/%2.png").arg(type).arg(icon_name);
  105. if (QFile::exists(icon_path)) {
  106. addFile(icon_path);
  107. }
  108. // Along with each name check for "<name>.active" and
  109. // "<name>.selected" for the Active and Selected modes, and
  110. // "<name>.on" to use for the on (checked) state.
  111. // XXX Allow more (or all) combinations.
  112. QString icon_path_active = path_pfx_ + QString("%1/%2.active.png").arg(type).arg(icon_name);
  113. if (QFile::exists(icon_path_active)) {
  114. addFile(icon_path_active, QSize(), QIcon::Active, QIcon::On);
  115. }
  116. QString icon_path_selected = path_pfx_ + QString("%1/%2.selected.png").arg(type).arg(icon_name);
  117. if (QFile::exists(icon_path_selected)) {
  118. addFile(icon_path_selected, QSize(), QIcon::Selected, QIcon::On);
  119. }
  120. QString icon_path_on = path_pfx_ + QString("%1/%2.on.png").arg(type).arg(icon_name);
  121. if (QFile::exists(icon_path_on)) {
  122. addFile(icon_path_on, QSize(), QIcon::Normal, QIcon::On);
  123. }
  124. }
  125. }
  126. // Create a square icon filled with the specified color.
  127. QIcon StockIcon::colorIcon(const QRgb bg_color, const QRgb fg_color, const QString glyph)
  128. {
  129. QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
  130. QIcon color_icon;
  131. foreach (int size, sizes) {
  132. QPixmap pm(size, size);
  133. QPainter painter(&pm);
  134. QRect border(0, 0, size - 1, size - 1);
  135. painter.setPen(fg_color);
  136. painter.setBrush(QColor(bg_color));
  137. painter.drawRect(border);
  138. if (!glyph.isEmpty()) {
  139. QFont font(qApp->font());
  140. font.setPointSizeF(size / 2.0);
  141. painter.setFont(font);
  142. QRectF bounding = painter.boundingRect(pm.rect(), glyph, Qt::AlignHCenter | Qt::AlignVCenter);
  143. painter.drawText(bounding, glyph);
  144. }
  145. color_icon.addPixmap(pm);
  146. }
  147. return color_icon;
  148. }
  149. // Create a triangle icon filled with the specified color.
  150. QIcon StockIcon::colorIconTriangle(const QRgb bg_color, const QRgb fg_color)
  151. {
  152. QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
  153. QIcon color_icon;
  154. foreach (int size, sizes) {
  155. QPixmap pm(size, size);
  156. QPainter painter(&pm);
  157. QPainterPath triangle;
  158. pm.fill();
  159. painter.fillRect(0, 0, size-1, size-1, Qt::transparent);
  160. painter.setPen(fg_color);
  161. painter.setBrush(QColor(bg_color));
  162. triangle.moveTo(0, size-1);
  163. triangle.lineTo(size-1, size-1);
  164. triangle.lineTo((size-1)/2, 0);
  165. triangle.closeSubpath();
  166. painter.fillPath(triangle, QColor(bg_color));
  167. color_icon.addPixmap(pm);
  168. }
  169. return color_icon;
  170. }
  171. // Create a cross icon filled with the specified color.
  172. QIcon StockIcon::colorIconCross(const QRgb bg_color, const QRgb fg_color)
  173. {
  174. QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
  175. QIcon color_icon;
  176. foreach (int size, sizes) {
  177. QPixmap pm(size, size);
  178. QPainter painter(&pm);
  179. QPainterPath cross;
  180. pm.fill();
  181. painter.fillRect(0, 0, size-1, size-1, Qt::transparent);
  182. painter.setPen(QPen(QBrush(bg_color), 3));
  183. painter.setBrush(QColor(fg_color));
  184. cross.moveTo(0, 0);
  185. cross.lineTo(size-1, size-1);
  186. cross.moveTo(0, size-1);
  187. cross.lineTo(size-1, 0);
  188. painter.drawPath(cross);
  189. color_icon.addPixmap(pm);
  190. }
  191. return color_icon;
  192. }
  193. // Create a circle icon filled with the specified color.
  194. QIcon StockIcon::colorIconCircle(const QRgb bg_color, const QRgb fg_color)
  195. {
  196. QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
  197. QIcon color_icon;
  198. foreach (int size, sizes) {
  199. QPixmap pm(size, size);
  200. QPainter painter(&pm);
  201. QRect border(2, 2, size - 3, size - 3);
  202. pm.fill();
  203. painter.fillRect(0, 0, size-1, size-1, Qt::transparent);
  204. painter.setPen(QPen(QBrush(bg_color), 3));
  205. painter.setBrush(QColor(fg_color));
  206. painter.setBrush(QColor(bg_color));
  207. painter.drawEllipse(border);
  208. color_icon.addPixmap(pm);
  209. }
  210. return color_icon;
  211. }
  212. void StockIcon::fillIconNameMap()
  213. {
  214. // Note that some of Qt's standard pixmaps are awful. We shouldn't add an
  215. // entry just because a match can be made.
  216. icon_name_to_standard_pixmap_["document-open"] = QStyle::SP_DirIcon;
  217. icon_name_to_standard_pixmap_["media-playback-pause"] = QStyle::SP_MediaPause;
  218. icon_name_to_standard_pixmap_["media-playback-start"] = QStyle::SP_MediaPlay;
  219. icon_name_to_standard_pixmap_["media-playback-stop"] = QStyle::SP_MediaStop;
  220. }