From 0683afa2b03704d49f501cbc6dcee5cbde2ffa9e Mon Sep 17 00:00:00 2001 From: YiFang Xiao Date: Thu, 24 Aug 2023 22:10:38 +0800 Subject: [PATCH] opt: add Ctrl+Shift+S to stop the current playing sound [autofix.ci] apply automated fixes --- src/ffmpegaudio.cc | 4 +++- src/ui/articleview.cc | 5 +++++ src/ui/articleview.hh | 2 ++ src/ui/mainwindow.cc | 16 ++++++++++++++-- src/ui/mainwindow.hh | 3 +++ src/ui/scanpopup.cc | 34 +++++++++++++++++++++++----------- src/ui/scanpopup.hh | 24 +++++++++++++----------- 7 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/ffmpegaudio.cc b/src/ffmpegaudio.cc index 76dc82a1..0a7e6d71 100644 --- a/src/ffmpegaudio.cc +++ b/src/ffmpegaudio.cc @@ -54,7 +54,9 @@ void AudioService::playMemory( const char * ptr, int size ) emit cancelPlaying( false ); QByteArray audioData( ptr, size ); thread = std::make_shared< DecoderThread >( audioData, this ); - + connect( this, &AudioService::cancelPlaying, thread.get(), [ this ]( bool waitFinished ) { + thread->cancel( waitFinished ); + } ); thread->start(); } diff --git a/src/ui/articleview.cc b/src/ui/articleview.cc index 133b1c2b..b291d48d 100644 --- a/src/ui/articleview.cc +++ b/src/ui/articleview.cc @@ -1433,6 +1433,11 @@ void ArticleView::playSound() } ); } +void ArticleView::stopSound() +{ + audioPlayer->stop(); +} + void ArticleView::toHtml( const std::function< void( QString & ) > & callback ) { webview->page()->toHtml( [ = ]( const QString & content ) { diff --git a/src/ui/articleview.hh b/src/ui/articleview.hh index abe07c60..12058b94 100644 --- a/src/ui/articleview.hh +++ b/src/ui/articleview.hh @@ -200,6 +200,8 @@ public: /// Reloads the view void reload(); + void stopSound(); + /// Returns true if there's an audio reference on the page, false otherwise. void hasSound( const std::function< void( bool has ) > & callback ); diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index 9bc2a6f8..1911de5b 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -149,6 +149,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ): focusHeadwordsDlgAction( this ), focusArticleViewAction( this ), addAllTabToFavoritesAction( this ), + stopAudioAction( this ), trayIconMenu( this ), addTab( this ), cfg( cfg_ ), @@ -428,6 +429,11 @@ MainWindow::MainWindow( Config::Class & cfg_ ): showFullTextSearchDialog(); } ); + addGlobalAction( &stopAudioAction, [ this ]() { + stopAudio(); + } ); + stopAudioAction.setShortcut( QKeySequence( "Ctrl+Shift+S" ) ); + addTabAction.setShortcutContext( Qt::WidgetWithChildrenShortcut ); addTabAction.setShortcut( QKeySequence( "Ctrl+T" ) ); @@ -4023,14 +4029,20 @@ void MainWindow::focusHeadwordsDialog() void MainWindow::focusArticleView() { - ArticleView * view = getCurrentArticleView(); - if ( view ) { + if ( ArticleView * view = getCurrentArticleView() ) { if ( !isActiveWindow() ) activateWindow(); view->focus(); } } +void MainWindow::stopAudio() +{ + if ( ArticleView * view = getCurrentArticleView() ) { + view->stopSound(); + } +} + void MainWindow::editDictionary( Dictionary::Class * dict ) { QString dictFilename = dict->getMainFilename(); diff --git a/src/ui/mainwindow.hh b/src/ui/mainwindow.hh index 145ccd21..c66a02c9 100644 --- a/src/ui/mainwindow.hh +++ b/src/ui/mainwindow.hh @@ -109,6 +109,8 @@ private: closeRestTabAction, switchToNextTabAction, switchToPrevTabAction, showDictBarNamesAction, useSmallIconsInToolbarsAction, toggleMenuBarAction, focusHeadwordsDlgAction, focusArticleViewAction, addAllTabToFavoritesAction; + + QAction stopAudioAction; QToolBar * navToolbar; MainStatusBar * mainStatusBar; QAction *navBack, *navForward, *navPronounce, *enableScanningAction; @@ -454,6 +456,7 @@ private slots: void focusHeadwordsDialog(); void focusArticleView(); + void stopAudio(); void proxyAuthentication( const QNetworkProxy & proxy, QAuthenticator * authenticator ); diff --git a/src/ui/scanpopup.cc b/src/ui/scanpopup.cc index a473fc60..33640719 100644 --- a/src/ui/scanpopup.cc +++ b/src/ui/scanpopup.cc @@ -77,6 +77,7 @@ ScanPopup::ScanPopup( QWidget * parent, escapeAction( this ), switchExpandModeAction( this ), focusTranslateLineAction( this ), + stopAudioAction( this ), openSearchAction( this ), wordFinder( this ), dictionaryBar( this, configEvents, cfg.editDictionaryCommandLine, cfg.preferences.maxDictionaryRefsInContextMenu ), @@ -212,6 +213,12 @@ ScanPopup::ScanPopup( QWidget * parent, connect( &focusTranslateLineAction, &QAction::triggered, this, &ScanPopup::focusTranslateLine ); + stopAudioAction.setShortcutContext( Qt::WidgetWithChildrenShortcut ); + addAction( &stopAudioAction ); + stopAudioAction.setShortcut( QKeySequence( "Ctrl+Shift+S" ) ); + + connect( &stopAudioAction, &QAction::triggered, this, &ScanPopup::stopAudio ); + QAction * const focusArticleViewAction = new QAction( this ); focusArticleViewAction->setShortcutContext( Qt::WidgetWithChildrenShortcut ); focusArticleViewAction->setShortcut( QKeySequence( "Ctrl+N" ) ); @@ -313,7 +320,7 @@ ScanPopup::~ScanPopup() ungrabGesture( Gestures::GDSwipeGestureType ); } -void ScanPopup::saveConfigData() +void ScanPopup::saveConfigData() const { // Save state, geometry and pin status cfg.popupWindowState = saveState(); @@ -328,7 +335,7 @@ void ScanPopup::inspectElementWhenPinned( QWebEnginePage * page ) emit inspectSignal( page ); } -void ScanPopup::applyZoomFactor() +void ScanPopup::applyZoomFactor() const { definition->setZoomFactor( cfg.preferences.zoomFactor ); } @@ -649,7 +656,7 @@ void ScanPopup::translateInputFinished() showTranslationFor( pendingWord ); } -void ScanPopup::showTranslationFor( QString const & word ) +void ScanPopup::showTranslationFor( QString const & word ) const { ui.pronounceButton->setDisabled( true ); @@ -914,7 +921,7 @@ void ScanPopup::prefixMatchFinished() } } -void ScanPopup::on_pronounceButton_clicked() +void ScanPopup::on_pronounceButton_clicked() const { definition->playSound(); } @@ -966,6 +973,11 @@ void ScanPopup::focusTranslateLine() ui.translateBox->translateLine()->selectAll(); } +void ScanPopup::stopAudio() const +{ + definition->stopSound(); +} + void ScanPopup::on_showDictionaryBar_clicked( bool checked ) { dictionaryBar.setVisible( checked ); @@ -979,7 +991,7 @@ void ScanPopup::hideTimerExpired() hideWindow(); } -void ScanPopup::pageLoaded( ArticleView * ) +void ScanPopup::pageLoaded( ArticleView * ) const { if ( !isVisible() ) return; @@ -992,7 +1004,7 @@ void ScanPopup::pageLoaded( ArticleView * ) updateBackForwardButtons(); } -void ScanPopup::showStatusBarMessage( QString const & message, int timeout, QPixmap const & icon ) +void ScanPopup::showStatusBarMessage( QString const & message, int timeout, QPixmap const & icon ) const { mainStatusBar->showMessage( message, timeout, icon ); } @@ -1104,18 +1116,18 @@ void ScanPopup::switchExpandOptionalPartsMode() emit switchExpandMode(); } -void ScanPopup::updateBackForwardButtons() +void ScanPopup::updateBackForwardButtons() const { ui.goBackButton->setEnabled( definition->canGoBack() ); ui.goForwardButton->setEnabled( definition->canGoForward() ); } -void ScanPopup::on_goBackButton_clicked() +void ScanPopup::on_goBackButton_clicked() const { definition->back(); } -void ScanPopup::on_goForwardButton_clicked() +void ScanPopup::on_goForwardButton_clicked() const { definition->forward(); } @@ -1127,7 +1139,7 @@ void ScanPopup::setDictionaryIconSize() dictionaryBar.setDictionaryIconSize( extent ); } -void ScanPopup::setGroupByName( QString const & name ) +void ScanPopup::setGroupByName( QString const & name ) const { int i; for ( i = 0; i < ui.groupList->count(); i++ ) { @@ -1154,7 +1166,7 @@ void ScanPopup::alwaysOnTopClicked( bool checked ) } } -void ScanPopup::titleChanged( ArticleView *, QString const & title ) +void ScanPopup::titleChanged( ArticleView *, QString const & title ) const { unsigned groupId = ui.groupList->getCurrentGroup(); diff --git a/src/ui/scanpopup.hh b/src/ui/scanpopup.hh index bb363cce..7be1c63e 100644 --- a/src/ui/scanpopup.hh +++ b/src/ui/scanpopup.hh @@ -42,14 +42,14 @@ public: /// Applies current zoom factor to the popup's view. Should be called when /// it's changed. - void applyZoomFactor(); + void applyZoomFactor() const; void applyWordsZoomLevel(); /// Translate the word void translateWord( QString const & word ); void setDictionaryIconSize(); - void saveConfigData(); + void saveConfigData() const; #ifdef HAVE_X11 /// Interaction with scan flag window @@ -98,7 +98,7 @@ public slots: /// From the dictionary bar. void editGroupRequested(); - void setGroupByName( QString const & name ); + void setGroupByName( QString const & name ) const; #ifdef HAVE_X11 void showEngagePopup(); @@ -131,6 +131,7 @@ private: Ui::ScanPopup ui; ArticleView * definition; QAction escapeAction, switchExpandModeAction, focusTranslateLineAction; + QAction stopAudioAction; QAction openSearchAction; QString pendingWord; // Word that is going to be translated WordFinder wordFinder; @@ -186,23 +187,23 @@ private: /// Returns inputWord, chopped with appended ... if it's too long/ QString elideInputWord(); - void updateBackForwardButtons(); + void updateBackForwardButtons() const; - void showTranslationFor( QString const & inputPhrase ); + void showTranslationFor( QString const & inputPhrase ) const; void updateSuggestionList(); void updateSuggestionList( QString const & text ); private slots: void currentGroupChanged( int ); void prefixMatchFinished(); - void on_pronounceButton_clicked(); + void on_pronounceButton_clicked() const; void pinButtonClicked( bool checked ); void on_showDictionaryBar_clicked( bool checked ); - void showStatusBarMessage( QString const &, int, QPixmap const & ); + void showStatusBarMessage( QString const &, int, QPixmap const & ) const; void on_sendWordButton_clicked(); void on_sendWordToFavoritesButton_clicked(); - void on_goBackButton_clicked(); - void on_goForwardButton_clicked(); + void on_goBackButton_clicked() const; + void on_goForwardButton_clicked() const; void hideTimerExpired(); @@ -212,7 +213,7 @@ private slots: /// polling stops. void mouseGrabPoll(); - void pageLoaded( ArticleView * ); + void pageLoaded( ArticleView * ) const; void escapePressed(); @@ -224,12 +225,13 @@ private slots: void translateInputFinished(); void focusTranslateLine(); + void stopAudio() const; void typingEvent( QString const & ); void alwaysOnTopClicked( bool checked ); - void titleChanged( ArticleView *, QString const & title ); + void titleChanged( ArticleView *, QString const & title ) const; }; #endif