bineditor.h 7.54 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34 35 36
#ifndef BINEDITOR_H
#define BINEDITOR_H

37
#include <QtCore/QBasicTimer>
ck's avatar
ck committed
38
#include <QtCore/QMap>
39 40
#include <QtCore/QSet>
#include <QtCore/QStack>
ck's avatar
ck committed
41
#include <QtCore/QString>
42 43 44 45

#include <QtGui/QAbstractScrollArea>
#include <QtGui/QTextDocument>
#include <QtGui/QTextFormat>
con's avatar
con committed
46

47 48
QT_FORWARD_DECLARE_CLASS(QMenu)

con's avatar
con committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62
namespace Core {
class IEditor;
}

namespace TextEditor {
class FontSettings;
}

namespace BINEditor {

class BinEditor : public QAbstractScrollArea
{
    Q_OBJECT
    Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
63
    Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly DESIGNABLE false)
con's avatar
con committed
64

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

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

hjk's avatar
hjk committed
71 72 73
    Q_INVOKABLE void setSizes(quint64 startAddr, int range, bool fixedSize, int blockSize = 4096);
    int dataBlockSize() const { return m_blockSize; }
    Q_INVOKABLE void addData(quint64 block, const QByteArray &data);
74
    Q_INVOKABLE void setNewWindowRequestAllowed();
75
    Q_INVOKABLE void updateContents();
ck's avatar
ck committed
76
    bool save(const QString &oldFileName, const QString &newFileName);
77

con's avatar
con committed
78 79 80 81 82 83 84 85 86
    void zoomIn(int range = 1);
    void zoomOut(int range = 1);

    enum MoveMode {
        MoveAnchor,
        KeepAnchor
    };

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

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

93 94 95
    void setReadOnly(bool);
    bool isReadOnly() const;

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

    void selectAll();
    void clear();

    void undo();
    void redo();

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

    bool hasSelection() const { return m_cursorPosition != m_anchorPosition; }
    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
117
    QString addressString(quint64 address);
con's avatar
con committed
118

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

public Q_SLOTS:
    void setFontSettings(const TextEditor::FontSettings &fs);
hjk's avatar
hjk committed
123 124
    void highlightSearchResults(const QByteArray &pattern,
        QTextDocument::FindFlags findFlags = 0);
125
    void copy(bool raw = false);
con's avatar
con committed
126 127 128 129 130 131 132 133

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

hjk's avatar
hjk committed
134
    void dataRequested(Core::IEditor *editor, quint64 block, bool synchronous);
135
    void newWindowRequested(quint64 address);
ck's avatar
ck committed
136
    void newRangeRequested(Core::IEditor *, quint64 address);
137 138
    void startOfFileRequested(Core::IEditor *);
    void endOfFileRequested(Core::IEditor *);
hjk's avatar
hjk committed
139
    void dataChanged(Core::IEditor *, quint64 address, const QByteArray &data);
140

con's avatar
con committed
141 142 143 144 145 146 147 148 149 150 151 152 153
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 *);
154
    void contextMenuEvent(QContextMenuEvent *event);
con's avatar
con committed
155 156

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

167 168 169 170
    int dataIndexOf(const QByteArray &pattern, int from, bool caseSensitive = true) const;
    int dataLastIndexOf(const QByteArray &pattern, int from, bool caseSensitive = true) const;

    bool requestDataAt(int pos, bool synchronous = false) const;
171 172 173
    bool requestOldDataAt(int pos) const;
    char dataAt(int pos, bool old = false) const;
    char oldDataAt(int pos) const;
174
    void changeDataAt(int pos, char c);
175
    QByteArray dataMid(int from, int length, bool old = false) const;
176
    QByteArray blockData(int block, bool old = false) const;
177

178
    QPoint offsetToPos(int offset);
179 180
    void asIntegers(int offset, int count, quint64 &beValue, quint64 &leValue,
        bool old = false);
181

con's avatar
con committed
182
    int m_unmodifiedState;
183
    int m_readOnly;
con's avatar
con committed
184 185 186 187 188 189 190 191 192 193 194
    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
195
    quint64 m_baseAddr;
hjk's avatar
hjk committed
196
    bool m_fixedSize;
con's avatar
con committed
197 198 199 200 201 202 203 204 205 206

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

    QByteArray m_searchPattern;
    QByteArray m_searchPatternHex;
207
    bool m_caseSensitiveSearch;
con's avatar
con committed
208 209 210 211 212 213 214

    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
215 216
    void updateLines();
    void updateLines(int fromPosition, int toPosition);
con's avatar
con committed
217 218 219 220 221
    void ensureCursorVisible();
    void setBlinkingCursorEnabled(bool enable);

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

hjk's avatar
hjk committed
222 223
    int findPattern(const QByteArray &data, const QByteArray &dataHex,
        int from, int offset, int *match);
con's avatar
con committed
224
    void drawItems(QPainter *painter, int x, int y, const QString &itemString);
225
    void drawChanges(QPainter *painter, int x, int y, const char *changes);
con's avatar
con committed
226

227 228 229
    void setupJumpToMenuAction(QMenu *menu, QAction *actionHere, QAction *actionNew,
                               quint64 addr);

con's avatar
con committed
230 231 232 233 234 235 236 237 238 239
    struct BinEditorEditCommand {
        int position;
        uchar character;
        bool highNibble;
    };
    QStack<BinEditorEditCommand> m_undoStack, m_redoStack;

    QBasicTimer m_autoScrollTimer;
    Core::IEditor *m_ieditor;
    QString m_addressString;
240
    int m_addressBytes;
241
    bool m_canRequestNewWindow;
con's avatar
con committed
242 243 244 245 246
};

} // namespace BINEditor

#endif // BINEDITOR_H