diff --git a/src/plugins/debugger/peripheralregisterhandler.cpp b/src/plugins/debugger/peripheralregisterhandler.cpp index f94e11e6235..100212c149d 100644 --- a/src/plugins/debugger/peripheralregisterhandler.cpp +++ b/src/plugins/debugger/peripheralregisterhandler.cpp @@ -6,6 +6,7 @@ #include "debuggeractions.h" #include "debuggercore.h" #include "debuggertr.h" +#include "registerhandler.h" #include @@ -448,89 +449,6 @@ void PeripheralRegisterItem::triggerChange() 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(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(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(DebuggerEngine *engine) @@ -732,8 +650,7 @@ QList PeripheralRegisterHandler::activeRegisters() const QVariant PeripheralRegisterHandler::data(const QModelIndex &idx, int role) const { if (role == BaseTreeView::ItemDelegateRole) { - return QVariant::fromValue(static_cast( - new PeripheralRegisterDelegate)); + return QVariant::fromValue(createRegisterDelegate(PeripheralRegisterValueColumn)); } return PeripheralRegisterModel::data(idx, role); } diff --git a/src/plugins/debugger/registerhandler.cpp b/src/plugins/debugger/registerhandler.cpp index 348888f29b8..79d82d8501c 100644 --- a/src/plugins/debugger/registerhandler.cpp +++ b/src/plugins/debugger/registerhandler.cpp @@ -45,12 +45,12 @@ enum RegisterDataRole class RegisterDelegate : public QItemDelegate { public: - RegisterDelegate() = default; + RegisterDelegate(int column) : m_column(column) {} 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); lineEdit->setAlignment(Qt::AlignLeft); lineEdit->setFrame(false); @@ -67,9 +67,9 @@ public: } 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(editor); QTC_ASSERT(lineEdit, return); model->setData(index, lineEdit->text(), Qt::EditRole); @@ -77,16 +77,16 @@ public: } void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, - const QModelIndex &) const override + const QModelIndex &) const override { editor->setGeometry(option.rect); } void paint(QPainter *painter, const QStyleOptionViewItem &option, - const QModelIndex &index) const override + const QModelIndex &index) const override { - if (index.column() == RegisterValueColumn) { - const bool paintRed = index.data(RegisterChangedRole).toBool(); + if (index.column() == m_column) { + const bool paintRed = index.data(Qt::UserRole).toBool(); QPen oldPen = painter->pen(); const QColor lightColor(140, 140, 140); if (paintRed) @@ -122,6 +122,8 @@ public: 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 { if (role == BaseTreeView::ItemDelegateRole) - return QVariant::fromValue(static_cast(new RegisterDelegate)); + return QVariant::fromValue(createRegisterDelegate(RegisterValueColumn)); return RegisterModel::data(idx, role); } @@ -886,4 +888,9 @@ Qt::ItemFlags RegisterEditItem::flags(int column) const return f; } +QAbstractItemDelegate *createRegisterDelegate(int column) +{ + return new RegisterDelegate(column); +} + } // Debugger::Internal diff --git a/src/plugins/debugger/registerhandler.h b/src/plugins/debugger/registerhandler.h index 8a02fd2eaa1..b8dd7873ca9 100644 --- a/src/plugins/debugger/registerhandler.h +++ b/src/plugins/debugger/registerhandler.h @@ -8,6 +8,10 @@ #include #include +QT_BEGIN_NAMESPACE +class QAbstractItemDelegate; +QT_END_NAMESPACE + namespace Utils { class ItemViewEvent; } namespace Debugger::Internal { @@ -111,4 +115,6 @@ private: DebuggerEngine * const m_engine; }; +QAbstractItemDelegate *createRegisterDelegate(int column); + } // Debugger::Internal