Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
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 QtCursorToString (Qt::CursorShape cursorShape)
27 {
28  if (cursorShapesLookupTable.count () == 0) {
29 
30  // Initialize
31  cursorShapesLookupTable [Qt::ArrowCursor] = "Qt::ArrowCursor";
32  cursorShapesLookupTable [Qt::BitmapCursor] = "Qt::BitmapCursor";
33  cursorShapesLookupTable [Qt::CrossCursor] = "Qt::CrossCursor";
34  cursorShapesLookupTable [Qt::WaitCursor] = "Qt::WaitCursor";
35  }
36 
37  if (cursorShapesLookupTable.contains (cursorShape)) {
38 
39  return cursorShapesLookupTable [cursorShape];
40 
41  } else {
42 
43  return "Qt::<unknown>";
44 
45  }
46 }
47 
48 QString QLocaleToString (const QLocale &locale)
49 {
50  return QString ("%1/%2")
51  .arg (QLocale::languageToString (locale.language()))
52  .arg (QLocale::countryToString(locale.country()));
53 }
54 
55 QString QTransformToString (const QTransform &transform)
56 {
57  const int FIELD_WIDTH = 12;
58 
59  QString str = QString ("%1 %2 %3 %4\n"
60  "%5 %6 %7 %8\n"
61  "%9 %10 %11 %12")
62  .arg (INDENTATION_PAST_TIMESTAMP)
63  .arg (transform.m11 (), FIELD_WIDTH)
64  .arg (transform.m12 (), FIELD_WIDTH)
65  .arg (transform.m13 (), FIELD_WIDTH)
66  .arg (INDENTATION_PAST_TIMESTAMP)
67  .arg (transform.m21 (), FIELD_WIDTH)
68  .arg (transform.m22 (), FIELD_WIDTH)
69  .arg (transform.m23 (), FIELD_WIDTH)
70  .arg (INDENTATION_PAST_TIMESTAMP)
71  .arg (transform.m31 (), FIELD_WIDTH)
72  .arg (transform.m32 (), FIELD_WIDTH)
73  .arg (transform.m33 (), FIELD_WIDTH);
74 
75  return str;
76 }
77 
78 QString QXmlStreamReaderTokenTypeToString (QXmlStreamReader::TokenType tokenType)
79 {
80  if (xmlTokenTypeLookupTable.count () == 0) {
81 
82  // Initialize
83  xmlTokenTypeLookupTable [QXmlStreamReader::Characters] = "Characters";
84  xmlTokenTypeLookupTable [QXmlStreamReader::Comment] = "Comment";
85  xmlTokenTypeLookupTable [QXmlStreamReader::DTD] = "DTD";
86  xmlTokenTypeLookupTable [QXmlStreamReader::EndDocument] = "EndDocument";
87  xmlTokenTypeLookupTable [QXmlStreamReader::EndElement] = "EndElement";
88  xmlTokenTypeLookupTable [QXmlStreamReader::EntityReference] = "EntityReference";
89  xmlTokenTypeLookupTable [QXmlStreamReader::Invalid] = "Invalid";
90  xmlTokenTypeLookupTable [QXmlStreamReader::NoToken] = "NoToken";
91  xmlTokenTypeLookupTable [QXmlStreamReader::ProcessingInstruction] = "ProcessingInstruction";
92  xmlTokenTypeLookupTable [QXmlStreamReader::StartDocument] = "StartDocument";
93  xmlTokenTypeLookupTable [QXmlStreamReader::StartElement] = "StartElement";
94  }
95 
96  if (xmlTokenTypeLookupTable.contains (tokenType)) {
97 
98  return xmlTokenTypeLookupTable [tokenType];
99 
100  } else {
101 
102  return "<Unknown>";
103 
104  }
105 }
106 
107 QString roleAsString (int role)
108 {
109  if (rolesAsStringsLookupTable.count () == 0) {
110 
111  // Initialize with list from qnamespace.h
112  rolesAsStringsLookupTable [Qt::AccessibleDescriptionRole] = "AccessibleDescriptionRole";
113  rolesAsStringsLookupTable [Qt::AccessibleTextRole] = "AccessibleTextRole";
114  rolesAsStringsLookupTable [Qt::BackgroundRole] = "BackgroundRole";
115  rolesAsStringsLookupTable [Qt::BackgroundColorRole] = "BackgroundColorRole";
116  rolesAsStringsLookupTable [Qt::CheckStateRole] = "CheckStateRole";
117  rolesAsStringsLookupTable [Qt::DecorationRole] = "DecorationRole";
118  rolesAsStringsLookupTable [Qt::DisplayRole] = "DisplayRole";
119  rolesAsStringsLookupTable [Qt::EditRole] = "EditRole";
120  rolesAsStringsLookupTable [Qt::FontRole] = "FontRole";
121  rolesAsStringsLookupTable [Qt::ForegroundRole] = "ForegroundRole";
122  rolesAsStringsLookupTable [Qt::InitialSortOrderRole] = "InitialSortOrderRole";
123  rolesAsStringsLookupTable [Qt::SizeHintRole] = "SizeHintRole";
124  rolesAsStringsLookupTable [Qt::StatusTipRole] = "StatusTipRole";
125  rolesAsStringsLookupTable [Qt::TextAlignmentRole] = "TextAlignmentRole";
126  rolesAsStringsLookupTable [Qt::TextColorRole] = "TextColorRole";
127  rolesAsStringsLookupTable [Qt::ToolTipRole] = "ToolTipRole";
128  rolesAsStringsLookupTable [Qt::UserRole] = "UserRole";
129  rolesAsStringsLookupTable [Qt::WhatsThisRole] = "WhatsThisRole";
130  }
131 
132  if (rolesAsStringsLookupTable.contains (role)) {
133 
134  return rolesAsStringsLookupTable [role];
135 
136  } else {
137 
138  return QString ("%1?").arg (role);
139 
140  }
141 }
142 
143 QString rolesAsString (const QVector<int> &roles)
144 {
145  QString str;
146 
147  for (int i = 0; i < roles.count (); i++) {
148  if (i > 0) {
149  str += ",";
150  }
151  str += roleAsString (roles [i]);
152  }
153 
154  return str;
155 }