QElectroTech  0.70
diagramcontextwidget.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 "diagramcontextwidget.h"
19 #include "ui_diagramcontextwidget.h"
20 
22  QWidget(parent),
23  ui(new Ui::DiagramContextWidget)
24 {
25  ui->setupUi(this);
26  connect(ui->m_table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(checkTableRows()));
27 }
28 
30 {
31  delete ui;
32 }
33 
39 {
41 
42  for (int i = 0 ; i < ui->m_table-> rowCount() ; ++ i)
43  {
44  QTableWidgetItem *qtwi_name = ui->m_table-> item(i, 0);
45  QTableWidgetItem *qtwi_value = ui->m_table-> item(i, 1);
46  if (!qtwi_name || !qtwi_value) {
47  continue;
48  }
49 
50  QString key = qtwi_name -> text();
51  if (key.isEmpty()) {
52  continue;
53  }
54 
55  QString value = qtwi_value -> text();
56  context.addValue(key, value);
57  }
58 
59  return(context);
60 }
61 
68 {
69  clear();
70 
71  int i = 0;
72  for (QString key : context.keys(DiagramContext::Alphabetical))
73  {
74  ui->m_table->setItem(i, 0, new QTableWidgetItem(key));
75  ui->m_table->setItem(i, 1, new QTableWidgetItem(context[key].toString()));
76  ++ i;
77  }
78 
80 }
81 
87 {
88  int name_less_rows_count = 0;
89  for (int i = 0 ; i < ui->m_table->rowCount() ; ++ i)
90  {
91  QTableWidgetItem *qtwi_name = ui->m_table->item(i, 0);
92  if (qtwi_name && qtwi_name -> text().isEmpty()) {
93  ++ name_less_rows_count;
94  }
95  }
96 
97  return(name_less_rows_count);
98 }
99 
105 {
106  ui->m_table->clearContents();
107  for (int i = 1 ; i < ui->m_table->rowCount() ; ++ i) {
108  ui->m_table->removeRow(i);
109  }
110 
112 }
113 
120 {
121  static QRegExp re(DiagramContext::validKeyRegExp());
122 
123  QBrush fg_brush = ui->m_table->palette().brush(QPalette::WindowText);
124 
125  int invalid_keys = 0;
126  for (int i = 0 ; i < ui->m_table->rowCount() ; ++ i)
127  {
128  QTableWidgetItem *qtwi_name = ui->m_table->item(i, 0);
129  if (!qtwi_name) {
130  continue;
131  }
132 
133  bool highlight = false;
134  if (!qtwi_name -> text().isEmpty())
135  {
136  if (!re.exactMatch(qtwi_name -> text()))
137  {
138  highlight = true;
139  ++ invalid_keys;
140  }
141  }
142  qtwi_name -> setForeground(highlight ? Qt::red : fg_brush);
143  }
144 
145  return(invalid_keys);
146 }
147 
154 {
155  QString format_text = tr(
156  "Les noms ne peuvent contenir que des lettres minuscules, des "
157  "chiffres et des tirets."
158  );
159 
161  format_text = QString("<span style=\"color: red;\">%1</span>").arg(format_text);
162  }
163  ui->m_label->setText(format_text);
164 }
165 
171 {
173  if (!nameLessRowsCount())
174  {
175  int new_idx = ui->m_table->rowCount();
176  ui->m_table->setRowCount(new_idx + 1);
177  ui->m_table->setItem(new_idx, 0, new QTableWidgetItem(""));
178  ui->m_table->setItem(new_idx, 1, new QTableWidgetItem(""));
179  }
180 }
Ui::DiagramContextWidget * ui
void checkTableRows()
DiagramContextWidget::checkTableRows Adds a row in the additional fields table if needed...
void refreshFormatLabel()
DiagramContextWidget::refreshFormatLabel Sets the text describing the acceptable format for keys when...
bool addValue(const QString &, const QVariant &, bool show=true)
QList< QString > keys(KeyOrder=None) const
DiagramContext context() const
DiagramContextWidget::context.
int nameLessRowsCount() const
DiagramContextWidget::nameLessRowsCount.
QIcon tr
Definition: qeticons.cpp:204
DiagramContextWidget(QWidget *parent=nullptr)
int highlightNonAcceptableKeys()
DiagramContextWidget::highlightNonAcceptableKeys Highlight keys that would not be accepted by a Diagr...
void setContext(const DiagramContext &context)
DiagramContextWidget::setContext Load the content from into this widget.
static QString validKeyRegExp()
void clear()
DiagramContextWidget::clear Clear any values entered within this widget.