main_window.cpp
Go to the documentation of this file.
00001 
00013 #include <iostream>
00014 #include <QtGui>
00015 #include <QMessageBox>
00016 
00017 #include <ros/package.h>
00018 #include <rosbag/bag.h>
00019 #include <rosbag/view.h>
00020 
00021 #include <boost/foreach.hpp>
00022 #include <boost/lexical_cast.hpp>
00023 
00024 #include <color_table/main_window.h>
00025 
00026 namespace color_table {
00027 
00028   using namespace Qt;
00029 
00035   MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
00036     ui.setupUi(this); // Calling this incidentally connects all ui's triggers to on_...() callbacks in this class.
00037     initialize();
00038     classWindow.loadDataDirectory(getBaseDirectory() + "/data/");
00039     classWindow.openDefaultColorTable();
00040   }
00041 
00042   void MainWindow::on_actionOpen_Bag_triggered() {
00043 
00044     images.clear();
00045 
00046     QString fileName = QFileDialog::getOpenFileName(this, tr("Open Bag"),
00047         QString((getBaseDirectory() + "/data/").c_str()),
00048         tr("ROS Bag Files (*.bag)"));
00049 
00050     if (fileName.isNull()) {
00051       ui.statusBar->showMessage("User cancelled operation");
00052       return;
00053     }
00054     rosbag::Bag bag;
00055     try {
00056       bag.open(fileName.toStdString(), rosbag::bagmode::Read);
00057     } catch (rosbag::BagIOException e) {
00058       ui.statusBar->showMessage("Error opening bag file!!");
00059       return;
00060     }
00061 
00062     std::vector<std::string> topics;
00063     topics.push_back(std::string("/camera/rgb/image_color"));
00064 
00065     rosbag::View view(bag, rosbag::TopicQuery(topics));
00066     BOOST_FOREACH(rosbag::MessageInstance const m, view) {
00067       sensor_msgs::ImageConstPtr image = m.instantiate<sensor_msgs::Image>();
00068       images.push_back(image);
00069     }
00070 
00071     ui.frameSlider->setEnabled(true);
00072     ui.frameSlider->setMaximum(images.size() - 1);
00073     ui.frameSlider->setMinimum(0);
00074 
00075     ui.currentFrameSpin->setEnabled(true);
00076     ui.currentFrameSpin->setMaximum(images.size() - 1);
00077     ui.currentFrameSpin->setMinimum(0);
00078 
00079     ui.numFrameEdit->setText(QString((boost::lexical_cast<std::string>(images.size() - 1)).c_str()));
00080     
00081     classWindow.changeImage(images[0]); 
00082 
00083     ui.statusBar->showMessage(("Bag file with " + boost::lexical_cast<std::string>(images.size()) + " frames").c_str());
00084     
00085   }
00086 
00090   std::string MainWindow::getBaseDirectory() {
00091     return ros::package::getPath("color_table");
00092   }
00093 
00094   void MainWindow::on_currentFrameSpin_valueChanged(int value) {
00095     ui.frameSlider->setValue(value);
00096     classWindow.changeImage(images[value]);
00097   }
00098 
00099   void MainWindow::on_frameSlider_sliderMoved(int value) {
00100     ui.currentFrameSpin->setValue(value);  //implicitly calls the slot for currentFrameSpin -> value Changed
00101   }
00102 
00103   void MainWindow::initialize() {
00104     ui.frameSlider->setEnabled(false);
00105     ui.currentFrameSpin->setEnabled(false);
00106     ui.numFrameEdit->setText(QString("0"));    
00107   }
00108 
00113   void MainWindow::closeEvent(QCloseEvent *event) {
00114     classWindow.close();
00115     //WriteSettings();
00116     event->accept();
00117   }
00118 
00123   void MainWindow::show() {
00124     QMainWindow::show();
00125     classWindow.show();
00126   }
00127 
00128 }  // namespace color_table


color_table
Author(s): Piyush Khandelwal
autogenerated on Mon Jan 6 2014 11:54:34