bineditor.h 8.13 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
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28 29
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

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

36 37
#include "markup.h"

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

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

48
QT_FORWARD_DECLARE_CLASS(QMenu)
49
QT_FORWARD_DECLARE_CLASS(QHelpEvent)
50

con's avatar
con committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
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)
65
    Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly DESIGNABLE false)
66 67
    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
68
public:
con's avatar
con committed
69 70 71
    BinEditor(QWidget *parent = 0);
    ~BinEditor();

72
    quint64 baseAddress() const { return m_baseAddr; }
73

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

    bool newWindowRequestAllowed() const { return m_canRequestNewWindow; }

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

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

    enum MoveMode {
        MoveAnchor,
        KeepAnchor
    };

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

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

98 99 100
    void setReadOnly(bool);
    bool isReadOnly() const;

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

    void selectAll();
    void clear();

    void undo();
    void redo();

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

    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
122
    QString addressString(quint64 address);
con's avatar
con committed
123

124 125
    bool isMemoryView() const; // Is a debugger memory view without file?

ck's avatar
ck committed
126
    static const int SearchStride = 1024 * 1024;
con's avatar
con committed
127

128 129
    QList<Markup> markup() const { return m_markup; }

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

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

hjk's avatar
hjk committed
145
    void dataRequested(Core::IEditor *editor, quint64 block);
146
    void newWindowRequested(quint64 address);
ck's avatar
ck committed
147
    void newRangeRequested(Core::IEditor *, quint64 address);
148 149
    void startOfFileRequested(Core::IEditor *);
    void endOfFileRequested(Core::IEditor *);
hjk's avatar
hjk committed
150
    void dataChanged(Core::IEditor *, quint64 address, const QByteArray &data);
151

con's avatar
con committed
152 153 154 155 156 157 158 159 160 161 162 163 164
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 *);
165
    void contextMenuEvent(QContextMenuEvent *event);
con's avatar
con committed
166 167

private:
hjk's avatar
hjk committed
168 169 170
    typedef QMap<int, QByteArray> BlockMap;
    BlockMap m_data;
    BlockMap m_oldData;
171
    int m_blockSize;
hjk's avatar
hjk committed
172 173
    BlockMap m_modifiedData;
    mutable QSet<int> m_requests;
174
    QByteArray m_emptyBlock;
175
    QByteArray m_lowerBlock;
176 177
    int m_size;

178 179 180
    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
181
    bool requestDataAt(int pos) const;
182 183 184
    bool requestOldDataAt(int pos) const;
    char dataAt(int pos, bool old = false) const;
    char oldDataAt(int pos) const;
185
    void changeDataAt(int pos, char c);
186
    QByteArray dataMid(int from, int length, bool old = false) const;
187
    QByteArray blockData(int block, bool old = false) const;
188

189 190 191 192 193 194
    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;
195

con's avatar
con committed
196
    int m_unmodifiedState;
197
    int m_readOnly;
con's avatar
con committed
198 199 200 201 202 203 204 205 206 207 208
    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
209
    quint64 m_baseAddr;
con's avatar
con committed
210 211 212 213 214 215 216 217 218 219

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

    QByteArray m_searchPattern;
    QByteArray m_searchPatternHex;
220
    bool m_caseSensitiveSearch;
con's avatar
con committed
221 222 223 224 225 226 227

    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
228 229
    void updateLines();
    void updateLines(int fromPosition, int toPosition);
con's avatar
con committed
230 231 232 233 234
    void ensureCursorVisible();
    void setBlinkingCursorEnabled(bool enable);

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

hjk's avatar
hjk committed
235 236
    int findPattern(const QByteArray &data, const QByteArray &dataHex,
        int from, int offset, int *match);
con's avatar
con committed
237
    void drawItems(QPainter *painter, int x, int y, const QString &itemString);
238
    void drawChanges(QPainter *painter, int x, int y, const char *changes);
con's avatar
con committed
239

240 241 242
    void setupJumpToMenuAction(QMenu *menu, QAction *actionHere, QAction *actionNew,
                               quint64 addr);

con's avatar
con committed
243 244 245 246 247 248 249 250 251 252
    struct BinEditorEditCommand {
        int position;
        uchar character;
        bool highNibble;
    };
    QStack<BinEditorEditCommand> m_undoStack, m_redoStack;

    QBasicTimer m_autoScrollTimer;
    Core::IEditor *m_ieditor;
    QString m_addressString;
253
    int m_addressBytes;
254
    bool m_canRequestNewWindow;
255
    QList<Markup> m_markup;
con's avatar
con committed
256 257 258 259 260
};

} // namespace BINEditor

#endif // BINEDITOR_H