Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Add more representations of unprintable bytes #380

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

prybicki
Copy link
Contributor

@prybicki prybicki commented Nov 30, 2017

  • Convert byte to pretty unicode representation
  • Create gui options of default unprintable characters rendering
  • Save unprintable mode default settings
  • Add icon to view toolbar
  • Add instant unprintable mode changing

TODO: Find reasonable icon.


This change is Reviewable

*) Convert byte to pretty unicode representation
*) Create gui options of default unprintable characters rendering
*) Save unprintable mode default settings
*) Add icon to view toolbar
*) Add instant unprintable mode changing
@prybicki prybicki requested a review from mkow November 30, 2017 17:46
@@ -177,6 +179,8 @@ class HexEdit : public QAbstractScrollArea {
QScopedPointer<util::encoders::TextEncoder> textEncoder_;
util::EditEngine edit_engine_;

QString unprintablesModeString;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private field, so it should be named unprintables_mode_string_.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

#include "util/settings/theme.h"

#include <unistd.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't use this header, it's non-standard (and as a result it doesn't compile on Windows, for example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a debugging leftover, removed.

@@ -475,6 +471,21 @@ HexEdit::HexEdit(FileBlobModel* dataModel, QItemSelectionModel* selectionModel,
});
}

void HexEdit::updateAsciiCache() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please place this method in the same place as it was in the header file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for HexEdit::updateHexCache()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -25,6 +27,8 @@ int columnsNumber();
void setColumnsNumber(int number);
bool resizeColumnsToWindowWidth();
void setResizeColumnsToWindowWidth(bool on);
QString unprintablesModes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it should be unprintablesMode (without s).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, typo. Thanks for catching.

@@ -25,6 +27,8 @@ int columnsNumber();
void setColumnsNumber(int number);
bool resizeColumnsToWindowWidth();
void setResizeColumnsToWindowWidth(bool on);
QString unprintablesModes();
void setUnprintablesMode(QString mode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be const QString& mode.

Copy link
Contributor Author

@prybicki prybicki Dec 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewritten.

}

// ascii-unprintable chars
char a = (static_cast<char>(byte_val));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conversion is implementation-defined for byte_val >= 0x80, please wrap the higher values by hand (or create a helper function in veles::utils::).
And yes, this seems to be a general problem with Qt APIs - they use char* everywhere for byte arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -814,7 +853,7 @@ void HexEdit::paintEvent(QPaintEvent* event) {
auto bgc = byteBackroundColorFromPos(
byte_idx, modified_positions[byte_idx - start_byte]);
bool redraw_hex = invalidated_rect.intersects(hex_rect);
bool redraw_ascii = invalidated_rect.intersects(ascii_rect);
bool redraw_ascii = true; // invalidated_rect.intersects(ascii_rect);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A leftover from debugging? ;)

Copy link
Contributor Author

@prybicki prybicki Dec 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, removed.

unprintables_menu_.clear();
unprintables_menu_.addAction("dots");
unprintables_menu_.addSeparator();
unprintables_menu_.addAction("windows-1250");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

windows-1250 -> Windows-1250

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewritten.

void HexEditWidget::initUnprintablesMenu() {
unprintables_menu_.clear();
unprintables_menu_.addAction("dots");
unprintables_menu_.addSeparator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a separator here? We have only two options, so it not too useful now ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@@ -302,6 +319,13 @@ void HexEditWidget::initParsersMenu() {
}
}

void HexEditWidget::initUnprintablesMenu() {
unprintables_menu_.clear();
unprintables_menu_.addAction("dots");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dots -> Dots

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewritten.

@@ -39,6 +40,11 @@ namespace ui {
class HexEdit : public QAbstractScrollArea {
Q_OBJECT
public:
enum class UnprintablesMode {
Dots,
Windows_1250,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use uppercase names (see Google style guide)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

enum class UnprintablesMode {
Dots,
Windows_1250,
}; // do not change the order as settings may invalidate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this comment right after the enum class ... line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


void HexEdit::setUnprintablesMode(UnprintablesMode mode) {
unprintables_mode_ = mode;
if (mode == UnprintablesMode::Windows_1250 && windows1250_codec_ == nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should do this check before assigning to unprintables_mode_.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

void HexEdit::setUnprintablesMode(UnprintablesMode mode) {
unprintables_mode_ = mode;
if (mode == UnprintablesMode::Windows_1250 && windows1250_codec_ == nullptr) {
QMessageBox::warning(this, "Error", "Windows-1250 is unavailable.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Windows-1250 is -> "Windows-1250 encoding is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -602,6 +598,27 @@ void HexEdit::saveToFile(const QString& file_name) {
saveDataToFile(0, edit_engine_.dataSize(), file_name);
}

QString HexEdit::unprintablesModeToString(UnprintablesMode mode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method can be static.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


bool is_whitespace = unicode_repr.isSpace() || unicode_repr.isNull();

if (is_whitespace || is_undefined_windows1250) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a check for 0x7f here, it decodes to DEL and it's not printable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

// unprintable ASCII chars
return byte_val >= 0x7f ? windows1250_codec_->toUnicode(&a, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>= 0x7f -> > 0x7f

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

QAction* action = new QAction(hex_edit_->unprintablesModeToString(mode),
&unprintables_menu_);
connect(action, &QAction::triggered,
[=]() { this->hex_edit_->setUnprintablesMode(mode); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use [this, mode], it's less error-prone and more explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -18,6 +18,7 @@

#include <QMessageBox>

#include <ui/hexedit.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix:

  • Include grouping
  • Use of <>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/util/misc.cc Outdated
@@ -15,9 +15,26 @@
*
*/
#include "util/misc.h"
#include <cstdint>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix include grouping.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@mkow mkow changed the title [WIP] Add more representations of unprintable bytes Add more representations of unprintable bytes Dec 9, 2017
Copy link
Contributor

@mkow mkow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now. We can merge after I find a more reasonable icon for the button.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants