QElectroTech  0.70
diagramcontext.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 "diagramcontext.h"
19 #include <QRegExp>
20 #include "qet.h"
21 #include <algorithm>
22 
32 {
33  for (QString key : other.keys()) {
34  addValue(key, other.value(key));
35  }
36 }
37 
42 {
43  if (order == None) {
44  return m_content.keys();
45  }
46  else
47  {
48  QList<QString> keys_list = m_content.keys();
49  if (order == Alphabetical) {
50  std::sort(keys_list.begin(), keys_list.end());
51  } else {
52  std::sort(keys_list.begin(), keys_list.end(), DiagramContext::stringLongerThan);
53  }
54  return(keys_list);
55  }
56 }
57 
62 bool DiagramContext::contains(const QString &key) const {
63  return(m_content.contains(key));
64 }
65 
69 const QVariant DiagramContext::operator[](const QString &key) const {
70  return(m_content[key]);
71 }
72 
83 bool DiagramContext::addValue(const QString &key, const QVariant &value, bool show) {
84  if (keyIsAcceptable(key)) {
85  m_content.insert(key, value);
86  m_content_show.insert(key, show);
87  return(true);
88  }
89  return(false);
90 }
91 
92 QVariant DiagramContext::value(const QString &key) const {
93  return m_content.value(key);
94 }
95 
100  m_content.clear();
101  m_content_show.clear();
102 }
103 
108  return(m_content.count());
109 }
110 
115 bool DiagramContext::keyMustShow(const QString &key) const {
116  if (m_content_show.contains(key))
117  return m_content_show[key];
118  return false;
119 }
120 
122  return(m_content == dc.m_content &&
124 }
125 
127  return(!(*this == dc));
128 }
129 
134 void DiagramContext::toXml(QDomElement &e, const QString &tag_name) const {
135  foreach (QString key, keys()) {
136  QDomElement property = e.ownerDocument().createElement(tag_name);
137  property.setAttribute("name", key);
138  property.setAttribute("show",m_content_show[key]);
139  QDomText value = e.ownerDocument().createTextNode(m_content[key].toString());
140  property.appendChild(value);
141  e.appendChild(property);
142  }
143 }
144 
149 void DiagramContext::fromXml(const QDomElement &e, const QString &tag_name) {
150  foreach (QDomElement property, QET::findInDomElement(e, tag_name)) {
151  if (!property.hasAttribute("name")) continue;
152  addValue(property.attribute("name"), QVariant(property.text()));
153  m_content_show.insert(property.attribute("name"), property.attribute("show", "1").toInt());
154  }
155 }
156 
161 void DiagramContext::toSettings(QSettings &settings, const QString &array_name) const {
162  settings.beginWriteArray(array_name);
163  int i = 0;
164  foreach (QString key, m_content.keys()) {
165  settings.setArrayIndex(i);
166  settings.setValue("name", key);
167  settings.setValue("value", m_content[key].toString());
168  ++ i;
169  }
170  settings.endArray();
171 }
172 
177 void DiagramContext::fromSettings(QSettings &settings, const QString &array_name) {
178  int size = settings.beginReadArray(array_name);
179  for (int i = 0 ; i < size; ++ i) {
180  settings.setArrayIndex(i);
181  QString key = settings.value("name").toString();
182  if (key.isEmpty()) continue;
183  addValue(key, settings.value("value").toString());
184  }
185  settings.endArray();
186 }
187 
193  return("^[a-z0-9-]+$");
194 }
195 
199 bool DiagramContext::stringLongerThan(const QString &a, const QString &b) {
200  return (a.length() > b.length());
201 }
202 
207 bool DiagramContext::keyIsAcceptable(const QString &key) const {
208  QRegExp re(DiagramContext::validKeyRegExp());
209  return(re.exactMatch(key));
210 }
void fromSettings(QSettings &, const QString &)
static bool stringLongerThan(const QString &, const QString &)
QHash< QString, QVariant > m_content
Diagram context data (key/value pairs)
void add(DiagramContext other)
DiagramContext::add Add all value of to this. If a key already exist, the value is replaced...
void toSettings(QSettings &, const QString &) const
bool operator!=(const DiagramContext &) const
QVariant value(const QString &key) const
QHash< QString, bool > m_content_show
bool addValue(const QString &, const QVariant &, bool show=true)
QList< QString > keys(KeyOrder=None) const
bool contains(const QString &) const
void fromXml(const QDomElement &, const QString &="property")
const QVariant operator[](const QString &) const
QList< QDomElement > findInDomElement(const QDomElement &, const QString &)
Definition: qet.cpp:300
void toXml(QDomElement &, const QString &="property") const
bool keyMustShow(const QString &) const
DiagramContext::keyMustShow.
bool keyIsAcceptable(const QString &) const
static QString validKeyRegExp()
bool operator==(const DiagramContext &) const