view data/model/QueryModel.cpp @ 282:d9319859a4cf tip

(none)
author benoitrigolleau
date Fri, 31 Oct 2008 11:00:24 +0000
parents 60e84bb658bc
children
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*	Sound Access	
		EASAIER client application.	
		Silogic 2007. Laure Bajard. 
	
	This program 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.  See the file    
	COPYING included with this distribution for more information.
*/

#include "QueryModel.h"

QueryModel::QueryModel()
{}

QueryModel::~QueryModel()
{
	while (!m_themeModels.empty()) 
	{
		delete m_themeModels.begin()->second;
	    m_themeModels.erase(m_themeModels.begin());
	}

	m_curThemeModel = 0;
}

void QueryModel::addTheme(const QString &name, const QString &label)
{
	m_curThemeModel =  new QueryThemeModel(name, label);

	m_themeModels[name] = m_curThemeModel;
}

void QueryModel::newGroup(const QString &name, const QString &label)
{
	m_curThemeModel->newGroup(name, label);
}

void QueryModel::addProperty(const QString &name, const QString &label, const QString &type, 
	const QString &range, const QString &unit, const QString &comment)
{
	m_curThemeModel->addProperty(name, label, type, range, unit, comment);
}

void QueryModel::addRange(const QString &propertyName, const QString &range)
{
	m_curThemeModel->addRange(propertyName, range);
}

QueryThemeModel::QueryThemeModel(const QString &name, const QString &label) : 
	m_name(name),
	m_label(label),
	m_curGroupName(""),
	m_curGroupLabel("")
{
	setObjectName(name);
}

QueryThemeModel::~QueryThemeModel()
{
	m_propertiesName.clear();

	while (!m_properties.empty()) 
	{
		delete m_properties.begin()->second;
	    m_properties.erase(m_properties.begin());
	}
}

void QueryThemeModel::newGroup(const QString &name, const QString &label)
{
	m_curGroupName = name;
	m_curGroupLabel = label;
}

void QueryThemeModel::addProperty(const QString &name, const QString &label, const QString &type, 
	const QString &range, const QString &unit, const QString &comment)
{
	PropertyModel* newProperty = new PropertyModel(name, label, type, range, unit, comment, 
													m_curGroupName, m_curGroupLabel);

	m_properties[name] = newProperty;

	m_propertiesName.push_back(name);
}

QueryThemeModel::PropertyList QueryThemeModel::getProperties() const
{
	return m_propertiesName;
}

QString QueryThemeModel::getPropertyLabel(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getLabel();

	return "";
}

QueryThemeModel::PropertyType QueryThemeModel::getPropertyType(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getType();

	return InvalidProperty;
}

int QueryThemeModel::getPropertyRangeAndValue(const PropertyName &name, int *min, int *max) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
	{
		*min = iter->second->getMinRange();
		*max = iter->second->getMaxRange();
		if (iter->second->getType() != StringProperty)
			return iter->second->getValue().toInt();
	}

	return 0;
}

QString QueryThemeModel::getPropertyValue(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
	{
		PropertyModel * propModel = iter->second;
		return propModel->getValue();
	}

	return "";
}

QStringList QueryThemeModel::getPropertyRange(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getRange();

	return QStringList();
}

QString QueryThemeModel::getPropertyValueLabel(const PropertyName &name, int value) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getValueLabel(value);

	return "";
}

QString QueryThemeModel::getPropertyGroup(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getGroupName();

	return "";
}

QString QueryThemeModel::getPropertyGroupLabel(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getGroupLabel();

	return "";
}

QString QueryThemeModel::getPropertyUnit(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getUnit();

	return "";
}

QString QueryThemeModel::getPropertyComment(const PropertyName &name) const
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		return iter->second->getComment();

	return "";
}

void QueryThemeModel::setProperty(const PropertyName &name, int value)
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		iter->second->setValue(value);
}

void QueryThemeModel::setProperty(const PropertyName &name, QString value)
{
	std::map<QString, PropertyModel*>::const_iterator iter;
	iter = m_properties.find(name);

	if (iter != m_properties.end())
		iter->second->setValue(value);
}

void QueryThemeModel::setProperty(int value)
{
	QString name = sender()->objectName();
	setProperty(name, value);
}

void QueryThemeModel::setProperty(QString value)
{
	QString name = sender()->objectName();
	setProperty(name, value);
}

void QueryThemeModel::addRange(const QString &propertyName, const QString &range)
{
	std::map<QString, PropertyModel*>::iterator iter = m_properties.find(propertyName);
	if (iter != m_properties.end())
	{
		PropertyModel* model = iter->second;
		model->addRange(range);
	}
}

QueryThemeModel::PropertyModel::PropertyModel(const QString &name, const QString &label, const QString &type, 
											  const QString &range, const QString &unit, const QString &comment,
											  const QString &groupName, const QString &groupLabel) : 
	m_name(name),
	m_label(label),
	m_unit(unit),
	m_comment(comment),
	m_groupName(groupName),
	m_groupLabel(groupLabel),
	m_min(0),
	m_max(0),
	m_value("")
{
	if (type == "int")
	{
		m_type = RangeProperty;

		if (range != "")
		{
			int pos = range.indexOf("-");

			m_min = range.left(pos).toInt();
			m_max = range.right(range.length()-pos-1).toInt();
		}
	} else if (type == "string")
	{
		if (range == "") {
			m_type = StringProperty;
		} else {
			m_type = ValueProperty;

			if (range != "")
			{
				m_range = range.split("/");
				m_min = 0;
				m_max = m_range.size();
				m_value = m_range.at(0);
			}
		}
	} else if (type == "file")
	{
		m_type = FileProperty;

	} else if (type == "gYear")
	{
		m_type = RangeProperty;

		m_min = 0;
		m_max = 2007;

	} else
	{
		m_type = InvalidProperty;
	}
}


void QueryThemeModel::PropertyModel::addRange(const QString &range) 
{
	m_range << range;
	m_type = ValueProperty;
	m_min = 0;
	m_max = m_range.size();
	m_value = m_range.at(0);
}