2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-05-06 15:05:37 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01: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>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QDir>
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <QtCore/QFile>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtCore/QFileInfo>
|
2009-07-21 16:46:24 +02:00
|
|
|
#include <QtCore/QDebug>
|
2010-07-02 14:38:49 +02:00
|
|
|
#include <QtCore/QRegExp>
|
2011-02-28 14:33:04 +01:00
|
|
|
#include <QtCore/QVariant>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
#include <QtGui/QAction>
|
|
|
|
|
#include <QtGui/QMainWindow>
|
2011-04-04 15:24:13 +02:00
|
|
|
#include <QtGui/QMessageBox>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QHBoxLayout>
|
2010-07-02 14:38:49 +02:00
|
|
|
#include <QtGui/QLineEdit>
|
|
|
|
|
#include <QtGui/QRegExpValidator>
|
2009-07-16 17:34:04 +02:00
|
|
|
#include <QtGui/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>
|
2010-11-02 16:53:56 +01:00
|
|
|
#include <coreplugin/uniqueidmanager.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>
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2010-03-19 10:28:05 +01:00
|
|
|
#include <coreplugin/ifile.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
|
|
|
|
|
public:
|
2009-09-09 17:11:00 +02:00
|
|
|
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; }
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void clearResults() { m_editor->highlightSearchResults(QByteArray()); }
|
|
|
|
|
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()) {
|
|
|
|
|
m_editor->setCursorPosition(pos);
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-15 13:30:04 +02:00
|
|
|
return m_editor->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)
|
|
|
|
|
m_incrementalStartPos = m_editor->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;
|
2010-07-15 13:30:04 +02:00
|
|
|
m_editor->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
|
2009-09-09 17:11:00 +02:00
|
|
|
? -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
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
m_contPos = m_editor->cursorPosition();
|
2010-07-15 13:30:04 +02:00
|
|
|
if (findFlags & Find::FindBackward)
|
2009-09-09 17:11:00 +02:00
|
|
|
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;
|
2009-09-09 17:11:00 +02:00
|
|
|
m_contPos = -1;
|
|
|
|
|
if (wasReset)
|
2010-07-15 13:30:04 +02:00
|
|
|
m_editor->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
|
2009-09-09 17:11:00 +02:00
|
|
|
? -BinEditor::SearchStride : BinEditor::SearchStride;
|
|
|
|
|
} else {
|
|
|
|
|
result = NotFound;
|
|
|
|
|
m_contPos = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-09-09 17:11:00 +02:00
|
|
|
|
2010-07-14 14:45:02 +02:00
|
|
|
void replace(const QString &, const QString &,
|
2010-07-15 13:30:04 +02:00
|
|
|
Find::FindFlags) { }
|
2008-12-02 12:01:29 +01:00
|
|
|
bool replaceStep(const QString &, const QString &,
|
2010-07-15 13:30:04 +02:00
|
|
|
Find::FindFlags) { return false;}
|
2008-12-02 12:01:29 +01:00
|
|
|
int replaceAll(const QString &, const QString &,
|
2010-07-15 13:30:04 +02:00
|
|
|
Find::FindFlags) { return 0; }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
BinEditor *m_editor;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BinEditorFile : public Core::IFile
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
BinEditorFile(BinEditor *parent) :
|
2011-02-25 13:21:54 +01:00
|
|
|
Core::IFile(parent)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
m_editor = parent;
|
2011-02-28 15:01:19 +01:00
|
|
|
connect(m_editor, SIGNAL(dataRequested(Core::IEditor*,quint64)),
|
2010-02-17 17:33:42 +01:00
|
|
|
this, SLOT(provideData(Core::IEditor *, quint64)));
|
2010-07-02 12:17:08 +02:00
|
|
|
connect(m_editor, SIGNAL(newRangeRequested(Core::IEditor*,quint64)),
|
|
|
|
|
this, SLOT(provideNewRange(Core::IEditor*,quint64)));
|
2010-07-07 10:30:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
~BinEditorFile() {}
|
|
|
|
|
|
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-03-30 13:45:16 +02:00
|
|
|
bool save(QString *errorString, const QString &fileName = QString())
|
|
|
|
|
{
|
2010-08-19 10:16:16 +02:00
|
|
|
const QString fileNameToUse
|
|
|
|
|
= fileName.isEmpty() ? m_fileName : fileName;
|
2011-03-30 13:45:16 +02:00
|
|
|
if (m_editor->save(errorString, m_fileName, fileNameToUse)) {
|
2010-08-19 10:16:16 +02:00
|
|
|
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;
|
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;
|
2011-02-24 09:17:21 +01:00
|
|
|
m_editor->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);
|
2011-04-04 15:24:13 +02:00
|
|
|
if (offset >= static_cast<quint64>(file.size()))
|
|
|
|
|
return false;
|
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
m_fileName = fileName;
|
2011-03-02 14:49:19 +01:00
|
|
|
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
|
|
|
file.close();
|
|
|
|
|
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
|
|
|
|
|
QMessageBox::critical(Core::ICore::instance()->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)) {
|
2011-02-24 09:17:21 +01:00
|
|
|
int blockSize = m_editor->dataBlockSize();
|
2009-07-21 16:46:24 +02:00
|
|
|
file.seek(block * blockSize);
|
|
|
|
|
QByteArray data = file.read(blockSize);
|
2010-07-02 09:58:54 +02:00
|
|
|
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);
|
2009-07-21 16:46:24 +02:00
|
|
|
file.close();
|
2011-04-04 15:24:13 +02:00
|
|
|
} else {
|
|
|
|
|
QMessageBox::critical(Core::ICore::instance()->mainWindow(), tr("File Error"),
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2010-07-07 10:30:34 +02:00
|
|
|
void handleStartOfFileRequested(Core::IEditor *) {
|
2011-04-04 15:24:13 +02:00
|
|
|
open(0, m_fileName, 0);
|
2010-07-07 10:30:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleEndOfFileRequested(Core::IEditor *) {
|
2011-04-04 15:24:13 +02:00
|
|
|
open(0, m_fileName, QFileInfo(m_fileName).size() - 1);
|
2010-07-07 10:30:34 +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(); }
|
|
|
|
|
|
2011-02-28 14:33:04 +01:00
|
|
|
bool isModified() const { return m_editor->isMemoryView() ? false : m_editor->isModified(); }
|
2011-02-25 13:21:54 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
bool isReadOnly() const {
|
2011-02-28 14:33:04 +01:00
|
|
|
if (m_editor->isMemoryView())
|
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:
|
|
|
|
|
BinEditor *m_editor;
|
|
|
|
|
QString m_fileName;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BinEditorInterface : public Core::IEditor
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2010-01-14 17:49:29 +01:00
|
|
|
BinEditorInterface(BinEditor *editor)
|
2009-01-20 11:52:04 +01:00
|
|
|
{
|
2011-04-13 13:00:30 +02:00
|
|
|
setWidget(editor);
|
2010-01-14 17:49:29 +01:00
|
|
|
m_editor = editor;
|
|
|
|
|
m_file = new BinEditorFile(m_editor);
|
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);
|
|
|
|
|
|
2010-07-02 14:38:49 +02:00
|
|
|
connect(m_editor, SIGNAL(cursorPositionChanged(int)), this,
|
|
|
|
|
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()));
|
|
|
|
|
updateCursorPosition(m_editor->cursorPosition());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-01-14 17:49: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;
|
|
|
|
|
}
|
2011-04-04 15:24:13 +02:00
|
|
|
bool open(QString *errorString, const QString &fileName = QString()) {
|
|
|
|
|
return m_file->open(errorString, fileName);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
Core::IFile *file() { return m_file; }
|
2010-01-07 18:17:24 +01:00
|
|
|
QString id() const { return QLatin1String(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; }
|
2009-01-21 11:46:26 +01:00
|
|
|
IEditor *duplicate(QWidget * /* parent */) { return 0; }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-21 11:46:26 +01:00
|
|
|
QByteArray saveState() const { return QByteArray(); } // TODO
|
|
|
|
|
bool restoreState(const QByteArray & /* state */) { return false; } // TODO
|
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
|
|
|
|
2011-02-28 14:33:04 +01:00
|
|
|
bool isTemporary() const { return m_editor->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) {
|
2010-07-02 14:38:49 +02:00
|
|
|
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;
|
|
|
|
|
BinEditorFile *m_file;
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 18:17:24 +01:00
|
|
|
QString BinEditorFactory::id() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-07 18:17:24 +01:00
|
|
|
return QLatin1String(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BinEditorFactory::displayName() const
|
|
|
|
|
{
|
|
|
|
|
return tr(Constants::C_BINEDITOR_DISPLAY_NAME);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IFile *BinEditorFactory::open(const QString &fileName)
|
|
|
|
|
{
|
2009-01-21 15:52:34 +01:00
|
|
|
Core::EditorManager *em = Core::EditorManager::instance();
|
2010-01-07 18:17:24 +01:00
|
|
|
Core::IEditor *iface = em->openEditor(fileName, id());
|
2008-12-02 12:01:29 +01:00
|
|
|
return iface ? iface->file() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
return new BinEditor(parent);
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QAction *BinEditorPlugin::registerNewAction(const QString &id, const QString &title)
|
|
|
|
|
{
|
|
|
|
|
QAction *result = new QAction(title, this);
|
2009-01-20 11:52:04 +01:00
|
|
|
Core::ICore::instance()->actionManager()->registerAction(result, id, m_context);
|
2008-12-02 12:01:29 +01:00
|
|
|
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()));
|
2011-02-24 09:17:21 +01:00
|
|
|
editor->setEditor(editorInterface);
|
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) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
Core::ICore *core = Core::ICore::instance();
|
|
|
|
|
connect(core, SIGNAL(contextAboutToChange(Core::IContext *)),
|
2008-12-02 12:01:29 +01:00
|
|
|
this, SLOT(updateCurrentEditor(Core::IContext *)));
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"
|