diff --git a/sources/TerminalStrip/ui/terminalstriptreedockwidget.cpp b/sources/TerminalStrip/ui/terminalstriptreedockwidget.cpp index e6eddca94..3ec09f04f 100644 --- a/sources/TerminalStrip/ui/terminalstriptreedockwidget.cpp +++ b/sources/TerminalStrip/ui/terminalstriptreedockwidget.cpp @@ -26,7 +26,6 @@ #include "../realterminal.h" #include "../../qetgraphicsitem/terminalelement.h" #include "../terminalstrip.h" -#include "terminalstriptreewidget.h" TerminalStripTreeDockWidget::TerminalStripTreeDockWidget(QETProject *project, QWidget *parent) : QDockWidget(parent), @@ -41,8 +40,6 @@ TerminalStripTreeDockWidget::TerminalStripTreeDockWidget(QETProject *project, QW #else ui->m_tree_view->expandAll(); #endif - - setupUndoConnections(); } TerminalStripTreeDockWidget::~TerminalStripTreeDockWidget() @@ -112,10 +109,10 @@ QString TerminalStripTreeDockWidget::currentInstallation() const if (auto item = ui->m_tree_view->currentItem()) { - if (item->type() == TerminalStripTreeWidget::Location) { + if (item->type() == Location) { item = item->parent(); } - if (item->type() == TerminalStripTreeWidget::Installation) { + if (item->type() == Installation) { return item->data(0, Qt::DisplayRole).toString(); } } @@ -134,7 +131,7 @@ QString TerminalStripTreeDockWidget::currentLocation() const } if (auto item = ui->m_tree_view->currentItem()) { - if (item->type() == TerminalStripTreeWidget::Location) { + if (item->type() == Location) { return item->data(0, Qt::DisplayRole).toString(); } } @@ -157,8 +154,8 @@ void TerminalStripTreeDockWidget::setSelectedStrip(TerminalStrip *strip) { QSharedPointer TerminalStripTreeDockWidget::currentRealTerminal() const { if (auto item = ui->m_tree_view->currentItem()) { - if (item->type() == TerminalStripTreeWidget::Terminal) { - return m_uuid_terminal_H.value(item->data(0,TerminalStripTreeWidget::UUID_USER_ROLE).toUuid()); + if (item->type() == Terminal) { + return m_uuid_terminal_H.value(item->data(0,UUID_USER_ROLE).toUuid()); } } return QSharedPointer(); @@ -179,12 +176,12 @@ void TerminalStripTreeDockWidget::on_m_tree_view_currentItemChanged(QTreeWidgetI } TerminalStrip *strip_ = nullptr; - if (current->type() == TerminalStripTreeWidget::Strip) { + if (current->type() == Strip) { strip_ = m_item_strip_H.value(current); } - else if (current->type() == TerminalStripTreeWidget::Terminal + else if (current->type() == Terminal && current->parent() - && current->parent()->type() == TerminalStripTreeWidget::Strip) { + && current->parent()->type() == Strip) { strip_ = m_item_strip_H.value(current->parent()); } @@ -205,10 +202,10 @@ void TerminalStripTreeDockWidget::buildTree() } QStringList strl{title_}; - new QTreeWidgetItem(ui->m_tree_view, strl, TerminalStripTreeWidget::Root); + new QTreeWidgetItem(ui->m_tree_view, strl, Root); QStringList ftstrl(tr("Bornes indépendante")); - new QTreeWidgetItem(ui->m_tree_view, ftstrl, TerminalStripTreeWidget::FreeTerminal); + new QTreeWidgetItem(ui->m_tree_view, ftstrl, FreeTerminal); auto ts_vector = m_project->terminalStrip(); std::sort(ts_vector.begin(), ts_vector.end(), [](TerminalStrip *a, TerminalStrip *b) { @@ -242,7 +239,7 @@ QTreeWidgetItem* TerminalStripTreeDockWidget::addTerminalStrip(TerminalStrip *te } if (!inst_qtwi) { QStringList inst_strl{installation_str}; - inst_qtwi = new QTreeWidgetItem(root_item, inst_strl, TerminalStripTreeWidget::Installation); + inst_qtwi = new QTreeWidgetItem(root_item, inst_strl, Installation); } //Check if location already exist @@ -258,13 +255,13 @@ QTreeWidgetItem* TerminalStripTreeDockWidget::addTerminalStrip(TerminalStrip *te } if (!loc_qtwi) { QStringList loc_strl{location_str}; - loc_qtwi = new QTreeWidgetItem(inst_qtwi, loc_strl, TerminalStripTreeWidget::Location); + loc_qtwi = new QTreeWidgetItem(inst_qtwi, loc_strl, Location); } //Add the terminal strip QStringList name{terminal_strip->name()}; - auto strip_item = new QTreeWidgetItem(loc_qtwi, name, TerminalStripTreeWidget::Strip); - strip_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal_strip->uuid()); + auto strip_item = new QTreeWidgetItem(loc_qtwi, name, Strip); + strip_item->setData(0, UUID_USER_ROLE, terminal_strip->uuid()); strip_item->setIcon(0, QET::Icons::TerminalStrip); //Add child terminal of the strip @@ -282,8 +279,8 @@ QTreeWidgetItem* TerminalStripTreeDockWidget::addTerminalStrip(TerminalStrip *te text_.append(QStringLiteral(", ")).append(real_t->label()); } const auto real_t = phy_t->realTerminals().at(0); - auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(text_), TerminalStripTreeWidget::Terminal); - terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, phy_t->uuid()); + auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(text_), Terminal); + terminal_item->setData(0, UUID_USER_ROLE, phy_t->uuid()); terminal_item->setIcon(0, QET::Icons::ElementTerminal); } } @@ -318,66 +315,14 @@ void TerminalStripTreeDockWidget::addFreeTerminal() { QUuid uuid_ = terminal->uuid(); QStringList strl{terminal->actualLabel()}; - auto item = new QTreeWidgetItem(free_terminal_item, strl, TerminalStripTreeWidget::Terminal); - item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, uuid_.toString()); + auto item = new QTreeWidgetItem(free_terminal_item, strl, Terminal); + item->setData(0, UUID_USER_ROLE, uuid_.toString()); item->setIcon(0, QET::Icons::ElementTerminal); m_uuid_terminal_H.insert(uuid_, terminal->realTerminal()); } } -void TerminalStripTreeDockWidget::setupUndoConnections() -{ - connect(ui->m_tree_view, &TerminalStripTreeWidget::terminalAddedToStrip, this, - [=](QUuid terminal_uuid, QUuid strip_uuid) - { - auto terminal = m_uuid_terminal_H.value(terminal_uuid); - auto strip = m_uuid_strip_H.value(strip_uuid); - - if (!terminal || !strip) { - return; - } - - auto undo = new AddTerminalToStripCommand(terminal, strip); - m_project->undoStack()->push(undo); - }); - - connect(ui->m_tree_view, &TerminalStripTreeWidget::terminalMovedFromStripToStrip, this, - [=] (QUuid terminal_uuid, QUuid old_strip_uuid, QUuid new_strip_uuid) - { - auto old_strip = m_uuid_strip_H.value(old_strip_uuid); - auto new_strip = m_uuid_strip_H.value(new_strip_uuid); - - if (!old_strip || !new_strip) { - return; - } - auto terminal = old_strip->physicalTerminal(terminal_uuid); - if (!terminal) { - return; - } - - auto undo = new MoveTerminalCommand(terminal, old_strip, new_strip); - m_project->undoStack()->push(undo); - }); - - connect(ui->m_tree_view, &TerminalStripTreeWidget::terminalRemovedFromStrip, this, - [=] (QUuid terminal_uuid, QUuid old_strip_uuid) - { - auto strip_ = m_uuid_strip_H.value(old_strip_uuid); - if (!strip_) { - return; - } - - auto terminal_ = strip_->physicalTerminal(terminal_uuid); - if (!terminal_) { - return; - } - - auto undo = new RemoveTerminalFromStripCommand(terminal_, strip_); - m_project->undoStack()->push(undo); - }); -} - void TerminalStripTreeDockWidget::setCurrentStrip(TerminalStrip *strip) { m_current_strip = strip; diff --git a/sources/TerminalStrip/ui/terminalstriptreedockwidget.h b/sources/TerminalStrip/ui/terminalstriptreedockwidget.h index e1dfd08f6..586d212a1 100644 --- a/sources/TerminalStrip/ui/terminalstriptreedockwidget.h +++ b/sources/TerminalStrip/ui/terminalstriptreedockwidget.h @@ -33,6 +33,17 @@ namespace Ui { class TerminalStripTreeDockWidget : public QDockWidget { Q_OBJECT + private: + enum TreeWidgetType{ + Root, + Terminal, + FreeTerminal, + Installation, + Location, + Strip + }; + //Role used for data in QTreeWidgetItem + static constexpr int UUID_USER_ROLE{Qt::UserRole + 1}; public: explicit TerminalStripTreeDockWidget(QETProject *project, QWidget *parent = nullptr); @@ -56,7 +67,6 @@ class TerminalStripTreeDockWidget : public QDockWidget void buildTree(); QTreeWidgetItem* addTerminalStrip(TerminalStrip *terminal_strip); void addFreeTerminal(); - void setupUndoConnections(); void setCurrentStrip(TerminalStrip *strip); private: diff --git a/sources/TerminalStrip/ui/terminalstriptreedockwidget.ui b/sources/TerminalStrip/ui/terminalstriptreedockwidget.ui index f04b5df29..c0d19c3ea 100644 --- a/sources/TerminalStrip/ui/terminalstriptreedockwidget.ui +++ b/sources/TerminalStrip/ui/terminalstriptreedockwidget.ui @@ -16,10 +16,7 @@ - - - QAbstractItemView::InternalMove - + true @@ -36,13 +33,6 @@ - - - TerminalStripTreeWidget - QTreeWidget -
terminalstriptreewidget.h
-
-
diff --git a/sources/TerminalStrip/ui/terminalstriptreewidget.cpp b/sources/TerminalStrip/ui/terminalstriptreewidget.cpp deleted file mode 100644 index 17a184549..000000000 --- a/sources/TerminalStrip/ui/terminalstriptreewidget.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - Copyright 2006-2021 The QElectroTech Team - This file is part of QElectroTech. - - QElectroTech is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - QElectroTech is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with QElectroTech. If not, see . -*/ -#include "terminalstriptreewidget.h" -#include "../../qeticons.h" - -#include -#include -#include -#include - -TerminalStripTreeWidget::TerminalStripTreeWidget(QWidget *parent) : - QTreeWidget(parent) -{} - -QStringList TerminalStripTreeWidget::mimeTypes() const -{ - QStringList strl(QStringLiteral("application/x-qet-terminal-strip-tree-terminal-uuid")); - - return strl; -} - -void TerminalStripTreeWidget::startDrag(Qt::DropActions supportedActions) -{ - Q_UNUSED(supportedActions) - - auto item = currentItem(); - - if (!item || - item->type() != TerminalStripTreeWidget::Terminal) { - return; - } - - QDrag drag(this); - auto mime_data = new QMimeData(); - 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)); - drag.exec(Qt::MoveAction); -} - -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 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(); - - //Move terminal - if (dragged_item->parent()->type() == FreeTerminal && //From free to strip - overred_item->type() == Strip) { - event->accept(); - } - 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; - } - - auto old_parent = dragged_item->parent(); - old_parent->removeChild(dragged_item); - overred_item->addChild(dragged_item); - - //Move terminal - if (old_parent->type() == FreeTerminal && //From free to strip - overred_item->type() == Strip) { - emit terminalAddedToStrip(QUuid(dragged_item->data(0, UUID_USER_ROLE).toString()), - QUuid(overred_item->data(0, UUID_USER_ROLE).toString())); - } - else if (old_parent->type() == Strip) //From strip to ... - { - if (overred_item->type() == FreeTerminal) //Free terminal - { - emit terminalRemovedFromStrip(QUuid(dragged_item->data(0, UUID_USER_ROLE).toString()), - QUuid(old_parent->data(0, UUID_USER_ROLE).toString())); - } - else if (overred_item->type() == Strip) //To another strip - { - emit terminalMovedFromStripToStrip(QUuid(dragged_item->data(0, UUID_USER_ROLE).toString()), - QUuid(old_parent->data(0, UUID_USER_ROLE).toString()), - QUuid(overred_item->data(0, UUID_USER_ROLE).toString())); - } - } - -} - -Qt::DropActions TerminalStripTreeWidget::supportedDropActions() const { - return Qt::MoveAction; -} diff --git a/sources/TerminalStrip/ui/terminalstriptreewidget.h b/sources/TerminalStrip/ui/terminalstriptreewidget.h deleted file mode 100644 index d5b7fac1e..000000000 --- a/sources/TerminalStrip/ui/terminalstriptreewidget.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - Copyright 2006-2021 The QElectroTech Team - This file is part of QElectroTech. - - QElectroTech is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - QElectroTech is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with QElectroTech. If not, see . -*/ -#ifndef TERMINALSTRIPTREEWIDGET_H -#define TERMINALSTRIPTREEWIDGET_H - -#include -#include - -/** - * @brief The TerminalStripTreeWidget class - * Derived class use to implement custom drag and drop - */ -class TerminalStripTreeWidget : public QTreeWidget -{ - Q_OBJECT - public : - enum TreeWidgetType{ - Root, - Terminal, - FreeTerminal, - Installation, - Location, - Strip - }; - - //Role used for data in QTreeWidgetItem - static constexpr int UUID_USER_ROLE{Qt::UserRole + 1}; - - 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); - - protected: - QStringList mimeTypes() const override; - void startDrag(Qt::DropActions supportedActions) override; - void dragMoveEvent(QDragMoveEvent *event) override; - void dropEvent(QDropEvent *event) override; - Qt::DropActions supportedDropActions() const override; -}; - -#endif // TERMINALSTRIPTREEWIDGET_H