Skip to content

Commit

Permalink
qt: removed confusing aliases
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 3, 2024
1 parent 3f5bdf6 commit 5f95062
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 106 deletions.
12 changes: 6 additions & 6 deletions src/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <QMessageBox>
#include <QObject>

void fdt::open_file_dialog(widget *parent, path_callable &&callable) {
const string_list filters{
void fdt::open_file_dialog(QWidget *parent, path_callable &&callable) {
const QStringList filters{
parent->tr("FDT formats (*.dtb *.dtbo *.itb)"),
parent->tr("FIT container (*.itb)"),
parent->tr("FDT file (*.dtb)"),
Expand All @@ -25,7 +25,7 @@ void fdt::open_file_dialog(widget *parent, path_callable &&callable) {
callable(path);
}

void fdt::open_directory_dialog(widget *parent, path_callable &&callable) {
void fdt::open_directory_dialog(QWidget *parent, path_callable &&callable) {
QFileDialog dialog(parent);
dialog.setFileMode(QFileDialog::Directory);
dialog.setWindowTitle(parent->tr("Open Flattened Device Tree"));
Expand All @@ -35,16 +35,16 @@ void fdt::open_directory_dialog(widget *parent, path_callable &&callable) {
callable(dir);
}

auto dialogs::ask_already_opened(widget *parent) noexcept -> bool {
auto dialogs::ask_already_opened(QWidget *parent) noexcept -> bool {
return QMessageBox::question(parent, parent->tr("Question"), parent->tr("File is already opened, do you want to reload?"), QMessageBox::Yes | QMessageBox::No) !=
QMessageBox::Yes;
}

auto dialogs::warn_invalid_fdt(const string &filename, widget *parent) noexcept -> void {
auto dialogs::warn_invalid_fdt(const QString &filename, QWidget *parent) noexcept -> void {
QMessageBox::critical(parent, parent->tr("Invalid FDT format"), parent->tr("Unable to parse %1").arg(filename));
}

auto fdt::export_property_file_dialog(widget *parent, const QByteArray &data, const QString &hint) -> void {
auto fdt::export_property_file_dialog(QWidget *parent, const QByteArray &data, const QString &hint) -> void {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QFileDialog::saveFileContent(data, hint);
#else
Expand Down
14 changes: 8 additions & 6 deletions src/dialogs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
#include <QByteArray>
#include <QString>

class QWidget;

namespace dialogs {
auto ask_already_opened(widget *parent) noexcept -> bool;
auto warn_invalid_fdt(const string &filename, widget *parent) noexcept -> void;
auto ask_already_opened(QWidget *parent) noexcept -> bool;
auto warn_invalid_fdt(const QString &filename, QWidget *parent) noexcept -> void;
} // namespace dialogs

namespace fdt {

using path_callable = std::function<void(const string &path)>;
using path_callable = std::function<void(const QString &path)>;

void open_file_dialog(widget *parent, path_callable &&callable);
void open_directory_dialog(widget *parent, path_callable &&callable);
auto export_property_file_dialog(widget *parent, const QByteArray &data, const QString &hint) -> void;
void open_file_dialog(QWidget *parent, path_callable &&callable);
void open_directory_dialog(QWidget *parent, path_callable &&callable);
auto export_property_file_dialog(QWidget *parent, const QByteArray &data, const QString &hint) -> void;

} // namespace fdt
2 changes: 1 addition & 1 deletion src/endian-conversions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <types.hpp>

#include <algorithm>
#include <bit>
#include <cstring>

Expand All @@ -18,7 +19,6 @@ template <std::integral T>
T convert(const T data) noexcept {
if constexpr (std::endian::native == std::endian::little)
return byteswap(data);

return data;
}

Expand Down
7 changes: 4 additions & 3 deletions src/fdt/fdt-generator-qt.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "fdt-generator-qt.hpp"
#include "fdt/fdt-parser-tokens.hpp"
#include "fdt/fdt-property-types.hpp"
#include <memory>
#include <string_view>

using namespace fdt::qt_wrappers;

tree_generator::tree_generator(tree_info &reference, tree_widget *target, string &&name, string &&id) {
tree_generator::tree_generator(tree_info &reference, QTreeWidget *target, QString &&name, QString &&id) {
m_root = [&]() {
if (reference.root)
return reference.root;
Expand All @@ -16,8 +17,8 @@ tree_generator::tree_generator(tree_info &reference, tree_widget *target, string
}();

m_root->setText(0, name);
m_root->setData(0, QT_ROLE_FILEPATH, id);
m_root->setIcon(0, QIcon::fromTheme("folder-open"));
m_root->setData(0, QT_ROLE_FILEPATH, id);
m_root->setData(0, QT_ROLE_NODETYPE, QVariant::fromValue(NodeType::Node));
m_root->setExpanded(true);
m_root->setSelected(true);
Expand Down Expand Up @@ -60,7 +61,7 @@ void tree_generator::end_node() noexcept {
void tree_generator::insert_property(const fdt::parser::token_types::property &prop) noexcept {
auto item = new QTreeWidgetItem(m_tree_stack.top());

fdt::qt_wrappers::property property{
auto property = fdt::qt_wrappers::property{
.name = QString::fromUtf8(prop.name.data(), prop.name.size()),
.data = QByteArray::fromRawData(prop.data.data(), prop.data.size()),
};
Expand Down
19 changes: 9 additions & 10 deletions src/fdt/fdt-generator-qt.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#pragma once

#include <fdt/fdt-header.hpp>
#include <fdt/fdt-property-types.hpp>
#include <types.hpp>
#include "fdt/fdt-property-types.hpp"
#include "fdt/fdt-parser-tokens.hpp"

#include <stack>
#include <string_view>

#include <QMetaType>
#include <QTreeWidgetItem>
#include <QHash>
#include "fdt/fdt-parser-tokens.hpp"
#include <stack>
#include <string_view>

Q_DECLARE_METATYPE(fdt::qt_wrappers::property)

Expand All @@ -27,20 +26,20 @@ Q_DECLARE_METATYPE(NodeType)
template <typename... types>
using hash_map = QHash<types...>;

using node_map = hash_map<string, QTreeWidgetItem *>;
using node_map = hash_map<QString, QTreeWidgetItem *>;

struct tree_info {
string id;
QString id;
QTreeWidgetItem *root{nullptr};
node_map nodes;
};

using tree_map = hash_map<string, tree_info>;
using tree_map = hash_map<QString, tree_info>;

namespace fdt::qt_wrappers {

struct tree_generator {
tree_generator(tree_info &reference, tree_widget *target, string &&name, string &&id);
tree_generator(tree_info &reference, QTreeWidget *target, QString &&name, QString &&id);

void begin_node(std::string_view) noexcept;
void end_node() noexcept;
Expand Down
2 changes: 1 addition & 1 deletion src/fdt/fdt-parser-context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct context {

struct {
const char *data{nullptr};
int skip{};
u32 skip{};
} state;
};

Expand Down
3 changes: 2 additions & 1 deletion src/fdt/fdt-property-types.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include <types.hpp>
#include <QHash>

Expand Down Expand Up @@ -31,7 +32,7 @@ struct property_info {
word_size word{word_size::_8};
};

const static QHash<string, property_info> property_map = {
const static QHash<QString, property_info> property_map = {
{"compatible", {property_type::string, word_size::custom}},
{"phandle", {property_type::number, word_size::_32}},
{"pinctrl-names", {property_type::multiline, word_size::custom}},
Expand Down
32 changes: 16 additions & 16 deletions src/fdt/fdt-view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <string_view>

namespace {
constexpr auto BINARY_PREVIEW_LIMIT = 256;
constexpr auto BINARY_PREVIEW_LIMIT = 64;

string present_u32be(const QByteArray &data) {
string ret;
QString present_u32be(const QByteArray &data) {
QString ret;

auto array = reinterpret_cast<u8 *>(const_cast<char *>(data.data()));
for (auto i = 0; i < data.size(); ++i) {
Expand All @@ -33,8 +33,8 @@ string present_u32be(const QByteArray &data) {
}

QString present(const fdt::qt_wrappers::property &p) {
auto &&name = p.name;
auto &&data = p.data;
auto &name = p.name;
auto &data = p.data;

auto result = [&](QString &&value) {
return name + " = <" + value + ">;";
Expand All @@ -50,20 +50,20 @@ QString present(const fdt::qt_wrappers::property &p) {
return result_str(QString(data.data()));

if (property_type::number == info.type)
return result(string::number(convert(*reinterpret_cast<const u32 *>(data.data()))));
return result(QString::number(convert(*reinterpret_cast<const u32 *>(data.data()))));
}

const static QRegularExpression cells_regexp("^#.*-cells$");
const static QRegularExpression names_regexp("^.*-names");

if (cells_regexp.match(name).hasMatch())
return result(string::number(convert(*reinterpret_cast<const u32 *>(data.data()))));
return result(QString::number(convert(*reinterpret_cast<const u32 *>(data.data()))));

if (names_regexp.match(name).hasMatch()) {
auto lines = data.split(0);
lines.removeLast();

string ret;
QString ret;
for (auto i = 0; i < lines.count(); ++i) {
if (i == lines.count() - 1)
ret += lines[i];
Expand All @@ -81,15 +81,15 @@ QString present(const fdt::qt_wrappers::property &p) {
}
} // namespace

fdt::viewer::viewer(tree_widget *target)
fdt::viewer::viewer(QTreeWidget *target)
: m_target(target) {
}

auto fdt::viewer::is_loaded(string &&id) const noexcept -> bool {
auto fdt::viewer::is_loaded(QString &&id) const noexcept -> bool {
return m_tree.contains(id);
}

auto fdt::viewer::is_loaded(const string &id) const noexcept -> bool {
auto fdt::viewer::is_loaded(const QString &id) const noexcept -> bool {
return m_tree.contains(id);
}

Expand All @@ -98,7 +98,7 @@ struct overloaded : Ts... {
using Ts::operator()...;
};

bool fdt::viewer::load(QByteArray &&data, string &&name, string &&id) {
bool fdt::viewer::load(QByteArray &&data, QString &&name, QString &&id) {
using namespace fdt::parser;
using namespace fdt::qt_wrappers;

Expand Down Expand Up @@ -128,11 +128,11 @@ bool fdt::viewer::load(QByteArray &&data, string &&name, string &&id) {
return true;
}

void fdt::viewer::drop(string &&id) {
void fdt::viewer::drop(QString &&id) {
m_tree.remove(id);
}

bool fdt::fdt_content_filter(QTreeWidgetItem *node, const std::function<bool(const string &)> &match) {
bool fdt::fdt_content_filter(QTreeWidgetItem *node, const std::function<bool(const QString &)> &match) {
QList<QTreeWidgetItem *> nodes;
QList<QTreeWidgetItem *> properties;

Expand Down Expand Up @@ -169,8 +169,8 @@ bool fdt::fdt_content_filter(QTreeWidgetItem *node, const std::function<bool(con
return isFound;
}

bool fdt::fdt_view_dts(QTreeWidgetItem *item, string &ret, int depth) {
string depth_str;
bool fdt::fdt_view_dts(QTreeWidgetItem *item, QString &ret, int depth) {
QString depth_str;
depth_str.fill(' ', depth * 4);

QList<QTreeWidgetItem *> nodes;
Expand Down
22 changes: 13 additions & 9 deletions src/fdt/fdt-view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

#include <fdt/fdt-generator-qt.hpp>

class QString;
class QByteArray;
class QFileInfo;

namespace fdt {

class viewer {
public:
viewer(tree_widget *target);
viewer(QTreeWidget *target);

auto is_loaded(string &&id) const noexcept -> bool;
auto is_loaded(const string &id) const noexcept -> bool;
auto is_loaded(QString &&id) const noexcept -> bool;
auto is_loaded(const QString &id) const noexcept -> bool;

auto load(QByteArray &&data, string &&name, string &&id) -> bool;
auto drop(string &&id) -> void;
auto load(QByteArray &&data, QString &&name, QString &&id) -> bool;
auto drop(QString &&id) -> void;

private:
tree_map m_tree;
tree_widget *m_target;
QTreeWidget *m_target;
};

bool fdt_view_prepare(tree_widget *target, const byte_array &datamap, const file_info &info);
bool fdt_view_dts(QTreeWidgetItem *item, string &ret, int depth = 0);
bool fdt_content_filter(QTreeWidgetItem *item, const std::function<bool(const string &)> &match);
bool fdt_view_prepare(QTreeWidget *target, const QByteArray &datamap, const QFileInfo &info);
bool fdt_view_dts(QTreeWidgetItem *item, QString &ret, int depth = 0);
bool fdt_content_filter(QTreeWidgetItem *item, const std::function<bool(const QString &)> &match);

} // namespace fdt
14 changes: 7 additions & 7 deletions src/main-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "fdt/fdt-parser-tokens.hpp"
#include "fdt/fdt-property-types.hpp"
#include "qabstractitemview.h"
#include "qelapsedtimer.h"
#include "ui_main-window.h"

#include <QAction>
Expand Down Expand Up @@ -65,7 +66,7 @@ MainWindow::MainWindow(QWidget *parent)
auto node = m_ui->treeWidget->invisibleRootItem();

fdt::fdt_content_filter(
node, [&text](const string &value) -> bool {
node, [&text](const QString &value) -> bool {
if (text.isEmpty())
return true;

Expand Down Expand Up @@ -113,25 +114,25 @@ MainWindow::~MainWindow() {
settings.window_position.set(geometry());
}

void MainWindow::open_directory(const string &path) {
void MainWindow::open_directory(const QString &path) {
QDirIterator iter(path, {"*.dtb", "*.dtbo"}, QDir::Files);
while (iter.hasNext()) {
iter.next();
open_file(iter.filePath());
}
}

void MainWindow::open_file(const string &path) {
void MainWindow::open_file(const QString &path) {
if (!open(path))
dialogs::warn_invalid_fdt(path, this);
}

bool MainWindow::open(const string &path) {
bool MainWindow::open(const QString &path) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
return false;

const auto info = file_info(path);
const auto info = QFileInfo(path);

if (m_viewer->is_loaded(info.absoluteFilePath()) &&
dialogs::ask_already_opened(this))
Expand Down Expand Up @@ -191,9 +192,8 @@ void MainWindow::update_view() {
m_ui->text_view->clear();
update_fdt_path(item);

string ret;
QString ret;
ret.reserve(VIEW_TEXT_CACHE_SIZE);

fdt::fdt_view_dts(item, ret);
m_ui->text_view->setText(ret);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main-window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class MainWindow : public QMainWindow {
void open_file_dialog();
void open_directory_dialog();

void open_directory(const string &path);
void open_file(const string &path);
void open_directory(const QString &path);
void open_file(const QString &path);

bool open(const string &path);
bool open(const QString &path);

private:
void update_fdt_path(QTreeWidgetItem *item = nullptr);
Expand Down
Loading

0 comments on commit 5f95062

Please sign in to comment.