| 2108 | } |
| 2109 | |
| 2110 | void SpreadsheetView::reverseSelection() { |
| 2111 | auto* selectionModel = m_tableView->selectionModel(); |
| 2112 | const auto& indexes = selectionModel->selectedIndexes(); |
| 2113 | if (indexes.isEmpty()) |
| 2114 | return; |
| 2115 | |
| 2116 | // check if complete rows only are selected, reverse the selection for rows only in this case |
| 2117 | bool rowsOnly = true; |
| 2118 | QVector<int> selectedRows; |
| 2119 | for (const auto& index : indexes) { |
| 2120 | const int row = index.row(); |
| 2121 | if (selectionModel->isRowSelected(row)) { |
| 2122 | if (selectedRows.indexOf(row) == -1) |
| 2123 | selectedRows << row; |
| 2124 | } else { |
| 2125 | rowsOnly = false; |
| 2126 | break; |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | if (rowsOnly) { |
| 2131 | // clear the current selection |
| 2132 | m_suppressSelectionChangedEvent = true; |
| 2133 | selectionModel->clearSelection(); |
| 2134 | |
| 2135 | // temporarily switch to the "multi selection" mode and select the rows, that were not selected before |
| 2136 | m_tableView->setSelectionMode(QAbstractItemView::MultiSelection); |
| 2137 | for (int row = 0; row < m_spreadsheet->rowCount(); ++row) { |
| 2138 | if (selectedRows.indexOf(row) == -1) |
| 2139 | m_tableView->selectRow(row); |
| 2140 | } |
| 2141 | m_tableView->setSelectionMode(QAbstractItemView::ExtendedSelection); // switch back to the extended selection |
| 2142 | |
| 2143 | m_suppressSelectionChangedEvent = false; |
| 2144 | selectionChanged(QItemSelection(), QItemSelection()); |
| 2145 | return; |
| 2146 | } |
| 2147 | |
| 2148 | // check if complete columns are selected only, reverse the selection for columns only in this case |
| 2149 | bool columnsOnly = true; |
| 2150 | QVector<int> selectedColumns; |
| 2151 | for (const auto& index : indexes) { |
| 2152 | const int col = index.column(); |
| 2153 | if (selectionModel->isColumnSelected(col)) { |
| 2154 | if (selectedColumns.indexOf(col) == -1) |
| 2155 | selectedColumns << col; |
| 2156 | } else { |
| 2157 | columnsOnly = false; |
| 2158 | break; |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | if (columnsOnly) { |
| 2163 | // clear the current selection |
| 2164 | m_suppressSelectionChangedEvent = true; |
| 2165 | selectionModel->clearSelection(); |
| 2166 | |
| 2167 | // temporarily switch to the "multi selection" mode and select the columns, that were not selected before |
nothing calls this directly
no test coverage detected