bineditor.h 8.02 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
hjk's avatar
hjk committed
24 25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32
#ifndef BINEDITOR_H
#define BINEDITOR_H

33 34
#include "markup.h"

35 36 37 38 39
#include <QBasicTimer>
#include <QMap>
#include <QSet>
#include <QStack>
#include <QString>
40

41 42 43
#include <QAbstractScrollArea>
#include <QTextDocument>
#include <QTextFormat>
con's avatar
con committed
44

45
QT_FORWARD_DECLARE_CLASS(QMenu)
46
QT_FORWARD_DECLARE_CLASS(QHelpEvent)
47

con's avatar
con committed
48 49 50 51 52 53 54 55 56 57
namespace Core {
class IEditor;
}

namespace TextEditor {
class FontSettings;
}

namespace BINEditor {

58
class BinEditorWidget : public QAbstractScrollArea
con's avatar
con committed
59 60 61
{
    Q_OBJECT
    Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
62
    Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly DESIGNABLE false)
63 64
    Q_PROPERTY(QList<BINEditor::Markup> markup READ markup WRITE setMarkup DESIGNABLE false)
    Q_PROPERTY(bool newWindowRequestAllowed READ newWindowRequestAllowed WRITE setNewWindowRequestAllowed DESIGNABLE false)
hjk's avatar
hjk committed
65

hjk's avatar
hjk committed
66
public:
67 68
    BinEditorWidget(QWidget *parent = 0);
    ~BinEditorWidget();
con's avatar
con committed
69

70
    quint64 baseAddress() const { return m_baseAddr; }
71

72
    Q_INVOKABLE void setSizes(quint64 startAddr, int range, int blockSize = 4096);
hjk's avatar
hjk committed
73 74
    int dataBlockSize() const { return m_blockSize; }
    Q_INVOKABLE void addData(quint64 block, const QByteArray &data);
75 76 77

    bool newWindowRequestAllowed() const { return m_canRequestNewWindow; }

78
    Q_INVOKABLE void updateContents();
79
    bool save(QString *errorString, const QString &oldFileName, const QString &newFileName);
80

con's avatar
con committed
81 82 83 84 85 86 87 88 89
    void zoomIn(int range = 1);
    void zoomOut(int range = 1);

    enum MoveMode {
        MoveAnchor,
        KeepAnchor
    };

    int cursorPosition() const;
90
    Q_INVOKABLE void setCursorPosition(int pos, MoveMode moveMode = MoveAnchor);
91
    void jumpToAddress(quint64 address);
con's avatar
con committed
92 93 94 95

    void setModified(bool);
    bool isModified() const;

96 97 98
    void setReadOnly(bool);
    bool isReadOnly() const;

ck's avatar
ck committed
99 100
    int find(const QByteArray &pattern, int from = 0,
             QTextDocument::FindFlags findFlags = 0);
con's avatar
con committed
101 102 103 104 105 106 107

    void selectAll();
    void clear();

    void undo();
    void redo();

hjk's avatar
hjk committed
108 109
    Core::IEditor *editor() const { return m_ieditor; }
    void setEditor(Core::IEditor *ieditor) { m_ieditor = ieditor; }
con's avatar
con committed
110 111 112 113 114 115 116 117 118

    int selectionStart() const { return qMin(m_anchorPosition, m_cursorPosition); }
    int selectionEnd() const { return qMax(m_anchorPosition, m_cursorPosition); }

    bool event(QEvent*);

