]> sigrok.org Git - pulseview.git/blobdiff - pv/widgets/devicetoolbutton.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / widgets / devicetoolbutton.cpp
index 7ba8aa28ce6704a0553911890af33fd3ef86ff74..2e76f689a61f448b068e149e2c1886a5f67992b8 100644 (file)
@@ -14,8 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <cassert>
@@ -56,8 +55,13 @@ DeviceToolButton::DeviceToolButton(QWidget *parent,
        setDefaultAction(connect_action_);
        setMinimumWidth(QFontMetrics(font()).averageCharWidth() * 24);
 
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+       connect(&mapper_, SIGNAL(mappedObject(QObject*)),
+               this, SLOT(on_action(QObject*)));
+#else
        connect(&mapper_, SIGNAL(mapped(QObject*)),
                this, SLOT(on_action(QObject*)));
+#endif
 
        connect(&menu_, SIGNAL(hovered(QAction*)),
                this, SLOT(on_menu_hovered(QAction*)));
@@ -72,12 +76,19 @@ void DeviceToolButton::set_device_list(
        const list< shared_ptr<Device> > &devices, shared_ptr<Device> selected)
 {
        selected_device_ = selected;
-       setText(QString::fromStdString(
-               device_manager_.get_display_name(selected)));
+       setText(selected ? QString::fromStdString(
+               selected->display_name(device_manager_)) : tr("<No Device>"));
        devices_ = vector< weak_ptr<Device> >(devices.begin(), devices.end());
        update_device_list();
 }
 
+void DeviceToolButton::reset()
+{
+       setText(tr("<No Device>"));
+       selected_device_.reset();
+       update_device_list();
+}
+
 void DeviceToolButton::update_device_list()
 {
        menu_.clear();
@@ -85,17 +96,17 @@ void DeviceToolButton::update_device_list()
        menu_.setDefaultAction(connect_action_);
        menu_.addSeparator();
 
-       for (weak_ptr<Device> dev_weak_ptr : devices_) {
-               shared_ptr<Device> dev(dev_weak_ptr);
+       for (weak_ptr<Device>& dev_weak_ptr : devices_) {
+               shared_ptr<Device> dev(dev_weak_ptr.lock());
                if (!dev)
                        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->setData(QVariant::fromValue((void*)dev.get()));
+               a->setToolTip(QString::fromStdString(dev->full_name()));
                mapper_.setMapping(a, a);
 
                connect(a, SIGNAL(triggered()), &mapper_, SLOT(map()));
@@ -108,8 +119,10 @@ void DeviceToolButton::on_action(QObject *action)
 {
        assert(action);
 
+       selected_device_.reset();
+
        Device *const dev = (Device*)((QAction*)action)->data().value<void*>();
-       for (weak_ptr<Device> dev_weak_ptr : devices_) {
+       for (weak_ptr<Device>& dev_weak_ptr : devices_) {
                shared_ptr<Device> dev_ptr(dev_weak_ptr);
                if (dev_ptr.get() == dev) {
                        selected_device_ = shared_ptr<Device>(dev_ptr);
@@ -119,7 +132,7 @@ void DeviceToolButton::on_action(QObject *action)
 
        update_device_list();
        setText(QString::fromStdString(
-               device_manager_.get_display_name(selected_device_)));
+               selected_device_->display_name(device_manager_)));
 
        device_selected();
 }
@@ -149,5 +162,5 @@ void DeviceToolButton::on_menu_hover_timeout()
        QToolTip::showText(QCursor::pos(), device_tooltip_);
 }
 
-} // widgets
-} // pv
+}  // namespace widgets
+}  // namespace pv