#include "devicemanager.hpp"
#include "util.hpp"
+#include "devices/hardwaredevice.hpp"
#include "dialogs/about.hpp"
#include "toolbars/mainbar.hpp"
#include "view/view.hpp"
#include <stdarg.h>
#include <libsigrokcxx/libsigrokcxx.hpp>
+using std::dynamic_pointer_cast;
using std::list;
using std::make_shared;
using std::map;
QWidget *parent) :
QMainWindow(parent),
device_manager_(device_manager),
- session_(device_manager),
- open_file_name_(open_file_name),
- open_file_format_(open_file_format),
action_view_sticky_scrolling_(new QAction(this)),
action_view_coloured_bg_(new QAction(this)),
action_about_(new QAction(this))
setup_ui();
restore_ui_settings();
+
+ if (!open_file_name.empty()) {
+ shared_ptr<Session> session = add_session();
+ session->main_bar()->load_init_file(open_file_name, open_file_format);
+ }
+
+ // Add empty default session if there aren't any sessions
+ if (sessions_.size() == 0) {
+ shared_ptr<Session> session = add_session();
+
+ map<string, string> dev_info;
+ shared_ptr<devices::HardwareDevice> other_device, demo_device;
+
+ // Use any available device that's not demo
+ for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
+ if (dev->hardware_device()->driver()->name() == "demo") {
+ demo_device = dev;
+ } else {
+ other_device = dev;
+ }
+ }
+
+ // ...and if there isn't any, just use demo then
+ session->main_bar()->select_device(other_device ?
+ other_device : demo_device);
+ }
}
MainWindow::~MainWindow()
dock->setWidget(0);
const std::shared_ptr<pv::view::View> view = entry.second;
- session_.deregister_view(view);
+
+ for (shared_ptr<Session> session : sessions_)
+ if (session->has_view(view))
+ session->deregister_view(view);
}
}
shared_ptr<MainBar> main_bar = session.main_bar();
if (!main_bar) {
- main_bar = make_shared<MainBar>(session_, *this,
- open_file_name_, open_file_format_);
+ main_bar = make_shared<MainBar>(session, *this);
dock_main->addToolBar(main_bar.get());
session.set_main_bar(main_bar);
-
- open_file_name_.clear();
- open_file_format_.clear();
}
main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
}
return v;
}
+shared_ptr<Session> MainWindow::add_session()
+{
+ int id = sessions_.size();
+ QString name = tr("Untitled-%1").arg(id + 1);
+
+ shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
+
+ sessions_.push_back(session);
+
+ shared_ptr<view::View> main_view =
+ add_view(name, pv::view::TraceView, *session);
+
+ return session;
+}
+
void MainWindow::setup_ui()
{
setObjectName(QString::fromUtf8("MainWindow"));
action_about_->setObjectName(QString::fromUtf8("actionAbout"));
action_about_->setText(tr("&About..."));
- // Set up the initial view
- shared_ptr<view::View> main_view =
- add_view(tr("Untitled"), pv::view::TraceView, session_);
-
// Set the title
setWindowTitle(tr("PulseView"));
}
void MainWindow::save_ui_settings()
{
QSettings settings;
-
- map<string, string> dev_info;
- list<string> key_list;
+ int id = 0;
settings.beginGroup("MainWindow");
settings.setValue("state", saveState());
settings.setValue("geometry", saveGeometry());
settings.endGroup();
- if (session_.device()) {
- 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");
-
- dev_info = device_manager_.get_device_info(
- session_.device());
-
- for (string key : key_list) {
- if (dev_info.count(key))
- settings.setValue(QString::fromUtf8(key.c_str()),
- QString::fromUtf8(dev_info.at(key).c_str()));
- else
- settings.remove(QString::fromUtf8(key.c_str()));
+ for (shared_ptr<Session> session : sessions_) {
+ // Ignore sessions using the demo device
+ if (session->device()) {
+ shared_ptr<devices::HardwareDevice> device =
+ dynamic_pointer_cast< devices::HardwareDevice >
+ (session->device());
+
+ if (device->hardware_device()->driver()->name() == "demo")
+ continue;
}
+ settings.beginGroup("Session" + QString::number(id++));
+ session->save_settings(settings);
settings.endGroup();
}
+
+ settings.setValue("sessions", id);
}
void MainWindow::restore_ui_settings()
{
QSettings settings;
+ int i, session_count;
settings.beginGroup("MainWindow");
resize(1000, 720);
settings.endGroup();
+
+ session_count = settings.value("sessions", 0).toInt();
+
+ for (i = 0; i < session_count; i++) {
+ settings.beginGroup("Session" + QString::number(i));
+ shared_ptr<Session> session = add_session();
+ session->restore_settings(settings);
+ settings.endGroup();
+ }
}
void MainWindow::closeEvent(QCloseEvent *event)
std::shared_ptr<pv::view::View> add_view(const QString &title,
view::ViewType type, Session &session);
+ std::shared_ptr<Session> add_session();
+
private:
void setup_ui();
private:
DeviceManager &device_manager_;
- Session session_;
+ std::vector< std::shared_ptr<Session> > sessions_;
std::map< std::shared_ptr<QDockWidget>,
std::shared_ptr<pv::view::View> > view_docks_;
- std::string open_file_name_, open_file_format_;
-
QAction *const action_view_sticky_scrolling_;
QAction *const action_view_coloured_bg_;
QAction *const action_about_;
using Glib::Variant;
namespace pv {
-Session::Session(DeviceManager &device_manager) :
+Session::Session(DeviceManager &device_manager, QString name) :
device_manager_(device_manager),
+ name_(name),
capture_state_(Stopped),
cur_samplerate_(0)
{
return device_;
}
+QString Session::name() const
+{
+ return name_;
+}
+
+void Session::set_name(QString name)
+{
+ if (default_name_.isEmpty())
+ default_name_ = name;
+
+ name_ = name;
+
+ name_changed();
+}
+
std::shared_ptr<pv::view::View> Session::main_view() const
{
return main_view_;
return main_bar_;
}
+void Session::save_settings(QSettings &settings) const
+{
+ map<string, string> dev_info;
+ list<string> key_list;
+
+ if (device_) {
+ 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");
+
+ dev_info = device_manager_.get_device_info(device_);
+
+ for (string key : key_list) {
+ if (dev_info.count(key))
+ settings.setValue(QString::fromUtf8(key.c_str()),
+ QString::fromUtf8(dev_info.at(key).c_str()));
+ else
+ settings.remove(QString::fromUtf8(key.c_str()));
+ }
+
+ // TODO Save channel settings and decoders
+
+ settings.endGroup();
+ }
+}
+
+void Session::restore_settings(QSettings &settings)
+{
+ map<string, string> dev_info;
+ list<string> key_list;
+ shared_ptr<devices::HardwareDevice> device;
+
+ // Re-select last used device if possible but only if it's not demo
+ 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));
+ }
+
+ if (dev_info.count("model") > 0)
+ device = device_manager_.find_device_from_info(dev_info);
+
+ if (device) {
+ set_device(device);
+
+ // TODO Restore channel settings and decoders
+ }
+
+ settings.endGroup();
+}
+
void Session::set_device(shared_ptr<devices::Device> device)
{
assert(device);
device_.reset();
+ // Revert name back to default name (e.g. "Untitled-1") as the data is gone
+ name_ = default_name_;
+ name_changed();
+
// Remove all stored data
for (std::shared_ptr<pv::view::View> view : views_) {
view->clear_signals();
for (const shared_ptr<data::SignalData> d : all_signal_data_)
d->clear();
+ // Revert name back to default name (e.g. "Untitled-1") as the data is gone
+ name_ = default_name_;
+ name_changed();
+
// Begin the session
sampling_thread_ = std::thread(
&Session::sample_thread_proc, this, error_handler);
}
}
+bool Session::has_view(std::shared_ptr<pv::view::View> view)
+{
+ return views_.find(view) != views_.end();
+}
+
double Session::get_samplerate() const
{
double samplerate = 0.0;
#include <boost/thread/shared_mutex.hpp>
#include <QObject>
+#include <QSettings>
#include <QString>
#include "util.hpp"
};
public:
- Session(DeviceManager &device_manager);
+ Session(DeviceManager &device_manager, QString name);
~Session();
std::shared_ptr<devices::Device> device() const;
+ QString name() const;
+
+ void set_name(QString name);
+
std::shared_ptr<pv::view::View> main_view() const;
void set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar);
std::shared_ptr<pv::toolbars::MainBar> main_bar() const;
+ void save_settings(QSettings &settings) const;
+
+ void restore_settings(QSettings &settings);
+
/**
* Sets device instance that will be used in the next capture session.
*/
void deregister_view(std::shared_ptr<pv::view::View> view);
+ bool has_view(std::shared_ptr<pv::view::View> view);
+
const std::unordered_set< std::shared_ptr<data::SignalBase> >
signalbases() const;
private:
DeviceManager &device_manager_;
std::shared_ptr<devices::Device> device_;
+ QString default_name_, name_;
std::unordered_set< std::shared_ptr<pv::view::View> > views_;
std::shared_ptr<pv::view::View> main_view_;
mutable std::mutex sampling_mutex_; //!< Protects access to capture_state_.
capture_state capture_state_;
-
std::unordered_set< std::shared_ptr<data::SignalBase> > signalbases_;
std::unordered_set< std::shared_ptr<data::SignalData> > all_signal_data_;
void signals_changed();
+ void name_changed();
+
void trigger_event(util::Timestamp location);
void frame_began();
const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
-MainBar::MainBar(Session &session, MainWindow &main_window,
- string open_file_name, string open_file_format) :
+MainBar::MainBar(Session &session, MainWindow &main_window) :
QToolBar("Sampling Bar", &main_window),
action_open_(new QAction(this)),
action_save_as_(new QAction(this)),
connect(&session_, SIGNAL(device_selected()), this,
SLOT(device_selected()));
- // Figure out which file/device to use
- if (open_file_name.empty())
- select_init_device();
- else
- load_init_file(open_file_name, open_file_format);
+ update_device_list();
}
Session &MainBar::session(void) const
device_selector_.reset();
}
+void MainBar::select_device(shared_ptr<devices::Device> device)
+{
+ try {
+ if (device)
+ session_.set_device(device);
+ else
+ session_.set_default_device();
+ } catch (const QString &e) {
+ QMessageBox msg(this);
+ msg.setText(e);
+ msg.setInformativeText(tr("Failed to Select Device"));
+ msg.setStandardButtons(QMessageBox::Ok);
+ msg.setIcon(QMessageBox::Warning);
+ msg.exec();
+ }
+}
+
+void MainBar::load_init_file(const std::string &file_name,
+ const std::string &format)
+{
+ shared_ptr<InputFormat> input_format;
+
+ DeviceManager& device_manager = session_.device_manager();
+
+ 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);
+}
+
QAction* MainBar::action_open() const
{
return action_open_;
}
}
-void MainBar::select_device(shared_ptr<devices::Device> device)
-{
- try {
- if (device)
- session_.set_device(device);
- else
- session_.set_default_device();
- } catch (const QString &e) {
- QMessageBox msg(this);
- msg.setText(e);
- msg.setInformativeText(tr("Failed to Select Device"));
- msg.setStandardButtons(QMessageBox::Ok);
- msg.setIcon(QMessageBox::Warning);
- msg.exec();
- }
-}
-
-void MainBar::select_init_device()
-{
- QSettings settings;
- map<string, string> dev_info;
- list<string> key_list;
- shared_ptr<devices::HardwareDevice> device;
-
- DeviceManager& device_manager = session_.device_manager();
-
- // Re-select last used device if possible but only if it's not demo
- 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));
- }
-
- if (dev_info.count("model") > 0)
- if (dev_info.at("model").find("Demo device") == std::string::npos)
- device = device_manager.find_device_from_info(dev_info);
-
- // When we can't find a device similar to the one we used last
- // time and there is at least one device aside from demo, use it
- if (!device) {
- for (shared_ptr<devices::HardwareDevice> dev : device_manager.devices()) {
- dev_info = device_manager.get_device_info(dev);
-
- if (dev_info.count("model") > 0)
- if (dev_info.at("model").find("Demo device") == std::string::npos) {
- device = dev;
- break;
- }
- }
- }
-
- select_device(device);
- update_device_list();
-
- settings.endGroup();
-}
-
void MainBar::load_file(QString file_name,
std::shared_ptr<sigrok::InputFormat> format,
const std::map<std::string, Glib::VariantBase> &options)
return;
}
+ session_.set_name(QFileInfo(file_name).fileName());
+
update_device_list();
session_.start_capture([&, errorMessage](QString infoMessage) {
session_error(errorMessage, infoMessage); });
}
-void MainBar::load_init_file(const std::string &file_name,
- const std::string &format)
-{
- shared_ptr<InputFormat> input_format;
-
- DeviceManager& device_manager = session_.device_manager();
-
- 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 MainBar::update_sample_rate_selector()
{
Glib::VariantContainerBase gvar_dict;
options = dlg.options();
}
+ session_.set_name(QFileInfo(file_name).fileName());
+
StoreProgress *dlg = new StoreProgress(file_name, format, options,
sample_range, session_, this);
dlg->run();
static const char *SettingSaveDirectory;
public:
- MainBar(Session &session, pv::MainWindow &main_window,
- std::string open_file_name = std::string(),
- std::string open_file_format = std::string());
+ MainBar(Session &session, pv::MainWindow &main_window);
Session &session(void) const;
void reset_device_selector();
+ void select_device(std::shared_ptr<devices::Device> device);
+
+ void load_init_file(const std::string &file_name,
+ const std::string &format);
+
QAction* action_open() const;
QAction* action_save_as() const;
QAction* action_save_selection_as() const;
private:
void run_stop();
- void select_device(std::shared_ptr<devices::Device> device);
-
void select_init_device();
void load_file(QString file_name,
const std::map<std::string, Glib::VariantBase> &options =
std::map<std::string, Glib::VariantBase>());
- void load_init_file(const std::string &file_name,
- const std::string &format);
-
void save_selection_to_file();
void update_sample_rate_selector();