Go to the documentation of this file.00001
00045 #include "mainwindow.h"
00046 #include "ui_mainwindow.h"
00047
00048 #include <ros/ros.h>
00049
00050 #include <tinyxml.h>
00051
00052 #include <QDir>
00053 #include <QFileDialog>
00054 #include <QTableWidgetItem>
00055 #include <QDebug>
00056 #include <QLabel>
00057 #include <QMessageBox>
00058
00059 #include "comthread.h"
00060 #include "downloadobjectdialog.h"
00061
00062 struct MetaData {
00063 std::string model_name, model_type;
00064 size_t face_count;
00065 double scale;
00066
00067 bool load(const std::string& filename) {
00068 TiXmlDocument doc(filename);
00069 bool loaded = doc.LoadFile();
00070 if (!loaded)
00071 return false;
00072
00073 TiXmlHandle hdoc(&doc);
00074 TiXmlHandle model_handle = hdoc.FirstChildElement("model");
00075 TiXmlElement *model_element = model_handle.Element();
00076 if (model_element == NULL)
00077 return false;
00078 TiXmlElement *name_element = model_handle.FirstChildElement("name").Element();
00079 if (name_element == NULL)
00080 return false;
00081 model_name = name_element->GetText();
00082 TiXmlElement *type_element = model_handle.FirstChildElement("type").Element();
00083 if (type_element == NULL)
00084 return false;
00085 model_type = type_element->GetText();
00086 TiXmlElement *faces_element = model_handle.FirstChildElement("faces").Element();
00087 if (faces_element == NULL)
00088 return false;
00089 std::string faces_str = faces_element->GetText();
00090 face_count = boost::lexical_cast<int>(faces_str);
00091
00092 TiXmlHandle dimensions_handle = model_handle.FirstChildElement("dimensions");
00093 TiXmlElement *scale_element = dimensions_handle.FirstChildElement("scale").Element();
00094 if (scale_element == NULL)
00095 return false;
00096 std::string scale_str = scale_element->GetText();
00097 scale = boost::lexical_cast<double>(scale_str);
00098
00099 return true;
00100 }
00101 };
00102
00103 MainWindow::MainWindow(QWidget *parent) :
00104 QMainWindow(parent),
00105 ui(new Ui::MainWindow), comthread(new ComThread)
00106 {
00107 ui->setupUi(this);
00108
00109 connect(this, SIGNAL(model_added(QString,QString,QString)), comthread, SLOT(publishModelDir(QString,QString,QString)));
00110
00111 connect(comthread, SIGNAL(updateZaragozaDetectionImg(QImage)), this, SLOT(updateZaragozaDetectionImg(QImage)));
00112 connect(comthread, SIGNAL(updateKinectDetectionImg(QImage)), this, SLOT(updateKinectDetectionImg(QImage)));
00113
00114 imgLabelZaragoza = new QLabel();
00115 imgLabelZaragoza->resize(640, 480);
00116
00117 imgLabelKinect = new QLabel();
00118 imgLabelKinect->resize(640, 480);
00119
00120 comthread->start();
00121 }
00122
00123 void MainWindow::closeEvent(QCloseEvent *) {
00124 imgLabelZaragoza->close();
00125 imgLabelKinect->close();
00126 }
00127
00128 MainWindow::~MainWindow()
00129 {
00130 ros::shutdown();
00131 delete ui;
00132 }
00133
00134 void MainWindow::on_downloadModelButton_clicked()
00135 {
00136 DownloadObjectDialog dlg(this);
00137 int result = dlg.exec();
00138
00139 if (result == QDialog::Rejected)
00140 return;
00141
00142 Q_FOREACH(QString model_dir, dlg.getDownloadedModelDirs()) {
00143 addLocalModel(model_dir);
00144 }
00145 }
00146
00147 void MainWindow::addLocalModel(QString model_dir_str) {
00148 if (!model_dirs.contains(model_dir_str)) {
00149 QDir model_dir(model_dir_str);
00150
00151 MetaData md;
00152
00153
00154
00155
00156
00157
00158 if (!md.load(model_dir.filePath("meta.xml").toStdString())) {
00159 QMessageBox::warning(this, "Error opening model", "Could not load model directory. Check the 'meta.xml' file.");
00160 return;
00161 }
00162
00163 QTableWidgetItem *pathItem = new QTableWidgetItem(model_dir_str);
00164 QTableWidgetItem *nameItem = new QTableWidgetItem(QString::fromStdString(md.model_name));
00165 QTableWidgetItem *typeItem = new QTableWidgetItem(QString::fromStdString(md.model_type));
00166
00167 int row = ui->modelsTableWidget->rowCount();
00168 ui->modelsTableWidget->insertRow(row);
00169 ui->modelsTableWidget->setItem(row, 0, pathItem);
00170 ui->modelsTableWidget->setItem(row, 1, nameItem);
00171 ui->modelsTableWidget->setItem(row, 2, typeItem);
00172
00173 model_dirs.insert(model_dir_str);
00174 emit model_added(model_dir_str, QString::fromStdString(md.model_type), QString::fromStdString(md.model_name));
00175 }
00176 }
00177
00178 void MainWindow::on_addLocalModelButton_clicked()
00179 {
00180 QString model_dir_str = QFileDialog::getExistingDirectory(this, "Please specify a model directory");
00181
00182 if (!model_dir_str.isEmpty())
00183 addLocalModel(model_dir_str);
00184 }
00185
00186 void MainWindow::updateZaragozaDetectionImg(QImage img) {
00187 if (!imgLabelZaragoza->isVisible())
00188 imgLabelZaragoza->show();
00189 imgLabelZaragoza->setPixmap(QPixmap::fromImage(img));
00190 }
00191
00192 void MainWindow::updateKinectDetectionImg(QImage img) {
00193 if (!imgLabelKinect->isVisible())
00194 imgLabelKinect->show();
00195 imgLabelKinect->setPixmap(QPixmap::fromImage(img));
00196 }
00197
00198 void MainWindow::on_resendModelsButton_clicked() {
00199 for (int i=0; i<ui->modelsTableWidget->rowCount(); i++){
00200 QTableWidgetItem *pathItem = ui->modelsTableWidget->item(i, 0);
00201 QTableWidgetItem *nameItem = ui->modelsTableWidget->item(i, 1);
00202 QTableWidgetItem *typeItem = ui->modelsTableWidget->item(i, 2);
00203
00204 emit model_added(pathItem->text(), typeItem->text(), nameItem->text());
00205 }
00206 }