bineditorplugin.cpp 18.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 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
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
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
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32 33
#include "bineditorplugin.h"
#include "bineditor.h"
#include "bineditorconstants.h"

con's avatar
con committed
34 35
#include "imageviewer.h"

36
#include <QtCore/QFile>
con's avatar
con committed
37
#include <QtCore/QFileInfo>
38
#include <QtCore/QDebug>
39
#include <QtCore/QRegExp>
con's avatar
con committed
40 41 42 43
#include <QtGui/QMenu>
#include <QtGui/QAction>
#include <QtGui/QMainWindow>
#include <QtGui/QHBoxLayout>
44 45
#include <QtGui/QLineEdit>
#include <QtGui/QRegExpValidator>
con's avatar
con committed
46
#include <QtGui/QToolBar>
con's avatar
con committed
47

48
#include <coreplugin/actionmanager/actionmanager.h>
con's avatar
con committed
49
#include <coreplugin/coreconstants.h>
50
#include <coreplugin/editormanager/editormanager.h>
51
#include <coreplugin/editormanager/ieditor.h>
52
#include <coreplugin/icore.h>
53
#include <coreplugin/ifile.h>
con's avatar
con committed
54 55
#include <coreplugin/mimedatabase.h>
#include <coreplugin/uniqueidmanager.h>
56
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
57
#include <find/ifindsupport.h>
58 59
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorsettings.h>
con's avatar
con committed
60 61 62 63 64 65 66 67 68 69
#include <utils/reloadpromptutils.h>

using namespace BINEditor;
using namespace BINEditor::Internal;


class BinEditorFind : public Find::IFindSupport
{
    Q_OBJECT
public:
ck's avatar
ck committed
70 71 72 73 74
    BinEditorFind(BinEditor *editor)
    {
        m_editor = editor;
        m_incrementalStartPos = m_contPos = -1;
    }
con's avatar
con committed
75 76 77
    ~BinEditorFind() {}

    bool supportsReplace() const { return false; }
78 79 80 81 82
    IFindSupport::FindFlags supportedFindFlags() const
    {
        return IFindSupport::FindBackward | IFindSupport::FindCaseSensitively;
    }

ck's avatar
ck committed
83 84 85 86 87
    void resetIncrementalSearch()
    {
        m_incrementalStartPos = m_contPos = -1;
    }

con's avatar
con committed
88 89 90 91 92
    void clearResults() { m_editor->highlightSearchResults(QByteArray()); }
    QString currentFindString() const { return QString(); }
    QString completedFindString() const { return QString(); }


93
    int find(const QByteArray &pattern, int pos, Find::IFindSupport::FindFlags findFlags) {
con's avatar
con committed
94 95 96 97 98
        if (pattern.isEmpty()) {
            m_editor->setCursorPosition(pos);
            return pos;
        }

ck's avatar
ck committed
99
        return m_editor->find(pattern, pos, Find::IFindSupport::textDocumentFlagsForFindFlags(findFlags));
con's avatar
con committed
100 101
    }

ck's avatar
ck committed
102
    Result findIncremental(const QString &txt, Find::IFindSupport::FindFlags findFlags) {
con's avatar
con committed
103
        QByteArray pattern = txt.toLatin1();
ck's avatar
ck committed
104 105 106
        if (pattern != m_lastPattern)
            resetIncrementalSearch(); // Because we don't search for nibbles.
        m_lastPattern = pattern;
con's avatar
con committed
107 108
        if (m_incrementalStartPos < 0)
            m_incrementalStartPos = m_editor->selectionStart();
ck's avatar
ck committed
109 110 111 112 113 114
        if (m_contPos == -1)
            m_contPos = m_incrementalStartPos;
        int found = find(pattern, m_contPos, findFlags);
        Result result;
        if (found >= 0) {
            result = Found;
115
            m_editor->highlightSearchResults(pattern, Find::IFindSupport::textDocumentFlagsForFindFlags(findFlags));
ck's avatar
ck committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
            m_contPos = -1;
        } else {
            if (found == -2) {
                result = NotYetFound;
                m_contPos +=
                        findFlags & Find::IFindSupport::FindBackward
                        ? -BinEditor::SearchStride : BinEditor::SearchStride;
            } else {
                result = NotFound;
                m_contPos = -1;
                m_editor->highlightSearchResults(QByteArray(), 0);
            }
        }
        return result;
con's avatar
con committed
130 131
    }

ck's avatar
ck committed
132
    Result findStep(const QString &txt, Find::IFindSupport::FindFlags findFlags) {
con's avatar
con committed
133 134
        QByteArray pattern = txt.toLatin1();
        bool wasReset = (m_incrementalStartPos < 0);
ck's avatar
ck committed
135 136 137 138 139 140 141 142 143
        if (m_contPos == -1) {
            m_contPos = m_editor->cursorPosition();
            if (findFlags & Find::IFindSupport::FindBackward)
                m_contPos = m_editor->selectionStart()-1;
        }
        int found = find(pattern, m_contPos, findFlags);
        Result result;
        if (found >= 0) {
            result = Found;
con's avatar
con committed
144
            m_incrementalStartPos = found;
ck's avatar
ck committed
145 146 147 148 149 150 151 152 153 154 155 156 157
            m_contPos = -1;
            if (wasReset)
                m_editor->highlightSearchResults(pattern, Find::IFindSupport::textDocumentFlagsForFindFlags(findFlags));
        } else if (found == -2) {
            result = NotYetFound;
            m_contPos += findFlags & Find::IFindSupport::FindBackward
                         ? -BinEditor::SearchStride : BinEditor::SearchStride;
        } else {
            result = NotFound;
            m_contPos = -1;
        }

        return result;
con's avatar
con committed
158
    }
ck's avatar
ck committed
159

con's avatar
con committed
160
    bool replaceStep(const QString &, const QString &,
161
                     Find::IFindSupport::FindFlags) { return false;}
con's avatar
con committed
162
    int replaceAll(const QString &, const QString &,
163
                   Find::IFindSupport::FindFlags) { return 0; }
con's avatar
con committed
164 165 166 167

private:
    BinEditor *m_editor;
    int m_incrementalStartPos;
ck's avatar
ck committed
168 169
    int m_contPos; // Only valid if last result was NotYetFound.
    QByteArray m_lastPattern;
con's avatar
con committed
170 171 172 173 174 175 176 177 178 179 180 181
};


