Skip to content

Commit e2598b5

Browse files
committed
Added component button drag and drop to add component at a position in the scene
1 parent 6cc9127 commit e2598b5

File tree

6 files changed

+99
-7
lines changed

6 files changed

+99
-7
lines changed

SoundGenerator.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ SOURCES += \
5555
UI/pinoutputitem.cpp \
5656
UI/pininputitem.cpp \
5757
wavformat.cpp \
58-
UI/lineeditqreal.cpp
58+
UI/lineeditqreal.cpp \
59+
UI/pushordragbutton.cpp
5960

6061
HEADERS += \
6162
Components/AddComponent.h \
@@ -86,7 +87,8 @@ HEADERS += \
8687
UI/pinoutputitem.h \
8788
UI/pininputitem.h \
8889
wavformat.h \
89-
UI/lineeditqreal.h
90+
UI/lineeditqreal.h \
91+
UI/pushordragbutton.h
9092

9193
FORMS += \
9294
UI/mainwindow.ui

UI/mainwindow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "waveformview.h"
2727
#include "pininputitem.h"
2828
#include "lineeditqreal.h"
29+
#include "pushordragbutton.h"
2930
#include <QStyle>
3031
#include <utils.h>
3132

@@ -97,7 +98,7 @@ MainWindow::MainWindow(QWidget *parent) :
9798
QAction* action = new QAction(component, this);
9899
connect(action, &QAction::triggered, [this, component]() { ui->nodalView->createComponent(component); });
99100

100-
QPushButton* button = new QPushButton(component);
101+
PushOrDragButton* button = new PushOrDragButton(component);
101102
button->setProperty("class", "component");
102103
ui->buttonLayout->addWidget(button);
103104
connect(button, SIGNAL(clicked()), action, SLOT(trigger()));
@@ -373,7 +374,6 @@ void MainWindow::about()
373374

374375
box.setTextFormat(Qt::RichText);
375376
box.exec();
376-
377377
}
378378

379379
void MainWindow::exportWAV()

UI/nodalview.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,32 @@ void NodalView::mouseMoveEvent(QMouseEvent* event)
326326

327327
void NodalView::dragEnterEvent(QDragEnterEvent *event)
328328
{
329-
Q_UNUSED(event);
329+
if(event->mimeData()->hasFormat("add/component"))
330+
{
331+
event->acceptProposedAction();
332+
}
330333
}
331334

332335
void NodalView::dropEvent(QDropEvent *event)
336+
{
337+
event->acceptProposedAction();
338+
339+
QString componentName = QString::fromUtf8(event->mimeData()->data("add/component"));
340+
341+
m_nextCreationPosition = mapToScene(event->pos());
342+
createNode(componentName);
343+
}
344+
345+
// must be implemented
346+
void NodalView::dragMoveEvent(QDragMoveEvent *event)
333347
{
334348
Q_UNUSED(event);
335-
//m_nextCreationPosition = mapToScene(event->pos());
349+
}
336350

337-
//const QMimeData* data = event->mimeData();
351+
// must be implemented
352+
void NodalView::dragLeaveEvent(QDragLeaveEvent *event)
353+
{
354+
Q_UNUSED(event);
338355
}
339356

340357
void NodalView::drawBackground(QPainter *painter, const QRectF &rect)

UI/nodalview.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class NodalView : public QGraphicsView
5959
virtual void mouseReleaseEvent(QMouseEvent *event) override;
6060
virtual void mouseMoveEvent(QMouseEvent* event) override;
6161
virtual void dragEnterEvent(QDragEnterEvent* event) override;
62+
virtual void dragMoveEvent(QDragMoveEvent* event) override;
63+
virtual void dragLeaveEvent(QDragLeaveEvent* event) override;
6264
virtual void dropEvent(QDropEvent* event) override;
6365

6466
virtual void drawBackground(QPainter * painter, const QRectF & rect) override;

UI/pushordragbutton.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "pushordragbutton.h"
2+
#include <QApplication>
3+
#include <QDrag>
4+
#include <QMimeData>
5+
#include <qevent.h>
6+
#include <QtCore>
7+
8+
PushOrDragButton::PushOrDragButton(QWidget *parent)
9+
: QPushButton(parent)
10+
{ }
11+
12+
PushOrDragButton::PushOrDragButton(const QString &text, QWidget *parent)
13+
: QPushButton(text, parent)
14+
{ }
15+
16+
PushOrDragButton::PushOrDragButton(const QIcon &icon, const QString &text, QWidget *parent)
17+
: QPushButton(icon, text, parent)
18+
{ }
19+
20+
void PushOrDragButton::mousePressEvent(QMouseEvent *event)
21+
{
22+
QPushButton::mousePressEvent(event);
23+
24+
if (event->button() == Qt::LeftButton)
25+
m_dragStartPosition = event->pos();
26+
qDebug() << "click";
27+
}
28+
29+
void PushOrDragButton::mouseMoveEvent(QMouseEvent *event)
30+
{
31+
QPushButton::mouseMoveEvent(event);
32+
33+
if (!(event->buttons() & Qt::LeftButton))
34+
return;
35+
if ((event->pos() - m_dragStartPosition).manhattanLength()
36+
< QApplication::startDragDistance())
37+
return;
38+
39+
qDebug() << "drag";
40+
41+
QDrag *drag = new QDrag(this);
42+
QMimeData *mimeData = new QMimeData();
43+
44+
mimeData->setData("add/component", text().toUtf8());
45+
drag->setMimeData(mimeData);
46+
47+
drag->exec(Qt::MoveAction);
48+
49+
setDown(false);
50+
}

UI/pushordragbutton.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef PUSHORDRAGBUTTON_H
2+
#define PUSHORDRAGBUTTON_H
3+
4+
#include <QPushButton>
5+
6+
class PushOrDragButton : public QPushButton
7+
{
8+
public:
9+
PushOrDragButton(QWidget *parent = nullptr);
10+
PushOrDragButton(const QString &text, QWidget *parent = nullptr);
11+
PushOrDragButton(const QIcon &icon, const QString &text, QWidget *parent = nullptr);
12+
13+
protected:
14+
virtual void mousePressEvent(QMouseEvent* event) override;
15+
virtual void mouseMoveEvent(QMouseEvent* event) override;
16+
17+
private:
18+
QPointF m_dragStartPosition;
19+
};
20+
21+
#endif // PUSHORDRAGBUTTON_H

0 commit comments

Comments
 (0)