This commit is contained in:
Kellermorph
2026-04-26 10:48:47 +02:00
parent ee65142b65
commit 0118d94d4e
15 changed files with 669 additions and 90 deletions

View File

@@ -22,7 +22,12 @@
#include "../qeticons.h"
#include "elementcollectionitem.h"
#include "elementslocation.h"
#include "../qetproject.h"
#include "../diagram.h"
#include "xmlelementcollection.h"
#include "../nameslist.h"
#include <QPainter>
#include <QScopedPointer>
#include <QDrag>
#include <QStandardItemModel>
@@ -95,9 +100,107 @@ void ElementsTreeView::startElementDrag(const ElementsLocation &location)
if (location.isDirectory())
{
mime_data->setData("application/x-qet-category-uri",
location_str.toLatin1());
location_str.toLatin1());
drag->setPixmap(QET::Icons::Folder.pixmap(22, 22));
}
else if (location.fileName().endsWith(".qetmak"))
{
mime_data->setData("application/x-qet-element-uri", location_str.toLatin1());
QPixmap macro_pixmap;
// --- MINI-RENDERER FÜR DAS MAKRO-VORSCHAUBILD ---
QFile file(location.fileSystemPath());
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QDomDocument macro_doc;
if (macro_doc.setContent(&file)) {
QDomElement root = macro_doc.documentElement();
if (root.tagName() == "qet_macro") {
// 1. Unsichtbares Dummy-Projekt erstellen
QScopedPointer<QETProject> dummy_project(new QETProject());
// 2. Bauteile in das Dummy-Projekt laden (wie beim echten Drop)
QDomElement collection_node = root.firstChildElement("collection");
if (!collection_node.isNull()) {
QDomNodeList elements = collection_node.elementsByTagName("element");
for (int i = 0; i < elements.count(); ++i) {
QDomElement elmt_node = elements.at(i).toElement();
QString path = elmt_node.attribute("path");
QDomElement definition = elmt_node.firstChildElement("definition");
if (!path.isEmpty() && !definition.isNull()) {
int last_slash = path.lastIndexOf('/');
QString dir_path = (last_slash != -1) ? path.left(last_slash) : "";
QString file_name = (last_slash != -1) ? path.mid(last_slash + 1) : path;
if (!dir_path.isEmpty()) {
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QStringList parts = dir_path.split('/', QString::SkipEmptyParts);
#else
QStringList parts = dir_path.split('/', Qt::SkipEmptyParts);
#endif
QString current_path = "";
for (const QString &part : parts) {
QString parent_path = current_path;
if (!current_path.isEmpty()) current_path += "/";
current_path += part;
if (current_path == "import") continue;
NamesList empty_names;
dummy_project->embeddedElementCollection()->createDir(parent_path, part, empty_names);
}
}
dummy_project->embeddedElementCollection()->addElementDefinition(dir_path, file_name, definition);
}
}
}
Diagram *dummy_diagram = dummy_project->addNewDiagram();
// 4. Makro auf dem unsichtbaren Blatt zeichnen
QDomElement diagram_content_node = root.firstChildElement("diagram_content");
QDomElement diagram_node = diagram_content_node.firstChildElement("diagram");
if (!diagram_node.isNull()) {
QDomNodeList instances = diagram_node.elementsByTagName("element");
for (int i = 0; i < instances.count(); ++i) {
QDomElement inst = instances.at(i).toElement();
QString type = inst.attribute("type");
if (type.startsWith("macro://")) {
inst.setAttribute("type", type.replace("macro://", "embed://"));
}
}
dummy_diagram->fromXml(diagram_node, QPointF(0, 0), false, nullptr);
dummy_diagram->clearSelection();
// 5. "Screenshot" (Pixmap) von den gezeichneten Elementen machen
QRectF scene_rect = dummy_diagram->itemsBoundingRect();
if (!scene_rect.isEmpty()) {
scene_rect.adjust(-5, -5, 5, 5); // Kleiner Rand
macro_pixmap = QPixmap(scene_rect.size().toSize());
macro_pixmap.fill(Qt::transparent); // Transparenter Hintergrund
QPainter painter(&macro_pixmap);
painter.setRenderHint(QPainter::Antialiasing);
dummy_diagram->render(&painter, macro_pixmap.rect(), scene_rect);
}
}
}
}
}
if (macro_pixmap.isNull()) {
macro_pixmap = QET::Icons::Project.pixmap(32, 32);
}
// Bild verkleinern, falls das Makro gigantisch groß ist
if (macro_pixmap.width() > MAX_DND_PIXMAP_WIDTH || macro_pixmap.height() > MAX_DND_PIXMAP_HEIGHT) {
macro_pixmap = macro_pixmap.scaled(MAX_DND_PIXMAP_WIDTH, MAX_DND_PIXMAP_HEIGHT, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
drag->setPixmap(macro_pixmap);
// Bild zentriert an die Maus hängen
drag->setHotSpot(QPoint(macro_pixmap.width() / 2, macro_pixmap.height() / 2));
}
else if (location.isElement())
{
mime_data->setData("application/x-qet-element-uri",