2022-03-14 17:56:18 +01:00
|
|
|
/*
|
|
|
|
Copyright 2006-2022 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "terminalstriptreedockwidget.h"
|
|
|
|
#include "ui_terminalstriptreedockwidget.h"
|
|
|
|
|
|
|
|
#include "../UndoCommand/addterminaltostripcommand.h"
|
|
|
|
#include "../../elementprovider.h"
|
|
|
|
#include "../physicalterminal.h"
|
|
|
|
#include "../../qeticons.h"
|
|
|
|
#include "../../qetproject.h"
|
|
|
|
#include "../realterminal.h"
|
|
|
|
#include "../../qetgraphicsitem/terminalelement.h"
|
|
|
|
#include "../terminalstrip.h"
|
2022-06-22 17:34:05 +02:00
|
|
|
#include "../../qetinformation.h"
|
2022-03-14 17:56:18 +01:00
|
|
|
|
|
|
|
TerminalStripTreeDockWidget::TerminalStripTreeDockWidget(QETProject *project, QWidget *parent) :
|
|
|
|
QDockWidget(parent),
|
|
|
|
ui(new Ui::TerminalStripTreeDockWidget),
|
|
|
|
m_project(project)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
buildTree();
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
|
|
|
|
ui->m_tree_view->expandRecursively(ui->m_tree_view->rootIndex());
|
|
|
|
#else
|
|
|
|
ui->m_tree_view->expandAll();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TerminalStripTreeDockWidget::~TerminalStripTreeDockWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:58:36 +01:00
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::reload
|
|
|
|
*/
|
|
|
|
void TerminalStripTreeDockWidget::reload()
|
|
|
|
{
|
|
|
|
auto current_ = m_current_strip;
|
|
|
|
|
|
|
|
ui->m_tree_view->clear();
|
|
|
|
m_item_strip_H.clear();
|
|
|
|
m_uuid_terminal_H.clear();
|
|
|
|
m_uuid_strip_H.clear();
|
|
|
|
|
2022-04-08 20:48:32 +02:00
|
|
|
for (const auto &connection_ : qAsConst(m_strip_changed_connection)) {
|
|
|
|
disconnect(connection_);
|
|
|
|
}
|
|
|
|
m_strip_changed_connection.clear();
|
|
|
|
|
2022-03-16 18:58:36 +01:00
|
|
|
|
|
|
|
buildTree();
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
|
|
|
|
ui->m_tree_view->expandRecursively(ui->m_tree_view->rootIndex());
|
|
|
|
#else
|
2022-04-09 16:34:30 +02:00
|
|
|
ui->m_tree_view->expandAll();
|
2022-03-16 18:58:36 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//Reselect the tree widget item of the current edited strip
|
|
|
|
auto item = m_item_strip_H.key(current_);
|
|
|
|
if (item) {
|
|
|
|
ui->m_tree_view->setCurrentItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::currentIsStrip
|
|
|
|
* @return true if the current selected item is a terminal strip.
|
|
|
|
*/
|
|
|
|
bool TerminalStripTreeDockWidget::currentIsStrip() const {
|
|
|
|
return m_item_strip_H.contains(ui->m_tree_view->currentItem());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::currentStrip
|
|
|
|
* @return The current selected strip or nullptr if there is
|
|
|
|
* no strip selected;
|
|
|
|
*/
|
|
|
|
TerminalStrip *TerminalStripTreeDockWidget::currentStrip() const {
|
|
|
|
return m_current_strip;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::currentInstallation
|
|
|
|
* @return the installation according to the current selection
|
|
|
|
*/
|
|
|
|
QString TerminalStripTreeDockWidget::currentInstallation() const
|
|
|
|
{
|
|
|
|
if (m_current_strip) {
|
|
|
|
return m_current_strip->installation();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto item = ui->m_tree_view->currentItem())
|
|
|
|
{
|
2022-05-04 18:38:45 +02:00
|
|
|
if (item->type() == Location) {
|
2022-03-16 18:58:36 +01:00
|
|
|
item = item->parent();
|
|
|
|
}
|
2022-05-04 18:38:45 +02:00
|
|
|
if (item->type() == Installation) {
|
2022-03-16 18:58:36 +01:00
|
|
|
return item->data(0, Qt::DisplayRole).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::currentLocation
|
|
|
|
* @return the location according to the current selection
|
|
|
|
*/
|
|
|
|
QString TerminalStripTreeDockWidget::currentLocation() const
|
|
|
|
{
|
|
|
|
if (m_current_strip) {
|
|
|
|
return m_current_strip->location();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto item = ui->m_tree_view->currentItem()) {
|
2022-05-04 18:38:45 +02:00
|
|
|
if (item->type() == Location) {
|
2022-03-16 18:58:36 +01:00
|
|
|
return item->data(0, Qt::DisplayRole).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::setSelectedStrip
|
|
|
|
* @param strip
|
|
|
|
*/
|
|
|
|
void TerminalStripTreeDockWidget::setSelectedStrip(TerminalStrip *strip) {
|
|
|
|
ui->m_tree_view->setCurrentItem(m_item_strip_H.key(strip));
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:44:08 +01:00
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::currentRealTerminal
|
|
|
|
* @return the current real terminal or a null QSharedPointer.
|
|
|
|
*/
|
|
|
|
QSharedPointer<RealTerminal> TerminalStripTreeDockWidget::currentRealTerminal() const
|
|
|
|
{
|
|
|
|
if (auto item = ui->m_tree_view->currentItem()) {
|
2022-05-04 18:38:45 +02:00
|
|
|
if (item->type() == Terminal) {
|
|
|
|
return m_uuid_terminal_H.value(item->data(0,UUID_USER_ROLE).toUuid());
|
2022-03-16 22:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QSharedPointer<RealTerminal>();
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:58:36 +01:00
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::on_m_tree_view_currentItemChanged
|
|
|
|
* @param current
|
|
|
|
* @param previous
|
|
|
|
*/
|
|
|
|
void TerminalStripTreeDockWidget::on_m_tree_view_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
|
|
|
|
{
|
|
|
|
Q_UNUSED(previous)
|
|
|
|
|
|
|
|
if (!current) {
|
|
|
|
setCurrentStrip(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TerminalStrip *strip_ = nullptr;
|
2022-05-04 18:38:45 +02:00
|
|
|
if (current->type() == Strip) {
|
2022-03-16 18:58:36 +01:00
|
|
|
strip_ = m_item_strip_H.value(current);
|
|
|
|
}
|
2022-05-04 18:38:45 +02:00
|
|
|
else if (current->type() == Terminal
|
2022-03-16 18:58:36 +01:00
|
|
|
&& current->parent()
|
2022-05-04 18:38:45 +02:00
|
|
|
&& current->parent()->type() == Strip) {
|
2022-03-16 18:58:36 +01:00
|
|
|
strip_ = m_item_strip_H.value(current->parent());
|
|
|
|
}
|
2022-03-30 20:30:57 +02:00
|
|
|
|
|
|
|
if (strip_ != m_current_strip) {
|
|
|
|
setCurrentStrip(strip_);
|
|
|
|
}
|
2022-03-16 18:58:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:56:18 +01:00
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::buildTree
|
|
|
|
*/
|
|
|
|
void TerminalStripTreeDockWidget::buildTree()
|
|
|
|
{
|
|
|
|
|
|
|
|
auto title_ = m_project->title();
|
|
|
|
if (title_.isEmpty()) {
|
|
|
|
title_ = tr("Projet sans titre");
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList strl{title_};
|
2022-05-04 18:38:45 +02:00
|
|
|
new QTreeWidgetItem(ui->m_tree_view, strl, Root);
|
2022-03-14 17:56:18 +01:00
|
|
|
|
|
|
|
QStringList ftstrl(tr("Bornes indépendante"));
|
2022-05-04 18:38:45 +02:00
|
|
|
new QTreeWidgetItem(ui->m_tree_view, ftstrl, FreeTerminal);
|
2022-03-14 17:56:18 +01:00
|
|
|
|
|
|
|
auto ts_vector = m_project->terminalStrip();
|
|
|
|
std::sort(ts_vector.begin(), ts_vector.end(), [](TerminalStrip *a, TerminalStrip *b) {
|
|
|
|
return a->name() < b->name();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &ts : qAsConst(ts_vector)) {
|
|
|
|
addTerminalStrip(ts);
|
|
|
|
}
|
|
|
|
addFreeTerminal();
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeWidgetItem* TerminalStripTreeDockWidget::addTerminalStrip(TerminalStrip *terminal_strip)
|
|
|
|
{
|
|
|
|
if (auto item = m_item_strip_H.key(terminal_strip)) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto root_item = ui->m_tree_view->topLevelItem(0);
|
|
|
|
|
|
|
|
//Check if installation already exist
|
|
|
|
//if not create a new one
|
|
|
|
auto installation_str = terminal_strip->installation();
|
|
|
|
QTreeWidgetItem *inst_qtwi = nullptr;
|
|
|
|
for (int i = 0 ; i<root_item->childCount() ; ++i) {
|
|
|
|
auto child_inst = root_item->child(i);
|
|
|
|
if (child_inst->data(0, Qt::DisplayRole).toString() == installation_str) {
|
|
|
|
inst_qtwi = child_inst;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inst_qtwi) {
|
|
|
|
QStringList inst_strl{installation_str};
|
2022-05-04 18:38:45 +02:00
|
|
|
inst_qtwi = new QTreeWidgetItem(root_item, inst_strl, Installation);
|
2022-03-14 17:56:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check if location already exist
|
|
|
|
//if not create a new one
|
|
|
|
auto location_str = terminal_strip->location();
|
|
|
|
QTreeWidgetItem *loc_qtwi = nullptr;
|
|
|
|
for (int i = 0 ; i<inst_qtwi->childCount() ; ++i) {
|
|
|
|
auto child_loc = inst_qtwi->child(i);
|
|
|
|
if (child_loc->data(0, Qt::DisplayRole).toString() == location_str) {
|
|
|
|
loc_qtwi = child_loc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!loc_qtwi) {
|
|
|
|
QStringList loc_strl{location_str};
|
2022-05-04 18:38:45 +02:00
|
|
|
loc_qtwi = new QTreeWidgetItem(inst_qtwi, loc_strl, Location);
|
2022-03-14 17:56:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Add the terminal strip
|
|
|
|
QStringList name{terminal_strip->name()};
|
2022-05-04 18:38:45 +02:00
|
|
|
auto strip_item = new QTreeWidgetItem(loc_qtwi, name, Strip);
|
|
|
|
strip_item->setData(0, UUID_USER_ROLE, terminal_strip->uuid());
|
2022-03-14 17:56:18 +01:00
|
|
|
strip_item->setIcon(0, QET::Icons::TerminalStrip);
|
|
|
|
|
|
|
|
//Add child terminal of the strip
|
|
|
|
for (auto i=0 ; i<terminal_strip->physicalTerminalCount() ; ++i)
|
|
|
|
{
|
|
|
|
auto phy_t = terminal_strip->physicalTerminal(i);
|
|
|
|
if (phy_t->realTerminalCount())
|
|
|
|
{
|
|
|
|
QString text_;
|
|
|
|
for (const auto &real_t : phy_t->realTerminals())
|
|
|
|
{
|
|
|
|
if (text_.isEmpty())
|
|
|
|
text_ = real_t->label();
|
|
|
|
else
|
|
|
|
text_.append(QStringLiteral(", ")).append(real_t->label());
|
|
|
|
}
|
|
|
|
const auto real_t = phy_t->realTerminals().at(0);
|
2022-05-04 18:38:45 +02:00
|
|
|
auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(text_), Terminal);
|
|
|
|
terminal_item->setData(0, UUID_USER_ROLE, phy_t->uuid());
|
2022-03-14 17:56:18 +01:00
|
|
|
terminal_item->setIcon(0, QET::Icons::ElementTerminal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_item_strip_H.insert(strip_item, terminal_strip);
|
|
|
|
m_uuid_strip_H.insert(terminal_strip->uuid(), terminal_strip);
|
2022-04-08 20:48:32 +02:00
|
|
|
|
|
|
|
m_strip_changed_connection.append(connect(terminal_strip, &TerminalStrip::orderChanged, this, &TerminalStripTreeDockWidget::reload));
|
2022-03-14 17:56:18 +01:00
|
|
|
return strip_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief TerminalStripTreeDockWidget::addFreeTerminal
|
|
|
|
*/
|
|
|
|
void TerminalStripTreeDockWidget::addFreeTerminal()
|
|
|
|
{
|
|
|
|
ElementProvider ep(m_project);
|
|
|
|
auto vector_ = ep.freeTerminal();
|
|
|
|
|
|
|
|
if (vector_.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Sort the terminal element by label
|
2022-06-22 17:34:05 +02:00
|
|
|
std::sort(vector_.begin(), vector_.end(), [](TerminalElement *a, TerminalElement *b)
|
|
|
|
{
|
|
|
|
return a->elementData().m_informations.value(QETInformation::ELMT_LABEL).toString()
|
|
|
|
<
|
|
|
|
b->elementData().m_informations.value(QETInformation::ELMT_LABEL).toString();
|
2022-03-14 17:56:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
auto free_terminal_item = ui->m_tree_view->topLevelItem(1);
|
|
|
|
|
|
|
|
for (const auto terminal : qAsConst(vector_))
|
|
|
|
{
|
|
|
|
QUuid uuid_ = terminal->uuid();
|
|
|
|
QStringList strl{terminal->actualLabel()};
|
2022-05-04 18:38:45 +02:00
|
|
|
auto item = new QTreeWidgetItem(free_terminal_item, strl, Terminal);
|
|
|
|
item->setData(0, UUID_USER_ROLE, uuid_.toString());
|
2022-03-14 17:56:18 +01:00
|
|
|
item->setIcon(0, QET::Icons::ElementTerminal);
|
|
|
|
|
|
|
|
m_uuid_terminal_H.insert(uuid_, terminal->realTerminal());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:58:36 +01:00
|
|
|
void TerminalStripTreeDockWidget::setCurrentStrip(TerminalStrip *strip)
|
|
|
|
{
|
|
|
|
m_current_strip = strip;
|
2022-03-20 18:25:25 +01:00
|
|
|
emit currentStripChanged(strip);
|
2022-03-16 18:58:36 +01:00
|
|
|
}
|