00001 #include "VariablesViewPlugin.h"
00002 #include <VariablesViewPlugin.moc>
00003 #include "TargetModels.h"
00004 #include <QtGui>
00005 #include <QtDebug>
00006 #include <limits>
00007 #include <cassert>
00008
00009
00010
00011
00012 namespace Aseba
00013 {
00014 VariablesViewPlugin::VariablesViewPlugin(TargetVariablesModel* variablesModel) :
00015 variablesModel(variablesModel)
00016 {
00017
00018 }
00019
00020 VariablesViewPlugin::~VariablesViewPlugin()
00021 {
00022 if (variablesModel)
00023 variablesModel->unsubscribeViewPlugin(this);
00024 }
00025
00026 void VariablesViewPlugin::invalidateVariableModel()
00027 {
00028 variablesModel = 0;
00029 }
00030
00031
00032 LinearCameraViewVariablesDialog::LinearCameraViewVariablesDialog(TargetVariablesModel* variablesModel) :
00033 redVariable(new QComboBox),
00034 greenVariable(new QComboBox),
00035 blueVariable(new QComboBox),
00036 valuesRanges(new QComboBox)
00037 {
00038 QVBoxLayout* layout = new QVBoxLayout(this);
00039
00040 layout->addWidget(new QLabel(tr("Please choose your variables")));
00041 layout->addWidget(new QLabel(tr("red component")));
00042 layout->addWidget(redVariable);
00043 layout->addWidget(new QLabel(tr("green component")));
00044 layout->addWidget(greenVariable);
00045 layout->addWidget(new QLabel(tr("blue component")));
00046 layout->addWidget(blueVariable);
00047 layout->addWidget(new QLabel(tr("range of values")));
00048 layout->addWidget(valuesRanges);
00049
00050 QPushButton* okButton = new QPushButton(QIcon(":/images/ok.png"), tr("Ok"));
00051 connect(okButton, SIGNAL(clicked(bool)), SLOT(accept()));
00052 layout->addWidget(okButton);
00053
00054 valuesRanges->addItem(tr("auto range"));
00055 valuesRanges->addItem(tr("8 bits range (0–255)"));
00056 valuesRanges->addItem(tr("percent range (0–100)"));
00057
00058 const QList<TargetVariablesModel::Variable>& variables(variablesModel->getVariables());
00059 for (int i = 0; i < variables.size(); ++i)
00060 {
00061 redVariable->addItem(variables[i].name);
00062 greenVariable->addItem(variables[i].name);
00063 blueVariable->addItem(variables[i].name);
00064 }
00065
00066 redVariable->setEditText("camR");
00067 greenVariable->setEditText("camG");
00068 blueVariable->setEditText("camB");
00069
00070 setWindowTitle(tr("Linear Camera View Plugin"));
00071 }
00072
00073 LinearCameraViewPlugin::LinearCameraViewPlugin(TargetVariablesModel* variablesModel) :
00074 VariablesViewPlugin(variablesModel)
00075 {
00076 {
00077 LinearCameraViewVariablesDialog linearCameraViewVariablesDialog(variablesModel);
00078 if (linearCameraViewVariablesDialog.exec() == QDialog::Accepted)
00079 {
00080 redName = linearCameraViewVariablesDialog.redVariable->currentText();
00081 greenName = linearCameraViewVariablesDialog.greenVariable->currentText();
00082 blueName = linearCameraViewVariablesDialog.blueVariable->currentText();
00083 valuesRange = (ValuesRange)linearCameraViewVariablesDialog.valuesRanges->currentIndex();
00084 }
00085 else
00086 {
00087 deleteLater();
00088 return;
00089 }
00090 }
00091
00092 bool ok = true;
00093 ok &= variablesModel->subscribeToVariableOfInterest(this, redName);
00094 ok &= variablesModel->subscribeToVariableOfInterest(this, greenName);
00095 ok &= variablesModel->subscribeToVariableOfInterest(this, blueName);
00096
00097 if (!ok)
00098 {
00099 QMessageBox::warning(this, tr("Cannot initialize linear camera view plugin"), tr("One or more variable not found in %1, %2, or %3.").arg(redName).arg(greenName).arg(blueName));
00100 deleteLater();
00101 }
00102 else
00103 {
00104 QVBoxLayout *layout = new QVBoxLayout(this);
00105 image = new QLabel;
00106 image->resize(200, 40);
00107 image->setScaledContents(true);
00108 layout->addWidget(image);
00109 setAttribute(Qt::WA_DeleteOnClose);
00110 setWindowTitle(tr("Linear Camera View Plugin"));
00111 show();
00112 }
00113 }
00114
00115 void LinearCameraViewPlugin::variableValueUpdated(const QString& name, const VariablesDataVector& values)
00116 {
00117 if (name == redName)
00118 red = values;
00119 if (name == greenName)
00120 green = values;
00121 if (name == blueName)
00122 blue = values;
00123
00124 int width = qMin(red.size(), qMin(green.size(), blue.size()));
00125
00126 if (width)
00127 {
00128 QPixmap pixmap(width, 1);
00129 QPainter painter(&pixmap);
00130
00131 int min = std::numeric_limits<int>::max();
00132 int max = std::numeric_limits<int>::min();
00133 switch (valuesRange)
00134 {
00135 case VALUES_RANGE_AUTO:
00136 {
00137
00138 for (int i = 0; i < width; i++)
00139 {
00140 min = std::min(min, (int)red[i]);
00141 min = std::min(min, (int)green[i]);
00142 min = std::min(min, (int)blue[i]);
00143 max = std::max(max, (int)red[i]);
00144 max = std::max(max, (int)green[i]);
00145 max = std::max(max, (int)blue[i]);
00146 }
00147 }
00148 break;
00149
00150 case VALUES_RANGE_8BITS:
00151 {
00152 min = 0;
00153 max = 255;
00154 }
00155 break;
00156
00157 case VALUES_RANGE_PERCENT:
00158 {
00159 min = 0;
00160 max = 100;
00161 }
00162 break;
00163
00164 default:
00165 assert(false);
00166 break;
00167 }
00168
00169 for (int i = 0; i < width; i++)
00170 {
00171 int range = max - min;
00172 if (range == 0)
00173 range = 1;
00174 int r = ((red[i] - min) * 255) / range;
00175 int g = ((green[i] - min) * 255) / range;
00176 int b = ((blue[i] - min) * 255) / range;
00177 painter.fillRect(i, 0, 1, 1, QColor(r, g, b));
00178 }
00179
00180 image->setPixmap(pixmap);
00181 }
00182 }
00183 }