Dax89 / QHexView

A versatile Hexadecimal widget for Qt5
MIT License
323 stars 101 forks source link

Optional radio buttons that switch mode (insert / overwrite) inside header #59

Closed T-640 closed 2 years ago

T-640 commented 3 years ago

Instead of the “Ascii” abbreviation in the header’s third column perhaps it would be a good idea to put something more useful, optionally. Like two radio buttons that would allow to quickly and conveniently change the mode in which QHexView operates, insertion or overwriting.

T-640 commented 2 years ago

I found a solution that does not require any changes to the code. It may be a bit hacky but seems to work well. This example assumes version 5.0 is used.

QComboBox

#include <QCursor>
#include <QComboBox>
#include <QVBoxLayout>
#include <QApplication>

#include <qhexview.h>
#include <qmemorybuffer.h>

// This demonstratets insertion of QWidget into the QHexView.
// What is left out is automatically resizing the inserted widget upon changes in QHexView's size
// and connection of QComboBox with QHexView to change QHexCursor's mode.

int main (int argc, char *argv[])
{
    QApplication application (argc, argv);

    QComboBox* box = new QComboBox;
    // Some cursor needs to be set because there would be
    // problems from switching from text cursor to any other
    // when moving mouse from bottom to this widget.
    box->setCursor(QCursor(Qt::PointingHandCursor));
    box->addItems({"Overwrite", "Insert"});

    QHexDocument* document = QHexDocument::fromMemory<QMemoryBuffer>(QByteArray (128, 0x00));

    QHexOptions options;
    options.asciilabel = "";

    QHexView hex_view;
    hex_view.setAutoWidth(true);
    hex_view.setDocument(document);
    hex_view.setOptions(options);

    QVBoxLayout* layout = new QVBoxLayout;
    layout->setContentsMargins(0,0,0,0);
    layout->setAlignment(Qt::AlignTop | Qt::AlignRight);
    // This seems like a hack,
    // because I am not sure if one is ever supposed to set layout on the viewport widget.
    // But everything appears to be fine.
    hex_view.viewport()->setLayout(layout);
    layout->addWidget(box);

    box->setFixedHeight(QFontMetrics(hex_view.font()).height());
    hex_view.show();

    return application.exec();
}