• 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
  • poddecoder
poddecodertool.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2007-2010 Friedrich W. H. Kossebau <kossebau@kde.org>
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 
23 #include "poddecodertool.h"
24 
25 // lib
26 #include "typecodecs/binary8codec.h"
27 #include "typecodecs/octal8codec.h"
28 #include "typecodecs/hexadecimal8codec.h"
29 #include "typecodecs/uint8codec.h"
30 #include "typecodecs/uint16codec.h"
31 #include "typecodecs/uint32codec.h"
32 #include "typecodecs/uint64codec.h"
33 #include "typecodecs/sint8codec.h"
34 #include "typecodecs/sint16codec.h"
35 #include "typecodecs/sint32codec.h"
36 #include "typecodecs/sint64codec.h"
37 #include "typecodecs/float32codec.h"
38 #include "typecodecs/float64codec.h"
39 #include "typecodecs/char8codec.h"
40 #include "typecodecs/utf8codec.h"
41 #include "abstracttypecodec.h"
42 #include "abstractdifferentsizedialog.h"
43 #include <bytearraydocument.h>
44 #include <bytearrayview.h>
45 // Okteta core
46 #include <charcodec.h>
47 #include <abstractbytearraymodel.h>
48 #include <changesdescribable.h>
49 // KDE
50 #include <KLocale>
51 
52 
53 namespace Kasten2
54 {
55 
56 enum PODTypes
57 {
58  BinaryId = 0,
59  OctalId,
60  HexadecimalId,
61  Signed8BitId,
62  Unsigned8BitId,
63  Signed16BitId,
64  Unsigned16BitId,
65  Signed32BitId,
66  Unsigned32BitId,
67  Signed64BitId,
68  Unsigned64BitId,
69  Float32BitId,
70  Float64BitId,
71  Char8BitId,
72  UTF8Id,
73 // UTF16Id,
74  PODTypeCount
75 };
76 
77 
78 PODDecoderTool::PODDecoderTool()
79  : mByteArrayView( 0 ),
80  mByteArrayModel( 0 ),
81  mCursorIndex( 0 ),
82  mReadOnly( true ),
83  mIsPodMarked( false ),
84  mCharCodec( Okteta::CharCodec::createCodec(Okteta::LocalEncoding) ),
85  mDifferentSizeDialog( 0 ),
86  mUnsignedAsHex( true )
87 {
88  setObjectName( QLatin1String( "PODDecoder" ) );
89 
90  setupDecoder();
91 }
92 
93 QString PODDecoderTool::title() const { return i18nc("@title:window", "Decoding Table"); }
94 bool PODDecoderTool::isReadOnly() const { return mReadOnly; }
95 bool PODDecoderTool::isApplyable() const { return (mByteArrayModel != 0); }
96 
97 void PODDecoderTool::setTargetModel( AbstractModel* model )
98 {
99  const bool oldIsApplyable = isApplyable();
100 
101  if( mByteArrayView )
102  {
103  mByteArrayView->disconnect( this );
104  if( mIsPodMarked )
105  unmarkPOD();
106  }
107  if( mByteArrayModel ) mByteArrayModel->disconnect( this );
108 
109  mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : 0;
110  ByteArrayDocument *document =
111  mByteArrayView ? qobject_cast<ByteArrayDocument*>( mByteArrayView->baseModel() ) : 0;
112  mByteArrayModel = document ? document->content() : 0;
113 
114  if( mByteArrayModel && mByteArrayView )
115  {
116  mCursorIndex = mByteArrayView->cursorPosition();
117  connect( mByteArrayView, SIGNAL(cursorPositionChanged(Okteta::Address)),
118  SLOT(onCursorPositionChange(Okteta::Address)) );
119  connect( mByteArrayModel, SIGNAL(contentsChanged(Okteta::ArrayChangeMetricsList)),
120  SLOT(onContentsChange()) );
121  connect( mByteArrayView, SIGNAL(charCodecChanged(QString)),
122  SLOT(onCharCodecChange(QString)) );
123  connect( mByteArrayView, SIGNAL(readOnlyChanged(bool)),
124  SLOT(onReadOnlyChanged()) );
125  onCharCodecChange( mByteArrayView->charCodingName() );
126  }
127 
128  updateData();
129  onReadOnlyChanged();
130  const bool newIsApplyable = isApplyable();
131  if( oldIsApplyable != newIsApplyable )
132  emit isApplyableChanged( newIsApplyable );
133 }
134 
135 
136 void PODDecoderTool::setupDecoder()
137 {
138  mTypeCodecs.resize( PODTypeCount );
139  mTypeCodecs[BinaryId] = new Okteta::Binary8Codec();
140  mTypeCodecs[OctalId] = new Okteta::Octal8Codec();
141  mTypeCodecs[HexadecimalId] = new Okteta::Hexadecimal8Codec();
142  mTypeCodecs[Signed8BitId] = new Okteta::SInt8Codec();
143  mTypeCodecs[Unsigned8BitId] = new Okteta::UInt8Codec();
144  mTypeCodecs[Signed16BitId] = new Okteta::SInt16Codec();
145  mTypeCodecs[Unsigned16BitId] = new Okteta::UInt16Codec();
146  mTypeCodecs[Signed32BitId] = new Okteta::SInt32Codec();
147  mTypeCodecs[Unsigned32BitId] = new Okteta::UInt32Codec();
148  mTypeCodecs[Signed64BitId] = new Okteta::SInt64Codec();
149  mTypeCodecs[Unsigned64BitId] = new Okteta::UInt64Codec();
150  mTypeCodecs[Float32BitId] = new Okteta::Float32Codec();
151  mTypeCodecs[Float64BitId] = new Okteta::Float64Codec();
152  mTypeCodecs[Char8BitId] = new Okteta::Char8Codec( mCharCodec );
153  mTypeCodecs[UTF8Id] = new Okteta::Utf8Codec();
154 
155 #if 0
156  mDecoderNameList[UTF16Id] =
157  i18nc("@label:textbox","UTF-16:");
158 #endif
159 
160  mDecodedValueList.resize( PODTypeCount );
161  mDecodedValueByteCountList.resize( PODTypeCount );
162 }
163 
164 void PODDecoderTool::setDifferentSizeDialog( AbstractDifferentSizeDialog* differentSizeDialog )
165 {
166  mDifferentSizeDialog = differentSizeDialog;
167 }
168 
169 void PODDecoderTool::setUnsignedAsHex( bool unsignedAsHex )
170 {
171  if( mUnsignedAsHex == unsignedAsHex )
172  return;
173 
174  mUnsignedAsHex = unsignedAsHex;
175 
176  updateData();
177 }
178 
179 void PODDecoderTool::setByteOrder( int byteOrder )
180 {
181  // TODO: test on no change is done in PODData, not this level
182  mPODData.setByteOrder( (Okteta::ByteOrder)byteOrder );
183  updateData();
184 }
185 
186 void PODDecoderTool::onCharCodecChange( const QString& codecName )
187 {
188  if( codecName == mCharCodec->name() )
189  return;
190 
191  delete mCharCodec;
192  mCharCodec = Okteta::CharCodec::createCodec( codecName );
193  static_cast<Okteta::Char8Codec*>( mTypeCodecs[Char8BitId] )->setCharCodec( mCharCodec );
194  updateData();
195 }
196 
197 
198 void PODDecoderTool::onCursorPositionChange( Okteta::Address pos )
199 {
200  mCursorIndex = pos;
201  updateData();
202 }
203 
204 void PODDecoderTool::onContentsChange()
205 {
206  // TODO: only update if affected
207  updateData();
208 }
209 
210 
211 int PODDecoderTool::podCount() const { return mTypeCodecs.count(); }
212 
213 
214 QString PODDecoderTool::nameOfPOD( int podId ) const
215 {
216  return mTypeCodecs[podId]->name();
217 }
218 
219 
220 QVariant PODDecoderTool::value( int podId ) const
221 {
222  // TODO: add caching here
223  return mDecodedValueList[podId];
224 }
225 
226 
227 void PODDecoderTool::setData( const QVariant& data, int podId )
228 {
229  Okteta::AbstractTypeCodec* typeCodec = mTypeCodecs[podId];
230 
231  // QVariant::operator=() only compares values' addresses for custom types,
232  // so the comparison for values needs to be done by someone with knowledge about the type.
233  const bool isUnchangedValue = typeCodec->areEqual( data, mDecodedValueList[podId] );
234 
235  if( isUnchangedValue )
236  return;
237 
238  QByteArray bytes = typeCodec->valueToBytes( data );
239 
240  const int bytesSize = bytes.size();
241  if( bytesSize == 0 )
242  return;
243 
244  // need to swap the bytes
245  if( mPODData.byteOrder() != Okteta::thisMachineByteOrder )
246  {
247  const int firstHalfBytesCount = bytesSize/2;
248  int j = bytesSize - 1;
249  for( int i=0; i<firstHalfBytesCount; ++i,--j )
250  {
251  const char helper = bytes[i];
252  bytes[i] = bytes[j];
253  bytes[j] = helper;
254  }
255  }
256 
257  const int oldValueSize = mDecodedValueByteCountList[podId];
258  int removedBytesSize = bytesSize;
259  if( bytesSize != oldValueSize )
260  {
261 // const int sizeLeft = mByteArrayModel->size() - mCursorIndex;
262  const Answer answer = Cancel; // TODO: non-persistent editor closes on new dialog -> crash after dialog
263 // mDifferentSizeDialog ? mDifferentSizeDialog->query( bytesSize, oldValueSize, sizeLeft ) : Cancel;
264  if( answer == Cancel )
265  return;
266 
267  if( answer == AdaptSize )
268  removedBytesSize = oldValueSize;
269  }
270 
271  Okteta::ChangesDescribable* changesDescribable =
272  qobject_cast<Okteta::ChangesDescribable*>( mByteArrayModel );
273 
274  if( changesDescribable )
275  changesDescribable->openGroupedChange( i18nc("Edited as %datatype","Edited as %1", typeCodec->name()) );
276  mByteArrayModel->replace( Okteta::AddressRange::fromWidth(mCursorIndex,removedBytesSize), bytes );
277  if( changesDescribable )
278  changesDescribable->closeGroupedChange();
279 }
280 
281 void PODDecoderTool::updateData()
282 {
283  int dataSize;
284  if( mByteArrayModel )
285  {
286  dataSize = mByteArrayModel->size() - mCursorIndex;
287  if( dataSize > mPODData.Size )
288  dataSize = mPODData.Size;
289  else if( dataSize < 0 )
290  dataSize = 0;
291  }
292  else
293  dataSize = 0;
294 
295  const bool hasDataSet = ( dataSize > 0 );
296  if( hasDataSet )
297  mByteArrayModel->copyTo( mPODData.rawData(), mCursorIndex, mPODData.Size );
298 
299  const bool hasChanged = mPODData.updateRawData( dataSize );
300 
301  if( ! hasChanged )
302  return;
303 
304  // TODO: only calculate on demand + cache
305  for( int podId=0; podId<PODTypeCount; ++podId )
306  {
307  int byteCount = 0;
308  mDecodedValueList[podId] = mTypeCodecs[podId]->value( mPODData, &byteCount );
309  mDecodedValueByteCountList[podId] = byteCount;
310  }
311 
312  // TODO: only emit for those strings that changed
313  emit dataChanged();
314 }
315 
316 
317 void PODDecoderTool::markPOD( int podId )
318 {
319  const int length = mDecodedValueByteCountList[podId];
320  const Okteta::AddressRange markingRange = Okteta::AddressRange::fromWidth( mCursorIndex, length );
321  mByteArrayView->setMarking( markingRange, true );
322  mIsPodMarked = true;
323 }
324 
325 void PODDecoderTool::unmarkPOD()
326 {
327 // TODO: marked region is property of document, not view?
328  mByteArrayView->setMarking( Okteta::AddressRange() );
329  mIsPodMarked = false;
330 }
331 
332 void PODDecoderTool::onReadOnlyChanged()
333 {
334  const bool newReadOnly = ( (! mByteArrayModel) || (! mByteArrayView)
335  || mByteArrayView->isReadOnly() );
336  if( newReadOnly != mReadOnly )
337  {
338  mReadOnly = newReadOnly;
339  emit readOnlyChanged( newReadOnly );
340  }
341 }
342 
343 PODDecoderTool::~PODDecoderTool()
344 {
345  delete mCharCodec;
346  qDeleteAll( mTypeCodecs );
347 }
348 
349 }
Okteta::AbstractByteArrayModel::replace
virtual Size replace(const AddressRange &removeRange, const Byte *insertData, int insertLength)=0
replaces as much as possible
uint8codec.h
Okteta::AbstractTypeCodec
Definition: abstracttypecodec.h:38
Okteta::Address
qint32 Address
Definition: address.h:34
octal8codec.h
abstractbytearraymodel.h
Kasten2::Signed64BitId
Definition: poddecodertool.cpp:67
Kasten2::PODDecoderTool::markPOD
void markPOD(int podId)
Definition: poddecodertool.cpp:317
Kasten2::AbstractDifferentSizeDialog
Definition: abstractdifferentsizedialog.h:33
Kasten2::OctalId
Definition: poddecodertool.cpp:59
Okteta::Utf8Codec
Definition: utf8codec.h:35
Okteta::ChangesDescribable::closeGroupedChange
virtual void closeGroupedChange(const QString &description=QString())=0
Kasten2::PODDecoderTool::title
virtual QString title() const
Definition: poddecodertool.cpp:93
Kasten2::PODDecoderTool::nameOfPOD
QString nameOfPOD(int podId) const
Definition: poddecodertool.cpp:214
Kasten2::PODDecoderTool::unmarkPOD
void unmarkPOD()
Definition: poddecodertool.cpp:325
Kasten2::BinaryId
Definition: poddecodertool.cpp:58
Kasten2::ByteArrayView::charCodingName
QString charCodingName() const
Definition: bytearrayview.cpp:255
Kasten2::Unsigned8BitId
Definition: poddecodertool.cpp:62
Kasten2::PODDecoderTool::PODDecoderTool
PODDecoderTool()
Definition: poddecodertool.cpp:78
Kasten2::PODDecoderTool::~PODDecoderTool
virtual ~PODDecoderTool()
Definition: poddecodertool.cpp:343
KDE::NumberRange< Address, Size >
Okteta::Hexadecimal8Codec
Definition: hexadecimal8codec.h:33
Kasten2::Signed32BitId
Definition: poddecodertool.cpp:65
Okteta::ChangesDescribable::openGroupedChange
virtual void openGroupedChange(const QString &description=QString())=0
Kasten2::AdaptSize
Definition: kastencore.h:70
Okteta::ChangesDescribable
Definition: changesdescribable.h:33
Okteta::UInt8Codec
Definition: uint8codec.h:33
abstractdifferentsizedialog.h
Okteta::AbstractTypeCodec::areEqual
virtual bool areEqual(const QVariant &value, QVariant &otherValue) const =0
Okteta::UInt32Codec
Definition: uint32codec.h:33
Kasten2::Unsigned16BitId
Definition: poddecodertool.cpp:64
Okteta::SInt8Codec
Definition: sint8codec.h:33
Kasten2::PODDecoderTool::isReadOnly
bool isReadOnly() const
Definition: poddecodertool.cpp:94
Okteta::Float64Codec
Definition: float64codec.h:33
Kasten2::PODDecoderTool::setUnsignedAsHex
void setUnsignedAsHex(bool unsignedAsHex)
Definition: poddecodertool.cpp:169
Kasten2::UTF8Id
Definition: poddecodertool.cpp:72
abstracttypecodec.h
Kasten2::AbstractModel::baseModel
AbstractModel * baseModel() const
Definition: abstractmodel.cpp:40
uint32codec.h
Okteta::PODData::Size
static const int Size
Definition: poddata.h:37
Okteta::AbstractTypeCodec::name
const QString & name() const
Definition: abstracttypecodec.h:63
Okteta::AbstractByteArrayModel::copyTo
virtual Size copyTo(Byte *dest, const AddressRange &copyRange) const
copies the data of the section into a given array Dest.
Definition: abstractbytearraymodel.cpp:60
utf8codec.h
uint16codec.h
Kasten2::PODDecoderTool::readOnlyChanged
void readOnlyChanged(bool isReadOnly)
Kasten2::PODDecoderTool::isApplyable
bool isApplyable() const
Definition: poddecodertool.cpp:95
Okteta::Char8Codec
Definition: char8codec.h:35
poddecodertool.h
Okteta::UInt16Codec
Definition: uint16codec.h:33
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
uint64codec.h
Kasten2::ByteArrayView::cursorPosition
Okteta::Address cursorPosition() const
Definition: bytearrayview.cpp:227
Kasten2::Float32BitId
Definition: poddecodertool.cpp:69
Okteta::UInt64Codec
Definition: uint64codec.h:33
Okteta::ByteOrder
ByteOrder
Definition: oktetacore.h:111
sint32codec.h
sint8codec.h
Okteta::PODData::setByteOrder
void setByteOrder(ByteOrder byteOrder)
Definition: poddata.cpp:53
Kasten2::Signed16BitId
Definition: poddecodertool.cpp:63
Kasten2::PODDecoderTool::dataChanged
void dataChanged()
Okteta::Binary8Codec
Definition: binary8codec.h:33
sint16codec.h
KDE::NumberRange< Address, Size >::fromWidth
static NumberRange fromWidth(AddressstartIndex, Sizewidth)
constructs a range by width
Kasten2::PODDecoderTool::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: poddecodertool.cpp:97
sint64codec.h
Okteta::SInt32Codec
Definition: sint32codec.h:33
Kasten2::PODDecoderTool::value
QVariant value(int podId) const
Definition: poddecodertool.cpp:220
Okteta::ArrayChangeMetricsList
Definition: arraychangemetricslist.h:36
Kasten2::Answer
Answer
Definition: kastencore.h:58
Kasten2::PODDecoderTool::podCount
int podCount() const
Definition: poddecodertool.cpp:211
Kasten2::ByteArrayView::setMarking
void setMarking(const Okteta::AddressRange &range, bool ensureVisible=false)
Definition: bytearrayview.cpp:351
Kasten2::Unsigned64BitId
Definition: poddecodertool.cpp:68
Okteta::CharCodec::createCodec
static CharCodec * createCodec(CharCoding charCoding)
Definition: charcodec.cpp:68
Kasten2::PODDecoderTool::setData
void setData(const QVariant &data, int podId)
Definition: poddecodertool.cpp:227
hexadecimal8codec.h
Kasten2::Char8BitId
Definition: poddecodertool.cpp:71
Kasten2::AbstractModel::findBaseModel
T findBaseModel() const
returns the first baseModel which is of type T, or null if none is found.
Definition: abstractmodel.h:93
Kasten2::Cancel
Definition: kastencore.h:60
charcodec.h
Okteta::SInt16Codec
Definition: sint16codec.h:33
Okteta::SInt64Codec
Definition: sint64codec.h:33
Kasten2::PODTypeCount
Definition: poddecodertool.cpp:74
changesdescribable.h
Okteta::Float32Codec
Definition: float32codec.h:33
Okteta::CharCodec::name
virtual const QString & name() const =0
Kasten2::Signed8BitId
Definition: poddecodertool.cpp:61
Okteta::Octal8Codec
Definition: octal8codec.h:33
Okteta::PODData::updateRawData
bool updateRawData(int size)
Definition: poddata.cpp:118
Okteta::LocalEncoding
the coding of your shell
Definition: oktetacore.h:42
Kasten2::PODDecoderTool::setByteOrder
void setByteOrder(int byteOrder)
Definition: poddecodertool.cpp:179
Okteta::AbstractTypeCodec::valueToBytes
virtual QByteArray valueToBytes(const QVariant &value) const =0
Okteta::PODData::rawData
Byte * rawData()
Definition: poddata.cpp:51
Kasten2::HexadecimalId
Definition: poddecodertool.cpp:60
Kasten2::Unsigned32BitId
Definition: poddecodertool.cpp:66
Kasten2::ByteArrayDocument
Definition: bytearraydocument.h:54
binary8codec.h
Kasten2::AbstractModel
Definition: abstractmodel.h:40
char8codec.h
bytearraydocument.h
Kasten2::Float64BitId
Definition: poddecodertool.cpp:70
float64codec.h
float32codec.h
Kasten2::PODDecoderTool::setDifferentSizeDialog
void setDifferentSizeDialog(AbstractDifferentSizeDialog *differentSizeDialog)
Definition: poddecodertool.cpp:164
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Okteta::PODData::byteOrder
ByteOrder byteOrder() const
Definition: poddata.cpp:47
Kasten2::PODTypes
PODTypes
Definition: poddecodertool.cpp:56
Kasten2::ByteArrayView::isReadOnly
virtual bool isReadOnly() const
default returns true
Definition: bytearrayview.cpp:152
Kasten2::PODDecoderTool::isApplyableChanged
void isApplyableChanged(bool isApplyable)
Okteta::thisMachineByteOrder
static const ByteOrder thisMachineByteOrder
Definition: oktetacore.h:116
bytearrayview.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 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