| 1 |
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */ |
| 2 |
|
| 3 |
#include "QtTree.h" |
| 4 |
#include <QEvent> |
| 5 |
|
| 6 |
#include "SignalBlocker.h" |
| 7 |
|
| 8 |
|
| 9 |
CQtTree::CQtTree(QWidget* _parent) |
| 10 |
: QTreeWidget{ _parent } |
| 11 |
{ |
| 12 |
} |
| 13 |
|
| 14 |
QTreeWidgetItem* CQtTree::CreateItem(EFlags _flags) |
| 15 |
{ |
| 16 |
return CreateItem<QTreeWidget>(this, -1, "", _flags, -1); |
| 17 |
} |
| 18 |
|
| 19 |
QTreeWidgetItem* CQtTree::CreateItem(QTreeWidgetItem* _parent, EFlags _flags) |
| 20 |
{ |
| 21 |
return CreateItem<QTreeWidgetItem>(_parent, -1, "", _flags, -1); |
| 22 |
} |
| 23 |
|
| 24 |
QTreeWidgetItem* CQtTree::CreateItem(int _col, const std::string& _text, EFlags _flags, const QVariant& _data) |
| 25 |
{ |
| 26 |
return CreateItem<QTreeWidget>(this, _col, _text, _flags, _data); |
| 27 |
} |
| 28 |
|
| 29 |
QTreeWidgetItem* CQtTree::CreateItem(QTreeWidget* _parent, int _col, const std::string& _text, EFlags _flags, const QVariant& _data) |
| 30 |
{ |
| 31 |
return CreateItem<QTreeWidget>(_parent, _col, _text, _flags, _data); |
| 32 |
} |
| 33 |
|
| 34 |
QTreeWidgetItem* CQtTree::CreateItem(QTreeWidgetItem* _parent, int _col, const std::string& _text, EFlags _flags, const QVariant& _data) |
| 35 |
{ |
| 36 |
return CreateItem<QTreeWidgetItem>(_parent, _col, _text, _flags, _data); |
| 37 |
} |
| 38 |
|
| 39 |
QTreeWidgetItem* CQtTree::CreateItem(QTreeWidgetItem* _parent, int _col, const std::string& _text, EFlags _flags, const std::string& _data) |
| 40 |
{ |
| 41 |
return CreateItem<QTreeWidgetItem>(_parent, _col, _text, _flags, QString::fromStdString(_data)); |
| 42 |
} |
| 43 |
|
| 44 |
void CQtTree::SetText(QTreeWidgetItem* _item, int _col, const std::string& _text, const QVariant& _data) |
| 45 |
{ |
| 46 |
if (_col < 0) return; |
| 47 |
_item->setText(_col, QString::fromStdString(_text)); |
| 48 |
if (_data != -1) |
| 49 |
_item->setData(_col, Qt::UserRole, _data); |
| 50 |
} |
| 51 |
|
| 52 |
QComboBox* CQtTree::SetComboBox(QTreeWidgetItem* _item, int _col, const std::vector<QString>& _names, const std::vector<QVariant>& _data, const QVariant& _selected) |
| 53 |
{ |
| 54 |
auto* combo = new QComboBox{ this }; |
| 55 |
int selected = -1; |
| 56 |
for (size_t i = 0; i < _names.size(); ++i) |
| 57 |
{ |
| 58 |
combo->addItem(_names[i], i < _data.size() ? _data[i] : QVariant{}); |
| 59 |
if (_data[i] == _selected) |
| 60 |
selected = static_cast<int>(i); |
| 61 |
} |
| 62 |
combo->setCurrentIndex(selected); |
| 63 |
combo->installEventFilter(this); |
| 64 |
_item->setSizeHint(_col, { combo->sizeHint().width() + 10, combo->sizeHint().height() }); |
| 65 |
connect(combo, QOverload<int>::of(&QComboBox::highlighted), this, [this, _item] { setCurrentItem(_item); }); |
| 66 |
connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this, combo, _item] { ComboBoxIndexChanged(combo, _item); }); |
| 67 |
setItemWidget(_item, _col, combo); |
| 68 |
return combo; |
| 69 |
} |
| 70 |
|
| 71 |
void CQtTree::SetupComboBox(QTreeWidgetItem* _item, int _col, const std::vector<QString>& _names, const std::vector<QVariant>& _data, const QVariant& _selected) const |
| 72 |
{ |
| 73 |
auto* combo = dynamic_cast<QComboBox*>(itemWidget(_item, _col)); |
| 74 |
if (!combo) return; |
| 75 |
QSignalBlocker blocker{ combo }; |
| 76 |
combo->clear(); |
| 77 |
int selected = -1; |
| 78 |
for (size_t i = 0; i < _names.size(); ++i) |
| 79 |
{ |
| 80 |
combo->addItem(_names[i], i < _data.size() ? _data[i] : QVariant{}); |
| 81 |
if (_data[i] == _selected) |
| 82 |
selected = static_cast<int>(i); |
| 83 |
} |
| 84 |
combo->setCurrentIndex(selected); |
| 85 |
} |
| 86 |
|
| 87 |
void CQtTree::SetupComboBox(QTreeWidgetItem* _item, int _col, const std::vector<std::string>& _names, const std::vector<std::string>& _data, const std::string& _selected) const |
| 88 |
{ |
| 89 |
if (_names.size() != _data.size()) return; |
| 90 |
std::vector<QString> names; |
| 91 |
std::vector<QVariant> data; |
| 92 |
for (int i = 0; i < static_cast<int>(_names.size()); ++i) |
| 93 |
{ |
| 94 |
names.emplace_back(QString::fromStdString(_names[i])); |
| 95 |
data.emplace_back(QString::fromStdString(_data[i])); |
| 96 |
} |
| 97 |
SetupComboBox(_item, _col, names, data, QString::fromStdString(_selected)); |
| 98 |
} |
| 99 |
|
| 100 |
void CQtTree::SetComboBoxValue(QTreeWidgetItem* _item, int _col, const QVariant& _value) const |
| 101 |
{ |
| 102 |
auto* combo = dynamic_cast<QComboBox*>(itemWidget(_item, _col)); |
| 103 |
if (!combo) return; |
| 104 |
QSignalBlocker blocker{ combo }; |
| 105 |
for (int i = 0; i < combo->count(); ++i) |
| 106 |
if (combo->itemData(i) == _value) |
| 107 |
{ |
| 108 |
combo->setCurrentIndex(i); |
| 109 |
return; |
| 110 |
} |
| 111 |
combo->setCurrentIndex(-1); |
| 112 |
} |
| 113 |
|
| 114 |
QVariant CQtTree::GetComboBoxValue(QTreeWidgetItem* _item, int _col) const |
| 115 |
{ |
| 116 |
const auto* combo = dynamic_cast<QComboBox*>(itemWidget(_item, _col)); |
| 117 |
if (!combo) return{}; |
| 118 |
return combo->itemData(combo->currentIndex()); |
| 119 |
} |
| 120 |
|
| 121 |
QCheckBox* CQtTree::SetCheckBox(QTreeWidgetItem* _item, int _col, const QString& _text, bool _value) |
| 122 |
{ |
| 123 |
auto* check = new QCheckBox{ _text, this }; |
| 124 |
QSignalBlocker blocker{ check }; |
| 125 |
check->setChecked(_value); |
| 126 |
connect(check, &QCheckBox::stateChanged, this, [this, check, _item] { CheckBoxStateChanged(check, _item); }); |
| 127 |
setItemWidget(_item, _col, check); |
| 128 |
return check; |
| 129 |
} |
| 130 |
|
| 131 |
void CQtTree::SetCheckBoxValue(QTreeWidgetItem* _item, int _col, bool _value) const |
| 132 |
{ |
| 133 |
auto* check = dynamic_cast<QCheckBox*>(itemWidget(_item, _col)); |
| 134 |
if (!check) return; |
| 135 |
QSignalBlocker blocker{ check }; |
| 136 |
check->setChecked(_value); |
| 137 |
} |
| 138 |
|
| 139 |
bool CQtTree::GetCheckBoxValue(QTreeWidgetItem* _item, int _col) const |
| 140 |
{ |
| 141 |
const auto* check = dynamic_cast<QCheckBox*>(itemWidget(_item, _col)); |
| 142 |
if (!check) return{}; |
| 143 |
return check->isChecked(); |
| 144 |
} |
| 145 |
|
| 146 |
void CQtTree::SetItemFlags(QTreeWidgetItem* _item, EFlags _flags) |
| 147 |
{ |
| 148 |
if (QtTreeUtils::Contains(_flags, EFlags::EDIT)) _item->setFlags(_item->flags() | Qt::ItemIsEditable); |
| 149 |
if (QtTreeUtils::Contains(_flags, EFlags::NO_EDIT)) _item->setFlags(_item->flags() & ~Qt::ItemIsEditable); |
| 150 |
if (QtTreeUtils::Contains(_flags, EFlags::SELECT)) _item->setFlags(_item->flags() | Qt::ItemIsSelectable); |
| 151 |
if (QtTreeUtils::Contains(_flags, EFlags::NO_SELECT)) _item->setFlags(_item->flags() & ~Qt::ItemIsSelectable); |
| 152 |
if (QtTreeUtils::Contains(_flags, EFlags::ENABLED)) _item->setFlags(_item->flags() | Qt::ItemIsEnabled); |
| 153 |
if (QtTreeUtils::Contains(_flags, EFlags::DISABLED)) _item->setFlags(_item->flags() & ~Qt::ItemIsEnabled); |
| 154 |
} |
| 155 |
|
| 156 |
void CQtTree::SetCurrentItem(const std::vector<size_t>& _indices) |
| 157 |
{ |
| 158 |
if (_indices.empty()) return; |
| 159 |
QTreeWidgetItem* item = topLevelItem(static_cast<int>(_indices[0]) < topLevelItemCount() ? static_cast<int>(_indices[0]) : topLevelItemCount() - 1); |
| 160 |
for (size_t i = 1; i < _indices.size(); ++i) |
| 161 |
item = item->child(static_cast<int>(_indices[i]) < item->childCount() ? static_cast<int>(_indices[i]) : item->childCount() - 1); |
| 162 |
setCurrentItem(item); |
| 163 |
} |
| 164 |
|
| 165 |
void CQtTree::SetCurrentItem(const QVariant& _data) |
| 166 |
{ |
| 167 |
setCurrentItem(GetItem(_data)); |
| 168 |
} |
| 169 |
|
| 170 |
QTreeWidgetItem* CQtTree::GetItem(const QVariant& _data) const |
| 171 |
{ |
| 172 |
for (auto* item : findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard | Qt::MatchRecursive)) |
| 173 |
for (int i = 0; i < item->columnCount(); ++i) |
| 174 |
if (item->data(i, Qt::UserRole) == _data) |
| 175 |
return item; |
| 176 |
return nullptr; |
| 177 |
} |
| 178 |
|
| 179 |
QVariant CQtTree::GetData(const QTreeWidgetItem* _item, int _col) |
| 180 |
{ |
| 181 |
if (!_item || _col >= _item->columnCount()) return {}; |
| 182 |
return _item->data(_col, Qt::UserRole); |
| 183 |
} |
| 184 |
|
| 185 |
QString CQtTree::GetDataQStr(const QTreeWidgetItem* _item, int _col) |
| 186 |
{ |
| 187 |
return GetData(_item, _col).toString(); |
| 188 |
} |
| 189 |
|
| 190 |
std::string CQtTree::GetDataStr(const QTreeWidgetItem* _item, int _col) |
| 191 |
{ |
| 192 |
return GetDataQStr(_item, _col).toStdString(); |
| 193 |
} |
| 194 |
|
| 195 |
QString CQtTree::GetCurrentDataQStr(int _col) const |
| 196 |
{ |
| 197 |
return GetDataQStr(currentItem(), _col); |
| 198 |
} |
| 199 |
|
| 200 |
bool CQtTree::IsSuccessor(const QTreeWidgetItem* _parent, const QTreeWidgetItem* _child) |
| 201 |
{ |
| 202 |
if (_parent == _child) |
| 203 |
return true; |
| 204 |
|
| 205 |
for (int i = 0; i < _parent->childCount(); ++i) |
| 206 |
{ |
| 207 |
const QTreeWidgetItem* currChild = _parent->child(i); |
| 208 |
if (currChild == _child) |
| 209 |
return true; |
| 210 |
if (CQtTree::IsSuccessor(currChild, _child)) |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
bool CQtTree::blockSignals(bool _flag) |
| 218 |
{ |
| 219 |
for (auto* w : findChildren<QComboBox*>(QString(), Qt::FindChildrenRecursively)) w->blockSignals(_flag); |
| 220 |
for (auto* w : findChildren<QCheckBox*>(QString(), Qt::FindChildrenRecursively)) w->blockSignals(_flag); |
| 221 |
return QTreeWidget::blockSignals(_flag); |
| 222 |
} |
| 223 |
|
| 224 |
bool CQtTree::eventFilter(QObject* _object, QEvent* _event) |
| 225 |
{ |
| 226 |
if (_event->type() == QEvent::Wheel && dynamic_cast<QComboBox*>(_object)) |
| 227 |
return true; |
| 228 |
return QTreeWidget::eventFilter(_object, _event); |
| 229 |
} |
| 230 |
|
| 231 |
template <typename T> |
| 232 |
QTreeWidgetItem* CQtTree::CreateItem(T* _parent, int _col, const std::string& _text, EFlags _flags, const QVariant& _data) |
| 233 |
{ |
| 234 |
auto* item = new QTreeWidgetItem{ _parent }; |
| 235 |
if (_col != -1 && _data != -1) |
| 236 |
item->setData(_col, Qt::UserRole, _data); |
| 237 |
if (_col != -1 && !_text.empty()) |
| 238 |
item->setText(_col, QString::fromStdString(_text)); |
| 239 |
if (_flags != EFlags::DEFAULT) |
| 240 |
SetItemFlags(item, _flags); |
| 241 |
return item; |
| 242 |
} |