console_master.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2015, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL Southwest Research Institute® BE LIABLE 
00021 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00023 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00024 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00027 // DAMAGE.
00028 //
00029 // *****************************************************************************
00030 
00031 #include <swri_console/console_master.h>
00032 #include <swri_console/console_window.h>
00033 #include <swri_console/settings_keys.h>
00034 
00035 #include <QFontDialog>
00036 #include <QSettings>
00037 
00038 namespace swri_console
00039 {
00040 ConsoleMaster::ConsoleMaster(int argc, char** argv):
00041   ros_thread_(argc, argv),
00042   connected_(false),
00043   window_font_(QFont("Ubuntu Mono", 9))
00044 {
00045   // The RosThread takes advantage of queued connections when emitting log messages
00046   // to ensure that the messages are processed in the console window's event thread.
00047   // In order for that to work, we have to manually register the message type with
00048   // Qt's QMetaType system.
00049   qRegisterMetaType<rosgraph_msgs::LogConstPtr>("rosgraph_msgs::LogConstPtr");
00050 
00051   QObject::connect(&bag_reader_, SIGNAL(logReceived(const rosgraph_msgs::LogConstPtr& )),
00052                    &db_, SLOT(queueMessage(const rosgraph_msgs::LogConstPtr&) ));
00053   QObject::connect(&bag_reader_, SIGNAL(finishedReading()),
00054                    &db_, SLOT(processQueue()));
00055 }
00056 
00057 ConsoleMaster::~ConsoleMaster()
00058 {
00059   ros_thread_.shutdown();
00060   ros_thread_.wait();
00061 }
00062 
00063 void ConsoleMaster::createNewWindow()
00064 {
00065   ConsoleWindow* win = new ConsoleWindow(&db_);
00066   windows_.append(win);
00067 
00068   QSettings settings;
00069   window_font_ = settings.value(SettingsKeys::FONT, QFont("Ubuntu Mono", 9)).value<QFont>();
00070   win->setFont(window_font_);
00071   QObject::connect(win, SIGNAL(createNewWindow()),
00072                    this, SLOT(createNewWindow()));
00073 
00074   QObject::connect(&ros_thread_, SIGNAL(connected(bool)),
00075                    win, SLOT(connected(bool)));
00076 
00077   QObject::connect(this,
00078                    SIGNAL(fontChanged(const QFont &)),
00079                    win, SLOT(setFont(const QFont &)));
00080 
00081   QObject::connect(win, SIGNAL(selectFont()),
00082                    this, SLOT(selectFont()));
00083 
00084   QObject::connect(win, SIGNAL(readBagFile()),
00085                    &bag_reader_, SLOT(promptForBagFile()));
00086 
00087 
00088   if (!ros_thread_.isRunning())
00089   {
00090     // There's only one ROS thread, and it services every window.  We need to initialize
00091     // it and its connections to the LogDatabase when we first create a window, but
00092     // after that it doesn't need to be modified again.
00093     QObject::connect(&ros_thread_, SIGNAL(logReceived(const rosgraph_msgs::LogConstPtr& )),
00094                      &db_, SLOT(queueMessage(const rosgraph_msgs::LogConstPtr&) ));
00095 
00096     QObject::connect(&ros_thread_, SIGNAL(spun()),
00097                      &db_, SLOT(processQueue()));
00098 
00099     ros_thread_.start();
00100   }
00101 
00102   win->show();
00103 }
00104 
00105 void ConsoleMaster::fontSelectionChanged(const QFont &font)
00106 {
00107   window_font_ = font;
00108   QSettings settings;
00109   settings.setValue(SettingsKeys::FONT, font);
00110   Q_EMIT fontChanged(window_font_);
00111 }
00112 
00113 void ConsoleMaster::selectFont()
00114 {
00115   QFont starting_font = window_font_;
00116 
00117   QFontDialog dlg(window_font_);
00118     
00119   QObject::connect(&dlg, SIGNAL(currentFontChanged(const QFont &)),
00120                    this, SLOT(fontSelectionChanged(const QFont &)));
00121 
00122   int ret = dlg.exec();
00123 
00124   if (ret == QDialog::Accepted) {
00125     if (window_font_ != dlg.selectedFont()) {
00126       window_font_ = dlg.selectedFont();
00127       Q_EMIT fontChanged(window_font_);
00128     }
00129   } else {
00130     if (window_font_ != starting_font) {
00131       window_font_ = starting_font;
00132       Q_EMIT fontChanged(window_font_);
00133     }
00134   }
00135 }
00136 }  // namespace swri_console


swri_console
Author(s): Elliot Johnson
autogenerated on Tue Sep 12 2017 03:09:35