UnicornEnums::ItemType DragMimeData::itemType() const { if (hasFormat("item/type")) return (UnicornEnums::ItemType) QString::fromUtf8(data("item/type")).toInt(); if (hasFormat("item/track")) return UnicornEnums::ItemTrack; if (hasFormat("item/album")) return UnicornEnums::ItemAlbum; if (hasFormat("item/artist")) //leave last as album and track have this data too return UnicornEnums::ItemArtist; return UnicornEnums::ItemUnknown; }
void pasteItem(QStandardItemModel *model, QStandardItem *item) { auto mime = qApp->clipboard()->mimeData(); if (mime->hasFormat(itemMimeType)) { auto newItem = new BrushPresetItem(); { auto data = mime->data(itemMimeType); QDataStream stream(&data, QIODevice::ReadOnly); int count; stream >> count; newItem->read(stream); } addItems(model, item, {newItem}); }
void GraphEditor::updateEnabledActions(void) { if (not this->isVisible()) return; getActionMap()["undo"]->setEnabled(_stateManager->isPreviousAvailable()); getActionMap()["redo"]->setEnabled(_stateManager->isSubsequentAvailable()); getActionMap()["save"]->setEnabled(not _stateManager->isCurrentSaved()); getActionMap()["reload"]->setEnabled(not this->getCurrentFilePath().isEmpty()); getActionMap()["activateTopology"]->setChecked(_isTopologyActive); //can we paste something from the clipboard? auto mimeData = QApplication::clipboard()->mimeData(); const bool canPaste = mimeData->hasFormat("text/json/pothos_object_array") and not mimeData->data("text/json/pothos_object_array").isEmpty(); getActionMap()["paste"]->setEnabled(canPaste); //update window title QString subtext = this->getCurrentFilePath(); if (subtext.isEmpty()) subtext = tr("untitled"); emit this->newTitleSubtext(tr("Editing ") + subtext); }
void Viewport::onPaste() { auto data = QApplication::clipboard()->mimeData(); if (data->hasFormat("sb::viewport")) { auto g = App::instance()->getGraph(); const uint64_t new_uid = g->getUIDs(1).front(); // Update this node's UID and store the change in uid_map auto n = QJsonDocument::fromJson( data->data("sb::viewport")).object(); n["uid"] = int(new_uid); auto name = n["name"].toString(); if (!g->isNameUnique(name.toStdString())) { // Trim trailing numbers from the node's name while (name.at(name.size() - 1).isNumber()) name = name.left(name.size() - 1); if (name.isEmpty()) name = "n"; // Then use the remaining string as a prefix n["name"] = QString::fromStdString(g->nextName(name.toStdString())); } // Deserialize this node SceneDeserializer::Info ds; SceneDeserializer::deserializeNode(n, g, &ds); // Update the inspector positions by shifting a bit down and over for (auto& i : ds.inspectors) i += QPointF(10, 10); App::instance()->getGraphScene()->setInspectorPositions(ds.inspectors); App::instance()->pushStack( new UndoAddNodeCommand(g->childNodes().back(), "'paste'")); } }
void GraphEditor::handlePaste(void) { if (not this->isVisible()) return; auto draw = this->getCurrentGraphDraw(); auto mimeData = QApplication::clipboard()->mimeData(); const bool canPaste = mimeData->hasFormat("text/json/pothos_object_array") and not mimeData->data("text/json/pothos_object_array").isEmpty(); if (not canPaste) return; //extract object array const auto data = mimeData->data("text/json/pothos_object_array"); const std::string dataStr(data.constData(), data.size()); std::istringstream iss(dataStr); Poco::JSON::Parser p; p.parse(iss); auto graphObjects = p.getHandler()->asVar().extract<Poco::JSON::Array::Ptr>(); assert(graphObjects); //rewrite ids std::map<std::string, std::string> oldIdToNew; for (size_t objIndex = 0; objIndex < graphObjects->size(); objIndex++) { const auto jGraphObj = graphObjects->getObject(objIndex); auto oldId = jGraphObj->getValue<std::string>("id"); oldIdToNew[oldId] = this->newId(QString::fromStdString(oldId)).toStdString(); } for (size_t objIndex = 0; objIndex < graphObjects->size();) { for (auto &pair : *graphObjects->getObject(objIndex)) { if (QString::fromStdString(pair.first).endsWith("id", Qt::CaseInsensitive)) { //if not in oldIdToNew, remove from list if (oldIdToNew.count(pair.second) == 0) { graphObjects->remove(objIndex); goto nextObj; } pair.second = oldIdToNew[pair.second]; } } objIndex++; nextObj: continue; } //unselect all objects draw->deselectAllObjs(); //create objects GraphObjectList objsToMove; objsToMove.append(handlePasteType(draw, graphObjects, "Block")); objsToMove.append(handlePasteType(draw, graphObjects, "Breaker")); handlePasteType(draw, graphObjects, "Connection"); //dont append, connection position doesnt matter objsToMove.append(handlePasteType(draw, graphObjects, "Widget")); //deal with initial positions of pasted objects QPointF cornerest(1e6, 1e6); for (auto obj : objsToMove) { cornerest.setX(std::min(cornerest.x(), obj->pos().x())); cornerest.setY(std::min(cornerest.y(), obj->pos().y())); } //determine an acceptable position to center the paste auto view = dynamic_cast<QGraphicsView *>(this->currentWidget()); auto pastePos = view->mapToScene(view->mapFromGlobal(QCursor::pos())); if (not view->sceneRect().contains(pastePos)) { pastePos = view->mapToScene(this->size().width()/2, this->size().height()/2); } //move objects into position for (auto obj : objsToMove) obj->setPos(obj->pos()-cornerest+pastePos); handleStateChange(GraphState("edit-paste", tr("Paste %1").arg(draw->getSelectionDescription()))); }