class BinEditorFile : public Core::IFile
{
    Q_OBJECT
public:
    BinEditorFile(BinEditor *parent) :
        Core::IFile(parent),
        m_mimeType(QLatin1String(BINEditor::Constants::C_BINEDITOR_MIMETYPE))
    {
        m_editor = parent;
182 183
        connect(m_editor, SIGNAL(lazyDataRequested(Core::IEditor *, quint64, bool)),
            this, SLOT(provideData(Core::IEditor *, quint64)));
ck's avatar
ck committed
184 185
        connect(m_editor, SIGNAL(newRangeRequested(Core::IEditor*,quint64)),
            this, SLOT(provideNewRange(Core::IEditor*,quint64)));
con's avatar
con committed
186 187 188 189 190 191
    }
    ~BinEditorFile() {}

    virtual QString mimeType() const { return m_mimeType; }

    bool save(const QString &fileName = QString()) {
ck's avatar
ck committed
192
        if (m_editor->save(m_fileName, fileName)) {
con's avatar
con committed
193
            m_fileName = fileName;
ck's avatar
ck committed
194 195
            m_editor->editorInterface()->
                setDisplayName(QFileInfo(fileName).fileName());
con's avatar
con committed
196 197
            emit changed();
            return true;
ck's avatar
ck committed
198 199
        } else {
            return false;
con's avatar
con committed
200 201 202
        }
    }

203
    bool open(const QString &fileName, quint64 offset = 0) {
con's avatar
con committed
204
        QFile file(fileName);
ck's avatar
ck committed
205 206
        if (offset < static_cast<quint64>(file.size())
            && file.open(QIODevice::ReadOnly)) {
con's avatar
con committed
207
            m_fileName = fileName;
208 209
            qint64 maxRange = 64 * 1024 * 1024;
            if (file.isSequential() && file.size() <= maxRange) {
210 211
                m_editor->setData(file.readAll());
            } else {
212
                m_editor->setLazyData(offset, maxRange);
ck's avatar
ck committed
213 214
                m_editor->editorInterface()->
                        setDisplayName(QFileInfo(fileName).fileName());
215
            }
con's avatar
con committed
216 217 218 219 220 221
            file.close();
            return true;
        }
        return false;
    }

222
private slots:
223
    void provideData(Core::IEditor *, quint64 block) {
224 225 226 227 228
        QFile file(m_fileName);
        if (file.open(QIODevice::ReadOnly)) {
            int blockSize = m_editor->lazyDataBlockSize();
            file.seek(block * blockSize);
            QByteArray data = file.read(blockSize);
229 230 231
            const int dataSize = data.size();
            if (dataSize != blockSize)
                data += QByteArray(blockSize - dataSize, 0);
232 233 234 235
            m_editor->addLazyData(block, data);
            file.close();
        }
    }
236

ck's avatar
ck committed
237 238
    void provideNewRange(Core::IEditor *, quint64 offset) {
        open(m_fileName, offset);
239 240
    }

241
public:
con's avatar
con committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

