2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "bineditorconstants.h"
|
2016-07-14 10:00:15 +02:00
|
|
|
#include "bineditorservice.h"
|
2022-09-29 17:46:03 +02:00
|
|
|
#include "bineditortr.h"
|
|
|
|
|
#include "bineditorwidget.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2023-10-19 17:17:37 +02:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2023-02-17 23:21:31 +01:00
|
|
|
#include <coreplugin/coreplugintr.h>
|
2023-10-19 17:17:37 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2022-09-29 17:46:03 +02:00
|
|
|
#include <coreplugin/editormanager/ieditorfactory.h>
|
2023-10-19 17:17:37 +02:00
|
|
|
#include <coreplugin/find/ifindsupport.h>
|
2011-04-04 15:24:13 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2023-10-19 17:17:37 +02:00
|
|
|
#include <coreplugin/idocument.h>
|
|
|
|
|
|
2024-01-12 11:34:20 +01:00
|
|
|
#include <extensionsystem/iplugin.h>
|
2023-10-19 17:17:37 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2011-04-04 15:24:13 +02:00
|
|
|
|
2022-09-29 17:46:03 +02:00
|
|
|
#include <texteditor/codecchooser.h>
|
2011-02-28 14:33:04 +01:00
|
|
|
|
2023-10-19 17:17:37 +02:00
|
|
|
#include <utils/mimeconstants.h>
|
|
|
|
|
#include <utils/reloadpromptutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QAction>
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
#include <QLineEdit>
|
2022-09-29 17:46:03 +02:00
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QMessageBox>
|
2020-03-18 13:32:28 +01:00
|
|
|
#include <QRegularExpressionValidator>
|
2022-09-29 17:46:03 +02:00
|
|
|
#include <QTextCodec>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QToolBar>
|
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
|
|
|
|
2022-09-29 17:46:03 +02:00
|
|
|
namespace BinEditor::Internal {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2023-08-17 18:48:45 +02:00
|
|
|
class BinEditorFactory final : public QObject, public IEditorFactory
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2022-09-29 17:46:03 +02:00
|
|
|
public:
|
|
|
|
|
BinEditorFactory();
|
|
|
|
|
};
|
2013-05-25 02:48:52 +02:00
|
|
|
|
2022-09-29 17:46:03 +02:00
|
|
|
class BinEditorFind : public IFindSupport
|
|
|
|
|
{
|
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
|
|
|
}
|
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
|
|
|
{
|
2023-05-22 13:56:37 +02:00
|
|
|
m_widget->highlightSearchResults(txt.toLatin1(),
|
|
|
|
|
Utils::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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 13:56:37 +02:00
|
|
|
int res = m_widget->find(pattern, pos, Utils::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;
|
2023-05-22 13:56:37 +02:00
|
|
|
res = m_widget->find(pattern, pos, Utils::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;
|
2023-05-22 13:56:37 +02:00
|
|
|
m_widget->highlightSearchResults(pattern,
|
|
|
|
|
Utils::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;
|
2020-01-17 15:50:46 +01:00
|
|
|
m_widget->highlightSearchResults(QByteArray(), {});
|
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) {
|
2018-07-15 15:14:46 +03:00
|
|
|
m_contPos = m_widget->cursorPosition() + 1;
|
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;
|
2023-05-22 13:56:37 +02:00
|
|
|
if (wasReset) {
|
|
|
|
|
m_widget->highlightSearchResults(pattern,
|
|
|
|
|
Utils::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;
|
2018-02-01 18:05:57 +01:00
|
|
|
qint64 m_incrementalStartPos = -1;
|
|
|
|
|
qint64 m_contPos = -1; // Only valid if last result was NotYetFound.
|
|
|
|
|
bool m_incrementalWrappedState = false;
|
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);
|
2023-10-19 17:17:37 +02:00
|
|
|
setMimeType(Utils::Constants::OCTET_STREAM_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
|
|
|
{
|
2018-08-21 13:42:41 +02:00
|
|
|
return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type);
|
2014-11-06 09:20:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 14:24:11 +02:00
|
|
|
OpenResult open(QString *errorString, const FilePath &filePath,
|
|
|
|
|
const FilePath &realFilePath) override
|
2015-06-02 17:14:48 +02:00
|
|
|
{
|
2021-05-18 13:55:23 +02:00
|
|
|
QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving
|
|
|
|
|
return openImpl(errorString, filePath);
|
2015-06-02 17:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 14:24:11 +02:00
|
|
|
OpenResult openImpl(QString *errorString, const FilePath &filePath, quint64 offset = 0)
|
2015-06-02 17:14:48 +02:00
|
|
|
{
|
2021-07-23 14:24:11 +02:00
|
|
|
const qint64 size = filePath.fileSize();
|
|
|
|
|
if (size < 0) {
|
2022-09-29 17:46:03 +02:00
|
|
|
QString msg = Tr::tr("Cannot open %1: %2").arg(filePath.toUserOutput(), Tr::tr("File Error"));
|
2021-07-23 14:24:11 +02:00
|
|
|
// FIXME: Was: file.errorString(), but we don't have a file anymore.
|
|
|
|
|
if (errorString)
|
|
|
|
|
*errorString = msg;
|
|
|
|
|
else
|
2022-09-29 17:46:03 +02:00
|
|
|
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
|
2021-07-23 14:24:11 +02:00
|
|
|
return OpenResult::ReadError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size == 0) {
|
2022-09-29 17:46:03 +02:00
|
|
|
QString msg = Tr::tr("The Binary Editor cannot open empty files.");
|
2021-07-23 14:24:11 +02:00
|
|
|
if (errorString)
|
|
|
|
|
*errorString = msg;
|
|
|
|
|
else
|
2022-09-29 17:46:03 +02:00
|
|
|
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
|
2021-07-23 14:24:11 +02:00
|
|
|
return OpenResult::CannotHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size / 16 >= qint64(1) << 31) {
|
|
|
|
|
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
|
2022-09-29 17:46:03 +02:00
|
|
|
QString msg = Tr::tr("The file is too big for the Binary Editor (max. 32GB).");
|
2021-07-23 14:24:11 +02:00
|
|
|
if (errorString)
|
|
|
|
|
*errorString = msg;
|
|
|
|
|
else
|
2022-09-29 17:46:03 +02:00
|
|
|
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
|
2021-07-23 14:24:11 +02:00
|
|
|
return OpenResult::CannotHandle;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2021-07-23 14:24:11 +02:00
|
|
|
|
2021-08-18 15:44:30 +02:00
|
|
|
if (offset >= quint64(size))
|
2021-07-23 14:24:11 +02:00
|
|
|
return OpenResult::CannotHandle;
|
|
|
|
|
|
|
|
|
|
setFilePath(filePath);
|
|
|
|
|
m_widget->setSizes(offset, size);
|
|
|
|
|
return OpenResult::Success;
|
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
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
const FilePath fn = filePath();
|
2013-07-04 11:35:56 +02:00
|
|
|
if (fn.isEmpty())
|
2011-02-28 14:33:04 +01:00
|
|
|
return;
|
2021-07-23 14:24:11 +02:00
|
|
|
const int blockSize = m_widget->dataBlockSize();
|
2022-09-09 13:48:08 +02:00
|
|
|
QByteArray data = fn.fileContents(blockSize, address).value_or(QByteArray());
|
2021-07-23 14:24:11 +02:00
|
|
|
const int dataSize = data.size();
|
|
|
|
|
if (dataSize != blockSize)
|
|
|
|
|
data += QByteArray(blockSize - dataSize, 0);
|
|
|
|
|
m_widget->addData(address, data);
|
2022-09-29 17:46:03 +02:00
|
|
|
// QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"),
|
|
|
|
|
// Tr::tr("Cannot open %1: %2").arg(
|
2021-07-23 14:24:11 +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())
|
2021-05-18 13:55:23 +02:00
|
|
|
openImpl(nullptr, filePath(), offset);
|
2010-07-02 09:58:54 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-21 16:46:24 +02:00
|
|
|
public:
|
2016-10-20 08:34:55 +00:00
|
|
|
bool isModified() const override
|
|
|
|
|
{
|
|
|
|
|
return isTemporary()/*e.g. memory view*/ ? false
|
|
|
|
|
: m_widget->isModified();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2021-01-13 16:27:02 +01:00
|
|
|
Q_UNUSED(type)
|
2010-03-19 10:28:05 +01:00
|
|
|
if (flag == FlagIgnore)
|
2011-04-04 15:24:13 +02:00
|
|
|
return true;
|
2021-01-13 16:27:02 +01:00
|
|
|
emit aboutToReload();
|
|
|
|
|
int cPos = m_widget->cursorPosition();
|
|
|
|
|
m_widget->clear();
|
2021-05-18 13:55:23 +02:00
|
|
|
const bool success = (openImpl(errorString, filePath()) == OpenResult::Success);
|
2021-01-13 16:27:02 +01:00
|
|
|
m_widget->setCursorPosition(cPos);
|
|
|
|
|
emit reloadFinished(success);
|
|
|
|
|
return success;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 15:00:15 +02:00
|
|
|
protected:
|
|
|
|
|
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
|
2023-08-24 12:48:37 +02:00
|
|
|
if (m_widget->save(errorString, this->filePath(), filePath)) {
|
|
|
|
|
setFilePath(filePath);
|
2023-06-13 15:00:15 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2024-01-12 11:34:20 +01:00
|
|
|
class BinEditorImpl: public IEditor
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2024-01-12 11:34:20 +01:00
|
|
|
BinEditorImpl(BinEditorWidget *widget)
|
2009-01-20 11:52:04 +01:00
|
|
|
{
|
2022-07-27 23:01:13 +03:00
|
|
|
using namespace TextEditor;
|
2013-05-25 02:48:52 +02:00
|
|
|
setWidget(widget);
|
2014-10-30 00:45:38 +03:00
|
|
|
m_file = new BinEditorDocument(widget);
|
2010-07-02 14:38:49 +02:00
|
|
|
m_addressEdit = new QLineEdit;
|
2020-03-18 13:32:28 +01:00
|
|
|
auto addressValidator = new QRegularExpressionValidator(QRegularExpression("[0-9a-fA-F]{1,16}"), m_addressEdit);
|
2010-07-02 14:38:49 +02:00
|
|
|
m_addressEdit->setValidator(addressValidator);
|
2022-07-27 23:01:13 +03:00
|
|
|
m_codecChooser = new CodecChooser(CodecChooser::Filter::SingleByte);
|
|
|
|
|
m_codecChooser->prependNone();
|
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->setContentsMargins(0, 0, 5, 0);
|
|
|
|
|
l->addStretch(1);
|
2022-07-27 23:01:13 +03:00
|
|
|
l->addWidget(m_codecChooser);
|
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,
|
2024-01-12 11:34:20 +01:00
|
|
|
this, &BinEditorImpl::updateCursorPosition);
|
2016-06-01 10:54:41 +02:00
|
|
|
connect(m_addressEdit, &QLineEdit::editingFinished,
|
2024-01-12 11:34:20 +01:00
|
|
|
this, &BinEditorImpl::jumpToAddress);
|
2022-07-27 23:01:13 +03:00
|
|
|
connect(m_codecChooser, &CodecChooser::codecChanged,
|
|
|
|
|
widget, &BinEditorWidget::setCodec);
|
2016-06-01 10:54:41 +02:00
|
|
|
connect(widget, &BinEditorWidget::modificationChanged,
|
2016-10-20 08:34:55 +00:00
|
|
|
m_file, &IDocument::changed);
|
2014-10-30 00:45:38 +03:00
|
|
|
updateCursorPosition(widget->cursorPosition());
|
2022-07-27 23:01:13 +03:00
|
|
|
const QVariant setting = ICore::settings()->value(Constants::C_ENCODING_SETTING);
|
|
|
|
|
if (!setting.isNull())
|
|
|
|
|
m_codecChooser->setAssignedCodec(QTextCodec::codecForName(setting.toByteArray()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2013-05-25 03:47:40 +02:00
|
|
|
|
2024-01-12 11:34:20 +01:00
|
|
|
~BinEditorImpl() 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
|
|
|
|
2018-09-25 20:47:14 +03:00
|
|
|
IDocument *document() const 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() {
|
2018-11-04 20:33:51 +01:00
|
|
|
editorWidget()->jumpToAddress(m_addressEdit->text().toULongLong(nullptr, 16));
|
2014-10-30 00:45:38 +03:00
|
|
|
updateCursorPosition(editorWidget()->cursorPosition());
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:54:41 +02:00
|
|
|
BinEditorWidget *editorWidget() const
|
2014-10-30 00:45:38 +03:00
|
|
|
{
|
2018-11-04 20:33:51 +01:00
|
|
|
QTC_ASSERT(qobject_cast<BinEditorWidget *>(m_widget.data()), return nullptr);
|
2014-10-30 00:45:38 +03:00
|
|
|
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;
|
2022-07-27 23:01:13 +03:00
|
|
|
TextEditor::CodecChooser *m_codecChooser;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2024-01-12 11:34:20 +01:00
|
|
|
///////////////////////////////// BinEditor Services //////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class FactoryServiceImpl final : public QObject, public FactoryService
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
Q_INTERFACES(BinEditor::FactoryService)
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
EditorService *createEditorService(const QString &title0, bool wantsEditor) final
|
|
|
|
|
{
|
|
|
|
|
BinEditorWidget *widget = nullptr;
|
|
|
|
|
if (wantsEditor) {
|
|
|
|
|
QString title = title0;
|
|
|
|
|
IEditor *editor = EditorManager::openEditorWithContents(
|
|
|
|
|
Core::Constants::K_DEFAULT_BINARY_EDITOR_ID, &title);
|
|
|
|
|
if (!editor)
|
|
|
|
|
return nullptr;
|
|
|
|
|
widget = qobject_cast<BinEditorWidget *>(editor->widget());
|
|
|
|
|
widget->setEditor(editor);
|
|
|
|
|
} else {
|
|
|
|
|
widget = new BinEditorWidget;
|
|
|
|
|
widget->setWindowTitle(title0);
|
|
|
|
|
}
|
|
|
|
|
return widget->editorService();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-01 18:05:57 +01:00
|
|
|
///////////////////////////////// BinEditorPluginPrivate //////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class BinEditorPluginPrivate : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
BinEditorPluginPrivate();
|
2018-11-04 20:33:51 +01:00
|
|
|
~BinEditorPluginPrivate() override;
|
2018-02-01 18:05:57 +01:00
|
|
|
|
|
|
|
|
QAction *m_undoAction = nullptr;
|
|
|
|
|
QAction *m_redoAction = nullptr;
|
|
|
|
|
QAction *m_copyAction = nullptr;
|
|
|
|
|
QAction *m_selectAllAction = nullptr;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-02-01 18:05:57 +01:00
|
|
|
FactoryServiceImpl m_factoryService;
|
|
|
|
|
BinEditorFactory m_editorFactory;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BinEditorPluginPrivate::BinEditorPluginPrivate()
|
|
|
|
|
{
|
|
|
|
|
ExtensionSystem::PluginManager::addObject(&m_factoryService);
|
|
|
|
|
ExtensionSystem::PluginManager::addObject(&m_editorFactory);
|
|
|
|
|
|
2023-01-22 22:02:24 +01:00
|
|
|
m_undoAction = new QAction(Tr::tr("&Undo"), this);
|
2022-09-29 17:46:03 +02:00
|
|
|
m_redoAction = new QAction(Tr::tr("&Redo"), this);
|
2018-02-01 18:05:57 +01:00
|
|
|
m_copyAction = new QAction(this);
|
|
|
|
|
m_selectAllAction = new QAction(this);
|
|
|
|
|
|
|
|
|
|
Context context;
|
|
|
|
|
context.add(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
|
|
|
|
|
context.add(Constants::C_BINEDITOR);
|
|
|
|
|
|
|
|
|
|
ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, context);
|
|
|
|
|
ActionManager::registerAction(m_redoAction, Core::Constants::REDO, context);
|
|
|
|
|
ActionManager::registerAction(m_copyAction, Core::Constants::COPY, context);
|
|
|
|
|
ActionManager::registerAction(m_selectAllAction, Core::Constants::SELECTALL, context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinEditorPluginPrivate::~BinEditorPluginPrivate()
|
|
|
|
|
{
|
|
|
|
|
ExtensionSystem::PluginManager::removeObject(&m_editorFactory);
|
|
|
|
|
ExtensionSystem::PluginManager::removeObject(&m_factoryService);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static BinEditorPluginPrivate *dd = nullptr;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
///////////////////////////////// BinEditorFactory //////////////////////////////////
|
|
|
|
|
|
2018-02-01 18:05:57 +01:00
|
|
|
BinEditorFactory::BinEditorFactory()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-05-31 19:35:37 +02:00
|
|
|
setId(Core::Constants::K_DEFAULT_BINARY_EDITOR_ID);
|
2023-02-17 23:21:31 +01:00
|
|
|
setDisplayName(::Core::Tr::tr("Binary Editor"));
|
2023-10-19 17:17:37 +02:00
|
|
|
addMimeType(Utils::Constants::OCTET_STREAM_MIMETYPE);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-05-11 09:48:23 +02:00
|
|
|
setEditorCreator([] {
|
2020-02-04 18:16:57 +01:00
|
|
|
auto widget = new BinEditorWidget();
|
2024-01-12 11:34:20 +01:00
|
|
|
auto editor = new BinEditorImpl(widget);
|
2018-02-01 18:05:57 +01:00
|
|
|
|
2020-02-04 18:16:57 +01:00
|
|
|
connect(dd->m_undoAction, &QAction::triggered, widget, &BinEditorWidget::undo);
|
|
|
|
|
connect(dd->m_redoAction, &QAction::triggered, widget, &BinEditorWidget::redo);
|
|
|
|
|
connect(dd->m_copyAction, &QAction::triggered, widget, &BinEditorWidget::copy);
|
|
|
|
|
connect(dd->m_selectAllAction, &QAction::triggered, widget, &BinEditorWidget::selectAll);
|
2018-02-01 18:05:57 +01:00
|
|
|
|
2020-02-04 18:16:57 +01:00
|
|
|
auto updateActions = [widget] {
|
|
|
|
|
dd->m_selectAllAction->setEnabled(true);
|
|
|
|
|
dd->m_undoAction->setEnabled(widget->isUndoAvailable());
|
|
|
|
|
dd->m_redoAction->setEnabled(widget->isRedoAvailable());
|
|
|
|
|
};
|
2018-02-01 18:05:57 +01:00
|
|
|
|
2020-02-04 18:16:57 +01:00
|
|
|
connect(widget, &BinEditorWidget::undoAvailable, widget, updateActions);
|
|
|
|
|
connect(widget, &BinEditorWidget::redoAvailable, widget, updateActions);
|
2018-02-01 18:05:57 +01:00
|
|
|
|
2020-02-04 18:16:57 +01:00
|
|
|
auto aggregate = new Aggregation::Aggregate;
|
|
|
|
|
auto binEditorFind = new BinEditorFind(widget);
|
|
|
|
|
aggregate->add(binEditorFind);
|
|
|
|
|
aggregate->add(widget);
|
2018-02-01 18:05:57 +01:00
|
|
|
|
2020-02-04 18:16:57 +01:00
|
|
|
return editor;
|
|
|
|
|
});
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////// BinEditorPlugin //////////////////////////////////
|
|
|
|
|
|
2024-01-12 11:34:20 +01:00
|
|
|
class BinEditorPlugin final : public ExtensionSystem::IPlugin
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2024-01-12 11:34:20 +01:00
|
|
|
Q_OBJECT
|
|
|
|
|
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "BinEditor.json")
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2024-01-12 11:34:20 +01:00
|
|
|
~BinEditorPlugin() override
|
|
|
|
|
{
|
|
|
|
|
delete dd;
|
|
|
|
|
dd = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initialize() final
|
|
|
|
|
{
|
|
|
|
|
dd = new BinEditorPluginPrivate;
|
|
|
|
|
}
|
|
|
|
|
};
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-09-29 17:46:03 +02:00
|
|
|
} // BinEditor::Internal
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include "bineditorplugin.moc"
|