Files
qt-creator/src/plugins/bineditor/bineditorplugin.cpp

605 lines
19 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** 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.
**
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
#include "bineditorplugin.h"
#include "bineditor.h"
#include "bineditorconstants.h"
#include <coreplugin/icore.h>
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QRegExp>
#include <QVariant>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QRegExpValidator>
#include <QToolBar>
2008-12-02 12:01:29 +01:00
#include <coreplugin/actionmanager/actionmanager.h>
2008-12-02 12:01:29 +01:00
#include <coreplugin/coreconstants.h>
#include <coreplugin/id.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
2008-12-02 12:01:29 +01:00
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h>
2008-12-02 12:01:29 +01:00
#include <find/ifindsupport.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorsettings.h>
2008-12-02 12:01:29 +01:00
#include <utils/reloadpromptutils.h>
#include <utils/qtcassert.h>
2008-12-02 12:01:29 +01:00
using namespace BINEditor;
using namespace BINEditor::Internal;
class BinEditorFind : public Find::IFindSupport
{
Q_OBJECT
public:
BinEditorFind(BinEditor *editor)
{
m_editor = editor;
m_incrementalStartPos = m_contPos = -1;
}
2008-12-02 12:01:29 +01:00
~BinEditorFind() {}
bool supportsReplace() const { return false; }
Find::FindFlags supportedFindFlags() const
{
return Find::FindBackward | Find::FindCaseSensitively;
}
void resetIncrementalSearch()
{
m_incrementalStartPos = m_contPos = -1;
}
virtual void highlightAll(const QString &txt, Find::FindFlags findFlags)
{
m_editor->highlightSearchResults(txt.toLatin1(), Find::textDocumentFlagsForFindFlags(findFlags));
}
2008-12-02 12:01:29 +01:00
void clearResults() { m_editor->highlightSearchResults(QByteArray()); }
QString currentFindString() const { return QString(); }
QString completedFindString() const { return QString(); }
int find(const QByteArray &pattern, int pos, Find::FindFlags findFlags) {
2008-12-02 12:01:29 +01:00
if (pattern.isEmpty()) {
m_editor->setCursorPosition(pos);
return pos;
}
return m_editor->find(pattern, pos, Find::textDocumentFlagsForFindFlags(findFlags));
2008-12-02 12:01:29 +01:00
}
Result findIncremental(const QString &txt, Find::FindFlags findFlags) {
2008-12-02 12:01:29 +01:00
QByteArray pattern = txt.toLatin1();
if (pattern != m_lastPattern)
resetIncrementalSearch(); // Because we don't search for nibbles.
m_lastPattern = pattern;
2008-12-02 12:01:29 +01:00
if (m_incrementalStartPos < 0)
m_incrementalStartPos = m_editor->selectionStart();
if (m_contPos == -1)
m_contPos = m_incrementalStartPos;
int found = find(pattern, m_contPos, findFlags);
Result result;
if (found >= 0) {
result = Found;
m_editor->highlightSearchResults(pattern, Find::textDocumentFlagsForFindFlags(findFlags));
m_contPos = -1;
} else {
if (found == -2) {
result = NotYetFound;
m_contPos +=
findFlags & Find::FindBackward
? -BinEditor::SearchStride : BinEditor::SearchStride;
} else {
result = NotFound;
m_contPos = -1;
m_editor->highlightSearchResults(QByteArray(), 0);
}
}
return result;
2008-12-02 12:01:29 +01:00
}
Result findStep(const QString &txt, Find::FindFlags findFlags) {
2008-12-02 12:01:29 +01:00
QByteArray pattern = txt.toLatin1();
bool wasReset = (m_incrementalStartPos < 0);
if (m_contPos == -1) {
m_contPos = m_editor->cursorPosition();
if (findFlags & Find::FindBackward)
m_contPos = m_editor->selectionStart()-1;
}
int found = find(pattern, m_contPos, findFlags);
Result result;
if (found >= 0) {
result = Found;
2008-12-02 12:01:29 +01:00
m_incrementalStartPos = found;
m_contPos = -1;
if (wasReset)
m_editor->highlightSearchResults(pattern, Find::textDocumentFlagsForFindFlags(findFlags));
} else if (found == -2) {
result = NotYetFound;
m_contPos += findFlags & Find::FindBackward
? -BinEditor::SearchStride : BinEditor::SearchStride;
} else {
result = NotFound;
m_contPos = -1;
}
return result;
2008-12-02 12:01:29 +01:00
}
2008-12-02 12:01:29 +01:00
private:
BinEditor *m_editor;
int m_incrementalStartPos;
int m_contPos; // Only valid if last result was NotYetFound.
QByteArray m_lastPattern;
2008-12-02 12:01:29 +01:00
};
class BinEditorDocument : public Core::IDocument
2008-12-02 12:01:29 +01:00
{
Q_OBJECT
public:
BinEditorDocument(BinEditor *parent) :
Core::IDocument(parent)
2008-12-02 12:01:29 +01:00
{
m_editor = parent;
connect(m_editor, SIGNAL(dataRequested(Core::IEditor*,quint64)),
this, SLOT(provideData(Core::IEditor*,quint64)));
connect(m_editor, SIGNAL(newRangeRequested(Core::IEditor*,quint64)),
this, SLOT(provideNewRange(Core::IEditor*,quint64)));
connect(m_editor, SIGNAL(startOfFileRequested(Core::IEditor*)), this,
SLOT(handleStartOfFileRequested(Core::IEditor*)));
connect(m_editor, SIGNAL(endOfFileRequested(Core::IEditor*)), this,
SLOT(handleEndOfFileRequested(Core::IEditor*)));
2008-12-02 12:01:29 +01:00
}
~BinEditorDocument() {}
2008-12-02 12:01:29 +01:00
2011-02-25 13:21:54 +01:00
QString mimeType() const {
return QLatin1String(Constants::C_BINEDITOR_MIMETYPE);
}
2008-12-02 12:01:29 +01:00
bool save(QString *errorString, const QString &fileName, bool autoSave)
{
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
const QString fileNameToUse
= fileName.isEmpty() ? m_fileName : fileName;
if (m_editor->save(errorString, m_fileName, fileNameToUse)) {
m_fileName = fileNameToUse;
2011-02-24 09:17:21 +01:00
m_editor->editor()->setDisplayName(QFileInfo(fileNameToUse).fileName());
2008-12-02 12:01:29 +01:00
emit changed();
return true;
} else {
return false;
2008-12-02 12:01:29 +01:00
}
}
void rename(const QString &newName) {
m_fileName = newName;
2011-02-24 09:17:21 +01:00
m_editor->editor()->setDisplayName(QFileInfo(fileName()).fileName());
emit changed();
}
bool open(QString *errorString, const QString &fileName, quint64 offset = 0) {
2008-12-02 12:01:29 +01:00
QFile file(fileName);
quint64 size = static_cast<quint64>(file.size());
if (size == 0 && !fileName.isEmpty()) {
QString msg = tr("The Binary Editor cannot open empty files.");
if (errorString)
*errorString = msg;
else
QMessageBox::critical(Core::ICore::mainWindow(), tr("File Error"), msg);
return false;
}
if (offset >= size)
return false;
if (file.open(QIODevice::ReadOnly)) {
file.close();
2008-12-02 12:01:29 +01:00
m_fileName = fileName;
m_editor->setSizes(offset, file.size());
2011-02-24 09:17:21 +01:00
m_editor->editor()->setDisplayName(QFileInfo(fileName).fileName());
2008-12-02 12:01:29 +01:00
return true;
}
QString errStr = tr("Cannot open %1: %2").arg(
QDir::toNativeSeparators(fileName), file.errorString());
if (errorString)
*errorString = errStr;
else
QMessageBox::critical(Core::ICore::mainWindow(), tr("File Error"), errStr);
2008-12-02 12:01:29 +01:00
return false;
}
private slots:
void provideData(Core::IEditor *, quint64 block) {
if (m_fileName.isEmpty())
return;
QFile file(m_fileName);
if (file.open(QIODevice::ReadOnly)) {
2011-02-24 09:17:21 +01:00
int blockSize = m_editor->dataBlockSize();
file.seek(block * blockSize);
QByteArray data = file.read(blockSize);
file.close();
const int dataSize = data.size();
if (dataSize != blockSize)
data += QByteArray(blockSize - dataSize, 0);
2011-02-24 09:17:21 +01:00
m_editor->addData(block, data);
} else {
QMessageBox::critical(Core::ICore::mainWindow(), tr("File Error"),
tr("Cannot open %1: %2").arg(
QDir::toNativeSeparators(m_fileName), file.errorString()));
}
}
void provideNewRange(Core::IEditor *, quint64 offset) {
open(0, m_fileName, offset);
}
void handleStartOfFileRequested(Core::IEditor *) {
open(0, m_fileName, 0);
}
void handleEndOfFileRequested(Core::IEditor *) {
open(0, m_fileName, QFileInfo(m_fileName).size() - 1);
}
public:
2008-12-02 12:01:29 +01:00
void setFilename(const QString &filename) {
m_fileName = filename;
}
2011-02-25 13:21:54 +01:00
QString fileName() const { return m_fileName; }
2008-12-02 12:01:29 +01:00
QString defaultPath() const { return QString(); }
2011-02-25 13:21:54 +01:00
2008-12-02 12:01:29 +01:00
QString suggestedFileName() const { return QString(); }
bool isModified() const { return m_editor->isMemoryView() ? false : m_editor->isModified(); }
2011-02-25 13:21:54 +01:00
bool isFileReadOnly() const {
if (m_editor->isMemoryView() || m_fileName.isEmpty())
2011-02-25 13:21:54 +01:00
return false;
2008-12-02 12:01:29 +01:00
const QFileInfo fi(m_fileName);
return !fi.isWritable();
}
bool isSaveAsAllowed() const { return true; }
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) {
if (flag == FlagIgnore)
return true;
if (type == TypePermissions) {
2008-12-02 12:01:29 +01:00
emit changed();
} else {
emit aboutToReload();
if (!open(errorString, m_fileName))
return false;
emit reloaded();
2008-12-02 12:01:29 +01:00
}
return true;
2008-12-02 12:01:29 +01:00
}
private:
BinEditor *m_editor;
QString m_fileName;
};
class BinEditorInterface : public Core::IEditor
{
Q_OBJECT
public:
BinEditorInterface(BinEditor *editor)
{
setWidget(editor);
m_editor = editor;
m_file = new BinEditorDocument(m_editor);
m_context.add(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
m_context.add(Constants::C_BINEDITOR);
m_addressEdit = new QLineEdit;
QRegExpValidator * const addressValidator
= new QRegExpValidator(QRegExp(QLatin1String("[0-9a-fA-F]{1,16}")),
m_addressEdit);
m_addressEdit->setValidator(addressValidator);
2008-12-02 12:01:29 +01:00
QHBoxLayout *l = new QHBoxLayout;
QWidget *w = new QWidget;
l->setMargin(0);
l->setContentsMargins(0, 0, 5, 0);
l->addStretch(1);
l->addWidget(m_addressEdit);
2008-12-02 12:01:29 +01:00
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)));
connect(m_file, SIGNAL(changed()), this, SIGNAL(changed()));
connect(m_addressEdit, SIGNAL(editingFinished()), this,
SLOT(jumpToAddress()));
updateCursorPosition(m_editor->cursorPosition());
2008-12-02 12:01:29 +01:00
}
~BinEditorInterface() {
delete m_editor;
}
2008-12-02 12:01:29 +01:00
bool createNew(const QString & /* contents */ = QString()) {
2011-02-24 09:17:21 +01:00
m_editor->clear();
2008-12-02 12:01:29 +01:00
m_file->setFilename(QString());
return true;
}
bool open(QString *errorString, const QString &fileName, const QString &realFileName) {
QTC_ASSERT(fileName == realFileName, return false); // The bineditor can do no autosaving
return m_file->open(errorString, fileName);
2008-12-02 12:01:29 +01:00
}
Core::IDocument *document() { return m_file; }
Core::Id id() const { return Core::Constants::K_DEFAULT_BINARY_EDITOR_ID; }
2008-12-02 12:01:29 +01:00
QString displayName() const { return m_displayName; }
void setDisplayName(const QString &title) { m_displayName = title; emit changed(); }
bool duplicateSupported() const { return false; }
IEditor *duplicate(QWidget * /* parent */) { return 0; }
2008-12-02 12:01:29 +01:00
QByteArray saveState() const { return QByteArray(); } // not supported
bool restoreState(const QByteArray & /* state */) { return false; } // not supported
2008-12-02 12:01:29 +01:00
2009-07-15 16:23:07 +02:00
QWidget *toolBar() { return m_toolBar; }
2008-12-02 12:01:29 +01:00
bool isTemporary() const { return m_editor->isMemoryView(); }
2009-05-18 19:11:11 +02:00
private slots:
2008-12-02 12:01:29 +01:00
void updateCursorPosition(int position) {
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());
2008-12-02 12:01:29 +01:00
}
private:
BinEditor *m_editor;
QString m_displayName;
BinEditorDocument *m_file;
2008-12-02 12:01:29 +01:00
QToolBar *m_toolBar;
QLineEdit *m_addressEdit;
2008-12-02 12:01:29 +01:00
};
///////////////////////////////// BinEditorFactory //////////////////////////////////
BinEditorFactory::BinEditorFactory(BinEditorPlugin *owner) :
2011-02-25 13:21:54 +01:00
m_mimeTypes(QLatin1String(Constants::C_BINEDITOR_MIMETYPE)),
2008-12-02 12:01:29 +01:00
m_owner(owner)
{
}
Core::Id BinEditorFactory::id() const
2008-12-02 12:01:29 +01:00
{
return Core::Constants::K_DEFAULT_BINARY_EDITOR_ID;
}
QString BinEditorFactory::displayName() const
{
return qApp->translate("OpenWith::Editors", Constants::C_BINEDITOR_DISPLAY_NAME);
2008-12-02 12:01:29 +01:00
}
Core::IEditor *BinEditorFactory::createEditor(QWidget *parent)
{
BinEditor *editor = new BinEditor(parent);
m_owner->initializeEditor(editor);
2011-02-24 09:17:21 +01:00
return editor->editor();
2008-12-02 12:01:29 +01:00
}
QStringList BinEditorFactory::mimeTypes() const
{
return m_mimeTypes;
}
/*!
\class BINEditor::BinEditorWidgetFactory
\brief Service registered with PluginManager to create bin editor widgets for plugins
without direct linkage.
\sa ExtensionSystem::PluginManager::getObjectByClassName, ExtensionSystem::invoke
*/
BinEditorWidgetFactory::BinEditorWidgetFactory(QObject *parent) :
QObject(parent)
{
}
QWidget *BinEditorWidgetFactory::createWidget(QWidget *parent)
{
return new BinEditor(parent);
}
2008-12-02 12:01:29 +01:00
///////////////////////////////// BinEditorPlugin //////////////////////////////////
BinEditorPlugin::BinEditorPlugin()
2008-12-02 12:01:29 +01:00
{
m_undoAction = m_redoAction = m_copyAction = m_selectAllAction = 0;
}
BinEditorPlugin::~BinEditorPlugin()
{
}
QAction *BinEditorPlugin::registerNewAction(Core::Id id, const QString &title)
2008-12-02 12:01:29 +01:00
{
QAction *result = new QAction(title, this);
Core::ActionManager::registerAction(result, id, m_context);
2008-12-02 12:01:29 +01:00
return result;
}
QAction *BinEditorPlugin::registerNewAction(Core::Id id,
2008-12-02 12:01:29 +01:00
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()));
2011-02-24 09:17:21 +01:00
editor->setEditor(editorInterface);
2008-12-02 12:01:29 +01:00
m_context.add(Constants::C_BINEDITOR);
2008-12-02 12:01:29 +01:00
if (!m_undoAction) {
m_undoAction = registerNewAction(Core::Constants::UNDO, this, SLOT(undoAction()), tr("&Undo"));
m_redoAction = registerNewAction(Core::Constants::REDO, this, SLOT(redoAction()), tr("&Redo"));
m_copyAction = registerNewAction(Core::Constants::COPY, this, SLOT(copyAction()));
m_selectAllAction = registerNewAction(Core::Constants::SELECTALL, this, SLOT(selectAllAction()));
2008-12-02 12:01:29 +01:00
}
// 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);
}
bool BinEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
2008-12-02 12:01:29 +01:00
{
Q_UNUSED(arguments)
Q_UNUSED(errorMessage)
connect(Core::ICore::instance(), SIGNAL(contextAboutToChange(Core::IContext*)),
this, SLOT(updateCurrentEditor(Core::IContext*)));
2008-12-02 12:01:29 +01:00
addAutoReleasedObject(new BinEditorFactory(this));
addAutoReleasedObject(new BinEditorWidgetFactory);
2008-12-02 12:01:29 +01:00
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"