| 141 | } |
| 142 | |
| 143 | bool QmitkDataStorageTreeModel::dropMimeData( |
| 144 | const QMimeData *data, Qt::DropAction action, int row, int /*column*/, const QModelIndex &parent) |
| 145 | { |
| 146 | // Early exit, returning true, but not actually doing anything (ignoring data). |
| 147 | if (action == Qt::IgnoreAction) |
| 148 | { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | // Note, we are returning true if we handled it, and false otherwise |
| 153 | bool returnValue = false; |
| 154 | |
| 155 | if (data->hasFormat("application/x-qabstractitemmodeldatalist")) |
| 156 | { |
| 157 | returnValue = true; |
| 158 | |
| 159 | // First we extract a Qlist of TreeItem* pointers. |
| 160 | QList<TreeItem *> listOfItemsToDrop = ToTreeItemPtrList(data); |
| 161 | if (listOfItemsToDrop.empty()) |
| 162 | { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | // Retrieve the TreeItem* where we are dropping stuff, and its parent. |
| 167 | TreeItem *dropItem = this->TreeItemFromIndex(parent); |
| 168 | TreeItem *parentItem = dropItem->GetParent(); |
| 169 | |
| 170 | // If item was dropped onto empty space, we select the root node |
| 171 | if (dropItem == m_Root) |
| 172 | { |
| 173 | parentItem = m_Root; |
| 174 | } |
| 175 | |
| 176 | // Dragging and Dropping is only allowed within the same parent, so use the first item in list to validate. |
| 177 | // (otherwise, you could have a derived image such as a segmentation, and assign it to another image). |
| 178 | // NOTE: We are assuming the input list is valid... i.e. when it was dragged, all the items had the same parent. |
| 179 | |
| 180 | // Determine whether or not the drag and drop operation is a valid one. |
| 181 | // Examples of invalid operations include: |
| 182 | // - dragging nodes with different parents |
| 183 | // - dragging nodes from one parent to another parent, if m_AllowHierarchyChange is false |
| 184 | // - dragging a node on one of its child nodes (only relevant if m_AllowHierarchyChange is true) |
| 185 | |
| 186 | bool isValidDragAndDropOperation(true); |
| 187 | |
| 188 | // different parents |
| 189 | { |
| 190 | TreeItem *firstParent = listOfItemsToDrop[0]->GetParent(); |
| 191 | QList<TreeItem *>::iterator diIter; |
| 192 | for (diIter = listOfItemsToDrop.begin() + 1; diIter != listOfItemsToDrop.end(); diIter++) |
| 193 | { |
| 194 | if (firstParent != (*diIter)->GetParent()) |
| 195 | { |
| 196 | isValidDragAndDropOperation = false; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |