HAL  v4.5.0-83-g30c8f0afc
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 
8 namespace hal
9 {
10  namespace gui_utility
11  {
12  void changeSvgColor(QString& svg_data, const QColor& from, const QColor& to)
13  {
14  QRegExp regex(from.name());
15  svg_data.replace(regex, to.name().toUtf8());
16  }
17 
18  void changeAllSvgColors(QString& svg_data, const QColor& to)
19  {
20  QRegExp regex("#[0-9a-f]{6}", Qt::CaseInsensitive);
21  svg_data.replace(regex, to.name().toUtf8());
22  }
23 
24  bool applyColorStyle(QString& svg_data, const QString& color_style)
25  {
26  QRegExp color_regex("#[0-9a-f]{6}", Qt::CaseInsensitive);
27  QRegExp all_to_color_regex("\\s*all\\s*->\\s*#[0-9a-f]{6}\\s*", Qt::CaseInsensitive);
28  QRegExp 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*", Qt::CaseInsensitive);
29  if (all_to_color_regex.exactMatch(color_style))
30  {
31  color_regex.indexIn(color_style);
32  QString color = color_regex.cap(0);
33  svg_data.replace(color_regex, color.toUtf8());
34  }
35  else if (color_to_color_regex.exactMatch(color_style))
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  QRegExp 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:18
void changeSvgColor(QString &svg_data, const QColor &from, const QColor &to)
Definition: graphics.cpp:12
bool applyColorStyle(QString &svg_data, const QString &color_style)
Definition: graphics.cpp:24
Definition: defines.h:45
QString name() const const
virtual bool open(QIODevice::OpenMode mode) override
QByteArray readAll()
QString cap(int nth) const const
bool exactMatch(const QString &str) const const
int indexIn(const QString &str, int offset, QRegExp::CaretMode caretMode) 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
CaseInsensitive