2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02: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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
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"
|
|
|
|
|
|
2011-04-04 15:24:13 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2012-05-06 12:30:23 +04:00
|
|
|
#include <QCoreApplication>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QRegExp>
|
|
|
|
|
#include <QVariant>
|
2011-02-28 14:33:04 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QRegExpValidator>
|
|
|
|
|
#include <QToolBar>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
2011-09-05 16:10:37 +02:00
|
|
|
#include <coreplugin/id.h>
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2009-10-01 16:38:08 +02:00
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2012-02-14 16:43:51 +01:00
|
|
|
#include <coreplugin/idocument.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/mimedatabase.h>
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <find/ifindsupport.h>
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <texteditor/fontsettings.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <utils/reloadpromptutils.h>
|
2011-02-28 14:33:04 +01:00
|
|
|
#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
|
2013-05-25 02:48:52 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
public:
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorFind(BinEditorWidget *widget)
|
2009-09-09 17:11:00 +02:00
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget = widget;
|
2009-09-09 17:11:00 +02:00
|
|
|
m_incrementalStartPos = m_contPos = -1;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
~BinEditorFind() {}
|
|
|
|
|
|
|
|
|
|
bool supportsReplace() const { return false; }
|
2013-05-25 02:48:52 +02:00
|
|
|
|
2010-07-15 13:30:04 +02:00
|
|
|
Find::FindFlags supportedFindFlags() const
|
2009-07-13 14:48:45 +02:00
|
|
|
{
|
2010-07-15 13:30:04 +02:00
|
|
|
return Find::FindBackward | Find::FindCaseSensitively;
|
2009-07-13 14:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-09 17:11:00 +02:00
|
|
|
void resetIncrementalSearch()
|
|
|
|
|
{
|
|
|
|
|
m_incrementalStartPos = m_contPos = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-09 11:08:01 +02:00
|
|
|
virtual void highlightAll(const QString &txt, Find::FindFlags findFlags)
|
|
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->highlightSearchResults(txt.toLatin1(), Find::textDocumentFlagsForFindFlags(findFlags));
|
2011-08-09 11:08:01 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
void clearResults() { m_widget->highlightSearchResults(QByteArray()); }
|
2008-12-02 12:01:29 +01:00
|
|
|
QString currentFindString() const { return QString(); }
|
|
|
|
|
QString completedFindString() const { return QString(); }
|
|
|
|
|
|
|
|
|
|
|
2010-07-15 13:30:04 +02:00
|
|
|
int find(const QByteArray &pattern, int pos, Find::FindFlags findFlags) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (pattern.isEmpty()) {
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->setCursorPosition(pos);
|
2008-12-02 12:01:29 +01:00
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
return m_widget->find(pattern, pos, Find::textDocumentFlagsForFindFlags(findFlags));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-15 13:30:04 +02:00
|
|
|
Result findIncremental(const QString &txt, Find::FindFlags findFlags) {
|
2008-12-02 12:01:29 +01:00
|
|
|
QByteArray pattern = txt.toLatin1();
|
2009-09-09 17:11:00 +02:00
|
|
|
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)
|
2013-05-25 02:48:52 +02:00
|
|
|
m_incrementalStartPos = m_widget->selectionStart();
|
2009-09-09 17:11:00 +02:00
|
|
|
if (m_contPos == -1)
|
|
|
|
|
m_contPos = m_incrementalStartPos;
|
|
|
|
|
int found = find(pattern, m_contPos, findFlags);
|
|
|
|
|
Result result;
|
|
|
|
|
if (found >= 0) {
|
|
|
|
|
result = Found;
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->highlightSearchResults(pattern, Find::textDocumentFlagsForFindFlags(findFlags));
|
2009-09-09 17:11:00 +02:00
|
|
|
m_contPos = -1;
|
|
|
|
|
} else {
|
|
|
|
|
if (found == -2) {
|
|
|
|
|
result = NotYetFound;
|
|
|
|
|
m_contPos +=
|
2010-07-15 13:30:04 +02:00
|
|
|
findFlags & Find::FindBackward
|
2013-05-25 02:48:52 +02:00
|
|
|
? -BinEditorWidget::SearchStride : BinEditorWidget::SearchStride;
|
2009-09-09 17:11:00 +02:00
|
|
|
} else {
|
|
|
|
|
result = NotFound;
|
|
|
|
|
m_contPos = -1;
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->highlightSearchResults(QByteArray(), 0);
|
2009-09-09 17:11:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-15 13:30:04 +02: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);
|
2009-09-09 17:11:00 +02:00
|
|
|
if (m_contPos == -1) {
|
2013-05-25 02:48:52 +02:00
|
|
|
m_contPos = m_widget->cursorPosition();
|
2010-07-15 13:30:04 +02:00
|
|
|
if (findFlags & Find::FindBackward)
|
2013-05-25 02:48:52 +02:00
|
|
|
m_contPos = m_widget->selectionStart()-1;
|
2009-09-09 17:11:00 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2009-09-09 17:11:00 +02:00
|
|
|
m_contPos = -1;
|
|
|
|
|
if (wasReset)
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->highlightSearchResults(pattern, Find::textDocumentFlagsForFindFlags(findFlags));
|
2009-09-09 17:11:00 +02:00
|
|
|
} else if (found == -2) {
|
|
|
|
|
result = NotYetFound;
|
2010-07-15 13:30:04 +02:00
|
|
|
m_contPos += findFlags & Find::FindBackward
|
2013-05-25 02:48:52 +02:00
|
|
|
? -BinEditorWidget::SearchStride : BinEditorWidget::SearchStride;
|
2009-09-09 17:11:00 +02:00
|
|
|
} else {
|
|
|
|
|
result = NotFound;
|
|
|
|
|
m_contPos = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-09-09 17:11:00 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
private:
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorWidget *m_widget;
|
2008-12-02 12:01:29 +01:00
|
|
|
int m_incrementalStartPos;
|
2009-09-09 17:11:00 +02:00
|
|
|
int m_contPos; // Only valid if last result was NotYetFound.
|
|
|
|
|
QByteArray m_lastPattern;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
class BinEditorDocument : public Core::IDocument
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorDocument(BinEditorWidget *parent) :
|
2012-02-14 16:43:51 +01:00
|
|
|
Core::IDocument(parent)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget = parent;
|
|
|
|
|
connect(m_widget, SIGNAL(dataRequested(Core::IEditor*,quint64)),
|
2012-03-05 22:30:59 +01:00
|
|
|
this, SLOT(provideData(Core::IEditor*,quint64)));
|
2013-05-25 02:48:52 +02:00
|
|
|
connect(m_widget, SIGNAL(newRangeRequested(Core::IEditor*,quint64)),
|
2010-07-02 12:17:08 +02:00
|
|
|
this, SLOT(provideNewRange(Core::IEditor*,quint64)));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2012-02-14 16:43:51 +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
|
|
|
|
2011-05-10 20:43:03 +02:00
|
|
|
bool save(QString *errorString, const QString &fileName, bool autoSave)
|
2011-03-30 13:45:16 +02:00
|
|
|
{
|
2011-05-10 20:43:03 +02:00
|
|
|
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
|
2010-08-19 10:16:16 +02:00
|
|
|
const QString fileNameToUse
|
|
|
|
|
= fileName.isEmpty() ? m_fileName : fileName;
|
2013-05-25 02:48:52 +02:00
|
|
|
if (m_widget->save(errorString, m_fileName, fileNameToUse)) {
|
2010-08-19 10:16:16 +02:00
|
|
|
m_fileName = fileNameToUse;
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->editor()->setDisplayName(QFileInfo(fileNameToUse).fileName());
|
2008-12-02 12:01:29 +01:00
|
|
|
emit changed();
|
|
|
|
|
return true;
|
2009-09-09 17:11:00 +02:00
|
|
|
} else {
|
|
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-11 14:13:38 +02:00
|
|
|
void rename(const QString &newName) {
|
|
|
|
|
m_fileName = newName;
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->editor()->setDisplayName(QFileInfo(fileName()).fileName());
|
2010-05-11 14:13:38 +02:00
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-04 15:24:13 +02:00
|
|
|
bool open(QString *errorString, const QString &fileName, quint64 offset = 0) {
|
2008-12-02 12:01:29 +01:00
|
|
|
QFile file(fileName);
|
2012-09-27 14:25:37 +02:00
|
|
|
quint64 size = static_cast<quint64>(file.size());
|
2012-12-20 22:50:45 +02:00
|
|
|
if (size == 0 && !fileName.isEmpty()) {
|
2012-10-01 16:49:47 +02:00
|
|
|
QString msg = tr("The Binary Editor cannot open empty files.");
|
2012-09-27 14:04:15 +02:00
|
|
|
if (errorString)
|
|
|
|
|
*errorString = msg;
|
|
|
|
|
else
|
|
|
|
|
QMessageBox::critical(Core::ICore::mainWindow(), tr("File Error"), msg);
|
2011-04-04 15:24:13 +02:00
|
|
|
return false;
|
2012-09-18 08:45:43 +02:00
|
|
|
}
|
2012-09-27 14:25:37 +02:00
|
|
|
if (offset >= size)
|
|
|
|
|
return false;
|
2011-04-04 15:24:13 +02:00
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
2012-03-13 12:47:55 +01:00
|
|
|
file.close();
|
2008-12-02 12:01:29 +01:00
|
|
|
m_fileName = fileName;
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->setSizes(offset, file.size());
|
|
|
|
|
m_widget->editor()->setDisplayName(QFileInfo(fileName).fileName());
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2011-04-04 15:24:13 +02:00
|
|
|
QString errStr = tr("Cannot open %1: %2").arg(
|
|
|
|
|
QDir::toNativeSeparators(fileName), file.errorString());
|
|
|
|
|
if (errorString)
|
|
|
|
|
*errorString = errStr;
|
|
|
|
|
else
|
2012-01-24 15:36:40 +01:00
|
|
|
QMessageBox::critical(Core::ICore::mainWindow(), tr("File Error"), errStr);
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-21 16:46:24 +02:00
|
|
|
private slots:
|
2010-02-17 17:33:42 +01:00
|
|
|
void provideData(Core::IEditor *, quint64 block) {
|
2011-02-28 14:33:04 +01:00
|
|
|
if (m_fileName.isEmpty())
|
|
|
|
|
return;
|
2009-07-21 16:46:24 +02:00
|
|
|
QFile file(m_fileName);
|
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
2013-05-25 02:48:52 +02:00
|
|
|
int blockSize = m_widget->dataBlockSize();
|
2009-07-21 16:46:24 +02:00
|
|
|
file.seek(block * blockSize);
|
|
|
|
|
QByteArray data = file.read(blockSize);
|
2012-03-13 15:51:41 +01:00
|
|
|
file.close();
|
2010-07-02 09:58:54 +02:00
|
|
|
const int dataSize = data.size();
|
|
|
|
|
if (dataSize != blockSize)
|
|
|
|
|
data += QByteArray(blockSize - dataSize, 0);
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->addData(block, data);
|
2011-04-04 15:24:13 +02:00
|
|
|
} else {
|
2012-01-24 15:36:40 +01:00
|
|
|
QMessageBox::critical(Core::ICore::mainWindow(), tr("File Error"),
|
2011-04-04 15:24:13 +02:00
|
|
|
tr("Cannot open %1: %2").arg(
|
|
|
|
|
QDir::toNativeSeparators(m_fileName), file.errorString()));
|
2009-07-21 16:46:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-07-02 09:58:54 +02:00
|
|
|
|
2010-07-02 12:17:08 +02:00
|
|
|
void provideNewRange(Core::IEditor *, quint64 offset) {
|
2011-04-04 15:24:13 +02:00
|
|
|
open(0, m_fileName, offset);
|
2010-07-02 09:58:54 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-21 16:46:24 +02:00
|
|
|
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(); }
|
|
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
bool isModified() const { return m_widget->isMemoryView() ? false : m_widget->isModified(); }
|
2011-02-25 13:21:54 +01:00
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
bool isFileReadOnly() const {
|
2013-05-25 02:48:52 +02:00
|
|
|
if (m_widget->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; }
|
|
|
|
|
|
2011-04-04 15:24:13 +02:00
|
|
|
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) {
|
2010-03-19 10:28:05 +01:00
|
|
|
if (flag == FlagIgnore)
|
2011-04-04 15:24:13 +02:00
|
|
|
return true;
|
2010-03-19 10:28:05 +01:00
|
|
|
if (type == TypePermissions) {
|
2008-12-02 12:01:29 +01:00
|
|
|
emit changed();
|
2010-03-19 10:28:05 +01:00
|
|
|
} else {
|
2010-05-28 14:06:57 +02:00
|
|
|
emit aboutToReload();
|
2011-04-04 15:24:13 +02:00
|
|
|
if (!open(errorString, m_fileName))
|
|
|
|
|
return false;
|
|
|
|
|
emit reloaded();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2011-04-04 15:24:13 +02:00
|
|
|
return true;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorWidget *m_widget;
|
2008-12-02 12:01:29 +01:00
|
|
|
QString m_fileName;
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
class BinEditor : public Core::IEditor
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditor(BinEditorWidget *widget)
|
2009-01-20 11:52:04 +01:00
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
setWidget(widget);
|
|
|
|
|
m_widget = widget;
|
|
|
|
|
m_file = new BinEditorDocument(m_widget);
|
2010-06-25 17:37:59 +02:00
|
|
|
m_context.add(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
|
|
|
|
|
m_context.add(Constants::C_BINEDITOR);
|
2010-07-02 14:38:49 +02:00
|
|
|
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);
|
2010-07-02 14:38:49 +02:00
|
|
|
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);
|
|
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
connect(m_widget, SIGNAL(cursorPositionChanged(int)), this,
|
2010-07-02 14:38:49 +02:00
|
|
|
SLOT(updateCursorPosition(int)));
|
2010-05-11 14:13:38 +02:00
|
|
|
connect(m_file, SIGNAL(changed()), this, SIGNAL(changed()));
|
2010-07-02 14:38:49 +02:00
|
|
|
connect(m_addressEdit, SIGNAL(editingFinished()), this,
|
|
|
|
|
SLOT(jumpToAddress()));
|
2013-05-25 02:48:52 +02:00
|
|
|
updateCursorPosition(m_widget->cursorPosition());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2013-05-25 02:48:52 +02:00
|
|
|
~BinEditor() {
|
|
|
|
|
delete m_widget;
|
2010-01-14 17:49:29 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
bool createNew(const QString & /* contents */ = QString()) {
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->clear();
|
2008-12-02 12:01:29 +01:00
|
|
|
m_file->setFilename(QString());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-05-10 20:43:03 +02:00
|
|
|
bool open(QString *errorString, const QString &fileName, const QString &realFileName) {
|
|
|
|
|
QTC_ASSERT(fileName == realFileName, return false); // The bineditor can do no autosaving
|
2011-04-04 15:24:13 +02:00
|
|
|
return m_file->open(errorString, fileName);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2012-02-14 16:43:51 +01:00
|
|
|
Core::IDocument *document() { return m_file; }
|
2012-11-20 07:18:01 +02:00
|
|
|
Core::Id id() const { return Core::Id(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(); }
|
|
|
|
|
|
2009-07-15 16:23:07 +02:00
|
|
|
QWidget *toolBar() { return m_toolBar; }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
bool isTemporary() const { return m_widget->isMemoryView(); }
|
2009-05-18 19:11:11 +02:00
|
|
|
|
2010-07-02 14:38:49 +02:00
|
|
|
private slots:
|
2008-12-02 12:01:29 +01:00
|
|
|
void updateCursorPosition(int position) {
|
2013-05-25 02:48:52 +02:00
|
|
|
m_addressEdit->setText(QString::number(m_widget->baseAddress() + position, 16));
|
2010-07-02 14:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void jumpToAddress() {
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->jumpToAddress(m_addressEdit->text().toULongLong(0, 16));
|
|
|
|
|
updateCursorPosition(m_widget->cursorPosition());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorWidget *m_widget;
|
2008-12-02 12:01:29 +01:00
|
|
|
QString m_displayName;
|
2012-02-14 16:43:51 +01:00
|
|
|
BinEditorDocument *m_file;
|
2008-12-02 12:01:29 +01:00
|
|
|
QToolBar *m_toolBar;
|
2010-07-02 14:38:49 +02:00
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-07 09:26:29 +02:00
|
|
|
Core::Id BinEditorFactory::id() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2012-11-20 07:18:01 +02:00
|
|
|
return Core::Id(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
|
2010-01-07 18:17:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BinEditorFactory::displayName() const
|
|
|
|
|
{
|
2012-05-06 12:30:23 +04:00
|
|
|
return qApp->translate("OpenWith::Editors", Constants::C_BINEDITOR_DISPLAY_NAME);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IEditor *BinEditorFactory::createEditor(QWidget *parent)
|
|
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorWidget *widget = new BinEditorWidget(parent);
|
|
|
|
|
m_owner->initializeEditor(widget);
|
|
|
|
|
return widget->editor();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList BinEditorFactory::mimeTypes() const
|
|
|
|
|
{
|
|
|
|
|
return m_mimeTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-19 12:15:18 +02:00
|
|
|
/*!
|
|
|
|
|
\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)
|
|
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
return new BinEditorWidget(parent);
|
2011-04-19 12:15:18 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
///////////////////////////////// BinEditorPlugin //////////////////////////////////
|
|
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
BinEditorPlugin::BinEditorPlugin()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
m_undoAction = m_redoAction = m_copyAction = m_selectAllAction = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinEditorPlugin::~BinEditorPlugin()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-28 16:07:07 +02:00
|
|
|
QAction *BinEditorPlugin::registerNewAction(Core::Id id, const QString &title)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
QAction *result = new QAction(title, this);
|
2012-06-28 16:07:07 +02:00
|
|
|
Core::ActionManager::registerAction(result, id, m_context);
|
2008-12-02 12:01:29 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-28 16:07:07 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
void BinEditorPlugin::initializeEditor(BinEditorWidget *widget)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditor *editor = new BinEditor(widget);
|
|
|
|
|
QObject::connect(widget, SIGNAL(modificationChanged(bool)), editor, SIGNAL(changed()));
|
|
|
|
|
widget->setEditor(editor);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-25 17:37:59 +02:00
|
|
|
m_context.add(Constants::C_BINEDITOR);
|
2008-12-02 12:01:29 +01:00
|
|
|
if (!m_undoAction) {
|
2012-06-28 16:07:07 +02:00
|
|
|
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();
|
2013-05-25 02:48:52 +02:00
|
|
|
widget->setFontSettings(settings->fontSettings());
|
2008-12-02 12:01:29 +01:00
|
|
|
connect(settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
|
2013-05-25 02:48:52 +02:00
|
|
|
widget, SLOT(setFontSettings(TextEditor::FontSettings)));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-05-25 02:48:52 +02: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;
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorFind *binEditorFind = new BinEditorFind(widget);
|
2008-12-02 12:01:29 +01:00
|
|
|
aggregate->add(binEditorFind);
|
2013-05-25 02:48:52 +02:00
|
|
|
aggregate->add(widget);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
bool BinEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-07-13 17:35:17 +02:00
|
|
|
Q_UNUSED(arguments)
|
2010-06-18 11:02:48 +02:00
|
|
|
Q_UNUSED(errorMessage)
|
2009-01-20 11:52:04 +01:00
|
|
|
|
2013-05-07 19:03:22 +02:00
|
|
|
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));
|
2011-04-19 12:15:18 +02:00
|
|
|
addAutoReleasedObject(new BinEditorWidgetFactory);
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinEditorPlugin::extensionsInitialized()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 19:03:22 +02:00
|
|
|
void BinEditorPlugin::updateCurrentEditor(Core::IEditor *editor)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorWidget *binEditor = 0;
|
2013-05-07 19:03:22 +02:00
|
|
|
if (editor)
|
2013-05-25 02:48:52 +02:00
|
|
|
binEditor = qobject_cast<BinEditorWidget *>(editor->widget());
|
2013-05-07 19:03:22 +02:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Q_EXPORT_PLUGIN(BinEditorPlugin)
|
|
|
|
|
|
|
|
|
|
#include "bineditorplugin.moc"
|