diff --git a/src/ui/mainwindow.hh b/src/ui/mainwindow.hh index ff67b1a4a..ae48f764c 100644 --- a/src/ui/mainwindow.hh +++ b/src/ui/mainwindow.hh @@ -17,7 +17,6 @@ #include "audio/audioplayerfactory.hh" #include "instances.hh" #include "article_maker.hh" -#include "scanpopup.hh" #include "ui/articleview.hh" #include "wordfinder.hh" #include "dictionarybar.hh" @@ -34,6 +33,7 @@ #ifdef HAVE_X11 #include #endif +#include "scanpopup.hh" #if defined( Q_OS_MAC ) #include "macos/gd_clipboard.hh" diff --git a/src/ui/scanpopup.cc b/src/ui/scanpopup.cc index bcb955920..23521d59c 100644 --- a/src/ui/scanpopup.cc +++ b/src/ui/scanpopup.cc @@ -77,6 +77,7 @@ ScanPopup::ScanPopup( QWidget * parent, hideTimer( this ) { ui.setupUi( this ); + toolbar = new QToolBar( "Found Dictionary", this ); if ( layoutDirection() == Qt::RightToLeft ) { // Adjust button icons for Right-To-Left layout @@ -104,6 +105,7 @@ ScanPopup::ScanPopup( QWidget * parent, connect( this, &ScanPopup::closeMenu, definition, &ArticleView::closePopupMenu ); connect( definition, &ArticleView::sendWordToHistory, this, &ScanPopup::sendWordToHistory ); connect( definition, &ArticleView::typingEvent, this, &ScanPopup::typingEvent ); + connect( definition, &ArticleView::updateFoundInDictsList, this, &ScanPopup::updateFoundInDictsList ); openSearchAction.setShortcut( QKeySequence( "Ctrl+F" ) ); openSearchAction.setShortcutContext( Qt::WidgetWithChildrenShortcut ); @@ -148,7 +150,7 @@ ScanPopup::ScanPopup( QWidget * parent, dictionaryBar.setMutedDictionaries( grp ? &grp->popupMutedDictionaries : nullptr ); } - addToolBar( Qt::RightToolBarArea, &dictionaryBar ); + addToolBar( Qt::RightToolBarArea, toolbar ); connect( &dictionaryBar, &DictionaryBar::editGroupRequested, this, &ScanPopup::editGroupRequested ); connect( this, &ScanPopup::closeMenu, &dictionaryBar, &DictionaryBar::closePopupMenu ); @@ -176,6 +178,9 @@ ScanPopup::ScanPopup( QWidget * parent, restoreState( cfg.popupWindowState ); } + //fix this toolbar + addToolBar( Qt::TopToolBarArea, &dictionaryBar ); + ui.onTopButton->setChecked( cfg.popupWindowAlwaysOnTop ); ui.onTopButton->setVisible( cfg.pinPopupWindow ); connect( ui.onTopButton, &QAbstractButton::clicked, this, &ScanPopup::alwaysOnTopClicked ); @@ -183,7 +188,7 @@ ScanPopup::ScanPopup( QWidget * parent, ui.pinButton->setChecked( cfg.pinPopupWindow ); if ( cfg.pinPopupWindow ) { - dictionaryBar.setMovable( true ); + dictionaryBar.setMovable( false ); Qt::WindowFlags flags = pinnedWindowFlags; if ( cfg.popupWindowAlwaysOnTop ) { flags |= Qt::WindowStaysOnTopHint; @@ -289,6 +294,64 @@ ScanPopup::ScanPopup( QWidget * parent, applyWordsZoomLevel(); } +void ScanPopup::onActionTriggered() +{ + QAction * action = qobject_cast< QAction * >( sender() ); + if ( action != nullptr ) { + auto dictId = action->data().toString(); + qDebug() << "Action triggered:" << dictId; + definition->jumpToDictionary( dictId, true ); + } +} + +void ScanPopup::updateFoundInDictsList() +{ + if ( !toolbar->isVisible() ) { + // nothing to do, the list is not visible + return; + } + toolbar->setUpdatesEnabled( false ); + + unsigned currentId = ui.groupList->getCurrentGroup(); + Instances::Group const * grp = groups.findGroup( currentId ); + + auto dictionaries = grp ? grp->dictionaries : allDictionaries; + QStringList ids = definition->getArticlesList(); + QString activeId = definition->getActiveArticleId(); + toolbar->clear(); + if ( actionGroup != nullptr ) { + actionGroup->deleteLater(); + } + actionGroup = new QActionGroup( this ); + actionGroup->setExclusive( true ); + for ( QStringList::const_iterator i = ids.constBegin(); i != ids.constEnd(); ++i ) { + // Find this dictionary + + for ( unsigned x = dictionaries.size(); x--; ) { + if ( dictionaries[ x ]->getId() == i->toUtf8().data() ) { + + auto dictionary = dictionaries[ x ]; + QIcon icon = dictionary->getIcon(); + QString dictName = QString::fromUtf8( dictionary->getName().c_str() ); + QAction * action = new QAction( dictName, this ); + action->setIcon( icon ); + QString id = QString::fromStdString( dictionary->getId() ); + action->setData( id ); + action->setCheckable( true ); + if ( id == activeId ) { + action->setChecked( true ); + } + connect( action, &QAction::triggered, this, &ScanPopup::onActionTriggered ); + toolbar->addAction( action ); + actionGroup->addAction( action ); + break; + } + } + } + + toolbar->setUpdatesEnabled( true ); +} + void ScanPopup::refresh() { // currentIndexChanged() signal is very trigger-happy. To avoid triggering @@ -898,7 +961,7 @@ void ScanPopup::pinButtonClicked( bool checked ) #endif setWindowTitle( QString( "%1 - GoldenDict-ng" ).arg( elideInputWord() ) ); - dictionaryBar.setMovable( true ); + dictionaryBar.setMovable( false ); hideTimer.stop(); } else { diff --git a/src/ui/scanpopup.hh b/src/ui/scanpopup.hh index 8d2f9c372..a14d9f7a9 100644 --- a/src/ui/scanpopup.hh +++ b/src/ui/scanpopup.hh @@ -11,6 +11,7 @@ #include "ui_scanpopup.h" #include #include +#include #include "history.hh" #include "dictionarybar.hh" #include "mainstatusbar.hh" @@ -18,6 +19,8 @@ #include "scanflag.hh" #endif +#include + /// This is a popup dialog to show translations when clipboard scanning mode /// is enabled. class ScanPopup: public QMainWindow, KeyboardState @@ -136,6 +139,8 @@ private: WordFinder wordFinder; Config::Events configEvents; DictionaryBar dictionaryBar; + QToolBar * toolbar; + QActionGroup * actionGroup = nullptr; MainStatusBar * mainStatusBar; /// Fonts saved before words zooming is in effect, so it could be reset back. QFont wordListDefaultFont, translateLineDefaultFont, groupListDefaultFont; @@ -227,4 +232,6 @@ private slots: void alwaysOnTopClicked( bool checked ); void titleChanged( ArticleView *, QString const & title ) const; + void updateFoundInDictsList(); + void onActionTriggered(); };