VariablesViewPlugin.cpp
Go to the documentation of this file.
00001 #include "VariablesViewPlugin.h"
00002 #include <VariablesViewPlugin.moc>
00003 #include "../Target.h"
00004 #include <QtGui>
00005 #include <QtDebug>
00006 #include <limits>
00007 #include <cassert>
00008 
00009 
00010 //#include <VariablesViewPlugin.moc>
00011 
00012 namespace Aseba
00013 {
00014         LinearCameraViewVariablesDialog::LinearCameraViewVariablesDialog(TargetVariablesModel* variablesModel) :
00015                 redVariable(new QComboBox),
00016                 greenVariable(new QComboBox),
00017                 blueVariable(new QComboBox),
00018                 valuesRanges(new QComboBox)
00019         {
00020                 QVBoxLayout* layout = new QVBoxLayout(this);
00021         
00022                 layout->addWidget(new QLabel(tr("Please choose your variables")));
00023                 layout->addWidget(new QLabel(tr("red component")));
00024                 layout->addWidget(redVariable);
00025                 layout->addWidget(new QLabel(tr("green component")));
00026                 layout->addWidget(greenVariable);
00027                 layout->addWidget(new QLabel(tr("blue component")));
00028                 layout->addWidget(blueVariable);
00029                 layout->addWidget(new QLabel(tr("range of values")));
00030                 layout->addWidget(valuesRanges);
00031                 
00032                 QPushButton* okButton = new QPushButton(QIcon(":/images/ok.png"), tr("Ok"));
00033                 connect(okButton, SIGNAL(clicked(bool)), SLOT(accept()));
00034                 layout->addWidget(okButton);
00035                 
00036                 valuesRanges->addItem(tr("auto range"));
00037                 valuesRanges->addItem(tr("8 bits range (0–255)"));
00038                 valuesRanges->addItem(tr("percent range (0–100)"));
00039                 
00040                 const QList<TargetVariablesModel::Variable>& variables(variablesModel->getVariables());
00041                 for (int i = 0; i < variables.size(); ++i)
00042                 {
00043                         redVariable->addItem(variables[i].name);
00044                         greenVariable->addItem(variables[i].name);
00045                         blueVariable->addItem(variables[i].name);
00046                 }
00047                 
00048                 redVariable->setEditText("cam_red");
00049                 greenVariable->setEditText("cam_green");
00050                 blueVariable->setEditText("cam_blue");
00051                 
00052                 setWindowTitle(tr("Linear Camera View Plugin"));
00053         }
00054         
00055         LinearCameraViewPlugin::LinearCameraViewPlugin(NodeTab* nodeTab) :
00056                 InvasivePlugin(nodeTab),
00057                 VariableListener(getVariablesModel()),
00058                 componentsReceived(0)
00059         {
00060                 QVBoxLayout *layout = new QVBoxLayout(this);
00061                 image = new QLabel;
00062                 image->setScaledContents(true);
00063                 layout->addWidget(image);
00064                 setWindowTitle(tr("Linear Camera View Plugin"));
00065                 resize(200, 40);
00066         }
00067 
00068 
00069         QWidget* LinearCameraViewPlugin::createMenuEntry()
00070         {
00071                 QCheckBox *checkbox = new QCheckBox(tr("linear camera viewer"));
00072                 connect(checkbox, SIGNAL(clicked(bool)), SLOT(setEnabled(bool)));
00073                 connect(this, SIGNAL(dialogBoxResult(bool)), checkbox, SLOT(setChecked(bool)));
00074                 return checkbox;
00075         }
00076         
00077         void LinearCameraViewPlugin::closeAsSoonAsPossible()
00078         {
00079                 close();
00080         }
00081         
00082         void LinearCameraViewPlugin::setEnabled(bool enabled)
00083         {
00084                 if (enabled)
00085                         enablePlugin();
00086                 else
00087                         disablePlugin();
00088         }
00089         
00090         void LinearCameraViewPlugin::enablePlugin()
00091         {
00092                 // unsubscribe to existing variables, if any
00093                 unsubscribeToVariablesOfInterest();
00094                 
00095                 {
00096                         LinearCameraViewVariablesDialog linearCameraViewVariablesDialog(variablesModel);
00097                         if (linearCameraViewVariablesDialog.exec() != QDialog::Accepted)
00098                         {
00099                                 emit dialogBoxResult(false);
00100                                 return;
00101                         }
00102                         
00103                         redName = linearCameraViewVariablesDialog.redVariable->currentText();
00104                         greenName = linearCameraViewVariablesDialog.greenVariable->currentText();
00105                         blueName = linearCameraViewVariablesDialog.blueVariable->currentText();
00106                         valuesRange = (ValuesRange)linearCameraViewVariablesDialog.valuesRanges->currentIndex();
00107                 }
00108                 
00109                 bool ok = true;
00110                 ok &= subscribeToVariableOfInterest(redName);
00111                 ok &= subscribeToVariableOfInterest(greenName);
00112                 ok &= subscribeToVariableOfInterest(blueName);
00113                 
00114                 if (!ok)
00115                 {
00116                         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));
00117                         unsubscribeToVariablesOfInterest();
00118                         emit dialogBoxResult(false);
00119                         return;
00120                 }
00121                 
00122                 redPos = getVariablesModel()->getVariablePos(redName);
00123                 greenPos = getVariablesModel()->getVariablePos(greenName);
00124                 bluePos = getVariablesModel()->getVariablePos(blueName);
00125                 redSize = getVariablesModel()->getVariableSize(redName);
00126                 greenSize = getVariablesModel()->getVariableSize(greenName);
00127                 blueSize = getVariablesModel()->getVariableSize(blueName);
00128                 
00129                 getTarget()->getVariables(getNodeId(), redPos, redSize);
00130                 getTarget()->getVariables(getNodeId(), greenPos, greenSize);
00131                 getTarget()->getVariables(getNodeId(), bluePos, blueSize);
00132                 
00133                 // TODO: let the user choose the refresh rate
00134                 startTimer(100);
00135                 
00136                 show();
00137         }
00138         
00139         void LinearCameraViewPlugin::disablePlugin()
00140         {
00141                 // unsubscribe to existing variables, if any
00142                 unsubscribeToVariablesOfInterest();
00143                 hide();
00144         }
00145         
00146         void LinearCameraViewPlugin::timerEvent ( QTimerEvent * event )
00147         {
00148                 getTarget()->getVariables(getNodeId(), redPos, redSize);
00149                 getTarget()->getVariables(getNodeId(), greenPos, greenSize);
00150                 getTarget()->getVariables(getNodeId(), bluePos, blueSize);
00151         }
00152         
00153         void LinearCameraViewPlugin::variableValueUpdated(const QString& name, const VariablesDataVector& values)
00154         {
00155                 if (name == redName)
00156                 {
00157                         red = values;
00158                         componentsReceived |= 1 << 0;
00159                 }
00160                 if (name == greenName)
00161                 {
00162                         green = values;
00163                         componentsReceived |= 1 << 1;
00164                 }
00165                 if (name == blueName)
00166                 {
00167                         blue = values;
00168                         componentsReceived |= 1 << 2;
00169                 }
00170                 
00171                 const int width = qMin(red.size(), qMin(green.size(), blue.size()));
00172                 if ((componentsReceived == 0x7) && width)
00173                 {
00174                         componentsReceived = 0;
00175                         QPixmap pixmap(width, 1);
00176                         QPainter painter(&pixmap);
00177                         
00178                         int min = std::numeric_limits<int>::max();
00179                         int max = std::numeric_limits<int>::min();
00180                         switch (valuesRange)
00181                         {
00182                                 case VALUES_RANGE_AUTO:
00183                                 {
00184                                         // get min and max values for auto range adjustment
00185                                         for (int i = 0; i < width; i++)
00186                                         {
00187                                                 min = std::min(min, (int)red[i]);
00188                                                 min = std::min(min, (int)green[i]);
00189                                                 min = std::min(min, (int)blue[i]);
00190                                                 max = std::max(max, (int)red[i]);
00191                                                 max = std::max(max, (int)green[i]);
00192                                                 max = std::max(max, (int)blue[i]);
00193                                         }
00194                                 }
00195                                 break;
00196                                 
00197                                 case VALUES_RANGE_8BITS:
00198                                 {
00199                                         min = 0;
00200                                         max = 255;
00201                                 }
00202                                 break;
00203                                 
00204                                 case VALUES_RANGE_PERCENT:
00205                                 {
00206                                         min = 0;
00207                                         max = 100;
00208                                 }
00209                                 break;
00210                                 
00211                                 default:
00212                                 assert(false);
00213                                 break;
00214                         }
00215                         
00216                         for (int i = 0; i < width; i++)
00217                         {
00218                                 int range = max - min;
00219                                 if (range == 0)
00220                                         range = 1;
00221                                 int r = ((red[i] - min) * 255) / range;
00222                                 int g = ((green[i] - min) * 255) / range;
00223                                 int b = ((blue[i] - min) * 255) / range;
00224                                 painter.fillRect(i, 0, 1, 1, QColor(r, g, b));
00225                         }
00226                         
00227                         image->setPixmap(pixmap);
00228                 }
00229         }
00230 }


aseba
Author(s): Stéphane Magnenat
autogenerated on Thu Jan 2 2014 11:17:17