QCodeEditor: Update to current cpeditor/QCodeEditor fork, commit ed1196a
[smuview.git] / src / session.cpp
blobc74ed2bf9e05381691f29bc6f6eeabda2bb8e123
1 /*
2 * This file is part of the SmuView project.
4 * Copyright (C) 2017-2021 Frank Stettner <frank-stettner@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <cassert>
21 #include <list>
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
28 #include <QDebug>
30 #include "session.hpp"
31 #include "config.h"
32 #include "src/devicemanager.hpp"
33 #include "src/util.hpp"
34 #include "src/devices/basedevice.hpp"
35 #include "src/devices/hardwaredevice.hpp"
36 #include "src/devices/userdevice.hpp"
37 #include "src/python/smuscriptrunner.hpp"
39 using std::list;
40 using std::make_pair;
41 using std::make_shared;
42 using std::map;
43 using std::shared_ptr;
44 using std::string;
45 using std::vector;
47 namespace sigrok {
48 class Context;
51 namespace sv {
53 shared_ptr<sigrok::Context> Session::sr_context;
54 double Session::session_start_timestamp = .0;
56 Session::Session(DeviceManager &device_manager) :
57 device_manager_(device_manager)
59 smu_script_runner_ = make_shared<python::SmuScriptRunner>(*this);
60 connect(smu_script_runner_.get(), &python::SmuScriptRunner::script_error,
61 this, &Session::error_handler);
63 // Connect devices
64 for (const auto &device : device_manager.user_spec_devices()) {
65 this->add_device(device);
69 Session::~Session()
71 for (auto &device_pair_ : device_map_)
72 device_pair_.second->close();
75 DeviceManager &Session::device_manager()
77 return device_manager_;
80 const DeviceManager &Session::device_manager() const
82 return device_manager_;
85 map<string, shared_ptr<devices::BaseDevice>> Session::device_map() const
87 return device_map_;
90 list<shared_ptr<devices::HardwareDevice>>
91 Session::connect_device(const string &conn_string)
93 // Determine the driver name and options (in generic format).
94 vector<string> driver_opts = sv::util::split_string(conn_string, ":");
95 string driver_name = driver_opts.front();
96 driver_opts.erase(driver_opts.begin());
98 // Scan for the specified driver, passing scan options.
99 list<shared_ptr<devices::HardwareDevice>> devices =
100 device_manager_.driver_scan(driver_name, driver_opts);
102 for (const auto &device : devices)
103 add_device(device);
105 return devices;
108 void Session::add_device(shared_ptr<devices::BaseDevice> device)
110 assert(device);
112 try {
113 device->open();
115 catch (const QString &e) {
116 qCritical() << e;
117 device.reset();
120 connect(device.get(), &devices::BaseDevice::device_error,
121 this, &Session::error_handler);
123 device_map_.insert(make_pair(device->id(), device));
125 Q_EMIT device_added(device);
128 shared_ptr<devices::UserDevice> Session::add_user_device()
130 string vendor = "SmuView";
131 string model = "User Device";
132 string version = SV_VERSION_STRING;
134 auto device = make_shared<devices::UserDevice>(
135 sr_context, vendor, model, version);
136 this->add_device(device);
138 return device;
141 void Session::remove_device(shared_ptr<devices::BaseDevice> device)
143 if (device) {
144 device->close();
146 disconnect(device.get(), &devices::BaseDevice::device_error,
147 this, &Session::error_handler);
149 device_map_.erase(device->id());
151 Q_EMIT device_removed(device);
156 shared_ptr<python::SmuScriptRunner> Session::smu_script_runner()
158 return smu_script_runner_;
161 void Session::run_smu_script(const string &script_file)
163 smu_script_runner_->run(script_file);
166 void Session::set_main_window(MainWindow *main_window)
168 main_window_ = main_window;
171 MainWindow *Session::main_window() const
173 return main_window_;
176 void Session::error_handler(const std::string &sender, const std::string &msg)
178 qCritical() << QString::fromStdString(sender) <<
179 " error: " << QString::fromStdString(msg);
182 } // namespace sv