]> sigrok.org Git - pulseview.git/commitdiff
devices::Device: Moved in full_name and display_name
authorJoel Holdsworth <redacted>
Fri, 3 Apr 2015 10:35:49 +0000 (11:35 +0100)
committerJoel Holdsworth <redacted>
Thu, 28 May 2015 14:59:05 +0000 (15:59 +0100)
pv/devices/device.hpp
pv/devices/hardwaredevice.cpp
pv/devices/hardwaredevice.hpp
pv/devices/sessionfile.cpp
pv/devices/sessionfile.hpp

index 529fdd5b33c42e0bc52fc42bb1e49a5f72559c38..7e0b58ad065600bdc993751f343ff8eb348d4de1 100644 (file)
@@ -22,7 +22,7 @@
 #define PULSEVIEW_PV_DEVICES_DEVICE_HPP
 
 #include <memory>
-
+#include <string>
 
 namespace sigrok {
 class Device;
@@ -30,6 +30,9 @@ class Session;
 } // namespace sigrok
 
 namespace pv {
+
+class DeviceManager;
+
 namespace devices {
 
 class Device
@@ -44,6 +47,19 @@ public:
 
        std::shared_ptr<sigrok::Device> device() const;
 
+       /**
+        * Builds the full name. It only contains all the fields.
+        */
+       virtual std::string full_name() const = 0;
+
+       /**
+        * Builds the display name. It only contains fields as required.
+        * @param device_manager a reference to the device manager is needed
+        * so that other similarly titled devices can be detected.
+        */
+       virtual std::string display_name(
+               const DeviceManager &device_manager) const = 0;
+
        virtual void create() = 0;
 
        virtual void run();
index 4fa27f6dd7521e8b8a82433c9b5c940bc4bea435..6374dafe6e03aaddb219a05c1fa4c3a21ab71dd5 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <boost/algorithm/string/join.hpp>
+
 #include <QString>
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
+#include <pv/devicemanager.hpp>
+
 #include "hardwaredevice.hpp"
 
+using std::dynamic_pointer_cast;
 using std::shared_ptr;
 using std::static_pointer_cast;
+using std::string;
+using std::vector;
+
+using boost::algorithm::join;
+
+using sigrok::HardwareDevice;
 
 namespace pv {
 namespace devices {
@@ -42,10 +53,49 @@ HardwareDevice::~HardwareDevice() {
                session_->remove_devices();
 }
 
+string HardwareDevice::full_name() const {
+       vector<string> parts = {device_->vendor(), device_->model(),
+               device_->version(), device_->serial_number()};
+       if (device_->connection_id().length() > 0)
+               parts.push_back("(" + device_->connection_id() + ")");
+       return join(parts, " ");
+}
+
 shared_ptr<sigrok::HardwareDevice> HardwareDevice::hardware_device() const {
        return static_pointer_cast<sigrok::HardwareDevice>(device_);
 }
 
+string HardwareDevice::display_name(
+       const DeviceManager &device_manager) const {
+       const auto hw_dev = hardware_device();
+
+       // If we can find another device with the same model/vendor then
+       // we have at least two such devices and need to distinguish them.
+       const auto &devices = device_manager.devices();
+       const bool multiple_dev = hw_dev && any_of(
+               devices.begin(), devices.end(),
+               [&](shared_ptr<devices::HardwareDevice> dev) {
+                       return dev->hardware_device()->vendor() ==
+                                       hw_dev->vendor() &&
+                               dev->hardware_device()->model() ==
+                                       hw_dev->model() &&
+                               dev->device_ != device_;
+               });
+
+       vector<string> parts = {device_->vendor(), device_->model()};
+
+       if (multiple_dev) {
+               parts.push_back(device_->version());
+               parts.push_back(device_->serial_number());
+
+               if ((device_->serial_number().length() == 0) &&
+                       (device_->connection_id().length() > 0))
+                       parts.push_back("(" + device_->connection_id() + ")");
+       }
+
+       return join(parts, " ");
+}
+
 void HardwareDevice::create() {
        // Open the device
         try {
index 41d79068a2b3765c68586bd807f5312f22c38496..9f052d7e85efa5a5a35b494b201dd310354631af 100644 (file)
@@ -41,6 +41,18 @@ public:
 
        std::shared_ptr<sigrok::HardwareDevice> hardware_device() const;
 
+       /**
+        * Builds the full name. It only contains all the fields.
+        */
+       std::string full_name() const;
+
+       /**
+        * Builds the display name. It only contains fields as required.
+        * @param device_manager a reference to the device manager is needed
+        * so that other similarly titled devices can be detected.
+        */
+       std::string display_name(const DeviceManager &device_manager) const;
+
        void create();
 
 private:
index 4ca982ae7cf4f3897dbf83778faecf3ff0a08dfd..687c52bdcd5a225a29d1cdfaeffac104b7c4c9a3 100644 (file)
@@ -20,6 +20,8 @@
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
+#include <boost/filesystem.hpp>
+
 #include "sessionfile.hpp"
 
 namespace pv {
@@ -31,6 +33,14 @@ SessionFile::SessionFile(const std::shared_ptr<sigrok::Context> &context,
        file_name_(file_name) {
 }
 
+std::string SessionFile::full_name() const {
+       return boost::filesystem::path(file_name_).filename().string();
+}
+
+std::string SessionFile::display_name(const DeviceManager&) const {
+       return SessionFile::full_name();
+}
+
 void SessionFile::create() {
        session_ = context_->load_session(file_name_);
        device_ = session_->devices()[0];
index 0fcaa34cb767b3c3066e28f34d577a33aad42106..a4b69d3e1d1cb8f83402416c858739e5d191c8fb 100644 (file)
@@ -39,6 +39,16 @@ public:
        SessionFile(const std::shared_ptr<sigrok::Context> &context,
                const std::string &file_name);
 
+       /**
+        * Builds the full name. It only contains all the fields.
+        */
+       std::string full_name() const;
+
+       /**
+        * Builds the display name. It only contains fields as required.
+        */
+       std::string display_name(const DeviceManager&) const;
+
        void create();
 
 private: