mirror of
https://github.com/tnodir/fort
synced 2024-11-15 08:35:08 +00:00
UI: Refactor TreeItemDelegate
This commit is contained in:
parent
1401acfb74
commit
d9d20790f1
@ -4,22 +4,13 @@
|
||||
|
||||
TreeItemDelegate::TreeItemDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
|
||||
|
||||
void TreeItemDelegate::setModel(TableItemModel *model)
|
||||
{
|
||||
m_model = model;
|
||||
Q_ASSERT(m_model);
|
||||
}
|
||||
|
||||
void TreeItemDelegate::paint(
|
||||
QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
QStyleOptionViewItem opt(option);
|
||||
if (index.parent().isValid() && opt.rect.left() > 0) {
|
||||
opt.rect.setLeft(16);
|
||||
}
|
||||
|
||||
if (model()->flagIsEnabled(index) != Qt::ItemIsEnabled) {
|
||||
opt.state &= ~QStyle::State_Enabled;
|
||||
if (index.parent().isValid()) {
|
||||
setOptionEnabled(opt, index);
|
||||
}
|
||||
|
||||
QStyledItemDelegate::paint(painter, opt, index);
|
||||
@ -30,3 +21,11 @@ QSize TreeItemDelegate::sizeHint(
|
||||
{
|
||||
return QSize(100, 24);
|
||||
}
|
||||
|
||||
void TreeItemDelegate::setOptionEnabled(QStyleOptionViewItem &opt, const QModelIndex &index) const
|
||||
{
|
||||
const bool isIndexEnabled = index.data(TableItemModel::EnabledRole).toBool();
|
||||
if (!isIndexEnabled) {
|
||||
opt.state &= ~QStyle::State_Enabled;
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,6 @@
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
class TableItemModel;
|
||||
|
||||
class TreeItemDelegate : public QStyledItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -12,15 +10,12 @@ class TreeItemDelegate : public QStyledItemDelegate
|
||||
public:
|
||||
explicit TreeItemDelegate(QObject *parent = nullptr);
|
||||
|
||||
TableItemModel *model() const { return m_model; }
|
||||
void setModel(TableItemModel *model);
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const override;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
|
||||
private:
|
||||
TableItemModel *m_model = nullptr;
|
||||
void setOptionEnabled(QStyleOptionViewItem &opt, const QModelIndex &index) const;
|
||||
};
|
||||
|
||||
#endif // TREEITEMDELEGATE_H
|
||||
|
@ -20,8 +20,6 @@ void TreeView::setModel(QAbstractItemModel *model)
|
||||
|
||||
connect(model, &QAbstractItemModel::modelReset, this,
|
||||
[&] { emit currentIndexChanged(currentIndex()); });
|
||||
|
||||
setupItemDelegateModel();
|
||||
}
|
||||
|
||||
void TreeView::setupItemDelegate()
|
||||
@ -31,14 +29,6 @@ void TreeView::setupItemDelegate()
|
||||
setItemDelegateForColumn(0, tid);
|
||||
}
|
||||
|
||||
void TreeView::setupItemDelegateModel()
|
||||
{
|
||||
auto tid = qobject_cast<TreeItemDelegate *>(itemDelegateForColumn(0));
|
||||
Q_ASSERT(tid);
|
||||
|
||||
tid->setModel(qobject_cast<TableItemModel *>(model()));
|
||||
}
|
||||
|
||||
void TreeView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
|
||||
{
|
||||
QTreeView::selectionChanged(selected, deselected);
|
||||
|
@ -27,9 +27,6 @@ protected:
|
||||
|
||||
void contextMenuEvent(QContextMenuEvent *event) override;
|
||||
|
||||
private:
|
||||
void setupItemDelegateModel();
|
||||
|
||||
private:
|
||||
QMenu *m_menu = nullptr;
|
||||
};
|
||||
|
@ -175,6 +175,10 @@ QVariant RuleListModel::data(const QModelIndex &index, int role) const
|
||||
// Icon
|
||||
case Qt::DecorationRole:
|
||||
return dataDecoration(index);
|
||||
|
||||
// Enabled
|
||||
case EnabledRole:
|
||||
return dataEnabled(index);
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
@ -238,6 +242,12 @@ QVariant RuleListModel::dataDecoration(const QModelIndex &index) const
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QVariant RuleListModel::dataEnabled(const QModelIndex &index) const
|
||||
{
|
||||
const auto &ruleRow = ruleRowAt(index);
|
||||
return ruleRow.enabled;
|
||||
}
|
||||
|
||||
void RuleListModel::fillQueryVars(QVariantHash &vars) const
|
||||
{
|
||||
FtsTableSqlModel::fillQueryVars(vars);
|
||||
@ -245,14 +255,6 @@ void RuleListModel::fillQueryVars(QVariantHash &vars) const
|
||||
vars.insert(":type", sqlRuleType());
|
||||
}
|
||||
|
||||
Qt::ItemFlags RuleListModel::flagIsEnabled(const QModelIndex &index) const
|
||||
{
|
||||
setSqlRuleType(index);
|
||||
|
||||
const auto &ruleRow = ruleRowAt(index);
|
||||
return ruleRow.enabled ? Qt::ItemIsEnabled : Qt::NoItemFlags;
|
||||
}
|
||||
|
||||
Qt::ItemFlags RuleListModel::flagHasChildren(const QModelIndex &index) const
|
||||
{
|
||||
return isIndexRule(index) ? Qt::ItemNeverHasChildren : Qt::NoItemFlags;
|
||||
|
@ -49,15 +49,14 @@ public:
|
||||
int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
Qt::ItemFlags flagIsEnabled(const QModelIndex &index) const override;
|
||||
Qt::ItemFlags flagHasChildren(const QModelIndex &index) const override;
|
||||
|
||||
const RuleRow &ruleRowAt(const QModelIndex &index) const;
|
||||
RuleRow ruleRowById(int ruleId, Rule::RuleType ruleType) const;
|
||||
|
||||
static QStringList ruleTypeNames();
|
||||
|
||||
protected:
|
||||
Qt::ItemFlags flagHasChildren(const QModelIndex &index) const override;
|
||||
|
||||
void fillQueryVars(QVariantHash &vars) const override;
|
||||
|
||||
bool updateTableRow(const QVariantHash &vars, int row) const override;
|
||||
|
@ -37,8 +37,6 @@ public:
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
|
||||
Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override;
|
||||
|
||||
const ZoneRow &zoneRowAt(int row) const;
|
||||
|
||||
QVariant zoneTypeByCode(const QString &typeCode) const;
|
||||
@ -47,6 +45,8 @@ public:
|
||||
const QVariantList &zoneSources() const { return m_zoneSources; }
|
||||
|
||||
protected:
|
||||
Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override;
|
||||
|
||||
bool updateTableRow(const QVariantHash &vars, int row) const override;
|
||||
TableRow &tableRow() const override { return m_zoneRow; }
|
||||
|
||||
|
@ -38,8 +38,6 @@ public:
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
|
||||
Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override;
|
||||
|
||||
void setupTaskRows();
|
||||
|
||||
QVariant toVariant() const;
|
||||
@ -48,6 +46,8 @@ signals:
|
||||
void dataEdited();
|
||||
|
||||
protected:
|
||||
Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override;
|
||||
|
||||
bool updateTableRow(const QVariantHash & /*vars*/, int /*row*/) const override { return true; }
|
||||
TableRow &tableRow() const override { return m_taskRow; }
|
||||
|
||||
|
@ -39,11 +39,6 @@ void TableItemModel::resetLater()
|
||||
m_resetTimer->startTrigger();
|
||||
}
|
||||
|
||||
Qt::ItemFlags TableItemModel::flagIsEnabled(const QModelIndex &index) const
|
||||
{
|
||||
return Qt::ItemIsEnabled;
|
||||
}
|
||||
|
||||
Qt::ItemFlags TableItemModel::flagHasChildren(const QModelIndex & /*index*/) const
|
||||
{
|
||||
return Qt::ItemNeverHasChildren;
|
||||
|
@ -19,6 +19,11 @@ class TableItemModel : public QAbstractItemModel
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum TableItemRole {
|
||||
EnabledRole = Qt::UserRole,
|
||||
EndRole,
|
||||
};
|
||||
|
||||
explicit TableItemModel(QObject *parent = nullptr);
|
||||
|
||||
QModelIndex index(
|
||||
@ -29,16 +34,15 @@ public:
|
||||
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
|
||||
virtual Qt::ItemFlags flagIsEnabled(const QModelIndex &index) const;
|
||||
virtual Qt::ItemFlags flagHasChildren(const QModelIndex &index) const;
|
||||
virtual Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const;
|
||||
|
||||
public slots:
|
||||
void resetLater();
|
||||
void reset();
|
||||
void refresh();
|
||||
|
||||
protected:
|
||||
virtual Qt::ItemFlags flagHasChildren(const QModelIndex &index) const;
|
||||
virtual Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const;
|
||||
|
||||
virtual void invalidateRowCache() const;
|
||||
void updateRowCache(int row) const;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user