all repos — mgba @ 9877c1bf1e969a7d7441d843b57205acc71cbed2

mGBA Game Boy Advance Emulator

Qt: Refactor out duplicated named view setup
Vicki Pfau vi@endrift.com
Mon, 22 Feb 2021 18:15:30 -0800
commit

9877c1bf1e969a7d7441d843b57205acc71cbed2

parent

06e6e1a27d75c5111ff81a2aeae9b3c204ca6ec1

2 files changed, 18 insertions(+), 46 deletions(-)

jump to
M src/platform/qt/Window.cppsrc/platform/qt/Window.cpp

@@ -162,11 +162,6 @@

Window::~Window() { delete m_logView; -#ifdef USE_FFMPEG - delete m_videoView; - delete m_gifView; -#endif - #ifdef USE_SQLITE3 delete m_libraryView; #endif

@@ -492,7 +487,6 @@ openView(view);

}; } - template <typename T, typename... A> std::function<void()> Window::openControllerTView(A... arg) { return [=]() {

@@ -501,29 +495,20 @@ openView(view);

}; } -#ifdef USE_FFMPEG -void Window::openVideoWindow() { - if (!m_videoView) { - m_videoView = new VideoView(); - if (m_controller) { - m_videoView->setController(m_controller); - } - connect(this, &Window::shutdown, m_videoView, &QWidget::close); - } - m_videoView->show(); -} - -void Window::openGIFWindow() { - if (!m_gifView) { - m_gifView = new GIFView(); - if (m_controller) { - m_gifView->setController(m_controller); +template <typename T, typename... A> +std::function<void()> Window::openNamedControllerTView(std::unique_ptr<T>* name, A... arg) { + return [=]() { + if (!*name) { + *name = std::make_unique<T>(arg...); + if (m_controller) { + (*name)->setController(m_controller); + } + connect(this, &Window::shutdown, name->get(), &QWidget::close); } - connect(this, &Window::shutdown, m_gifView, &QWidget::close); - } - m_gifView->show(); + (*name)->show(); + (*name)->setFocus(Qt::PopupFocusReason); + }; } -#endif #ifdef USE_GDB_STUB void Window::gdbOpen() {

@@ -1465,8 +1450,8 @@ }, "av", tr("F12"));

#endif #ifdef USE_FFMPEG - addGameAction(tr("Record A/V..."), "recordOutput", this, &Window::openVideoWindow, "av"); - addGameAction(tr("Record GIF/WebP/APNG..."), "recordGIF", this, &Window::openGIFWindow, "av"); + addGameAction(tr("Record A/V..."), "recordOutput", openNamedControllerTView<VideoView>(&m_videoView), "av"); + addGameAction(tr("Record GIF/WebP/APNG..."), "recordGIF", openNamedControllerTView<GIFView>(&m_gifView), "av"); #endif m_actions.addSeparator("av");

@@ -1490,16 +1475,7 @@ m_overrideView->show();

m_overrideView->recheck(); }, "tools"); - m_actions.addAction(tr("Game Pak sensors..."), "sensorWindow", [this]() { - if (!m_sensorView) { - m_sensorView = std::make_unique<SensorView>(&m_inputController); - if (m_controller) { - m_sensorView->setController(m_controller); - } - connect(this, &Window::shutdown, m_sensorView.get(), &QWidget::close); - } - m_sensorView->show(); - }, "tools"); + m_actions.addAction(tr("Game Pak sensors..."), "sensorWindow", openNamedControllerTView<SensorView>(&m_sensorView, &m_inputController), "tools"); addGameAction(tr("&Cheats..."), "cheatsWindow", openControllerTView<CheatsView>(), "tools");
M src/platform/qt/Window.hsrc/platform/qt/Window.h

@@ -105,11 +105,6 @@ #ifdef USE_DEBUGGERS

void consoleOpen(); #endif -#ifdef USE_FFMPEG - void openVideoWindow(); - void openGIFWindow(); -#endif - #ifdef USE_GDB_STUB void gdbOpen(); #endif

@@ -168,6 +163,7 @@ void openView(QWidget* widget);

template <typename T, typename... A> std::function<void()> openTView(A... arg); template <typename T, typename... A> std::function<void()> openControllerTView(A... arg); + template <typename T, typename... A> std::function<void()> openNamedControllerTView(std::unique_ptr<T>*, A... arg); Action* addGameAction(const QString& visibleName, const QString& name, Action::Function action, const QString& menu = {}, const QKeySequence& = {}); template<typename T, typename V> Action* addGameAction(const QString& visibleName, const QString& name, T* obj, V (T::*action)(), const QString& menu = {}, const QKeySequence& = {});

@@ -231,8 +227,8 @@ std::unique_ptr<SensorView> m_sensorView;

FrameView* m_frameView = nullptr; #ifdef USE_FFMPEG - VideoView* m_videoView = nullptr; - GIFView* m_gifView = nullptr; + std::unique_ptr<VideoView> m_videoView; + std::unique_ptr<GIFView> m_gifView; #endif #ifdef USE_GDB_STUB