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

574 lines
18 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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 The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company 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 <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/find/ifindsupport.h>
#include <coreplugin/idocument.h>
#include <extensionsystem/pluginmanager.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 Utils;
using namespace Core;
2008-12-02 12:01:29 +01:00
namespace BinEditor {
///////////////////////////////// BinEditorWidgetFactory //////////////////////////////////
/*!
\class BinEditor::BinEditorWidgetFactory
\brief The BinEditorWidgetFactory class offers a service registered with
PluginManager to create bin editor widgets for plugins
without direct linkage.
\sa ExtensionSystem::PluginManager::getObjectByClassName, ExtensionSystem::invoke
*/
class BinEditorWidgetFactory : public QObject
{
Q_OBJECT
public:
BinEditorWidgetFactory() {}
Q_INVOKABLE QWidget *createWidget(QWidget *parent)
{
return new BinEditorWidget(parent);
}
};
namespace Internal {
2008-12-02 12:01:29 +01:00
class BinEditorFind : public IFindSupport
2008-12-02 12:01:29 +01:00
{
Q_OBJECT
2008-12-02 12:01:29 +01:00
public:
BinEditorFind(BinEditorWidget *widget)
{
m_widget = widget;
m_incrementalStartPos = m_contPos = -1;
m_incrementalWrappedState = false;
}
2008-12-02 12:01:29 +01:00
bool supportsReplace() const { return false; }
QString currentFindString() const { return QString(); }
QString completedFindString() const { return QString(); }
FindFlags supportedFindFlags() const
{
return FindBackward | FindCaseSensitively;
}
void resetIncrementalSearch()
{
m_incrementalStartPos = m_contPos = -1;
m_incrementalWrappedState = false;
}
virtual void highlightAll(const QString &txt, FindFlags findFlags)
{
m_widget->highlightSearchResults(txt.toLatin1(), textDocumentFlagsForFindFlags(findFlags));
}
void clearHighlights()
{
m_widget->highlightSearchResults(QByteArray());
}
2008-12-02 12:01:29 +01:00
int find(const QByteArray &pattern, int pos, FindFlags findFlags, bool *wrapped)
{
if (wrapped)
*wrapped = false;
2008-12-02 12:01:29 +01:00
if (pattern.isEmpty()) {
m_widget->setCursorPosition(pos);
2008-12-02 12:01:29 +01:00
return pos;
}
int res = m_widget->find(pattern, pos, textDocumentFlagsForFindFlags(findFlags));
if (res < 0) {
pos = (findFlags & FindBackward) ? -1 : 0;
res = m_widget->find(pattern, pos, textDocumentFlagsForFindFlags(findFlags));
if (res < 0)
return res;
if (wrapped)
*wrapped = true;
}
return res;
2008-12-02 12:01:29 +01:00
}
Result findIncremental(const QString &txt, 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_widget->selectionStart();
if (m_contPos == -1)
m_contPos = m_incrementalStartPos;
bool wrapped;
int found = find(pattern, m_contPos, findFlags, &wrapped);
if (wrapped != m_incrementalWrappedState && (found >= 0)) {
m_incrementalWrappedState = wrapped;
showWrapIndicator(m_widget);
}
Result result;
if (found >= 0) {
result = Found;
m_widget->highlightSearchResults(pattern, textDocumentFlagsForFindFlags(findFlags));
m_contPos = -1;
} else {
if (found == -2) {
result = NotYetFound;
m_contPos +=
findFlags & FindBackward
? -BinEditorWidget::SearchStride : BinEditorWidget::SearchStride;
} else {
result = NotFound;
m_contPos = -1;
m_widget->highlightSearchResults(QByteArray(), 0);
}
}
return result;
2008-12-02 12:01:29 +01:00
}
Result findStep(const QString &txt, 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_widget->cursorPosition();
if (findFlags & FindBackward)
m_contPos = m_widget->selectionStart()-1;
}
bool wrapped;
int found = find(pattern, m_contPos, findFlags, &wrapped);
if (wrapped)
showWrapIndicator(m_widget);
Result result;
if (found >= 0) {
result = Found;
2008-12-02 12:01:29 +01:00
m_incrementalStartPos = found;
m_contPos = -1;
if (wasReset)
m_widget->highlightSearchResults(pattern, textDocumentFlagsForFindFlags(findFlags));
} else if (found == -2) {
result = NotYetFound;
m_contPos += findFlags & FindBackward
? -BinEditorWidget::SearchStride : BinEditorWidget::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:
BinEditorWidget *m_widget;
2008-12-02 12:01:29 +01:00
int m_incrementalStartPos;
int m_contPos; // Only valid if last result was NotYetFound.
bool m_incrementalWrappedState;
QByteArray m_lastPattern;
2008-12-02 12:01:29 +01:00
};
class BinEditorDocument : public IDocument
2008-12-02 12:01:29 +01:00
{
Q_OBJECT
public:
BinEditorDocument(BinEditorWidget *parent) :
IDocument(parent)
2008-12-02 12:01:29 +01:00
{
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
setMimeType(QLatin1String(BinEditor::Constants::C_BINEDITOR_MIMETYPE));
m_widget = parent;
connect(m_widget, SIGNAL(dataRequested(quint64)),
this, SLOT(provideData(quint64)));
connect(m_widget, SIGNAL(newRangeRequested(quint64)),
this, SLOT(provideNewRange(quint64)));
2008-12-02 12:01:29 +01:00
}
bool setContents(const QByteArray &contents)
{
if (!contents.isEmpty())
return false;
m_widget->clear();
return true;
}
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const
{
Q_UNUSED(state)
return type == TypeRemoved ? BehaviorSilent : BehaviorAsk;
}
bool save(QString *errorString, const QString &fn, bool autoSave)
{
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
const FileName fileNameToUse = fn.isEmpty() ? filePath() : FileName::fromString(fn);
if (m_widget->save(errorString, filePath().toString(), fileNameToUse.toString())) {
setFilePath(fileNameToUse);
2008-12-02 12:01:29 +01:00
return true;
} else {
return false;
2008-12-02 12:01:29 +01:00
}
}
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(ICore::mainWindow(), tr("File Error"), msg);
return false;
}
if (offset >= size)
return false;
if (file.open(QIODevice::ReadOnly)) {
file.close();
setFilePath(FileName::fromString(fileName));
m_widget->setSizes(offset, file.size());
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(ICore::mainWindow(), tr("File Error"), errStr);
2008-12-02 12:01:29 +01:00
return false;
}
private slots:
void provideData(quint64 block)
{
const FileName fn = filePath();
if (fn.isEmpty())
return;
QFile file(fn.toString());
if (file.open(QIODevice::ReadOnly)) {
int blockSize = m_widget->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);
m_widget->addData(block, data);
} else {
QMessageBox::critical(ICore::mainWindow(), tr("File Error"),
tr("Cannot open %1: %2").arg(
fn.toUserOutput(), file.errorString()));
}
}
void provideNewRange(quint64 offset)
{
open(0, filePath().toString(), offset);
}
public:
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 isTemporary()/*e.g. memory view*/ ? false
: m_widget->isModified(); }
2011-02-25 13:21:54 +01:00
bool isFileReadOnly() const {
const FileName fn = filePath();
if (fn.isEmpty())
2011-02-25 13:21:54 +01:00
return false;
return !fn.toFileInfo().isWritable();
2008-12-02 12:01:29 +01:00
}
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();
int cPos = m_widget->cursorPosition();
m_widget->clear();
const bool success = open(errorString, filePath().toString());
m_widget->setCursorPosition(cPos);
emit reloadFinished(success);
return success;
2008-12-02 12:01:29 +01:00
}
return true;
2008-12-02 12:01:29 +01:00
}
private:
BinEditorWidget *m_widget;
2008-12-02 12:01:29 +01:00
};
class BinEditor : public IEditor
2008-12-02 12:01:29 +01:00
{
Q_OBJECT
public:
BinEditor(BinEditorWidget *widget)
{
setWidget(widget);
m_file = new BinEditorDocument(widget);
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);
widget->setEditor(this);
connect(widget, SIGNAL(cursorPositionChanged(int)), SLOT(updateCursorPosition(int)));
connect(m_addressEdit, SIGNAL(editingFinished()), SLOT(jumpToAddress()));
connect(widget, SIGNAL(modificationChanged(bool)), m_file, SIGNAL(changed()));
updateCursorPosition(widget->cursorPosition());
2008-12-02 12:01:29 +01:00
}
~BinEditor()
{
delete m_widget;
}
2008-12-02 12:01:29 +01:00
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
}
IDocument *document() { return m_file; }
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
private slots:
2008-12-02 12:01:29 +01:00
void updateCursorPosition(int position) {
m_addressEdit->setText(QString::number(editorWidget()->baseAddress() + position, 16));
}
void jumpToAddress() {
editorWidget()->jumpToAddress(m_addressEdit->text().toULongLong(0, 16));
updateCursorPosition(editorWidget()->cursorPosition());
}
private:
inline BinEditorWidget *editorWidget() const
{
QTC_ASSERT(qobject_cast<BinEditorWidget *>(m_widget.data()), return 0);
return static_cast<BinEditorWidget *>(m_widget.data());
2008-12-02 12:01:29 +01:00
}
private:
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) :
m_owner(owner)
{
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
setDisplayName(qApp->translate("OpenWith::Editors", Constants::C_BINEDITOR_DISPLAY_NAME));
addMimeType(Constants::C_BINEDITOR_MIMETYPE);
2008-12-02 12:01:29 +01:00
}
IEditor *BinEditorFactory::createEditor()
2008-12-02 12:01:29 +01:00
{
BinEditorWidget *widget = new BinEditorWidget();
BinEditor *editor = new BinEditor(widget);
m_owner->initializeEditor(widget);
return editor;
2008-12-02 12:01:29 +01:00
}
///////////////////////////////// BinEditorPlugin //////////////////////////////////
BinEditorPlugin::BinEditorPlugin() : m_factory(0)
2008-12-02 12:01:29 +01:00
{
m_undoAction = m_redoAction = m_copyAction = m_selectAllAction = 0;
}
BinEditorPlugin::~BinEditorPlugin()
{
}
QAction *BinEditorPlugin::registerNewAction(Id id, const QString &title)
2008-12-02 12:01:29 +01:00
{
QAction *result = new QAction(title, this);
ActionManager::registerAction(result, id, m_context);
2008-12-02 12:01:29 +01:00
return result;
}
QAction *BinEditorPlugin::registerNewAction(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(BinEditorWidget *widget)
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
}
QObject::connect(widget, SIGNAL(undoAvailable(bool)), this, SLOT(updateActions()));
QObject::connect(widget, SIGNAL(redoAvailable(bool)), this, SLOT(updateActions()));
2008-12-02 12:01:29 +01:00
Aggregation::Aggregate *aggregate = new Aggregation::Aggregate;
BinEditorFind *binEditorFind = new BinEditorFind(widget);
2008-12-02 12:01:29 +01:00
aggregate->add(binEditorFind);
aggregate->add(widget);
2008-12-02 12:01:29 +01:00
}
bool BinEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
2008-12-02 12:01:29 +01:00
{
Q_UNUSED(arguments)
Q_UNUSED(errorMessage)
connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
this, SLOT(updateCurrentEditor(Core::IEditor*)));
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(IEditor *editor)
2008-12-02 12:01:29 +01:00
{
BinEditorWidget *binEditor = 0;
if (editor)
binEditor = qobject_cast<BinEditorWidget *>(editor->widget());
if (m_currentEditor == binEditor)
return;
m_currentEditor = binEditor;
2008-12-02 12:01:29 +01:00
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());
}
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();
}
} // namespace Internal
} // namespace BinEditor
2008-12-02 12:01:29 +01:00
#include "bineditorplugin.moc"