2019-01-02 16:56:46 +00:00
|
|
|
/*
|
2025-01-04 13:37:40 +01:00
|
|
|
Copyright 2006-2025 The QElectroTech Team
|
2019-01-02 16:56:46 +00:00
|
|
|
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 "namelistwidget.h"
|
|
|
|
#include "ui_namelistwidget.h"
|
|
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QClipboard>
|
|
|
|
|
|
|
|
NameListWidget::NameListWidget(QWidget *parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
ui(new Ui::NameListWidget)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
ui->m_clipboard_cb->setHidden(true);
|
|
|
|
connect(ui->m_add_line_pb, &QPushButton::clicked, this, &NameListWidget::addLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
NameListWidget::~NameListWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief NameListWidget::addLine
|
|
|
|
Add a new line to the name list widget
|
|
|
|
*/
|
2019-01-02 16:56:46 +00:00
|
|
|
void NameListWidget::addLine()
|
|
|
|
{
|
|
|
|
clean();
|
|
|
|
if (m_read_only) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QTreeWidgetItem *qtwi = new QTreeWidgetItem();
|
|
|
|
qtwi->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
|
|
|
ui->m_tree->addTopLevelItem(qtwi);
|
|
|
|
ui->m_tree->setCurrentItem(qtwi);
|
|
|
|
ui->m_tree->editItem(qtwi);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief NameListWidget::setNames
|
2020-08-18 20:07:55 +02:00
|
|
|
Set the current names of this dialog from name_list
|
2020-08-16 11:19:36 +02:00
|
|
|
@param name_list
|
|
|
|
*/
|
2019-01-02 16:56:46 +00:00
|
|
|
void NameListWidget::setNames(const NamesList &name_list)
|
|
|
|
{
|
|
|
|
for (QString lang : name_list.langs())
|
|
|
|
{
|
|
|
|
QString value = name_list[lang];
|
|
|
|
QStringList values;
|
|
|
|
values << lang << value;
|
|
|
|
QTreeWidgetItem *qtwi = new QTreeWidgetItem(values);
|
|
|
|
if (!m_read_only) {
|
2020-08-18 20:08:32 +02:00
|
|
|
qtwi->setFlags(Qt::ItemIsEditable
|
|
|
|
| Qt::ItemIsEnabled
|
|
|
|
| Qt::ItemIsSelectable);
|
2019-01-02 16:56:46 +00:00
|
|
|
}
|
|
|
|
ui->m_tree->addTopLevelItem(qtwi);
|
|
|
|
ui->m_tree->sortItems(0, Qt::AscendingOrder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief NameListWidget::names
|
|
|
|
@return the current name list edited by this dialog
|
|
|
|
*/
|
2019-01-02 16:56:46 +00:00
|
|
|
NamesList NameListWidget::names() const
|
|
|
|
{
|
|
|
|
NamesList nl_;
|
|
|
|
|
|
|
|
int names_count = ui->m_tree->topLevelItemCount();
|
|
|
|
for (int i = 0 ; i < names_count ; ++ i)
|
|
|
|
{
|
|
|
|
QString lang = ui->m_tree->topLevelItem(i)->text(0);
|
|
|
|
QString value = ui->m_tree->topLevelItem(i)->text(1);
|
|
|
|
if (lang != "" && value != "") {
|
|
|
|
nl_.addName(lang, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nl_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief NameListWidget::setReadOnly
|
|
|
|
Set this dialog to read only or not.
|
|
|
|
@param ro
|
|
|
|
*/
|
2019-01-02 16:56:46 +00:00
|
|
|
void NameListWidget::setReadOnly(bool ro)
|
|
|
|
{
|
|
|
|
m_read_only = ro;
|
|
|
|
|
|
|
|
int names_count = ui->m_tree->topLevelItemCount() - 1;
|
|
|
|
for (int i = names_count ; i >= 0 ; -- i)
|
|
|
|
{
|
|
|
|
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
|
|
if (!m_read_only) {
|
|
|
|
flags |= Qt::ItemIsEditable;
|
|
|
|
}
|
|
|
|
ui->m_tree->topLevelItem(i)->setFlags(flags);
|
|
|
|
}
|
|
|
|
ui->m_add_line_pb->setEnabled(!ro);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief NameListWidget::isEmpty
|
|
|
|
@return true if empty.
|
|
|
|
An empty dialog, is a dialog without any edited lang.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
bool NameListWidget::isEmpty() const
|
|
|
|
{
|
2019-01-02 16:56:46 +00:00
|
|
|
return names().isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NameListWidget::setClipboardValue(QHash<QString, QString> value)
|
|
|
|
{
|
|
|
|
if (value.isEmpty()) {
|
|
|
|
ui->m_clipboard_cb->setHidden(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->m_clipboard_cb->setVisible(true);
|
|
|
|
|
|
|
|
QStringList list = value.keys();
|
|
|
|
list.sort();
|
|
|
|
for (QString key : list) {
|
|
|
|
ui->m_clipboard_cb->addItem(key, value.value(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief NameListWidget::clean
|
2022-12-04 08:21:12 -05:00
|
|
|
Clean the lists of names by removing the empty lines
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2019-01-02 16:56:46 +00:00
|
|
|
void NameListWidget::clean()
|
|
|
|
{
|
|
|
|
int names_count = ui->m_tree->topLevelItemCount() - 1;
|
|
|
|
for (int i = names_count ; i >= 0 ; -- i)
|
|
|
|
{
|
|
|
|
if (ui->m_tree->topLevelItem(i)->text(0).isEmpty() &&
|
|
|
|
ui->m_tree->topLevelItem(i)->text(1).isEmpty())
|
|
|
|
{
|
|
|
|
ui->m_tree->takeTopLevelItem(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NameListWidget::on_m_clipboard_cb_activated(int index)
|
|
|
|
{
|
|
|
|
Q_UNUSED(index);
|
|
|
|
|
|
|
|
QClipboard *clipboard = QApplication::clipboard();
|
|
|
|
clipboard->setText(ui->m_clipboard_cb->currentData().toString());
|
|
|
|
ui->m_clipboard_cb->setCurrentIndex(0);
|
|
|
|
}
|