| 230 | } |
| 231 | |
| 232 | void ServicesWidget::onTableContextMenu(const QPoint &pos) |
| 233 | { |
| 234 | this->m_tableContextMenuOpen = true; |
| 235 | |
| 236 | const QModelIndex clickedIndex = this->ui->tableView->indexAt(pos); |
| 237 | const QModelIndex targetIndex = clickedIndex.isValid() ? clickedIndex : this->ui->tableView->currentIndex(); |
| 238 | const bool hasTargetCell = targetIndex.isValid(); |
| 239 | const QModelIndexList selectedRowsIdx = this->ui->tableView->selectionModel()->selectedRows(); |
| 240 | QList<int> selectedRows; |
| 241 | selectedRows.reserve(selectedRowsIdx.size()); |
| 242 | for (const QModelIndex &idx : selectedRowsIdx) |
| 243 | selectedRows.append(idx.row()); |
| 244 | std::sort(selectedRows.begin(), selectedRows.end()); |
| 245 | const bool hasSelectedRows = !selectedRows.isEmpty(); |
| 246 | const bool multipleRowsSelected = selectedRows.size() > 1; |
| 247 | const bool hasRowTarget = hasTargetCell || hasSelectedRows; |
| 248 | const QModelIndex sourceTargetIndex = this->sourceIndexFromProxy(targetIndex); |
| 249 | const QString targetUnit = this->serviceUnitFromSourceIndex(sourceTargetIndex); |
| 250 | const bool hasSingleTargetService = !targetUnit.isEmpty() && !multipleRowsSelected; |
| 251 | |
| 252 | QMenu menu(this); |
| 253 | QMenu *copyMenu = menu.addMenu(tr("Copy")); |
| 254 | |
| 255 | QAction *copyRowAct = copyMenu->addAction(tr("Entire row")); |
| 256 | copyRowAct->setEnabled(hasRowTarget); |
| 257 | connect(copyRowAct, &QAction::triggered, this, [this, targetIndex, selectedRows, multipleRowsSelected]() |
| 258 | { |
| 259 | if (multipleRowsSelected) |
| 260 | { |
| 261 | QGuiApplication::clipboard()->setText(UIHelper::GetVisibleRowsText(this->ui->tableView, selectedRows)); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | const int row = targetIndex.isValid() ? targetIndex.row() : selectedRows.value(0, -1); |
| 266 | QGuiApplication::clipboard()->setText(UIHelper::GetVisibleRowText(this->ui->tableView, row)); |
| 267 | }); |
| 268 | |
| 269 | QAction *copyCellAct = copyMenu->addAction(tr("Selected cell")); |
| 270 | copyCellAct->setEnabled(hasTargetCell && !multipleRowsSelected); |
| 271 | connect(copyCellAct, &QAction::triggered, this, [this, targetIndex]() |
| 272 | { |
| 273 | QGuiApplication::clipboard()->setText(this->ui->tableView->model()->data(targetIndex, Qt::DisplayRole).toString()); |
| 274 | }); |
| 275 | |
| 276 | menu.addSeparator(); |
| 277 | |
| 278 | QAction *startAct = menu.addAction(tr("Start")); |
| 279 | startAct->setEnabled(hasSingleTargetService); |
| 280 | connect(startAct, &QAction::triggered, this, [this, targetUnit]() |
| 281 | { |
| 282 | this->manageServiceUnit(targetUnit, OS::ServiceHelper::UnitAction::Start, tr("start")); |
| 283 | }); |
| 284 | |
| 285 | QAction *stopAct = menu.addAction(tr("Stop")); |
| 286 | stopAct->setEnabled(hasSingleTargetService); |
| 287 | connect(stopAct, &QAction::triggered, this, [this, targetUnit]() |
| 288 | { |
| 289 | this->manageServiceUnit(targetUnit, OS::ServiceHelper::UnitAction::Stop, tr("stop")); |
nothing calls this directly
no test coverage detected