void ScriptTableView::startDrag(Qt::DropActions supportedActions) { QModelIndexList indexes = selectedIndexes(); for(int i = indexes.count() - 1 ; i >= 0; --i) { const QModelIndex &index = indexes.at(i); if(!(index.flags() & Qt::ItemIsDragEnabled)) indexes.removeAt(i); } if(indexes.count() == 0) return; QMimeData *data = model()->mimeData(indexes); if(!data) return; QRect rect; QDrag *drag = new QDrag(this); drag->setMimeData(data); Qt::DropAction finalDefaultDropAction = Qt::IgnoreAction; if(defaultDropAction() != Qt::IgnoreAction && (supportedActions & defaultDropAction())) finalDefaultDropAction = defaultDropAction(); else if(supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove) finalDefaultDropAction = Qt::CopyAction; drag->exec(supportedActions, finalDefaultDropAction); }
QModelIndexList NBFolderView::getSelection() { QModelIndexList selectedList; selectedList << IconView->selection(); foreach( QModelIndex idx, selectedList ) if ( idx.column() ) selectedList.removeAt( selectedList.indexOf( idx ) ); return selectedList; };
void TagModel::deleteTags(QModelIndexList tags) { //make sure that if we delete a parent, we don't delete its child after cuz that won't exist for (int i = 0; i < tags.size(); /* happens internally */) { //when we remove at the top level, we don't need to increment because i //will now refer to the next value //only delete 0 indices if (tags[i].column() != 0) { tags.removeAt(i); continue; } Tag * parent = static_cast<Tag *>(tags[i].internalPointer()); if (parent->id() == 0) { tags.removeAt(i); continue; } else { //figure out which tags we should ditch QList<int> toRemove; for (int j = 0; j < tags.size(); j++) { Tag * child = static_cast<Tag *>(tags[j].internalPointer()); if (parent->id() == child->id()) continue; if (parent->hasChild(child, true)) toRemove.push_back(j); } //ditch them for (int j = toRemove.size() - 1; j >= 0; j--) { tags.removeAt(toRemove[j]); } //increment i++; } } for (int i = 0; i < tags.size(); i++) { beginRemoveRows(tags[i].parent(), tags[i].row(), tags[i].row()); Tag * tag = static_cast<Tag *>(tags[i].internalPointer()); mDB->tag_destroy(tag); endRemoveRows(); } }
void Desktopwidget::stackPages (void) { QModelIndexList list = _view->getSelectedListSource (); QModelIndex dest; // pick the first item as the destination if (list.size () >= 2) { dest = list [0]; list.removeAt (0); err_complain (_contents->stackItems (dest, list, 0)); } }
void OrderModel::addOrder() { if (OrderItem::groupsCount(groupsSelection->selectedIndexes()) && messagesSelection->selectedIndexes().count()) { beginInsertRows(QModelIndex(), 0, 0); QModelIndexList communities = groupsSelection->selectedIndexes(); for (int row = 0; row < communities.size(); ++row) { if (qvariant_cast<GroupItem*>(communities[row].data(GroupModel::GROUPITEM))->type() == GroupItem::GROUP) { qDebug() << qvariant_cast<GroupItem*>(communities[row].data(GroupModel::GROUPITEM))->getName(); communities.removeAt(row); } } orders.append(new OrderItem(messagesSelection->selectedIndexes(),communities)); endInsertRows(); } }
void TreeViewWidget::startDrag(Qt::DropActions pSupportedActions) { // This a reimplementation of QAbstractItemView::startDrag, so that we can // provide OpenCOR with a better pixmap for the drag object // Note: indeed, on Windows, the pixmap only shows the dragged item that are // visible in the QTreeView. Also, if there an item covers several // columns, then the pixmap will show several 'cells' some of them // empty if a column is empty, so... instead we want to provide a // generic pixmap which looks 'good' on all platforms... // Retrieve the selected draggable items, if any // Note: the following code is based on // QAbstractItemViewPrivate::selectedDraggableIndexes... QModelIndexList selectedDraggableIndexes = selectedIndexes(); for (int i = selectedDraggableIndexes.count()-1; i >= 0; --i) if ( !(model()->flags(selectedDraggableIndexes[i]) & Qt::ItemIsDragEnabled) || selectedDraggableIndexes[i].column()) // The current selected item is not draggable or is not in the first // column // Note: regarding the test on the column number, it is because we // may have a model data that requires several columns (e.g. // QFileSystemModel) in which case selectedIndexes would // return a number of indexes equal to the number of rows // times the number of columns while we only want a number of // indexes to be equal to the number of rows (since we have a // selection mode of QAbstractItemView::ExtendedSelection) selectedDraggableIndexes.removeAt(i); // Start the dragging action is there is at least one selected draggable // item if (selectedDraggableIndexes.count()) { // There is at least one selected draggable item, so create a QMimeData // object for it QMimeData *mimeData = model()->mimeData(selectedDraggableIndexes); if (!mimeData) return; // Create the pixmap that will be associated with the dragging action QPixmap pixmap((selectedDraggableIndexes.count() == 1)? ":oxygen/mimetypes/application-x-zerosize.png": ":oxygen/places/document-multiple.png"); // Create the drag object QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); drag->setPixmap(pixmap); drag->setHotSpot(QPoint(0.5*pixmap.width(), 0.5*pixmap.height())); // Do the dragging itself Qt::DropAction realDefaultDropAction = Qt::IgnoreAction; if ( (defaultDropAction() != Qt::IgnoreAction) && (pSupportedActions & defaultDropAction())) realDefaultDropAction = defaultDropAction(); else if ( (pSupportedActions & Qt::CopyAction) && (dragDropMode() != QAbstractItemView::InternalMove)) realDefaultDropAction = Qt::CopyAction; if (drag->exec(pSupportedActions, realDefaultDropAction) == Qt::MoveAction) { // We want to move the items // Note: the following code is based on // QAbstractItemViewPrivate::clearOrRemove... const QItemSelection selection = selectionModel()->selection(); if (!dragDropOverwriteMode()) { foreach (const QItemSelectionRange &itemSelectionRange, selection) { QModelIndex parent = itemSelectionRange.parent(); if (itemSelectionRange.left()) continue; if (itemSelectionRange.right() != (model()->columnCount(parent)-1)) continue; model()->removeRows(itemSelectionRange.top(), itemSelectionRange.bottom()-itemSelectionRange.top()+1, parent); } } else {
QModelIndexList FileOrganiserWidget::cleanIndexList(const QModelIndexList &pIndexes) const { // A list of indexes may contain indexes that are not relevant or // effectively the duplicate of another existing index, so... QModelIndexList res; // If both the index of a folder and some (if not all) of its (in)direct // contents is in the original list, then we only keep track of the index of // the folder for (int i = 0, iMax = pIndexes.count(); i < iMax; ++i) { // Check whether one of the current index's parents is already in the // list. If so, then skip the current index QModelIndex crtIndex = pIndexes[i]; if (!parentIndexExists(crtIndex, pIndexes)) { // None of the index's parents is in the list, so we can safely add // the index to the list res << crtIndex; // If the index refers to a folder, then we must double check that // the list of cleaned indexes doesn't contain any of the index's // children. If it does, then we must remove all of them QStandardItem *crtItem = mModel->itemFromIndex(crtIndex); if (crtItem && crtItem->data(Item::Folder).toBool()) for (int j = res.count()-1; j >= 0; --j) if (parentIndexExists(res[j], res)) res.removeAt(j); } } // At this stage, we have indexes for folders that are unique, but we may // still have indexes for files that are effectively the duplicate of // another file, so these are to be removed from the cleaned list and from // the model for (int i = res.count()-1; i >= 0; --i) { QStandardItem *crtItem = mModel->itemFromIndex(res[i]); if (crtItem && !crtItem->data(Item::Folder).toBool()) // The index corresponds to a valid file item, so check whether in // the cleaned list there is another file item referencing the same // physical file and, if so, remove it from the cleaned list and the // model for (int j = 0; j < i; ++j) { QStandardItem *testItem = mModel->itemFromIndex(res[j]); if ( testItem && !testItem->data(Item::Folder).toBool() && !crtItem->data(Item::Path).toString().compare(testItem->data(Item::Path).toString())) { // The test item is a valid file item and references the // same physical file as our current item, so remove the // current item from the cleaned list res.removeAt(i); // Also remove the current item from the model crtItem->parent()->removeRow(crtItem->row()); // Go to our next current item break; } } } // We are all done, so... return res; }
void MacroTreeView::startDrag(Qt::DropActions supportedActions) { QModelIndexList indexes = selectionModel()->selectedRows(); for(int i = indexes.count() - 1 ; i >= 0; --i) { if (!(indexes.at(i).isValid()) || !(model()->flags(indexes.at(i)) & Qt::ItemIsDragEnabled)) { indexes.removeAt(i); } } if (indexes.count() > 0) { QMimeData *data = model()->mimeData(indexes); if (!data) { return; } QPixmap dragPixmap(":/icons/resources/macro.png"); QModelIndex srcIndex = dynamic_cast<QSortFilterProxyModel*>(model())->mapToSource(indexes[0]); const ModelItemMacro* item = static_cast<const ModelItemMacro*>(srcIndex.internalPointer()); graph::Vertex::Ptr dummyVertex = app::MacroManager::instance().createVertexInstance(item->macro().signature()); if (!dummyVertex.isNull()) { graph::VertexItem::Ptr vertexItem = dummyVertex->sceneItem().staticCast<graph::VertexItem>(); if (!vertexItem.isNull()) { dragPixmap = vertexItem->paintToPixmap(); } app::MacroManager::instance().deleteVertexInstance(dummyVertex); } data->setImageData(dragPixmap.toImage()); QDrag* dragObjectPtr = new QDrag(this); dragObjectPtr->setPixmap(dragPixmap); dragObjectPtr->setMimeData(data); dragObjectPtr->setHotSpot(dragPixmap.rect().center()); Qt::DropAction defDropAction = Qt::IgnoreAction; if (defaultDropAction() != Qt::IgnoreAction && (supportedActions & defaultDropAction())) { defDropAction = defaultDropAction(); } else if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove) { defDropAction = Qt::CopyAction; } if (dragObjectPtr->exec(supportedActions, defDropAction) == Qt::MoveAction) { // clear or remove dragged items const QItemSelection selection = selectionModel()->selection(); QList<QItemSelectionRange>::const_iterator it = selection.constBegin(); if (!dragDropOverwriteMode()) { for (; it != selection.constEnd(); ++it) { QModelIndex parent = (*it).parent(); if ((*it).left() != 0) continue; if ((*it).right() != (model()->columnCount(parent) - 1)) continue; int count = (*it).bottom() - (*it).top() + 1; model()->removeRows((*it).top(), count, parent); } } else { QModelIndexList list = selection.indexes(); for (int i=0; i < list.size(); ++i) { QModelIndex index = list.at(i); QMap<int, QVariant> roles = model()->itemData(index); for (QMap<int, QVariant>::Iterator it = roles.begin(); it != roles.end(); ++it) { it.value() = QVariant(); } model()->setItemData(index, roles); } } } } }