From c98a0c26c4ccb5c4ae0e9f5810be910a7b299037 Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 26 Apr 2025 03:06:14 -0400 Subject: Add proper tables display for storage devices --- gui/storageview.cc | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 gui/storageview.cc (limited to 'gui/storageview.cc') diff --git a/gui/storageview.cc b/gui/storageview.cc new file mode 100644 index 0000000..f6f9736 --- /dev/null +++ b/gui/storageview.cc @@ -0,0 +1,70 @@ +// Simulator for the RISC-V[ECTOR] mini-ISA +// Copyright (C) 2025 Siddarth Suresh +// Copyright (C) 2025 bdunahu + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "storageview.h" +#include "definitions.h" +#include +#include + +StorageView::StorageView(int rows, QObject *parent) + : QAbstractTableModel(parent) +{ + this->r = rows; + this->d.resize(rows); + for (auto &row : this->d) + row.resize(LINE_SIZE, 0); +} + +int StorageView::rowCount(const QModelIndex &) const { return this->r; } + +int StorageView::columnCount(const QModelIndex &) const { return LINE_SIZE; } + +QVariant StorageView::data(const QModelIndex &i, int role) const +{ + Qt::Alignment a; + + if (role == Qt::TextAlignmentRole) { + a = Qt::AlignRight | Qt::AlignVCenter; + return QVariant(static_cast(a)); + } + if (!i.isValid() || role != Qt::DisplayRole) + return QVariant(); + return this->d[i.row()][i.column()]; +} + +QVariant StorageView::headerData(int section, Qt::Orientation o, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (o == Qt::Vertical) { + return section * 4; + } + return QVariant(); +} + +Qt::ItemFlags StorageView::flags(const QModelIndex &i) const { + (void)i; + return Qt::ItemIsEnabled; +} + +void StorageView::set_data(const QVector> &data) +{ + beginResetModel(); + this->d = data; + endResetModel(); +} -- cgit v1.2.3 From b32bc409c18ceb4cd8147f11021e2c4b2746184b Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 26 Apr 2025 14:41:02 -0400 Subject: right-align colum headers on table objects --- gui/digitlabeldelegate.cc | 6 ------ gui/digitlabeldelegate.h | 3 +-- gui/digitlabelhelper.h | 2 +- gui/storageview.cc | 7 +++++++ 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'gui/storageview.cc') diff --git a/gui/digitlabeldelegate.cc b/gui/digitlabeldelegate.cc index 641b5a0..bf4b175 100644 --- a/gui/digitlabeldelegate.cc +++ b/gui/digitlabeldelegate.cc @@ -19,12 +19,6 @@ #include "digitlabelhelper.h" #include -DigitLabelDelegate::DigitLabelDelegate(QObject *parent) - : QStyledItemDelegate(parent) -{ - ; -} - void DigitLabelDelegate::set_hex_display(bool hex) { if (this->is_hex != hex) { diff --git a/gui/digitlabeldelegate.h b/gui/digitlabeldelegate.h index a823154..7714d25 100644 --- a/gui/digitlabeldelegate.h +++ b/gui/digitlabeldelegate.h @@ -28,9 +28,8 @@ class DigitLabelDelegate : public QStyledItemDelegate { Q_OBJECT - public: - explicit DigitLabelDelegate(QObject *parent = nullptr); + using QStyledItemDelegate::QStyledItemDelegate; public slots: void set_hex_display(bool hex); diff --git a/gui/digitlabelhelper.h b/gui/digitlabelhelper.h index 715089a..26e4637 100644 --- a/gui/digitlabelhelper.h +++ b/gui/digitlabelhelper.h @@ -36,7 +36,7 @@ class DigitLabelHelper { if (is_cleared) return QString(); - return is_hex ? QString::asprintf("%04X", value) + return is_hex ? QString::asprintf("%X", value) : QString::number(value); } }; diff --git a/gui/storageview.cc b/gui/storageview.cc index f6f9736..43837a9 100644 --- a/gui/storageview.cc +++ b/gui/storageview.cc @@ -48,6 +48,13 @@ QVariant StorageView::data(const QModelIndex &i, int role) const QVariant StorageView::headerData(int section, Qt::Orientation o, int role) const { + Qt::Alignment a; + + if (role == Qt::TextAlignmentRole) { + a = Qt::AlignRight | Qt::AlignVCenter; + return QVariant(static_cast(a)); + } + if (role != Qt::DisplayRole) return QVariant(); -- cgit v1.2.3 From ae2aaecfc1b2402a55e99cf674eff7b6175b0b6d Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 26 Apr 2025 14:57:56 -0400 Subject: Basic hex formatting on row headers for storage tables --- gui/digitlabelhelper.h | 3 +-- gui/gui.cc | 3 ++- gui/storageview.cc | 15 +++++++++++++-- gui/storageview.h | 7 +++++++ 4 files changed, 23 insertions(+), 5 deletions(-) (limited to 'gui/storageview.cc') diff --git a/gui/digitlabelhelper.h b/gui/digitlabelhelper.h index 26e4637..4d45c68 100644 --- a/gui/digitlabelhelper.h +++ b/gui/digitlabelhelper.h @@ -36,8 +36,7 @@ class DigitLabelHelper { if (is_cleared) return QString(); - return is_hex ? QString::asprintf("%X", value) - : QString::number(value); + return is_hex ? QString::asprintf("%X", value) : QString::number(value); } }; diff --git a/gui/gui.cc b/gui/gui.cc index 80d4a2a..2555435 100644 --- a/gui/gui.cc +++ b/gui/gui.cc @@ -353,7 +353,6 @@ void GUI::make_tabs(int num) qDeleteAll(this->tab_boxes); for (i = 0; i < num; ++i) { - // make the name if (i == 0) { n = "Registers"; e = new StorageView(0, this); @@ -370,6 +369,8 @@ void GUI::make_tabs(int num) t->setModel(e); d = new DigitLabelDelegate(t); + connect( + this, &GUI::hex_toggled, e, &StorageView::set_hex_display); connect( this, &GUI::hex_toggled, d, &DigitLabelDelegate::set_hex_display); diff --git a/gui/storageview.cc b/gui/storageview.cc index 43837a9..22baca0 100644 --- a/gui/storageview.cc +++ b/gui/storageview.cc @@ -17,6 +17,7 @@ #include "storageview.h" #include "definitions.h" +#include "digitlabelhelper.h" #include #include @@ -59,12 +60,13 @@ QVariant StorageView::headerData(int section, Qt::Orientation o, int role) const return QVariant(); if (o == Qt::Vertical) { - return section * 4; + return DigitLabelHelper::format_value(section * 4, this->is_hex); } return QVariant(); } -Qt::ItemFlags StorageView::flags(const QModelIndex &i) const { +Qt::ItemFlags StorageView::flags(const QModelIndex &i) const +{ (void)i; return Qt::ItemIsEnabled; } @@ -75,3 +77,12 @@ void StorageView::set_data(const QVector> &data) this->d = data; endResetModel(); } + +void StorageView::set_hex_display(bool hex) +{ + if (this->is_hex != hex) { + beginResetModel(); + this->is_hex = hex; + endResetModel(); + } +} diff --git a/gui/storageview.h b/gui/storageview.h index 4956f23..0518d8f 100644 --- a/gui/storageview.h +++ b/gui/storageview.h @@ -72,11 +72,18 @@ class StorageView : public QAbstractTableModel */ void set_data(const QVector> &data); + public slots: + void set_hex_display(bool hex); + private: /** * The number of rows in this table. */ int r; + /** + * Whether or not the headers should be displayed in hex. + */ + bool is_hex = true; /** * The data this table displays. */ -- cgit v1.2.3 From 6382d595cf947eb54249ff5fea20d8eb073ef3c1 Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 26 Apr 2025 15:11:19 -0400 Subject: Move digit formatter to util.h --- gui/digitlabel.cc | 4 ++-- gui/digitlabeldelegate.cc | 4 ++-- gui/digitlabelhelper.h | 43 ------------------------------------------- gui/storageview.cc | 4 ++-- gui/util.cc | 8 ++++++++ gui/util.h | 23 +++++++++++++++++++++-- 6 files changed, 35 insertions(+), 51 deletions(-) delete mode 100644 gui/digitlabelhelper.h (limited to 'gui/storageview.cc') diff --git a/gui/digitlabel.cc b/gui/digitlabel.cc index a24a1e1..f77c1fa 100644 --- a/gui/digitlabel.cc +++ b/gui/digitlabel.cc @@ -16,7 +16,7 @@ // along with this program. If not, see . #include "digitlabel.h" -#include "digitlabelhelper.h" +#include "util.h" #include "gui.h" DigitLabel::DigitLabel(QWidget *parent) : QLabel(parent) { setText(QString()); } @@ -43,6 +43,6 @@ void DigitLabel::on_hex_toggle(bool is_hex) void DigitLabel::update_display() { QString t; - t = DigitLabelHelper::format_value(this->v, this->is_hex, this->is_cleared); + t = format_toggled_value(this->v, this->is_hex, this->is_cleared); setText(t); } diff --git a/gui/digitlabeldelegate.cc b/gui/digitlabeldelegate.cc index bf4b175..430946c 100644 --- a/gui/digitlabeldelegate.cc +++ b/gui/digitlabeldelegate.cc @@ -16,7 +16,7 @@ // along with this program. If not, see . #include "digitlabeldelegate.h" -#include "digitlabelhelper.h" +#include "util.h" #include void DigitLabelDelegate::set_hex_display(bool hex) @@ -39,7 +39,7 @@ void DigitLabelDelegate::paint( QStyle *s; v = index.data(Qt::DisplayRole).toInt(); - t = DigitLabelHelper::format_value(v, this->is_hex); + t = format_toggled_value(v, this->is_hex); o = option; initStyleOption(&o, index); diff --git a/gui/digitlabelhelper.h b/gui/digitlabelhelper.h deleted file mode 100644 index 744bf0f..0000000 --- a/gui/digitlabelhelper.h +++ /dev/null @@ -1,43 +0,0 @@ -// Simulator for the RISC-V[ECTOR] mini-ISA -// Copyright (C) 2025 Siddarth Suresh -// Copyright (C) 2025 bdunahu - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -#ifndef DIGITLABELHELPER_H -#define DIGITLABELHELPER_H - -#include - -class DigitLabelHelper -{ - public: - /** - * Contains the main formatting logic used to format integers. Uses 2's - * complement for hexadecimal numbers. - * @param the value to be formated - * @param if the value should be displayed in hex. If false, displays in - * decimal. - * @param if the value should display. - * @return a string respecting the above parameters. - */ - static QString format_value(int value, bool is_hex, bool is_cleared = false) - { - if (is_cleared) - return QString(); - return is_hex ? QString::asprintf("%08X", value) : QString::number(value); - } -}; - -#endif // DIGITLABELHELPER_H diff --git a/gui/storageview.cc b/gui/storageview.cc index 22baca0..2f444a9 100644 --- a/gui/storageview.cc +++ b/gui/storageview.cc @@ -17,7 +17,7 @@ #include "storageview.h" #include "definitions.h" -#include "digitlabelhelper.h" +#include "util.h" #include #include @@ -60,7 +60,7 @@ QVariant StorageView::headerData(int section, Qt::Orientation o, int role) const return QVariant(); if (o == Qt::Vertical) { - return DigitLabelHelper::format_value(section * 4, this->is_hex); + return format_toggled_value(section * 4, this->is_hex); } return QVariant(); } diff --git a/gui/util.cc b/gui/util.cc index ee75e56..72c0d87 100644 --- a/gui/util.cc +++ b/gui/util.cc @@ -1,4 +1,5 @@ #include "util.h" +#include int cache_size_mapper(int total_levels, int level) { @@ -14,3 +15,10 @@ int cache_size_mapper(int total_levels, int level) return r; } + +QString format_toggled_value(int value, bool is_hex, bool is_cleared) +{ + if (is_cleared) + return QString(); + return is_hex ? QString::asprintf("%X", value) : QString::number(value); +} diff --git a/gui/util.h b/gui/util.h index 87c33f6..8e9d308 100644 --- a/gui/util.h +++ b/gui/util.h @@ -1,8 +1,27 @@ +#ifndef UTIL_H +#define UTIL_H + +#include + /** - * Given `total_levels', returns an integer between 4 and 12 which is a linear map of `level' onto `total_levels'. - * This is used for generating cache sizes given a number of levels. + * Given `total_levels', returns an integer between 4 and 12 which is a linear + * map of `level' onto `total_levels'. This is used for generating cache sizes + * given a number of levels. * @param the total number of cache levels, zero-indexed. * @param a numberedcache level, zero-indexed. * @return an integer between 4-12, linearly scaled with level. */ int cache_size_mapper(int total_levels, int level); + +/** + * Contains the main formatting logic used to format integers. Uses 2's + * complement for hexadecimal numbers. + * @param the value to be formated + * @param if the value should be displayed in hex. If false, displays in + * decimal. + * @param if the value should display. + * @return a string respecting the above parameters. + */ +QString format_toggled_value(int value, bool is_hex, bool is_cleared = false); + +#endif // UTIL_H -- cgit v1.2.3