HAL  v4.5.0-133-g64838ea8d
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
graphics.cpp
Go to the documentation of this file.
2 
4 
5 #include <QFile>
6 #include <QImage>
7 #include <QRegularExpression>
8 
9 namespace hal
10 {
11  namespace gui_utility
12  {
13  void changeSvgColor(QString& svg_data, const QColor& from, const QColor& to)
14  {
15  QRegularExpression regex(from.name());
16  svg_data.replace(regex, to.name().toUtf8());
17  }
18 
19  void changeAllSvgColors(QString& svg_data, const QColor& to)
20  {
22  svg_data.replace(regex, to.name().toUtf8());
23  }
24 
25  bool applyColorStyle(QString& svg_data, const QString& color_style)
26  {
28  QRegularExpression all_to_color_regex("\\s*all\\s*->\\s*#[0-9a-f]{6}\\s*", QRegularExpression::CaseInsensitiveOption);
29  QRegularExpression color_to_color_regex("\\s*(#[0-9a-f]{6}\\s*->\\s*#[0-9a-f]{6}\\s*,\\s*)*#[0-9a-f]{6}\\s*->\\s*#[0-9a-f]{6}\\s*", QRegularExpression::CaseInsensitiveOption);
30  if (all_to_color_regex.match(color_style).hasMatch())
31  {
32  QString color = color_regex.match(color_style).captured();
33  svg_data.replace(color_regex, color.toUtf8());
34  }
35  else if (color_to_color_regex.match(color_style).hasMatch())
36  {
37  QString copy = color_style;
38  copy = copy.simplified();
39  copy.replace(" ", "");
40 
41  QStringList list = copy.split(",");
42 
43  for (QString string : list)
44  {
45  QString from_color = string.left(7);
46  QString to_color = string.right(7);
47 
48  QRegularExpression regex(from_color);
49  svg_data.replace(regex, to_color.toUtf8());
50  }
51  }
52  else
53  {
54  return false;
55  }
56  return true;
57  }
58 
59 
60  QIcon getStyledSvgIcon(const QString& from_to_colors_enabled, const QString& svg_path, QString from_to_colors_disabled)
61  {
62  QFile file(svg_path);
63  if (!file.open(QFile::ReadOnly))
64  {
65  //TODO: print error
66  return QIcon();
67  }
68 
69  QString svgDataEnabled(file.readAll());
70  if (svgDataEnabled.isEmpty())
71  {
72  //TODO: print error
73  return QIcon();
74  }
75 
76  QString svgDataDisabled(svgDataEnabled);
77 
78  if (!from_to_colors_enabled.isEmpty())
79  {
80  if (!applyColorStyle(svgDataEnabled,from_to_colors_enabled))
81  {
82  //TODO: print error
83  return QIcon();
84  }
85  }
86 
87  if (from_to_colors_disabled.isEmpty())
88  {
89  svgDataDisabled = QString();
90  }
91  else
92  {
93  if (!applyColorStyle(svgDataDisabled,from_to_colors_disabled))
94  svgDataDisabled = QString();
95  }
96 
97  return QIcon(new SvgIconEngine(svgDataEnabled.toUtf8(),svgDataDisabled.toUtf8()));
98  }
99  } // namespace gui_utility
100 }
Creates Icons from svg data.
S to(const T &str)
QIcon getStyledSvgIcon(const QString &from_to_colors_enabled, const QString &svg_path, QString from_to_colors_disabled=QString())
Definition: graphics.cpp:60
void changeAllSvgColors(QString &svg_data, const QColor &to)
Definition: graphics.cpp:19
void changeSvgColor(QString &svg_data, const QColor &from, const QColor &to)
Definition: graphics.cpp:13
bool applyColorStyle(QString &svg_data, const QString &color_style)
Definition: graphics.cpp:25
Definition: defines.h:45
QString name() const const
virtual bool open(QIODevice::OpenMode mode) override
QByteArray readAll()
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
QString captured(int nth) const const
bool hasMatch() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString left(int n) const const
QString & replace(int position, int n, QChar after)
QString right(int n) const const
QString simplified() const const
QByteArray toUtf8() const const