2010-12-19 18:08:08 +00:00
|
|
|
/*
|
2017-01-20 10:55:49 +00:00
|
|
|
Copyright 2006-2017 The QElectroTech Team
|
2010-12-19 18:08:08 +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 "diagramcontext.h"
|
|
|
|
#include <QRegExp>
|
2012-07-01 21:54:05 +00:00
|
|
|
#include "qet.h"
|
2017-02-03 13:48:42 +00:00
|
|
|
#include <algorithm>
|
2010-12-19 18:08:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
@return a list containing all the keys in the context object.
|
|
|
|
*/
|
2017-02-16 14:10:30 +00:00
|
|
|
QList<QString> DiagramContext::keys(DiagramContext::KeyOrder order) const
|
|
|
|
{
|
2012-06-29 05:21:46 +00:00
|
|
|
if (order == None) {
|
2017-02-16 14:10:30 +00:00
|
|
|
return m_content.keys();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QList<QString> keys_list = m_content.keys();
|
2012-06-29 05:21:46 +00:00
|
|
|
if (order == Alphabetical) {
|
2017-02-16 14:10:30 +00:00
|
|
|
std::sort(keys_list.begin(), keys_list.end());
|
2012-06-29 05:21:46 +00:00
|
|
|
} else {
|
2017-02-03 13:48:42 +00:00
|
|
|
std::sort(keys_list.begin(), keys_list.end(), DiagramContext::stringLongerThan);
|
2012-06-29 05:21:46 +00:00
|
|
|
}
|
|
|
|
return(keys_list);
|
|
|
|
}
|
2010-12-19 18:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param key string key
|
|
|
|
@return true if that key is known to the diagram context, false otherwise
|
|
|
|
*/
|
|
|
|
bool DiagramContext::contains(const QString &key) const {
|
2017-02-16 14:10:30 +00:00
|
|
|
return(m_content.contains(key));
|
2010-12-19 18:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param key
|
|
|
|
*/
|
|
|
|
const QVariant DiagramContext::operator[](const QString &key) const {
|
2017-02-16 14:10:30 +00:00
|
|
|
return(m_content[key]);
|
2010-12-19 18:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param key key to insert in the context - the key may only contain lowercase
|
2014-02-12 17:36:35 +00:00
|
|
|
letters and dashes.
|
|
|
|
If embedded key is set, key must be find it else value is not added.
|
2010-12-19 18:08:08 +00:00
|
|
|
@see DiagramContext::keyIsAcceptable()
|
|
|
|
@param value value to insert in the context
|
2014-02-12 17:36:35 +00:00
|
|
|
@param show if value is used to be show on the diagram or somewhere else,
|
|
|
|
we can specify if he is show(true) or not(false)
|
2010-12-19 18:08:08 +00:00
|
|
|
@return true if the insertion succeeds, false otherwise
|
|
|
|
*/
|
2014-02-12 17:36:35 +00:00
|
|
|
bool DiagramContext::addValue(const QString &key, const QVariant &value, bool show) {
|
2010-12-19 18:08:08 +00:00
|
|
|
if (keyIsAcceptable(key)) {
|
2017-02-16 14:10:30 +00:00
|
|
|
m_content.insert(key, value);
|
|
|
|
m_content_show.insert(key, show);
|
2010-12-19 18:08:08 +00:00
|
|
|
return(true);
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
2010-12-19 18:08:08 +00:00
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2017-08-06 10:18:33 +00:00
|
|
|
QVariant DiagramContext::value(const QString &key) const {
|
|
|
|
return m_content.value(key);
|
|
|
|
}
|
|
|
|
|
2012-07-01 21:54:03 +00:00
|
|
|
/**
|
|
|
|
Clear the content of this diagram context.
|
|
|
|
*/
|
|
|
|
void DiagramContext::clear() {
|
2017-02-16 14:10:30 +00:00
|
|
|
m_content.clear();
|
|
|
|
m_content_show.clear();
|
2012-07-01 21:54:03 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 21:54:05 +00:00
|
|
|
/**
|
|
|
|
@return the number of key/value pairs stored in this object.
|
|
|
|
*/
|
|
|
|
int DiagramContext::count() {
|
2017-02-16 14:10:30 +00:00
|
|
|
return(m_content.count());
|
2012-07-01 21:54:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:36:35 +00:00
|
|
|
/**
|
|
|
|
* @brief DiagramContext::keyMustShow
|
|
|
|
* @return the value pairs with key, if key no found, return false
|
|
|
|
*/
|
|
|
|
bool DiagramContext::keyMustShow(const QString &key) const {
|
2017-02-16 14:10:30 +00:00
|
|
|
if (m_content_show.contains(key))
|
|
|
|
return m_content_show[key];
|
2014-02-12 17:36:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-09 15:16:51 +00:00
|
|
|
bool DiagramContext::operator==(const DiagramContext &dc) const {
|
2017-02-16 14:10:30 +00:00
|
|
|
return(m_content == dc.m_content &&
|
|
|
|
m_content_show == dc.m_content_show);
|
2011-01-09 15:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DiagramContext::operator!=(const DiagramContext &dc) const {
|
|
|
|
return(!(*this == dc));
|
|
|
|
}
|
|
|
|
|
2012-07-01 21:54:05 +00:00
|
|
|
/**
|
|
|
|
Export this context properties under the \a e XML element, using tags
|
|
|
|
named \a tag_name (defaults to "property").
|
|
|
|
*/
|
|
|
|
void DiagramContext::toXml(QDomElement &e, const QString &tag_name) const {
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QString key, keys()) {
|
2012-07-01 21:54:05 +00:00
|
|
|
QDomElement property = e.ownerDocument().createElement(tag_name);
|
|
|
|
property.setAttribute("name", key);
|
2017-02-16 14:10:30 +00:00
|
|
|
property.setAttribute("show",m_content_show[key]);
|
|
|
|
QDomText value = e.ownerDocument().createTextNode(m_content[key].toString());
|
2012-07-01 21:54:05 +00:00
|
|
|
property.appendChild(value);
|
|
|
|
e.appendChild(property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Read this context properties from the \a e XML element, looking for tags named
|
|
|
|
\a tag_name (defaults to "property").
|
|
|
|
*/
|
|
|
|
void DiagramContext::fromXml(const QDomElement &e, const QString &tag_name) {
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QDomElement property, QET::findInDomElement(e, tag_name)) {
|
2012-07-01 21:54:05 +00:00
|
|
|
if (!property.hasAttribute("name")) continue;
|
|
|
|
addValue(property.attribute("name"), QVariant(property.text()));
|
2017-02-16 14:10:30 +00:00
|
|
|
m_content_show.insert(property.attribute("name"), property.attribute("show", "1").toInt());
|
2012-07-01 21:54:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Export this context properties to \a settings by creating an array named \a
|
|
|
|
array_name.
|
|
|
|
*/
|
|
|
|
void DiagramContext::toSettings(QSettings &settings, const QString &array_name) const {
|
|
|
|
settings.beginWriteArray(array_name);
|
|
|
|
int i = 0;
|
2017-02-16 14:10:30 +00:00
|
|
|
foreach (QString key, m_content.keys()) {
|
2012-07-01 21:54:05 +00:00
|
|
|
settings.setArrayIndex(i);
|
|
|
|
settings.setValue("name", key);
|
2017-02-16 14:10:30 +00:00
|
|
|
settings.setValue("value", m_content[key].toString());
|
2012-07-01 21:54:05 +00:00
|
|
|
++ i;
|
|
|
|
}
|
|
|
|
settings.endArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Read this context properties from \a settings by running through the array
|
|
|
|
named \a array_name.
|
|
|
|
*/
|
|
|
|
void DiagramContext::fromSettings(QSettings &settings, const QString &array_name) {
|
|
|
|
int size = settings.beginReadArray(array_name);
|
|
|
|
for (int i = 0 ; i < size; ++ i) {
|
|
|
|
settings.setArrayIndex(i);
|
|
|
|
QString key = settings.value("name").toString();
|
|
|
|
if (key.isEmpty()) continue;
|
|
|
|
addValue(key, settings.value("value").toString());
|
|
|
|
}
|
|
|
|
settings.endArray();
|
|
|
|
}
|
|
|
|
|
2012-05-09 17:21:46 +00:00
|
|
|
/**
|
|
|
|
@return the regular expression used to check whether a given key is acceptable.
|
|
|
|
@see keyIsAcceptable()
|
|
|
|
*/
|
|
|
|
QString DiagramContext::validKeyRegExp() {
|
|
|
|
return("^[a-z0-9-]+$");
|
|
|
|
}
|
|
|
|
|
2012-06-29 05:21:46 +00:00
|
|
|
/**
|
|
|
|
@return True if \a a is longer than \a b, false otherwise.
|
|
|
|
*/
|
|
|
|
bool DiagramContext::stringLongerThan(const QString &a, const QString &b) {
|
|
|
|
return (a.length() > b.length());
|
|
|
|
}
|
|
|
|
|
2010-12-19 18:08:08 +00:00
|
|
|
/**
|
|
|
|
@param key a key string
|
|
|
|
@return true if that key is acceptable, false otherwise
|
|
|
|
*/
|
|
|
|
bool DiagramContext::keyIsAcceptable(const QString &key) const {
|
2012-05-09 17:21:46 +00:00
|
|
|
static QRegExp re(DiagramContext::validKeyRegExp());
|
2010-12-19 18:08:08 +00:00
|
|
|
return(re.exactMatch(key));
|
|
|
|
}
|