#ifndef TRAY_GUI_HELPER_H #define TRAY_GUI_HELPER_H #include #include #include #include #include #include #include #include QT_FORWARD_DECLARE_CLASS(QPoint) QT_FORWARD_DECLARE_CLASS(QMenu) namespace QtGui { void showViewMenu(const QPoint &position, const QTreeView &view, QMenu &menu); inline auto copyToClipboard(const QString &text) { return [=] { QGuiApplication::clipboard()->setText(text); }; } template struct BasicRowData { operator bool() const; typename ViewType::ModelType *model = nullptr; QModelIndex index; decltype(model->info(index)) data = decltype(model->info(index))(); }; template BasicRowData::operator bool() const { if (!model || !index.isValid()) { return false; } if constexpr (CppUtilities::Traits::IsSpecializationOf::value || CppUtilities::Traits::IsSpecializationOf::value) { return data.first; } else { return data; } } template struct SelectedRow : public BasicRowData { explicit SelectedRow(ViewType *view); }; template SelectedRow::SelectedRow(ViewType *view) { auto *const selectionModel = view->selectionModel(); if (!selectionModel) { return; } const auto selectedRows = selectionModel->selectedRows(0); if (selectedRows.size() != 1) { return; } this->index = selectedRows.at(0); if constexpr (std::is_void_v) { this->model = qobject_cast(view->model()); } else { const auto *const sortFilterModel = qobject_cast(view->model()); if (sortFilterModel) { this->index = sortFilterModel->mapToSource(this->index); this->model = qobject_cast(sortFilterModel->sourceModel()); } } if (this->model) { this->data = this->model->info(this->index); } } template struct ClickedRow : public BasicRowData { explicit ClickedRow(ViewType *view, const QPoint &clickPoint); typename ViewType::SortFilterModelType *proxyModel = nullptr; QModelIndex proxyIndex; }; template ClickedRow::ClickedRow(ViewType *view, const QPoint &clickPoint) { if constexpr (!std::is_void_v) { proxyModel = qobject_cast(view->model()); } this->model = qobject_cast(proxyModel ? proxyModel->sourceModel() : view->model()); proxyIndex = view->indexAt(clickPoint); this->index = proxyModel ? proxyModel->mapToSource(proxyIndex) : proxyIndex; if (this->model && this->index.isValid() && this->index.column() == 1) { this->data = this->model->info(this->index); } } template inline auto triggerActionForSelectedRow(ViewType *view, ActionType action) { return [=] { if (const auto selectedRow = SelectedRow(view)) { std::invoke(action, view, CppUtilities::Traits::dereferenceMaybe(selectedRow.data)); } }; } inline int centerObj(int avail, int size) { return (avail - size) / 2; } } // namespace QtGui #endif // TRAY_GUI_HELPER_H