2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
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"
|
2016-06-26 22:09:29 +03:00
|
|
|
#include "bineditorwidget.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "bineditorconstants.h"
|
2016-07-14 10:00:15 +02:00
|
|
|
#include "bineditorservice.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
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>
|
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-12-21 21:54:30 +02:00
|
|
|
using namespace Utils;
|
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 {
|
|
|
|
|
namespace Internal {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
class BinEditorFind : public 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
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
bool supportsReplace() const override { return false; }
|
|
|
|
|
QString currentFindString() const override { return QString(); }
|
|
|
|
|
QString completedFindString() const override { return QString(); }
|
2013-05-25 02:48:52 +02:00
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
FindFlags supportedFindFlags() const override
|
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
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
void resetIncrementalSearch() override
|
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
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
void highlightAll(const QString &txt, FindFlags findFlags) override
|
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
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
void clearHighlights() override
|
2013-05-25 03:47:40 +02:00
|
|
|
{
|
|
|
|
|
m_widget->highlightSearchResults(QByteArray());
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
int find(const QByteArray &pattern, int pos, 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
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
Result findIncremental(const QString &txt, FindFlags findFlags) override
|
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
Result findStep(const QString &txt, FindFlags findFlags) override
|
|
|
|
|
{
|
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;
|
2016-06-01 10:54:41 +02:00
|
|
|
qint64 m_incrementalStartPos;
|
|
|
|
|
qint64 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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
class BinEditorDocument : public IDocument
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2013-05-25 02:48:52 +02:00
|
|
|
BinEditorDocument(BinEditorWidget *parent) :
|
2015-02-03 23:56:02 +02:00
|
|
|
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;
|
2016-07-14 10:00:15 +02:00
|
|
|
EditorService *es = m_widget->editorService();
|
|
|
|
|
es->setFetchDataHandler([this](quint64 address) { provideData(address); });
|
|
|
|
|
es->setNewRangeRequestHandler([this](quint64 offset) { provideNewRange(offset); });
|
|
|
|
|
es->setDataChangedHandler([this](quint64, const QByteArray &) { contentsChanged(); });
|
2016-01-15 15:28:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray contents() const override
|
|
|
|
|
{
|
|
|
|
|
return m_widget->contents();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-04 15:54:10 +02:00
|
|
|
bool setContents(const QByteArray &contents) override
|
2013-07-15 15:14:10 +02:00
|
|
|
{
|
|
|
|
|
m_widget->clear();
|
2016-03-08 16:54:12 +02:00
|
|
|
if (!contents.isEmpty()) {
|
|
|
|
|
m_widget->setSizes(0, contents.length(), contents.length());
|
|
|
|
|
m_widget->addData(0, contents);
|
|
|
|
|
}
|
2013-07-15 15:14:10 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 15:54:10 +02:00
|
|
|
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override
|
2014-11-06 09:20:42 +01:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(state)
|
|
|
|
|
return type == TypeRemoved ? BehaviorSilent : BehaviorAsk;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 15:54:10 +02:00
|
|
|
bool save(QString *errorString, const QString &fn, bool autoSave) override
|
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
|
2014-12-21 21:54:30 +02:00
|
|
|
const FileName fileNameToUse = fn.isEmpty() ? filePath() : FileName::fromString(fn);
|
|
|
|
|
if (m_widget->save(errorString, filePath().toString(), fileNameToUse.toString())) {
|
2013-07-04 13:30:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 15:54:10 +02:00
|
|
|
OpenResult open(QString *errorString, const QString &fileName,
|
|
|
|
|
const QString &realFileName) override
|
2015-06-02 17:14:48 +02:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(fileName == realFileName); // The bineditor can do no autosaving
|
|
|
|
|
return openImpl(errorString, fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 15:21:02 +02:00
|
|
|
OpenResult openImpl(QString *errorString, const QString &fileName, quint64 offset = 0)
|
2015-06-02 17:14:48 +02:00
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
QFile file(fileName);
|
2011-04-04 15:24:13 +02:00
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
2012-03-13 12:47:55 +01:00
|
|
|
file.close();
|
2015-03-03 08:17:21 +01:00
|
|
|
quint64 size = static_cast<quint64>(file.size());
|
|
|
|
|
if (size == 0) {
|
|
|
|
|
QString msg = tr("The Binary Editor cannot open empty files.");
|
|
|
|
|
if (errorString)
|
|
|
|
|
*errorString = msg;
|
|
|
|
|
else
|
|
|
|
|
QMessageBox::critical(ICore::mainWindow(), tr("File Error"), msg);
|
2015-06-04 15:21:02 +02:00
|
|
|
return OpenResult::CannotHandle;
|
2015-03-03 08:17:21 +01:00
|
|
|
}
|
2016-06-01 09:48:53 +02:00
|
|
|
if (size / 16 >= qint64(1) << 31) {
|
|
|
|
|
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
|
|
|
|
|
QString msg = tr("The file is too big for the Binary Editor (max. 32GB).");
|
2015-03-10 09:42:31 +01:00
|
|
|
if (errorString)
|
|
|
|
|
*errorString = msg;
|
|
|
|
|
else
|
|
|
|
|
QMessageBox::critical(ICore::mainWindow(), tr("File Error"), msg);
|
2015-06-04 15:21:02 +02:00
|
|
|
return OpenResult::CannotHandle;
|
2015-03-10 09:42:31 +01:00
|
|
|
}
|
2015-03-03 08:17:21 +01:00
|
|
|
if (offset >= size)
|
2015-06-04 15:21:02 +02:00
|
|
|
return OpenResult::CannotHandle;
|
2015-02-03 23:56:02 +02:00
|
|
|
setFilePath(FileName::fromString(fileName));
|
2013-05-25 02:48:52 +02:00
|
|
|
m_widget->setSizes(offset, file.size());
|
2015-06-04 15:21:02 +02:00
|
|
|
return OpenResult::Success;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
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
|
2015-02-03 23:56:02 +02:00
|
|
|
QMessageBox::critical(ICore::mainWindow(), tr("File Error"), errStr);
|
2015-06-04 15:21:02 +02:00
|
|
|
return OpenResult::ReadError;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2016-07-14 10:00:15 +02:00
|
|
|
void provideData(quint64 address)
|
2013-05-25 03:10:12 +02:00
|
|
|
{
|
2014-12-21 21:54:30 +02:00
|
|
|
const FileName fn = filePath();
|
2013-07-04 11:35:56 +02:00
|
|
|
if (fn.isEmpty())
|
2011-02-28 14:33:04 +01:00
|
|
|
return;
|
2014-12-21 21:54:30 +02:00
|
|
|
QFile file(fn.toString());
|
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();
|
2016-07-14 10:00:15 +02:00
|
|
|
file.seek(address);
|
2009-07-21 16:46:24 +02:00
|
|
|
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);
|
2016-07-14 10:00:15 +02:00
|
|
|
m_widget->addData(address, data);
|
2011-04-04 15:24:13 +02:00
|
|
|
} else {
|
2015-02-03 23:56:02 +02:00
|
|
|
QMessageBox::critical(ICore::mainWindow(), tr("File Error"),
|
2011-04-04 15:24:13 +02:00
|
|
|
tr("Cannot open %1: %2").arg(
|
2014-12-21 21:54:30 +02:00
|
|
|
fn.toUserOutput(), 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)
|
|
|
|
|
{
|
2016-08-19 08:28:24 +02:00
|
|
|
if (filePath().exists())
|
|
|
|
|
openImpl(0, filePath().toString(), offset);
|
2010-07-02 09:58:54 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-21 16:46:24 +02:00
|
|
|
public:
|
2015-06-04 15:54:10 +02:00
|
|
|
bool isFileReadOnly() const override {
|
2014-12-21 21:54:30 +02:00
|
|
|
const FileName fn = filePath();
|
2013-07-12 15:36:29 +02:00
|
|
|
if (fn.isEmpty())
|
2011-02-25 13:21:54 +01:00
|
|
|
return false;
|
2014-12-21 21:54:30 +02:00
|
|
|
return !fn.toFileInfo().isWritable();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-04 15:54:10 +02:00
|
|
|
bool isSaveAsAllowed() const override { return true; }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override
|
|
|
|
|
{
|
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();
|
2015-06-04 15:21:02 +02:00
|
|
|
const bool success = (openImpl(errorString, filePath().toString()) == OpenResult::Success);
|
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
|
|
|
};
|
|
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
class BinEditor : public 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);
|
2014-10-30 00:45:38 +03:00
|
|
|
m_file = new BinEditorDocument(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;
|
2016-06-01 10:54:41 +02:00
|
|
|
auto addressValidator = new QRegExpValidator(QRegExp(QLatin1String("[0-9a-fA-F]{1,16}")),
|
|
|
|
|
m_addressEdit);
|
2010-07-02 14:38:49 +02:00
|
|
|
m_addressEdit->setValidator(addressValidator);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
auto l = new QHBoxLayout;
|
|
|
|
|
auto w = new QWidget;
|
2008-12-02 12:01:29 +01:00
|
|
|
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);
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
connect(widget, &BinEditorWidget::cursorPositionChanged,
|
|
|
|
|
this, &BinEditor::updateCursorPosition);
|
|
|
|
|
connect(m_addressEdit, &QLineEdit::editingFinished,
|
|
|
|
|
this, &BinEditor::jumpToAddress);
|
|
|
|
|
connect(widget, &BinEditorWidget::modificationChanged,
|
2016-10-07 15:58:40 +02:00
|
|
|
m_file, &IDocument::setModified);
|
|
|
|
|
connect(m_file, &IDocument::modificationChanged,
|
|
|
|
|
widget, &BinEditorWidget::setModified);
|
|
|
|
|
|
|
|
|
|
m_file->setModified(widget->isModified());
|
|
|
|
|
|
2014-10-30 00:45:38 +03:00
|
|
|
updateCursorPosition(widget->cursorPosition());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2013-05-25 03:47:40 +02:00
|
|
|
|
2015-06-01 17:55:31 +02:00
|
|
|
~BinEditor() override
|
2014-10-30 00:45:38 +03:00
|
|
|
{
|
2013-05-25 02:48:52 +02:00
|
|
|
delete m_widget;
|
2010-01-14 17:49:29 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2015-06-01 17:55:31 +02:00
|
|
|
IDocument *document() override { return m_file; }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2015-06-01 17:55:31 +02:00
|
|
|
QWidget *toolBar() override { return m_toolBar; }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
private:
|
|
|
|
|
void updateCursorPosition(qint64 position) {
|
2014-10-30 00:45:38 +03:00
|
|
|
m_addressEdit->setText(QString::number(editorWidget()->baseAddress() + position, 16));
|
2010-07-02 14:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void jumpToAddress() {
|
2014-10-30 00:45:38 +03:00
|
|
|
editorWidget()->jumpToAddress(m_addressEdit->text().toULongLong(0, 16));
|
|
|
|
|
updateCursorPosition(editorWidget()->cursorPosition());
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
BinEditorWidget *editorWidget() const
|
2014-10-30 00:45:38 +03:00
|
|
|
{
|
|
|
|
|
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:
|
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
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
IEditor *BinEditorFactory::createEditor()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2016-06-01 10:54:41 +02:00
|
|
|
auto widget = new BinEditorWidget();
|
|
|
|
|
auto 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
|
|
|
}
|
|
|
|
|
|
2016-07-14 10:00:15 +02:00
|
|
|
///////////////////////////////// BinEditor Services //////////////////////////////////
|
|
|
|
|
|
|
|
|
|
EditorService *FactoryServiceImpl::createEditorService(const QString &title0, bool wantsEditor)
|
|
|
|
|
{
|
|
|
|
|
BinEditorWidget *widget = nullptr;
|
|
|
|
|
if (wantsEditor) {
|
|
|
|
|
QString title = title0;
|
|
|
|
|
IEditor *editor = EditorManager::openEditorWithContents(
|
|
|
|
|
Core::Constants::K_DEFAULT_BINARY_EDITOR_ID, &title);
|
|
|
|
|
if (!editor)
|
|
|
|
|
return 0;
|
|
|
|
|
widget = qobject_cast<BinEditorWidget *>(editor->widget());
|
|
|
|
|
widget->setEditor(editor);
|
|
|
|
|
} else {
|
|
|
|
|
widget = new BinEditorWidget;
|
|
|
|
|
widget->setWindowTitle(title0);
|
|
|
|
|
}
|
|
|
|
|
return widget->editorService();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
///////////////////////////////// BinEditorPlugin //////////////////////////////////
|
|
|
|
|
|
2015-04-02 19:56:27 +03:00
|
|
|
BinEditorPlugin::BinEditorPlugin()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinEditorPlugin::~BinEditorPlugin()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
QAction *BinEditorPlugin::registerNewAction(Id id, const QString &title)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2016-06-01 10:54:41 +02:00
|
|
|
auto result = new QAction(title, this);
|
2015-02-03 23:56:02 +02:00
|
|
|
ActionManager::registerAction(result, id, m_context);
|
2008-12-02 12:01:29 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2016-06-01 10:54:41 +02:00
|
|
|
m_undoAction = registerNewAction(Core::Constants::UNDO, tr("&Undo"));
|
|
|
|
|
connect(m_undoAction, &QAction::triggered, this, &BinEditorPlugin::undoAction);
|
|
|
|
|
m_redoAction = registerNewAction(Core::Constants::REDO, tr("&Redo"));
|
|
|
|
|
connect(m_redoAction, &QAction::triggered, this, &BinEditorPlugin::redoAction);
|
|
|
|
|
m_copyAction = registerNewAction(Core::Constants::COPY);
|
|
|
|
|
connect(m_copyAction, &QAction::triggered, this, &BinEditorPlugin::copyAction);
|
|
|
|
|
m_selectAllAction = registerNewAction(Core::Constants::SELECTALL);
|
|
|
|
|
connect(m_selectAllAction, &QAction::triggered, this, &BinEditorPlugin::selectAllAction);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
QObject::connect(widget, &BinEditorWidget::undoAvailable,
|
|
|
|
|
this, &BinEditorPlugin::updateActions);
|
|
|
|
|
QObject::connect(widget, &BinEditorWidget::redoAvailable,
|
|
|
|
|
this, &BinEditorPlugin::updateActions);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
auto aggregate = new Aggregation::Aggregate;
|
|
|
|
|
auto 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
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
connect(Core::EditorManager::instance(), &EditorManager::currentEditorChanged,
|
|
|
|
|
this, &BinEditorPlugin::updateCurrentEditor);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-07-14 10:00:15 +02:00
|
|
|
addAutoReleasedObject(new FactoryServiceImpl);
|
2008-12-02 12:01:29 +01:00
|
|
|
addAutoReleasedObject(new BinEditorFactory(this));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinEditorPlugin::extensionsInitialized()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
void BinEditorPlugin::updateCurrentEditor(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"
|