Allow "capture info data" to not be a singleton.
[metze/wireshark/wip.git] / ui / qt / stock_icon.cpp
1 /* stock_icon.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "stock_icon.h"
23
24 // Stock icons. Based on gtk/stock_icons.h
25
26 // Toolbar icon sizes:
27 // OS X freestanding: 32x32, 32x32@2x, segmented (inside a button): <= 19x19
28 // Windows: 16x16, 24x24, 32x32
29 // GNOME: 24x24 (default), 48x48
30
31 // References:
32 //
33 // http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
34 // http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
35 //
36 // http://mithatkonar.com/wiki/doku.php/qt/icons
37 //
38 // https://developer.apple.com/library/mac/documentation/userexperience/conceptual/applehiguidelines/IconsImages/IconsImages.html#//apple_ref/doc/uid/20000967-TPXREF102
39 // http://msdn.microsoft.com/en-us/library/windows/desktop/dn742485.aspx
40 // https://developer.gnome.org/hig-book/stable/icons-types.html.en
41 // http://msdn.microsoft.com/en-us/library/ms246582.aspx
42
43 // To do:
44 // - 32x32, 48x48, 64x64, and unscaled (.svg) icons
45 // - Indent find & go actions when those panes are open.
46 // - Replace or remove:
47 //   WIRESHARK_STOCK_CAPTURE_FILTER x-capture-filter
48 //   WIRESHARK_STOCK_DISPLAY_FILTER x-display-filter
49 //   GTK_STOCK_SELECT_COLOR x-coloring-rules
50 //   GTK_STOCK_PREFERENCES preferences-system
51 //   GTK_STOCK_HELP help-contents
52
53 #include "wireshark_application.h"
54
55 #include <QFile>
56 #include <QFontMetrics>
57 #include <QMap>
58 #include <QPainter>
59 #include <QStyle>
60
61 // XXX We're using icons in more than just the toolbar.
62 static const QString path_pfx_ = ":/icons/toolbar/";
63
64 // Map FreeDesktop icon names to Qt standard pixmaps.
65 static QMap<QString, QStyle::StandardPixmap> icon_name_to_standard_pixmap_;
66
67 StockIcon::StockIcon(const QString icon_name) :
68     QIcon()
69 {
70     if (icon_name_to_standard_pixmap_.isEmpty()) {
71         fillIconNameMap();
72     }
73
74     // Does our theme contain this icon?
75     // X11 only as per the QIcon documentation.
76     if (hasThemeIcon(icon_name)) {
77         QIcon theme_icon = fromTheme(icon_name);
78 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
79         swap(theme_icon);
80 #endif
81         return;
82     }
83
84     // Is this is an icon we've manually mapped to a standard pixmap below?
85     if (icon_name_to_standard_pixmap_.contains(icon_name)) {
86         QIcon standard_icon = wsApp->style()->standardIcon(icon_name_to_standard_pixmap_[icon_name]);
87 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
88         swap(standard_icon);
89 #endif
90         return;
91     }
92
93     // Is this one of our locally sourced, cage-free, organic icons?
94     QStringList types = QStringList() << "14x14" << "16x16" << "24x14" << "24x24";
95     foreach (QString type, types) {
96         QString icon_path = path_pfx_ + QString("%1/%2.png").arg(type).arg(icon_name);
97         if (QFile::exists(icon_path)) {
98             addFile(icon_path);
99         }
100
101         // Along with each name check for "<name>.active" and
102         // "<name>.selected" for the Active and Selected modes, and
103         // "<name>.on" to use for the on (checked) state.
104         // XXX Allow more (or all) combinations.
105         QString icon_path_active = path_pfx_ + QString("%1/%2.active.png").arg(type).arg(icon_name);
106         if (QFile::exists(icon_path_active)) {
107             addFile(icon_path_active, QSize(), QIcon::Active, QIcon::On);
108         }
109
110         QString icon_path_selected = path_pfx_ + QString("%1/%2.selected.png").arg(type).arg(icon_name);
111         if (QFile::exists(icon_path_selected)) {
112             addFile(icon_path_selected, QSize(), QIcon::Selected, QIcon::On);
113         }
114
115         QString icon_path_on = path_pfx_ + QString("%1/%2.on.png").arg(type).arg(icon_name);
116         if (QFile::exists(icon_path_on)) {
117             addFile(icon_path_on, QSize(), QIcon::Normal, QIcon::On);
118         }
119     }
120 }
121
122 // Create a square icon filled with the specified color.
123 QIcon StockIcon::colorIcon(const QRgb bg_color, const QRgb fg_color, const QString glyph)
124 {
125     QList<int> sizes = QList<int>() << 12 << 16 << 24 << 32 << 48;
126     QIcon color_icon;
127
128     foreach (int size, sizes) {
129         QPixmap pm(size, size);
130         QPainter painter(&pm);
131         QRect border(0, 0, size - 1, size - 1);
132         painter.setPen(fg_color);
133         painter.setBrush(QColor(bg_color));
134         painter.drawRect(border);
135
136         if (!glyph.isEmpty()) {
137             QFont font(wsApp->font());
138             font.setPointSizeF(size / 2.0);
139             painter.setFont(font);
140             QRectF bounding = painter.boundingRect(pm.rect(), glyph, Qt::AlignHCenter | Qt::AlignVCenter);
141             painter.drawText(bounding, glyph);
142         }
143
144         color_icon.addPixmap(pm);
145     }
146     return color_icon;
147 }
148
149 void StockIcon::fillIconNameMap()
150 {
151     // Note that some of Qt's standard pixmaps are awful. We shouldn't add an
152     // entry just because a match can be made.
153     icon_name_to_standard_pixmap_["document-open"] = QStyle::SP_DirIcon;
154     icon_name_to_standard_pixmap_["media-playback-pause"] = QStyle::SP_MediaPause;
155     icon_name_to_standard_pixmap_["media-playback-start"] = QStyle::SP_MediaPlay;
156     icon_name_to_standard_pixmap_["media-playback-stop"] = QStyle::SP_MediaStop;
157 }
158
159 /*
160  * Editor modelines
161  *
162  * Local Variables:
163  * c-basic-offset: 4
164  * tab-width: 8
165  * indent-tabs-mode: nil
166  * End:
167  *
168  * ex: set shiftwidth=4 tabstop=8 expandtab:
169  * :indentSize=4:tabSize=8:noTabs=true:
170  */