#include "session.hpp"
#include <cassert>
+#include <functional>
#include <stdexcept>
#include <sstream>
#include <string>
using boost::algorithm::join;
+using std::bind;
using std::dynamic_pointer_cast;
using std::list;
using std::map;
driver_devices.push_back(d);
}
- for (shared_ptr<devices::HardwareDevice> device : driver_devices)
- build_display_name(device);
-
devices_.insert(devices_.end(), driver_devices.begin(),
driver_devices.end());
- devices_.sort([&](shared_ptr<devices::Device> a,
- shared_ptr<devices::Device> b)
- { return compare_devices(a, b); });
+ devices_.sort(bind(&DeviceManager::compare_devices, this, _1, _2));
+ driver_devices.sort(bind(
+ &DeviceManager::compare_devices, this, _1, _2));
return driver_devices;
}
return last_resort_dev;
}
-void DeviceManager::build_display_name(shared_ptr<devices::Device> device)
-{
- const shared_ptr<sigrok::Device> sr_dev = device->device();
- auto session_device = dynamic_pointer_cast<sigrok::SessionDevice>(sr_dev);
- auto hardware_device = dynamic_pointer_cast<sigrok::HardwareDevice>(sr_dev);
-
- if (session_device) {
- full_names_[device] = display_names_[device] =
- boost::filesystem::path(
- session_device->parent()->filename()).filename().string();
- return;
- }
-
- // First, build the device's full name. It always contains all
- // possible information.
- vector<string> parts = {sr_dev->vendor(), sr_dev->model(),
- sr_dev->version(), sr_dev->serial_number()};
-
- if (sr_dev->connection_id().length() > 0)
- parts.push_back("("+sr_dev->connection_id()+")");
-
- full_names_[device] = join(parts, " ");
-
- // Next, build the display name. It only contains fields as required.
-
- // 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 bool multiple_dev = hardware_device && any_of(
- devices_.begin(), devices_.end(),
- [&](shared_ptr<devices::HardwareDevice> dev) {
- return (dev->device()->vendor() == hardware_device->vendor() &&
- dev->device()->model() == hardware_device->model()) &&
- dev != device;
- } );
-
- parts = {sr_dev->vendor(), sr_dev->model()};
-
- if (multiple_dev) {
- parts.push_back(sr_dev->version());
- parts.push_back(sr_dev->serial_number());
-
- if ((sr_dev->serial_number().length() == 0) &&
- (sr_dev->connection_id().length() > 0))
- parts.push_back("("+sr_dev->connection_id()+")");
- }
-
- display_names_[device] = join(parts, " ");
-}
-
-const std::string DeviceManager::get_display_name(
- std::shared_ptr<devices::Device> dev)
-{
- return display_names_[dev];
-}
-
-const std::string DeviceManager::get_full_name(
- std::shared_ptr<devices::Device> dev)
-{
- return full_names_[dev];
-}
-
-void DeviceManager::update_display_name(
- std::shared_ptr<devices::Device> dev)
-{
- build_display_name(dev);
-}
-
-bool DeviceManager::compare_devices(
- shared_ptr<devices::Device> a, shared_ptr<devices::Device> b)
-{
+bool DeviceManager::compare_devices(shared_ptr<devices::Device> a,
+ shared_ptr<devices::Device> b) {
assert(a);
assert(b);
-
- return display_names_[a].compare(display_names_[b]) < 0;
+ return a->display_name(*this).compare(b->display_name(*this)) < 0;
}
} // namespace pv
const std::shared_ptr<devices::HardwareDevice> find_device_from_info(
const std::map<std::string, std::string> search_info);
- void build_display_name(std::shared_ptr<devices::Device> device);
-
- const std::string get_display_name(
- std::shared_ptr<devices::Device> dev);
-
- const std::string get_full_name(std::shared_ptr<devices::Device> dev);
-
- void update_display_name(std::shared_ptr<devices::Device> dev);
-
private:
bool compare_devices(std::shared_ptr<devices::Device> a,
std::shared_ptr<devices::Device> b);
protected:
std::shared_ptr<sigrok::Context> context_;
std::list< std::shared_ptr<devices::HardwareDevice> > devices_;
-
- std::map< std::shared_ptr<devices::Device>, std::string > display_names_;
- std::map< std::shared_ptr<devices::Device>, std::string > full_names_;
};
} // namespace pv
{
selected_device_ = selected;
setText(QString::fromStdString(
- device_manager_.get_display_name(selected)));
+ selected->display_name(device_manager_)));
devices_ = vector< weak_ptr<Device> >(devices.begin(), devices.end());
update_device_list();
}
continue;
QAction *const a = new QAction(QString::fromStdString(
- device_manager_.get_display_name(dev)), this);
+ dev->display_name(device_manager_)), this);
a->setCheckable(true);
a->setChecked(selected_device_ == dev);
a->setData(qVariantFromValue((void*)dev.get()));
- a->setToolTip(QString::fromStdString(device_manager_.get_full_name(dev)));
+ a->setToolTip(QString::fromStdString(dev->full_name()));
mapper_.setMapping(a, a);
connect(a, SIGNAL(triggered()), &mapper_, SLOT(map()));
update_device_list();
setText(QString::fromStdString(
- device_manager_.get_display_name(selected_device_)));
+ selected_device_->display_name(device_manager_)));
device_selected();
}