Skip to content

Commit

Permalink
Rev.G. Improve the hexeditor
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonsmartins committed Dec 22, 2023
1 parent fc4fc95 commit c415ca8
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 100 deletions.
20 changes: 20 additions & 0 deletions software/usbflashprog/i18n/ufprog_en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,25 @@
<source>Disconnect</source>
<translation>Disconnect</translation>
</message>
<message>
<source>The amount of data in the buffer is different from the device size.</source>
<translation>The amount of data in the buffer is different from the device size.</translation>
</message>
<message>
<source>Do you want to write the truncated data anyway?</source>
<translation>Do you want to write the truncated data anyway?</translation>
</message>
<message>
<source>Size: %1 | Checksum: 0x%2 [ADD16] | 0x%3 [CRC32]</source>
<translation>Size: %1 | Checksum: 0x%2 [ADD16] | 0x%3 [CRC32]</translation>
</message>
<message>
<source>0 - Byte Programming</source>
<translation>0 - Byte Programming</translation>
</message>
<message>
<source>Press to change unit</source>
<translation>Press to change unit</translation>
</message>
</context>
</TS>
20 changes: 20 additions & 0 deletions software/usbflashprog/i18n/ufprog_pt_BR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,25 @@
<source>Disconnect</source>
<translation>Desconectar</translation>
</message>
<message>
<source>The amount of data in the buffer is different from the device size.</source>
<translation>A quantidade de dados no editor é diferente do tamanho do dispositivo.</translation>
</message>
<message>
<source>Do you want to write the truncated data anyway?</source>
<translation>Você quer gravar o dispositivo com os dados truncados, mesmo assim?</translation>
</message>
<message>
<source>Size: %1 | Checksum: 0x%2 [ADD16] | 0x%3 [CRC32]</source>
<translation>Tamanho: %1 | Soma: 0x%2 [ADD16] | 0x%3 [CRC32]</translation>
</message>
<message>
<source>0 - Byte Programming</source>
<translation>0 - Gravação por Byte</translation>
</message>
<message>
<source>Press to change unit</source>
<translation>Aperte para alterar a unidade</translation>
</message>
</context>
</TS>
254 changes: 160 additions & 94 deletions software/usbflashprog/main/mainwindow.cpp

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions software/usbflashprog/main/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class MainWindow : public QMainWindow {
Ui::MainWindow *ui_;
/* @brief Pointer to QHexEditor widget. */
QHexEditor *hexeditor_;
/* @brief Pointer to checksum QLabel widget. */
QLabel *checksumLabel_;
/* @brief Pointer to QProgressDialog widget (Prog). */
QProgressDialog *progress_;
/* @brief Ports Enumerator Timer (Diag). */
Expand Down Expand Up @@ -204,6 +206,8 @@ class MainWindow : public QMainWindow {
QString getOpenDialogFilter_();
/* @brief Gets file filter of the save dialog (Editor). */
QString getSaveDialogFilter_();
/* @brief Updates checksum info (Editor). */
void updateCheckSum_();
/*
* @brief Connects to board via CDC port (Diag).
* @param state True to connect, false to disconnect.
Expand Down
16 changes: 15 additions & 1 deletion software/usbflashprog/main/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
<property name="toolTip">
<string>Press to select...</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
Expand Down Expand Up @@ -537,6 +540,9 @@
<height>25</height>
</size>
</property>
<property name="toolTip">
<string>Press to change unit</string>
</property>
<property name="styleSheet">
<string notr="true">QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;}</string>
</property>
Expand Down Expand Up @@ -601,6 +607,9 @@
<height>25</height>
</size>
</property>
<property name="toolTip">
<string>Press to change unit</string>
</property>
<property name="styleSheet">
<string notr="true">QComboBox::drop-down {border-width: 0px;} QComboBox::down-arrow {image: url(noimg); border-width: 0px;}</string>
</property>
Expand Down Expand Up @@ -701,10 +710,15 @@
</property>
<property name="minimumSize">
<size>
<width>50</width>
<width>120</width>
<height>25</height>
</size>
</property>
<item>
<property name="text">
<string>0 - Byte Programming</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">64</string>
Expand Down
2 changes: 1 addition & 1 deletion software/usbflashprog/third/QHexView/qhexview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ void QHexView::ensureVisible()
auto vlines = this->visibleLines();

if(pos.line >= (this->verticalScrollBar()->value() + vlines))
this->verticalScrollBar()->setValue(static_cast<int>(pos.line - vlines + 1));
this->verticalScrollBar()->setValue(static_cast<int>(pos.line - vlines + 2));
else if(pos.line < this->verticalScrollBar()->value())
this->verticalScrollBar()->setValue(static_cast<int>(pos.line));
else
Expand Down
60 changes: 56 additions & 4 deletions software/usbflashprog/ui/qhexeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void QHexEditor::putData(const QByteArray &data) {
QHexDocument::fromMemory<QMemoryBuffer>(data.left(size_));
this->setDocument(document);
changed_ = true;
emit changed(true);
emit changed();
}

QByteArray QHexEditor::getData(void) const {
Expand All @@ -103,8 +103,8 @@ void QHexEditor::fill(quint8 value) {
QByteArray data = QByteArray(size_, value);
QHexDocument *document = QHexDocument::fromMemory<QMemoryBuffer>(data);
this->setDocument(document);
changed_ = true;
emit changed(true);
changed_ = (value != 0xFF);
emit changed(changed_);
}

void QHexEditor::random(void) {
Expand All @@ -115,7 +115,7 @@ void QHexEditor::random(void) {
QHexDocument *document = QHexDocument::fromMemory<QMemoryBuffer>(data);
this->setDocument(document);
changed_ = true;
emit changed(true);
emit changed();
}

void QHexEditor::setSize(qint32 value) {
Expand All @@ -135,6 +135,7 @@ void QHexEditor::setSize(qint32 value) {
document = QHexDocument::fromMemory<QMemoryBuffer>(data);
this->setDocument(document);
size_ = value;
emit changed(false);
}

qint32 QHexEditor::size(void) const {
Expand Down Expand Up @@ -181,6 +182,22 @@ void QHexEditor::showReplaceDialog(void) {
dialog.setWindowFlags(dialog.windowFlags() &
~Qt::WindowContextHelpButtonHint);
dialog.exec();
QByteArray data = getData();
if (data.size() != size_) {
QHexCursor *cursor = hexCursor();
QHexPosition pos = cursor->position();
qint64 start = cursor->selectionStartOffset();
qint64 len = cursor->selectionLength();
if (data.size() < size_) {
data.append(QByteArray(size_ - data.size(), 0xFF));
} else if (data.size() > size_) {
data.resize(size_);
}
putData(data);
cursor->select(start);
cursor->selectSize(len);
cursor->move(pos);
}
}

void QHexEditor::onDataChanged(const QByteArray &data, quint32 offset,
Expand All @@ -189,6 +206,41 @@ void QHexEditor::onDataChanged(const QByteArray &data, quint32 offset,
emit changed();
}

void QHexEditor::keyPressEvent(QKeyEvent *e) {
QHexCursor *cursor = hexCursor();
auto start = cursor->selectionStartOffset();
auto end = cursor->selectionEndOffset();
switch (e->key()) {
case Qt::Key_Backspace:
case Qt::Key_Delete: {
if (end - start < 0) {
e->accept();
return;
}
QByteArray data = getData();
if (e->key() == Qt::Key_Backspace && start == end)
data = data.remove(start - 1, end - start + 1);
else
data = data.remove(start, end - start + 1);
data.append(QByteArray(end - start + 1, 0xFF));
putData(data);
if (e->key() == Qt::Key_Backspace)
cursor->move(start - 1);
else
cursor->move(end);
e->accept();
break;
}
case Qt::Key_Insert:
e->accept();
break;
default:
if (cursor->hasSelection()) cursor->move(start);
QHexView::keyPressEvent(e);
break;
}
}

void QHexEditor::setGroupLength(unsigned int l) {
QHexView::setGroupLength(l);
}
7 changes: 7 additions & 0 deletions software/usbflashprog/ui/qhexeditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef UI_QHEXEDITOR_HPP_
#define UI_QHEXEDITOR_HPP_

#include <QKeyEvent>

#include <qhexview.h>
#include "backend/epromfile/qepromfile.hpp"

Expand Down Expand Up @@ -142,6 +144,11 @@ class QHexEditor : public QHexView {
void onDataChanged(const QByteArray& data, quint32 offset,
QHexDocument::ChangeReason reason);

protected:
/* @brief Key Press Event handler
* @param e Pointer to the QKeyEvent object. */
void keyPressEvent(QKeyEvent* e) override;

private:
/* @brief Data changed status. */
bool changed_;
Expand Down

0 comments on commit c415ca8

Please sign in to comment.