Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions trikKernel/include/trikKernel/configurer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class Configurer
/// Returns version of the config file which shall correspond to casing model.
QString version() const;

/// Creates new model-config file
void generateConfigFile(const QString &fileName, const QString &dirPath) const;

/// Changes value of given attribute of a device on given port
void changeAttributeByPort(const QString &port, const QString &attributeName, const QString &newAttributeValue);

private:
struct Device {
QString name;
Expand Down
94 changes: 94 additions & 0 deletions trikKernel/src/configurer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,100 @@ QString Configurer::version() const
{
return mVersion;
}
void Configurer::generateConfigFile(const QString &fileName, const QString &dirPath) const
{
QString content = "";
QString indent = " ";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

content += "<config version = \"" + version() + "\">" + '\n';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше сразу QString content = ...

QStringList sortedPorts = ports();
sortedPorts.sort();

for (int i = 0; i < sortedPorts.length(); i++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не for (const QString &port : sortedPorts) {...} ?

QString port = sortedPorts[i];
ModelConfigurationElement currentElement = mModelConfiguration[port];
content += indent + "<" + port + ">" + '\n';
content += indent + indent + "<" + currentElement.deviceType;

QStringList sortedAttributes = currentElement.attributes.keys();
sortedAttributes.sort();
if (sortedAttributes.length() != 0)

@yurii-litvinov yurii-litvinov Jul 22, 2016

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

по стайлгайду надо фигурные скобки вокруг тела if-а

content += '\n';

for (int i = 0; i < sortedAttributes.length(); i++) {
QString name = sortedAttributes[i];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

if (mDeviceTypes[currentElement.deviceType].attributes[name] != currentElement.attributes[name])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и тут

content += indent + indent + indent + name + "=\"" + currentElement.attributes[name] + "\"" + '\n';
}

if (sortedAttributes.length() != 0)
content += indent + indent + "/>" + '\n';
else
content += QString(" ") + "/>" + '\n';
content += indent + "</" + port + ">" + '\n';
}

QStringList sortedDeviceClasses = mAdditionalModelConfiguration.keys();
sortedDeviceClasses.sort();
for (int i = 0; i < sortedDeviceClasses.length(); i++) {
QString deviceClassName = sortedDeviceClasses[i];
AdditionalModelConfigurationElement currentElement = mAdditionalModelConfiguration[deviceClassName];
content += indent + "<" + deviceClassName;

QStringList sortedAttributes = currentElement.attributes.keys();
sortedAttributes.sort();
if (sortedAttributes.length() != 0)
content += '\n';

for (int i = 0; i < sortedAttributes.length(); i++) {
QString name = sortedAttributes[i];
if (mDevices[deviceClassName].attributes[name] != currentElement.attributes[name])
content += indent + indent + name + "=\"" + currentElement.attributes[name] + "\"" + '\n';
}

if (sortedAttributes.length() != 0)
content += indent + "/>" + '\n';
else
content += " />" + '\n';
}
content += "</config>";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

после закрывающей фигурной скобки надо пустую строку

FileUtils::writeToFile(fileName, content, dirPath);
}

void Configurer::changeAttributeByPort(const QString &port, const QString &attributeName,
const QString &newAttributeValue)
{
if (!mModelConfiguration.contains(port)) {
throw MalformedConfigException(QString("Port '%1' is not configured").arg(port));
}
if (mModelConfiguration[port].attributes.contains(attributeName))
{
mModelConfiguration[port].attributes[attributeName] = newAttributeValue;
}

const QString &deviceType = mModelConfiguration.value(port).deviceType;
if (mDeviceTypes.contains(deviceType)) {
if (mDeviceTypes[deviceType].attributes.contains(attributeName)) {
mModelConfiguration[port].attributes.insert(attributeName, newAttributeValue);
}
const QString deviceClass = mDeviceTypes[deviceType].deviceClass;
if (mDevices.contains(deviceClass)) {
const Device &device = mDevices[deviceClass];

if (device.attributes.contains(attributeName)) {
if (!mAdditionalModelConfiguration.contains(device.name)){
AdditionalModelConfigurationElement element;
element.deviceType = device.name;
mAdditionalModelConfiguration.insert(device.name, element);
}

if (!mAdditionalModelConfiguration[device.name].attributes.contains(attributeName))
mAdditionalModelConfiguration[device.name].attributes.insert(attributeName, newAttributeValue);
else
mAdditionalModelConfiguration[device.name].attributes[attributeName] = newAttributeValue;
}
}
}
}

void Configurer::parseDeviceClasses(const QDomElement &element)
{
Expand Down