bineditorplugin.cpp 17.5 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>
con's avatar
con committed
39 40 41 42
#include <QtGui/QMenu>
#include <QtGui/QAction>
#include <QtGui/QMainWindow>
#include <QtGui/QHBoxLayout>
con's avatar
con committed
43
#include <QtGui/QToolBar>
con's avatar
con committed
44

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

using namespace BINEditor;
using namespace BINEditor::Internal;


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

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

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

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


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

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

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

ck's avatar
ck committed
130
    Result findStep(const QString &txt, Find::IFindSupport::FindFlags findFlags) {
con's avatar
con committed
131 132
        QByteArray pattern = txt.toLatin1();
        bool wasReset = (m_incrementalStartPos < 0);
ck's avatar
ck committed
133 134 135 136 137 138 139 140 141
        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
142
            m_incrementalStartPos = found;
ck's avatar
ck committed
143 144 145 146 147 148 149 150 151 152 153 154 155
            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
156
    }
ck's avatar
ck committed
157

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

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


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;
180 181
        connect(m_editor, SIGNAL(lazyDataRequested(Core::IEditor *, quint64, bool)),
            this, SLOT(provideData(Core::IEditor *, quint64)));
con's avatar
con committed
182 183 184 185 186 187
    }
    ~BinEditorFile() {}

    virtual QString mimeType() const { return m_mimeType; }

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

dt's avatar
dt committed
199 200 201 202 203 204
    void rename(const QString &newName) {
        m_fileName = newName;
        m_editor->editorInterface()->setDisplayName(QFileInfo(fileName()).fileName());
        emit changed();
    }

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

223
private slots:
224
    void provideData(Core::IEditor *, quint64 block) {
225 226 227 228 229 230 231 232 233 234 235 236
        QFile file(m_fileName);
        if (file.open(QIODevice::ReadOnly)) {
            int blockSize = m_editor->lazyDataBlockSize();
            file.seek(block * blockSize);
            QByteArray data = file.read(blockSize);
            if (data.size() != blockSize)
                data.resize(blockSize);
            m_editor->addLazyData(block, data);
            file.close();
        }
    }
public:
con's avatar
con committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

    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; }

261 262 263 264 265 266 267 268 269 270
    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
271

272 273
    void reload(ReloadFlag flag, ChangeType type) {
        if (flag == FlagIgnore)
con's avatar
con committed
274
            return;
275
        if (type == TypePermissions) {
con's avatar
con committed
276
            emit changed();
277 278
        } else {
            open(m_fileName);
con's avatar
con committed
279 280 281 282 283 284 285 286 287 288 289 290 291
        }
    }

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

class BinEditorInterface : public Core::IEditor
{
    Q_OBJECT
public:
292
    BinEditorInterface(BinEditor *editor)
293
    {
294
        Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
295 296
        m_editor = editor;
        m_file = new BinEditorFile(m_editor);
297
        m_context << uidm->uniqueIdentifier(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
298
        m_context << uidm->uniqueIdentifier(Constants::C_BINEDITOR);
299
        m_cursorPositionLabel = new Utils::LineColumnLabel;
con's avatar
con committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313

        QHBoxLayout *l = new QHBoxLayout;
        QWidget *w = new QWidget;
        l->setMargin(0);
        l->setContentsMargins(0, 0, 5, 0);
        l->addStretch(1);
        l->addWidget(m_cursorPositionLabel);
        w->setLayout(l);

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

        connect(m_editor, SIGNAL(cursorPositionChanged(int)), this, SLOT(updateCursorPosition(int)));
dt's avatar
dt committed
314
        connect(m_file, SIGNAL(changed()), this, SIGNAL(changed()));
con's avatar
con committed
315
    }
316 317 318
    ~BinEditorInterface() {
        delete m_editor;
    }
con's avatar
con committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332

    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; }
333
    QString id() const { return QLatin1String(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID); }
con's avatar
con committed
334 335 336 337
    QString displayName() const { return m_displayName; }
    void setDisplayName(const QString &title) { m_displayName = title; emit changed(); }

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

340 341
    QByteArray saveState() const { return QByteArray(); } // TODO
    bool restoreState(const QByteArray & /* state */) { return false; } // TODO
con's avatar
con committed
342

con's avatar
con committed
343
    QWidget *toolBar() { return m_toolBar; }
con's avatar
con committed
344

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

con's avatar
con committed
347 348 349 350 351 352 353 354 355 356 357 358
public slots:
    void updateCursorPosition(int position) {
        m_cursorPositionLabel->setText(m_editor->addressString((uint)position),
                                       m_editor->addressString((uint)m_editor->data().size()));
    }

private:
    BinEditor *m_editor;
    QString m_displayName;
    BinEditorFile *m_file;
    QList<int> m_context;
    QToolBar *m_toolBar;
359
    Utils::LineColumnLabel *m_cursorPositionLabel;
con's avatar
con committed
360 361 362 363 364 365 366 367 368 369 370 371 372
};




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

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

373
QString BinEditorFactory::id() const
con's avatar
con committed
374
{
375 376 377 378 379 380
    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
381 382 383 384
}

Core::IFile *BinEditorFactory::open(const QString &fileName)
{
385
    Core::EditorManager *em = Core::EditorManager::instance();
386
    Core::IEditor *iface = em->openEditor(fileName, id());
con's avatar
con committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    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 //////////////////////////////////

404
BinEditorPlugin::BinEditorPlugin()
con's avatar
con committed
405 406 407 408 409 410 411 412 413 414 415
{
    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);
416
    Core::ICore::instance()->actionManager()->registerAction(result, id, m_context);
con's avatar
con committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    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);

439
    Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
440
    m_context << uidm->uniqueIdentifier(Constants::C_BINEDITOR);
con's avatar
con committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    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);
}

470
bool BinEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
471
{
472
    Q_UNUSED(arguments)
473 474

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

478
    connect(core, SIGNAL(contextAboutToChange(Core::IContext *)),
con's avatar
con committed
479 480 481
        this, SLOT(updateCurrentEditor(Core::IContext *)));

    addAutoReleasedObject(new BinEditorFactory(this));
con's avatar
con committed
482
    addAutoReleasedObject(new ImageViewerFactory);
con's avatar
con committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 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
    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"