#include "xtreewidget.h" #include "qheaderview.h" #include "qmenu.h" #include "logmanager.h" #include "xtreewidgetitem.h" #include XTreeWidget::XTreeWidget(QWidget *parent) : QTreeWidget(parent) { setContextMenuPolicy(Qt::CustomContextMenu); setSelectionMode(QTreeWidget::SingleSelection); setColumnCount(3); header()->hide(); setMinimumWidth(350); setSortingEnabled(false); // Preserve parse order instead of sorting alphabetically setIconSize(QSize(16, 16)); header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); connect(this, &XTreeWidget::itemSelectionChanged, this, &XTreeWidget::ItemSelectionChanged); connect(this, &XTreeWidget::customContextMenuRequested, this, &XTreeWidget::PrepareContextMenu); } void XTreeWidget::PrepareContextMenu(const QPoint &pos) { auto activeItem = itemAt(pos); if (!activeItem) { return; } if (activeItem->text(0).isEmpty()) { return; } QString activeText = activeItem->text(0); QMenu *contextMenu = new QMenu(this); QPoint pt(pos); contextMenu->exec(mapToGlobal(pt)); delete contextMenu; } void XTreeWidget::ItemSelectionChanged() { if (selectedItems().isEmpty()) { return; } XTreeWidgetItem *selectedItem = dynamic_cast(selectedItems().first()); if (!selectedItem) { return; } if (selectedItem->text(0).isEmpty()) { return; } QString selectedText = selectedItem->text(0); emit ItemSelected(selectedText); }