Skip to content

Commit

Permalink
HexViewrPlugin: load files using an hex viewer
Browse files Browse the repository at this point in the history
I am amazed that this was so fast, and simple. 30 minutes work, with
playing and finding this new widget.
  • Loading branch information
diegoiast committed Sep 17, 2024
1 parent 2e416c8 commit 89022f2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ CPMAddPackage("gh:diegoiast/qmdilib#main")
CPMAddPackage("gh:diegoiast/qutepart-cpp#main")
CPMAddPackage("gh:diegoiast/command-palette-widget#main")
CPMAddPackage("gh:palacaze/image-viewer#master")
CPMAddPackage("gh:Dax89/QHexView#v5.0.0")

download_breeze_icons(6.4.0)

find_package(Qt6 COMPONENTS Widgets PrintSupport Network)
Expand Down Expand Up @@ -53,6 +55,8 @@ set(qtedit4_sources
src/plugins/help/help_plg.h
src/plugins/imageviewer/imageviewer_plg.cpp
src/plugins/imageviewer/imageviewer_plg.h
src/plugins/hexviewer/hexviewer_plg.cpp
src/plugins/hexviewer/hexviewer_plg.h
src/plugins/ProjectManager/ProjectBuildConfig.cpp
src/plugins/ProjectManager/ProjectBuildConfig.h
src/plugins/ProjectManager/ProjectManagerPlg.cpp
Expand Down Expand Up @@ -86,7 +90,7 @@ target_include_directories(qtedit4 PUBLIC
${qutepart_SOURCE_DIR}/include
src/widgets
)
target_link_libraries(qtedit4 PUBLIC qmdilib qutepart CommandPaletteWidget Qt6::PrintSupport Pal::ImageViewer)
target_link_libraries(qtedit4 PUBLIC qmdilib qutepart CommandPaletteWidget Qt6::PrintSupport Pal::ImageViewer QHexView)

if(MSVC)
# if we don't do this - file will be under Debug/Relase subdir
Expand Down
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ Tasks for v0.0.1

1. ~~Name the main window~~
1. ~~Icon for main window~~
1. Hex editor support for binary files (https://github.com/Simsys/qhexedit2)
1. ~~Hex editor support for binary files, https://github.com/Dax89/QHexView~~
1. ~~Image viewer (used palacaze/image-viewer)~~
1. Markdown display
1. Internal web browser? why not
1. First run actions
1. Create kits
1. Windows:
Expand All @@ -122,8 +121,9 @@ Tasks for v0.0.1
1. Download ninja
1. Download git
1. Download Qt (?)
1. SCM support
1. Project plugin will need a refactor, its getting too ugly.
1. SCM support
1. Internal web browser? why not
1. Global hotkeys https://github.com/Skycoder42/QHotkey/blob/master/README.md
1. Updater: https://github.com/alex-spataru/QSimpleUpdater, https://github.com/Skycoder42/QtAutoUpdater
1. Terminal widget: https://github.com/lxqt/qtermwidget (see what Kate did "back then")
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "plugins/ProjectManager/ProjectManagerPlg.h"
#include "plugins/filesystem/filesystembrowser.h"
#include "plugins/help/help_plg.h"
#include "plugins/hexviewer/hexviewer_plg.h"
#include "plugins/imageviewer/imageviewer_plg.h"
#include "plugins/texteditor/texteditor_plg.h"

Expand Down Expand Up @@ -59,6 +60,7 @@ int main(int argc, char *argv[]) {
pluginManager.addPlugin(new HelpPlugin);
pluginManager.addPlugin(new ProjectManagerPlugin);
pluginManager.addPlugin(new ImageViewrPlugin);
pluginManager.addPlugin(new HexViewrPlugin);
pluginManager.updateGUI();
pluginManager.hideUnusedPanels();
pluginManager.restoreSettings();
Expand Down
70 changes: 70 additions & 0 deletions src/plugins/hexviewer/hexviewer_plg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* \file imageviewer_plg
* \brief Definition of
* \author Diego Iastrubni [email protected]
* License MIT
* \see class name
*/

#include "hexviewer_plg.h"
#include <QFileInfo>
#include <model/buffer/qmemorybuffer.h>
#include <qhexview.h>

class qmdiHexViewer : public QHexView, public qmdiClient {
public:
QString thisFileName;
qmdiHexViewer(QWidget *p, const QString &fileName) : QHexView(p) {
QHexDocument *document = QHexDocument::fromMappedFile(fileName, this);
this->setDocument(document);

auto fi = QFileInfo(fileName);
this->mdiClientName = fi.fileName();
this->thisFileName = fileName;
}

virtual QString mdiClientFileName() override { return thisFileName; }

virtual std::optional<std::tuple<int, int, int>> get_coordinates() const override { return {}; }
};

HexViewrPlugin::HexViewrPlugin() {
name = tr("Image viewer plugin - based on QutePart");
author = tr("Diego Iastrubni <[email protected]>");
iVersion = 0;
sVersion = "0.0.1";
autoEnabled = true;
alwaysEnabled = false;
}

HexViewrPlugin::~HexViewrPlugin() {}

QStringList HexViewrPlugin::myExtensions() {
QStringList s;
// s << tr("Images", "ImageViewrPlugin::myExtensions") + " (*.jpg *.jpeg *.bmp *.png *.pcx
// *.ico)";
return s;
}

int HexViewrPlugin::canOpenFile(const QString fileName) {
static const QStringList extensions = {".bin", ".img", "blob", ".so", ".AppImage",
".a", ".exe", ".dll", ".dlib"};
for (const QString &ext : extensions) {
if (fileName.endsWith(ext, Qt::CaseInsensitive)) {
return 5;
}
}

if (!fileName.contains(".")) {
return 5;
}
return 1;
}

bool HexViewrPlugin::openFile(const QString fileName, int x, int y, int z) {
auto tabWidget = dynamic_cast<QTabWidget *>(mdiServer);
auto fi = QFileInfo(fileName);
auto viewer = new qmdiHexViewer(tabWidget, fileName);
mdiServer->addClient(viewer);
return true;
}
24 changes: 24 additions & 0 deletions src/plugins/hexviewer/hexviewer_plg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* \file imageviewer_plg.h
* \brief Definition of the image viewer system plugin
* \author Diego Iastrubni ([email protected])
* License MIT
* \see ImageViewer plugin
*/

#pragma once

#include "iplugin.h"

class QAction;

class HexViewrPlugin : public IPlugin {
Q_OBJECT
public:
HexViewrPlugin();
~HexViewrPlugin();

QStringList myExtensions() override;
int canOpenFile(const QString fileName) override;
bool openFile(const QString fileName, int x = -1, int y = -1, int z = -1) override;
};

0 comments on commit 89022f2

Please sign in to comment.