From: Joel Holdsworth Date: Sat, 26 Oct 2013 14:28:59 +0000 (+0100) Subject: Factored out DecoderMenu X-Git-Tag: pulseview-0.2.0~223 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=f0d37dab05f9fb055a9af8a05d776d1a5b4b0909;p=pulseview.git Factored out DecoderMenu --- diff --git a/CMakeLists.txt b/CMakeLists.txt index ca89c107..51ba86ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index e1f83140..37b7bfcd 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -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(); - assert(dec); - - _session.add_decoder(dec); + assert(decoder); + _session.add_decoder(decoder); } void MainWindow::run_stop() diff --git a/pv/mainwindow.h b/pv/mainwindow.h index 47704c2f..ce09284d 100644 --- a/pv/mainwindow.h +++ b/pv/mainwindow.h @@ -26,7 +26,6 @@ #include #include -#include #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 index 00000000..68fc4781 --- /dev/null +++ b/pv/widgets/decodermenu.cpp @@ -0,0 +1,66 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2013 Joel Holdsworth + * + * 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 + +#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(); + assert(dec); + + decoder_selected(dec); +} + +} // widgets +} // pv diff --git a/pv/widgets/decodermenu.h b/pv/widgets/decodermenu.h new file mode 100644 index 00000000..88dcfaa4 --- /dev/null +++ b/pv/widgets/decodermenu.h @@ -0,0 +1,56 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2013 Joel Holdsworth + * + * 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 +#include + +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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6e578b70..afe26a16 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 )