qt/LedOK/base/loqtreewidget.cpp

66 lines
2.4 KiB
C++
Raw Normal View History

2022-08-25 18:37:24 +08:00
#include "loqtreewidget.h"
#include <QCheckBox>
#include <QHeaderView>
class CheckableHeader : public QHeaderView {
public:
explicit CheckableHeader(Qt::Orientation orientation, QWidget *isSelAll) : QHeaderView(orientation), fdIsSelAll(isSelAll) {
fdIsSelAll->setParent(this);
}
QWidget *fdIsSelAll;
protected:
2023-08-07 16:24:33 +08:00
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override {
QHeaderView::paintSection(painter, rect, logicalIndex);
if(logicalIndex == 0) fdIsSelAll->setGeometry(rect);
}
2022-08-25 18:37:24 +08:00
};
LoQTreeWidget::LoQTreeWidget(QWidget *parent) : QTreeWidget(parent), m_checkState(CheckNone) {
2023-08-07 16:24:33 +08:00
fdIsSelAll = new QCheckBox;
2023-04-18 14:14:46 +08:00
fdIsSelAll->setStyleSheet("QCheckBox{margin-left: 5px;}");
2022-08-25 18:37:24 +08:00
m_header = new CheckableHeader(Qt::Horizontal, fdIsSelAll);
setHeader(m_header);
setProperty("ssType", "topList");
connect(fdIsSelAll, SIGNAL(toggled(bool)), this, SLOT(onCheckAll(bool)));
connect(this, &LoQTreeWidget::itemClicked, this, [this](QTreeWidgetItem *item, int column) {
2023-05-15 16:06:10 +08:00
if(selectionMode()==QAbstractItemView::NoSelection) return;
2022-08-25 18:37:24 +08:00
if(column > 0) item->setCheckState(0, item->checkState(0) == Qt::Unchecked ? Qt::Checked : Qt::Unchecked);
adjustCheckState();
});
setFocusPolicy(Qt::NoFocus);
}
void LoQTreeWidget::adjustCheckState() {
bool isAllChecked = true;
int cnt = topLevelItemCount();
if(cnt == 0) {
isAllChecked = false;
m_checkState = CheckNone;
} else {
int k = 0;
for(int i=0; i<cnt; i++) if(!this->isRowHidden(i,indexFromItem(topLevelItem(i)->parent()))) {
if(topLevelItem(i)->checkState(0) == Qt::Unchecked) isAllChecked = false;
else k++;
}
if(k > 1) m_checkState = CheckMulti;
else if(k > 0) m_checkState = CheckOne;
else m_checkState = CheckNone;
}
fdIsSelAll->blockSignals(true);
fdIsSelAll->setChecked(isAllChecked);
fdIsSelAll->blockSignals(false);
emit sigCheckStateChanged(m_checkState);
}
void LoQTreeWidget::onCheckAll(bool checked) {
int cnt = topLevelItemCount();
for(int i=0; i<cnt; i++) {
if(checked) {
if(this->isRowHidden(i,indexFromItem(topLevelItem(i)->parent()))) topLevelItem(i)->setCheckState(0, Qt::Unchecked);
else topLevelItem(i)->setCheckState(0, Qt::Checked);
}
else topLevelItem(i)->setCheckState(0, Qt::Unchecked);
}
adjustCheckState();
}