Engauge Digitizer  2
QtToString.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "Logger.h"
8 #include <QHash>
9 #include <QLocale>
10 #include <QTransform>
11 #include "QtToString.h"
12 
13 static QHash<Qt::CursorShape, QString> cursorShapesLookupTable;
14 static QHash<int, QString> rolesAsStringsLookupTable;
15 static QHash<QXmlStreamReader::TokenType, QString> xmlTokenTypeLookupTable;
16 
17 QString QPointFToString (const QPointF &pos)
18 {
19  QString str = QString ("(%1, %2)")
20  .arg (pos.x ())
21  .arg (pos.y ());
22 
23  return str;
24 }
25 
26 QString QRectFToString (const QRectF &rectF)
27 {
28  QString str = QString ("(%1x%2+%3+%4)")
29  .arg (rectF.width())
30  .arg (rectF.height())
31  .arg (rectF.x())
32  .arg (rectF.y());
33 
34  return str;
35 }
36 
37 QString QtCursorToString (Qt::CursorShape cursorShape)
38 {
39  if (cursorShapesLookupTable.count () == 0) {
40 
41  // Initialize
42  cursorShapesLookupTable [Qt::ArrowCursor] = "Qt::ArrowCursor";
43  cursorShapesLookupTable [Qt::BitmapCursor] = "Qt::BitmapCursor";
44  cursorShapesLookupTable [Qt::CrossCursor] = "Qt::CrossCursor";
45  cursorShapesLookupTable [Qt::WaitCursor] = "Qt::WaitCursor";
46  }
47 
48  if (cursorShapesLookupTable.contains (cursorShape)) {
49 
50  return cursorShapesLookupTable [cursorShape];
51 
52  } else {
53 
54  return "Qt::<unknown>";
55 
56  }
57 }
58 
59 QString QLocaleToString (const QLocale &locale)
60 {
61  return QString ("%1/%2")
62  .arg (QLocale::languageToString (locale.language()))
63  .arg (QLocale::countryToString(locale.country()));
64 }
65 
66 QString QTransformToString (const QTransform &transform)
67 {
68  const int FIELD_WIDTH = 12;
69 
70  QString str = QString ("%1 %2 %3 %4\n"
71  "%5 %6 %7 %8\n"
72  "%9 %10 %11 %12")
73  .arg (INDENTATION_PAST_TIMESTAMP)
74  .arg (transform.m11 (), FIELD_WIDTH)
75  .arg (transform.m12 (), FIELD_WIDTH)
76  .arg (transform.m13 (), FIELD_WIDTH)
77  .arg (INDENTATION_PAST_TIMESTAMP)
78  .arg (transform.m21 (), FIELD_WIDTH)
79  .arg (transform.m22 (), FIELD_WIDTH)
80  .arg (transform.m23 (), FIELD_WIDTH)
81  .arg (INDENTATION_PAST_TIMESTAMP)
82  .arg (transform.m31 (), FIELD_WIDTH)
83  .arg (transform.m32 (), FIELD_WIDTH)
84  .arg (transform.m33 (), FIELD_WIDTH);
85 
86  return str;
87 }
88 
89 QString QXmlStreamReaderTokenTypeToString (QXmlStreamReader::TokenType tokenType)
90 {
91  if (xmlTokenTypeLookupTable.count () == 0) {
92 
93  // Initialize
94  xmlTokenTypeLookupTable [QXmlStreamReader::Characters] = "Characters";
95  xmlTokenTypeLookupTable [QXmlStreamReader::Comment] = "Comment";
96  xmlTokenTypeLookupTable [QXmlStreamReader::DTD] = "DTD";
97  xmlTokenTypeLookupTable [QXmlStreamReader::EndDocument] = "EndDocument";
98  xmlTokenTypeLookupTable [QXmlStreamReader::EndElement] = "EndElement";
99  xmlTokenTypeLookupTable [QXmlStreamReader::EntityReference] = "EntityReference";
100  xmlTokenTypeLookupTable [QXmlStreamReader::Invalid] = "Invalid";
101  xmlTokenTypeLookupTable [QXmlStreamReader::NoToken] = "NoToken";
102  xmlTokenTypeLookupTable [QXmlStreamReader::ProcessingInstruction] = "ProcessingInstruction";
103  xmlTokenTypeLookupTable [QXmlStreamReader::StartDocument] = "StartDocument";
104  xmlTokenTypeLookupTable [QXmlStreamReader::StartElement] = "StartElement";
105  }
106 
107  if (xmlTokenTypeLookupTable.contains (tokenType)) {
108 
109  return xmlTokenTypeLookupTable [tokenType];
110 
111  } else {
112 
113  return "<Unknown>";
114 
115  }
116 }
117 
118 QString roleAsString (int role)
119 {
120  if (rolesAsStringsLookupTable.count () == 0) {
121 
122  // Initialize with list from qnamespace.h
123  rolesAsStringsLookupTable [Qt::AccessibleDescriptionRole] = "AccessibleDescriptionRole";
124  rolesAsStringsLookupTable [Qt::AccessibleTextRole] = "AccessibleTextRole";
125  rolesAsStringsLookupTable [Qt::BackgroundRole] = "BackgroundRole";
126  rolesAsStringsLookupTable [Qt::BackgroundColorRole] = "BackgroundColorRole";
127  rolesAsStringsLookupTable [Qt::CheckStateRole] = "CheckStateRole";
128  rolesAsStringsLookupTable [Qt::DecorationRole] = "DecorationRole";
129  rolesAsStringsLookupTable [Qt::DisplayRole] = "DisplayRole";
130  rolesAsStringsLookupTable [Qt::EditRole] = "EditRole";
131  rolesAsStringsLookupTable [Qt::FontRole] = "FontRole";
132  rolesAsStringsLookupTable [Qt::ForegroundRole] = "ForegroundRole";
133  rolesAsStringsLookupTable [Qt::InitialSortOrderRole] = "InitialSortOrderRole";
134  rolesAsStringsLookupTable [Qt::SizeHintRole] = "SizeHintRole";
135  rolesAsStringsLookupTable [Qt::StatusTipRole] = "StatusTipRole";
136  rolesAsStringsLookupTable [Qt::TextAlignmentRole] = "TextAlignmentRole";
137  rolesAsStringsLookupTable [Qt::TextColorRole] = "TextColorRole";
138  rolesAsStringsLookupTable [Qt::ToolTipRole] = "ToolTipRole";
139  rolesAsStringsLookupTable [Qt::UserRole] = "UserRole";
140  rolesAsStringsLookupTable [Qt::WhatsThisRole] = "WhatsThisRole";
141  }
142 
143  if (rolesAsStringsLookupTable.contains (role)) {
144 
145  return rolesAsStringsLookupTable [role];
146 
147  } else {
148 
149  return QString ("%1?").arg (role);
150 
151  }
152 }
153 
154 QString rolesAsString (const QVector<int> &roles)
155 {
156  QString str;
157 
158  for (int i = 0; i < roles.count (); i++) {
159  if (i > 0) {
160  str += ",";
161  }
162  str += roleAsString (roles [i]);
163  }
164 
165  return str;
166 }