]> sigrok.org Git - pulseview.git/commitdiff
Factored out DecoderMenu
authorJoel Holdsworth <redacted>
Sat, 26 Oct 2013 14:28:59 +0000 (15:28 +0100)
committerJoel Holdsworth <redacted>
Tue, 19 Nov 2013 19:32:10 +0000 (19:32 +0000)
CMakeLists.txt
pv/mainwindow.cpp
pv/mainwindow.h
pv/widgets/decodermenu.cpp [new file with mode: 0644]
pv/widgets/decodermenu.h [new file with mode: 0644]
test/CMakeLists.txt

index ca89c107374f0aac953bc342d7c82bc942e5e085..51ba86ef31583d28470e4296cbf3b0b45f4e9649 100644 (file)
@@ -144,6 +144,7 @@ set(pulseview_SOURCES
        pv/view/decode/annotation.cpp
        pv/widgets/colourbutton.cpp
        pv/widgets/colourpopup.cpp
+       pv/widgets/decodermenu.cpp
        pv/widgets/popup.cpp
        pv/widgets/popuptoolbutton.cpp
        pv/widgets/wellarray.cpp
@@ -179,6 +180,7 @@ set(pulseview_HEADERS
        pv/view/viewport.h
        pv/widgets/colourbutton.h
        pv/widgets/colourpopup.h
+       pv/widgets/decodermenu.h
        pv/widgets/popuptoolbutton.h
        pv/widgets/wellarray.h
 )
index e1f831408a54385d7b12da34999e92a6f6956e2d..37b7bfcd3854792a40e639e8b5c8ab9a05baa05e 100644 (file)
@@ -42,6 +42,7 @@
 #include "toolbars/samplingbar.h"
 #include "view/logicsignal.h"
 #include "view/view.h"
+#include "widgets/decodermenu.h"
 
 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
 #define __STDC_FORMAT_MACROS
@@ -65,8 +66,7 @@ MainWindow::MainWindow(DeviceManager &device_manager,
        QWidget *parent) :
        QMainWindow(parent),
        _device_manager(device_manager),
-       _session(device_manager),
-       _decoders_add_mapper(this)
+       _session(device_manager)
 {
        setup_ui();
        if (open_file_name) {
@@ -177,14 +177,13 @@ void MainWindow::setup_ui()
        _menu_decoders->setTitle(QApplication::translate(
                "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
 
-       _menu_decoders_add = new QMenu(_menu_decoders);
+       _menu_decoders_add = new pv::widgets::DecoderMenu(_menu_decoders);
        _menu_decoders_add->setTitle(QApplication::translate(
                "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
-       setup_add_decoders(_menu_decoders_add);
+       connect(_menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
+               this, SLOT(add_decoder(srd_decoder*)));
 
        _menu_decoders->addMenu(_menu_decoders_add);
-       connect(&_decoders_add_mapper, SIGNAL(mapped(QObject*)),
-               this, SLOT(add_decoder(QObject*)));
 
        // Help Menu
        _menu_help = new QMenu(_menu_bar);
@@ -286,28 +285,6 @@ void MainWindow::show_session_error(
        msg.exec();
 }
 
-gint MainWindow::decoder_name_cmp(gconstpointer a, gconstpointer b)
-{
-       return strcmp(((const srd_decoder*)a)->name,
-               ((const srd_decoder*)b)->name);
-}
-
-void MainWindow::setup_add_decoders(QMenu *parent)
-{
-       GSList *l = g_slist_sort(g_slist_copy(
-               (GSList*)srd_decoder_list()), decoder_name_cmp);
-       for(; l; l = l->next)
-       {
-               QAction *const action = parent->addAction(QString(
-                       ((srd_decoder*)l->data)->name));
-               action->setData(qVariantFromValue(l->data));
-               _decoders_add_mapper.setMapping(action, action);
-               connect(action, SIGNAL(triggered()),
-                       &_decoders_add_mapper, SLOT(map()));
-       }
-       g_slist_free(l);
-}
-
 void MainWindow::on_actionOpen_triggered()
 {
        // Enumerate the file formats
@@ -368,14 +345,10 @@ void MainWindow::on_actionAbout_triggered()
        dlg.exec();
 }
 
-void MainWindow::add_decoder(QObject *action)
+void MainWindow::add_decoder(srd_decoder *decoder)
 {
-       assert(action);
-       srd_decoder *const dec =
-               (srd_decoder*)((QAction*)action)->data().value<void*>();
-       assert(dec);
-
-       _session.add_decoder(dec);
+       assert(decoder);
+       _session.add_decoder(decoder);
 }
 
 void MainWindow::run_stop()
index 47704c2fc8f7abe1ab614c33f684ba4a87f1d513..ce09284d7af565879083e560ddca5506f1799377 100644 (file)
@@ -26,7 +26,6 @@
 #include <boost/weak_ptr.hpp>
 
 #include <QMainWindow>
-#include <QSignalMapper>
 
 #include "sigsession.h"
 
@@ -51,6 +50,10 @@ namespace view {
 class View;
 }
 
+namespace widgets {
+class DecoderMenu;
+}
+
 class MainWindow : public QMainWindow
 {
        Q_OBJECT
@@ -75,7 +78,6 @@ private:
                struct sr_dev_inst *selected_device = NULL);
 
        static gint decoder_name_cmp(gconstpointer a, gconstpointer b);
-       void setup_add_decoders(QMenu *parent);
 
 private slots:
        void load_file(QString file_name);
@@ -97,7 +99,7 @@ private slots:
 
        void on_actionAbout_triggered();
 
-       void add_decoder(QObject *action);
+       void add_decoder(srd_decoder *decoder);
 
        void run_stop();
 
@@ -122,8 +124,7 @@ private:
        QAction *_action_view_show_cursors;
 
        QMenu *_menu_decoders;
-       QMenu *_menu_decoders_add;
-       QSignalMapper _decoders_add_mapper;
+       pv::widgets::DecoderMenu *_menu_decoders_add;
 
        QMenu *_menu_help;
        QAction *_action_about;
diff --git a/pv/widgets/decodermenu.cpp b/pv/widgets/decodermenu.cpp
new file mode 100644 (file)
index 0000000..68fc478
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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
+ */
+
+#include <libsigrokdecode/libsigrokdecode.h>
+
+#include "decodermenu.h"
+
+namespace pv {
+namespace widgets {
+
+DecoderMenu::DecoderMenu(QWidget *parent) :
+       QMenu(parent),
+       _mapper(this)
+{
+       GSList *l = g_slist_sort(g_slist_copy(
+               (GSList*)srd_decoder_list()), decoder_name_cmp);
+       for(; l; l = l->next)
+       {
+               QAction *const action = addAction(QString(
+                       ((srd_decoder*)l->data)->name));
+               action->setData(qVariantFromValue(l->data));
+               _mapper.setMapping(action, action);
+               connect(action, SIGNAL(triggered()),
+                       &_mapper, SLOT(map()));
+       }
+       g_slist_free(l);
+
+       connect(&_mapper, SIGNAL(mapped(QObject*)),
+               this, SLOT(on_action(QObject*)));
+}
+
+int DecoderMenu::decoder_name_cmp(const void *a, const void *b)
+{
+       return strcmp(((const srd_decoder*)a)->name,
+               ((const srd_decoder*)b)->name);
+}
+
+void DecoderMenu::on_action(QObject *action)
+{
+       assert(action);
+       srd_decoder *const dec =
+               (srd_decoder*)((QAction*)action)->data().value<void*>();
+       assert(dec);
+
+       decoder_selected(dec);  
+}
+
+} // widgets
+} // pv
diff --git a/pv/widgets/decodermenu.h b/pv/widgets/decodermenu.h
new file mode 100644 (file)
index 0000000..88dcfaa
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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
+ */
+
+#ifndef PULSEVIEW_PV_WIDGETS_DECODERMENU_H
+#define PULSEVIEW_PV_WIDGETS_DECODERMENU_H
+
+#include <QMenu>
+#include <QSignalMapper>
+
+struct srd_decoder;
+
+namespace pv {
+namespace widgets {
+
+class DecoderMenu : public QMenu
+{
+       Q_OBJECT;
+
+public:
+       DecoderMenu(QWidget *parent);
+
+private:
+       static int decoder_name_cmp(const void *a, const void *b);
+
+
+private slots:
+       void on_action(QObject *action);
+
+signals:
+       void decoder_selected(srd_decoder *decoder);
+
+private:
+       QSignalMapper _mapper;
+};
+
+} // widgets
+} // pv
+
+#endif // PULSEVIEW_PV_WIDGETS_DECODERMENU_H
index 6e578b704dfe4fa595f7b1ce95117b79064818a2..afe26a16fd8a24f772663480302258f7d0cc196b 100644 (file)
@@ -69,6 +69,7 @@ set(pulseview_TEST_SOURCES
        ${PROJECT_SOURCE_DIR}/pv/view/decode/annotation.cpp
        ${PROJECT_SOURCE_DIR}/pv/widgets/colourbutton.cpp
        ${PROJECT_SOURCE_DIR}/pv/widgets/colourpopup.cpp
+       ${PROJECT_SOURCE_DIR}/pv/widgets/decodermenu.cpp
        ${PROJECT_SOURCE_DIR}/pv/widgets/popup.cpp
        ${PROJECT_SOURCE_DIR}/pv/widgets/wellarray.cpp
        data/analogsnapshot.cpp
@@ -98,6 +99,7 @@ set(pulseview_TEST_HEADERS
        ${PROJECT_SOURCE_DIR}/pv/view/viewport.h
        ${PROJECT_SOURCE_DIR}/pv/widgets/colourbutton.h
        ${PROJECT_SOURCE_DIR}/pv/widgets/colourpopup.h
+       ${PROJECT_SOURCE_DIR}/pv/widgets/decodermenu.h
        ${PROJECT_SOURCE_DIR}/pv/widgets/wellarray.h
 )