Skip to content

Commit

Permalink
gui: added qscintilla editor and syntax highlights
Browse files Browse the repository at this point in the history
Signed-off-by: Bartłomiej Burdukiewicz <[email protected]>
  • Loading branch information
dev-0x7C6 committed Jul 20, 2024
1 parent 839e055 commit 651ef5b
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 17 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_compile_definitions("-D__cpp_concepts=202002L")

find_package(Qt6 COMPONENTS Widgets Core Qsci)

find_path(QSCI_INCLUDE_DIR NAMES qt6/Qsci/qsciscintilla.h)
find_library(QSCI_LIBRARY NAMES qscintilla2_qt6)

if (QSCI_INCLUDE_DIR AND QSCI_LIBRARY)
message(STATUS "Found QScintilla")
else()
message(FATAL_ERROR "QScintilla not found")
endif()

add_library(QScintilla INTERFACE)
target_include_directories(QScintilla INTERFACE ${QSCI_INCLUDE_DIR})
target_link_libraries(QScintilla INTERFACE ${QSCI_LIBRARY})

find_package(Qt6 COMPONENTS Widgets Core)
if (Qt6_FOUND)
add_subdirectory("src")
Expand Down
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ add_subdirectory("submodules/qhexview")

file(GLOB_RECURSE fdt-viewer-headers *.hpp)
file(GLOB_RECURSE fdt-viewer-sources *.cpp)
file(GLOB_RECURSE fdt-viewer-uis *.ui)

add_executable(fdt-viewer
${fdt-viewer-headers}
${fdt-viewer-sources}
${fdt-viewer-uis}
../resources.qrc
)