    void setFilename(const QString &filename) {
        m_fileName = filename;
    }

    QString fileName() const {
        return m_fileName;
    }

    QString defaultPath() const { return QString(); }
    QString suggestedFileName() const { return QString(); }
    QString fileFilter() const { return QString(); }
    QString fileExtension() const { return QString(); }

    bool isModified() const {
        return m_editor->isModified();
    }
    bool isReadOnly() const {
        const QFileInfo fi(m_fileName);
        return !fi.isWritable();
    }

    bool isSaveAsAllowed() const { return true; }

266 267 268 269 270 271 272 273 274 275
    ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const {
        if (type == TypePermissions)
            return BehaviorSilent;
        if (type == TypeContents) {
            if (state == TriggerInternal && !isModified())
                return BehaviorSilent;
            return BehaviorAsk;
        }
        return BehaviorAsk;
    }
con's avatar
con committed
276

277 278
    void reload(ReloadFlag flag, ChangeType type) {
        if (flag == FlagIgnore)
con's avatar
con committed
279
            return;
280
        if (type == TypePermissions) {
con's avatar
con committed
281
            emit changed();
282
        } else {
283 284 285
            emit aboutToReload();
            if (open(m_fileName))
                emit reloaded();
con's avatar
con committed
286 287 288 289 290 291 292 293 294 295 296 297 298
        }
    }

private:
    const QString m_mimeType;
    BinEditor *m_editor;
    QString m_fileName;
};

