void PerformanceCounterSelection::updateParentCheckState(RDTreeWidgetItem *item) { if(!item) return; int numChecked = 0; int numPartial = 0; for(int i = 0; i < item->childCount(); i++) { Qt::CheckState state = item->child(i)->checkState(0); if(state == Qt::PartiallyChecked) numPartial++; else if(state == Qt::Checked) numChecked++; } if(numChecked == item->childCount()) item->setCheckState(0, Qt::Checked); else if(numChecked > 0 || numPartial > 0) item->setCheckState(0, Qt::PartiallyChecked); else item->setCheckState(0, Qt::Unchecked); item->setData(0, PreviousCheckStateRole, item->checkState(0)); updateParentCheckState(item->parent()); }
void ZObjsModel::updateParentCheckState(const QModelIndex &child) { QModelIndex idx = parent(child); if (!idx.isValid()) return; int numChecked = 0; int numUnChecked = 0; int numPartialChecked = 0; Qt::CheckState oldCheckState = getModelIndexCheckState(idx); Qt::CheckState newCheckState; for (int i=0; i<rowCount(idx); i++) { QModelIndex cld = index(i, 0, idx); Qt::CheckState cs = getModelIndexCheckState(cld); if (cs == Qt::Checked) { ++numChecked; } else if (cs == Qt::Unchecked) { ++numUnChecked; } else { // should be PartialChecked ++numPartialChecked; break; } } if (numPartialChecked > 0) { newCheckState = Qt::PartiallyChecked; } else if (numChecked > 0 && numUnChecked > 0) { newCheckState = Qt::PartiallyChecked; } else if (numChecked == 0) { if (numUnChecked == 0) { LERROR() << "This Index should have at least one child."; return; } newCheckState = Qt::Unchecked; } else { // numUnChecked == 0 if (numChecked == 0) { LERROR() << "This Index should have at least one child."; return; } newCheckState = Qt::Checked; } if (newCheckState != oldCheckState) { setModelIndexCheckState(idx, newCheckState); updateParentCheckState(idx); } }
bool ZObjsModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; if (role == Qt::CheckStateRole && index.column() == 0 && needCheckbox(index)) { Qt::CheckState cs = static_cast<Qt::CheckState>(value.toInt()); setModelIndexCheckState(index, cs); // update child items check state updateChildCheckState(index, cs); // update parent items updateParentCheckState(index); return true; } return false; }
PerformanceCounterSelection::PerformanceCounterSelection(ICaptureContext &ctx, const QList<GPUCounter> &selectedCounters, QWidget *parent) : QDialog(parent), ui(new Ui::PerformanceCounterSelection), m_Ctx(ctx) { ui->setupUi(this); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); ui->counterTree->setColumns({QString()}); ui->counterTree->setHeaderHidden(true); connect(ui->counterTree, &RDTreeWidget::currentItemChanged, [this](RDTreeWidgetItem *item, RDTreeWidgetItem *) -> void { const QVariant d = item->data(0, CounterDescriptionRole); if(d.isValid()) { ui->counterDescription->setText( QString(lit("<b>%1</b><hr>%2")).arg(item->text(0)).arg(d.toString())); } }); connect(ui->save, &QPushButton::pressed, this, &PerformanceCounterSelection::Save); connect(ui->load, &QPushButton::pressed, this, &PerformanceCounterSelection::Load); connect(ui->sampleCounters, &QPushButton::pressed, this, &PerformanceCounterSelection::accept); connect(ui->cancel, &QPushButton::pressed, this, &PerformanceCounterSelection::reject); connect(ui->counterTree, &RDTreeWidget::itemChanged, [this](RDTreeWidgetItem *item, int) -> void { const QVariant d = item->data(0, CounterIdRole); static bool recurse = false; if(d.isValid()) { if(item->checkState(0) == Qt::Checked) { // Add QListWidgetItem *listItem = new QListWidgetItem(ui->enabledCounters); listItem->setText(item->text(0)); listItem->setData(CounterIdRole, d); m_SelectedCounters.insert((GPUCounter)d.toUInt(), listItem); } else { // Remove QListWidgetItem *listItem = m_SelectedCounters.take((GPUCounter)d.toUInt()); delete listItem; } if(!recurse) { recurse = true; updateParentCheckState(item->parent()); recurse = false; } } else if(!recurse) { Qt::CheckState prev = item->data(0, PreviousCheckStateRole).value<Qt::CheckState>(); if(item->checkState(0) != prev) { recurse = true; if(item->checkState(0) == Qt::Checked) { checkAllChildren(item); } else { uncheckAllChildren(item); } item->setData(0, PreviousCheckStateRole, item->checkState(0)); updateParentCheckState(item); recurse = false; } } }); ui->counterTree->setMouseTracking(true); ctx.Replay().AsyncInvoke([this, selectedCounters](IReplayController *controller) -> void { QVector<CounterDescription> counterDescriptions; for(const GPUCounter counter : controller->EnumerateCounters()) { counterDescriptions.append(controller->DescribeCounter(counter)); } GUIInvoke::call([counterDescriptions, selectedCounters, this]() -> void { SetCounters(counterDescriptions); SetSelectedCounters(selectedCounters); }); }); ui->counterTree->setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(ui->counterTree, &RDTreeWidget::customContextMenuRequested, this, &PerformanceCounterSelection::counterTree_contextMenu); }