10 #include <QFontDatabase>
14 #include <QKeySequence>
26 return doj::alphanum_impl(this->text(0).toLocal8Bit(), other.text(0).toLocal8Bit()) <
35 setEditTriggers(NoEditTriggers);
36 setDragEnabled(
false);
37 setDefaultDropAction(Qt::IgnoreAction);
38 setDragDropOverwriteMode(
false);
39 setMouseTracking(
true);
40 setDragDropMode(NoDragDrop);
41 viewport()->installEventFilter(
this);
42 setSelectionMode(ExtendedSelection);
43 setSelectionBehavior(QAbstractItemView::SelectRows);
44 setFocusPolicy(Qt::ClickFocus);
46 header()->setVisible(
false);
47 header()->setStretchLastSection(
true);
48 header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
49 setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
51 connect(
this, &QTreeWidget::itemDoubleClicked,
this,
52 [
this](QTreeWidgetItem* item,
int column) {
59 connect(selectionModel(), &QItemSelectionModel::selectionChanged,
this, [
this]() {
64 setFocusPolicy(Qt::NoFocus);
69 setFocusPolicy(Qt::ClickFocus);
77 if (tooltip.isValid())
79 QToolTip::showText(_tooltip_pos, tooltip.toString(), this, QRect(), 10000);
96 const QString& plot_ID)
105 bool use_separator = settings.value(
"Preferences::use_separator",
true).toBool();
114 parts.push_back(tree_name);
117 if (parts.size() == 0)
122 bool prefix_is_group = tree_name.startsWith(group_name);
123 bool hasGroup = !group_name.isEmpty();
126 if (hasGroup && !prefix_is_group)
128 parts = group_parts + parts;
131 QTreeWidgetItem* tree_parent = this->invisibleRootItem();
133 for (
int i = 0; i < parts.size(); i++)
135 bool is_leaf = (i == parts.size() - 1);
136 const auto& part = parts[i];
138 QTreeWidgetItem* matching_child =
nullptr;
140 for (
int c = 0; c < tree_parent->childCount(); c++)
142 QTreeWidgetItem* tree_child = tree_parent->child(c);
143 if (tree_child->text(0) == part)
145 matching_child = tree_child;
152 tree_parent = matching_child;
157 child_item->setText(0, part);
158 child_item->setText(1, is_leaf ?
"-" :
"");
160 bool isGroupCell = (i < group_parts.size());
162 QFont font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
165 child_item->setFont(0, font);
167 font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
169 child_item->setFont(1, font);
170 child_item->setTextAlignment(1, Qt::AlignRight);
172 tree_parent = child_item;
174 auto current_flag = child_item->flags();
178 child_item->setData(0,
Name, group_name);
179 child_item->setData(0,
IsGroupName, (i + 1) == group_parts.size());
184 child_item->setFlags(current_flag | Qt::ItemIsSelectable);
185 child_item->setData(0,
Name, plot_ID);
189 child_item->setFlags(current_flag & (~Qt::ItemIsSelectable));
198 invisibleRootItem()->sortChildren(0, Qt::AscendingOrder);
199 treeVisitor([&](QTreeWidgetItem* item) { item->sortChildren(0, Qt::AscendingOrder); });
200 header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
205 std::vector<std::string> non_hidden_list;
207 for (
const auto& item : selectedItems())
209 non_hidden_list.push_back(item->data(0, Qt::UserRole).toString().toStdString());
211 return non_hidden_list;
216 header()->setSectionResizeMode(0, QHeaderView::Fixed);
217 header()->setSectionResizeMode(1, QHeaderView::Fixed);
220 auto font = item->font(0);
222 item->setFont(0, font);
223 font = item->font(1);
225 item->setFont(1, font);
228 header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
229 header()->setSectionResizeMode(1, QHeaderView::Stretch);
234 bool updated =
false;
239 auto hideFunc = [&](QTreeWidgetItem* item) {
240 QString name = item->data(0, Qt::UserRole).toString();
247 if (search_string.isEmpty() ==
false)
249 for (
const auto& spaced_item : spaced_items)
251 if (name.contains(spaced_item, Qt::CaseInsensitive) ==
false)
263 if (toHide != item->isHidden())
268 item->setHidden(toHide);
271 auto parent = item->parent();
274 bool all_children_hidden =
true;
275 for (
int c = 0; c < parent->childCount(); c++)
277 if (!parent->child(c)->isHidden())
279 all_children_hidden =
false;
283 auto parent_hidden = parent->isHidden();
284 if (all_children_hidden != parent_hidden)
286 parent->setHidden(all_children_hidden);
287 parent = parent->parent();
304 if (event->type() == QEvent::MouseMove)
306 auto mouse_event =
static_cast<QMouseEvent*
>(event);
307 auto* item = itemAt(mouse_event->pos());
315 if (event->type() == QEvent::Leave)
323 return QWidget::eventFilter(
object, event);
333 auto removeFunc = [&](QTreeWidgetItem* item) {
334 QString curve_name = item->data(0, Qt::UserRole).toString();
335 if (curve_name == to_be_deleted)
338 auto parent_item = item->parent();
341 parent_item = invisibleRootItem();
343 parent_item->removeChild(item);
345 while (parent_item->childCount() == 0 && parent_item != invisibleRootItem())
347 auto prev_item = parent_item;
348 parent_item = parent_item->parent();
351 parent_item = invisibleRootItem();
353 parent_item->removeChild(prev_item);
366 setColumnHidden(1, hide);
371 std::function<
void(QTreeWidgetItem*)> recursiveFunction;
372 recursiveFunction = [&](QTreeWidgetItem* item) {
374 for (
int c = 0; c < item->childCount(); c++)
376 recursiveFunction(item->child(c));
380 for (
int c = 0; c < invisibleRootItem()->childCount(); c++)
382 recursiveFunction(invisibleRootItem()->child(c));
388 if (event->matches(QKeySequence::Copy))
390 auto selected = selectedItems();
391 if (selected.size() > 0)
393 QClipboard* clipboard = QApplication::clipboard();
394 clipboard->setText(selected.front()->data(0,
Name).toString());
401 int childCount = item->childCount();
402 for (
int i = 0; i < childCount; i++)
404 const auto child = item->child(i);
406 if (child->childCount() > 0)
408 child->setExpanded(expanded);