2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 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
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/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
|
2014-10-01 13:21:18 +02:00
|
|
|
** 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.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
|
|
|
|
** 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 <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>
|
2014-01-13 16:17:34 +01:00
|
|
|
#include <coreplugin/find/ifindsupport.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 <utils/reloadpromptutils.h>
|
2011-02-28 14:33:04 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
using namespace Core;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-08-21 20:35:48 +02: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
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
class BinEditorFind : public Core::IFindSupport
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
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;
|
2013-07-16 12:36:14 +03:00
|
|
|
m_incrementalWrappedState = false;
|
2009-09-09 17:11:00 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
bool supportsReplace() const { return false; }
|
2013-05-25 03:47:40 +02:00
|
|
|
QString currentFindString() const { return QString(); }
|
|
|
|
|
QString completedFindString() const { return QString(); }
|
2013-05-25 02:48:52 +02:00
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
Core::FindFlags supportedFindFlags() const
|
2009-07-13 14:48:45 +02:00
|
|
|
{
|
2014-01-13 16:17:34 +01:00
|
|
|
return FindBackward | FindCaseSensitively;
|
2009-07-13 14:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-09 17:11:00 +02:00
|
|
|
void resetIncrementalSearch()
|
|
|
|
|
{
|
|
|
|
|
m_incrementalStartPos = m_contPos = -1;
|
2013-07-16 12:36:14 +03:00
|
|
|
m_incrementalWrappedState = false;
|
2009-09-09 17:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
virtual void highlightAll(const QString &txt, Core::FindFlags findFlags)
|
2011-08-09 11:08:01 +02:00
|
|
|
{
|
2014-01-13 16:17:34 +01:00
|
|
|
m_widget->highlightSearchResults(txt.toLatin1(), textDocumentFlagsForFindFlags(findFlags));
|
2011-08-09 11:08:01 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-02 13:43:31 +02:00
|
|
|
void clearHighlights()
|
2013-05-25 03:47:40 +02:00
|
|
|
{
|
|
|
|
|
m_widget->highlightSearchResults(QByteArray());
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
int find(const QByteArray &pattern, int pos, Core::FindFlags findFlags, bool *wrapped)
|
2013-05-25 03:47:40 +02:00
|
|
|
{
|
2013-07-16 12:36:14 +03:00
|
|
|
if (wrapped)
|
|
|
|
|
*wrapped = false;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
int res = m_widget->find(pattern, pos, textDocumentFlagsForFindFlags(findFlags));
|
2013-07-16 12:36:14 +03:00
|
|
|
if (res < 0) {
|
2014-01-13 16:17:34 +01:00
|
|
|
pos = (findFlags & FindBackward) ? -1 : 0;
|
|
|
|
|
res = m_widget->find(pattern, pos, textDocumentFlagsForFindFlags(findFlags));
|
2013-07-16 12:36:14 +03:00
|
|
|
if (res < 0)
|
|
|
|
|
return res;
|
|
|
|
|
if (wrapped)
|
|
|
|
|
*wrapped = true;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
Result findIncremental(const QString &txt, Core::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;
|
2013-07-16 12:36:14 +03:00
|
|
|
bool wrapped;
|
|
|
|
|
int found = find(pattern, m_contPos, findFlags, &wrapped);
|
|
|
|
|
if (wrapped != m_incrementalWrappedState && (found >= 0)) {
|
|
|
|
|
m_incrementalWrappedState = wrapped;
|
|
|
|
|
showWrapIndicator(m_widget);
|
|
|
|
|
}
|
2009-09-09 17:11:00 +02:00
|
|
|
Result result;
|
|
|
|
|
if (found >= 0) {
|
|
|
|
|
result = Found;
|
2014-01-13 16:17:34 +01:00
|
|
|
m_widget->highlightSearchResults(pattern, textDocumentFlagsForFindFlags(findFlags));
|
2009-09-09 17:11:00 +02:00
|
|
|
m_contPos = -1;
|
|
|
|
|
} else {
|
|
|
|
|
if (found == -2) {
|
|
|
|
|
result = NotYetFound;
|
|
|
|
|
m_contPos +=
|
2014-01-13 16:17:34 +01:00
|
|
|
findFlags & 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
|
|
|
}
|
|
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
Result findStep(const QString &txt, Core::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();
|
2014-01-13 16:17:34 +01:00
|
|
|
if (findFlags & FindBackward)
|
2013-05-25 02:48:52 +02:00
|
|
|
m_contPos = m_widget->selectionStart()-1;
|
2009-09-09 17:11:00 +02:00
|
|
|
}
|
2013-07-16 12:36:14 +03:00
|
|
|
bool wrapped;
|
|
|
|
|
int found = find(pattern, m_contPos, findFlags, &wrapped);
|
|
|
|
|
if (wrapped)
|
|
|
|
|
showWrapIndicator(m_widget);
|
2009-09-09 17:11:00 +02:00
|
|
|
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)
|
2014-01-13 16:17:34 +01:00
|
|
|
m_widget->highlightSearchResults(pattern, textDocumentFlagsForFindFlags(findFlags));
|
2009-09-09 17:11:00 +02:00
|
|
|
} else if (found == -2) {
|
|
|
|
|
result = NotYetFound;
|
2014-01-13 16:17:34 +01:00
|
|
|
m_contPos += findFlags & 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.
|
2013-07-16 12:36:14 +03:00
|
|
|
bool m_incrementalWrappedState;
|
2009-09-09 17:11:00 +02:00
|
|
|
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
|
|
|
{
|
2014-03-05 15:58:12 +01:00
|
|
|
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
|
2014-08-21 20:35:48 +02:00
|
|
|
setMimeType(QLatin1String(BinEditor::Constants::C_BINEDITOR_MIMETYPE));
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget = parent;
|
2013-05-25 03:10:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2013-07-15 15:14:10 +02:00
|
|
|
bool setContents(const QByteArray &contents)
|
|
|
|
|
{
|
|
|
|
|
if (!contents.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
m_widget->clear();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 11:35:56 +02:00
|
|
|
bool save(QString *errorString, const QString &fn, 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
|
2013-07-04 13:30:26 +02:00
|
|
|
= fn.isEmpty() ? filePath() : fn;
|
|
|
|
|
if (m_widget->save(errorString, filePath(), fileNameToUse)) {
|
|
|
|
|
setFilePath(fileNameToUse);
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
2009-09-09 17:11:00 +02:00
|
|
|
} else {
|
|
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
2013-07-04 13:30:26 +02:00
|
|
|
setFilePath(fileName);
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->setSizes(offset, file.size());
|
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:
|
2013-05-25 03:10:12 +02:00
|
|
|
void provideData(quint64 block)
|
|
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
const QString fn = filePath();
|
2013-07-04 11:35:56 +02:00
|
|
|
if (fn.isEmpty())
|
2011-02-28 14:33:04 +01:00
|
|
|
return;
|
2013-07-04 11:35:56 +02:00
|
|
|
QFile file(fn);
|
2009-07-21 16:46:24 +02:00
|
|
|
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(
|
2013-07-04 11:35:56 +02:00
|
|
|
QDir::toNativeSeparators(fn), file.errorString()));
|
2009-07-21 16:46:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-07-02 09:58:54 +02:00
|
|
|
|
2013-05-25 03:10:12 +02:00
|
|
|
void provideNewRange(quint64 offset)
|
|
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
open(0, filePath(), 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
|
|
|
|
|
|
|
|
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-07-12 15:36:29 +02:00
|
|
|
bool isModified() const { return isTemporary()/*e.g. memory view*/ ? false
|
|
|
|
|
: m_widget->isModified(); }
|
2011-02-25 13:21:54 +01:00
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
bool isFileReadOnly() const {
|
2013-07-04 13:30:26 +02:00
|
|
|
const QString fn = filePath();
|
2013-07-12 15:36:29 +02:00
|
|
|
if (fn.isEmpty())
|
2011-02-25 13:21:54 +01:00
|
|
|
return false;
|
2013-07-04 11:35:56 +02:00
|
|
|
const QFileInfo fi(fn);
|
2008-12-02 12:01:29 +01:00
|
|
|
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();
|
2014-01-30 18:57:54 +01:00
|
|
|
int cPos = m_widget->cursorPosition();
|
|
|
|
|
m_widget->clear();
|
2013-07-04 13:30:26 +02:00
|
|
|
const bool success = open(errorString, filePath());
|
2014-01-30 18:57:54 +01:00
|
|
|
m_widget->setCursorPosition(cPos);
|
2013-06-20 12:49:19 +02:00
|
|
|
emit reloadFinished(success);
|
|
|
|
|
return success;
|
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
|
|
|
};
|
|
|
|
|
|
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);
|
2014-08-21 20:35:48 +02:00
|
|
|
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 03:47:40 +02:00
|
|
|
widget->setEditor(this);
|
|
|
|
|
|
|
|
|
|
connect(m_widget, SIGNAL(cursorPositionChanged(int)), SLOT(updateCursorPosition(int)));
|
|
|
|
|
connect(m_addressEdit, SIGNAL(editingFinished()), SLOT(jumpToAddress()));
|
2013-07-04 22:25:15 +02:00
|
|
|
connect(m_widget, SIGNAL(modificationChanged(bool)), m_file, SIGNAL(changed()));
|
2013-05-25 02:48:52 +02:00
|
|
|
updateCursorPosition(m_widget->cursorPosition());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2013-05-25 03:47:40 +02: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
|
|
|
|
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; }
|
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
|
|
|
|
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;
|
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) :
|
|
|
|
|
m_owner(owner)
|
|
|
|
|
{
|
2013-05-31 19:35:37 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2014-01-07 14:41:57 +01:00
|
|
|
Core::IEditor *BinEditorFactory::createEditor()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-01-07 14:41:57 +01:00
|
|
|
BinEditorWidget *widget = new BinEditorWidget();
|
2013-05-25 03:47:40 +02:00
|
|
|
BinEditor *editor = new BinEditor(widget);
|
|
|
|
|
|
2013-05-25 02:48:52 +02:00
|
|
|
m_owner->initializeEditor(widget);
|
2013-05-25 03:47:40 +02:00
|
|
|
return editor;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////// BinEditorPlugin //////////////////////////////////
|
|
|
|
|
|
2014-08-04 14:01:20 +02:00
|
|
|
BinEditorPlugin::BinEditorPlugin() : m_factory(0)
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-21 20:35:48 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace BinEditor
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include "bineditorplugin.moc"
|