Skip to content

Commit

Permalink
Add the QHexEdit widget
Browse files Browse the repository at this point in the history
Add the QHexEdit widget as some sort of library. Feel free to improve
the project files - I won't, every build system ever made is just
horrible.
  • Loading branch information
MKleusberg committed Mar 16, 2013
1 parent ac9901a commit 0cf9ecd
Show file tree
Hide file tree
Showing 13 changed files with 2,346 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Makefile
sqlitedbbrowser.pro.user
src/qrc_icons.cpp
src/sqlitebrowser
Makefile.antlr
*.so
*.so.*
15 changes: 15 additions & 0 deletions libs/qhexedit/qhexedit.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
TEMPLATE = lib

MOC_DIR = .moc

HEADERS += \
src/commands.h \
src/qhexedit.h \
src/qhexedit_p.h \
src/xbytearray.h

SOURCES += \
src/commands.cpp \
src/qhexedit.cpp \
src/qhexedit_p.cpp \
src/xbytearray.cpp
115 changes: 115 additions & 0 deletions libs/qhexedit/src/commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "commands.h"

CharCommand::CharCommand(XByteArray * xData, Cmd cmd, int charPos, char newChar, QUndoCommand *parent)
: QUndoCommand(parent)
{
_xData = xData;
_charPos = charPos;
_newChar = newChar;
_cmd = cmd;
}

bool CharCommand::mergeWith(const QUndoCommand *command)
{
const CharCommand *nextCommand = static_cast<const CharCommand *>(command);
bool result = false;

if (_cmd != remove)
{
if (nextCommand->_cmd == replace)
if (nextCommand->_charPos == _charPos)
{
_newChar = nextCommand->_newChar;
result = true;
}
}
return result;
}

void CharCommand::undo()
{
switch (_cmd)
{
case insert:
_xData->remove(_charPos, 1);
break;
case replace:
_xData->replace(_charPos, _oldChar);
_xData->setDataChanged(_charPos, _wasChanged);
break;
case remove:
_xData->insert(_charPos, _oldChar);
_xData->setDataChanged(_charPos, _wasChanged);
break;
}
}

void CharCommand::redo()
{
switch (_cmd)
{
case insert:
_xData->insert(_charPos, _newChar);
break;
case replace:
_oldChar = _xData->data()[_charPos];
_wasChanged = _xData->dataChanged(_charPos);
_xData->replace(_charPos, _newChar);
break;
case remove:
_oldChar = _xData->data()[_charPos];
_wasChanged = _xData->dataChanged(_charPos);
_xData->remove(_charPos, 1);
break;
}
}



ArrayCommand::ArrayCommand(XByteArray * xData, Cmd cmd, int baPos, QByteArray newBa, int len, QUndoCommand *parent)
: QUndoCommand(parent)
{
_cmd = cmd;
_xData = xData;
_baPos = baPos;
_newBa = newBa;
_len = len;
}

void ArrayCommand::undo()
{
switch (_cmd)
{
case insert:
_xData->remove(_baPos, _newBa.length());
break;
case replace:
_xData->replace(_baPos, _oldBa);
_xData->setDataChanged(_baPos, _wasChanged);
break;
case remove:
_xData->insert(_baPos, _oldBa);
_xData->setDataChanged(_baPos, _wasChanged);
break;
}
}

void ArrayCommand::redo()
{
switch (_cmd)
{
case insert:
_xData->insert(_baPos, _newBa);
break;
case replace:
_oldBa = _xData->data().mid(_baPos, _len);
_wasChanged = _xData->dataChanged(_baPos, _len);
_xData->replace(_baPos, _newBa);
break;
case remove:
_oldBa = _xData->data().mid(_baPos, _len);
_wasChanged = _xData->dataChanged(_baPos, _len);
_xData->remove(_baPos, _len);
break;
}
}
70 changes: 70 additions & 0 deletions libs/qhexedit/src/commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef COMMANDS_H
#define COMMANDS_H

/** \cond docNever */

#include <QUndoCommand>

#include "xbytearray.h"

/*! CharCommand is a class to prived undo/redo functionality in QHexEdit.
A QUndoCommand represents a single editing action on a document. CharCommand
is responsable for manipulations on single chars. It can insert. replace and
remove characters. A manipulation stores allways to actions
1. redo (or do) action
2. undo action.
CharCommand also supports command compression via mergeWidht(). This allows
the user to execute a undo command contation e.g. 3 steps in a single command.
If you for example insert a new byt "34" this means for the editor doing 3
steps: insert a "00", replace it with "03" and the replace it with "34". These
3 steps are combined into a single step, insert a "34".
*/
class CharCommand : public QUndoCommand
{
public:
enum { Id = 1234 };
enum Cmd {insert, remove, replace};

CharCommand(XByteArray * xData, Cmd cmd, int charPos, char newChar,
QUndoCommand *parent=0);

void undo();
void redo();
bool mergeWith(const QUndoCommand *command);
int id() const { return Id; }

private:
XByteArray * _xData;
int _charPos;
bool _wasChanged;
char _newChar;
char _oldChar;
Cmd _cmd;
};

/*! ArrayCommand provides undo/redo functionality for handling binary strings. It
can undo/redo insert, replace and remove binary strins (QByteArrays).
*/
class ArrayCommand : public QUndoCommand
{
public:
enum Cmd {insert, remove, replace};
ArrayCommand(XByteArray * xData, Cmd cmd, int baPos, QByteArray newBa=QByteArray(), int len=0,
QUndoCommand *parent=0);
void undo();
void redo();

private:
Cmd _cmd;
XByteArray * _xData;
int _baPos;
int _len;
QByteArray _wasChanged;
QByteArray _newBa;
QByteArray _oldBa;
};

/** \endcond docNever */

#endif // COMMANDS_H
Loading

0 comments on commit 0cf9ecd

Please sign in to comment.