QElectroTech  0.70
namelistwidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "namelistwidget.h"
19 #include "ui_namelistwidget.h"
20 
21 #include <QPushButton>
22 #include <QClipboard>
23 
25  QWidget(parent),
26  ui(new Ui::NameListWidget)
27 {
28  ui->setupUi(this);
29  ui->m_clipboard_cb->setHidden(true);
30  connect(ui->m_add_line_pb, &QPushButton::clicked, this, &NameListWidget::addLine);
31 }
32 
34 {
35  delete ui;
36 }
37 
43 {
44  clean();
45  if (m_read_only) {
46  return;
47  }
48  QTreeWidgetItem *qtwi = new QTreeWidgetItem();
49  qtwi->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
50  ui->m_tree->addTopLevelItem(qtwi);
51  ui->m_tree->setCurrentItem(qtwi);
52  ui->m_tree->editItem(qtwi);
53 }
54 
60 void NameListWidget::setNames(const NamesList &name_list)
61 {
62  for (QString lang : name_list.langs())
63  {
64  QString value = name_list[lang];
65  QStringList values;
66  values << lang << value;
67  QTreeWidgetItem *qtwi = new QTreeWidgetItem(values);
68  if (!m_read_only) {
69  qtwi->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
70  }
71  ui->m_tree->addTopLevelItem(qtwi);
72  ui->m_tree->sortItems(0, Qt::AscendingOrder);
73  }
74 }
75 
81 {
82  NamesList nl_;
83 
84  int names_count = ui->m_tree->topLevelItemCount();
85  for (int i = 0 ; i < names_count ; ++ i)
86  {
87  QString lang = ui->m_tree->topLevelItem(i)->text(0);
88  QString value = ui->m_tree->topLevelItem(i)->text(1);
89  if (lang != "" && value != "") {
90  nl_.addName(lang, value);
91  }
92  }
93 
94  return nl_;
95 }
96 
103 {
104  m_read_only = ro;
105 
106  int names_count = ui->m_tree->topLevelItemCount() - 1;
107  for (int i = names_count ; i >= 0 ; -- i)
108  {
109  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
110  if (!m_read_only) {
111  flags |= Qt::ItemIsEditable;
112  }
113  ui->m_tree->topLevelItem(i)->setFlags(flags);
114  }
115  ui->m_add_line_pb->setEnabled(!ro);
116 }
117 
124  return names().isEmpty();
125 }
126 
127 void NameListWidget::setClipboardValue(QHash<QString, QString> value)
128 {
129  if (value.isEmpty()) {
130  ui->m_clipboard_cb->setHidden(true);
131  }
132  else
133  {
134  ui->m_clipboard_cb->setVisible(true);
135 
136  QStringList list = value.keys();
137  list.sort();
138  for (QString key : list) {
139  ui->m_clipboard_cb->addItem(key, value.value(key));
140  }
141  }
142 }
143 
149 {
150  int names_count = ui->m_tree->topLevelItemCount() - 1;
151  for (int i = names_count ; i >= 0 ; -- i)
152  {
153  if (ui->m_tree->topLevelItem(i)->text(0).isEmpty() &&
154  ui->m_tree->topLevelItem(i)->text(1).isEmpty())
155  {
156  ui->m_tree->takeTopLevelItem(i);
157  }
158  }
159 }
160 
162 {
163  Q_UNUSED(index);
164 
165  QClipboard *clipboard = QApplication::clipboard();
166  clipboard->setText(ui->m_clipboard_cb->currentData().toString());
167  ui->m_clipboard_cb->setCurrentIndex(0);
168 }
void addLine()
NameListWidget::addLine Add a new line to the name list widget.
void setNames(const NamesList &name_list)
NameListWidget::setNames Set the current names of this dialog from .
NamesList names() const
NameListWidget::names.
bool isEmpty() const
NameListWidget::isEmpty.
void addName(const QString &, const QString &)
Definition: nameslist.cpp:50
NameListWidget(QWidget *parent=nullptr)
bool isEmpty() const
Definition: nameslist.cpp:80
void on_m_clipboard_cb_activated(int index)
void setClipboardValue(QHash< QString, QString > value)
Ui::NameListWidget * ui
QIcon ro
Definition: qeticons.cpp:199
void setReadOnly(bool ro)
NameListWidget::setReadOnly Set this dialog to read only or not.
void clean()
NameListWidget::clean Clean the lists of names by removing the emtpy lines.
QList< QString > langs() const
Definition: nameslist.cpp:73
The NameListWidget class Provide a widget for let user define localized string;.