]> sigrok.org Git - pulseview.git/commitdiff
Support specifying input formats on the command line
authorJoel Holdsworth <redacted>
Sun, 12 Apr 2015 14:08:51 +0000 (15:08 +0100)
committerUwe Hermann <redacted>
Thu, 11 Jun 2015 18:38:48 +0000 (20:38 +0200)
Also removes the signal dispatch that was previously used to delay
the file loading until after the constructor.

doc/pulseview.1
main.cpp
pv/mainwindow.cpp
pv/mainwindow.hpp

index b830f08cdae9f01104f539da497046876d4fa9fa..4b103f79fc689e5b4c7bcf104d05f1726212606d 100644 (file)
@@ -39,6 +39,15 @@ Show a help text and exit.
 .TP
 .B "\-V, \-\-version"
 Show version information and exit.
+.TP
+.BR "\-i, \-\-input\-file " <filename>
+Load input from a file. If the
+.B \-\-input\-format
+option is not supplied, pulseview attempts to load the file as a sigrok session
+file.
+.TP
+.BR "\-I, \-\-input\-format " <format>
+Specifies the format of the input file to be loaded.
 .SH "EXIT STATUS"
 .B PulseView
 exits with 0 on success, 1 on most failures.
index 285c1535f3c20898307923de6c49ad08bcbfc899..61ec03a840243be3dd11f4654d8c9f6fa76b6590 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -53,7 +53,7 @@ void usage()
 {
        fprintf(stdout,
                "Usage:\n"
-               "  %s [OPTION…] [FILE] — %s\n"
+               "  %s [OPTION…] — %s\n"
                "\n"
                "Help Options:\n"
                "  -h, -?, --help                  Show help option\n"
@@ -61,6 +61,8 @@ void usage()
                "Application Options:\n"
                "  -V, --version                   Show release version\n"
                "  -l, --loglevel                  Set libsigrok/libsigrokdecode loglevel\n"
+               "  -i, --input-file                Load input from file\n"
+               "  -I, --input-format              Input format\n"
                "\n", PV_BIN_NAME, PV_DESCRIPTION);
 }
 
@@ -68,7 +70,7 @@ int main(int argc, char *argv[])
 {
        int ret = 0;
        std::shared_ptr<sigrok::Context> context;
-       const char *open_file = NULL;
+       std::string open_file, open_file_format;
 
        Application a(argc, argv);
 
@@ -83,11 +85,13 @@ int main(int argc, char *argv[])
                        {"help", no_argument, 0, 'h'},
                        {"version", no_argument, 0, 'V'},
                        {"loglevel", required_argument, 0, 'l'},
+                       {"input-file", required_argument, 0, 'i'},
+                       {"input-format", required_argument, 0, 'I'},
                        {0, 0, 0, 0}
                };
 
                const int c = getopt_long(argc, argv,
-                       "l:Vh?", long_options, nullptr);
+                       "l:Vh?i:I:", long_options, nullptr);
                if (c == -1)
                        break;
 
@@ -113,14 +117,21 @@ int main(int argc, char *argv[])
 
                        break;
                }
+
+               case 'i':
+                       open_file = optarg;
+                       break;
+
+               case 'I':
+                       open_file_format = optarg;
+                       break;
                }
        }
 
-       if (argc - optind > 1) {
-               fprintf(stderr, "Only one file can be openened.\n");
+       if (argc != optind) {
+               fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
                return 1;
-       } else if (argc - optind == 1)
-               open_file = argv[argc - 1];
+       }
 
        // Initialise libsigrok
        context = sigrok::Context::create();
@@ -143,7 +154,8 @@ int main(int argc, char *argv[])
                        pv::DeviceManager device_manager(context);
 
                        // Initialise the main window
-                       pv::MainWindow w(device_manager, open_file);
+                       pv::MainWindow w(device_manager,
+                               open_file, open_file_format);
                        w.show();
 
 #ifdef ENABLE_SIGNALS
index eb567af8dfe077d4954ffe6613333e7bb2f61da2..797944b9bea2295a50b00d0d374e3b8ee6c0f186 100644 (file)
 #include <glib.h>
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
+using std::cerr;
+using std::endl;
 using std::list;
 using std::map;
+using std::pair;
 using std::shared_ptr;
 using std::string;
 using std::vector;
@@ -90,7 +93,7 @@ const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
 
 MainWindow::MainWindow(DeviceManager &device_manager,
-       const char *open_file_name,
+       string open_file_name, string open_file_format,
        QWidget *parent) :
        QMainWindow(parent),
        device_manager_(device_manager),
@@ -111,12 +114,10 @@ MainWindow::MainWindow(DeviceManager &device_manager,
 {
        setup_ui();
        restore_ui_settings();
-       if (open_file_name) {
-               const QString s(QString::fromUtf8(open_file_name));
-               QMetaObject::invokeMethod(this, "load_file",
-                       Qt::QueuedConnection,
-                       Q_ARG(QString, s));
-       }
+       if (open_file_name.empty())
+               select_init_device();
+       else
+               load_init_file(open_file_name, open_file_format);
 }
 
 QAction* MainWindow::action_open() const
@@ -470,6 +471,59 @@ void MainWindow::setup_ui()
                SLOT(device_selected()));
 }
 
