hyperion.ng/src/hyperion-remote/CustomParameter.h

115 lines
3.0 KiB
C
Raw Normal View History

#pragma once
// Qt includes
#include <QColor>
#include <QImage>
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
// hyperion-remote includes
#include "ColorTransformValues.h"
2013-08-31 14:36:54 +02:00
/// Data parameter for a color
typedef vlofgren::PODParameter<QColor> ColorParameter;
2013-08-31 14:36:54 +02:00
/// Data parameter for an image
typedef vlofgren::PODParameter<QImage> ImageParameter;
2013-08-31 14:36:54 +02:00
/// Data parameter for color transform values (list of three values)
typedef vlofgren::PODParameter<ColorTransformValues> TransformParameter;
namespace vlofgren {
2013-09-09 22:35:28 +02:00
///
/// Translates a string (as passed on the commandline) to a color
///
/// @param[in] s The string (as passed on the commandline)
///
/// @return The translated color
///
/// @throws Parameter::ParameterRejected If the string did not result in a color
///
template<>
QColor ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
{
// Check if we can create the color by name
QColor color(s.c_str());
if (color.isValid())
{
return color;
}
// check if we can create the color by hex RRGGBB value
if (s.length() == 6 && isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4]) && isxdigit(s[5]))
{
bool ok = true;
int rgb[3];
for (int i = 0; i < 3 && ok; ++i)
{
QString colorComponent(s.substr(2*i, 2).c_str());
rgb[i] = colorComponent.toInt(&ok, 16);
}
// check if all components parsed succesfully
if (ok)
{
color.setRgb(rgb[0], rgb[1], rgb[2]);
return color;
}
}
std::stringstream errorMessage;
errorMessage << "Invalid color. A color is specified by a six lettered RRGGBB hex value or one of the following names:";
foreach (const QString & colorname, QColor::colorNames()) {
errorMessage << "\n " << colorname.toStdString();
}
throw Parameter::ParameterRejected(errorMessage.str());
return color;
}
template<>
QImage ImageParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
{
QImage image(s.c_str());
if (image.isNull())
{
std::stringstream errorMessage;
errorMessage << "File " << s << " could not be opened as an image";
throw Parameter::ParameterRejected(errorMessage.str());
}
return image;
}
template<>
ColorTransformValues TransformParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
{
ColorTransformValues transform;
// s should be split in 3 parts
// seperators are either a ',' or a space
QStringList components = QString(s.c_str()).split(" ", QString::SkipEmptyParts);
if (components.size() == 3)
{
bool ok1, ok2, ok3;
transform.valueRed = components[0].toDouble(&ok1);
transform.valueGreen = components[1].toDouble(&ok2);
transform.valueBlue = components[2].toDouble(&ok3);
if (ok1 && ok2 && ok3)
{
return transform;
}
}
std::stringstream errorMessage;
errorMessage << "Argument " << s << " can not be parsed to 3 double values";
throw Parameter::ParameterRejected(errorMessage.str());
return transform;
}
}