• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
  • datatypes
  • primitive
chardatainformation.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2009, 2010, 2011 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 #include "chardatainformation.h"
23 
24 #include <QScriptValue>
25 #include <KLineEdit>
26 #include <KGlobal>
27 #include <KLocale>
28 
29 #include "structviewpreferences.h"
30 
31 namespace {
32  QString charString(quint8 value)
33  {
34  switch (value)
35  {
36  case '\0': return QLatin1String("'\\0'");
37  case '\a': return QLatin1String("'\\a'");
38  case '\b': return QLatin1String("'\\b'");
39  case '\f': return QLatin1String("'\\f'");
40  case '\n': return QLatin1String("'\\n'");
41  case '\r': return QLatin1String("'\\r'");
42  case '\t': return QLatin1String("'\\t'");
43  case '\v': return QLatin1String("'\\v'");
44  default: break;
45  }
46  QChar qchar = QChar(quint32(value));
47  if (!qchar.isPrint())
48  qchar = QChar::ReplacementCharacter;
49  return QString(QLatin1Char('\'') + qchar + QLatin1Char('\''));
50  }
51 }
52 
53 QString CharDataInformationMethods::staticValueString(quint8 value)
54 {
55  QString charStr = charString(value);
56  if (Kasten2::StructViewPreferences::showCharNumericalValue())
57  {
58  int base = Kasten2::StructViewPreferences::charDisplayBase();
59  QString num = QString::number(value, base);
60  if (base == 10 && Kasten2::StructViewPreferences::localeAwareDecimalFormatting())
61  num = KGlobal::locale()->formatNumber(num, false, 0);
62  charStr += QLatin1String(" (") + PrimitiveDataInformation::basePrefix(base)
63  + num + QLatin1Char(')');
64  }
65  return charStr;
66 }
67 
68 QWidget* CharDataInformationMethods::staticCreateEditWidget(QWidget* parent)
69 {
70  return new KLineEdit(parent);
71 }
72 
73 QVariant CharDataInformationMethods::staticDataFromWidget(const QWidget* w)
74 {
75  //TODO fix this code!!
76  const KLineEdit* edit = dynamic_cast<const KLineEdit*> (w);
77  if (edit)
78  {
79  QString text = edit->text();
80  if (text.length() == 0)
81  {
82  return QVariant();
83  }
84  if (text.length() == 1)
85  {
86  //TODO char codec
87  return (unsigned char) text.at(0).toLatin1();
88  }
89  if (text.at(0) == QLatin1Char('\\'))
90  {
91  //escape sequence
92  if (text.at(1) == QLatin1Char('x'))
93  {
94  //hex escape:
95  bool okay;
96  QString valStr = text.mid(2, 2); //only 2 chars
97  quint8 val = valStr.toInt(&okay, 16);
98  if (okay)
99  return val;
100  else
101  return QVariant();
102  }
103  else if (text.at(1) == QLatin1Char('n'))
104  {
105  return (quint8) '\n'; //newline
106  }
107  else if (text.at(1) == QLatin1Char('t'))
108  {
109  return (quint8) '\t'; //tab
110  }
111  else if (text.at(1) == QLatin1Char('r'))
112  {
113  return (quint8) '\r'; //cr
114  }
115  else
116  {
117  //octal escape:
118  bool okay;
119  QString valStr = text.mid(1, 3); //only 2 chars
120  quint8 val = valStr.toInt(&okay, 8);
121  if (okay)
122  return val;
123  else
124  return QVariant();
125  }
126  }
127  }
128  return QVariant();
129 }
130 
131 void CharDataInformationMethods::staticSetWidgetData(quint8 value, QWidget* w)
132 {
133  KLineEdit* edit = dynamic_cast<KLineEdit*> (w);
134  if (edit)
135  {
136  QChar qchar(value, 0);
137  if (! qchar.isPrint())
138  qchar = QChar(QChar::ReplacementCharacter);
139  edit->setText( qchar );
140  }
141 }
142 
143 QScriptValue CharDataInformationMethods::asScriptValue(quint8 value, QScriptEngine* engine, ScriptHandlerInfo* handler)
144 {
145  Q_UNUSED(engine);
146  Q_UNUSED(handler);
147  return QScriptValue(QString(value > 127 ? QChar::ReplacementCharacter : QChar(value, 0)));
148 }
Kasten2::StructViewPreferences::charDisplayBase
static int charDisplayBase()
Get CharDisplayBase.
Definition: structviewpreferences.h:88
CharDataInformationMethods::staticValueString
static QString staticValueString(quint8 value)
Definition: chardatainformation.cpp:53
QWidget
structviewpreferences.h
CharDataInformationMethods::asScriptValue
static QScriptValue asScriptValue(quint8 value, QScriptEngine *engine, ScriptHandlerInfo *handler)
Definition: chardatainformation.cpp:143
CharDataInformationMethods::staticSetWidgetData
static void staticSetWidgetData(quint8 value, QWidget *w)
Definition: chardatainformation.cpp:131
CharDataInformationMethods::staticCreateEditWidget
static QWidget * staticCreateEditWidget(QWidget *parent)
Definition: chardatainformation.cpp:68
CharDataInformationMethods::staticDataFromWidget
static QVariant staticDataFromWidget(const QWidget *w)
Definition: chardatainformation.cpp:73
Kasten2::StructViewPreferences::localeAwareDecimalFormatting
static bool localeAwareDecimalFormatting()
Get LocaleAwareDecimalFormatting.
Definition: structviewpreferences.h:196
KLineEdit
ScriptHandlerInfo
Definition: scripthandlerinfo.h:39
Kasten2::StructViewPreferences::showCharNumericalValue
static bool showCharNumericalValue()
Get ShowCharNumericalValue.
Definition: structviewpreferences.h:115
PrimitiveDataInformation::basePrefix
static QString basePrefix(int base)
Definition: primitivedatainformation.h:143
chardatainformation.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal