• 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
  • gui
  • controller
kvalueeditor.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Gui library, made within the KDE community.
3 
4  Copyright 2004,2008 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 "kvalueeditor.h"
24 
25 // lib
26 #include <bytearraytablecursor.h>
27 #include <abstractbytearrayview.h>
28 // Okteta core
29 #include <abstractbytearraymodel.h>
30 #include <changesdescribable.h>
31 #include <valuecodec.h>
32 // KDE
33 #include <KLocale>
34 // Qt
35 #include <QtGui/QKeyEvent>
36 
37 
38 namespace Okteta
39 {
40 
41 KValueEditor::KValueEditor( ByteArrayTableCursor* cursor, AbstractByteArrayView* view, KController* parent )
42  : KEditor( cursor, view, parent ),
43  mInEditMode( false ),
44  mEditModeByInsert( false )
45 {
46 }
47 
48 void KValueEditor::adaptToValueCodecChange()
49 {
50  const uint newCodingWidth = mView->valueCodec()->encodingWidth();
51  mValueString.resize( newCodingWidth );
52 }
53 
54 void KValueEditor::startEdit( const QString &description )
55 {
56  Q_ASSERT( !mInEditMode );
57 
58  Okteta::AbstractByteArrayModel* byteArrayModel = mView->byteArrayModel();
59  Okteta::ChangesDescribable *changesDescribable =
60  qobject_cast<Okteta::ChangesDescribable*>( byteArrayModel );
61 
62  if( changesDescribable )
63  changesDescribable->openGroupedChange( description );
64 
65  mInEditMode = true;
66 }
67 
68 void KValueEditor::cancelEdit( bool undoChanges )
69 {
70 // Q_ASSERT( mInEditMode );
71  if( !mInEditMode )
72  return;
73 
74  mInEditMode = false;
75 
76  if( undoChanges )
77  {
78  Okteta::AbstractByteArrayModel* byteArrayModel = mView->byteArrayModel();
79  Okteta::ChangesDescribable* changesDescribable =
80  qobject_cast<Okteta::ChangesDescribable*>( byteArrayModel );
81 
82  // TODO: if !changesDescribable the changes need to be undone, too
83  if( changesDescribable )
84  changesDescribable->cancelGroupedChange();
85  }
86 }
87 
88 void KValueEditor::finishEdit()
89 {
90  if( !mInEditMode )
91  return;
92 
93  mInEditMode = false;
94 
95  Okteta::AbstractByteArrayModel* byteArrayModel = mView->byteArrayModel();
96  Okteta::ChangesDescribable *changesDescribable =
97  qobject_cast<Okteta::ChangesDescribable*>( byteArrayModel );
98 
99  if( changesDescribable )
100  changesDescribable->closeGroupedChange();
101 }
102 
103 
104 bool KValueEditor::handleKeyPress( QKeyEvent *keyEvent )
105 {
106  bool keyUsed = true;
107 
108  // TODO: for now we don't touch it if there are selections
109  if( !mView->hasSelectedData() )
110  {
111  //
112  switch( keyEvent->key() )
113  {
114  case Qt::Key_Plus:
115  doValueEditAction( IncValue );
116  break;
117  case Qt::Key_Minus:
118  doValueEditAction( DecValue );
119  break;
120  case Qt::Key_Space:
121  if( !mInEditMode )
122  {
123  keyUsed = false;
124  break;
125  }
126  case Qt::Key_Enter:
127  case Qt::Key_Return:
128  doValueEditAction( mInEditMode?LeaveValue:EnterValue );
129  break;
130  case Qt::Key_Escape:
131  if( mInEditMode )
132  cancelEdit();
133  else
134  keyUsed = false;
135  break;
136  case Qt::Key_Backspace:
137  if( mInEditMode )
138  doValueEditAction( ValueBackspace );
139  else
140  keyUsed = false;
141  break;
142  default:
143  // is plain char?
144  if( keyEvent->text().length() > 0
145  && ( !(keyEvent->modifiers()&( Qt::CTRL | Qt::ALT | Qt::META )) ) )
146  {
147  const int input = keyEvent->text()[0].toLatin1();
148  // no usable char?
149  if( input < 32 )
150  {
151  keyUsed = false;
152  break;
153  }
154 
155  const Okteta::ValueCodec* valueCodec = mView->valueCodec();
156  if( mInEditMode )
157  {
158  if( mInsertedDigitsCount < valueCodec->encodingWidth() )
159  doValueEditAction( ValueAppend, input );
160  }
161  else
162  {
163  Byte inputValue = 0;
164  // valid digit?
165  if( valueCodec->appendDigit(&inputValue,input) )
166  {
167  if( mView->isOverwriteMode() )
168  doValueEditAction( ValueEdit, inputValue );
169  else
170  {
171  const Address index = mCursor->realIndex();
172 
173  startEdit( i18nc( "name of the change", "Insert" ) );
174  if( mView->byteArrayModel()->insert(index,&inputValue,1) > 0 )
175  {
176  mEditModeByInsert = true;
177  mOldValue = mEditValue = inputValue;
178  mInsertedDigitsCount = 1;
179  valueCodec->encode( mValueString, 0, mEditValue );
180 
181  mCursor->gotoIndex(index);
182  mView->ensureCursorVisible();
183 // mView->updateCursors();
184  emit mView->cursorPositionChanged( mCursor->realIndex() );
185  }
186  else
187  cancelEdit();
188  }
189  }
190  }
191  }
192  else
193  keyUsed = false;
194  }
195  }
196  else
197  keyUsed = false;
198 
199  return keyUsed ? true : KEditor::handleKeyPress(keyEvent);
200 }
201 
202 
203 void KValueEditor::doValueEditAction( KValueEditAction Action, int input )
204 {
205  const Okteta::ValueCodec* valueCodec = mView->valueCodec();
206 
207  // we are not yet in edit mode?
208  if( !mInEditMode )
209  {
210  const Address validIndex = mCursor->validIndex();
211  // no valid cursor position?
212  if( validIndex == -1 || (!mView->isOverwriteMode() && input == -1) || mCursor->isBehind() )
213  return;
214 
215  startEdit( i18nc( "name of the change", "Replace" ) );
216  mEditModeByInsert = false; // default, to be overwritten if so
217 
218  // save old value
219  mOldValue = mEditValue = (unsigned char)mView->byteArrayModel()->byte( validIndex );
220  mInsertedDigitsCount = valueCodec->encodingWidth();
221  }
222 
223  //
224  Byte newValue = mEditValue;
225  bool stayInEditMode = true;
226  bool moveToNext = false;
227 
228  switch( Action )
229  {
230  case ValueEdit:
231  newValue = input;
232  mEditValue = newValue^255; // force update
233  mEditModeByInsert = true;
234  mInsertedDigitsCount = 1;
235  break;
236  case ValueBackspace:
237  if( mInsertedDigitsCount > 0 )
238  {
239  if( newValue > 0 )
240  valueCodec->removeLastDigit( &newValue );
241  --mInsertedDigitsCount;
242  }
243  break;
244  case EnterValue:
245  mEditValue ^= 255; // force update
246  break;
247  case IncValue:
248  if( newValue < 255 )
249  {
250  ++newValue;
251  mInsertedDigitsCount = valueCodec->encodingWidth();
252  }
253  break;
254  case DecValue:
255  if( newValue > 0 )
256  {
257  --newValue;
258  mInsertedDigitsCount = valueCodec->encodingWidth();
259  }
260  break;
261  case ValueAppend:
262  if( valueCodec->appendDigit(&newValue,input) )
263  {
264  ++mInsertedDigitsCount;
265  if( mEditModeByInsert &&
266  (newValue >= valueCodec->digitsFilledLimit() || mInsertedDigitsCount == valueCodec->encodingWidth()) )
267  {
268  stayInEditMode = false;
269  moveToNext = true;
270  }
271  }
272  break;
273  case LeaveValue:
274  stayInEditMode = false;
275  moveToNext = mEditModeByInsert;
276  break;
277  }
278 
279  // change happened?
280  if( newValue != mEditValue )
281  {
282  // sync value
283  mEditValue = newValue;
284  valueCodec->encode( mValueString, 0, mEditValue );
285  mView->byteArrayModel()->replace( mCursor->index(), 1, &mEditValue, 1 );
286  }
287 
288 // mView->updateCursors();
289 
290  if( !stayInEditMode )
291  {
292  mView->pauseCursor();
293  finishEdit();
294 
295  if( moveToNext )
296  mCursor->gotoNextByte();
297 
298  mView->unpauseCursor();
299  if( moveToNext )
300  emit mView->cursorPositionChanged( mCursor->realIndex() );
301  }
302 }
303 
304 KValueEditor::~KValueEditor() {}
305 
306 }
Okteta::KValueEditor::finishEdit
void finishEdit()
Definition: kvalueeditor.cpp:88
Okteta::AbstractByteArrayModel::replace
virtual Size replace(const AddressRange &removeRange, const Byte *insertData, int insertLength)=0
replaces as much as possible
Okteta::KValueEditor::KValueEditor
KValueEditor(ByteArrayTableCursor *cursor, AbstractByteArrayView *view, KController *parent)
Definition: kvalueeditor.cpp:41
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
Okteta::ByteArrayTableCursor::index
Address index() const
the index that is drawn at the actual coord
Definition: bytearraytablecursor.h:170
abstractbytearraymodel.h
Okteta::KValueEditor::startEdit
void startEdit(const QString &description)
Definition: kvalueeditor.cpp:54
Okteta::KValueEditor::mValueString
QString mValueString
buffer with the
Definition: kvalueeditor.h:86
Okteta::KValueEditor::handleKeyPress
virtual bool handleKeyPress(QKeyEvent *keyEvent)
Definition: kvalueeditor.cpp:104
Okteta::KEditor::mCursor
ByteArrayTableCursor * mCursor
Definition: keditor.h:54
Okteta::ChangesDescribable::closeGroupedChange
virtual void closeGroupedChange(const QString &description=QString())=0
Okteta::KValueEditor::~KValueEditor
virtual ~KValueEditor()
Definition: kvalueeditor.cpp:304
Okteta::AbstractByteArrayModel::insert
virtual Size insert(Address offset, const Byte *insertData, int insertLength)
inserts bytes copied from the given source at Position.
Definition: abstractbytearraymodel.cpp:47
Okteta::KValueEditor::ValueAppend
Definition: kvalueeditor.h:44
Okteta::KEditor::mView
AbstractByteArrayView * mView
Definition: keditor.h:55
Okteta::AbstractByteArrayView::hasSelectedData
bool hasSelectedData() const
returns true if there is a selected range in the array
Definition: abstractbytearrayview.cpp:166
Okteta::ChangesDescribable::openGroupedChange
virtual void openGroupedChange(const QString &description=QString())=0
abstractbytearrayview.h
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::ChangesDescribable
Definition: changesdescribable.h:33
Okteta::AbstractByteArrayView::valueCodec
const Okteta::ValueCodec * valueCodec() const
Definition: abstractbytearrayview.cpp:85
Okteta::AbstractByteArrayView
Definition: abstractbytearrayview.h:55
Okteta::KValueEditor::ValueBackspace
Definition: kvalueeditor.h:44
Okteta::KValueEditor::mInsertedDigitsCount
unsigned int mInsertedDigitsCount
Definition: kvalueeditor.h:84
Okteta::AbstractByteArrayView::isOverwriteMode
bool isOverwriteMode() const
Definition: abstractbytearrayview.cpp:49
Okteta::KValueEditor::DecValue
Definition: kvalueeditor.h:44
Okteta::KValueEditor::mInEditMode
bool mInEditMode
flag whether we are in editing mode
Definition: kvalueeditor.h:76
Okteta::ByteArrayTableCursor::gotoIndex
void gotoIndex(Address index)
Definition: bytearraytablecursor.cpp:260
Okteta::KValueEditor::LeaveValue
Definition: kvalueeditor.h:44
Okteta::ByteArrayTableCursor::gotoNextByte
void gotoNextByte()
Definition: bytearraytablecursor.cpp:102
Okteta::KEditor
Definition: keditor.h:36
Okteta::KValueEditor::doValueEditAction
void doValueEditAction(KValueEditAction action, int input=-1)
executes keyboard Action Action.
Definition: kvalueeditor.cpp:203
Okteta::KValueEditor::mEditModeByInsert
bool mEditModeByInsert
flag whether byte edit mode was reached by inserting
Definition: kvalueeditor.h:78
Okteta::ValueCodec
Class that is able to convert codings to and from hexadecimal, decimal, octal, and binary...
Definition: valuecodec.h:45
Okteta::AbstractByteArrayView::byteArrayModel
Okteta::AbstractByteArrayModel * byteArrayModel() const
Definition: abstractbytearrayview.cpp:44
Okteta::ChangesDescribable::cancelGroupedChange
virtual void cancelGroupedChange()=0
Okteta::ByteArrayTableCursor::validIndex
Address validIndex() const
returns the true index if it is valid index that is it is inside the data's range.
Definition: bytearraytablecursor.cpp:329
Okteta::AbstractByteArrayView::pauseCursor
void pauseCursor()
simply pauses any blinking, i.e.
Definition: abstractbytearrayview.cpp:421
Okteta::AbstractByteArrayView::cursorPositionChanged
void cursorPositionChanged(Okteta::Address index)
Okteta::KValueEditor::mOldValue
Byte mOldValue
stores the old byte value
Definition: kvalueeditor.h:82
valuecodec.h
Okteta::AbstractByteArrayModel::byte
virtual Byte byte(Address offset) const =0
locates working range The idea behind is to tell buffer which range will be requested in the followin...
Okteta::ByteArrayTableCursor
navigates through the buffer in an abstract way, based on the layout
Definition: bytearraytablecursor.h:60
Okteta::KValueEditor::IncValue
Definition: kvalueeditor.h:44
Okteta::KValueEditor::cancelEdit
void cancelEdit(bool undoChanges=true)
Definition: kvalueeditor.cpp:68
changesdescribable.h
Okteta::AbstractByteArrayView::unpauseCursor
void unpauseCursor()
undoes pauseCursor
Definition: abstractbytearrayview.cpp:409
Okteta::ByteArrayTableCursor::realIndex
Address realIndex() const
returns the real index.
Definition: bytearraytablecursor.h:175
Okteta::KValueEditor::EnterValue
Definition: kvalueeditor.h:44
Okteta::ValueCodec::digitsFilledLimit
virtual Byte digitsFilledLimit() const =0
Okteta::ValueCodec::encodingWidth
virtual unsigned int encodingWidth() const =0
Okteta::ByteArrayTableCursor::isBehind
bool isBehind() const
true if the cursor is located to the right of the actual coord but still shown at the coord ...
Definition: bytearraytablecursor.h:174
Okteta::AbstractByteArrayView::ensureCursorVisible
void ensureCursorVisible()
scrolls the view as much as needed to have the cursor fully visible
Definition: abstractbytearrayview.cpp:390
kvalueeditor.h
char
Okteta::KValueEditor::KValueEditAction
KValueEditAction
Definition: kvalueeditor.h:43
bytearraytablecursor.h
Okteta::KValueEditor::mEditValue
Byte mEditValue
Definition: kvalueeditor.h:80
Okteta::ValueCodec::appendDigit
virtual bool appendDigit(Byte *byte, unsigned char digit) const =0
Tries to increase the byte value by appending a digit to the digits of the current encoding...
Okteta::KEditor::handleKeyPress
virtual bool handleKeyPress(QKeyEvent *keyEvent)
Definition: keditor.cpp:47
Okteta::KValueEditor::adaptToValueCodecChange
void adaptToValueCodecChange()
Definition: kvalueeditor.cpp:48
Okteta::ValueCodec::removeLastDigit
virtual void removeLastDigit(Byte *byte) const =0
Tries to remove the last (least significant) digit from byte.
Okteta::KValueEditor::ValueEdit
Definition: kvalueeditor.h:44
Okteta::KController
Definition: kcontroller.h:32
Okteta::ValueCodec::encode
virtual void encode(QString &digits, unsigned int pos, Byte byte) const =0
Encodes the byte using full coding width, prefixing with 0s if needed, and writes the result to digit...
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