    bool isUndoAvailable() const { return m_undoStack.size(); }
    bool isRedoAvailable() const { return m_redoStack.size(); }

ck's avatar
ck committed
119
    QString addressString(quint64 address);
con's avatar
con committed
120

ck's avatar
ck committed
121
    static const int SearchStride = 1024 * 1024;
con's avatar
con committed
122

123 124
    QList<Markup> markup() const { return m_markup; }

con's avatar
con committed
125 126
public Q_SLOTS:
    void setFontSettings(const TextEditor::FontSettings &fs);
hjk's avatar
hjk committed
127 128
    void highlightSearchResults(const QByteArray &pattern,
        QTextDocument::FindFlags findFlags = 0);
129
    void copy(bool raw = false);
130 131
    void setMarkup(const QList<Markup> &markup);
    void setNewWindowRequestAllowed(bool c);
con's avatar
con committed
132 133 134 135 136 137 138

Q_SIGNALS:
    void modificationChanged(bool modified);
    void undoAvailable(bool);
    void redoAvailable(bool);
    void cursorPositionChanged(int position);

139
    void dataRequested(quint64 block);
140
    void newWindowRequested(quint64 address);
141
    void newRangeRequested(quint64 address);
142
    void addWatchpointRequested(quint64 address, uint size);
143
    void dataChanged(quint64 address, const QByteArray &data);
144

con's avatar
con committed
145 146 147 148 149 150 151 152 153 154 155 156 157
protected:
    void scrollContentsBy(int dx, int dy);
    void paintEvent(QPaintEvent *e);
    void resizeEvent(QResizeEvent *);
    void changeEvent(QEvent *);
    void wheelEvent(QWheelEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
    void keyPressEvent(QKeyEvent *e);
    void focusInEvent(QFocusEvent *);
    void focusOutEvent(QFocusEvent *);
    void timerEvent(QTimerEvent *);
158
    void contextMenuEvent(QContextMenuEvent *event);
con's avatar
con committed
159 160

private:
hjk's avatar
hjk committed
161 162 163
    typedef QMap<int, QByteArray> BlockMap;
    BlockMap m_data;
    BlockMap m_oldData;
164
    int m_blockSize;
hjk's avatar
hjk committed
165 166
    BlockMap m_modifiedData;
    mutable QSet<int> m_requests;
167
    QByteArray m_emptyBlock;
168
    QByteArray m_lowerBlock;
169 170
    int m_size;

171 172 173
    int dataIndexOf(const QByteArray &pattern, int from, bool caseSensitive = true) const;
    int dataLastIndexOf(const QByteArray &pattern, int from, bool caseSensitive = true) const;

hjk's avatar
hjk committed
174
    bool requestDataAt(int pos) const;
175 176 177
    bool requestOldDataAt(int pos) const;
    char dataAt(int pos, bool old = false) const;
    char oldDataAt(int pos) const;
178
    void changeDataAt(int pos, char c);
179
    QByteArray dataMid(int from, int length, bool old = false) const;
180
    QByteArray blockData(int block, bool old = false) const;
181

182 183 184 185 186 187
    QPoint offsetToPos(int offset) const;
    void asIntegers(int offset, int count, quint64 &bigEndianValue, quint64 &littleEndianValue,
        bool old = false) const;
    void asFloat(int offset, float &value, bool old) const;
    void asDouble(int offset, double &value, bool old) const;
    QString toolTip(const QHelpEvent *helpEvent) const;
188

hjk's avatar
hjk committed
189
    int m_bytesPerLine;
con's avatar
con committed
190
    int m_unmodifiedState;
191
    int m_readOnly;
con's avatar
con committed
192 193 194 195 196 197 198 199 200 201 202
    int m_margin;
    int m_descent;
    int m_ascent;
    int m_lineHeight;
    int m_charWidth;
    int m_labelWidth;
    int m_textWidth;
    int m_columnWidth;
    int m_numLines;
    int m_numVisibleLines;

ck's avatar
ck committed
203
    quint64 m_baseAddr;
con's avatar
con committed
204 205 206 207 208 209 210 211 212 213

    bool m_cursorVisible;
    int m_cursorPosition;
    int m_anchorPosition;
    bool m_hexCursor;
    bool m_lowNibble;
    bool m_isMonospacedFont;

    QByteArray m_searchPattern;
    QByteArray m_searchPatternHex;
214
    bool m_caseSensitiveSearch;
con's avatar
con committed
215 216 217 218 219 220 221

    QBasicTimer m_cursorBlinkTimer;

    void init();
    int posAt(const QPoint &pos) const;
    bool inTextArea(const QPoint &pos) const;
    QRect cursorRect() const;
hjk's avatar
hjk committed
222 223
    void updateLines();
    void updateLines(int fromPosition, int toPosition);
con's avatar
con committed
224 225 226 227 228
    void ensureCursorVisible();
    void setBlinkingCursorEnabled(bool enable);

    void changeData(int position, uchar character, bool highNibble = false);

hjk's avatar
hjk committed
229 230
    int findPattern(const QByteArray &data, const QByteArray &dataHex,
        int from, int offset, int *match);
con's avatar
con committed
231
    void drawItems(QPainter *painter, int x, int y, const QString &itemString);
232
    void drawChanges(QPainter *painter, int x, int y, const char *changes);
con's avatar
con committed
233

234 235 236
    void setupJumpToMenuAction(QMenu *menu, QAction *actionHere, QAction *actionNew,
                               quint64 addr);

con's avatar
con committed
237 238 239 240 241 242 243 244 245 246
    struct BinEditorEditCommand {
        int position;
        uchar character;
        bool highNibble;
    };
    QStack<BinEditorEditCommand> m_undoStack, m_redoStack;

    QBasicTimer m_autoScrollTimer;
    Core::IEditor *m_ieditor;
    QString m_addressString;
247
    int m_addressBytes;
248
    bool m_canRequestNewWindow;
249
    QList<Markup> m_markup;
con's avatar
con committed
250 251 252 253 254
};

} // namespace BINEditor

#endif // BINEDITOR_H