class BinEditorInterface : public Core::IEditor
{
    Q_OBJECT
public:
299
    BinEditorInterface(BinEditor *editor)
300
    {
301
        Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
302 303
        m_editor = editor;
        m_file = new BinEditorFile(m_editor);
304
        m_context << uidm->uniqueIdentifier(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
305
        m_context << uidm->uniqueIdentifier(Constants::C_BINEDITOR);
306 307 308 309 310
        m_addressEdit = new QLineEdit;
        QRegExpValidator * const addressValidator
            = new QRegExpValidator(QRegExp(QLatin1String("[0-9a-fA-F]{1,16}")),
                m_addressEdit);
        m_addressEdit->setValidator(addressValidator);
con's avatar
con committed
311 312 313 314 315 316

        QHBoxLayout *l = new QHBoxLayout;
        QWidget *w = new QWidget;
        l->setMargin(0);
        l->setContentsMargins(0, 0, 5, 0);
        l->addStretch(1);
317
        l->addWidget(m_addressEdit);
con's avatar
con committed
318 319 320 321 322 323
        w->setLayout(l);

        m_toolBar = new QToolBar;
        m_toolBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        m_toolBar->addWidget(w);

324 325 326 327 328
        connect(m_editor, SIGNAL(cursorPositionChanged(int)), this,
            SLOT(updateCursorPosition(int)));
        connect(m_addressEdit, SIGNAL(editingFinished()), this,
            SLOT(jumpToAddress()));
        updateCursorPosition(m_editor->cursorPosition());
con's avatar
con committed
329
    }
330 331 332
    ~BinEditorInterface() {
        delete m_editor;
    }
con's avatar
con committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346

    QWidget *widget() { return m_editor; }

    QList<int> context() const { return m_context; }

    bool createNew(const QString & /* contents */ = QString()) {
        m_editor->setData(QByteArray());
        m_file->setFilename(QString());
        return true;
    }
    bool open(const QString &fileName = QString()) {
        return m_file->open(fileName);
    }
    Core::IFile *file() { return m_file; }
347
    QString id() const { return QLatin1String(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID); }
con's avatar
con committed
348 349 350 351
    QString displayName() const { return m_displayName; }
    void setDisplayName(const QString &title) { m_displayName = title; emit changed(); }

    bool duplicateSupported() const { return false; }
352
    IEditor *duplicate(QWidget * /* parent */) { return 0; }
con's avatar
con committed
353

354 355
    QByteArray saveState() const { return QByteArray(); } // TODO
    bool restoreState(const QByteArray & /* state */) { return false; } // TODO
con's avatar
con committed
356

con's avatar
con committed
357
    QWidget *toolBar() { return m_toolBar; }
con's avatar
con committed
358

con's avatar
con committed
359
    bool isTemporary() const { return false; }
dt's avatar
dt committed
360

361
private slots:
con's avatar
con committed
362
    void updateCursorPosition(int position) {
363 364 365 366 367 368
        m_addressEdit->setText(QString::number(m_editor->baseAddress() + position, 16));
    }

    void jumpToAddress() {
        m_editor->jumpToAddress(m_addressEdit->text().toULongLong(0, 16));
        updateCursorPosition(m_editor->cursorPosition());
con's avatar
con committed
369 370 371 372 373 374 375 376
    }

private:
    BinEditor *m_editor;
    QString m_displayName;
    BinEditorFile *m_file;
    QList<int> m_context;
    QToolBar *m_toolBar;
377
    QLineEdit *m_addressEdit;
con's avatar
con committed
378 379 380 381 382 383 384 385 386 387 388 389 390
};




///////////////////////////////// BinEditorFactory //////////////////////////////////

BinEditorFactory::BinEditorFactory(BinEditorPlugin *owner) :
    m_mimeTypes(QLatin1String(BINEditor::Constants::C_BINEDITOR_MIMETYPE)),
    m_owner(owner)
{
}

391
QString BinEditorFactory::id() const
con's avatar
con committed
392
{
393 394 395 396 397 398
    return QLatin1String(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
}

QString BinEditorFactory::displayName() const
{
    return tr(Constants::C_BINEDITOR_DISPLAY_NAME);
con's avatar
con committed
399 400 401 402
}

Core::IFile *BinEditorFactory::open(const QString &fileName)
{
403
    Core::EditorManager *em = Core::EditorManager::instance();
404
    Core::IEditor *iface = em->openEditor(fileName, id());
con's avatar
con committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    return iface ? iface->file() : 0;
}

Core::IEditor *BinEditorFactory::createEditor(QWidget *parent)
{
    BinEditor *editor = new BinEditor(parent);
    m_owner->initializeEditor(editor);
    return editor->editorInterface();
}

QStringList BinEditorFactory::mimeTypes() const
{
    return m_mimeTypes;
}

///////////////////////////////// BinEditorPlugin //////////////////////////////////

422
BinEditorPlugin::BinEditorPlugin()
con's avatar
con committed
423 424 425 426 427 428 429 430 431 432 433
{
    m_undoAction = m_redoAction = m_copyAction = m_selectAllAction = 0;
}

BinEditorPlugin::~BinEditorPlugin()
{
}

QAction *BinEditorPlugin::registerNewAction(const QString &id, const QString &title)
{
    QAction *result = new QAction(title, this);
434
    Core::ICore::instance()->actionManager()->registerAction(result, id, m_context);
con's avatar
con committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    return result;
}

QAction *BinEditorPlugin::registerNewAction(const QString &id,
                                            QObject *receiver,
                                            const char *slot,
                                            const QString &title)
{
    QAction *rc = registerNewAction(id, title);
    if (!rc)
        return 0;

    connect(rc, SIGNAL(triggered()), receiver, slot);
    return rc;
}

void BinEditorPlugin::initializeEditor(BinEditor *editor)
{
    BinEditorInterface *editorInterface = new BinEditorInterface(editor);
    QObject::connect(editor, SIGNAL(modificationChanged(bool)), editorInterface, SIGNAL(changed()));
    editor->setEditorInterface(editorInterface);

457
    Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
458
    m_context << uidm->uniqueIdentifier(Constants::C_BINEDITOR);
con's avatar
con committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    if (!m_undoAction) {
        m_undoAction      = registerNewAction(QLatin1String(Core::Constants::UNDO),
                                              this, SLOT(undoAction()),
                                              tr("&Undo"));
        m_redoAction      = registerNewAction(QLatin1String(Core::Constants::REDO),
                                              this, SLOT(redoAction()),
                                              tr("&Redo"));
        m_copyAction      = registerNewAction(QLatin1String(Core::Constants::COPY),
                                              this, SLOT(copyAction()));
        m_selectAllAction = registerNewAction(QLatin1String(Core::Constants::SELECTALL),
                                              this, SLOT(selectAllAction()));
    }

    // Font settings
    TextEditor::TextEditorSettings *settings = TextEditor::TextEditorSettings::instance();
    editor->setFontSettings(settings->fontSettings());
    connect(settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
            editor, SLOT(setFontSettings(TextEditor::FontSettings)));

    QObject::connect(editor, SIGNAL(undoAvailable(bool)), this, SLOT(updateActions()));
    QObject::connect(editor, SIGNAL(redoAvailable(bool)), this, SLOT(updateActions()));
    QObject::connect(editor, SIGNAL(copyAvailable(bool)), this, SLOT(updateActions()));

    Aggregation::Aggregate *aggregate = new Aggregation::Aggregate;
    BinEditorFind *binEditorFind = new BinEditorFind(editor);
    aggregate->add(binEditorFind);
    aggregate->add(editor);
}

488
bool BinEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
489
{
490
    Q_UNUSED(arguments)
491 492

    Core::ICore *core = Core::ICore::instance();
con's avatar
con committed
493 494
    if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/bineditor/ImageViewer.mimetypes.xml"), errorMessage))
        return false;
con's avatar
con committed
495

496
    connect(core, SIGNAL(contextAboutToChange(Core::IContext *)),
con's avatar
con committed
497 498 499
        this, SLOT(updateCurrentEditor(Core::IContext *)));

    addAutoReleasedObject(new BinEditorFactory(this));
con's avatar
con committed
500
    addAutoReleasedObject(new ImageViewerFactory);
con's avatar
con committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    return true;
}

void BinEditorPlugin::extensionsInitialized()
{
}

void BinEditorPlugin::updateCurrentEditor(Core::IContext *object)
{
    do {
        if (!object) {
            if (!m_currentEditor)
                return;

            m_currentEditor = 0;
            break;
        }
        BinEditor *editor = qobject_cast<BinEditor *>(object->widget());
        if (!editor) {
            if (!m_currentEditor)
                return;

            m_currentEditor = 0;
            break;
        }

        if (editor == m_currentEditor)
            return;

        m_currentEditor = editor;

    } while (false);
    updateActions();
}

void BinEditorPlugin::updateActions()
{
    bool hasEditor = (m_currentEditor != 0);
    if (m_selectAllAction)
        m_selectAllAction->setEnabled(hasEditor);
    if (m_undoAction)
        m_undoAction->setEnabled(m_currentEditor && m_currentEditor->isUndoAvailable());
    if (m_redoAction)
        m_redoAction->setEnabled(m_currentEditor && m_currentEditor->isRedoAvailable());
    if (m_copyAction)
        m_copyAction->setEnabled(m_currentEditor && m_currentEditor->hasSelection());
}

void BinEditorPlugin::undoAction()
{
    if (m_currentEditor)
        m_currentEditor->undo();
}

void BinEditorPlugin::redoAction()
{
    if (m_currentEditor)
        m_currentEditor->redo();
}

void BinEditorPlugin::copyAction()
{
    if (m_currentEditor)
        m_currentEditor->copy();
}

void BinEditorPlugin::selectAllAction()
{
    if (m_currentEditor)
        m_currentEditor->selectAll();
}


Q_EXPORT_PLUGIN(BinEditorPlugin)

#include "bineditorplugin.moc"