mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 12:40:35 +01:00
Enable drag and drop of terminal tree widget item
This commit is contained in:
@@ -48,6 +48,7 @@ class TerminalStrip : public QObject
|
||||
QString comment() const {return m_data.m_comment;}
|
||||
void setDescription(const QString &description);
|
||||
QString description() const {return m_data.m_description;}
|
||||
QUuid uuid() const {return m_data.m_uuid;}
|
||||
|
||||
bool addTerminal(Element *terminal);
|
||||
bool removeTerminal(Element *terminal);
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
#include "../properties/propertiesinterface.h"
|
||||
|
||||
#include <QUuid>
|
||||
|
||||
class TerminalStripData : public PropertiesInterface
|
||||
{
|
||||
friend class TerminalStrip;
|
||||
@@ -39,6 +41,7 @@ class TerminalStripData : public PropertiesInterface
|
||||
m_name,
|
||||
m_comment,
|
||||
m_description;
|
||||
QUuid m_uuid = QUuid::createUuid();
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "../qetgraphicsitem/terminalelement.h"
|
||||
#include "../UndoCommand/addterminalstripcommand.h"
|
||||
#include "terminalstriptreewidget.h"
|
||||
#include "../../qeticons.h"
|
||||
|
||||
#include <QTreeWidgetItem>
|
||||
|
||||
@@ -65,7 +66,7 @@ void TerminalStripEditor::buildTree()
|
||||
new QTreeWidgetItem(ui->m_terminal_strip_tw, strl, TerminalStripTreeWidget::Root);
|
||||
|
||||
QStringList ftstrl(tr("Bornes indépendante"));
|
||||
new QTreeWidgetItem(ui->m_terminal_strip_tw, ftstrl, TerminalStripTreeWidget::FreeTerminalSection);
|
||||
new QTreeWidgetItem(ui->m_terminal_strip_tw, ftstrl, TerminalStripTreeWidget::FreeTerminal);
|
||||
|
||||
auto ts_vector = m_project->terminalStrip();
|
||||
std::sort(ts_vector.begin(), ts_vector.end(), [](TerminalStrip *a, TerminalStrip *b) {
|
||||
@@ -106,7 +107,7 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s
|
||||
}
|
||||
if (!inst_qtwi) {
|
||||
QStringList inst_strl{installation_str};
|
||||
inst_qtwi = new QTreeWidgetItem(root_item, inst_strl, TerminalStripTreeWidget::Inst);
|
||||
inst_qtwi = new QTreeWidgetItem(root_item, inst_strl, TerminalStripTreeWidget::Installation);
|
||||
}
|
||||
|
||||
//Check if location already exist
|
||||
@@ -122,12 +123,14 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s
|
||||
}
|
||||
if (!loc_qtwi) {
|
||||
QStringList loc_strl{location_str};
|
||||
loc_qtwi = new QTreeWidgetItem(inst_qtwi, loc_strl, TerminalStripTreeWidget::Loc);
|
||||
loc_qtwi = new QTreeWidgetItem(inst_qtwi, loc_strl, TerminalStripTreeWidget::Location);
|
||||
}
|
||||
|
||||
//Add the terminal strip
|
||||
QStringList name{terminal_strip->name()};
|
||||
auto item = new QTreeWidgetItem(loc_qtwi, name, TerminalStripTreeWidget::Strip);
|
||||
item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal_strip->uuid());
|
||||
item->setIcon(0, QET::Icons::TerminalStrip);
|
||||
m_H_item_strip.insert(item, terminal_strip);
|
||||
return item;
|
||||
}
|
||||
@@ -156,8 +159,9 @@ void TerminalStripEditor::addFreeTerminal()
|
||||
for (const auto terminal : qAsConst(vector_))
|
||||
{
|
||||
QStringList strl{terminal->actualLabel()};
|
||||
auto item = new QTreeWidgetItem(free_terminal_item, strl, TerminalStripTreeWidget::FreeTerminal);
|
||||
auto item = new QTreeWidgetItem(free_terminal_item, strl, TerminalStripTreeWidget::Terminal);
|
||||
item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal->uuid().toString());
|
||||
item->setIcon(0, QET::Icons::ElementTerminal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,11 +178,11 @@ void TerminalStripEditor::on_m_add_terminal_strip_pb_clicked()
|
||||
if (item->type() == TerminalStripTreeWidget::Strip) {
|
||||
item = item->parent();
|
||||
}
|
||||
if (item->type() == TerminalStripTreeWidget::Loc) {
|
||||
if (item->type() == TerminalStripTreeWidget::Location) {
|
||||
dialog->setLocation(item->data(0, Qt::DisplayRole).toString());
|
||||
item = item->parent();
|
||||
}
|
||||
if (item->type() == TerminalStripTreeWidget::Inst) {
|
||||
if (item->type() == TerminalStripTreeWidget::Installation) {
|
||||
dialog->setInstallation(item->data(0, Qt::DisplayRole).toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
Copyright 2006-2021 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
@@ -31,7 +31,7 @@ TerminalStripTreeWidget::TerminalStripTreeWidget(QWidget *parent) :
|
||||
|
||||
QStringList TerminalStripTreeWidget::mimeTypes() const
|
||||
{
|
||||
QStringList strl(QStringLiteral("application/x-qet-terminal-strip-tree-item"));
|
||||
QStringList strl(QStringLiteral("application/x-qet-terminal-strip-tree-terminal-uuid"));
|
||||
|
||||
return strl;
|
||||
}
|
||||
@@ -43,13 +43,13 @@ void TerminalStripTreeWidget::startDrag(Qt::DropActions supportedActions)
|
||||
auto item = currentItem();
|
||||
|
||||
if (!item ||
|
||||
item->type() != TerminalStripTreeWidget::FreeTerminal) {
|
||||
item->type() != TerminalStripTreeWidget::Terminal) {
|
||||
return;
|
||||
}
|
||||
|
||||
QDrag drag(this);
|
||||
auto mime_data = new QMimeData();
|
||||
mime_data->setData("application/x-qet-terminal-strip-tree-item", item->data(0, UUID_USER_ROLE).toString().toLatin1());
|
||||
mime_data->setData("application/x-qet-terminal-strip-tree-terminal-uuid", item->data(0, UUID_USER_ROLE).toString().toLatin1());
|
||||
|
||||
drag.setMimeData(mime_data);
|
||||
drag.setPixmap(QET::Icons::ElementTerminal.pixmap(16,16));
|
||||
@@ -58,20 +58,81 @@ void TerminalStripTreeWidget::startDrag(Qt::DropActions supportedActions)
|
||||
|
||||
void TerminalStripTreeWidget::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
auto strl = event->mimeData()->formats();
|
||||
if (strl.size() != 1 ||
|
||||
strl.first() != "application/x-qet-terminal-strip-tree-terminal-uuid") {
|
||||
event->ignore();
|
||||
return;
|
||||
}
|
||||
//Accepted move are :
|
||||
//free terminal to terminal strip
|
||||
//terminal strip to another terminal strip
|
||||
//terminal strip to free terminal
|
||||
//All other other move is ignored
|
||||
QTreeWidget::dragMoveEvent(event);
|
||||
|
||||
auto item = itemAt(event->pos());
|
||||
auto overred_item = itemAt(event->pos());
|
||||
auto dragged_item = currentItem();
|
||||
if (!overred_item ||
|
||||
!dragged_item ||
|
||||
!dragged_item->parent()) {
|
||||
return;
|
||||
}
|
||||
//Ignore the event by default, we confirm it bellow if needed.
|
||||
event->ignore();
|
||||
|
||||
if (item &&
|
||||
item->type() == TerminalStripTreeWidget::Strip) {
|
||||
//Move terminal
|
||||
if (dragged_item->parent()->type() == FreeTerminal && //From free to strip
|
||||
overred_item->type() == Strip) {
|
||||
event->accept();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
else if (dragged_item->parent()->type() == Strip) //From strip to ...
|
||||
{
|
||||
if (overred_item->type() == FreeTerminal) { //Free terminal
|
||||
event->accept();
|
||||
} else if (overred_item->type() == Strip && //Another strip
|
||||
dragged_item->parent() != overred_item) {
|
||||
event->accept();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TerminalStripTreeWidget::dropEvent(QDropEvent *event)
|
||||
{}
|
||||
{
|
||||
auto overred_item = itemAt(event->pos());
|
||||
auto dragged_item = currentItem();
|
||||
if (!overred_item ||
|
||||
!dragged_item ||
|
||||
!dragged_item->parent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
dragged_item->parent()->removeChild(dragged_item);
|
||||
overred_item->addChild(dragged_item);
|
||||
|
||||
//Move terminal
|
||||
if (dragged_item->parent()->type() == FreeTerminal && //From free to strip
|
||||
overred_item->type() == Strip) {
|
||||
emit terminalAddedToStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()),
|
||||
QUuid::fromString(overred_item->data(0, UUID_USER_ROLE).toString()));
|
||||
}
|
||||
else if (dragged_item->parent()->type() == Strip) //From strip to ...
|
||||
{
|
||||
if (overred_item->type() == FreeTerminal) //Free terminal
|
||||
{
|
||||
emit terminalRemovedFromStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()),
|
||||
QUuid::fromString(overred_item->data(0, UUID_USER_ROLE).toString()));
|
||||
}
|
||||
else if (overred_item->type() == Strip && //Another strip
|
||||
dragged_item->parent() != overred_item)
|
||||
{
|
||||
emit terminalMovedFromStripToStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()),
|
||||
QUuid::fromString(dragged_item->parent()->data(0, UUID_USER_ROLE).toString()),
|
||||
QUuid::fromString(overred_item->data(0, UUID_USER_ROLE).toString()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Qt::DropActions TerminalStripTreeWidget::supportedDropActions() const {
|
||||
return Qt::MoveAction;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#define TERMINALSTRIPTREEWIDGET_H
|
||||
|
||||
#include <QTreeWidget>
|
||||
#include <QUuid>
|
||||
|
||||
/**
|
||||
* @brief The TerminalStripTreeWidget class
|
||||
@@ -30,16 +31,34 @@ class TerminalStripTreeWidget : public QTreeWidget
|
||||
public :
|
||||
enum TreeWidgetType{
|
||||
Root,
|
||||
Terminal,
|
||||
FreeTerminal,
|
||||
FreeTerminalSection,
|
||||
Inst,
|
||||
Loc,
|
||||
Installation,
|
||||
Location,
|
||||
Strip
|
||||
};
|
||||
|
||||
//Role used for data in QTreeWidgetItem
|
||||
static int UUID_USER_ROLE;
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief terminalAddedToStrip
|
||||
* Signal emited when a terminal is moved from free terminal to a terminals trip
|
||||
*/
|
||||
void terminalAddedToStrip(QUuid terminal_uuid, QUuid strip_uuid);
|
||||
/**
|
||||
* @brief terminalMovedFromStripToStrip
|
||||
* Signam emitted when a terminal is moved from from a terminal stip to another one
|
||||
*/
|
||||
void terminalMovedFromStripToStrip(QUuid terminal_uuid, QUuid old_strip_uuid, QUuid new_strip_uuid);
|
||||
/**
|
||||
* @brief terminalRemovedFromStrip
|
||||
* Signal emitted when a terminal is moved from a terminal strip to free terminal
|
||||
*/
|
||||
void terminalRemovedFromStrip(QUuid terminal_uuid, QUuid old_strip_uuid);
|
||||
|
||||
|
||||
public:
|
||||
TerminalStripTreeWidget(QWidget *parent = nullptr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user