wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
TrashButton.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include <QDragEnterEvent>
7#include <QMessageBox>
8#include <QMimeData>
9#include <QVersionNumber>
10
12#include "App/Core/Enums.h"
13#include "App/Core/MimeTypes.h"
15
17 : QPushButton(parent)
18{
19 setAcceptDrops(true);
20}
21
22void TrashButton::dragEnterEvent(QDragEnterEvent *event)
23{
24 // Accept both the legacy MIME type and the current one so that IC files
25 // dragged from either old or new element panels are accepted
26 if (event->mimeData()->hasFormat(MimeType::DragDropLegacy)
27 || event->mimeData()->hasFormat(MimeType::DragDrop)) {
28 event->acceptProposedAction();
29 }
30}
31
32void TrashButton::dropEvent(QDropEvent *event)
33{
34 if (event->mimeData()->hasFormat(MimeType::DragDropLegacy)
35 || event->mimeData()->hasFormat(MimeType::DragDrop)) {
36 QByteArray itemData;
37
38 if (event->mimeData()->hasFormat(MimeType::DragDropLegacy)) {
39 itemData = event->mimeData()->data(MimeType::DragDropLegacy);
40 }
41
42 if (event->mimeData()->hasFormat(MimeType::DragDrop)) {
43 itemData = event->mimeData()->data(MimeType::DragDrop);
44 }
45
46 // Deserialise the drag payload to extract the IC file name and optional embedded fields
47 QDataStream stream(&itemData, QIODevice::ReadOnly);
49
50 const auto payload = readDragDropPayload(stream);
51
52 QMessageBox msgBox;
53 msgBox.setParent(this);
54 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
55 msgBox.setWindowModality(Qt::WindowModal);
56 msgBox.setDefaultButton(QMessageBox::No);
57
58 if (payload.isEmbedded) {
59 msgBox.setText(tr("Remove all \"%1\" instances from the circuit?").arg(payload.blobName));
60 } else {
61 msgBox.setText(tr("Remove this IC? Its file will be moved to the system trash and its instances deleted from the circuit."));
62 }
63
64 if (msgBox.exec() != QMessageBox::Yes) {
65 event->setAccepted(false);
66 return;
67 }
68
69 if (payload.isEmbedded) {
70 emit removeEmbeddedIC(payload.blobName);
71 } else {
72 emit removeICFile(payload.icFileName);
73 }
74 }
75}
DragDropPayload readDragDropPayload(QDataStream &stream)
Reads a drag-and-drop payload from stream.
Shared reader for wiRedPanda's drag-and-drop MIME payload format.
Central enumeration types for element types, groups, and signal status.
MIME type string constants for drag-and-drop operations.
Circuit and waveform file serialization/deserialization utilities.
TrashButton: a push-button that accepts drag-and-drop of IC files for deletion.
static QVersionNumber readPandaHeader(QDataStream &stream)
Reads and validates the .panda circuit file header; returns the stored version number.
void removeEmbeddedIC(const QString &blobName)
Removes all embedded IC instances with the given blobName from the scene.
void dropEvent(QDropEvent *event) override
void dragEnterEvent(QDragEnterEvent *event) override
TrashButton(QWidget *parent=nullptr)
Constructs the trash button with optional parent.
void removeICFile(const QString &icFileName)
Removes the IC file icFileName from disk after user confirmation.
constexpr const char * DragDropLegacy
Legacy drag-and-drop MIME type retained for backward compatibility.
Definition MimeTypes.h:15
constexpr const char * DragDrop
Current drag-and-drop MIME type for element palette → scene drops.
Definition MimeTypes.h:13