+void MainWindow::select_init_device() {
+       QSettings settings;
+       map<string, string> dev_info;
+       list<string> key_list;
+
+       // Re-select last used device if possible.
+       settings.beginGroup("Device");
+       key_list.push_back("vendor");
+       key_list.push_back("model");
+       key_list.push_back("version");
+       key_list.push_back("serial_num");
+       key_list.push_back("connection_id");
+
+       for (string key : key_list) {
+               const QString k = QString::fromStdString(key);
+               if (!settings.contains(k))
+                       continue;
+
+               const string value = settings.value(k).toString().toStdString();
+               if (!value.empty())
+                       dev_info.insert(std::make_pair(key, value));
+       }
+
+       const shared_ptr<devices::HardwareDevice> device =
+               device_manager_.find_device_from_info(dev_info);
+       select_device(device);
+       update_device_list();
+
+       settings.endGroup();
+}
+
+void MainWindow::load_init_file(const std::string &file_name,
+       const std::string &format) {
+       shared_ptr<InputFormat> input_format;
+
+       if (!format.empty()) {
+               const map<string, shared_ptr<InputFormat> > formats =
+                       device_manager_.context()->input_formats();
+               const auto iter = find_if(formats.begin(), formats.end(),
+                       [&](const pair<string, shared_ptr<InputFormat> > f) {
+                               return f.first == format; });
+               if (iter == formats.end()) {
+                       cerr << "Unexpected input format: " << format << endl;
+                       return;
+               }
+
+               input_format = (*iter).second;
+       }
+
+       load_file(QString::fromStdString(file_name), input_format);
+}
+
+
 void MainWindow::save_ui_settings()
 {
        QSettings settings;
@@ -510,9 +564,6 @@ void MainWindow::restore_ui_settings()
 {
        QSettings settings;
 
-       map<string, string> dev_info;
-       list<string> key_list;
-
        settings.beginGroup("MainWindow");
 
        if (settings.contains("geometry")) {
@@ -522,31 +573,6 @@ void MainWindow::restore_ui_settings()
                resize(1000, 720);
 
        settings.endGroup();
-
-       // Re-select last used device if possible.
-       settings.beginGroup("Device");
-       key_list.push_back("vendor");
-       key_list.push_back("model");
-       key_list.push_back("version");
-       key_list.push_back("serial_num");
-       key_list.push_back("connection_id");
-
-       for (string key : key_list) {
-               const QString k = QString::fromStdString(key);
-               if (!settings.contains(k))
-                       continue;
-
-               const string value = settings.value(k).toString().toStdString();
-               if (!value.empty())
-                       dev_info.insert(std::make_pair(key, value));
-       }
-
-       const shared_ptr<devices::HardwareDevice> device =
-               device_manager_.find_device_from_info(dev_info);
-       select_device(device);
-       update_device_list();
-
-       settings.endGroup();
 }
 
 void MainWindow::session_error(
@@ -562,21 +588,6 @@ void MainWindow::update_device_list()
        main_bar_->update_device_list();
 }
 
-void MainWindow::closeEvent(QCloseEvent *event)
-{
-       save_ui_settings();
-       event->accept();
-}
-
-void MainWindow::keyReleaseEvent(QKeyEvent *event)
-{
-       if (event->key() == Qt::Key_Alt) {
-               menuBar()->setHidden(!menuBar()->isHidden());
-               menuBar()->setFocus();
-       }
-       QMainWindow::keyReleaseEvent(event);
-}
-
 void MainWindow::load_file(QString file_name,
        std::shared_ptr<sigrok::InputFormat> format,
        const std::map<std::string, Glib::VariantBase> &options)
@@ -610,6 +621,21 @@ void MainWindow::load_file(QString file_name,
                session_error(errorMessage, infoMessage); });
 }
 
+void MainWindow::closeEvent(QCloseEvent *event)
+{
+       save_ui_settings();
+       event->accept();
+}
+
+void MainWindow::keyReleaseEvent(QKeyEvent *event)
+{
+       if (event->key() == Qt::Key_Alt) {
+               menuBar()->setHidden(!menuBar()->isHidden());
+               menuBar()->setFocus();
+       }
+       QMainWindow::keyReleaseEvent(event);
+}
+
 void MainWindow::show_session_error(
        const QString text, const QString info_text)
 {
index a6b2c89831ab60ab6796297918bce86eb3277430..6a4bdfc80f4b46134fde599d33f4eea2dda469a1 100644 (file)
@@ -78,7 +78,8 @@ private:
 
 public:
        explicit MainWindow(DeviceManager &device_manager,
-               const char *open_file_name = nullptr,
+               std::string open_file_name = std::string(),
+               std::string open_file_format = std::string(),
                QWidget *parent = 0);
 
        QAction* action_open() const;
@@ -107,6 +108,11 @@ public Q_SLOTS:
 private:
        void setup_ui();
 
+       void select_init_device();
+
+       void load_init_file(const std::string &file_name,
+               const std::string &format);
+
        void save_ui_settings();
 
        void restore_ui_settings();
@@ -118,17 +124,17 @@ private:
         */
        void update_device_list();      
 
+       void load_file(QString file_name,
+               std::shared_ptr<sigrok::InputFormat> format = nullptr,
+               const std::map<std::string, Glib::VariantBase> &options =
+                       std::map<std::string, Glib::VariantBase>());
+
 private:
        void closeEvent(QCloseEvent *event);
 
        void keyReleaseEvent(QKeyEvent *event);
 
 private Q_SLOTS:
-       void load_file(QString file_name,
-               std::shared_ptr<sigrok::InputFormat> format = nullptr,
-               const std::map<std::string, Glib::VariantBase> &options =
-                       std::map<std::string, Glib::VariantBase>());
-
        void show_session_error(
                const QString text, const QString info_text);