rockbox/utils/regtools/qeditor/regtab.cpp
Amaury Pouly 970c2482dd qeditor: rework modified indicator, register tab names depend on content
Because Qt doesn't support QObject multiple inherance, it is a bit tricky
to have a base class which interact with the UI. The register tab name
now display:
- file dump name (for dumps)
- hwstub device path (for hwstub)
And the register editor display the filename
Change-Id: If2579992098c02627c67d560c824f1668e73bc45
Reviewed-on: http://gerrit.rockbox.org/979
Reviewed-by: Amaury Pouly <amaury.pouly@gmail.com>
2014-09-19 10:58:33 +02:00

411 lines
11 KiB
C++

#include "regtab.h"
#include <QSizePolicy>
#include <QStringBuilder>
#include <QFileDialog>
#include <QDebug>
#include <QStyle>
#include <QMessageBox>
#include "backend.h"
#include "analyser.h"
#include "regdisplaypanel.h"
namespace
{
enum
{
RegTreeDevType = QTreeWidgetItem::UserType,
RegTreeRegType,
RegTreeSocType
};
class SocTreeItem : public QTreeWidgetItem
{
public:
SocTreeItem(const QString& string, const SocRef& ref)
:QTreeWidgetItem(QStringList(string), RegTreeSocType), m_ref(ref) {}
const SocRef& GetRef() { return m_ref; }
private:
SocRef m_ref;
};
class DevTreeItem : public QTreeWidgetItem
{
public:
DevTreeItem(const QString& string, const SocDevRef& ref)
:QTreeWidgetItem(QStringList(string), RegTreeDevType), m_ref(ref) {}
const SocDevRef& GetRef() { return m_ref; }
private:
SocDevRef m_ref;
};
class RegTreeItem : public QTreeWidgetItem
{
public:
RegTreeItem(const QString& string, const SocRegRef& ref)
:QTreeWidgetItem(QStringList(string), RegTreeRegType), m_ref(ref) {}
const SocRegRef& GetRef() { return m_ref; }
private:
SocRegRef m_ref;
};
}
/**
* EmptyRegTabPanel
*/
EmptyRegTabPanel::EmptyRegTabPanel(QWidget *parent)
:QWidget(parent)
{
QVBoxLayout *l = new QVBoxLayout;
l->addStretch();
setLayout(l);
}
void EmptyRegTabPanel::AllowWrite(bool en)
{
Q_UNUSED(en);
}
QWidget *EmptyRegTabPanel::GetWidget()
{
return this;
}
/**
* RegTab
*/
RegTab::RegTab(Backend *backend, QWidget *parent)
:QSplitter(parent), m_backend(backend)
{
QWidget *left = new QWidget;
this->addWidget(left);
this->setStretchFactor(0, 1);
QVBoxLayout *left_layout = new QVBoxLayout;
left->setLayout(left_layout);
QGroupBox *top_group = new QGroupBox("SOC selection");
QVBoxLayout *top_group_layout = new QVBoxLayout;
m_soc_selector = new QComboBox;
top_group_layout->addWidget(m_soc_selector);
top_group->setLayout(top_group_layout);
m_reg_tree = new QTreeWidget();
m_reg_tree->setColumnCount(1);
m_reg_tree->setHeaderLabel(QString("Name"));
m_analysers_list = new QListWidget;
m_type_selector = new QTabWidget;
m_type_selector->addTab(m_reg_tree, "Registers");
m_type_selector->addTab(m_analysers_list, "Analyzers");
m_type_selector->setTabPosition(QTabWidget::West);
left_layout->addWidget(top_group);
left_layout->addWidget(m_type_selector);
m_right_panel = new QVBoxLayout;
QGroupBox *data_sel_group = new QGroupBox("Data selection");
QHBoxLayout *data_sel_layout = new QHBoxLayout;
m_backend_selector = new BackendSelector(m_backend, this);
m_readonly_check = new QCheckBox("Read-only");
m_readonly_check->setCheckState(Qt::Checked);
m_data_soc_label = new QLabel;
m_dump = new QPushButton("Dump", this);
m_dump->setIcon(QIcon::fromTheme("system-run"));
m_data_sel_reload = new QPushButton(this);
m_data_sel_reload->setIcon(QIcon::fromTheme("view-refresh"));
m_data_sel_reload->setToolTip("Reload data");
data_sel_layout->addWidget(m_backend_selector);
data_sel_layout->addWidget(m_readonly_check);
data_sel_layout->addWidget(m_data_soc_label);
data_sel_layout->addWidget(m_dump);
data_sel_layout->addWidget(m_data_sel_reload);
data_sel_group->setLayout(data_sel_layout);
m_data_soc_label->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
m_right_panel->addWidget(data_sel_group, 0);
m_right_content = 0;
SetPanel(new EmptyRegTabPanel);
QWidget *w = new QWidget;
w->setLayout(m_right_panel);
this->addWidget(w);
this->setStretchFactor(1, 2);
m_io_backend = m_backend_selector->GetBackend();
connect(m_soc_selector, SIGNAL(currentIndexChanged(int)),
this, SLOT(OnSocChanged(int)));
connect(m_backend, SIGNAL(OnSocListChanged()), this, SLOT(OnSocListChanged()));
connect(m_reg_tree, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
this, SLOT(OnRegItemClicked(QTreeWidgetItem*, int)));
connect(m_data_soc_label, SIGNAL(linkActivated(const QString&)), this,
SLOT(OnDataSocActivated(const QString&)));
connect(m_analysers_list, SIGNAL(itemClicked(QListWidgetItem *)),
this, SLOT(OnAnalyserClicked(QListWidgetItem *)));
connect(m_backend_selector, SIGNAL(OnSelect(IoBackend *)),
this, SLOT(OnBackendSelect(IoBackend *)));
connect(m_readonly_check, SIGNAL(clicked(bool)), this, SLOT(OnReadOnlyClicked(bool)));
connect(m_dump, SIGNAL(clicked(bool)), this, SLOT(OnDumpRegs(bool)));
connect(m_data_sel_reload, SIGNAL(clicked(bool)), this, SLOT(OnBackendReload(bool)));
connect(m_type_selector, SIGNAL(currentChanged(int)), this, SLOT(OnTypeChanged(int)));
OnSocListChanged();
SetDataSocName("");
}
QWidget *RegTab::GetWidget()
{
return this;
}
RegTab::~RegTab()
{
/* backend will be deleted by backend selector */
}
bool RegTab::Quit()
{
return true;
}
void RegTab::SetDataSocName(const QString& socname)
{
if(socname.size() != 0)
{
m_data_soc_label->setText("<a href=\"" + socname + "\">" + socname + "</a>");
m_data_soc_label->setTextFormat(Qt::RichText);
m_data_soc_label->show();
}
else
{
m_data_soc_label->setText("");
m_data_soc_label->hide();
}
}
void RegTab::OnDataSocActivated(const QString& str)
{
int index = m_soc_selector->findText(str);
if(index != -1)
m_soc_selector->setCurrentIndex(index);
}
void RegTab::UpdateTabName()
{
/* do it the ugly way: try to cast to the different possible backends */
FileIoBackend *file = dynamic_cast< FileIoBackend* >(m_io_backend);
#ifdef HAVE_HWSTUB
HWStubIoBackend *hwstub = dynamic_cast< HWStubIoBackend* >(m_io_backend);
#endif
if(file)
{
QFileInfo info(file->GetFileName());
SetTabName(info.fileName());
}
#ifdef HAVE_HWSTUB
else if(hwstub)
{
HWStubDevice *dev = hwstub->GetDevice();
SetTabName(QString("HWStub %1.%2").arg(dev->GetBusNumber())
.arg(dev->GetDevAddress()));
}
#endif
else
{
SetTabName("Register Tab");
}
}
void RegTab::OnBackendSelect(IoBackend *backend)
{
m_io_backend = backend;
SetReadOnlyIndicator();
SetDataSocName(m_io_backend->GetSocName());
OnDataSocActivated(m_io_backend->GetSocName());
OnDataChanged();
UpdateTabName();
}
void RegTab::SetReadOnlyIndicator()
{
if(m_io_backend->IsReadOnly())
m_readonly_check->setCheckState(Qt::Checked);
}
void RegTab::OnDataChanged()
{
OnRegItemClicked(m_reg_tree->currentItem(), 0);
}
void RegTab::OnRegItemClicked(QTreeWidgetItem *current, int col)
{
Q_UNUSED(col);
if(current == 0)
return;
if(current->type() == RegTreeSocType)
{
SocTreeItem *item = dynamic_cast< SocTreeItem * >(current);
DisplaySoc(item->GetRef());
}
if(current->type() == RegTreeRegType)
{
RegTreeItem *item = dynamic_cast< RegTreeItem * >(current);
DisplayRegister(item->GetRef());
}
else if(current->type() == RegTreeDevType)
{
DevTreeItem *item = dynamic_cast< DevTreeItem * >(current);
DisplayDevice(item->GetRef());
}
}
void RegTab::OnAnalyserClicked(QListWidgetItem *current)
{
if(current == 0)
return;
AnalyserFactory *ana = AnalyserFactory::GetAnalyserByName(current->text());
SetPanel(ana->Create(m_cur_soc, m_io_backend));
}
void RegTab::DisplayRegister(const SocRegRef& ref)
{
SetPanel(new RegDisplayPanel(this, m_io_backend, ref));
}
void RegTab::DisplayDevice(const SocDevRef& ref)
{
SetPanel(new DevDisplayPanel(this, ref));
}
void RegTab::DisplaySoc(const SocRef& ref)
{
SetPanel(new SocDisplayPanel(this, ref));
}
void RegTab::SetPanel(RegTabPanel *panel)
{
delete m_right_content;
m_right_content = panel;
m_right_content->AllowWrite(m_readonly_check->checkState() == Qt::Unchecked);
m_right_panel->addWidget(m_right_content->GetWidget(), 1);
}
void RegTab::OnSocListChanged()
{
m_soc_selector->clear();
QList< SocRef > socs = m_backend->GetSocList();
for(int i = 0; i < socs.size(); i++)
{
QVariant v;
v.setValue(socs[i]);
m_soc_selector->addItem(QString::fromStdString(socs[i].GetSoc().name), v);
}
}
void RegTab::FillDevSubTree(QTreeWidgetItem *_item)
{
DevTreeItem *item = dynamic_cast< DevTreeItem* >(_item);
const soc_dev_t& dev = item->GetRef().GetDev();
for(size_t i = 0; i < dev.reg.size(); i++)
{
const soc_reg_t& reg = dev.reg[i];
for(size_t j = 0; j < reg.addr.size(); j++)
{
RegTreeItem *reg_item = new RegTreeItem(reg.addr[j].name.c_str(),
SocRegRef(item->GetRef(), i, j));
item->addChild(reg_item);
}
}
}
void RegTab::FillSocSubTree(QTreeWidgetItem *_item)
{
SocTreeItem *item = dynamic_cast< SocTreeItem* >(_item);
const soc_t& soc = item->GetRef().GetSoc();
for(size_t i = 0; i < soc.dev.size(); i++)
{
const soc_dev_t& dev = soc.dev[i];
for(size_t j = 0; j < dev.addr.size(); j++)
{
DevTreeItem *dev_item = new DevTreeItem(dev.addr[j].name.c_str(),
SocDevRef(m_cur_soc, i, j));
FillDevSubTree(dev_item);
item->addChild(dev_item);
}
}
}
void RegTab::FillRegTree()
{
SocTreeItem *soc_item = new SocTreeItem(m_cur_soc.GetSoc().name.c_str(),
m_cur_soc);
FillSocSubTree(soc_item);
m_reg_tree->addTopLevelItem(soc_item);
m_reg_tree->expandItem(soc_item);
}
void RegTab::FillAnalyserList()
{
m_analysers_list->clear();
m_analysers_list->addItems(AnalyserFactory::GetAnalysersForSoc(m_cur_soc.GetSoc().name.c_str()));
}
void RegTab::OnSocChanged(int index)
{
if(index == -1)
return;
m_reg_tree->clear();
m_cur_soc = m_soc_selector->itemData(index).value< SocRef >();
FillRegTree();
FillAnalyserList();
}
void RegTab::OnReadOnlyClicked(bool checked)
{
if(m_io_backend->IsReadOnly())
return SetReadOnlyIndicator();
m_right_content->AllowWrite(!checked);
}
void RegTab::OnDumpRegs(bool c)
{
Q_UNUSED(c);
QFileDialog *fd = new QFileDialog(this);
fd->setAcceptMode(QFileDialog::AcceptSave);
fd->setFilter("Textual files (*.txt);;All files (*)");
fd->setDirectory(Settings::Get()->value("loaddatadir", QDir::currentPath()).toString());
if(!fd->exec())
return;
QStringList filenames = fd->selectedFiles();
Settings::Get()->setValue("loaddatadir", fd->directory().absolutePath());
BackendHelper bh(m_io_backend, m_cur_soc);
if(!bh.DumpAllRegisters(filenames[0]))
{
QMessageBox::warning(this, "The register dump was not saved",
"There was an error when dumping the registers");
}
}
void RegTab::OnBackendReload(bool c)
{
Q_UNUSED(c);
m_io_backend->Reload();
OnDataChanged();
}
void RegTab::OnTypeChanged(int index)
{
if(index == -1)
return;
if(index == 0) /* registers */
OnRegItemClicked(m_reg_tree->currentItem(), 0);
else if(index == 1) /* analysers */
OnAnalyserClicked(m_analysers_list->currentItem());
}