From: Joel Holdsworth Date: Sun, 12 Apr 2015 14:08:51 +0000 (+0100) Subject: Support specifying input formats on the command line X-Git-Tag: pulseview-0.3.0~191 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=e3c79b07a04a6621e7f3d8d36c1afac5ef7d0db5;p=pulseview.git Support specifying input formats on the command line Also removes the signal dispatch that was previously used to delay the file loading until after the constructor. --- diff --git a/doc/pulseview.1 b/doc/pulseview.1 index b830f08c..4b103f79 100644 --- a/doc/pulseview.1 +++ b/doc/pulseview.1 @@ -39,6 +39,15 @@ Show a help text and exit. .TP .B "\-V, \-\-version" Show version information and exit. +.TP +.BR "\-i, \-\-input\-file " +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 " +Specifies the format of the input file to be loaded. .SH "EXIT STATUS" .B PulseView exits with 0 on success, 1 on most failures. diff --git a/main.cpp b/main.cpp index 285c1535..61ec03a8 100644 --- 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 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 diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index eb567af8..797944b9 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -68,8 +68,11 @@ #include #include +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 dev_info; + list 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 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 input_format; + + if (!format.empty()) { + const map > formats = + device_manager_.context()->input_formats(); + const auto iter = find_if(formats.begin(), formats.end(), + [&](const pair > 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 dev_info; - list 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 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 format, const std::map &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) { diff --git a/pv/mainwindow.hpp b/pv/mainwindow.hpp index a6b2c898..6a4bdfc8 100644 --- a/pv/mainwindow.hpp +++ b/pv/mainwindow.hpp @@ -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 format = nullptr, + const std::map &options = + std::map()); + private: void closeEvent(QCloseEvent *event); void keyReleaseEvent(QKeyEvent *event); private Q_SLOTS: - void load_file(QString file_name, - std::shared_ptr format = nullptr, - const std::map &options = - std::map()); - void show_session_error( const QString text, const QString info_text);