Move radix selection logic to separate widget
ADD
src/radixselectorwidget.cpp
+#include "radixselectorwidget.h"
+#include "ui_radixselectorwidget.h"
+
+#include <QtGlobal>
+
+RadixSelectorWidget::RadixSelectorWidget(QWidget* parent) : QWidget(parent), m_ui(new Ui::RadixSelectorWidget) {
+ m_ui->setupUi(this);
+ setupRadixComboBox();
+}
+
+RadixSelectorWidget::~RadixSelectorWidget() {
+ delete m_ui;
+}
+
+void RadixSelectorWidget::setRadix(Radix r) {
+ for (int i = 0; i < m_ui->displayType->count(); i++) {
+ const Radix itemRadix = qvariant_cast<Radix>(m_ui->displayType->itemData(i));
+ if (r == itemRadix) {
+ m_ui->displayType->setCurrentIndex(i);
+ return;
+ }
+ }
+ Q_UNREACHABLE();
+}
+
+void RadixSelectorWidget::setupRadixComboBox() {
+ for (const auto& rt : s_radixName) {
+ m_ui->displayType->addItem(rt.second, QVariant::fromValue(rt.first));
+ }
+
+ connect(m_ui->displayType, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index) {
+ const Radix r = qvariant_cast<Radix>(m_ui->displayType->itemData(index));
+ emit radixChanged(r);
+ });
+}

ADD
src/radixselectorwidget.h
+#ifndef RADIXSELECTORWIDGET_H
+#define RADIXSELECTORWIDGET_H
+
+#include <QWidget>
+
+#include "radix.h"
+
+namespace Ui {
+class RadixSelectorWidget;
+}
+
+class RadixSelectorWidget : public QWidget {
+ Q_OBJECT
+
+public:
+ explicit RadixSelectorWidget(QWidget* parent = nullptr);
+ ~RadixSelectorWidget();
+ void setupRadixComboBox();
+ void setRadix(Radix r);
+
+signals:
+ void radixChanged(Radix r);
+
+private:
+ Ui::RadixSelectorWidget* m_ui;
+};
+
+#endif // RADIXSELECTORWIDGET_H

ADD
src/radixselectorwidget.ui
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>RadixSelectorWidget</class>
+ <widget class="QWidget" name="RadixSelectorWidget">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>199</width>
+ <height>27</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Form</string>
+ </property>
+ <layout class="QGridLayout" name="gridLayout">
+ <property name="leftMargin">
+ <number>0</number>
+ </property>
+ <property name="topMargin">
+ <number>0</number>
+ </property>
+ <property name="rightMargin">
+ <number>0</number>
+ </property>
+ <property name="bottomMargin">
+ <number>0</number>
+ </property>
+ <item row="0" column="0">
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QLabel" name="label">
+ <property name="text">
+ <string>Display type:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QComboBox" name="displayType"/>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

MODIFY
src/registermodel.cpp
src/registermodel.cpp
@@ -10,7 +10,7 @@ RegisterModel::RegisterModel(ProcessorHandler& handler, QObject* parent)
std::vector<uint32_t> RegisterModel::gatherRegisterValues() {
std::vector<uint32_t> vals;
for (int i = 0; i < rowCount(); i++) {
- vals.push_back(valueData(i).toUInt());
+ vals.push_back(m_handler.getRegisterValue(i));
}
return vals;
}

MODIFY
src/registerwidget.cpp
src/registerwidget.cpp
#include <QScrollBar>
+#include "radixselectorwidget.h"
+
RegisterWidget::RegisterWidget(QWidget* parent) : QWidget(parent), m_ui(new Ui::RegisterWidget) {
m_ui->setupUi(this);
- setupRadixComboBox();
}
RegisterWidget::~RegisterWidget() {
@@ -17,35 +18,14 @@ void RegisterWidget::setHandler(ProcessorHandler* handler) {
updateModel();
}
-void RegisterWidget::setupRadixComboBox() {
- for (const auto& rt : s_radixName) {
- m_ui->displayMode->addItem(rt.second, QVariant::fromValue(rt.first));
- }
-
- connect(m_ui->displayMode, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index) {
- Radix r = qvariant_cast<Radix>(m_ui->displayMode->itemData(index));
- m_registerModel->setRadix(r);
- });
-}
-
-void RegisterWidget::updateRadixComboBoxIndex() {
- // Synchronize the index of the radix selection combo box with the radix currently set in the model
- for (int i = 0; i < m_ui->displayMode->count(); i++) {
- const Radix itemRadix = qvariant_cast<Radix>(m_ui->displayMode->itemData(i));
- if (m_registerModel->getRadix() == itemRadix) {
- m_ui->displayMode->setCurrentIndex(i);
- return;
- }
- }
-}
-
void RegisterWidget::updateModel() {
auto* oldModel = m_registerModel;
m_registerModel = new RegisterModel(*m_handler, this);
m_ui->registerView->setModel(m_registerModel);
- updateRadixComboBoxIndex();
+ m_ui->radixSelector->setRadix(m_registerModel->getRadix());
connect(m_registerModel, &RegisterModel::registerChanged, this, &RegisterWidget::setRegisterviewCenterIndex);
+ connect(m_ui->radixSelector, &RadixSelectorWidget::radixChanged, m_registerModel, &RegisterModel::setRadix);
m_ui->registerView->horizontalHeader()->setSectionResizeMode(RegisterModel::Name, QHeaderView::ResizeToContents);
m_ui->registerView->horizontalHeader()->setSectionResizeMode(RegisterModel::Alias, QHeaderView::ResizeToContents);

MODIFY
src/registerwidget.h
src/registerwidget.h
@@ -27,9 +27,6 @@ public slots:
void setRegisterviewCenterIndex(unsigned index);
private:
- void setupRadixComboBox();
- void updateRadixComboBoxIndex();
-
Ui::RegisterWidget* m_ui;
ProcessorHandler* m_handler = nullptr;
};

MODIFY
src/registerwidget.ui
src/registerwidget.ui
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
- <widget class="QLabel" name="label">
- <property name="text">
- <string>Display type:</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QComboBox" name="displayMode"/>
+ <widget class="RadixSelectorWidget" name="radixSelector" native="true"/>
</item>
<item>
<spacer name="horizontalSpacer">
</item>
</layout>
</widget>
+ <customwidgets>
+ <customwidget>
+ <class>RadixSelectorWidget</class>
+ <extends>QWidget</extends>
+ <header>radixselectorwidget.h</header>
+ <container>1</container>
+ </customwidget>
+ </customwidgets>
<resources/>
<connections/>
</ui>

Move radix selection logic to separate widget