204 lines
6.5 KiB
C++
Raw Normal View History

/*
2021-02-20 12:13:46 +01:00
Copyright 2006-2021 The QElectroTech Team
2020-10-17 20:25:30 +02:00
This file is part of QElectroTech.
2020-10-17 20:25:30 +02:00
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.
2020-10-17 20:25:30 +02:00
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.
2020-10-17 20:25:30 +02:00
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xrefproperties.h"
2020-12-09 15:28:43 +01:00
#include "../qetapp.h"
#include <QHash>
#include <QMetaEnum>
#include "../qetxml.h"
/**
2020-10-17 20:25:30 +02:00
@brief XRefProperties::XRefProperties
Default Constructor
2020-08-16 11:19:36 +02:00
*/
XRefProperties::XRefProperties()
{
2021-03-04 19:18:28 +01:00
setTagName("xref");
}
/**
2020-10-17 20:25:30 +02:00
@brief XRefProperties::toSettings
Save to settings
@param settings: QSettings to use
@param prefix: prefix before properties name
2020-08-16 11:19:36 +02:00
*/
2020-08-19 21:27:14 +02:00
void XRefProperties::toSettings(QSettings &settings,
2020-10-19 11:07:04 +02:00
const QString &prefix) const
2020-09-07 22:03:40 +02:00
{
2020-10-17 20:25:30 +02:00
settings.setValue(prefix + "showpowerctc", m_show_power_ctc);
QString display = m_display == Cross? "cross" : "contacts";
settings.setValue(prefix + "displayhas", display);
QString snap = m_snap_to == Bottom? "bottom" : "label";
settings.setValue(prefix + "snapto", snap);
int offset = m_offset;
settings.setValue(prefix + "offset", offset);
QString master_label = m_master_label;
settings.setValue(prefix + "master_label", master_label);
QString slave_label = m_slave_label;
settings.setValue(prefix + "slave_label", slave_label);
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
settings.setValue(prefix + "xrefpos", var.valueToKey(m_xref_pos));
foreach (QString key, m_prefix.keys()) {
settings.setValue(prefix + key + "prefix", m_prefix.value(key));
}
}
/**
2020-10-17 20:25:30 +02:00
@brief XRefProperties::fromSettings
load from settings
@param settings: QSettings to use
@param prefix: prefix before properties name
2020-08-16 11:19:36 +02:00
*/
2020-10-19 11:07:04 +02:00
void XRefProperties::fromSettings(QSettings &settings,
const QString &prefix)
{
2020-10-17 20:25:30 +02:00
m_show_power_ctc = settings.value(prefix + "showpowerctc", true).toBool();
QString display = settings.value(prefix + "displayhas", "cross").toString();
display == "cross"? m_display = Cross : m_display = Contacts;
QString snap = settings.value(prefix + "snapto", "label").toString();
snap == "bottom"? m_snap_to = Bottom : m_snap_to = Label;
m_offset = settings.value(prefix + "offset", "0").toInt();
m_master_label = settings.value(prefix + "master_label", "%f-%l%c").toString();
m_slave_label = settings.value(prefix + "slave_label", "(%f-%l%c)").toString();
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
m_xref_pos = Qt::AlignmentFlag(var.keyToValue((settings.value(prefix + "xrefpos").toString()).toStdString().data()));
for (QString key : m_prefix_keys) {
m_prefix.insert(key, settings.value(prefix + key + "prefix").toString());
}
}
/**
2020-10-17 20:25:30 +02:00
@brief XRefProperties::toXml
Save to xml
@param xml_document : QDomElement to use for saving
@return QDomElement
2020-08-16 11:19:36 +02:00
*/
void XRefProperties::toXmlPriv(QDomElement& xml_element) const
2020-09-07 22:03:40 +02:00
{
xml_element.setAttribute("type", m_key);
xml_element.setAttribute("showpowerctc", m_show_power_ctc? "true" : "false");
QString display = m_display == Cross? "cross" : "contacts";
xml_element.setAttribute("displayhas", display);
QString snap = m_snap_to == Bottom? "bottom" : "label";
xml_element.setAttribute("snapto", snap);
QString xrefpos;
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
xml_element.setAttribute("xrefpos", var.valueToKey(m_xref_pos));
int offset = m_offset;
xml_element.setAttribute("offset", QString::number(offset));
QString master_label = m_master_label;
xml_element.setAttribute("master_label", master_label);
QString slave_label = m_slave_label;
xml_element.setAttribute("slave_label", slave_label);
foreach (QString key, m_prefix.keys()) {
xml_element.setAttribute(key + "prefix", m_prefix.value(key));
}
}
/** RETURNS True
2021-03-04 19:18:28 +01:00
@brief XRefProperties::fromXmlPriv
2020-10-17 20:25:30 +02:00
Load from xml
@param xml_element: QDomElement to use for load
2020-08-16 11:19:36 +02:00
*/
2021-03-04 19:18:28 +01:00
bool XRefProperties::fromXmlPriv(const QDomElement &xml_element) {
2020-10-16 11:45:17 +02:00
if (QETXML::propertyBool(xml_element, "showpowerctc", &m_show_power_ctc))
2020-10-17 20:25:30 +02:00
return false;
QString display;
if (QETXML::propertyString(xml_element, "displayhas", &display) != QETXML::PropertyFlags::NotFound) {
2020-10-17 20:25:30 +02:00
display == "cross"? m_display = Cross : m_display = Contacts;
}
QString snap;
if (QETXML::propertyString(xml_element, "snapto", &snap) != QETXML::PropertyFlags::NotFound) {
2020-10-17 20:25:30 +02:00
snap == "bottom"? m_snap_to = Bottom : m_snap_to = Label;
}
QString xrefpos;
if (QETXML::propertyString(xml_element, "xrefpos", &xrefpos) != QETXML::PropertyFlags::NotFound) {
2020-10-17 20:25:30 +02:00
QMetaEnum var = QMetaEnum::fromType<Qt::Alignment>();
m_xref_pos = Qt::AlignmentFlag(var.keyToValue(xrefpos.toStdString().data()));
}
// TODO: why it compiles without this true??
QETXML::propertyInteger(xml_element, "offset", &m_offset);
QETXML::propertyString(xml_element, "master_label", &m_master_label);
QETXML::propertyString(xml_element, "slave_label", &m_slave_label);
2020-10-17 20:25:30 +02:00
QString value;
foreach (QString key, m_prefix_keys) {
if (!QETXML::propertyString(xml_element, key + "prefix", &value))
2020-10-17 20:25:30 +02:00
m_prefix.insert(key, value);
}
return true;
}
/**
2020-10-17 20:25:30 +02:00
@brief XRefProperties::defaultProperties
@return the default properties stored in the setting file
For the xref, there is 2 propreties.
For coil, stored with the string "coil" in the returned QHash.
For protection, stored with the string "protection" in the returned QHash.
2020-08-16 11:19:36 +02:00
*/
QHash<QString, XRefProperties> XRefProperties::defaultProperties()
{
2020-10-17 20:25:30 +02:00
QHash <QString, XRefProperties> hash;
QStringList keys;
keys << "coil" << "protection" << "commutator";
2020-10-17 20:25:30 +02:00
QSettings settings;
2020-10-17 20:25:30 +02:00
foreach (QString key, keys)
{
XRefProperties properties;
QString str("diagrameditor/defaultxref");
properties.fromSettings(settings, str += key);
hash.insert(key, properties);
}
2020-10-17 20:25:30 +02:00
return hash;
}
bool XRefProperties::operator ==(const XRefProperties &xrp) const{
2020-10-17 20:25:30 +02:00
return (m_show_power_ctc == xrp.m_show_power_ctc &&
m_display == xrp.m_display &&
m_snap_to == xrp.m_snap_to &&
m_prefix == xrp.m_prefix &&
m_master_label == xrp.m_master_label &&
m_offset == xrp.m_offset &&
m_xref_pos == xrp.m_xref_pos );
}
2020-09-07 22:03:40 +02:00
bool XRefProperties::operator !=(const XRefProperties &xrp) const
{
2020-10-17 20:25:30 +02:00
return (! (*this == xrp));
}