/*
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 "terminalstripeditor.h"
#include "ui_terminalstripeditor.h"
#include "terminalstripcreatordialog.h"
#include "../../qetproject.h"
#include "../terminalstrip.h"
#include "../elementprovider.h"
#include "../qetgraphicsitem/terminalelement.h"
#include
/**
* @brief TerminalStripEditor::TerminalStripEditor
* @param project : Project to manage the terminal strip
* @param parent : paent widget
*/
TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) :
QDialog(parent),
ui(new Ui::TerminalStripEditor),
m_project(project)
{
ui->setupUi(this);
buildTree();
}
/**
* @brief TerminalStripEditor::~TerminalStripEditor
*/
TerminalStripEditor::~TerminalStripEditor() {
delete ui;
}
/**
* @brief TerminalStripEditor::buildTree
* Build the tree widget use to explore terminal strip
*/
void TerminalStripEditor::buildTree()
{
ui->m_terminal_strip_tw->clear();
auto title = m_project->title();
if (title.isEmpty()) {
title = tr("Projet sans titre");
}
QStringList strl{title};
new QTreeWidgetItem(ui->m_terminal_strip_tw, strl, TerminalStripEditor::Root);
QStringList ftstrl(tr("Bornes indépendante"));
new QTreeWidgetItem(ui->m_terminal_strip_tw, ftstrl, TerminalStripEditor::FreeTerminal);
const auto ts_vector = m_project->terminalStrip();
for (const auto ts : ts_vector) {
addTerminalStrip(ts);
}
addFreeTerminal();
}
/**
* @brief TerminalStripEditor::addTerminalStrip
* Add a new terminal strip to the list of displayed terminal strip
* in the tree widget
* @param terminal_strip
* @return the QTreeWidgetItem who represent the terminal strip
*/
QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_strip)
{
auto root_item = ui->m_terminal_strip_tw->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 ; ichildCount() ; ++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};
inst_qtwi = new QTreeWidgetItem(root_item, inst_strl, TerminalStripEditor::Inst);
}
//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 ; ichildCount() ; ++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};
loc_qtwi = new QTreeWidgetItem(inst_qtwi, loc_strl, TerminalStripEditor::Loc);
}
//Add the terminal strip
QStringList name{terminal_strip->name()};
return new QTreeWidgetItem(loc_qtwi, name, TerminalStripEditor::Strip);
}
/**
* @brief TerminalStripEditor::addFreeTerminal
* Add free terminal (aka terminal which not belong to a terminal strip)
* in the tree widget
*/
void TerminalStripEditor::addFreeTerminal()
{
ElementProvider ep(m_project);
auto vector_ = ep.freeTerminal();
if (vector_.isEmpty()) {
return;
}
//Sort the terminal element by label
std::sort(vector_.begin(), vector_.end(), [](TerminalElement *a, TerminalElement *b) {
return a->actualLabel() < b->actualLabel();
});
auto free_terminal_item = ui->m_terminal_strip_tw->topLevelItem(1);
for (const auto terminal : qAsConst(vector_))
{
QStringList strl{terminal->actualLabel()};
new QTreeWidgetItem(free_terminal_item, strl, TerminalStripEditor::FreeTerminal);
}
}
/**
* @brief TerminalStripEditor::on_m_add_terminal_strip_pb_clicked
* Action when user click on add terminal strip button
*/
void TerminalStripEditor::on_m_add_terminal_strip_pb_clicked()
{
QScopedPointer dialog(new TerminalStripCreatorDialog(m_project, this));
if (auto item = ui->m_terminal_strip_tw->currentItem())
{
if (item->type() == TerminalStripEditor::Strip) {
item = item->parent();
}
if (item->type() == TerminalStripEditor::Loc) {
dialog->setLocation(item->data(0, Qt::DisplayRole).toString());
item = item->parent();
}
if (item->type() == TerminalStripEditor::Inst) {
dialog->setInstallation(item->data(0, Qt::DisplayRole).toString());
}
}
if (dialog->exec() == QDialog::Accepted)
{
auto item = addTerminalStrip(dialog->generatedTerminalStrip());
ui->m_terminal_strip_tw->setCurrentItem(item);
}
}