Debugger: Introduce createRegisterDelegate() helper

Merge RegisterDelegate with PeripheralRegisterDelegate
and use it in RegisterHandler and PeripheralRegisterHandler.

Change-Id: I55088bafa5701ef828095c9889d333d282848ab4
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-07-18 12:35:24 +02:00
parent 43a2974baa
commit cd7b635910
3 changed files with 25 additions and 95 deletions

View File

@@ -6,6 +6,7 @@
#include "debuggeractions.h" #include "debuggeractions.h"
#include "debuggercore.h" #include "debuggercore.h"
#include "debuggertr.h" #include "debuggertr.h"
#include "registerhandler.h"
#include <utils/basetreeview.h> #include <utils/basetreeview.h>
@@ -448,89 +449,6 @@ void PeripheralRegisterItem::triggerChange()
m_reg.currentValue.v); m_reg.currentValue.v);
} }
// PeripheralRegisterDelegate
class PeripheralRegisterDelegate final : public QItemDelegate
{
public:
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &index) const final
{
if (index.column() == PeripheralRegisterValueColumn) {
const auto lineEdit = new QLineEdit(parent);
lineEdit->setAlignment(Qt::AlignLeft);
lineEdit->setFrame(false);
return lineEdit;
}
return nullptr;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const final
{
const auto lineEdit = qobject_cast<QLineEdit *>(editor);
QTC_ASSERT(lineEdit, return);
lineEdit->setText(index.data(Qt::EditRole).toString());
}
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const final
{
if (index.column() == PeripheralRegisterValueColumn) {
const auto lineEdit = qobject_cast<QLineEdit *>(editor);
QTC_ASSERT(lineEdit, return);
model->setData(index, lineEdit->text(), Qt::EditRole);
}
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &) const final
{
editor->setGeometry(option.rect);
}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const final
{
if (index.column() == PeripheralRegisterValueColumn) {
const bool paintRed = index.data(PeripheralRegisterChangedRole).toBool();
const QPen oldPen = painter->pen();
const QColor lightColor(140, 140, 140);
if (paintRed)
painter->setPen(QColor(200, 0, 0));
else
painter->setPen(lightColor);
// FIXME: performance? this changes only on real font changes.
const QFontMetrics fm(option.font);
const int charWidth = qMax(fm.horizontalAdvance('x'),
fm.horizontalAdvance('0'));
const QString str = index.data(Qt::DisplayRole).toString();
int x = option.rect.x();
bool light = !paintRed;
for (int i = 0; i < str.size(); ++i) {
const QChar c = str.at(i);
const int uc = c.unicode();
if (light && (uc != 'x' && uc != '0')) {
light = false;
painter->setPen(oldPen.color());
}
if (uc == ' ') {
light = true;
painter->setPen(lightColor);
} else {
QRect r = option.rect;
r.setX(x);
r.setWidth(charWidth);
painter->drawText(r, Qt::AlignHCenter, c);
}
x += charWidth;
}
painter->setPen(oldPen);
} else {
QItemDelegate::paint(painter, option, index);
}
}
};
// PeripheralRegisterHandler // PeripheralRegisterHandler
PeripheralRegisterHandler::PeripheralRegisterHandler(DebuggerEngine *engine) PeripheralRegisterHandler::PeripheralRegisterHandler(DebuggerEngine *engine)
@@ -732,8 +650,7 @@ QList<quint64> PeripheralRegisterHandler::activeRegisters() const
QVariant PeripheralRegisterHandler::data(const QModelIndex &idx, int role) const QVariant PeripheralRegisterHandler::data(const QModelIndex &idx, int role) const
{ {
if (role == BaseTreeView::ItemDelegateRole) { if (role == BaseTreeView::ItemDelegateRole) {
return QVariant::fromValue(static_cast<QAbstractItemDelegate *>( return QVariant::fromValue(createRegisterDelegate(PeripheralRegisterValueColumn));
new PeripheralRegisterDelegate));
} }
return PeripheralRegisterModel::data(idx, role); return PeripheralRegisterModel::data(idx, role);
} }

View File

@@ -45,12 +45,12 @@ enum RegisterDataRole
class RegisterDelegate : public QItemDelegate class RegisterDelegate : public QItemDelegate
{ {
public: public:
RegisterDelegate() = default; RegisterDelegate(int column) : m_column(column) {}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &index) const override const QModelIndex &index) const override
{ {
if (index.column() == RegisterValueColumn) { if (index.column() == m_column) {
auto lineEdit = new QLineEdit(parent); auto lineEdit = new QLineEdit(parent);
lineEdit->setAlignment(Qt::AlignLeft); lineEdit->setAlignment(Qt::AlignLeft);
lineEdit->setFrame(false); lineEdit->setFrame(false);
@@ -69,7 +69,7 @@ public:
void setModelData(QWidget *editor, QAbstractItemModel *model, void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const override const QModelIndex &index) const override
{ {
if (index.column() == RegisterValueColumn) { if (index.column() == m_column) {
auto lineEdit = qobject_cast<QLineEdit *>(editor); auto lineEdit = qobject_cast<QLineEdit *>(editor);
QTC_ASSERT(lineEdit, return); QTC_ASSERT(lineEdit, return);
model->setData(index, lineEdit->text(), Qt::EditRole); model->setData(index, lineEdit->text(), Qt::EditRole);
@@ -85,8 +85,8 @@ public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override const QModelIndex &index) const override
{ {
if (index.column() == RegisterValueColumn) { if (index.column() == m_column) {
const bool paintRed = index.data(RegisterChangedRole).toBool(); const bool paintRed = index.data(Qt::UserRole).toBool();
QPen oldPen = painter->pen(); QPen oldPen = painter->pen();
const QColor lightColor(140, 140, 140); const QColor lightColor(140, 140, 140);
if (paintRed) if (paintRed)
@@ -122,6 +122,8 @@ public:
QItemDelegate::paint(painter, option, index); QItemDelegate::paint(painter, option, index);
} }
} }
const int m_column;
}; };
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
@@ -713,7 +715,7 @@ RegisterMap RegisterHandler::registerMap() const
QVariant RegisterHandler::data(const QModelIndex &idx, int role) const QVariant RegisterHandler::data(const QModelIndex &idx, int role) const
{ {
if (role == BaseTreeView::ItemDelegateRole) if (role == BaseTreeView::ItemDelegateRole)
return QVariant::fromValue(static_cast<QAbstractItemDelegate *>(new RegisterDelegate)); return QVariant::fromValue(createRegisterDelegate(RegisterValueColumn));
return RegisterModel::data(idx, role); return RegisterModel::data(idx, role);
} }
@@ -886,4 +888,9 @@ Qt::ItemFlags RegisterEditItem::flags(int column) const
return f; return f;
} }
QAbstractItemDelegate *createRegisterDelegate(int column)
{
return new RegisterDelegate(column);
}
} // Debugger::Internal } // Debugger::Internal

View File

@@ -8,6 +8,10 @@
#include <QHash> #include <QHash>
#include <QSet> #include <QSet>
QT_BEGIN_NAMESPACE
class QAbstractItemDelegate;
QT_END_NAMESPACE
namespace Utils { class ItemViewEvent; } namespace Utils { class ItemViewEvent; }
namespace Debugger::Internal { namespace Debugger::Internal {
@@ -111,4 +115,6 @@ private:
DebuggerEngine * const m_engine; DebuggerEngine * const m_engine;
}; };
QAbstractItemDelegate *createRegisterDelegate(int column);
} // Debugger::Internal } // Debugger::Internal