SILICON
Loading...
Searching...
No Matches
binaryEditor.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <cstddef>
21#include <cstdint>
22#include <memory>
23#include <optional>
24#include <vector>
25
26#include <QByteArray>
27#include <QWidget>
28
29#include <ui/lifetime.hpp>
31
32class QLineEdit;
33class QUndoStack;
34
35namespace SILICON::ui {
36
37class BinaryHexView;
38class BinaryUndoStack;
39
41class BinaryEditor : public QWidget {
42public:
43 explicit BinaryEditor(QWidget* parent = nullptr);
44 ~BinaryEditor() override;
45
46 void setData(QByteArray data);
47 [[nodiscard]] const QByteArray& data() const noexcept;
48
49 bool setByteOffset(std::size_t offset);
50
51 [[nodiscard]] bool canReadValue(std::size_t bitWidth) const;
52 [[nodiscard]] std::optional<std::uint64_t> readValue(std::size_t bitWidth) const;
53 bool overwriteValue(std::uint64_t value, std::size_t bitWidth,
54 const QString& undoText = {});
55 bool overwriteHexDigit(unsigned digit);
56
57 [[nodiscard]] QUndoStack* history() const noexcept;
58 [[nodiscard]] bool isModified() const;
59 void setModified(bool modified);
60
61 void refreshInspectors();
63 void zoomBy(int steps);
64
65private:
66 friend class BinaryHexView;
68
69 struct Inspector {
70 QLineEdit* edit;
72 std::size_t width;
73 };
74
75 [[nodiscard]] unsigned nibbleAt(std::size_t offset) const;
76 void applyNibbles(std::size_t offset, const std::vector<unsigned>& values);
77 void pushNibbleEdit(std::size_t offset, std::vector<unsigned> values,
78 const QString& text);
79 void commitInspector(Inspector& inspector);
80 void commitOffset();
81 void refreshOffset();
82 void updateEditorState();
83
84 QByteArray bytes;
85 std::size_t selectedByte = 0;
86 bool highHexDigitEntered = false;
88 QLineEdit* offsetEdit;
89 std::vector<Inspector> inspectors;
90 std::unique_ptr<BinaryUndoStack> undoHistory;
92};
93
94} // namespace SILICON::ui
bool overwriteHexDigit(unsigned digit)
void commitInspector(Inspector &inspector)
void applyNibbles(std::size_t offset, const std::vector< unsigned > &values)
std::optional< std::uint64_t > readValue(std::size_t bitWidth) const
friend class BinaryNibbleEditCommand
bool canReadValue(std::size_t bitWidth) const
QUndoStack * history() const noexcept
bool setByteOffset(std::size_t offset)
unsigned nibbleAt(std::size_t offset) const
bool overwriteValue(std::uint64_t value, std::size_t bitWidth, const QString &undoText={})
std::unique_ptr< BinaryUndoStack > undoHistory
std::vector< Inspector > inspectors
void setModified(bool modified)
void zoomBy(int steps)
Adjusts the hexadecimal view's text size by the given number of points.
void setData(QByteArray data)
BinaryEditor(QWidget *parent=nullptr)
void pushNibbleEdit(std::size_t offset, std::vector< unsigned > values, const QString &text)
const QByteArray & data() const noexcept
SILICON::core::BusValueFormat format