PARP Research Group University of Murcia, Spain


src/qvgui/qvcameraworkerwidget.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008, 2009. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <qvgui/qvcameraworkerwidget.h>
00026 
00027 #include <QVMPlayerCameraWorker>
00028 #include <QVYUV4MPEG2CameraWorker>
00029 
00030 #include <QFileDialog>
00031 
00032 QVCameraWorkerWidget::QVCameraWorkerWidget(QVCameraWorker *cam, QWidget *parent): QWidget(parent), QVPropertyContainer("")
00033         {
00034         slider_active = true;
00035         form.setupUi(this);
00036         this->camera = cam;
00037 
00038         // Set prop. container name:
00039         setName(QString("GUI for camera ")+cam->getName());
00040 
00041         // First, we initialize our properties by looking at the initial properties of the camera.
00042         // Danger: Here we must read directly from camera properties, in order to initialize correctly the values of the
00043         // widget. We need to do it this way to avoid the need of making them also output properties in the camera
00044         // In any case, here it is still safe to do this, because the camera worker is still not running. It is a similar
00045         // case to that of a qvparaminspectorwidget.
00046         int cols = camera->getPropertyValue<int>("Cols"), rows = camera->getPropertyValue<int>("Rows");
00047         QString url = camera->getPropertyValue<QString>("URL");
00048         bool noloop = camera->getPropertyValue<bool>("NoLoop"), deinterlaced = camera->getPropertyValue<bool>("Deinterlaced");
00049         bool realTime = camera->getPropertyValue<bool>("RealTime");
00050 
00051         setPropertyValue<int>("Cols",cols);
00052         setPropertyValue<int>("Rows",rows);
00053         setPropertyValue<QString>("URL",url);
00054         setPropertyValue<bool>("NoLoop",noloop);
00055         setPropertyValue<bool>("Deinterlaced",deinterlaced);
00056 
00057         form.spinbox_cols->setValue(cols);
00058         form.spinbox_rows->setValue(rows);
00059         form.url_line_edit->setText(url);
00060         form.noloop_button->setChecked(noloop);
00061         form.deinterlaced_button->setChecked(deinterlaced);
00062         form.real_time_label->setText(realTime?"Real Time":"Max speed");
00063 
00064         // Subscribe (asynchronously) to all relevant input properties of the camera:
00065         subscribeToInputProperty(cam,"NoLoop");
00066         subscribeToInputProperty(cam,"Deinterlaced");
00067         subscribeToInputProperty(cam,"URL");
00068         subscribeToInputProperty(cam,"Cols");
00069         subscribeToInputProperty(cam,"Rows");
00070 
00071         subscribeToOutputProperty(cam,"Opened");
00072         subscribeToOutputProperty(cam,"FPS");
00073         subscribeToOutputProperty(cam,"Frames");
00074         subscribeToOutputProperty(cam,"ColsR");
00075         subscribeToOutputProperty(cam,"RowsR");
00076         subscribeToOutputProperty(cam,"Pos");
00077         subscribeToOutputProperty(cam,"Length");
00078 
00079         connect(cam,SIGNAL(opened()),this,SLOT(updateOpened())/*,Qt::BlockingQueuedConnection*/);
00080         connect(cam,SIGNAL(closed()),this,SLOT(updateClosed())/*,Qt::BlockingQueuedConnection*/);
00081         connect(cam,SIGNAL(grabbed()),this,SLOT(newFrameGrabbed())/*,Qt::BlockingQueuedConnection*/); // FIXME connection type OK?
00082 
00083         connect(form.stop_button,SIGNAL(pressed()),cam,SLOT(resetCameraWorker()));
00084         connect(form.stop_button,SIGNAL(pressed()),this,SLOT(stopPressed()));
00085 
00086         connect(form.pause_button,SIGNAL(pressed()),cam,SLOT(pause()));
00087         connect(form.pause_button,SIGNAL(pressed()),this,SLOT(pausePressed()));
00088 
00089         connect(form.play_button,SIGNAL(pressed()),cam,SLOT(unPause()));
00090         connect(form.play_button,SIGNAL(pressed()),this,SLOT(playPressed()));
00091 
00092         connect(form.next_button,SIGNAL(pressed()),cam,SLOT(step()));
00093         connect(form.next_button,SIGNAL(pressed()),this,SLOT(nextPressed()));
00094 
00095         connect(form.seek_slider,SIGNAL(sliderPressed()),this,SLOT(seekPressed()));
00096         connect(form.seek_slider,SIGNAL(sliderReleased()),this,SLOT(seekReleased()));
00097 
00098         connect(this,SIGNAL(seek_requested(double)),camera,SLOT(setCurrentPos(double)));
00099 
00100         connect(form.url_line_edit,SIGNAL(editingFinished()),this,SLOT(somePropertyChanged()));
00101         connect(form.spinbox_cols,SIGNAL(valueChanged(int)),this,SLOT(somePropertyChanged()));
00102         connect(form.spinbox_rows,SIGNAL(valueChanged(int)),this,SLOT(somePropertyChanged()));
00103         connect(form.noloop_button,SIGNAL(toggled(bool)),this,SLOT(somePropertyChanged()));
00104         connect(form.deinterlaced_button,SIGNAL(toggled(bool)),this,SLOT(somePropertyChanged()));
00105 
00106         connect(form.file_open_button,SIGNAL(pressed()),this,SLOT(fileOpenButtonPressed()));
00107         connect(this,SIGNAL(file_selected()),cam,SLOT(reopen()));
00108 
00109         connect(form.reopen_button,SIGNAL(pressed()),cam,SLOT(reopen()));
00110 
00111         }
00112 
00113 void QVCameraWorkerWidget::subscribeToOutputProperty(QVPropertyContainer *qvp, QString name, LinkType linktype)
00114         {
00115         // Adds a property with the same name than that of another qvpropertycontainer, and links to it (from there to here)
00116         // with the desired link type. Observe that the property is initialized with its current value in the qvp object:
00117         addPropertyFromQVariant(name, inputFlag, qvp->getPropertyQVariantValue(name),qvp->getPropertyInfo(name));
00118         qvp->linkProperty(name,this,name,linktype);
00119         }
00120 
00121 void QVCameraWorkerWidget::subscribeToInputProperty(QVPropertyContainer *qvp, QString name, LinkType linktype)
00122         {
00123         // Adds a property with the same name than that of another qvpropertycontainer, and links to it (from here to there)
00124         // with the desired link type. Observe that the property is initialized with its current value in the qvp object:
00125         addPropertyFromQVariant(name, outputFlag, qvp->getPropertyQVariantValue(name),qvp->getPropertyInfo(name));
00126         this->linkProperty(name,qvp,name,linktype);
00127         }
00128 
00129 void QVCameraWorkerWidget::somePropertyChanged()
00130         {
00131         setPropertyValue<QString>("URL",form.url_line_edit->text());
00132         setPropertyValue<int>("Cols",form.spinbox_cols->value());
00133         setPropertyValue<int>("Rows",form.spinbox_rows->value());
00134         setPropertyValue<bool>("NoLoop",form.noloop_button->isChecked());
00135         setPropertyValue<bool>("Deinterlaced",form.deinterlaced_button->isChecked());
00136 
00137         writeOutputProperties();
00138         }
00139 
00140 void QVCameraWorkerWidget::updateOpened()
00141         {
00142         form.stop_button->setEnabled(TRUE);
00143         form.pause_button->setEnabled(TRUE);
00144         form.play_button->setEnabled(FALSE);
00145         form.next_button->setEnabled(FALSE);
00146         }
00147 
00148 void QVCameraWorkerWidget::updateClosed()
00149         {
00150         form.stop_button->setEnabled(FALSE);
00151         form.pause_button->setEnabled(FALSE);
00152         form.play_button->setEnabled(FALSE);
00153         form.next_button->setEnabled(FALSE);
00154         }
00155 
00156 void QVCameraWorkerWidget::stopPressed()
00157         {
00158         form.stop_button->setEnabled(FALSE);
00159         form.pause_button->setEnabled(FALSE);
00160         form.play_button->setEnabled(FALSE);
00161         form.next_button->setEnabled(FALSE);
00162         }
00163 
00164 void QVCameraWorkerWidget::pausePressed()
00165         {
00166         form.pause_button->setEnabled(FALSE);
00167         form.play_button->setEnabled(TRUE);
00168         form.next_button->setEnabled(TRUE);
00169         }
00170 
00171 void QVCameraWorkerWidget::playPressed()
00172         {
00173         form.pause_button->setEnabled(TRUE);
00174         form.play_button->setEnabled(FALSE);
00175         form.next_button->setEnabled(FALSE);
00176         }
00177 
00178 void QVCameraWorkerWidget::nextPressed()
00179         {
00180         }
00181 
00182 void QVCameraWorkerWidget::newFrameGrabbed()
00183         {
00184         readInputProperties();
00185 
00186         form.frames_label->setText(QString("Frames: %1").arg(getPropertyValue<int>("Frames")));
00187         form.size_label->setText(QString("Size: %1x%2").arg(getPropertyValue<int>("ColsR")).arg(getPropertyValue<int>("RowsR")));
00188         QString len_string = QString("%1").arg(getPropertyValue<double>("Length"),1,'f',1);
00189         QString pos_string = QString("%1").arg(getPropertyValue<double>("Pos"),1,'f',1);
00190         form.pos_label->setText("Position: " + pos_string + "/" + len_string );
00191         form.fps_label->setText(QString("FPS: %1").arg(getPropertyValue<int>("FPS")));
00192 
00193         if(getPropertyValue<double>("Length") > 0)
00194                 {
00195                 form.seek_slider->setEnabled(TRUE);
00196                 if(slider_active)
00197                         {
00198                         int pos = (int)(form.seek_slider->maximum()*getPropertyValue<double>("Pos")/getPropertyValue<double>("Length"));
00199                         form.seek_slider->setValue(pos);
00200                         }
00201                 }
00202         }
00203 
00204 
00205 void QVCameraWorkerWidget::seekPressed()
00206         {
00207         slider_active = false;
00208         }
00209 
00210 void QVCameraWorkerWidget::seekReleased()
00211         {
00212         slider_active = true;
00213         emit seek_requested(form.seek_slider->value()*getPropertyValue<double>("Length")/form.seek_slider->maximum());
00214         }
00215 
00216 void QVCameraWorkerWidget::fileOpenButtonPressed()
00217         {
00218         QFileDialog dialog(this);
00219 
00220         QStringList filters;
00221         if((dynamic_cast<QVMPlayerCameraWorker*>(camera)) != NULL)
00222                 filters << "Video Files (*.avi *.dv *.mpg *.mpeg *.yuv *.wmv)" << "All files (*)";
00223         else if((dynamic_cast<QVYUV4MPEG2CameraWorker*>(camera)) != NULL)
00224                 filters << "Video Files (*.yuv)" << "All files (*)";
00225         else
00226                 filters << "All files (*)";
00227 
00228         #if QT_VERSION >= 0x040400
00229                 dialog.setNameFilters(filters);
00230         #else
00231                 dialog.setFilters(filters);
00232         #endif
00233         
00234         dialog.setWindowTitle("Open video file");
00235         dialog.setFileMode(QFileDialog::ExistingFile);
00236         dialog.setViewMode(QFileDialog::Detail);
00237 
00238         QString str = getPropertyValue<QString>("URL");
00239         QStringList strl = str.split("/");
00240         strl.removeLast();
00241         str = strl.join("/");
00242         QDir dir(str);
00243 
00244         if(dir.exists())
00245                 dialog.setDirectory(str);
00246         else
00247                 dialog.setDirectory(QDir::currentPath());
00248 
00249         QString fileName;
00250         if (dialog.exec())
00251                 {
00252                 QString filename = dialog.selectedFiles().first();
00253                 setPropertyValue<QString>("URL",filename);
00254                 form.url_line_edit->setText(filename);
00255                 writeOutputProperties();
00256                 emit file_selected();
00257                 }
00258         }
00259 
00260 



QVision framework. PARP research group, copyright 2007, 2008.