CustusX  2021.04.21-dev+develop.337f9
An IGT application
cxTypeConversions.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxTypeConversions.h"
13 #include <QStringList>
14 #include <iostream>
15 #include "cxVector3D.h"
16 
17 template<> cstring_cast_Placeholder cstring_cast<QString>(const QString& val)
18 {
19  return cstring_cast_Placeholder(val);
20 }
21 template<> cstring_cast_Placeholder cstring_cast<QVariant>(const QVariant& val)
22 {
23  return cstring_cast_Placeholder(val.toString());
24 }
25 
28 std::ostream& operator<<(std::ostream& str, const QString& qstring)
29 {
30  str << qstring.toStdString();
31  return str;
32 }
33 
34 std::vector<double> convertQString2DoubleVector(const QString& input, bool* ok)
35 {
36  if (ok)
37  *ok = true;
38  QStringList comp = input.split(QRegExp("\\s+"), QString::SkipEmptyParts);
39  std::vector<double> retval(comp.size());
40  bool tempOk = true;
41 
42  for (unsigned i=0; i<retval.size(); ++i)
43  {
44  retval[i] = comp[i].toDouble(&tempOk);
45  if (ok)
46  *ok = *ok && tempOk;
47  }
48  return retval;
49 }
50 
51 
52 QString color2string(QColor color)
53 {
54  QString retval = QString("%1/%2/%3")
55  .arg(color.red())
56  .arg(color.green())
57  .arg(color.blue());
58  if (color.alpha()!=255)
59  retval += QString("/%1").arg(color.alpha());
60  return retval;
61 }
62 
63 
64 QColor string2color(QString input, QColor defaultValue)
65 {
66  QStringList c = input.split("/");
67  if (c.size()==3)
68  c.push_back("255");
69  if (c.size()<4)
70  return defaultValue;
71  bool ok;
72  QColor retval = QColor::fromRgb(
73  c[0].toInt(&ok),
74  c[1].toInt(&ok),
75  c[2].toInt(&ok),
76  c[3].toInt(&ok));
77  if (!ok)
78  return defaultValue;
79  return retval;
80 }
81 
82 
83 
QColor string2color(QString input, QColor defaultValue)
QString color2string(QColor color)
cstring_cast_Placeholder cstring_cast(const T &val)
std::ostream & operator<<(std::ostream &str, const QString &qstring)
std::vector< double > convertQString2DoubleVector(const QString &input, bool *ok)