target_link_libraries(fdt-viewer PRIVATE Qt6::Widgets Qt6::Core QHexView)
target_link_libraries(fdt-viewer PRIVATE Qt6::Widgets Qt6::Core QScintilla QHexView)
install(TARGETS fdt-viewer RUNTIME DESTINATION bin)
9 changes: 9 additions & 0 deletions src/fdt/fdt-generator-qt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ struct tree_info {

using tree_map = hash_map<QString, tree_info>;

namespace fdt {
template <typename T>
concept TreeGenerator = requires(T t, std::string_view sv, const fdt::parser::token_types::property &prop) {
{ t.begin_node(sv) };
{ t.end_node() };
{ t.insert_property(prop) };
};
} // namespace fdt

namespace fdt::qt_wrappers {

constexpr auto ROLE_PROPERTY = Qt::UserRole;
Expand Down
5 changes: 5 additions & 0 deletions src/fdt/fdt-header.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <types.hpp>
#include <cstddef>

namespace fdt::decode {
struct header {
Expand All @@ -26,6 +27,10 @@ constexpr auto is_magic_invalid(const header &v) -> bool {
return v.magic != header_magic_value;
}

constexpr auto is_data_truncated(const header &v, const std::size_t size) -> bool {
return size < v.totalsize;
}

constexpr auto is_version_unsupported(const header &v) -> bool {
constexpr auto header_support_above = 16;
return v.version <= header_support_above;
Expand Down
2 changes: 1 addition & 1 deletion src/fdt/fdt-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ auto fdt::parser::parse(std::string_view view) -> std::expected<fdt::parser::tok
if (decode::is_magic_invalid(header))
return std::unexpected(error::invalid_magic);

if (view.size() < header.totalsize)
if (decode::is_data_truncated(header, view.size()))
return std::unexpected(error::data_truncated);

if (decode::is_version_unsupported(header))
Expand Down
5 changes: 4 additions & 1 deletion src/fdt/fdt-view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <endian-conversions.hpp>
#include <fdt/fdt-generator-qt.hpp>
#include <fdt/fdt-parser.hpp>
#include <cstddef>

#include <QTreeWidget>
#include <QTreeWidgetItem>
Expand Down Expand Up @@ -102,7 +103,7 @@ bool fdt::viewer::load(QByteArray &&data, QString &&name, QString &&id) {
using namespace fdt::parser;
using namespace fdt::qt_wrappers;

auto tokens = parse({data.data(), data.size()});
auto tokens = parse({data.data(), static_cast<std::size_t>(data.size())});

if (!tokens)
return false;
Expand All @@ -112,7 +113,9 @@ bool fdt::viewer::load(QByteArray &&data, QString &&name, QString &&id) {

fdt::parser::rename_root(tokens.value(), name.toStdString());

m_target->blockSignals(true);
tree_generator generator(m_tree[id], m_target, std::move(name), std::move(id));
m_target->blockSignals(false);

for (auto &&token : tokens.value())
std::visit(overloaded{
Expand Down
34 changes: 26 additions & 8 deletions src/main-window.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "main-window.hpp"
#include "fdt/fdt-parser-tokens.hpp"
#include "fdt/fdt-property-types.hpp"
#include "qabstractitemview.h"
#include "ui_main-window.h"

#include <QAction>
Expand All @@ -19,6 +17,9 @@
#include <menu-manager.hpp>
#include <viewer-settings.hpp>

#include <Qsci/qscilexercpp.h>
#include <Qsci/qsciscintilla.h>

#include "submodules/qhexview/model/buffer/qmemorybuffer.h"
#include "submodules/qhexview/qhexview.h"

Expand Down Expand Up @@ -46,10 +47,10 @@ MainWindow::MainWindow(QWidget *parent)
connect(m_menu.get(), &menu_manager::show_normal, this, &MainWindow::showNormal);
connect(m_menu.get(), &menu_manager::quit, this, &MainWindow::close);

m_ui->text_view->setWordWrapMode(QTextOption::NoWrap);
m_ui->editor->setWrapMode(QsciScintilla::WrapNone);

connect(m_menu.get(), &menu_manager::use_word_wrap, [this](const bool value) {
m_ui->text_view->setWordWrapMode(value ? QTextOption::WordWrap : QTextOption::NoWrap);
m_ui->editor->setWrapMode(value ? QsciScintilla::WrapWord : QsciScintilla::WrapNone);
});

connect(m_menu.get(), &menu_manager::show_about_qt, []() { QApplication::aboutQt(); });
Expand Down Expand Up @@ -97,7 +98,18 @@ MainWindow::MainWindow(QWidget *parent)
connect(m_ui->treeWidget, &QTreeWidget::itemSelectionChanged, this, &MainWindow::update_view);

viewer_settings settings;
m_ui->text_view->setWordWrapMode(settings.view_word_wrap.value() ? QTextOption::WordWrap : QTextOption::NoWrap);
auto lexer = new QsciLexerCPP(this, false);

lexer->setFont(QFont{});
lexer->setColor(Qt::yellow, QsciLexerCPP::Identifier);
lexer->setColor(Qt::lightGray, QsciLexerCPP::Operator);
lexer->setColor(Qt::green, QsciLexerCPP::DoubleQuotedString);

m_ui->editor->setLexer(lexer);
m_ui->editor->setMarginsBackgroundColor(Qt::black);
m_ui->editor->setMarginsForegroundColor(Qt::green);
m_ui->editor->setMarginType(0, QsciScintilla::NumberMargin);
m_ui->editor->setMarginWidth(0, 50);

if (settings.window_show_fullscreen.value())
showFullScreen();
Expand Down Expand Up @@ -172,7 +184,7 @@ void MainWindow::update_view() {

if (m_ui->treeWidget->selectedItems().isEmpty()) {
m_ui->preview->setCurrentWidget(m_ui->text_view_page);
m_ui->text_view->clear();
m_ui->editor->clear();
m_ui->statusbar->clearMessage();
m_ui->path->clear();
return;
Expand All @@ -188,13 +200,19 @@ void MainWindow::update_view() {
m_hexview->setDocument(QHexDocument::fromMemory<QMemoryBuffer>(property.data));
}

m_ui->text_view->clear();
m_ui->editor->clear();
update_fdt_path(item);

QString ret;
ret.reserve(VIEW_TEXT_CACHE_SIZE);
fdt::fdt_view_dts(item, ret);
m_ui->text_view->setText(ret);

m_ui->editor->setText(ret);

QFontMetrics metrics(QFont{});
const auto num = QString::number(m_ui->editor->lines());

m_ui->editor->setMarginWidth(0, metrics.horizontalAdvance(num) * 1.25);
}

void MainWindow::property_export() {
Expand Down
15 changes: 9 additions & 6 deletions src/main-window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<widget class="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -90,11 +90,7 @@
<number>0</number>
</property>
<item>
<widget class="QTextBrowser" name="text_view">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
</widget>
<widget class="QsciScintilla" name="editor" native="true"/>
</item>
</layout>
</widget>
Expand Down Expand Up @@ -122,6 +118,13 @@
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>QsciScintilla</class>
<extends>QWidget</extends>
<header location="global">Qsci/qsciscintilla.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit 651ef5b

Please sign in to comment.