Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkStyleManager.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include <QmitkStyleManager.h>
14 
15 #include <mitkLogMacros.h>
16 
17 #include <QApplication>
18 #include <QFile>
19 #include <QImage>
20 #include <QPixmap>
21 #include <QRegularExpression>
22 
23 namespace
24 {
25  QString ParseColor(const QString &subject, const QString &pattern, const QString &fallback)
26  {
27  QRegularExpression re(pattern, QRegularExpression::CaseInsensitiveOption);
28  auto match = re.match(subject);
29 
30  return match.hasMatch()
31  ? match.captured(1)
32  : fallback;
33  }
34 }
35 
36 QIcon QmitkStyleManager::ThemeIcon(const QByteArray &originalSVG)
37 {
38  auto styleSheet = qApp->styleSheet();
39 
40  if (styleSheet.isEmpty())
41  return QPixmap::fromImage(QImage::fromData(originalSVG));
42 
43  auto iconColor = ParseColor(styleSheet,
44  QStringLiteral("iconColor\\s*[=:]\\s*(#[0-9a-f]{6})"),
45  QStringLiteral("#000000"));
46 
47  auto iconAccentColor = ParseColor(styleSheet,
48  QStringLiteral("iconAccentColor\\s*[=:]\\s*(#[0-9a-f]{6})"),
49  QStringLiteral("#ffffff"));
50 
51  auto themedSVG = QString(originalSVG).replace(QStringLiteral("#00ff00"), iconColor, Qt::CaseInsensitive);
52  themedSVG = themedSVG.replace(QStringLiteral("#ff00ff"), iconAccentColor, Qt::CaseInsensitive);
53 
54  return QPixmap::fromImage(QImage::fromData(themedSVG.toLatin1()));
55 }
56 
57 QIcon QmitkStyleManager::ThemeIcon(const QString &resourcePath)
58 {
59  QFile resourceFile(resourcePath);
60 
61  if (resourceFile.open(QIODevice::ReadOnly))
62  {
63  auto originalSVG = resourceFile.readAll();
64  return ThemeIcon(originalSVG);
65  }
66 
67  MITK_WARN << "Could not read " << resourcePath.toStdString();
68  return QIcon();
69 }
#define MITK_WARN
Definition: mitkLogMacros.h:19
static QString ParseColor(const QString &subject, const QString &pattern, const QString &fallback)
static QIcon ThemeIcon(const QByteArray &originalSVG)