Skip to content

Commit

Permalink
feat: add founded dictionaries bar in the scanpop dialog (#2050)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang authored Jan 2, 2025
1 parent d96deec commit f13e223
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ui/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,6 +33,7 @@
#ifdef HAVE_X11
#include <fixx11h.h>
#endif
#include "scanpopup.hh"

#if defined( Q_OS_MAC )
#include "macos/gd_clipboard.hh"
Expand Down
69 changes: 66 additions & 3 deletions src/ui/scanpopup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -176,14 +178,17 @@ 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 );

ui.pinButton->setChecked( cfg.pinPopupWindow );

if ( cfg.pinPopupWindow ) {
dictionaryBar.setMovable( true );
dictionaryBar.setMovable( false );
Qt::WindowFlags flags = pinnedWindowFlags;
if ( cfg.popupWindowAlwaysOnTop ) {
flags |= Qt::WindowStaysOnTopHint;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/ui/scanpopup.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include "ui_scanpopup.h"
#include <QDialog>
#include <QClipboard>
#include <QToolBar>
#include "history.hh"
#include "dictionarybar.hh"
#include "mainstatusbar.hh"
#ifdef HAVE_X11
#include "scanflag.hh"
#endif

#include <QActionGroup>

/// This is a popup dialog to show translations when clipboard scanning mode
/// is enabled.
class ScanPopup: public QMainWindow, KeyboardState
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -227,4 +232,6 @@ private slots:
void alwaysOnTopClicked( bool checked );

void titleChanged( ArticleView *, QString const & title ) const;
void updateFoundInDictsList();
void onActionTriggered();
};

0 comments on commit f13e223

Please sign in to comment.