2021-08-14 07:25:10 +00:00
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
2009-01-28 20:55:45 +00:00
* Part of GoldenDict . Licensed under GPLv3 or later , see the LICENSE file */
2014-05-20 13:59:56 +00:00
# ifndef NO_EPWING_SUPPORT
# include "epwing_book.hh"
# endif
2009-01-28 20:55:45 +00:00
# include "mainwindow.hh"
2021-08-05 06:57:22 +00:00
# include <QWebEngineProfile>
2009-04-30 15:29:03 +00:00
# include "editdictionaries.hh"
# include "loaddictionaries.hh"
2009-02-05 20:55:00 +00:00
# include "preferences.hh"
2010-11-14 16:28:50 +00:00
# include "about.hh"
2011-11-02 23:37:50 +00:00
# include "mruqmenu.hh"
2014-02-04 13:35:42 +00:00
# include "gestures.hh"
2014-02-28 12:36:28 +00:00
# include "dictheadwords.hh"
2009-04-10 12:48:40 +00:00
# include <limits.h>
2013-05-30 13:24:21 +00:00
# include <QDebug>
# include <QTextStream>
2009-01-28 20:55:45 +00:00
# include <QDir>
2013-05-31 04:20:25 +00:00
# include <QUrl>
2009-01-28 20:55:45 +00:00
# include <QMessageBox>
# include <QIcon>
2011-11-02 23:37:50 +00:00
# include <QList>
2009-01-28 20:55:45 +00:00
# include <QToolBar>
2009-02-05 20:55:00 +00:00
# include <QCloseEvent>
2009-02-08 14:02:27 +00:00
# include <QDesktopServices>
2013-05-30 13:24:21 +00:00
# include <QProcess>
# include <QCryptographicHash>
# include <QFileDialog>
# include <QPrinter>
# include <QPageSetupDialog>
# include <QPrintPreviewDialog>
# include <QPrintDialog>
2013-06-08 04:27:18 +00:00
# include <QRunnable>
# include <QThreadPool>
# include <QSslConfiguration>
2013-05-30 13:24:21 +00:00
# include <limits.h>
2009-01-28 20:55:45 +00:00
# include <set>
# include <map>
2013-11-16 18:34:09 +00:00
# include "gddebug.hh"
2013-05-30 13:24:21 +00:00
2012-09-24 12:51:51 +00:00
# include "dictinfo.hh"
2012-11-28 19:32:37 +00:00
# include "fsencoding.hh"
2013-01-17 09:08:53 +00:00
# include "historypanewidget.hh"
2021-11-27 07:17:33 +00:00
# include "utils.hh"
2022-02-27 05:17:37 +00:00
# include <qscreen.h>
2014-04-03 14:21:02 +00:00
# include "ui_authentication.h"
2022-01-09 01:52:40 +00:00
# include "resourceschemehandler.h"
2009-01-28 20:55:45 +00:00
2022-11-19 18:48:26 +00:00
# include "keyboardstate.hh"
2012-06-03 18:29:49 +00:00
# ifdef Q_OS_MAC
2013-12-26 14:13:10 +00:00
# include "macmouseover.hh"
2012-06-03 18:29:49 +00:00
# endif
2012-09-18 23:01:31 +00:00
# ifdef Q_OS_WIN32
# include <windows.h>
2013-01-26 14:49:02 +00:00
# include "wstring.hh"
# include "wstring_qt.hh"
2012-09-18 23:01:31 +00:00
# endif
2022-05-23 12:14:26 +00:00
# include <QWebEngineSettings>
# include <QWebEngineProfile>
2022-12-13 12:01:18 +00:00
# include <QProxyStyle>
2022-05-23 12:14:26 +00:00
2013-05-31 05:28:36 +00:00
# ifdef HAVE_X11
2022-02-27 14:42:40 +00:00
# if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
2022-05-28 01:38:10 +00:00
# include <QGuiApplication>
2022-02-27 14:42:40 +00:00
# else
2012-12-29 10:01:36 +00:00
# include <QX11Info>
2022-02-27 14:42:40 +00:00
# endif
2012-12-29 10:01:36 +00:00
# include <X11/Xlib.h>
2019-01-28 13:27:48 +00:00
# include <fixx11h.h>
2012-12-29 10:01:36 +00:00
# endif
2014-04-18 12:33:44 +00:00
# define MIN_THREAD_COUNT 4
2009-01-28 20:55:45 +00:00
using std : : set ;
using std : : wstring ;
using std : : map ;
using std : : pair ;
2022-12-15 07:11:09 +00:00
# ifndef QT_NO_SSL
2013-06-07 13:00:30 +00:00
class InitSSLRunnable : public QRunnable
{
virtual void run ( )
{
/// This action force SSL library initialisation which may continue a few seconds
QSslConfiguration : : setDefaultConfiguration ( QSslConfiguration : : defaultConfiguration ( ) ) ;
}
} ;
# endif
2022-05-23 12:14:26 +00:00
void MainWindow : : changeWebEngineViewFont ( )
{
if ( cfg . preferences . webFontFamily . isEmpty ( ) )
{
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > resetFontFamily ( QWebEngineSettings : : StandardFont ) ;
}
else
{
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > setFontFamily ( QWebEngineSettings : : StandardFont ,
cfg . preferences . webFontFamily ) ;
}
}
2009-04-12 20:46:25 +00:00
MainWindow : : MainWindow ( Config : : Class & cfg_ ) :
2009-02-05 20:55:00 +00:00
trayIcon ( 0 ) ,
2009-04-20 14:13:39 +00:00
groupLabel ( & searchPaneTitleBar ) ,
2011-06-05 11:49:50 +00:00
foundInDictsLabel ( & dictsPaneTitleBar ) ,
2010-04-27 07:49:37 +00:00
escAction ( this ) ,
2009-04-12 22:36:47 +00:00
focusTranslateLineAction ( this ) ,
2009-04-09 22:11:38 +00:00
addTabAction ( this ) ,
closeCurrentTabAction ( this ) ,
2010-09-16 18:52:40 +00:00
closeAllTabAction ( this ) ,
closeRestTabAction ( this ) ,
2009-04-09 22:11:38 +00:00
switchToNextTabAction ( this ) ,
switchToPrevTabAction ( this ) ,
2012-12-30 09:29:19 +00:00
showDictBarNamesAction ( tr ( " Show Names in Dictionary &Bar " ) , this ) ,
useSmallIconsInToolbarsAction ( tr ( " Show Small Icons in &Toolbars " ) , this ) ,
2011-06-26 11:53:28 +00:00
toggleMenuBarAction ( tr ( " &Menubar " ) , this ) ,
2012-09-16 10:19:47 +00:00
switchExpandModeAction ( this ) ,
2014-03-04 16:58:51 +00:00
focusHeadwordsDlgAction ( this ) ,
2014-05-11 11:52:25 +00:00
focusArticleViewAction ( this ) ,
2017-11-07 14:45:21 +00:00
addAllTabToFavoritesAction ( this ) ,
2009-02-08 20:20:02 +00:00
trayIconMenu ( this ) ,
2009-01-28 20:55:45 +00:00
addTab ( this ) ,
2009-04-12 20:46:25 +00:00
cfg ( cfg_ ) ,
2013-01-17 14:24:13 +00:00
history ( History : : Load ( ) , cfg_ . preferences . maxStringsInHistory , cfg_ . maxHeadwordSize ) ,
2013-06-11 18:31:01 +00:00
dictionaryBar ( this , configEvents , cfg . editDictionaryCommandLine , cfg . preferences . maxDictionaryRefsInContextMenu ) ,
2012-12-10 14:14:13 +00:00
articleMaker ( dictionaries , groupInstances , cfg . preferences . displayStyle ,
cfg . preferences . addonStyle ) ,
2009-08-31 12:18:08 +00:00
articleNetMgr ( this , dictionaries , articleMaker ,
2013-04-08 12:20:12 +00:00
cfg . preferences . disallowContentFromOtherSites , cfg . preferences . hideGoldenDictHeader ) ,
2009-03-26 19:00:08 +00:00
dictNetMgr ( this ) ,
2018-03-21 17:49:34 +00:00
audioPlayerFactory ( cfg . preferences ) ,
2009-01-29 19:16:25 +00:00
wordFinder ( this ) ,
2012-11-25 11:37:57 +00:00
newReleaseCheckTimer ( this ) ,
2013-06-07 12:59:18 +00:00
latestReleaseReply ( 0 ) ,
2012-11-25 11:37:57 +00:00
wordListSelChanged ( false )
2013-01-23 20:36:53 +00:00
, wasMaximized ( false )
2013-01-31 13:01:08 +00:00
, blockUpdateWindowTitle ( false )
2014-03-03 13:46:41 +00:00
, headwordsDlg ( 0 )
2014-04-16 16:18:28 +00:00
, ftsIndexing ( dictionaries )
, ftsDlg ( 0 )
2014-06-23 15:11:15 +00:00
, helpWindow ( 0 )
2022-01-24 14:23:38 +00:00
, starIcon ( " :/icons/star.svg " )
, blueStarIcon ( " :/icons/star_blue.svg " )
2012-12-13 16:17:44 +00:00
# ifdef Q_OS_WIN32
, gdAskMessage ( 0xFFFFFFFF )
# endif
2009-01-28 20:55:45 +00:00
{
2014-04-16 16:18:28 +00:00
if ( QThreadPool : : globalInstance ( ) - > maxThreadCount ( ) < MIN_THREAD_COUNT )
QThreadPool : : globalInstance ( ) - > setMaxThreadCount ( MIN_THREAD_COUNT ) ;
2022-12-15 07:11:09 +00:00
# ifndef QT_NO_SSL
2013-06-07 13:00:30 +00:00
QThreadPool : : globalInstance ( ) - > start ( new InitSSLRunnable ) ;
# endif
2022-04-23 08:15:23 +00:00
GlobalBroadcaster : : instance ( ) - > setPreference ( & cfg . preferences ) ;
2021-08-05 06:57:22 +00:00
2022-12-22 01:08:41 +00:00
localSchemeHandler = new LocalSchemeHandler ( articleNetMgr , this ) ;
2022-05-13 16:00:23 +00:00
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " gdlookup " , localSchemeHandler ) ;
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " bword " , localSchemeHandler ) ;
2022-06-07 00:11:10 +00:00
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " entry " , localSchemeHandler ) ;
2022-05-13 16:00:23 +00:00
iframeSchemeHandler = new IframeSchemeHandler ( this ) ;
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " ifr " , iframeSchemeHandler ) ;
QStringList localSchemes = { " gdau " , " gico " , " qrcx " , " bres " , " gdprg " , " gdvideo " , " gdpicture " , " gdtts " } ;
2022-12-22 01:08:41 +00:00
resourceSchemeHandler = new ResourceSchemeHandler ( articleNetMgr , this ) ;
2022-05-13 16:00:23 +00:00
for ( int i = 0 ; i < localSchemes . size ( ) ; i + + )
{
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( localSchemes . at ( i ) . toLatin1 ( ) ,
resourceSchemeHandler ) ;
2021-09-21 10:22:08 +00:00
}
2021-08-05 06:57:22 +00:00
2022-12-22 01:08:41 +00:00
QWebEngineProfile : : defaultProfile ( ) - > setUrlRequestInterceptor ( new WebUrlRequestInterceptor ( this ) ) ;
2021-12-11 16:34:37 +00:00
2022-02-08 04:55:15 +00:00
if ( ! cfg . preferences . hideGoldenDictHeader ) {
2022-09-18 00:57:17 +00:00
QWebEngineProfile : : defaultProfile ( ) - > setHttpUserAgent ( QWebEngineProfile : : defaultProfile ( ) - > httpUserAgent ( ) + " GoldenDict/WebEngine " ) ;
2022-02-08 04:55:15 +00:00
}
2021-06-10 16:13:11 +00:00
qRegisterMetaType < Config : : InputPhrase > ( ) ;
2014-05-20 13:59:56 +00:00
# ifndef NO_EPWING_SUPPORT
Epwing : : initialize ( ) ;
# endif
2009-01-28 20:55:45 +00:00
ui . setupUi ( this ) ;
2013-06-02 11:20:33 +00:00
articleMaker . setCollapseParameters ( cfg . preferences . collapseBigArticles , cfg . preferences . articleSizeLimit ) ;
2014-02-04 13:35:42 +00:00
// Set own gesture recognizers
2022-03-27 15:05:45 +00:00
# ifndef Q_OS_MAC
2014-02-04 13:35:42 +00:00
Gestures : : registerRecognizers ( ) ;
2022-03-27 15:05:45 +00:00
# endif
2015-11-01 12:22:18 +00:00
// use our own, custom statusbar
2011-06-27 18:54:15 +00:00
setStatusBar ( 0 ) ;
mainStatusBar = new MainStatusBar ( this ) ;
2009-01-28 20:55:45 +00:00
// Make the toolbar
2012-12-30 09:29:19 +00:00
navToolbar = addToolBar ( tr ( " &Navigation " ) ) ;
2009-04-03 21:24:07 +00:00
navToolbar - > setObjectName ( " navToolbar " ) ;
2022-01-24 14:23:38 +00:00
navBack = navToolbar - > addAction ( QIcon ( " :/icons/previous.svg " ) , tr ( " Back " ) ) ;
2012-12-27 08:30:43 +00:00
navToolbar - > widgetForAction ( navBack ) - > setObjectName ( " backButton " ) ;
2022-01-24 14:23:38 +00:00
navForward = navToolbar - > addAction ( QIcon ( " :/icons/next.svg " ) , tr ( " Forward " ) ) ;
2012-12-27 08:30:43 +00:00
navToolbar - > widgetForAction ( navForward ) - > setObjectName ( " forwardButton " ) ;
2009-04-18 18:47:01 +00:00
2013-02-07 15:24:24 +00:00
QWidget * translateBoxWidget = new QWidget ( this ) ;
QHBoxLayout * translateBoxLayout = new QHBoxLayout ( translateBoxWidget ) ;
translateBoxWidget - > setLayout ( translateBoxLayout ) ;
translateBoxLayout - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
translateBoxLayout - > setSpacing ( 0 ) ;
2013-01-06 11:22:20 +00:00
// translate box
groupListInToolbar = new GroupComboBox ( navToolbar ) ;
2013-02-07 15:24:24 +00:00
groupListInToolbar - > setSizePolicy ( QSizePolicy : : Fixed , QSizePolicy : : Preferred ) ;
groupListInToolbar - > setSizeAdjustPolicy ( QComboBox : : AdjustToContents ) ;
translateBoxLayout - > addWidget ( groupListInToolbar ) ;
2013-01-06 11:22:20 +00:00
translateBox = new TranslateBox ( navToolbar ) ;
2013-02-07 18:15:34 +00:00
translateBox - > setSizePolicy ( QSizePolicy : : MinimumExpanding , QSizePolicy : : Fixed ) ;
2013-02-07 15:24:24 +00:00
translateBoxLayout - > addWidget ( translateBox ) ;
translateBoxToolBarAction = navToolbar - > addWidget ( translateBoxWidget ) ;
2013-01-06 11:22:20 +00:00
// scan popup
2022-11-19 14:19:21 +00:00
navToolbar - > addSeparator ( ) ;
2011-05-31 03:35:43 +00:00
2022-11-19 15:40:57 +00:00
enableScanningAction = navToolbar - > addAction ( QIcon ( " :/icons/wizard.svg " ) , tr ( " Enable Scanning " ) ) ;
enableScanningAction - > setCheckable ( true ) ;
2022-11-19 14:19:21 +00:00
2022-11-19 15:40:57 +00:00
navToolbar - > widgetForAction ( enableScanningAction ) - > setObjectName ( " scanPopupButton " ) ;
2022-11-19 14:19:21 +00:00
if ( cfg . preferences . startWithScanPopupOn )
2022-08-31 13:53:56 +00:00
{
2022-11-19 15:40:57 +00:00
enableScanningAction - > setIcon ( QIcon ( " :/icons/wizard-selected.svg " ) ) ;
enableScanningAction - > setChecked ( true ) ;
2022-08-31 13:53:56 +00:00
}
2022-12-26 02:08:17 +00:00
connect ( enableScanningAction , & QAction : : toggled , this , & MainWindow : : scanEnableToggled ) ;
2009-02-08 20:20:02 +00:00
2022-11-19 14:19:21 +00:00
navToolbar - > addSeparator ( ) ;
2012-12-26 13:46:04 +00:00
2013-01-06 11:22:20 +00:00
// sound
2013-08-21 08:02:46 +00:00
navPronounce = navToolbar - > addAction ( QIcon ( " :/icons/playsound_full.png " ) , tr ( " Pronounce Word (Alt+S) " ) ) ;
2009-05-14 19:46:25 +00:00
navPronounce - > setShortcut ( QKeySequence ( " Alt+S " ) ) ;
2009-04-10 21:07:03 +00:00
navPronounce - > setEnabled ( false ) ;
2012-12-27 08:30:43 +00:00
navToolbar - > widgetForAction ( navPronounce ) - > setObjectName ( " soundButton " ) ;
2009-04-10 21:07:03 +00:00
connect ( navPronounce , SIGNAL ( triggered ( ) ) ,
this , SLOT ( pronounce ( ) ) ) ;
2009-04-30 19:57:25 +00:00
// zooming
2012-12-26 17:16:21 +00:00
// named separator (to be able to hide it via CSS)
navToolbar - > widgetForAction ( navToolbar - > addSeparator ( ) ) - > setObjectName ( " separatorBeforeZoom " ) ;
2022-12-05 01:54:37 +00:00
zoomIn = navToolbar - > addAction ( QIcon ( " :/icons/icon32_zoomin.png " ) , tr ( " Zoom In " ) ) ;
2013-01-21 23:01:00 +00:00
zoomIn - > setShortcuts ( QList < QKeySequence > ( ) < <
QKeySequence : : ZoomIn < <
QKeySequence ( " Ctrl+= " ) ) ;
2012-12-26 17:16:21 +00:00
navToolbar - > widgetForAction ( zoomIn ) - > setObjectName ( " zoomInButton " ) ;
2022-12-05 01:54:37 +00:00
zoomOut = navToolbar - > addAction ( QIcon ( " :/icons/icon32_zoomout.png " ) , tr ( " Zoom Out " ) ) ;
2010-07-02 11:19:02 +00:00
zoomOut - > setShortcut ( QKeySequence : : ZoomOut ) ;
2012-12-26 17:16:21 +00:00
navToolbar - > widgetForAction ( zoomOut ) - > setObjectName ( " zoomOutButton " ) ;
2022-12-05 01:54:37 +00:00
zoomBase = navToolbar - > addAction ( QIcon ( " :/icons/icon32_zoombase.png " ) , tr ( " Normal Size " ) ) ;
2010-07-02 11:19:02 +00:00
zoomBase - > setShortcut ( QKeySequence ( " Ctrl+0 " ) ) ;
2012-12-26 17:16:21 +00:00
navToolbar - > widgetForAction ( zoomBase ) - > setObjectName ( " zoomBaseButton " ) ;
// named separator (to be able to hide it via CSS)
navToolbar - > widgetForAction ( navToolbar - > addSeparator ( ) ) - > setObjectName ( " separatorBeforeSave " ) ;
2009-04-30 19:57:25 +00:00
2012-12-26 13:46:04 +00:00
navToolbar - > addAction ( ui . saveArticle ) ;
2012-12-26 17:16:21 +00:00
navToolbar - > widgetForAction ( ui . saveArticle ) - > setObjectName ( " saveArticleButton " ) ;
2012-12-26 13:46:04 +00:00
navToolbar - > addAction ( ui . print ) ;
2012-12-26 17:16:21 +00:00
navToolbar - > widgetForAction ( ui . print ) - > setObjectName ( " printButton " ) ;
2012-12-26 13:46:04 +00:00
2017-05-05 14:39:51 +00:00
navToolbar - > widgetForAction ( navToolbar - > addSeparator ( ) ) - > setObjectName ( " separatorBeforeAddToFavorites " ) ;
2017-10-23 14:21:43 +00:00
addToFavorites = navToolbar - > addAction ( starIcon , tr ( " Add current tab to Favorites " ) ) ;
2017-05-05 14:39:51 +00:00
navToolbar - > widgetForAction ( addToFavorites ) - > setObjectName ( " addToFavoritesButton " ) ;
2022-12-26 02:08:17 +00:00
connect ( addToFavorites , & QAction : : triggered , this , & MainWindow : : handleAddToFavoritesButton ) ;
connect ( ui . actionAddToFavorites , & QAction : : triggered , this , & MainWindow : : addCurrentTabToFavorites ) ;
2017-05-05 14:39:51 +00:00
2012-12-29 18:46:43 +00:00
beforeOptionsSeparator = navToolbar - > addSeparator ( ) ;
navToolbar - > widgetForAction ( beforeOptionsSeparator ) - > setObjectName ( " beforeOptionsSeparator " ) ;
beforeOptionsSeparator - > setVisible ( cfg . preferences . hideMenubar ) ;
QMenu * buttonMenu = new QMenu ( this ) ;
buttonMenu - > addAction ( ui . dictionaries ) ;
buttonMenu - > addAction ( ui . preferences ) ;
buttonMenu - > addSeparator ( ) ;
2017-05-05 14:39:51 +00:00
buttonMenu - > addMenu ( ui . menuFavorites ) ;
2012-12-29 18:46:43 +00:00
buttonMenu - > addMenu ( ui . menuHistory ) ;
buttonMenu - > addSeparator ( ) ;
buttonMenu - > addMenu ( ui . menuFile ) ;
buttonMenu - > addMenu ( ui . menuView ) ;
2014-04-16 16:18:28 +00:00
buttonMenu - > addMenu ( ui . menuSearch ) ;
2012-12-29 18:46:43 +00:00
buttonMenu - > addMenu ( ui . menu_Help ) ;
2014-04-17 14:31:51 +00:00
ui . fullTextSearchAction - > setEnabled ( cfg . preferences . fts . enabled ) ;
2013-01-23 13:40:49 +00:00
menuButton = new QToolButton ( navToolbar ) ;
2012-12-29 18:46:43 +00:00
menuButton - > setPopupMode ( QToolButton : : InstantPopup ) ;
menuButton - > setMenu ( buttonMenu ) ;
2022-01-29 05:25:51 +00:00
menuButton - > setIcon ( QIcon ( " :/icons/menu_button.svg " ) ) ;
2012-12-29 18:46:43 +00:00
menuButton - > addAction ( ui . menuOptions ) ;
menuButton - > setToolTip ( tr ( " Menu Button " ) ) ;
menuButton - > setObjectName ( " menuButton " ) ;
2012-12-31 18:23:14 +00:00
menuButton - > setFocusPolicy ( Qt : : NoFocus ) ;
2012-12-29 18:46:43 +00:00
menuButtonAction = navToolbar - > addWidget ( menuButton ) ;
menuButtonAction - > setVisible ( cfg . preferences . hideMenubar ) ;
2012-12-24 11:30:03 +00:00
// Make the search pane's titlebar
groupLabel . setText ( tr ( " Look up in: " ) ) ;
groupListInDock = new GroupComboBox ( & searchPaneTitleBar ) ;
searchPaneTitleBarLayout . setContentsMargins ( 8 , 5 , 8 , 4 ) ;
searchPaneTitleBarLayout . addWidget ( & groupLabel ) ;
searchPaneTitleBarLayout . addWidget ( groupListInDock ) ;
searchPaneTitleBarLayout . addStretch ( ) ;
searchPaneTitleBar . setLayout ( & searchPaneTitleBarLayout ) ;
ui . searchPane - > setTitleBarWidget ( & searchPaneTitleBar ) ;
2022-12-26 02:08:17 +00:00
connect ( ui . searchPane - > toggleViewAction ( ) , & QAction : : triggered , this , & MainWindow : : updateSearchPaneAndBar ) ;
2012-12-24 11:30:03 +00:00
2013-01-05 11:26:55 +00:00
if ( cfg . preferences . searchInDock )
2012-12-24 11:30:03 +00:00
{
groupList = groupListInDock ;
translateLine = ui . translateLine ;
wordList = ui . wordList ;
}
else
{
groupList = groupListInToolbar ;
translateLine = translateBox - > translateLine ( ) ;
wordList = translateBox - > wordList ( ) ;
}
2013-01-25 15:42:44 +00:00
wordList - > attachFinder ( & wordFinder ) ;
// for the old UI:
ui . wordList - > setTranslateLine ( ui . translateLine ) ;
2012-12-24 11:30:03 +00:00
groupList - > setFocusPolicy ( Qt : : ClickFocus ) ;
wordList - > setFocusPolicy ( Qt : : ClickFocus ) ;
wordListDefaultFont = wordList - > font ( ) ;
translateLineDefaultFont = translateLine - > font ( ) ;
2019-01-26 19:11:27 +00:00
groupListDefaultFont = groupList - > font ( ) ;
2012-12-24 11:30:03 +00:00
// Make the dictionaries pane's titlebar
foundInDictsLabel . setText ( tr ( " Found in Dictionaries: " ) ) ;
dictsPaneTitleBarLayout . addWidget ( & foundInDictsLabel ) ;
2012-12-27 12:11:11 +00:00
dictsPaneTitleBarLayout . setContentsMargins ( 5 , 5 , 5 , 5 ) ;
2012-12-24 11:30:03 +00:00
dictsPaneTitleBar . setLayout ( & dictsPaneTitleBarLayout ) ;
2012-12-27 12:11:11 +00:00
dictsPaneTitleBar . setObjectName ( " dictsPaneTitleBar " ) ;
2012-12-24 11:30:03 +00:00
ui . dictsPane - > setTitleBarWidget ( & dictsPaneTitleBar ) ;
ui . dictsList - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
connect ( ui . dictsPane , SIGNAL ( visibilityChanged ( bool ) ) ,
this , SLOT ( dictsPaneVisibilityChanged ( bool ) ) ) ;
2022-12-26 02:08:17 +00:00
connect ( ui . dictsList , & QListWidget : : itemClicked , this , & MainWindow : : foundDictsPaneClicked ) ;
2012-12-24 11:30:03 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . dictsList , & QWidget : : customContextMenuRequested , this , & MainWindow : : foundDictsContextMenuRequested ) ;
2012-12-24 11:30:03 +00:00
2022-12-26 02:08:17 +00:00
connect ( zoomIn , & QAction : : triggered , this , & MainWindow : : zoomin ) ;
connect ( zoomOut , & QAction : : triggered , this , & MainWindow : : zoomout ) ;
connect ( zoomBase , & QAction : : triggered , this , & MainWindow : : unzoom ) ;
2009-04-30 19:57:25 +00:00
2010-07-02 11:19:02 +00:00
ui . menuZoom - > addAction ( zoomIn ) ;
ui . menuZoom - > addAction ( zoomOut ) ;
ui . menuZoom - > addAction ( zoomBase ) ;
ui . menuZoom - > addSeparator ( ) ;
2022-12-05 01:54:37 +00:00
wordsZoomIn = ui . menuZoom - > addAction ( QIcon ( " :/icons/icon32_zoomin.png " ) , tr ( " Words Zoom In " ) ) ;
2013-01-21 23:01:00 +00:00
wordsZoomIn - > setShortcuts ( QList < QKeySequence > ( ) < <
QKeySequence ( " Alt++ " ) < <
QKeySequence ( " Alt+= " ) ) ;
2022-12-05 01:54:37 +00:00
wordsZoomOut = ui . menuZoom - > addAction ( QIcon ( " :/icons/icon32_zoomout.png " ) , tr ( " Words Zoom Out " ) ) ;
2010-07-02 11:19:02 +00:00
wordsZoomOut - > setShortcut ( QKeySequence ( " Alt+- " ) ) ;
2022-12-05 01:54:37 +00:00
wordsZoomBase = ui . menuZoom - > addAction ( QIcon ( " :/icons/icon32_zoombase.png " ) , tr ( " Words Normal Size " ) ) ;
2010-07-02 11:19:02 +00:00
wordsZoomBase - > setShortcut ( QKeySequence ( " Alt+0 " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( wordsZoomIn , & QAction : : triggered , this , & MainWindow : : doWordsZoomIn ) ;
connect ( wordsZoomOut , & QAction : : triggered , this , & MainWindow : : doWordsZoomOut ) ;
connect ( wordsZoomBase , & QAction : : triggered , this , & MainWindow : : doWordsZoomBase ) ;
2010-07-02 11:19:02 +00:00
2009-04-30 19:57:25 +00:00
// tray icon
2022-12-26 02:08:17 +00:00
connect ( trayIconMenu . addAction ( tr ( " Show &Main Window " ) ) ,
& QAction : : triggered ,
this ,
& MainWindow : : showMainWindow ) ;
2022-11-19 15:40:57 +00:00
trayIconMenu . addAction ( enableScanningAction ) ;
2022-11-19 15:30:31 +00:00
2009-02-08 20:20:02 +00:00
trayIconMenu . addSeparator ( ) ;
2022-12-26 02:08:17 +00:00
connect ( trayIconMenu . addAction ( tr ( " &Quit " ) ) , & QAction : : triggered , this , & MainWindow : : quitApp ) ;
2009-04-08 22:25:46 +00:00
2014-05-11 11:52:25 +00:00
addGlobalAction ( & escAction , SLOT ( handleEsc ( ) ) ) ;
2010-04-27 07:49:37 +00:00
escAction . setShortcut ( QKeySequence ( " Esc " ) ) ;
2014-05-11 11:52:25 +00:00
addGlobalAction ( & focusTranslateLineAction , SLOT ( focusTranslateLine ( ) ) ) ;
2010-01-13 18:04:39 +00:00
focusTranslateLineAction . setShortcuts ( QList < QKeySequence > ( ) < <
QKeySequence ( " Alt+D " ) < <
QKeySequence ( " Ctrl+L " ) ) ;
2009-04-08 22:25:46 +00:00
2014-05-11 11:52:25 +00:00
addGlobalAction ( & focusHeadwordsDlgAction , SLOT ( focusHeadwordsDialog ( ) ) ) ;
2014-03-04 16:58:51 +00:00
focusHeadwordsDlgAction . setShortcut ( QKeySequence ( " Ctrl+D " ) ) ;
2014-05-11 11:52:25 +00:00
addGlobalAction ( & focusArticleViewAction , SLOT ( focusArticleView ( ) ) ) ;
focusArticleViewAction . setShortcut ( QKeySequence ( " Ctrl+N " ) ) ;
addGlobalAction ( ui . fullTextSearchAction , SLOT ( showFullTextSearchDialog ( ) ) ) ;
2014-04-16 16:18:28 +00:00
2009-04-09 22:11:38 +00:00
addTabAction . setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
addTabAction . setShortcut ( QKeySequence ( " Ctrl+T " ) ) ;
2010-09-16 18:52:40 +00:00
// Tab management
2011-11-02 23:37:50 +00:00
tabListMenu = new MRUQMenu ( tr ( " Opened tabs " ) , ui . tabWidget ) ;
2022-12-26 02:08:17 +00:00
connect ( tabListMenu , & MRUQMenu : : ctrlReleased , this , & MainWindow : : ctrlReleased ) ;
2010-09-16 18:52:40 +00:00
2022-12-26 02:08:17 +00:00
connect ( & addTabAction , & QAction : : triggered , this , & MainWindow : : addNewTab ) ;
2009-04-09 22:11:38 +00:00
2009-04-29 23:55:41 +00:00
addAction ( & addTabAction ) ;
2009-04-09 22:11:38 +00:00
closeCurrentTabAction . setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
closeCurrentTabAction . setShortcut ( QKeySequence ( " Ctrl+W " ) ) ;
2010-09-16 18:52:40 +00:00
closeCurrentTabAction . setText ( tr ( " Close current tab " ) ) ;
2009-04-09 22:11:38 +00:00
2022-12-26 02:08:17 +00:00
connect ( & closeCurrentTabAction , & QAction : : triggered , this , & MainWindow : : closeCurrentTab ) ;
2009-04-09 22:11:38 +00:00
2009-04-29 23:55:41 +00:00
addAction ( & closeCurrentTabAction ) ;
2009-04-09 22:11:38 +00:00
2010-09-16 18:52:40 +00:00
closeAllTabAction . setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
closeAllTabAction . setShortcut ( QKeySequence ( " Ctrl+Shift+W " ) ) ;
closeAllTabAction . setText ( tr ( " Close all tabs " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( & closeAllTabAction , & QAction : : triggered , this , & MainWindow : : closeAllTabs ) ;
2010-09-16 18:52:40 +00:00
addAction ( & closeAllTabAction ) ;
closeRestTabAction . setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
closeRestTabAction . setText ( tr ( " Close all tabs except current " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( & closeRestTabAction , & QAction : : triggered , this , & MainWindow : : closeRestTabs ) ;
2010-09-16 18:52:40 +00:00
addAction ( & closeRestTabAction ) ;
2009-04-09 22:11:38 +00:00
switchToNextTabAction . setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
switchToNextTabAction . setShortcut ( QKeySequence ( " Ctrl+PgDown " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( & switchToNextTabAction , & QAction : : triggered , this , & MainWindow : : switchToNextTab ) ;
2009-04-09 22:11:38 +00:00
2009-04-29 23:55:41 +00:00
addAction ( & switchToNextTabAction ) ;
2009-04-09 22:11:38 +00:00
switchToPrevTabAction . setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
switchToPrevTabAction . setShortcut ( QKeySequence ( " Ctrl+PgUp " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( & switchToPrevTabAction , & QAction : : triggered , this , & MainWindow : : switchToPrevTab ) ;
2009-04-09 22:11:38 +00:00
2009-04-29 23:55:41 +00:00
addAction ( & switchToPrevTabAction ) ;
2009-04-09 22:11:38 +00:00
2012-09-16 10:19:47 +00:00
switchExpandModeAction . setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
switchExpandModeAction . setShortcuts ( QList < QKeySequence > ( ) < <
2022-02-27 05:17:37 +00:00
QKeySequence ( Qt : : CTRL | Qt : : Key_8 ) < <
QKeySequence ( Qt : : CTRL | Qt : : Key_Asterisk ) < <
QKeySequence ( Qt : : CTRL | Qt : : SHIFT | Qt : : Key_8 ) ) ;
2012-09-16 10:19:47 +00:00
2022-12-26 02:08:17 +00:00
connect ( & switchExpandModeAction , & QAction : : triggered , this , & MainWindow : : switchExpandOptionalPartsMode ) ;
2012-09-16 10:19:47 +00:00
addAction ( & switchExpandModeAction ) ;
2009-10-12 12:41:20 +00:00
2017-11-07 14:45:21 +00:00
addAllTabToFavoritesAction . setText ( tr ( " Add all tabs to Favorites " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( & addAllTabToFavoritesAction , & QAction : : triggered , this , & MainWindow : : addAllTabsToFavorites ) ;
2017-11-07 14:45:21 +00:00
2010-09-16 18:52:40 +00:00
tabMenu = new QMenu ( this ) ;
tabMenu - > addAction ( & closeCurrentTabAction ) ;
tabMenu - > addAction ( & closeRestTabAction ) ;
tabMenu - > addSeparator ( ) ;
tabMenu - > addAction ( & closeAllTabAction ) ;
2017-11-07 14:45:21 +00:00
tabMenu - > addSeparator ( ) ;
tabMenu - > addAction ( addToFavorites ) ;
tabMenu - > addAction ( & addAllTabToFavoritesAction ) ;
2010-09-16 18:52:40 +00:00
// Dictionary bar names
2009-10-12 12:41:20 +00:00
showDictBarNamesAction . setCheckable ( true ) ;
showDictBarNamesAction . setChecked ( cfg . showingDictBarNames ) ;
2022-12-26 02:08:17 +00:00
connect ( & showDictBarNamesAction , & QAction : : triggered , this , & MainWindow : : showDictBarNamesTriggered ) ;
2009-10-12 12:41:20 +00:00
2011-05-22 02:42:05 +00:00
// Use small icons in toolbars
useSmallIconsInToolbarsAction . setCheckable ( true ) ;
useSmallIconsInToolbarsAction . setChecked ( cfg . usingSmallIconsInToolbars ) ;
2022-12-26 02:08:17 +00:00
connect ( & useSmallIconsInToolbarsAction , & QAction : : triggered , this , & MainWindow : : useSmallIconsInToolbarsTriggered ) ;
2011-05-22 02:42:05 +00:00
2011-06-26 11:53:28 +00:00
// Toggle Menubar
toggleMenuBarAction . setCheckable ( true ) ;
2012-12-29 20:24:48 +00:00
toggleMenuBarAction . setChecked ( ! cfg . preferences . hideMenubar ) ;
2011-06-26 11:53:28 +00:00
toggleMenuBarAction . setShortcut ( QKeySequence ( " Ctrl+M " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( & toggleMenuBarAction , & QAction : : triggered , this , & MainWindow : : toggleMenuBarTriggered ) ;
2011-06-26 11:53:28 +00:00
2011-05-22 02:42:05 +00:00
// Populate 'View' menu
2009-09-23 18:44:38 +00:00
2011-06-26 11:53:28 +00:00
ui . menuView - > addAction ( & toggleMenuBarAction ) ;
2013-01-17 09:08:53 +00:00
ui . menuView - > addSeparator ( ) ;
2009-09-23 18:44:38 +00:00
ui . menuView - > addAction ( ui . searchPane - > toggleViewAction ( ) ) ;
2012-12-30 10:06:58 +00:00
ui . searchPane - > toggleViewAction ( ) - > setShortcut ( QKeySequence ( " Ctrl+S " ) ) ;
2011-06-05 11:49:50 +00:00
ui . menuView - > addAction ( ui . dictsPane - > toggleViewAction ( ) ) ;
2012-12-30 10:06:58 +00:00
ui . dictsPane - > toggleViewAction ( ) - > setShortcut ( QKeySequence ( " Ctrl+R " ) ) ;
2017-05-05 14:39:51 +00:00
ui . menuView - > addAction ( ui . favoritesPane - > toggleViewAction ( ) ) ;
ui . favoritesPane - > toggleViewAction ( ) - > setShortcut ( QKeySequence ( " Ctrl+I " ) ) ;
2013-01-17 09:08:53 +00:00
ui . menuView - > addAction ( ui . historyPane - > toggleViewAction ( ) ) ;
ui . historyPane - > toggleViewAction ( ) - > setShortcut ( QKeySequence ( " Ctrl+H " ) ) ;
2009-09-23 18:44:38 +00:00
ui . menuView - > addSeparator ( ) ;
ui . menuView - > addAction ( dictionaryBar . toggleViewAction ( ) ) ;
ui . menuView - > addAction ( navToolbar - > toggleViewAction ( ) ) ;
2009-10-12 12:41:20 +00:00
ui . menuView - > addSeparator ( ) ;
ui . menuView - > addAction ( & showDictBarNamesAction ) ;
2011-05-22 02:42:05 +00:00
ui . menuView - > addAction ( & useSmallIconsInToolbarsAction ) ;
2012-12-29 11:19:49 +00:00
ui . menuView - > addSeparator ( ) ;
ui . alwaysOnTop - > setChecked ( cfg . preferences . alwaysOnTop ) ;
ui . menuView - > addAction ( ui . alwaysOnTop ) ;
2009-09-23 18:44:38 +00:00
2009-09-21 17:50:03 +00:00
// Dictionary bar
2012-09-26 13:13:47 +00:00
Instances : : Group const * igrp = groupInstances . findGroup ( cfg . lastMainGroupId ) ;
if ( cfg . lastMainGroupId = = Instances : : Group : : AllGroupId )
{
if ( igrp )
igrp - > checkMutedDictionaries ( & cfg . mutedDictionaries ) ;
dictionaryBar . setMutedDictionaries ( & cfg . mutedDictionaries ) ;
}
else
{
Config : : Group * grp = cfg . getGroup ( cfg . lastMainGroupId ) ;
if ( igrp & & grp )
igrp - > checkMutedDictionaries ( & grp - > mutedDictionaries ) ;
dictionaryBar . setMutedDictionaries ( grp ? & grp - > mutedDictionaries : 0 ) ;
}
2022-11-24 12:34:21 +00:00
GlobalBroadcaster : : instance ( ) - > currentGroupId = cfg . lastMainGroupId ;
2012-09-26 13:13:47 +00:00
2009-10-12 12:41:20 +00:00
showDictBarNamesTriggered ( ) ; // Make update its state according to initial
// setting
2011-05-22 02:42:05 +00:00
useSmallIconsInToolbarsTriggered ( ) ;
2012-09-19 12:44:36 +00:00
connect ( this , SIGNAL ( clickOnDictPane ( QString const & ) ) ,
& dictionaryBar , SLOT ( dictsPaneClicked ( QString const & ) ) ) ;
2009-09-21 17:50:03 +00:00
addToolBar ( & dictionaryBar ) ;
2022-12-26 02:08:17 +00:00
connect ( dictionaryBar . toggleViewAction ( ) , & QAction : : triggered , this , & MainWindow : : dictionaryBarToggled ) ;
2009-09-24 12:20:20 +00:00
// This one will be disconnected once the slot is activated. It exists
// only to handle the initial appearance of the dictionary bar.
2022-12-26 02:08:17 +00:00
connect ( dictionaryBar . toggleViewAction ( ) , & QAction : : toggled , this , & MainWindow : : dictionaryBarToggled ) ;
2009-09-23 18:44:38 +00:00
2022-12-26 02:08:17 +00:00
connect ( & dictionaryBar , & DictionaryBar : : editGroupRequested , this , & MainWindow : : editCurrentGroup ) ;
2010-05-08 14:01:59 +00:00
2022-12-26 02:08:17 +00:00
connect ( & dictionaryBar , & DictionaryBar : : showDictionaryInfo , this , & MainWindow : : showDictionaryInfo ) ;
2012-09-25 13:13:35 +00:00
2022-12-26 02:08:17 +00:00
connect ( & dictionaryBar ,
SIGNAL ( showDictionaryHeadwords ( QString const & ) ) ,
this ,
SLOT ( showDictionaryHeadwords ( QString const & ) ) ) ;
2014-03-01 13:11:14 +00:00
2022-12-26 02:08:17 +00:00
connect ( & dictionaryBar , & DictionaryBar : : openDictionaryFolder , this , & MainWindow : : openDictionaryFolder ) ;
2013-06-09 13:31:57 +00:00
2017-05-05 14:39:51 +00:00
// Favorites
ui . favoritesPaneWidget - > setUp ( & cfg , ui . menuFavorites ) ;
2017-05-13 10:18:25 +00:00
ui . favoritesPaneWidget - > setSaveInterval ( cfg . preferences . favoritesStoreInterval ) ;
2017-05-05 14:39:51 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . favoritesPane , & QDockWidget : : visibilityChanged , this , & MainWindow : : updateFavoritesMenu ) ;
2017-05-05 14:39:51 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . menuFavorites , & QMenu : : aboutToShow , this , & MainWindow : : updateFavoritesMenu ) ;
2017-05-05 14:39:51 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . favoritesPaneWidget ,
& FavoritesPaneWidget : : favoritesItemRequested ,
this ,
& MainWindow : : headwordFromFavorites ) ;
2017-05-05 14:39:51 +00:00
2009-10-21 19:37:07 +00:00
// History
2013-01-17 09:08:53 +00:00
ui . historyPaneWidget - > setUp ( & cfg , & history , ui . menuHistory ) ;
2012-09-11 13:03:10 +00:00
history . enableAdd ( cfg . preferences . storeHistory ) ;
2009-11-01 16:20:26 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . historyPaneWidget , & HistoryPaneWidget : : historyItemRequested , this , & MainWindow : : showHistoryItem ) ;
2013-01-17 09:08:53 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . historyPane , & QDockWidget : : visibilityChanged , this , & MainWindow : : updateHistoryMenu ) ;
2013-01-17 09:08:53 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . menuHistory , & QMenu : : aboutToShow , this , & MainWindow : : updateHistoryMenu ) ;
2013-01-17 09:08:53 +00:00
2021-11-18 06:28:12 +00:00
# if !defined( HAVE_X11 )
2009-10-09 21:03:55 +00:00
// Show tray icon early so the user would be happy. It won't be functional
// though until the program inits fully.
2019-05-23 05:34:48 +00:00
// Do not create dummy tray icon in X. Cause QT5 failed to upgrade systemtray context menu.
// And as result menu for some DEs apppear to be empty, for example in MATE DE.
2009-01-28 20:55:45 +00:00
2009-10-09 21:03:55 +00:00
if ( cfg . preferences . enableTrayIcon )
{
2018-04-17 07:28:20 +00:00
trayIcon = new QSystemTrayIcon ( QIcon : : fromTheme ( " goldendict-tray " , QIcon ( " :/icons/programicon_old.png " ) ) , this ) ;
2009-02-08 20:20:02 +00:00
trayIcon - > setToolTip ( tr ( " Loading... " ) ) ;
2009-10-09 21:03:55 +00:00
trayIcon - > show ( ) ;
}
2019-05-23 05:34:48 +00:00
# endif
2009-04-18 18:47:01 +00:00
2022-12-26 02:08:17 +00:00
connect ( navBack , & QAction : : triggered , this , & MainWindow : : backClicked ) ;
connect ( navForward , & QAction : : triggered , this , & MainWindow : : forwardClicked ) ;
2009-01-28 20:55:45 +00:00
addTab . setAutoRaise ( true ) ;
2011-07-10 10:29:52 +00:00
addTab . setToolTip ( tr ( " New Tab " ) ) ;
2020-10-22 17:41:37 +00:00
addTab . setFocusPolicy ( Qt : : NoFocus ) ;
2022-01-24 14:23:38 +00:00
addTab . setIcon ( QIcon ( " :/icons/addtab.svg " ) ) ;
2009-01-28 20:55:45 +00:00
2011-06-23 14:17:09 +00:00
ui . tabWidget - > setHideSingleTab ( cfg . preferences . hideSingleTab ) ;
2009-01-28 20:55:45 +00:00
ui . tabWidget - > clear ( ) ;
ui . tabWidget - > setCornerWidget ( & addTab , Qt : : TopLeftCorner ) ;
//ui.tabWidget->setCornerWidget( &closeTab, Qt::TopRightCorner );
ui . tabWidget - > setMovable ( true ) ;
2009-04-29 23:18:26 +00:00
# ifndef Q_OS_WIN32
2009-01-28 20:55:45 +00:00
ui . tabWidget - > setDocumentMode ( true ) ;
2009-04-29 23:18:26 +00:00
# endif
2009-01-28 20:55:45 +00:00
2010-09-16 18:52:40 +00:00
ui . tabWidget - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2022-12-26 02:08:17 +00:00
connect ( & addTab , & QAbstractButton : : clicked , this , & MainWindow : : addNewTab ) ;
2009-01-28 20:55:45 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . tabWidget , & MainTabWidget : : doubleClicked , this , & MainWindow : : addNewTab ) ;
2011-07-19 10:03:13 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . tabWidget , & QTabWidget : : tabCloseRequested , this , & MainWindow : : tabCloseRequested ) ;
2009-01-28 20:55:45 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . tabWidget , & QTabWidget : : currentChanged , this , & MainWindow : : tabSwitched ) ;
2009-04-10 21:07:03 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . tabWidget , & QWidget : : customContextMenuRequested , this , & MainWindow : : tabMenuRequested ) ;
2010-09-16 18:52:40 +00:00
2009-01-28 20:55:45 +00:00
ui . tabWidget - > setTabsClosable ( true ) ;
2022-12-26 02:08:17 +00:00
connect ( ui . quit , & QAction : : triggered , this , & MainWindow : : quitApp ) ;
2009-02-05 20:55:00 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . dictionaries , & QAction : : triggered , this , & MainWindow : : editDictionaries ) ;
2009-01-28 20:55:45 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . preferences , & QAction : : triggered , this , & MainWindow : : editPreferences ) ;
2009-04-18 18:47:01 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . visitHomepage , & QAction : : triggered , this , & MainWindow : : visitHomepage ) ;
connect ( ui . visitForum , & QAction : : triggered , this , & MainWindow : : visitForum ) ;
connect ( ui . openConfigFolder , & QAction : : triggered , this , & MainWindow : : openConfigFolder ) ;
connect ( ui . about , & QAction : : triggered , this , & MainWindow : : showAbout ) ;
connect ( ui . showReference , & QAction : : triggered , this , & MainWindow : : showGDHelp ) ;
2009-02-05 20:55:00 +00:00
2022-02-27 06:26:49 +00:00
connect ( groupListInDock , & GroupComboBox : : currentIndexChanged ,
this , & MainWindow : : currentGroupChanged ) ;
2012-12-24 11:30:03 +00:00
2022-02-27 06:26:49 +00:00
connect ( groupListInToolbar , & GroupComboBox : : currentIndexChanged ,
this , & MainWindow : : currentGroupChanged ) ;
2009-02-06 17:04:11 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . translateLine , & QLineEdit : : textChanged , this , & MainWindow : : translateInputChanged ) ;
2009-04-18 18:47:01 +00:00
2022-12-26 02:08:17 +00:00
connect ( translateBox - > translateLine ( ) , & QLineEdit : : textChanged , this , & MainWindow : : translateInputChanged ) ;
2012-12-24 11:30:03 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . translateLine , SIGNAL ( returnPressed ( ) ) , this , SLOT ( translateInputFinished ( ) ) ) ;
2009-01-28 20:55:45 +00:00
2012-12-24 11:30:03 +00:00
connect ( translateBox - > translateLine ( ) , SIGNAL ( returnPressed ( ) ) ,
this , SLOT ( translateInputFinished ( ) ) ) ;
2022-12-26 02:08:17 +00:00
connect ( ui . wordList , & QListWidget : : itemSelectionChanged , this , & MainWindow : : wordListSelectionChanged ) ;
2012-12-24 21:56:31 +00:00
connect ( translateBox - > wordList ( ) , SIGNAL ( itemDoubleClicked ( QListWidgetItem * ) ) ,
this , SLOT ( wordListItemActivated ( QListWidgetItem * ) ) ) ;
2009-04-18 18:47:01 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . wordList , & QListWidget : : itemClicked , this , & MainWindow : : wordListItemActivated ) ;
2012-11-25 11:37:57 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . wordList , & WordList : : statusBarMessage , this , & MainWindow : : showStatusBarMessage ) ;
2013-01-25 15:42:44 +00:00
2022-12-26 02:08:17 +00:00
connect ( translateBox - > wordList ( ) , & WordList : : statusBarMessage , this , & MainWindow : : showStatusBarMessage ) ;
2013-01-25 15:42:44 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . dictsList , & QListWidget : : itemSelectionChanged , this , & MainWindow : : dictsListSelectionChanged ) ;
2011-06-05 11:49:50 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . dictsList , & QListWidget : : itemDoubleClicked , this , & MainWindow : : dictsListItemActivated ) ;
2013-06-28 16:00:13 +00:00
2022-12-26 02:08:17 +00:00
connect ( & configEvents , & Config : : Events : : mutedDictionariesChanged , this , & MainWindow : : mutedDictionariesChanged ) ;
2009-09-23 18:44:38 +00:00
2012-12-25 09:25:54 +00:00
this - > installEventFilter ( this ) ;
2009-03-29 17:38:54 +00:00
ui . translateLine - > installEventFilter ( this ) ;
2012-12-24 11:30:03 +00:00
translateBox - > translateLine ( ) - > installEventFilter ( this ) ;
2009-03-29 17:38:54 +00:00
ui . wordList - > installEventFilter ( this ) ;
2012-12-24 11:30:03 +00:00
translateBox - > wordList ( ) - > installEventFilter ( this ) ;
2011-06-07 08:26:49 +00:00
ui . wordList - > viewport ( ) - > installEventFilter ( this ) ;
2012-12-24 11:30:03 +00:00
translateBox - > wordList ( ) - > viewport ( ) - > installEventFilter ( this ) ;
2011-06-07 08:26:49 +00:00
ui . dictsList - > installEventFilter ( this ) ;
ui . dictsList - > viewport ( ) - > installEventFilter ( this ) ;
2011-11-02 23:37:50 +00:00
//tabWidget doesn't propagate Ctrl+Tab to the parent widget unless event filter is installed
ui . tabWidget - > installEventFilter ( this ) ;
2009-03-29 17:38:54 +00:00
2013-01-17 09:08:53 +00:00
ui . historyList - > installEventFilter ( this ) ;
2022-07-14 15:14:28 +00:00
ui . favoritesTree - > installEventFilter ( this ) ;
groupListInDock - > installEventFilter ( this ) ;
groupListInToolbar - > installEventFilter ( this ) ;
2022-12-26 02:08:17 +00:00
connect ( & ftsIndexing , & FTS : : FtsIndexing : : newIndexingName , this , & MainWindow : : showFTSIndexingName ) ;
2015-11-01 12:22:18 +00:00
2019-09-30 16:36:59 +00:00
# ifndef Q_OS_MAC
2013-01-29 19:20:53 +00:00
{
if ( cfg . mainWindowGeometry . size ( ) )
restoreGeometry ( cfg . mainWindowGeometry ) ;
if ( cfg . mainWindowState . size ( ) )
restoreState ( cfg . mainWindowState , 1 ) ;
}
2019-09-30 16:36:59 +00:00
# endif
2013-01-05 11:26:55 +00:00
updateSearchPaneAndBar ( cfg . preferences . searchInDock ) ;
ui . searchPane - > setVisible ( cfg . preferences . searchInDock ) ;
2009-04-03 17:10:27 +00:00
applyProxySettings ( ) ;
2022-05-23 12:14:26 +00:00
//set webengineview font
changeWebEngineViewFont ( ) ;
2022-12-26 02:08:17 +00:00
connect ( & dictNetMgr , & QNetworkAccessManager : : proxyAuthenticationRequired , this , & MainWindow : : proxyAuthentication ) ;
2014-04-03 14:21:02 +00:00
2022-12-26 02:08:17 +00:00
connect ( & articleNetMgr ,
& QNetworkAccessManager : : proxyAuthenticationRequired ,
this ,
& MainWindow : : proxyAuthentication ) ;
2014-04-03 14:21:02 +00:00
2020-11-12 15:57:10 +00:00
setupNetworkCache ( cfg . preferences . maxNetworkCacheSize ) ;
2009-01-28 20:55:45 +00:00
makeDictionaries ( ) ;
2009-10-21 19:37:07 +00:00
// After we have dictionaries and groups, we can populate history
2012-02-16 14:56:25 +00:00
// historyChanged();
2009-10-21 19:37:07 +00:00
2013-02-01 12:36:01 +00:00
setWindowTitle ( " GoldenDict " ) ;
2013-01-31 13:01:08 +00:00
2019-09-30 16:36:59 +00:00
# ifdef Q_OS_MAC
{
if ( cfg . mainWindowGeometry . size ( ) )
restoreGeometry ( cfg . mainWindowGeometry ) ;
if ( cfg . mainWindowState . size ( ) )
restoreState ( cfg . mainWindowState , 1 ) ;
}
# endif
2013-01-31 13:01:08 +00:00
blockUpdateWindowTitle = true ;
2009-01-28 20:55:45 +00:00
addNewTab ( ) ;
2009-01-29 19:16:25 +00:00
2010-09-16 18:52:40 +00:00
// Create tab list menu
createTabList ( ) ;
2009-02-08 18:35:29 +00:00
// Show the initial welcome text
{
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-02-08 18:35:29 +00:00
2013-01-20 18:25:12 +00:00
history . enableAdd ( false ) ;
2013-01-31 13:01:08 +00:00
blockUpdateWindowTitle = true ;
2011-07-01 19:22:40 +00:00
view - > showDefinition ( tr ( " Welcome! " ) , Instances : : Group : : HelpGroupId ) ;
2013-01-20 18:25:12 +00:00
history . enableAdd ( cfg . preferences . storeHistory ) ;
2009-02-08 18:35:29 +00:00
}
2009-04-18 18:47:01 +00:00
2012-12-24 11:30:03 +00:00
translateLine - > setFocus ( ) ;
2009-02-05 20:55:00 +00:00
2022-12-11 01:22:54 +00:00
applyQtStyleSheet ( cfg . preferences . displayStyle , cfg . preferences . addonStyle , cfg . preferences . darkMode ) ;
2019-03-19 14:37:51 +00:00
2019-05-13 15:17:27 +00:00
makeScanPopup ( ) ;
2009-10-09 21:03:55 +00:00
if ( trayIcon )
{
// Upgrade existing dummy tray icon into a full-functional one
trayIcon - > setContextMenu ( & trayIconMenu ) ;
trayIcon - > show ( ) ;
2022-12-26 02:08:17 +00:00
connect ( trayIcon , & QSystemTrayIcon : : activated , this , & MainWindow : : trayIconActivated ) ;
2009-10-09 21:03:55 +00:00
}
2009-02-05 20:55:00 +00:00
updateTrayIcon ( ) ;
2009-04-30 22:09:04 +00:00
// Update zoomers
2019-01-31 12:45:30 +00:00
adjustCurrentZoomFactor ( ) ;
scaleArticlesByCurrentZoomFactor ( ) ;
2010-07-02 11:19:02 +00:00
applyWordsZoomLevel ( ) ;
2009-04-30 22:09:04 +00:00
2009-04-18 18:47:01 +00:00
// Update autostart info
setAutostart ( cfg . preferences . autoStart ) ;
2009-04-19 21:32:18 +00:00
// Initialize global hotkeys
2009-04-21 18:27:26 +00:00
installHotKeys ( ) ;
2009-04-19 21:32:18 +00:00
2012-12-29 11:19:49 +00:00
if ( cfg . preferences . alwaysOnTop )
{
on_alwaysOnTop_triggered ( true ) ;
}
2009-02-05 20:55:00 +00:00
// Only show window initially if it wasn't configured differently
if ( ! cfg . preferences . enableTrayIcon | | ! cfg . preferences . startToTray )
2009-05-13 13:47:51 +00:00
{
2009-02-05 20:55:00 +00:00
show ( ) ;
2009-05-13 13:47:51 +00:00
focusTranslateLine ( ) ;
}
2009-04-20 19:54:34 +00:00
2022-12-26 02:08:17 +00:00
connect ( & newReleaseCheckTimer , & QTimer : : timeout , this , & MainWindow : : checkForNewRelease ) ;
2009-04-20 19:54:34 +00:00
2011-06-26 11:53:28 +00:00
if ( cfg . preferences . hideMenubar )
{
toggleMenuBarTriggered ( false ) ;
}
2009-04-20 19:54:34 +00:00
prepareNewReleaseChecks ( ) ;
2009-05-17 22:02:54 +00:00
// makeDictionaries() didn't do deferred init - we do it here, at the end.
2022-03-30 07:34:59 +00:00
doDeferredInit ( dictionaries ) ;
2011-06-27 18:54:15 +00:00
updateStatusLine ( ) ;
2012-06-03 18:29:49 +00:00
2014-05-12 13:53:13 +00:00
# ifdef Q_OS_MAC
2013-12-26 14:13:10 +00:00
if ( cfg . preferences . startWithScanPopupOn & & ! MacMouseOver : : isAXAPIEnabled ( ) )
2013-04-12 14:30:24 +00:00
mainStatusBar - > showMessage ( tr ( " Accessibility API is not enabled " ) , 10000 ,
2022-01-27 00:23:57 +00:00
QPixmap ( " :/icons/error.svg " ) ) ;
2013-04-12 14:30:24 +00:00
# endif
2013-01-23 20:36:53 +00:00
wasMaximized = isMaximized ( ) ;
2013-02-05 12:51:23 +00:00
history . setSaveInterval ( cfg . preferences . historyStoreInterval ) ;
2014-02-04 13:35:42 +00:00
ui . centralWidget - > grabGesture ( Gestures : : GDPinchGestureType ) ;
ui . centralWidget - > grabGesture ( Gestures : : GDSwipeGestureType ) ;
2014-03-20 13:44:46 +00:00
if ( layoutDirection ( ) = = Qt : : RightToLeft )
{
// Adjust button icons for Right-To-Left layout
2022-01-24 14:23:38 +00:00
navBack - > setIcon ( QIcon ( " :/icons/next.svg " ) ) ;
navForward - > setIcon ( QIcon ( " :/icons/previous.svg " ) ) ;
2014-03-20 13:44:46 +00:00
}
2022-05-20 09:18:38 +00:00
2022-08-09 12:29:59 +00:00
inspector . reset ( new ArticleInspector ( this ) ) ;
2022-08-02 14:27:14 +00:00
2022-11-19 18:48:26 +00:00
connect ( QApplication : : clipboard ( ) , & QClipboard : : changed , this , & MainWindow : : clipboardChange ) ;
2022-11-28 01:24:49 +00:00
# ifdef Q_OS_WIN
// Regiser and update URL Scheme for windows
// https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)
// Windows will automatically map registry key to Computer\HKEY_CLASSES_ROOT\ */
QSettings urlRegistry ( R " (HKEY_CURRENT_USER \ Software \ Classes) " , QSettings::NativeFormat) ;
urlRegistry . beginGroup ( " goldendict " ) ;
urlRegistry . setValue ( " Default " , " URL: goldendict Protocol " ) ;
urlRegistry . setValue ( " URL Protocol " , " " ) ;
urlRegistry . setValue ( " shell/open/command/Default " ,
QString ( " \" %1 \" " ) . arg ( QDir : : toNativeSeparators ( QApplication : : applicationFilePath ( ) ) ) + " \" %1 \" " ) ;
urlRegistry . endGroup ( ) ;
# endif
2022-08-02 14:27:14 +00:00
}
2022-11-19 18:48:26 +00:00
void MainWindow : : clipboardChange ( QClipboard : : Mode m )
2022-08-02 14:27:14 +00:00
{
2022-11-19 18:48:26 +00:00
if ( scanPopup & & enableScanningAction - > isChecked ( ) )
2022-08-02 14:27:14 +00:00
{
2022-11-20 02:54:22 +00:00
# ifdef HAVE_X11
2022-11-19 18:48:26 +00:00
if ( m = = QClipboard : : Clipboard ) {
if ( ! cfg . preferences . trackClipboardScan ) return ;
scanPopup - > translateWordFromClipboard ( ) ;
2022-11-20 02:15:23 +00:00
return ;
2022-11-19 18:48:26 +00:00
}
if ( m = = QClipboard : : Selection ) {
2022-11-20 05:39:41 +00:00
// Multiple ways to stoping a word from showing up when selecting
// Explictly disabled on preferences
2022-11-19 18:48:26 +00:00
if ( ! cfg . preferences . trackSelectionScan ) return ;
2022-11-20 05:39:41 +00:00
// Keyboard Modifier
2022-11-20 02:15:23 +00:00
if ( cfg . preferences . enableScanPopupModifiers & &
! KeyboardState : : checkModifiersPressed ( cfg . preferences . scanPopupModifiers ) ) {
return ;
}
2022-11-20 05:39:41 +00:00
// Show a Flag instead of translate directly.
// And hand over the control of showing the popup to scanFlag
if ( cfg . preferences . showScanFlag ) {
2022-11-20 06:19:27 +00:00
scanPopup - > showScanFlag ( ) ;
2022-11-20 05:39:41 +00:00
return ;
}
2022-11-19 18:48:26 +00:00
scanPopup - > translateWordFromSelection ( ) ;
}
2022-11-20 02:54:22 +00:00
# else
scanPopup - > translateWordFromClipboard ( ) ;
# endif
}
2009-01-28 20:55:45 +00:00
}
2011-11-02 23:37:50 +00:00
void MainWindow : : ctrlTabPressed ( )
{
emit fillWindowsMenu ( ) ;
tabListButton - > click ( ) ;
}
2013-01-05 11:26:55 +00:00
void MainWindow : : updateSearchPaneAndBar ( bool searchInDock )
2012-12-24 11:30:03 +00:00
{
2013-01-23 19:11:39 +00:00
QString text = translateLine - > text ( ) ;
2019-07-09 15:00:23 +00:00
if ( ftsDlg )
removeGroupComboBoxActionsFromDialog ( ftsDlg , groupList ) ;
if ( headwordsDlg )
removeGroupComboBoxActionsFromDialog ( headwordsDlg , groupList ) ;
2013-01-05 11:26:55 +00:00
if ( searchInDock )
2012-12-24 11:30:03 +00:00
{
2013-01-05 11:26:55 +00:00
cfg . preferences . searchInDock = true ;
2012-12-24 11:30:03 +00:00
2013-01-21 20:50:57 +00:00
navToolbar - > setAllowedAreas ( Qt : : AllToolBarAreas ) ;
2012-12-24 11:30:03 +00:00
groupList = groupListInDock ;
translateLine = ui . translateLine ;
wordList = ui . wordList ;
2013-02-07 15:24:24 +00:00
translateBoxToolBarAction - > setVisible ( false ) ;
2012-12-24 11:30:03 +00:00
}
else
{
2013-01-05 11:26:55 +00:00
cfg . preferences . searchInDock = false ;
2012-12-24 11:30:03 +00:00
2013-01-21 20:50:57 +00:00
// handle the main toolbar, it must not be on the side, since it should
// contain the group widget and the translate line. Valid locations: Top and Bottom.
navToolbar - > setAllowedAreas ( Qt : : BottomToolBarArea | Qt : : TopToolBarArea ) ;
if ( toolBarArea ( navToolbar ) & ( Qt : : LeftToolBarArea | Qt : : RightToolBarArea ) )
{
if ( toolBarArea ( & dictionaryBar ) = = Qt : : TopToolBarArea )
{
insertToolBar ( & dictionaryBar , navToolbar ) ;
}
else
{
addToolBar ( Qt : : TopToolBarArea , navToolbar ) ;
}
}
2012-12-24 11:30:03 +00:00
groupList = groupListInToolbar ;
translateLine = translateBox - > translateLine ( ) ;
wordList = translateBox - > wordList ( ) ;
2013-02-07 15:24:24 +00:00
translateBoxToolBarAction - > setVisible ( true ) ;
2012-12-24 11:30:03 +00:00
}
2019-07-09 15:00:23 +00:00
if ( ftsDlg )
addGroupComboBoxActionsToDialog ( ftsDlg , groupList ) ;
if ( headwordsDlg )
addGroupComboBoxActionsToDialog ( headwordsDlg , groupList ) ;
2014-03-14 12:39:54 +00:00
translateLine - > setToolTip ( tr ( " String to search in dictionaries. The wildcards '*', '?' and sets of symbols '[...]' are allowed. \n To find '*', '?', '[', ']' symbols use ' \\ *', ' \\ ?', ' \\ [', ' \\ ]' respectively " ) ) ;
2014-03-12 13:43:52 +00:00
2013-02-08 17:09:37 +00:00
// reset the flag when switching UI modes
wordListSelChanged = false ;
2013-01-25 15:42:44 +00:00
wordList - > attachFinder ( & wordFinder ) ;
2012-12-24 11:30:03 +00:00
updateGroupList ( ) ;
2013-01-21 21:49:58 +00:00
applyWordsZoomLevel ( ) ;
2013-01-27 22:12:00 +00:00
2021-06-30 16:49:58 +00:00
setTranslateBoxTextAndKeepSuffix ( text , WildcardsAreAlreadyEscaped , DisablePopup ) ;
2013-01-23 19:11:39 +00:00
focusTranslateLine ( ) ;
2012-12-24 11:30:03 +00:00
}
2009-07-31 11:40:54 +00:00
void MainWindow : : mousePressEvent ( QMouseEvent * event )
{
2011-06-07 08:26:49 +00:00
if ( handleBackForwardMouseButtons ( event ) )
{
return ;
}
2022-01-08 13:16:22 +00:00
if ( event - > button ( ) ! = Qt : : MiddleButton )
2010-09-16 18:52:40 +00:00
return QMainWindow : : mousePressEvent ( event ) ;
2011-06-07 08:26:49 +00:00
2010-09-16 18:52:40 +00:00
// middle clicked
2011-06-07 08:26:49 +00:00
QString subtype = " plain " ;
2010-09-16 18:52:40 +00:00
2022-06-24 13:20:16 +00:00
QString str = QApplication : : clipboard ( ) - > text ( subtype , QClipboard : : Selection ) ;
2021-06-30 16:49:58 +00:00
setTranslateBoxTextAndClearSuffix ( str , EscapeWildcards , NoPopupChange ) ;
2009-07-31 11:40:54 +00:00
2022-06-24 13:20:16 +00:00
QKeyEvent ev ( QEvent : : KeyPress , Qt : : Key_Enter , Qt : : NoModifier ) ;
QApplication : : sendEvent ( translateLine , & ev ) ;
2009-07-31 11:40:54 +00:00
}
2009-02-06 17:04:11 +00:00
MainWindow : : ~ MainWindow ( )
{
2014-03-03 13:46:41 +00:00
closeHeadwordsDialog ( ) ;
2014-04-16 16:18:28 +00:00
ftsIndexing . stopIndexing ( ) ;
2014-02-04 13:35:42 +00:00
ui . centralWidget - > ungrabGesture ( Gestures : : GDPinchGestureType ) ;
ui . centralWidget - > ungrabGesture ( Gestures : : GDSwipeGestureType ) ;
2019-04-22 16:50:06 +00:00
// Gestures::unregisterRecognizers();
2014-02-04 13:35:42 +00:00
2009-05-23 14:19:57 +00:00
// Close all tabs -- they should be destroyed before network managers
// do.
while ( ui . tabWidget - > count ( ) )
{
QWidget * w = ui . tabWidget - > widget ( 0 ) ;
ui . tabWidget - > removeTab ( 0 ) ;
delete w ;
}
2012-02-16 14:56:25 +00:00
2014-05-20 13:59:56 +00:00
# ifndef NO_EPWING_SUPPORT
Epwing : : finalize ( ) ;
# endif
2009-02-06 17:04:11 +00:00
}
2014-05-11 11:52:25 +00:00
void MainWindow : : addGlobalAction ( QAction * action , const char * slot )
{
action - > setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
connect ( action , SIGNAL ( triggered ( ) ) , this , slot ) ;
ui . centralWidget - > addAction ( action ) ;
ui . dictsPane - > addAction ( action ) ;
ui . searchPaneWidget - > addAction ( action ) ;
2017-05-05 14:39:51 +00:00
ui . favoritesPane - > addAction ( action ) ;
2014-05-11 11:52:25 +00:00
ui . historyPane - > addAction ( action ) ;
groupList - > addAction ( action ) ;
translateBox - > addAction ( action ) ;
}
void MainWindow : : addGlobalActionsToDialog ( QDialog * dialog )
{
dialog - > addAction ( & focusTranslateLineAction ) ;
dialog - > addAction ( & focusHeadwordsDlgAction ) ;
dialog - > addAction ( & focusArticleViewAction ) ;
dialog - > addAction ( ui . fullTextSearchAction ) ;
}
2019-07-09 15:00:23 +00:00
void MainWindow : : addGroupComboBoxActionsToDialog ( QDialog * dialog , GroupComboBox * pGroupComboBox )
{
dialog - > addActions ( pGroupComboBox - > getExternActions ( ) ) ;
}
void MainWindow : : removeGroupComboBoxActionsFromDialog ( QDialog * dialog , GroupComboBox * pGroupComboBox )
{
QList < QAction * > actions = pGroupComboBox - > getExternActions ( ) ;
for ( QList < QAction * > : : iterator it = actions . begin ( ) ; it ! = actions . end ( ) ; + + it )
dialog - > removeAction ( * it ) ;
}
2010-06-28 15:14:07 +00:00
void MainWindow : : commitData ( QSessionManager & )
{
2018-03-27 18:01:33 +00:00
commitData ( ) ;
2010-06-28 15:14:07 +00:00
}
2018-03-27 18:01:33 +00:00
void MainWindow : : commitData ( )
2010-06-28 15:14:07 +00:00
{
2020-11-12 15:57:10 +00:00
if ( cfg . preferences . clearNetworkCacheOnExit )
if ( QAbstractNetworkCache * cache = articleNetMgr . cache ( ) )
cache - > clear ( ) ;
2018-03-27 14:46:03 +00:00
try
2010-06-28 15:14:07 +00:00
{
// Save MainWindow state and geometry
cfg . mainWindowState = saveState ( 1 ) ;
cfg . mainWindowGeometry = saveGeometry ( ) ;
2018-03-27 18:01:33 +00:00
// Save popup window state and geometry
2010-06-28 15:14:07 +00:00
2018-03-27 18:01:33 +00:00
if ( scanPopup . get ( ) )
scanPopup - > saveConfigData ( ) ;
2010-06-28 15:14:07 +00:00
// Save any changes in last chosen groups etc
2015-03-30 15:01:34 +00:00
try
{
Config : : save ( cfg ) ;
}
catch ( std : : exception & e )
{
gdWarning ( " Configuration saving failed, error: %s \n " , e . what ( ) ) ;
}
2014-04-08 18:57:07 +00:00
2018-03-27 14:46:03 +00:00
// Save history
2014-04-08 18:57:07 +00:00
history . save ( ) ;
2018-03-26 14:35:49 +00:00
2018-03-27 14:46:03 +00:00
// Save favorites
2018-03-26 14:35:49 +00:00
ui . favoritesPaneWidget - > saveData ( ) ;
2018-03-27 14:46:03 +00:00
}
catch ( std : : exception & e )
{
gdWarning ( " Commit data failed, error: %s \n " , e . what ( ) ) ;
2010-06-28 15:14:07 +00:00
}
}
2009-11-11 09:40:32 +00:00
QPrinter & MainWindow : : getPrinter ( )
{
if ( printer . get ( ) )
return * printer ;
2022-11-29 03:54:31 +00:00
printer = std : : make_shared < QPrinter > ( QPrinter : : HighResolution ) ;
2009-11-11 09:40:32 +00:00
return * printer ;
}
2022-12-11 01:22:54 +00:00
void MainWindow : : applyQtStyleSheet ( QString const & displayStyle , QString const & addonStyle , bool const & darkMode )
2009-05-11 11:03:36 +00:00
{
2022-12-22 12:55:42 +00:00
# ifdef Q_OS_WIN32
2022-12-11 01:22:54 +00:00
if ( darkMode )
{
//https://forum.qt.io/topic/101391/windows-10-dark-theme
# ifdef Q_OS_WIN32
qApp - > setStyle ( QStyleFactory : : create ( " Fusion " ) ) ;
# endif
QPalette darkPalette ;
QColor darkColor = QColor ( 45 , 45 , 45 ) ;
QColor disabledColor = QColor ( 127 , 127 , 127 ) ;
darkPalette . setColor ( QPalette : : Window , darkColor ) ;
darkPalette . setColor ( QPalette : : WindowText , Qt : : white ) ;
darkPalette . setColor ( QPalette : : Base , QColor ( 18 , 18 , 18 ) ) ;
darkPalette . setColor ( QPalette : : AlternateBase , darkColor ) ;
darkPalette . setColor ( QPalette : : ToolTipBase , Qt : : white ) ;
darkPalette . setColor ( QPalette : : ToolTipText , Qt : : white ) ;
darkPalette . setColor ( QPalette : : Text , Qt : : white ) ;
darkPalette . setColor ( QPalette : : Disabled , QPalette : : Text , disabledColor ) ;
darkPalette . setColor ( QPalette : : Button , darkColor ) ;
darkPalette . setColor ( QPalette : : ButtonText , Qt : : white ) ;
2022-12-16 13:00:02 +00:00
darkPalette . setColor ( QPalette : : Dark , QColor ( 35 , 35 , 35 ) ) ;
darkPalette . setColor ( QPalette : : Shadow , QColor ( 20 , 20 , 20 ) ) ;
2022-12-11 01:22:54 +00:00
darkPalette . setColor ( QPalette : : Disabled , QPalette : : ButtonText , disabledColor ) ;
darkPalette . setColor ( QPalette : : BrightText , Qt : : red ) ;
darkPalette . setColor ( QPalette : : Link , QColor ( 42 , 130 , 218 ) ) ;
darkPalette . setColor ( QPalette : : Highlight , QColor ( 42 , 130 , 218 ) ) ;
darkPalette . setColor ( QPalette : : HighlightedText , Qt : : black ) ;
darkPalette . setColor ( QPalette : : Disabled , QPalette : : HighlightedText , disabledColor ) ;
qApp - > setPalette ( darkPalette ) ;
}
else
{
2022-12-13 12:01:18 +00:00
qApp - > setStyle ( new QProxyStyle ( ) ) ;
2022-12-11 01:22:54 +00:00
qApp - > setPalette ( QPalette ( ) ) ;
}
2022-12-22 12:55:42 +00:00
# endif
2022-12-11 01:22:54 +00:00
2009-05-11 11:03:36 +00:00
QFile builtInCssFile ( " :/qt-style.css " ) ;
builtInCssFile . open ( QFile : : ReadOnly ) ;
QByteArray css = builtInCssFile . readAll ( ) ;
2013-04-01 16:37:44 +00:00
# if defined(Q_OS_MAC)
QFile macCssFile ( " :/qt-style-macos.css " ) ;
macCssFile . open ( QFile : : ReadOnly ) ;
css + = macCssFile . readAll ( ) ;
# endif
2022-12-25 06:31:07 +00:00
# if defined(Q_OS_WIN)
QFile winCssFile ( " :/qt-style-win.css " ) ;
winCssFile . open ( QFile : : ReadOnly ) ;
css + = winCssFile . readAll ( ) ;
# endif
2009-05-11 11:03:36 +00:00
// Try loading a style sheet if there's one
QFile cssFile ( Config : : getUserQtCssFileName ( ) ) ;
if ( cssFile . open ( QFile : : ReadOnly ) )
css + = cssFile . readAll ( ) ;
2012-12-10 14:14:13 +00:00
if ( ! addonStyle . isEmpty ( ) )
{
QString name = Config : : getStylesDir ( ) + addonStyle
+ QDir : : separator ( ) + " qt-style.css " ;
QFile addonCss ( name ) ;
if ( addonCss . open ( QFile : : ReadOnly ) )
css + = addonCss . readAll ( ) ;
}
2022-12-22 12:55:42 +00:00
# ifdef Q_OS_WIN32
2022-12-11 01:22:54 +00:00
if ( darkMode ) {
css + = " QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; } " ;
}
2022-12-22 12:55:42 +00:00
# endif
2022-12-11 01:22:54 +00:00
2009-05-16 18:04:21 +00:00
setStyleSheet ( css ) ;
2009-05-11 11:03:36 +00:00
}
2009-01-28 20:55:45 +00:00
2009-02-05 20:55:00 +00:00
void MainWindow : : updateTrayIcon ( )
{
if ( ! trayIcon & & cfg . preferences . enableTrayIcon )
{
// Need to show it
2018-04-17 07:28:20 +00:00
trayIcon = new QSystemTrayIcon ( QIcon : : fromTheme ( " goldendict-tray " , QIcon ( " :/icons/programicon_old.png " ) ) , this ) ;
2009-02-08 20:20:02 +00:00
trayIcon - > setContextMenu ( & trayIconMenu ) ;
2009-02-05 20:55:00 +00:00
trayIcon - > show ( ) ;
2022-12-26 02:08:17 +00:00
connect ( trayIcon , & QSystemTrayIcon : : activated , this , & MainWindow : : trayIconActivated ) ;
2009-02-05 20:55:00 +00:00
}
2022-12-26 02:08:17 +00:00
else if ( trayIcon & & ! cfg . preferences . enableTrayIcon )
2009-02-05 20:55:00 +00:00
{
// Need to hide it
delete trayIcon ;
trayIcon = 0 ;
}
if ( trayIcon )
2009-02-08 20:20:02 +00:00
{
// Update the icon to reflect the scanning mode
2022-11-19 15:40:57 +00:00
trayIcon - > setIcon ( enableScanningAction - > isChecked ( ) ?
2018-04-17 07:28:20 +00:00
QIcon : : fromTheme ( " goldendict-scan-tray " , QIcon ( " :/icons/programicon_scan.png " ) ) :
QIcon : : fromTheme ( " goldendict-tray " , QIcon ( " :/icons/programicon_old.png " ) ) ) ;
2009-04-18 18:47:01 +00:00
2009-02-05 20:55:00 +00:00
trayIcon - > setToolTip ( " GoldenDict " ) ;
2009-02-08 20:20:02 +00:00
}
2009-04-20 12:25:26 +00:00
// The 'Close to tray' action is associated with the tray icon, so we hide
// or show it here.
ui . actionCloseToTray - > setVisible ( cfg . preferences . enableTrayIcon ) ;
2009-02-05 20:55:00 +00:00
}
2013-01-01 23:05:29 +00:00
void MainWindow : : wheelEvent ( QWheelEvent * ev )
{
if ( ev - > modifiers ( ) . testFlag ( Qt : : ControlModifier ) )
{
2022-01-08 13:16:22 +00:00
if ( ev - > angleDelta ( ) . y ( ) > 0 )
2013-01-01 23:05:29 +00:00
{
zoomin ( ) ;
}
2022-01-08 13:16:22 +00:00
else if ( ev - > angleDelta ( ) . y ( ) < 0 )
2013-01-01 23:05:29 +00:00
{
zoomout ( ) ;
}
ev - > accept ( ) ;
}
else
{
ev - > ignore ( ) ;
}
}
2009-02-05 20:55:00 +00:00
void MainWindow : : closeEvent ( QCloseEvent * ev )
{
if ( cfg . preferences . enableTrayIcon & & cfg . preferences . closeToTray )
{
2017-09-01 16:13:29 +00:00
if ( ! cfg . preferences . searchInDock )
translateBox - > setPopupEnabled ( false ) ;
2022-02-18 12:22:56 +00:00
# ifdef Q_OS_MACOS
2022-02-18 12:43:07 +00:00
if ( ! ev - > spontaneous ( ) | | ! isVisible ( ) ) {
2022-02-18 12:22:56 +00:00
return ;
}
# endif
2021-11-28 15:30:35 +00:00
# ifdef HAVE_X11
// Don't ignore the close event, because doing so cancels session logout if
// the main window is visible when the user attempts to log out.
// The main window will be only hidden, because QApplication::quitOnLastWindowClosed
2022-03-01 12:32:12 +00:00
// property is false and Qt::WA_DeleteOnClose widget is not set.
2021-11-28 15:30:35 +00:00
Q_ASSERT ( ! QApplication : : quitOnLastWindowClosed ( ) ) ;
Q_ASSERT ( ! testAttribute ( Qt : : WA_DeleteOnClose ) ) ;
# else
// Ignore the close event because closing the main window breaks global hotkeys on Windows.
ev - > ignore ( ) ;
2009-02-05 20:55:00 +00:00
hide ( ) ;
2021-11-28 15:30:35 +00:00
# endif
2009-02-05 20:55:00 +00:00
}
else
2010-05-08 13:57:41 +00:00
{
2009-02-05 20:55:00 +00:00
ev - > accept ( ) ;
2018-03-27 14:46:03 +00:00
quitApp ( ) ;
2010-05-08 13:57:41 +00:00
}
2009-02-05 20:55:00 +00:00
}
2018-03-27 14:46:03 +00:00
void MainWindow : : quitApp ( )
{
2022-05-23 15:38:04 +00:00
if ( inspector & & inspector - > isVisible ( ) )
{
2022-05-28 05:11:08 +00:00
inspector - > close ( ) ;
2022-05-23 15:38:04 +00:00
}
2018-03-27 18:01:33 +00:00
commitData ( ) ;
2018-03-27 14:46:03 +00:00
qApp - > quit ( ) ;
}
2009-04-03 17:10:27 +00:00
void MainWindow : : applyProxySettings ( )
{
2014-04-01 17:00:13 +00:00
if ( cfg . preferences . proxyServer . enabled & & cfg . preferences . proxyServer . useSystemProxy )
{
2014-04-03 14:21:02 +00:00
QList < QNetworkProxy > proxies = QNetworkProxyFactory : : systemProxyForQuery ( ) ;
if ( ! cfg . preferences . proxyServer . systemProxyUser . isEmpty ( ) )
{
proxies . first ( ) . setUser ( cfg . preferences . proxyServer . systemProxyUser ) ;
proxies . first ( ) . setPassword ( cfg . preferences . proxyServer . systemProxyPassword ) ;
}
QNetworkProxy : : setApplicationProxy ( proxies . first ( ) ) ;
2014-04-01 17:00:13 +00:00
return ;
}
2009-04-13 17:31:05 +00:00
QNetworkProxy : : ProxyType type = QNetworkProxy : : NoProxy ;
2009-04-03 17:10:27 +00:00
2009-04-13 17:31:05 +00:00
if ( cfg . preferences . proxyServer . enabled )
{
switch ( cfg . preferences . proxyServer . type )
{
case Config : : ProxyServer : : Socks5 :
type = QNetworkProxy : : Socks5Proxy ;
break ;
case Config : : ProxyServer : : HttpConnect :
type = QNetworkProxy : : HttpProxy ;
break ;
case Config : : ProxyServer : : HttpGet :
type = QNetworkProxy : : HttpCachingProxy ;
break ;
default :
break ;
}
}
QNetworkProxy proxy ( type ) ;
if ( cfg . preferences . proxyServer . enabled )
{
2009-04-03 17:10:27 +00:00
proxy . setHostName ( cfg . preferences . proxyServer . host ) ;
proxy . setPort ( cfg . preferences . proxyServer . port ) ;
2009-04-18 18:47:01 +00:00
2009-04-03 17:10:27 +00:00
if ( cfg . preferences . proxyServer . user . size ( ) )
proxy . setUser ( cfg . preferences . proxyServer . user ) ;
2009-04-18 18:47:01 +00:00
2009-04-03 17:10:27 +00:00
if ( cfg . preferences . proxyServer . password . size ( ) )
proxy . setPassword ( cfg . preferences . proxyServer . password ) ;
2009-04-13 17:31:05 +00:00
}
2009-04-18 18:47:01 +00:00
2009-04-13 17:31:05 +00:00
QNetworkProxy : : setApplicationProxy ( proxy ) ;
2009-04-03 17:10:27 +00:00
}
2020-11-12 15:57:10 +00:00
void MainWindow : : setupNetworkCache ( int maxSize )
{
// x << 20 == x * 2^20 converts mebibytes to bytes.
qint64 const maxCacheSizeInBytes = maxSize < = 0 ? qint64 ( 0 ) : static_cast < qint64 > ( maxSize ) < < 20 ;
if ( QAbstractNetworkCache * abstractCache = articleNetMgr . cache ( ) )
{
QNetworkDiskCache * const diskCache = qobject_cast < QNetworkDiskCache * > ( abstractCache ) ;
Q_ASSERT_X ( diskCache , Q_FUNC_INFO , " Unexpected network cache type. " ) ;
diskCache - > setMaximumCacheSize ( maxCacheSizeInBytes ) ;
return ;
}
if ( maxCacheSizeInBytes = = 0 )
return ; // There is currently no cache and it is not needed.
QString const cacheDirectory = Config : : getNetworkCacheDir ( ) ;
if ( ! QDir ( ) . mkpath ( cacheDirectory ) )
{
gdWarning ( " Cannot create a cache directory %s. Disabling network cache. " , cacheDirectory . toUtf8 ( ) . constData ( ) ) ;
return ;
}
QNetworkDiskCache * const diskCache = new QNetworkDiskCache ( this ) ;
diskCache - > setMaximumCacheSize ( maxCacheSizeInBytes ) ;
diskCache - > setCacheDirectory ( cacheDirectory ) ;
articleNetMgr . setCache ( diskCache ) ;
}
2009-01-28 20:55:45 +00:00
void MainWindow : : makeDictionaries ( )
{
2019-05-15 07:57:37 +00:00
Q_ASSERT ( ! scanPopup & & " Scan popup must not exist while dictionaries are initialized. "
" It does not support dictionaries changes and must be constructed anew. " ) ;
2009-03-26 19:00:08 +00:00
wordFinder . clear ( ) ;
2009-09-23 18:44:38 +00:00
dictionariesUnmuted . clear ( ) ;
2014-04-16 16:18:28 +00:00
ftsIndexing . stopIndexing ( ) ;
2014-04-17 14:18:15 +00:00
ftsIndexing . clearDictionaries ( ) ;
2014-04-16 16:18:28 +00:00
2009-05-17 22:02:54 +00:00
loadDictionaries ( this , isVisible ( ) , cfg , dictionaries , dictNetMgr , false ) ;
2009-01-28 20:55:45 +00:00
2022-03-13 16:17:34 +00:00
//create map
2022-03-14 00:22:31 +00:00
dictMap = Dictionary : : dictToMap ( dictionaries ) ;
2022-03-13 16:17:34 +00:00
2014-04-17 14:31:51 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + )
2017-03-09 16:11:17 +00:00
{
2014-04-17 14:31:51 +00:00
dictionaries [ x ] - > setFTSParameters ( cfg . preferences . fts ) ;
2017-03-09 16:11:17 +00:00
dictionaries [ x ] - > setSynonymSearchEnabled ( cfg . preferences . synonymSearchEnabled ) ;
}
2014-04-17 14:31:51 +00:00
2014-04-17 14:18:15 +00:00
ftsIndexing . setDictionaries ( dictionaries ) ;
2014-04-16 16:18:28 +00:00
ftsIndexing . doIndexing ( ) ;
2009-01-28 20:55:45 +00:00
updateStatusLine ( ) ;
updateGroupList ( ) ;
}
void MainWindow : : updateStatusLine ( )
{
unsigned articleCount = 0 , wordCount = 0 ;
for ( unsigned x = dictionaries . size ( ) ; x - - ; )
{
articleCount + = dictionaries [ x ] - > getArticleCount ( ) ;
wordCount + = dictionaries [ x ] - > getWordCount ( ) ;
}
2011-06-27 18:54:15 +00:00
mainStatusBar - > showMessage ( tr ( " %1 dictionaries, %2 articles, %3 words " ) .
2009-01-28 20:55:45 +00:00
arg ( dictionaries . size ( ) ) . arg ( articleCount ) .
2011-06-27 18:54:15 +00:00
arg ( wordCount ) , 10000 ) ;
2009-01-28 20:55:45 +00:00
}
void MainWindow : : updateGroupList ( )
{
bool haveGroups = cfg . groups . size ( ) ;
2012-12-24 11:30:03 +00:00
groupList - > setVisible ( haveGroups ) ;
2009-01-28 20:55:45 +00:00
2009-04-20 14:13:39 +00:00
groupLabel . setText ( haveGroups ? tr ( " Look up in: " ) : tr ( " Look up: " ) ) ;
2009-01-28 20:55:45 +00:00
2009-02-06 17:04:11 +00:00
// currentIndexChanged() signal is very trigger-happy. To avoid triggering
// it, we disconnect it while we're clearing and filling back groups.
2022-02-27 06:26:49 +00:00
disconnect ( groupList , & GroupComboBox : : currentIndexChanged ,
this , & MainWindow : : currentGroupChanged ) ;
2009-02-06 17:04:11 +00:00
2009-03-26 19:00:08 +00:00
groupInstances . clear ( ) ;
2009-05-18 18:01:50 +00:00
// Add dictionaryOrder first, as the 'All' group.
2009-04-01 12:00:28 +00:00
{
2022-07-08 13:47:09 +00:00
Instances : : Group g ( cfg . dictionaryOrder , dictionaries , Config : : Group ( ) ) ;
2009-01-28 20:55:45 +00:00
2009-05-23 14:19:57 +00:00
// Add any missing entries to dictionary order
Instances : : complementDictionaryOrder ( g ,
2022-07-08 13:47:09 +00:00
Instances : : Group ( cfg . inactiveDictionaries , dictionaries , Config : : Group ( ) ) ,
2009-05-23 14:19:57 +00:00
dictionaries ) ;
2009-05-18 18:01:50 +00:00
g . name = tr ( " All " ) ;
2010-05-08 14:01:59 +00:00
g . id = Instances : : Group : : AllGroupId ;
2022-03-27 10:11:23 +00:00
g . icon = " folder.png " ;
2009-05-18 18:01:50 +00:00
groupInstances . push_back ( g ) ;
2009-04-01 12:00:28 +00:00
}
2012-12-10 12:49:45 +00:00
for ( int x = 0 ; x < cfg . groups . size ( ) ; + + x )
2022-07-08 13:47:09 +00:00
groupInstances . push_back ( Instances : : Group ( cfg . groups [ x ] , dictionaries , cfg . inactiveDictionaries ) ) ;
2009-05-18 18:01:50 +00:00
// Update names for dictionaries that are present, so that they could be
// found in case they got moved.
Instances : : updateNames ( cfg , dictionaries ) ;
2012-12-24 11:30:03 +00:00
groupList - > fill ( groupInstances ) ;
groupList - > setCurrentGroup ( cfg . lastMainGroupId ) ;
2022-06-08 00:19:23 +00:00
2010-11-15 15:22:35 +00:00
updateCurrentGroupProperty ( ) ;
2009-02-06 17:04:11 +00:00
2009-09-21 17:50:03 +00:00
updateDictionaryBar ( ) ;
2013-09-20 14:25:44 +00:00
# ifdef QT_DEBUG
2013-06-10 21:39:40 +00:00
qDebug ( ) < < " Reloading all the tabs... " ;
2013-09-20 14:25:44 +00:00
# endif
2013-06-10 21:39:40 +00:00
for ( int i = 0 ; i < ui . tabWidget - > count ( ) ; + + i )
{
ArticleView & view =
dynamic_cast < ArticleView & > ( * ( ui . tabWidget - > widget ( i ) ) ) ;
view . reload ( ) ;
}
2022-02-27 06:26:49 +00:00
connect ( groupList , & GroupComboBox : : currentIndexChanged ,
this , & MainWindow : : currentGroupChanged ) ;
2009-02-01 00:08:08 +00:00
}
2009-01-28 20:55:45 +00:00
2009-09-21 17:50:03 +00:00
void MainWindow : : updateDictionaryBar ( )
{
2009-09-23 18:44:38 +00:00
if ( ! dictionaryBar . toggleViewAction ( ) - > isChecked ( ) )
return ; // It's not enabled, therefore hidden -- don't waste time
2012-12-24 11:30:03 +00:00
unsigned currentId = groupList - > getCurrentGroup ( ) ;
2012-09-26 13:59:48 +00:00
Instances : : Group * grp = groupInstances . findGroup ( currentId ) ;
2009-09-21 17:50:03 +00:00
2012-11-17 08:20:18 +00:00
dictionaryBar . setMutedDictionaries ( 0 ) ;
2012-09-26 13:59:48 +00:00
if ( grp ) { // Should always be !0, but check as a safeguard
2012-09-26 13:13:47 +00:00
if ( currentId = = Instances : : Group : : AllGroupId )
dictionaryBar . setMutedDictionaries ( & cfg . mutedDictionaries ) ;
else
{
Config : : Group * grp = cfg . getGroup ( currentId ) ;
dictionaryBar . setMutedDictionaries ( grp ? & grp - > mutedDictionaries : 0 ) ;
}
2011-05-22 02:42:05 +00:00
2012-11-17 08:20:18 +00:00
dictionaryBar . setDictionaries ( grp - > dictionaries ) ;
2022-01-23 11:41:50 +00:00
int extent = useSmallIconsInToolbarsAction . isChecked ( ) ?
QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_SmallIconSize ) :
QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_ToolBarIconSize ) ;
dictionaryBar . setDictionaryIconSize ( extent ) ;
2011-05-22 02:42:05 +00:00
}
2009-09-21 17:50:03 +00:00
}
2009-02-01 00:08:08 +00:00
void MainWindow : : makeScanPopup ( )
{
scanPopup . reset ( ) ;
2009-01-28 20:55:45 +00:00
2022-11-29 03:54:31 +00:00
scanPopup = std : : make_shared < ScanPopup > ( nullptr , cfg , articleNetMgr , audioPlayerFactory . player ( ) ,
2018-03-21 17:49:34 +00:00
dictionaries , groupInstances , history ) ;
2009-04-18 18:47:01 +00:00
2009-05-16 18:04:21 +00:00
scanPopup - > setStyleSheet ( styleSheet ( ) ) ;
2022-11-19 15:40:57 +00:00
if ( enableScanningAction - > isChecked ( ) )
2009-02-08 20:20:02 +00:00
scanPopup - > enableScanning ( ) ;
2010-05-08 14:01:59 +00:00
2012-09-16 10:19:47 +00:00
connect ( scanPopup . get ( ) , SIGNAL ( editGroupRequested ( unsigned ) ) ,
this , SLOT ( editDictionaries ( unsigned ) ) , Qt : : QueuedConnection ) ;
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) ,
& ScanPopup : : sendPhraseToMainWindow ,
this ,
& MainWindow : : phraseReceived ,
Qt : : QueuedConnection ) ;
2012-09-16 10:19:47 +00:00
2022-12-26 02:08:17 +00:00
connect ( this , & MainWindow : : setExpandOptionalParts , scanPopup . get ( ) , & ScanPopup : : setViewExpandMode ) ;
2012-09-16 10:19:47 +00:00
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : setExpandMode , this , & MainWindow : : setExpandMode ) ;
2012-09-16 10:30:14 +00:00
2022-09-08 13:11:25 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : inspectSignal , this , & MainWindow : : inspectElement ) ;
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : forceAddWordToHistory , this , & MainWindow : : forceAddWordToHistory ) ;
2012-09-24 13:20:58 +00:00
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : showDictionaryInfo , this , & MainWindow : : showDictionaryInfo ) ;
2012-09-25 13:13:35 +00:00
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : openDictionaryFolder , this , & MainWindow : : openDictionaryFolder ) ;
2013-06-09 13:31:57 +00:00
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : sendWordToHistory , this , & MainWindow : : addWordToHistory ) ;
2012-11-12 13:52:54 +00:00
2022-12-26 02:08:17 +00:00
connect ( this , & MainWindow : : setPopupGroupByName , scanPopup . get ( ) , & ScanPopup : : setGroupByName ) ;
2017-03-10 13:29:23 +00:00
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : sendWordToFavorites , this , & MainWindow : : addWordToFavorites ) ;
2017-05-15 15:08:06 +00:00
2022-12-26 02:08:17 +00:00
connect ( scanPopup . get ( ) , & ScanPopup : : isWordPresentedInFavorites , this , & MainWindow : : isWordPresentedInFavorites ) ;
2017-10-23 14:21:43 +00:00
2012-09-24 13:20:58 +00:00
# ifdef Q_OS_WIN32
connect ( scanPopup . get ( ) , SIGNAL ( isGoldenDictWindow ( HWND ) ) ,
this , SLOT ( isGoldenDictWindow ( HWND ) ) ) ;
# endif
2009-01-28 20:55:45 +00:00
}
vector < sptr < Dictionary : : Class > > const & MainWindow : : getActiveDicts ( )
2010-09-16 18:52:40 +00:00
{
2009-05-18 18:01:50 +00:00
if ( groupInstances . empty ( ) )
2009-01-28 20:55:45 +00:00
return dictionaries ;
2012-12-24 11:30:03 +00:00
int current = groupList - > currentIndex ( ) ;
2009-01-28 20:55:45 +00:00
if ( current < 0 | | current > = ( int ) groupInstances . size ( ) )
{
// This shouldn't ever happen
return dictionaries ;
}
2012-09-26 13:13:47 +00:00
Config : : MutedDictionaries const * mutedDictionaries = dictionaryBar . getMutedDictionaries ( ) ;
if ( ! dictionaryBar . toggleViewAction ( ) - > isChecked ( ) | | mutedDictionaries = = 0 )
2009-09-23 18:44:38 +00:00
return groupInstances [ current ] . dictionaries ;
else
{
vector < sptr < Dictionary : : Class > > const & activeDicts =
groupInstances [ current ] . dictionaries ;
// Populate the special dictionariesUnmuted array with only unmuted
// dictionaries
dictionariesUnmuted . clear ( ) ;
dictionariesUnmuted . reserve ( activeDicts . size ( ) ) ;
for ( unsigned x = 0 ; x < activeDicts . size ( ) ; + + x )
2012-09-26 13:13:47 +00:00
if ( ! mutedDictionaries - > contains (
2009-09-23 18:44:38 +00:00
QString : : fromStdString ( activeDicts [ x ] - > getId ( ) ) ) )
dictionariesUnmuted . push_back ( activeDicts [ x ] ) ;
return dictionariesUnmuted ;
}
2009-01-28 20:55:45 +00:00
}
2010-09-16 18:52:40 +00:00
void MainWindow : : createTabList ( )
{
2022-01-24 14:23:38 +00:00
tabListMenu - > setIcon ( QIcon ( " :/icons/windows-list.svg " ) ) ;
2022-12-26 02:08:17 +00:00
connect ( tabListMenu , & QMenu : : aboutToShow , this , & MainWindow : : fillWindowsMenu ) ;
connect ( tabListMenu , & QMenu : : triggered , this , & MainWindow : : switchToWindow ) ;
2010-09-16 18:52:40 +00:00
tabListButton = new QToolButton ( ui . tabWidget ) ;
tabListButton - > setAutoRaise ( true ) ;
tabListButton - > setIcon ( tabListMenu - > icon ( ) ) ;
tabListButton - > setMenu ( tabListMenu ) ;
2011-07-10 10:29:52 +00:00
tabListButton - > setToolTip ( tr ( " Open Tabs List " ) ) ;
2010-09-16 18:52:40 +00:00
tabListButton - > setPopupMode ( QToolButton : : InstantPopup ) ;
ui . tabWidget - > setCornerWidget ( tabListButton ) ;
2022-07-13 14:32:41 +00:00
tabListButton - > setFocusPolicy ( Qt : : NoFocus ) ;
2010-09-16 18:52:40 +00:00
}
void MainWindow : : fillWindowsMenu ( )
{
tabListMenu - > clear ( ) ;
2011-11-02 23:37:50 +00:00
if ( cfg . preferences . mruTabOrder )
2010-09-16 18:52:40 +00:00
{
2011-11-02 23:37:50 +00:00
for ( int i = 0 ; i < mruList . count ( ) ; i + + )
2010-09-16 18:52:40 +00:00
{
2011-11-02 23:37:50 +00:00
QAction * act = tabListMenu - > addAction ( ui . tabWidget - > tabIcon ( ui . tabWidget - > indexOf ( mruList . at ( i ) ) ) , ui . tabWidget - > tabText ( ui . tabWidget - > indexOf ( mruList . at ( i ) ) ) ) ;
//remember the index of the Tab to be later used in ctrlReleased()
act - > setData ( ui . tabWidget - > indexOf ( mruList . at ( i ) ) ) ;
if ( ui . tabWidget - > currentIndex ( ) = = ui . tabWidget - > indexOf ( mruList . at ( i ) ) )
{
QFont f ( act - > font ( ) ) ;
f . setBold ( true ) ;
act - > setFont ( f ) ;
}
}
if ( tabListMenu - > actions ( ) . size ( ) > 1 )
{
tabListMenu - > setActiveAction ( tabListMenu - > actions ( ) . at ( 1 ) ) ;
2010-09-16 18:52:40 +00:00
}
}
2011-11-02 23:37:50 +00:00
else
2010-09-16 18:52:40 +00:00
{
2011-11-02 23:37:50 +00:00
for ( int i = 0 ; i < ui . tabWidget - > count ( ) ; i + + )
2010-09-16 18:52:40 +00:00
{
2011-11-02 23:37:50 +00:00
QAction * act = tabListMenu - > addAction ( ui . tabWidget - > tabIcon ( i ) ,
ui . tabWidget - > tabText ( i ) ) ;
act - > setData ( i ) ;
if ( ui . tabWidget - > currentIndex ( ) = = i )
{
QFont f ( act - > font ( ) ) ;
f . setBold ( true ) ;
act - > setFont ( f ) ;
}
2010-09-16 18:52:40 +00:00
}
}
2011-11-02 23:37:50 +00:00
return ;
2010-09-16 18:52:40 +00:00
}
void MainWindow : : switchToWindow ( QAction * act )
{
int idx = act - > data ( ) . toInt ( ) ;
ui . tabWidget - > setCurrentIndex ( idx ) ;
}
2009-01-28 20:55:45 +00:00
void MainWindow : : addNewTab ( )
2009-04-13 12:51:25 +00:00
{
createNewTab ( true , tr ( " (untitled) " ) ) ;
}
ArticleView * MainWindow : : createNewTab ( bool switchToIt ,
QString const & name )
2009-01-28 20:55:45 +00:00
{
2018-03-21 17:49:34 +00:00
ArticleView * view = new ArticleView ( this , articleNetMgr , audioPlayerFactory . player ( ) ,
dictionaries , groupInstances , false , cfg ,
2014-04-16 16:18:28 +00:00
* ui . searchInPageAction ,
2009-09-23 18:44:38 +00:00
dictionaryBar . toggleViewAction ( ) ,
2012-12-24 11:30:03 +00:00
groupList ) ;
2009-01-28 20:55:45 +00:00
2022-08-19 11:56:49 +00:00
connect ( view , & ArticleView : : inspectSignal , this , & MainWindow : : inspectElement ) ;
2022-05-20 09:18:38 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : titleChanged , this , & MainWindow : : titleChanged ) ;
2009-01-28 20:55:45 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : iconChanged , this , & MainWindow : : iconChanged ) ;
2009-01-28 20:55:45 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : pageLoaded , this , & MainWindow : : pageLoaded ) ;
2009-04-10 21:07:03 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : updateFoundInDictsList , this , & MainWindow : : updateFoundInDictsList ) ;
2022-01-09 04:54:50 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : openLinkInNewTab , this , & MainWindow : : openLinkInNewTab ) ;
2009-04-13 12:51:25 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : showDefinitionInNewTab , this , & MainWindow : : showDefinitionInNewTab ) ;
2009-01-28 20:55:45 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : typingEvent , this , & MainWindow : : typingEvent ) ;
2009-05-12 13:25:18 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : activeArticleChanged , this , & MainWindow : : activeArticleChanged ) ;
2011-07-03 12:27:08 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : statusBarMessage , this , & MainWindow : : showStatusBarMessage ) ;
2011-07-02 13:04:49 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : showDictsPane , this , & MainWindow : : showDictsPane ) ;
2011-07-31 00:11:07 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : forceAddWordToHistory , this , & MainWindow : : forceAddWordToHistory ) ;
2012-09-12 14:11:30 +00:00
2022-12-26 02:08:17 +00:00
connect ( this , & MainWindow : : setExpandOptionalParts , view , & ArticleView : : receiveExpandOptionalParts ) ;
2012-09-16 10:19:47 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : setExpandMode , this , & MainWindow : : setExpandMode ) ;
2012-09-16 10:19:47 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : sendWordToHistory , this , & MainWindow : : addWordToHistory ) ;
2012-11-12 13:52:54 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : sendWordToInputLine , this , & MainWindow : : sendWordToInputLine ) ;
2013-02-22 12:44:23 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : storeResourceSavePath , this , & MainWindow : : storeResourceSavePath ) ;
2012-11-26 13:13:13 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : zoomIn , this , & MainWindow : : zoomin ) ;
2014-02-04 13:35:42 +00:00
2022-12-26 02:08:17 +00:00
connect ( view , & ArticleView : : zoomOut , this , & MainWindow : : zoomout ) ;
2022-06-01 15:11:41 +00:00
connect ( view , & ArticleView : : saveBookmarkSignal , this , & MainWindow : : addBookmarkToFavorite ) ;
2014-02-04 13:35:42 +00:00
2012-09-26 14:12:18 +00:00
view - > setSelectionBySingleClick ( cfg . preferences . selectWordBySingleClick ) ;
2009-04-13 12:51:25 +00:00
int index = cfg . preferences . newTabsOpenAfterCurrentOne ?
ui . tabWidget - > currentIndex ( ) + 1 : ui . tabWidget - > count ( ) ;
QString escaped = name ;
escaped . replace ( " & " , " && " ) ;
ui . tabWidget - > insertTab ( index , view , escaped ) ;
2011-11-02 23:37:50 +00:00
mruList . append ( dynamic_cast < QWidget * > ( view ) ) ;
2009-04-13 12:51:25 +00:00
if ( switchToIt )
ui . tabWidget - > setCurrentIndex ( index ) ;
2009-04-30 19:57:25 +00:00
view - > setZoomFactor ( cfg . preferences . zoomFactor ) ;
2012-09-18 23:01:31 +00:00
# ifdef Q_OS_WIN32
view - > installEventFilter ( this ) ;
# endif
2009-04-13 12:51:25 +00:00
return view ;
2009-01-28 20:55:45 +00:00
}
2022-08-19 11:56:49 +00:00
void MainWindow : : inspectElement ( QWebEnginePage * page )
{
2022-10-13 12:31:37 +00:00
inspector - > triggerAction ( page ) ;
2022-08-19 11:56:49 +00:00
}
2009-01-28 20:55:45 +00:00
void MainWindow : : tabCloseRequested ( int x )
{
QWidget * w = ui . tabWidget - > widget ( x ) ;
2011-11-02 23:37:50 +00:00
mruList . removeOne ( w ) ;
2013-06-07 13:58:27 +00:00
// In MRU case: First, we switch to the appropriate tab
// and only then remove the old one.
2010-09-16 18:52:40 +00:00
2011-11-02 23:37:50 +00:00
//activate a tab in accordance with MRU
2013-06-07 13:58:27 +00:00
if ( cfg . preferences . mruTabOrder & & mruList . size ( ) > 0 ) {
2012-03-27 01:38:41 +00:00
ui . tabWidget - > setCurrentWidget ( mruList . at ( 0 ) ) ;
}
2015-11-21 11:10:15 +00:00
else if ( ui . tabWidget - > count ( ) > 1 )
{
//activate neighboring tab
int n = x > = ui . tabWidget - > count ( ) - 1 ? x - 1 : x + 1 ;
if ( n > = 0 )
ui . tabWidget - > setCurrentIndex ( n ) ;
}
2011-11-02 23:37:50 +00:00
2013-06-07 13:58:27 +00:00
ui . tabWidget - > removeTab ( x ) ;
delete w ;
// if everything is closed, add a new tab
2010-09-16 18:52:40 +00:00
if ( ui . tabWidget - > count ( ) = = 0 )
addNewTab ( ) ;
2009-01-28 20:55:45 +00:00
}
2009-04-09 22:11:38 +00:00
void MainWindow : : closeCurrentTab ( )
{
tabCloseRequested ( ui . tabWidget - > currentIndex ( ) ) ;
}
2010-09-16 18:52:40 +00:00
void MainWindow : : closeAllTabs ( )
{
while ( ui . tabWidget - > count ( ) > 1 )
closeCurrentTab ( ) ;
// close last tab
closeCurrentTab ( ) ;
}
void MainWindow : : closeRestTabs ( )
{
if ( ui . tabWidget - > count ( ) < 2 )
return ;
int idx = ui . tabWidget - > currentIndex ( ) ;
for ( int i = 0 ; i < idx ; i + + )
tabCloseRequested ( 0 ) ;
ui . tabWidget - > setCurrentIndex ( 0 ) ;
while ( ui . tabWidget - > count ( ) > 1 )
tabCloseRequested ( 1 ) ;
}
2009-04-09 22:11:38 +00:00
void MainWindow : : switchToNextTab ( )
{
if ( ui . tabWidget - > count ( ) < 2 )
return ;
ui . tabWidget - > setCurrentIndex ( ( ui . tabWidget - > currentIndex ( ) + 1 ) % ui . tabWidget - > count ( ) ) ;
}
void MainWindow : : switchToPrevTab ( )
{
if ( ui . tabWidget - > count ( ) < 2 )
return ;
if ( ! ui . tabWidget - > currentIndex ( ) )
ui . tabWidget - > setCurrentIndex ( ui . tabWidget - > count ( ) - 1 ) ;
else
ui . tabWidget - > setCurrentIndex ( ui . tabWidget - > currentIndex ( ) - 1 ) ;
}
2011-11-02 23:37:50 +00:00
//emitted by tabListMenu when user releases Ctrl
void MainWindow : : ctrlReleased ( )
{
if ( tabListMenu - > actions ( ) . size ( ) > 1 )
{
2019-11-16 13:01:13 +00:00
QAction * act = tabListMenu - > activeAction ( ) ;
if ( act = = 0 )
act = tabListMenu - > actions ( ) . at ( 1 ) ;
ui . tabWidget - > setCurrentIndex ( act - > data ( ) . toInt ( ) ) ;
2011-11-02 23:37:50 +00:00
}
tabListMenu - > hide ( ) ;
}
2009-01-28 20:55:45 +00:00
void MainWindow : : backClicked ( )
{
2014-05-10 21:02:31 +00:00
GD_DPRINTF ( " Back \n " ) ;
2009-01-28 20:55:45 +00:00
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-01-28 20:55:45 +00:00
2011-07-01 19:22:40 +00:00
view - > back ( ) ;
2009-01-28 20:55:45 +00:00
}
void MainWindow : : forwardClicked ( )
{
2014-05-10 21:02:31 +00:00
GD_DPRINTF ( " Forward \n " ) ;
2009-01-28 20:55:45 +00:00
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-01-28 20:55:45 +00:00
2011-07-01 19:22:40 +00:00
view - > forward ( ) ;
2009-01-28 20:55:45 +00:00
}
void MainWindow : : titleChanged ( ArticleView * view , QString const & title )
{
2022-01-18 14:26:52 +00:00
//the title can be url if html title is empty.according to qwebenginepage title() document.
QString escaped ;
if ( title ! = nullptr & & title . contains ( " : " ) ) {
//check if the title is url.
QUrl url ( title ) ;
escaped = Utils : : Url : : queryItemValue ( url , " word " ) ;
} else {
escaped = title ;
}
2009-04-10 21:37:16 +00:00
escaped . replace ( " & " , " && " ) ;
2013-07-06 21:45:13 +00:00
if ( escaped . isRightToLeft ( ) )
{
escaped . insert ( 0 , ( ushort ) 0x202E ) ; // RLE, Right-to-Left Embedding
escaped . append ( ( ushort ) 0x202C ) ; // PDF, POP DIRECTIONAL FORMATTING
}
2022-07-29 15:48:04 +00:00
int index = ui . tabWidget - > indexOf ( view ) ;
2022-08-21 06:10:42 +00:00
if ( ! escaped . isEmpty ( ) )
ui . tabWidget - > setTabText ( index , escaped ) ;
2017-11-07 14:46:59 +00:00
2022-07-29 15:48:04 +00:00
if ( index = = ui . tabWidget - > currentIndex ( ) )
2017-12-07 15:47:53 +00:00
{
2022-07-29 15:48:04 +00:00
// Set icon for "Add to Favorites" action
if ( isWordPresentedInFavorites ( title , cfg . lastMainGroupId ) )
{
addToFavorites - > setIcon ( blueStarIcon ) ;
addToFavorites - > setToolTip ( tr ( " Remove current tab from Favorites " ) ) ;
}
else
{
addToFavorites - > setIcon ( starIcon ) ;
addToFavorites - > setToolTip ( tr ( " Add current tab to Favorites " ) ) ;
}
2017-11-07 14:46:59 +00:00
2022-07-29 15:48:04 +00:00
updateWindowTitle ( ) ;
}
2009-01-28 20:55:45 +00:00
}
void MainWindow : : iconChanged ( ArticleView * view , QIcon const & icon )
{
2009-05-18 18:01:50 +00:00
ui . tabWidget - > setTabIcon ( ui . tabWidget - > indexOf ( view ) , groupInstances . size ( ) > 1 ? icon : QIcon ( ) ) ;
2009-01-28 20:55:45 +00:00
}
2011-06-25 07:53:45 +00:00
void MainWindow : : updateWindowTitle ( )
{
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
if ( view )
{
2013-01-31 13:01:08 +00:00
QString str = view - > getTitle ( ) ;
if ( ! str . isEmpty ( ) )
{
2013-07-06 21:45:13 +00:00
if ( str . isRightToLeft ( ) )
{
str . insert ( 0 , ( ushort ) 0x202E ) ; // RLE, Right-to-Left Embedding
str . append ( ( ushort ) 0x202C ) ; // PDF, POP DIRECTIONAL FORMATTING
}
2013-01-31 13:01:08 +00:00
if ( ! blockUpdateWindowTitle )
2013-02-01 12:36:01 +00:00
setWindowTitle ( tr ( " %1 - %2 " ) . arg ( str , " GoldenDict " ) ) ;
2013-01-31 13:01:08 +00:00
blockUpdateWindowTitle = false ;
}
2011-07-01 19:22:40 +00:00
}
2011-06-25 07:53:45 +00:00
}
2009-05-14 19:27:19 +00:00
void MainWindow : : pageLoaded ( ArticleView * view )
2009-04-10 21:07:03 +00:00
{
2014-02-09 15:00:48 +00:00
if ( view ! = getCurrentArticleView ( ) )
return ; // It was background action
2011-06-07 11:27:19 +00:00
updateBackForwardButtons ( ) ;
2009-04-10 21:07:03 +00:00
updatePronounceAvailability ( ) ;
if ( cfg . preferences . pronounceOnLoadMain )
2009-05-14 19:27:19 +00:00
pronounce ( view ) ;
2011-06-05 11:49:50 +00:00
2022-01-09 04:54:50 +00:00
//updateFoundInDictsList();
2009-04-10 21:07:03 +00:00
}
2011-07-14 20:11:57 +00:00
void MainWindow : : showStatusBarMessage ( QString const & message , int timeout , QPixmap const & icon )
2011-07-02 13:04:49 +00:00
{
2015-11-01 12:22:18 +00:00
if ( message . isEmpty ( ) )
mainStatusBar - > clearMessage ( ) ;
else
mainStatusBar - > showMessage ( message , timeout , icon ) ;
2011-07-02 13:04:49 +00:00
}
2009-04-10 21:07:03 +00:00
void MainWindow : : tabSwitched ( int )
{
2013-01-27 22:12:00 +00:00
translateBox - > setPopupEnabled ( false ) ;
2011-06-07 11:27:19 +00:00
updateBackForwardButtons ( ) ;
2009-04-10 21:07:03 +00:00
updatePronounceAvailability ( ) ;
2011-06-05 11:49:50 +00:00
updateFoundInDictsList ( ) ;
2011-06-25 07:53:45 +00:00
updateWindowTitle ( ) ;
2011-11-02 23:37:50 +00:00
if ( mruList . size ( ) > 1 )
{
2013-05-29 06:17:12 +00:00
int from = mruList . indexOf ( ui . tabWidget - > widget ( ui . tabWidget - > currentIndex ( ) ) ) ;
if ( from > 0 )
mruList . move ( from , 0 ) ;
2011-11-02 23:37:50 +00:00
}
2017-11-07 14:46:59 +00:00
// Set icon for "Add to Favorites" action
QString headword = ui . tabWidget - > tabText ( ui . tabWidget - > currentIndex ( ) ) ;
2017-12-07 15:47:53 +00:00
if ( isWordPresentedInFavorites ( unescapeTabHeader ( headword ) , cfg . lastMainGroupId ) )
{
addToFavorites - > setIcon ( blueStarIcon ) ;
addToFavorites - > setToolTip ( tr ( " Remove current tab from Favorites " ) ) ;
}
else
{
addToFavorites - > setIcon ( starIcon ) ;
addToFavorites - > setToolTip ( tr ( " Add current tab to Favorites " ) ) ;
}
2009-04-10 21:07:03 +00:00
}
2010-09-16 18:52:40 +00:00
void MainWindow : : tabMenuRequested ( QPoint pos )
{
2017-09-16 13:14:26 +00:00
// // do not show this menu for single tab
2010-09-16 18:52:40 +00:00
// if ( ui.tabWidget->count() < 2 )
// return;
tabMenu - > popup ( ui . tabWidget - > mapToGlobal ( pos ) ) ;
}
2009-09-23 18:44:38 +00:00
void MainWindow : : dictionaryBarToggled ( bool )
{
2009-09-24 12:20:20 +00:00
// From now on, only the triggered() signal is interesting to us
2022-12-26 02:08:17 +00:00
disconnect ( dictionaryBar . toggleViewAction ( ) , & QAction : : toggled , this , & MainWindow : : dictionaryBarToggled ) ;
2009-09-24 12:20:20 +00:00
2009-09-23 18:44:38 +00:00
updateDictionaryBar ( ) ; // Updates dictionary bar contents if it's shown
applyMutedDictionariesState ( ) ; // Visibility change affects searches and results
}
2009-05-14 19:27:19 +00:00
void MainWindow : : pronounce ( ArticleView * view )
2009-04-10 21:07:03 +00:00
{
2009-05-14 19:27:19 +00:00
if ( view )
view - > playSound ( ) ;
else
2011-07-01 19:22:40 +00:00
getCurrentArticleView ( ) - > playSound ( ) ;
2009-04-10 21:07:03 +00:00
}
2011-07-31 00:11:07 +00:00
void MainWindow : : showDictsPane ( )
{
if ( ! ui . dictsPane - > isVisible ( ) )
ui . dictsPane - > show ( ) ;
}
2011-06-05 11:49:50 +00:00
void MainWindow : : dictsPaneVisibilityChanged ( bool visible )
{
if ( visible ) {
updateFoundInDictsList ( ) ;
}
}
void MainWindow : : updateFoundInDictsList ( )
{
if ( ! ui . dictsList - > isVisible ( ) )
{
// nothing to do, the list is not visible
return ;
}
ui . dictsList - > clear ( ) ;
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2011-06-05 11:49:50 +00:00
2011-07-01 19:22:40 +00:00
if ( view )
{
QStringList ids = view - > getArticlesList ( ) ;
2011-07-03 12:27:08 +00:00
QString activeId = view - > getActiveArticleId ( ) ;
2011-06-05 11:49:50 +00:00
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 ( ) )
{
QString dictName = QString : : fromUtf8 ( dictionaries [ x ] - > getName ( ) . c_str ( ) ) ;
2011-07-03 12:27:08 +00:00
QString dictId = QString : : fromUtf8 ( dictionaries [ x ] - > getId ( ) . c_str ( ) ) ;
2011-06-05 11:49:50 +00:00
QListWidgetItem * item =
new QListWidgetItem (
2022-03-15 14:26:24 +00:00
dictionaries [ x ] - > getIcon ( ) ,
2011-06-05 11:49:50 +00:00
dictName ,
ui . dictsList , QListWidgetItem : : Type ) ;
2011-07-03 12:27:08 +00:00
item - > setData ( Qt : : UserRole , QVariant ( dictId ) ) ;
2011-06-05 11:49:50 +00:00
item - > setToolTip ( dictName ) ;
ui . dictsList - > addItem ( item ) ;
2011-07-03 12:27:08 +00:00
if ( dictId = = activeId )
{
ui . dictsList - > setCurrentItem ( item ) ;
}
2011-06-05 11:49:50 +00:00
break ;
}
}
}
2022-01-17 13:50:54 +00:00
//if no item in dict List panel has been choose ,select first one.
2022-01-19 12:16:45 +00:00
if ( ui . dictsList - > count ( ) > 0 & & ui . dictsList - > selectedItems ( ) . empty ( ) ) {
2022-01-17 13:50:54 +00:00
ui . dictsList - > setCurrentRow ( 0 ) ;
}
2022-01-19 12:16:45 +00:00
2011-06-05 11:49:50 +00:00
}
}
2011-06-07 11:27:19 +00:00
void MainWindow : : updateBackForwardButtons ( )
{
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
if ( view )
2011-06-07 11:27:19 +00:00
{
2011-07-01 19:22:40 +00:00
navBack - > setEnabled ( view - > canGoBack ( ) ) ;
navForward - > setEnabled ( view - > canGoForward ( ) ) ;
2011-06-07 11:27:19 +00:00
}
}
2009-04-10 21:07:03 +00:00
void MainWindow : : updatePronounceAvailability ( )
{
2022-03-27 14:22:42 +00:00
if ( ui . tabWidget - > count ( ) > 0 ) {
getCurrentArticleView ( ) - > hasSound ( [ this ] ( bool has ) {
navPronounce - > setEnabled ( has ) ;
} ) ;
}
else {
navPronounce - > setEnabled ( false ) ;
}
2009-04-10 21:07:03 +00:00
}
2010-05-08 14:01:59 +00:00
void MainWindow : : editDictionaries ( unsigned editDictionaryGroup )
2009-01-28 20:55:45 +00:00
{
2009-04-30 15:29:03 +00:00
hotkeyWrapper . reset ( ) ; // No hotkeys while we're editing dictionaries
scanPopup . reset ( ) ; // No scan popup either. No one should use dictionaries.
2014-04-10 14:00:12 +00:00
closeHeadwordsDialog ( ) ;
2014-04-16 16:18:28 +00:00
closeFullTextSearchDialog ( ) ;
2009-04-30 19:57:25 +00:00
2009-09-23 18:44:38 +00:00
wordFinder . clear ( ) ;
dictionariesUnmuted . clear ( ) ;
2014-06-25 14:01:11 +00:00
hideGDHelp ( ) ;
2012-12-22 17:46:53 +00:00
{ // Limit existence of newCfg
2012-09-26 13:13:47 +00:00
Config : : Class newCfg = cfg ;
2022-07-08 13:47:09 +00:00
EditDictionaries dicts ( this , newCfg , dictionaries , groupInstances , dictNetMgr ) ;
2009-01-28 20:55:45 +00:00
2022-12-26 02:08:17 +00:00
connect ( & dicts , & EditDictionaries : : showDictionaryInfo , this , & MainWindow : : showDictionaryInfo ) ;
2013-07-07 08:09:46 +00:00
2022-12-26 02:08:17 +00:00
connect ( & dicts ,
SIGNAL ( showDictionaryHeadwords ( QString const & ) ) ,
this ,
SLOT ( showDictionaryHeadwords ( QString const & ) ) ) ;
2014-03-01 13:11:14 +00:00
2010-05-08 14:01:59 +00:00
if ( editDictionaryGroup ! = Instances : : Group : : NoGroupId )
dicts . editGroup ( editDictionaryGroup ) ;
2021-06-22 15:30:13 +00:00
dicts . restoreGeometry ( cfg . dictionariesDialogGeometry ) ;
2009-04-30 15:29:03 +00:00
dicts . exec ( ) ;
2021-06-22 15:30:13 +00:00
cfg . dictionariesDialogGeometry = newCfg . dictionariesDialogGeometry = dicts . saveGeometry ( ) ;
2009-04-30 19:57:25 +00:00
2009-04-30 15:29:03 +00:00
if ( dicts . areDictionariesChanged ( ) | | dicts . areGroupsChanged ( ) )
2009-03-26 19:00:08 +00:00
{
2022-03-03 15:59:42 +00:00
ftsIndexing . stopIndexing ( ) ;
ftsIndexing . clearDictionaries ( ) ;
2012-09-26 13:13:47 +00:00
// Set muted dictionaries from old groups
2012-12-10 12:49:45 +00:00
for ( int x = 0 ; x < newCfg . groups . size ( ) ; x + + )
2012-09-26 13:13:47 +00:00
{
unsigned id = newCfg . groups [ x ] . id ;
if ( id ! = Instances : : Group : : NoGroupId )
{
Config : : Group const * grp = cfg . getGroup ( id ) ;
if ( grp )
{
newCfg . groups [ x ] . mutedDictionaries = grp - > mutedDictionaries ;
newCfg . groups [ x ] . popupMutedDictionaries = grp - > popupMutedDictionaries ;
}
}
}
cfg = newCfg ;
2009-04-30 15:29:03 +00:00
updateGroupList ( ) ;
2009-04-30 19:57:25 +00:00
2009-03-26 19:00:08 +00:00
Config : : save ( cfg ) ;
2013-09-24 13:55:12 +00:00
2021-06-10 16:13:11 +00:00
updateSuggestionList ( ) ;
2022-03-03 15:59:42 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + )
{
dictionaries [ x ] - > setFTSParameters ( cfg . preferences . fts ) ;
dictionaries [ x ] - > setSynonymSearchEnabled ( cfg . preferences . synonymSearchEnabled ) ;
}
ftsIndexing . setDictionaries ( dictionaries ) ;
ftsIndexing . doIndexing ( ) ;
2009-01-28 20:55:45 +00:00
}
2009-04-30 19:57:25 +00:00
2012-12-22 17:46:53 +00:00
}
2009-02-01 00:08:08 +00:00
makeScanPopup ( ) ;
2009-04-30 15:29:03 +00:00
installHotKeys ( ) ;
2014-04-17 14:18:15 +00:00
2014-12-06 16:34:59 +00:00
2009-01-28 20:55:45 +00:00
}
2010-05-08 14:01:59 +00:00
void MainWindow : : editCurrentGroup ( )
{
2012-12-24 11:30:03 +00:00
editDictionaries ( groupList - > getCurrentGroup ( ) ) ;
2010-05-08 14:01:59 +00:00
}
2009-02-05 20:55:00 +00:00
void MainWindow : : editPreferences ( )
{
2009-04-21 18:27:26 +00:00
hotkeyWrapper . reset ( ) ; // So we could use the keys it hooks
2013-05-17 12:34:28 +00:00
scanPopup . reset ( ) ; // No scan popup either. No one should use dictionaries.
2014-04-10 14:00:12 +00:00
closeHeadwordsDialog ( ) ;
2014-04-16 16:18:28 +00:00
closeFullTextSearchDialog ( ) ;
2009-04-21 18:27:26 +00:00
2014-04-17 14:18:15 +00:00
ftsIndexing . stopIndexing ( ) ;
ftsIndexing . clearDictionaries ( ) ;
2009-04-21 18:27:26 +00:00
2014-06-24 13:55:06 +00:00
Preferences preferences ( this , cfg ) ;
2009-02-05 20:55:00 +00:00
2014-06-25 14:01:11 +00:00
hideGDHelp ( ) ;
2009-02-05 20:55:00 +00:00
preferences . show ( ) ;
if ( preferences . exec ( ) = = QDialog : : Accepted )
{
2009-05-11 11:03:36 +00:00
Config : : Preferences p = preferences . getPreferences ( ) ;
2012-11-12 14:34:47 +00:00
// These parameters are not set in dialog
p . zoomFactor = cfg . preferences . zoomFactor ;
2014-06-23 15:11:15 +00:00
p . helpZoomFactor = cfg . preferences . helpZoomFactor ;
2012-11-12 14:34:47 +00:00
p . wordsZoomLevel = cfg . preferences . wordsZoomLevel ;
p . hideMenubar = cfg . preferences . hideMenubar ;
2013-01-09 12:17:15 +00:00
p . searchInDock = cfg . preferences . searchInDock ;
p . alwaysOnTop = cfg . preferences . alwaysOnTop ;
2022-11-19 15:30:31 +00:00
2014-04-03 14:21:02 +00:00
p . proxyServer . systemProxyUser = cfg . preferences . proxyServer . systemProxyUser ;
p . proxyServer . systemProxyPassword = cfg . preferences . proxyServer . systemProxyPassword ;
2012-11-12 14:34:47 +00:00
2014-04-17 14:31:51 +00:00
p . fts . dialogGeometry = cfg . preferences . fts . dialogGeometry ;
p . fts . matchCase = cfg . preferences . fts . matchCase ;
p . fts . maxArticlesPerDictionary = cfg . preferences . fts . maxArticlesPerDictionary ;
p . fts . maxDistanceBetweenWords = cfg . preferences . fts . maxDistanceBetweenWords ;
p . fts . searchMode = cfg . preferences . fts . searchMode ;
p . fts . useMaxArticlesPerDictionary = cfg . preferences . fts . useMaxArticlesPerDictionary ;
p . fts . useMaxDistanceBetweenWords = cfg . preferences . fts . useMaxDistanceBetweenWords ;
2022-11-16 06:11:38 +00:00
p . fts . ignoreWordsOrder = cfg . preferences . fts . ignoreWordsOrder ;
p . fts . ignoreDiacritics = cfg . preferences . fts . ignoreDiacritics ;
2014-04-17 14:31:51 +00:00
2012-09-16 10:19:47 +00:00
bool needReload = false ;
2009-05-11 11:03:36 +00:00
// See if we need to reapply stylesheets
2022-12-11 01:22:54 +00:00
if ( cfg . preferences . displayStyle ! = p . displayStyle | | cfg . preferences . addonStyle ! = p . addonStyle | | cfg . preferences . darkMode ! = p . darkMode )
2009-05-11 11:03:36 +00:00
{
2022-12-11 01:22:54 +00:00
applyQtStyleSheet ( p . displayStyle , p . addonStyle , p . darkMode ) ;
2012-12-10 14:14:13 +00:00
articleMaker . setDisplayStyle ( p . displayStyle , p . addonStyle ) ;
2012-09-16 10:19:47 +00:00
needReload = true ;
}
2009-05-11 11:03:36 +00:00
2013-06-02 11:20:33 +00:00
if ( cfg . preferences . collapseBigArticles ! = p . collapseBigArticles
| | cfg . preferences . articleSizeLimit ! = p . articleSizeLimit )
{
articleMaker . setCollapseParameters ( p . collapseBigArticles , p . articleSizeLimit ) ;
}
2012-09-16 10:19:47 +00:00
// See if we need to reapply expand optional parts mode
if ( cfg . preferences . alwaysExpandOptionalParts ! = p . alwaysExpandOptionalParts )
{
emit setExpandOptionalParts ( p . alwaysExpandOptionalParts ) ;
// Signal setExpandOptionalParts reload all articles
needReload = false ;
}
2014-06-25 14:01:11 +00:00
// See if we need to change help language
if ( cfg . preferences . helpLanguage ! = p . helpLanguage )
closeGDHelp ( ) ;
2012-09-26 13:59:48 +00:00
for ( int x = 0 ; x < ui . tabWidget - > count ( ) ; + + x )
2012-09-16 10:19:47 +00:00
{
2012-09-26 13:59:48 +00:00
ArticleView & view =
dynamic_cast < ArticleView & > ( * ( ui . tabWidget - > widget ( x ) ) ) ;
view . setSelectionBySingleClick ( p . selectWordBySingleClick ) ;
2009-05-11 11:03:36 +00:00
2012-09-26 13:59:48 +00:00
if ( needReload )
2009-05-11 11:03:36 +00:00
view . reload ( ) ;
}
2013-02-05 12:51:23 +00:00
if ( cfg . preferences . historyStoreInterval ! = p . historyStoreInterval )
history . setSaveInterval ( p . historyStoreInterval ) ;
2017-05-13 10:18:25 +00:00
if ( cfg . preferences . favoritesStoreInterval ! = p . favoritesStoreInterval )
ui . favoritesPaneWidget - > setSaveInterval ( p . favoritesStoreInterval ) ;
2020-11-12 15:57:10 +00:00
if ( cfg . preferences . maxNetworkCacheSize ! = p . maxNetworkCacheSize )
setupNetworkCache ( p . maxNetworkCacheSize ) ;
2009-05-11 11:03:36 +00:00
cfg . preferences = p ;
2009-04-18 18:47:01 +00:00
2018-03-21 17:49:34 +00:00
audioPlayerFactory . setPreferences ( cfg . preferences ) ;
2009-02-05 20:55:00 +00:00
updateTrayIcon ( ) ;
2009-04-03 17:10:27 +00:00
applyProxySettings ( ) ;
2009-04-18 18:47:01 +00:00
2011-06-23 14:17:09 +00:00
ui . tabWidget - > setHideSingleTab ( cfg . preferences . hideSingleTab ) ;
2009-04-20 19:54:34 +00:00
setAutostart ( cfg . preferences . autoStart ) ;
prepareNewReleaseChecks ( ) ;
2009-04-18 18:47:01 +00:00
2012-09-11 13:03:10 +00:00
history . enableAdd ( cfg . preferences . storeHistory ) ;
2013-01-17 09:08:53 +00:00
history . setMaxSize ( cfg . preferences . maxStringsInHistory ) ;
ui . historyPaneWidget - > updateHistoryCounts ( ) ;
2012-09-11 13:03:10 +00:00
2014-04-17 14:31:51 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + )
2017-03-09 16:11:17 +00:00
{
2014-04-17 14:31:51 +00:00
dictionaries [ x ] - > setFTSParameters ( cfg . preferences . fts ) ;
2017-03-09 16:11:17 +00:00
dictionaries [ x ] - > setSynonymSearchEnabled ( cfg . preferences . synonymSearchEnabled ) ;
}
2014-04-17 14:31:51 +00:00
ui . fullTextSearchAction - > setEnabled ( cfg . preferences . fts . enabled ) ;
2009-02-05 20:55:00 +00:00
Config : : save ( cfg ) ;
}
2009-04-21 18:27:26 +00:00
2013-05-17 12:34:28 +00:00
makeScanPopup ( ) ;
2009-04-21 18:27:26 +00:00
installHotKeys ( ) ;
2014-04-17 14:18:15 +00:00
ftsIndexing . setDictionaries ( dictionaries ) ;
ftsIndexing . doIndexing ( ) ;
2009-02-05 20:55:00 +00:00
}
2022-02-27 06:26:49 +00:00
void MainWindow : : currentGroupChanged ( int )
2009-02-06 17:04:11 +00:00
{
2012-12-24 11:30:03 +00:00
cfg . lastMainGroupId = groupList - > getCurrentGroup ( ) ;
2012-09-26 13:13:47 +00:00
Instances : : Group const * igrp = groupInstances . findGroup ( cfg . lastMainGroupId ) ;
if ( cfg . lastMainGroupId = = Instances : : Group : : AllGroupId )
{
if ( igrp )
igrp - > checkMutedDictionaries ( & cfg . mutedDictionaries ) ;
dictionaryBar . setMutedDictionaries ( & cfg . mutedDictionaries ) ;
}
else
{
Config : : Group * grp = cfg . getGroup ( cfg . lastMainGroupId ) ;
if ( grp )
{
if ( igrp )
igrp - > checkMutedDictionaries ( & grp - > mutedDictionaries ) ;
dictionaryBar . setMutedDictionaries ( & grp - > mutedDictionaries ) ;
}
else
dictionaryBar . setMutedDictionaries ( 0 ) ;
}
2009-02-06 17:04:11 +00:00
2022-11-24 12:34:21 +00:00
if ( igrp ) {
GlobalBroadcaster : : instance ( ) - > currentGroupId = cfg . lastMainGroupId ;
}
2009-09-21 17:50:03 +00:00
updateDictionaryBar ( ) ;
2009-02-06 17:04:11 +00:00
// Update word search results
2013-01-27 22:12:00 +00:00
translateBox - > setPopupEnabled ( false ) ;
2021-06-10 16:13:11 +00:00
updateSuggestionList ( ) ;
2013-01-17 09:08:53 +00:00
translateInputFinished ( false ) ;
2010-11-15 15:22:35 +00:00
updateCurrentGroupProperty ( ) ;
2014-04-16 16:18:28 +00:00
if ( ftsDlg )
ftsDlg - > setCurrentGroup ( cfg . lastMainGroupId ) ;
2010-11-15 15:22:35 +00:00
}
void MainWindow : : updateCurrentGroupProperty ( )
{
// We maintain currentGroup property so styles could use that to change
// fonts based on group names
Instances : : Group * grp =
2012-12-24 11:30:03 +00:00
groupInstances . findGroup ( groupList - > getCurrentGroup ( ) ) ;
2010-11-15 15:22:35 +00:00
2012-12-24 11:30:03 +00:00
if ( grp & & translateLine - > property ( " currentGroup " ) . toString ( ) ! =
2010-11-15 15:22:35 +00:00
grp - > name )
{
2012-12-24 11:30:03 +00:00
translateLine - > setProperty ( " currentGroup " , grp - > name ) ;
wordList - > setProperty ( " currentGroup " , grp - > name ) ;
2010-11-15 15:22:35 +00:00
QString ss = styleSheet ( ) ;
// Only update stylesheet if it mentions currentGroup, as updating the
// stylesheet is a slow operation
if ( ss . contains ( " currentGroup " ) )
setStyleSheet ( ss ) ;
}
2009-02-06 17:04:11 +00:00
}
2009-01-29 19:16:25 +00:00
void MainWindow : : translateInputChanged ( QString const & newValue )
2021-06-10 16:13:11 +00:00
{
updateSuggestionList ( newValue ) ;
translateBoxSuffix = QString ( ) ;
}
void MainWindow : : updateSuggestionList ( )
{
updateSuggestionList ( translateLine - > text ( ) ) ;
}
void MainWindow : : updateSuggestionList ( QString const & newValue )
2009-01-28 20:55:45 +00:00
{
2009-03-26 19:00:08 +00:00
// If there's some status bar message present, clear it since it may be
// about the previous search that has failed.
2011-06-27 18:54:15 +00:00
if ( ! mainStatusBar - > currentMessage ( ) . isEmpty ( ) )
{
mainStatusBar - > clearMessage ( ) ;
}
2009-03-26 19:00:08 +00:00
2009-10-21 20:23:03 +00:00
// If some word is selected in the word list, unselect it. This prevents
// triggering a set of spurious activation signals when the list changes.
2012-12-24 11:30:03 +00:00
if ( wordList - > selectionModel ( ) - > hasSelection ( ) )
wordList - > setCurrentItem ( 0 , QItemSelectionModel : : Clear ) ;
2009-10-21 20:23:03 +00:00
2009-01-29 19:16:25 +00:00
QString req = newValue . trimmed ( ) ;
2009-01-28 20:55:45 +00:00
2009-01-29 19:16:25 +00:00
if ( ! req . size ( ) )
{
// An empty request always results in an empty result
2009-03-26 19:00:08 +00:00
wordFinder . cancel ( ) ;
2012-12-24 11:30:03 +00:00
wordList - > clear ( ) ;
wordList - > unsetCursor ( ) ;
2009-01-28 20:55:45 +00:00
2009-03-26 19:00:08 +00:00
// Reset the noResults mark if it's on right now
2012-12-24 11:30:03 +00:00
if ( translateLine - > property ( " noResults " ) . toBool ( ) )
2009-03-26 19:00:08 +00:00
{
2012-12-24 11:30:03 +00:00
translateLine - > setProperty ( " noResults " , false ) ;
2009-05-16 18:04:21 +00:00
setStyleSheet ( styleSheet ( ) ) ;
2009-03-26 19:00:08 +00:00
}
2009-01-28 20:55:45 +00:00
return ;
2009-01-29 19:16:25 +00:00
}
2009-01-28 20:55:45 +00:00
2012-12-24 11:30:03 +00:00
wordList - > setCursor ( Qt : : WaitCursor ) ;
2009-01-28 20:55:45 +00:00
2009-03-26 19:00:08 +00:00
wordFinder . prefixMatch ( req , getActiveDicts ( ) ) ;
2009-01-29 19:16:25 +00:00
}
2009-01-28 20:55:45 +00:00
2021-06-10 16:13:11 +00:00
void MainWindow : : translateInputFinished ( bool checkModifiers )
2009-02-08 14:02:27 +00:00
{
2014-04-26 11:46:26 +00:00
QString word = Folding : : unescapeWildcardSymbols ( translateLine - > text ( ) ) ;
2021-06-18 18:33:24 +00:00
respondToTranslationRequest ( Config : : InputPhrase ( word , translateBoxSuffix ) , checkModifiers ) ;
2021-06-10 16:13:11 +00:00
}
2009-03-29 17:38:54 +00:00
2021-06-10 16:13:11 +00:00
void MainWindow : : respondToTranslationRequest ( Config : : InputPhrase const & phrase ,
2021-06-29 08:59:16 +00:00
bool checkModifiers , QString const & scrollTo )
2021-06-10 16:13:11 +00:00
{
if ( phrase . isValid ( ) )
2009-05-05 20:12:20 +00:00
{
Qt : : KeyboardModifiers mods = QApplication : : keyboardModifiers ( ) ;
2012-09-03 12:42:43 +00:00
if ( checkModifiers & & ( mods & ( Qt : : ControlModifier | Qt : : ShiftModifier ) ) )
2009-05-05 20:12:20 +00:00
addNewTab ( ) ;
2021-06-29 08:59:16 +00:00
showTranslationFor ( phrase , 0 , scrollTo ) ;
2009-05-12 13:25:18 +00:00
2013-01-05 11:26:55 +00:00
if ( cfg . preferences . searchInDock )
2012-12-24 11:30:03 +00:00
{
if ( ui . searchPane - > isFloating ( ) )
activateWindow ( ) ;
}
2009-05-13 13:47:51 +00:00
2011-07-01 19:22:40 +00:00
getCurrentArticleView ( ) - > focus ( ) ;
2009-05-05 20:12:20 +00:00
}
2009-02-08 14:02:27 +00:00
}
2021-06-30 16:49:58 +00:00
void MainWindow : : setTranslateBoxTextAndKeepSuffix ( QString text , WildcardPolicy wildcardPolicy ,
TranslateBoxPopup popupAction )
2021-06-10 16:13:11 +00:00
{
2021-06-30 16:49:58 +00:00
if ( wildcardPolicy = = EscapeWildcards )
text = Folding : : escapeWildcardSymbols ( text ) ;
2021-06-18 18:33:24 +00:00
if ( popupAction = = NoPopupChange | | cfg . preferences . searchInDock )
2021-06-10 16:13:11 +00:00
translateLine - > setText ( text ) ;
else
2021-06-18 18:33:24 +00:00
translateBox - > setText ( text , popupAction = = EnablePopup ) ;
2021-06-10 16:13:11 +00:00
}
2021-06-30 16:49:58 +00:00
void MainWindow : : setTranslateBoxTextAndClearSuffix ( QString const & text , WildcardPolicy wildcardPolicy ,
TranslateBoxPopup popupAction )
2021-06-10 16:13:11 +00:00
{
2021-06-30 16:49:58 +00:00
setTranslateBoxTextAndKeepSuffix ( text , wildcardPolicy , popupAction ) ;
2021-06-10 16:13:11 +00:00
translateBoxSuffix = QString ( ) ;
}
2010-04-27 07:49:37 +00:00
void MainWindow : : handleEsc ( )
2009-04-08 22:25:46 +00:00
{
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
if ( view & & view - > closeSearch ( ) )
2009-05-16 11:14:43 +00:00
return ;
2011-05-07 13:42:49 +00:00
if ( cfg . preferences . escKeyHidesMainWindow )
2011-05-30 00:44:12 +00:00
{
2011-06-24 19:03:03 +00:00
toggleMainWindow ( ) ;
2011-05-30 00:44:12 +00:00
}
2011-05-07 13:42:49 +00:00
else
focusTranslateLine ( ) ;
2010-04-27 07:49:37 +00:00
}
void MainWindow : : focusTranslateLine ( )
{
2013-01-05 11:26:55 +00:00
if ( cfg . preferences . searchInDock )
2012-12-24 11:30:03 +00:00
{
if ( ui . searchPane - > isFloating ( ) )
ui . searchPane - > activateWindow ( ) ;
}
2013-01-17 09:08:53 +00:00
else
{
if ( ! isActiveWindow ( ) )
activateWindow ( ) ;
}
2009-05-13 13:47:51 +00:00
2017-06-11 20:00:22 +00:00
translateLine - > clearFocus ( ) ;
2012-12-24 11:30:03 +00:00
translateLine - > setFocus ( ) ;
translateLine - > selectAll ( ) ;
2009-04-08 22:25:46 +00:00
}
2009-09-23 18:44:38 +00:00
void MainWindow : : applyMutedDictionariesState ( )
{
2013-01-27 22:12:00 +00:00
translateBox - > setPopupEnabled ( false ) ;
2021-06-10 16:13:11 +00:00
updateSuggestionList ( ) ;
2009-09-23 18:44:38 +00:00
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
if ( view )
2011-06-11 04:50:19 +00:00
{
// Update active article view
2011-07-01 19:22:40 +00:00
view - > updateMutedContents ( ) ;
2011-06-11 04:50:19 +00:00
}
2009-09-23 18:44:38 +00:00
}
2011-06-07 08:26:49 +00:00
bool MainWindow : : handleBackForwardMouseButtons ( QMouseEvent * event ) {
if ( event - > button ( ) = = Qt : : XButton1 ) {
backClicked ( ) ;
return true ;
}
else
if ( event - > button ( ) = = Qt : : XButton2 ) {
forwardClicked ( ) ;
return true ;
}
else
return false ;
}
2009-03-29 17:38:54 +00:00
bool MainWindow : : eventFilter ( QObject * obj , QEvent * ev )
{
2014-04-23 13:46:48 +00:00
if ( ev - > type ( ) = = QEvent : : ShortcutOverride
| | ev - > type ( ) = = QEvent : : KeyPress )
{
2013-01-23 18:36:45 +00:00
// Handle Ctrl+H to show the History Pane.
2013-01-17 09:08:53 +00:00
QKeyEvent * ke = static_cast < QKeyEvent * > ( ev ) ;
if ( ke - > key ( ) = = Qt : : Key_H & & ke - > modifiers ( ) = = Qt : : ControlModifier )
{
2014-04-23 13:46:48 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress )
on_showHideHistory_triggered ( ) ;
2013-01-17 09:08:53 +00:00
ev - > accept ( ) ;
return true ;
}
2013-01-23 18:36:45 +00:00
2017-05-05 14:39:51 +00:00
// Handle Ctrl+I to show the Favorities Pane.
if ( ke - > key ( ) = = Qt : : Key_I & & ke - > modifiers ( ) = = Qt : : ControlModifier )
{
if ( ev - > type ( ) = = QEvent : : KeyPress )
on_showHideFavorites_triggered ( ) ;
ev - > accept ( ) ;
return true ;
}
2013-01-23 18:36:45 +00:00
// Handle F3/Shift+F3 shortcuts
if ( ke - > key ( ) = = Qt : : Key_F3 )
{
ArticleView * view = getCurrentArticleView ( ) ;
if ( view & & view - > handleF3 ( obj , ev ) )
return true ;
}
2013-01-17 09:08:53 +00:00
}
2012-12-25 09:25:54 +00:00
// when the main window is moved or resized, hide the word list suggestions
if ( obj = = this & & ( ev - > type ( ) = = QEvent : : Move | | ev - > type ( ) = = QEvent : : Resize ) )
{
2013-01-05 11:26:55 +00:00
if ( ! cfg . preferences . searchInDock )
2012-12-25 09:25:54 +00:00
{
2013-01-27 22:12:00 +00:00
translateBox - > setPopupEnabled ( false ) ;
2012-12-25 09:25:54 +00:00
return false ;
}
}
2013-01-23 20:36:53 +00:00
if ( obj = = this & & ev - > type ( ) = = QEvent : : WindowStateChange )
{
QWindowStateChangeEvent * stev = static_cast < QWindowStateChangeEvent * > ( ev ) ;
wasMaximized = ( stev - > oldState ( ) = = Qt : : WindowMaximized & & isMinimized ( ) ) ;
}
2011-06-07 08:26:49 +00:00
if ( ev - > type ( ) = = QEvent : : MouseButtonPress ) {
QMouseEvent * event = static_cast < QMouseEvent * > ( ev ) ;
2012-12-25 20:38:28 +00:00
// clicks outside of the word list should hide it.
if ( obj ! = translateBox - > wordList ( ) & & obj ! = translateBox - > wordList ( ) - > viewport ( ) ) {
2013-01-27 22:12:00 +00:00
translateBox - > setPopupEnabled ( false ) ;
2012-12-25 20:38:28 +00:00
}
2011-06-07 08:26:49 +00:00
return handleBackForwardMouseButtons ( event ) ;
}
2011-11-02 23:37:50 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress )
{
QKeyEvent * keyevent = static_cast < QKeyEvent * > ( ev ) ;
2022-07-14 15:14:28 +00:00
bool handleCtrlTab = ( obj = = translateLine
| | obj = = wordList
| | obj = = ui . historyList
| | obj = = ui . favoritesTree
| | obj = = ui . dictsList
| | obj = = groupList ) ;
2011-11-02 23:37:50 +00:00
if ( keyevent - > modifiers ( ) = = Qt : : ControlModifier & & keyevent - > key ( ) = = Qt : : Key_Tab )
{
if ( cfg . preferences . mruTabOrder )
{
2012-09-16 10:19:47 +00:00
ctrlTabPressed ( ) ;
return true ;
2011-11-02 23:37:50 +00:00
}
2022-07-14 15:14:28 +00:00
else if ( handleCtrlTab )
2022-07-13 14:31:57 +00:00
{
QApplication : : sendEvent ( ui . tabWidget , ev ) ;
return true ;
}
2011-11-02 23:37:50 +00:00
return false ;
}
2022-07-14 15:14:28 +00:00
if ( handleCtrlTab & & keyevent - > matches ( QKeySequence : : PreviousChild ) ) // Handle only Ctrl+Shist+Tab here because Ctrl+Tab was already handled before
{
QApplication : : sendEvent ( ui . tabWidget , ev ) ;
return true ;
}
2011-11-02 23:37:50 +00:00
}
2012-12-24 11:30:03 +00:00
if ( obj = = translateLine )
2009-03-29 17:38:54 +00:00
{
2011-06-09 07:29:21 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress )
2009-03-29 17:38:54 +00:00
{
QKeyEvent * keyEvent = static_cast < QKeyEvent * > ( ev ) ;
2009-04-18 18:47:01 +00:00
2013-01-05 11:26:55 +00:00
if ( cfg . preferences . searchInDock )
2009-03-29 17:38:54 +00:00
{
2012-12-24 11:30:03 +00:00
if ( keyEvent - > matches ( QKeySequence : : MoveToNextLine ) & & wordList - > count ( ) )
{
wordList - > setFocus ( Qt : : ShortcutFocusReason ) ;
2012-12-24 21:56:31 +00:00
wordList - > setCurrentRow ( 0 , QItemSelectionModel : : ClearAndSelect ) ;
2012-12-24 11:30:03 +00:00
return true ;
}
2009-03-29 17:38:54 +00:00
}
2022-07-13 14:31:57 +00:00
2009-03-29 17:38:54 +00:00
}
2012-12-24 11:30:03 +00:00
2011-06-09 07:29:21 +00:00
if ( ev - > type ( ) = = QEvent : : FocusIn ) {
2011-06-08 10:31:03 +00:00
QFocusEvent * focusEvent = static_cast < QFocusEvent * > ( ev ) ;
// select all on mouse click
if ( focusEvent - > reason ( ) = = Qt : : MouseFocusReason ) {
2022-12-26 02:08:17 +00:00
QTimer : : singleShot ( 0 , this , & MainWindow : : focusTranslateLine ) ;
2011-06-08 10:31:03 +00:00
}
return false ;
}
2012-12-24 11:30:03 +00:00
if ( ev - > type ( ) = = QEvent : : Resize ) {
QResizeEvent * resizeEvent = static_cast < QResizeEvent * > ( ev ) ;
groupList - > setFixedHeight ( resizeEvent - > size ( ) . height ( ) ) ;
return false ;
}
2009-03-29 17:38:54 +00:00
}
else
2012-12-24 11:30:03 +00:00
if ( obj = = wordList )
2009-03-29 17:38:54 +00:00
{
2021-12-16 14:46:21 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress | | ev - > type ( ) = = QEvent : : ShortcutOverride )
2009-03-29 17:38:54 +00:00
{
QKeyEvent * keyEvent = static_cast < QKeyEvent * > ( ev ) ;
if ( keyEvent - > matches ( QKeySequence : : MoveToPreviousLine ) & &
2012-12-24 11:30:03 +00:00
! wordList - > currentRow ( ) )
2009-03-29 17:38:54 +00:00
{
2012-12-24 11:30:03 +00:00
wordList - > setCurrentRow ( 0 , QItemSelectionModel : : Clear ) ;
translateLine - > setFocus ( Qt : : ShortcutFocusReason ) ;
2009-03-29 17:38:54 +00:00
return true ;
}
if ( keyEvent - > matches ( QKeySequence : : InsertParagraphSeparator ) & &
2012-12-24 11:30:03 +00:00
wordList - > selectedItems ( ) . size ( ) )
2009-03-29 17:38:54 +00:00
{
2013-01-05 11:26:55 +00:00
if ( cfg . preferences . searchInDock )
2012-12-24 11:30:03 +00:00
{
if ( ui . searchPane - > isFloating ( ) )
activateWindow ( ) ;
}
2009-05-13 13:47:51 +00:00
2011-07-01 19:22:40 +00:00
getCurrentArticleView ( ) - > focus ( ) ;
2009-03-29 17:38:54 +00:00
2013-01-05 11:26:55 +00:00
return cfg . preferences . searchInDock ;
2009-03-29 17:38:54 +00:00
}
2009-04-29 23:18:26 +00:00
2009-12-13 17:15:34 +00:00
// Handle typing events used to initiate new lookups
2011-06-06 09:54:23 +00:00
// TODO: refactor to eliminate duplication (see below)
2009-12-13 17:15:34 +00:00
if ( keyEvent - > modifiers ( ) &
2011-06-05 11:49:50 +00:00
( Qt : : ControlModifier | Qt : : AltModifier | Qt : : MetaModifier ) )
return false ; // A non-typing modifier is pressed
2022-01-14 14:58:19 +00:00
if ( Utils : : ignoreKeyEvent ( keyEvent ) )
2011-06-05 11:49:50 +00:00
return false ; // Those key have other uses than to start typing
// or don't make sense
QString text = keyEvent - > text ( ) ;
if ( text . size ( ) )
{
typingEvent ( text ) ;
return true ;
}
}
}
else
if ( obj = = ui . dictsList ) {
2021-12-16 14:46:21 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress | | ev - > type ( ) = = QEvent : : ShortcutOverride )
2011-06-05 11:49:50 +00:00
{
QKeyEvent * keyEvent = static_cast < QKeyEvent * > ( ev ) ;
// Handle typing events used to initiate new lookups
2011-06-06 09:54:23 +00:00
// TODO: refactor to eliminate duplication (see above)
2011-06-05 11:49:50 +00:00
if ( keyEvent - > modifiers ( ) &
( Qt : : ControlModifier | Qt : : AltModifier | Qt : : MetaModifier ) )
2009-12-13 17:15:34 +00:00
return false ; // A non-typing modifier is pressed
2022-01-14 14:58:19 +00:00
if ( Utils : : ignoreKeyEvent ( keyEvent ) )
2009-12-13 17:15:34 +00:00
return false ; // Those key have other uses than to start typing
// or don't make sense
QString text = keyEvent - > text ( ) ;
if ( text . size ( ) )
{
typingEvent ( text ) ;
return true ;
}
2009-03-29 17:38:54 +00:00
}
}
return QMainWindow : : eventFilter ( obj , ev ) ;
return false ;
}
2009-01-28 20:55:45 +00:00
void MainWindow : : wordListItemActivated ( QListWidgetItem * item )
{
2012-11-25 11:37:57 +00:00
if ( wordListSelChanged )
wordListSelChanged = false ;
2012-12-24 11:30:03 +00:00
else {
// TODO: code duplication with translateInputFinished!
Qt : : KeyboardModifiers mods = QApplication : : keyboardModifiers ( ) ;
if ( mods & ( Qt : : ControlModifier | Qt : : ShiftModifier ) )
addNewTab ( ) ;
2012-11-25 11:37:57 +00:00
showTranslationFor ( item - > text ( ) ) ;
2012-12-24 11:30:03 +00:00
getCurrentArticleView ( ) - > focus ( ) ;
}
2009-01-28 20:55:45 +00:00
}
2009-02-08 14:02:27 +00:00
void MainWindow : : wordListSelectionChanged ( )
{
2012-12-24 11:30:03 +00:00
QList < QListWidgetItem * > selected = wordList - > selectedItems ( ) ;
2009-02-08 14:02:27 +00:00
if ( selected . size ( ) )
2012-11-25 11:37:57 +00:00
{
wordListSelChanged = true ;
showTranslationFor ( selected . front ( ) - > text ( ) ) ;
}
2009-02-08 14:02:27 +00:00
}
2011-06-05 11:49:50 +00:00
void MainWindow : : dictsListItemActivated ( QListWidgetItem * item )
{
2013-06-28 16:00:13 +00:00
jumpToDictionary ( item , true ) ;
2011-06-05 11:49:50 +00:00
}
void MainWindow : : dictsListSelectionChanged ( )
{
QList < QListWidgetItem * > selected = ui . dictsList - > selectedItems ( ) ;
2022-02-25 15:33:34 +00:00
if ( selected . size ( ) )
2022-01-19 12:16:45 +00:00
{
2022-02-25 15:33:34 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
if ( view )
{
QString dictId = ui . dictsList - > selectedItems ( ) . at ( 0 ) - > data ( Qt : : UserRole ) . toString ( ) ;
view - > setActiveArticleId ( dictId ) ;
}
// selection change ,no need to jump to article ,if jump to article ,the position in webview would be changed
// when click the dictionary in the html.
// jumpToDictionary( selected.front() );
2022-01-19 12:16:45 +00:00
}
2013-06-28 16:00:13 +00:00
}
void MainWindow : : jumpToDictionary ( QListWidgetItem * item , bool force )
{
ArticleView * view = getCurrentArticleView ( ) ;
if ( view )
{
view - > jumpToDictionary ( item - > data ( Qt : : UserRole ) . toString ( ) , force ) ;
}
2011-06-05 11:49:50 +00:00
}
2009-02-08 15:49:17 +00:00
void MainWindow : : openLinkInNewTab ( QUrl const & url ,
2009-05-11 19:14:28 +00:00
QUrl const & referrer ,
2009-05-29 19:48:50 +00:00
QString const & fromArticle ,
ArticleView : : Contexts const & contexts )
2009-02-08 15:49:17 +00:00
{
2009-05-11 19:14:28 +00:00
createNewTab ( ! cfg . preferences . newTabsOpenInBackground , " " ) - >
2009-05-29 19:48:50 +00:00
openLink ( url , referrer , fromArticle , contexts ) ;
2009-02-08 15:49:17 +00:00
}
void MainWindow : : showDefinitionInNewTab ( QString const & word ,
2009-05-11 19:14:28 +00:00
unsigned group ,
2009-05-29 19:48:50 +00:00
QString const & fromArticle ,
ArticleView : : Contexts const & contexts )
2009-02-08 15:49:17 +00:00
{
2009-05-11 19:14:28 +00:00
createNewTab ( ! cfg . preferences . newTabsOpenInBackground , word ) - >
2009-05-29 19:48:50 +00:00
showDefinition ( word , group , fromArticle , contexts ) ;
2009-02-08 15:49:17 +00:00
}
2014-02-09 15:00:48 +00:00
void MainWindow : : activeArticleChanged ( ArticleView const * view , QString const & id )
2011-07-03 12:27:08 +00:00
{
2014-02-09 15:00:48 +00:00
if ( view ! = getCurrentArticleView ( ) )
return ; // It was background action
2011-07-03 12:27:08 +00:00
// select the row with the corresponding id
for ( int i = 0 ; i < ui . dictsList - > count ( ) ; + + i ) {
QListWidgetItem * w = ui . dictsList - > item ( i ) ;
QString dictId = w - > data ( Qt : : UserRole ) . toString ( ) ;
if ( dictId = = id )
{
// update the current row, but only if necessary
if ( i ! = ui . dictsList - > currentRow ( ) )
{
ui . dictsList - > setCurrentRow ( i ) ;
}
return ;
}
}
}
2009-05-12 13:25:18 +00:00
void MainWindow : : typingEvent ( QString const & t )
{
2009-05-12 15:54:37 +00:00
if ( t = = " \n " | | t = = " \r " )
2012-02-16 14:56:25 +00:00
{
2012-12-24 11:30:03 +00:00
if ( translateLine - > isEnabled ( ) )
2012-02-16 14:56:25 +00:00
focusTranslateLine ( ) ;
}
2009-05-12 15:54:37 +00:00
else
{
2013-01-05 11:26:55 +00:00
if ( ( cfg . preferences . searchInDock & & ui . searchPane - > isFloating ( ) ) | | ui . dictsPane - > isFloating ( ) )
2009-05-13 13:47:51 +00:00
ui . searchPane - > activateWindow ( ) ;
2012-12-24 11:30:03 +00:00
if ( translateLine - > isEnabled ( ) )
2012-02-16 14:56:25 +00:00
{
2021-12-16 14:46:21 +00:00
translateLine - > clear ( ) ;
translateLine - > setFocus ( ) ;
// Escaping the typed-in characters is the user's responsibility.
// setTranslateBoxTextAndClearSuffix( t, WildcardsAreAlreadyEscaped, EnablePopup );
// translateLine->setCursorPosition( t.size() );
2012-02-16 14:56:25 +00:00
}
2009-05-12 15:54:37 +00:00
}
2009-05-12 13:25:18 +00:00
}
2009-09-23 18:44:38 +00:00
void MainWindow : : mutedDictionariesChanged ( )
{
if ( dictionaryBar . toggleViewAction ( ) - > isChecked ( ) )
applyMutedDictionariesState ( ) ;
}
2013-01-17 09:08:53 +00:00
void MainWindow : : showHistoryItem ( QString const & word )
{
// qDebug() << "Showing history item" << word;
history . enableAdd ( false ) ;
2021-06-30 16:49:58 +00:00
setTranslateBoxTextAndClearSuffix ( word , EscapeWildcards , DisablePopup ) ;
2013-01-17 09:08:53 +00:00
showTranslationFor ( word ) ;
history . enableAdd ( cfg . preferences . storeHistory ) ;
}
2021-06-10 16:13:11 +00:00
void MainWindow : : showTranslationFor ( Config : : InputPhrase const & phrase ,
2014-11-17 14:37:19 +00:00
unsigned inGroup ,
2021-06-29 08:59:16 +00:00
QString const & scrollTo )
2009-01-28 20:55:45 +00:00
{
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-01-28 20:55:45 +00:00
2009-04-10 21:07:03 +00:00
navPronounce - > setEnabled ( false ) ;
2009-10-21 20:06:17 +00:00
unsigned group = inGroup ? inGroup :
2009-10-21 19:37:07 +00:00
( groupInstances . empty ( ) ? 0 :
2012-12-24 11:30:03 +00:00
groupInstances [ groupList - > currentIndex ( ) ] . id ) ;
2009-10-21 19:37:07 +00:00
2021-06-29 08:59:16 +00:00
view - > showDefinition ( phrase , group , scrollTo ) ;
2009-01-28 20:55:45 +00:00
2011-06-07 11:27:19 +00:00
2009-01-28 20:55:45 +00:00
//ui.tabWidget->setTabText( ui.tabWidget->indexOf(ui.tab), inWord.trimmed() );
}
2009-02-05 20:55:00 +00:00
2021-06-10 16:13:11 +00:00
void MainWindow : : showTranslationFor ( QString const & word )
{
showTranslationFor ( Config : : InputPhrase : : fromPhrase ( word ) ) ;
}
2014-04-16 16:18:28 +00:00
void MainWindow : : showTranslationFor ( QString const & inWord ,
2014-04-22 13:47:02 +00:00
QStringList const & dictIDs ,
2018-04-10 14:49:52 +00:00
QRegExp const & searchRegExp ,
bool ignoreDiacritics )
2014-04-16 16:18:28 +00:00
{
ArticleView * view = getCurrentArticleView ( ) ;
navPronounce - > setEnabled ( false ) ;
2014-04-22 18:45:42 +00:00
view - > showDefinition ( inWord , dictIDs , searchRegExp ,
2018-04-10 14:49:52 +00:00
groupInstances [ groupList - > currentIndex ( ) ] . id ,
ignoreDiacritics ) ;
2014-04-16 16:18:28 +00:00
}
2013-05-31 05:28:36 +00:00
# ifdef HAVE_X11
2013-03-11 12:56:26 +00:00
void MainWindow : : toggleMainWindow ( bool onlyShow , bool byIconClick )
# else
2009-04-20 12:25:26 +00:00
void MainWindow : : toggleMainWindow ( bool onlyShow )
2013-03-11 12:56:26 +00:00
# endif
2009-04-20 12:25:26 +00:00
{
2010-04-27 07:49:37 +00:00
bool shown = false ;
2017-09-01 16:13:29 +00:00
if ( ! cfg . preferences . searchInDock )
translateBox - > setPopupEnabled ( false ) ;
2009-04-20 12:25:26 +00:00
if ( ! isVisible ( ) )
2009-05-07 16:22:17 +00:00
{
2009-04-20 12:25:26 +00:00
show ( ) ;
2019-01-31 14:59:24 +00:00
2012-12-29 10:01:36 +00:00
qApp - > setActiveWindow ( this ) ;
2009-05-07 16:22:17 +00:00
activateWindow ( ) ;
raise ( ) ;
2010-04-27 07:49:37 +00:00
shown = true ;
2009-05-07 16:22:17 +00:00
}
2009-04-20 12:25:26 +00:00
else
if ( isMinimized ( ) )
{
2013-01-23 20:36:53 +00:00
if ( wasMaximized )
showMaximized ( ) ;
else
showNormal ( ) ;
2009-04-20 12:25:26 +00:00
activateWindow ( ) ;
raise ( ) ;
2010-04-27 07:49:37 +00:00
shown = true ;
2009-04-20 12:25:26 +00:00
}
else
if ( ! isActiveWindow ( ) )
{
2019-01-31 14:59:24 +00:00
qApp - > setActiveWindow ( this ) ;
2009-04-20 12:25:26 +00:00
raise ( ) ;
2019-01-31 14:59:24 +00:00
activateWindow ( ) ;
2010-04-27 07:49:37 +00:00
shown = true ;
2009-04-20 12:25:26 +00:00
}
else
if ( ! onlyShow )
2011-06-24 19:03:03 +00:00
{
if ( cfg . preferences . enableTrayIcon )
hide ( ) ;
else
showMinimized ( ) ;
2014-03-03 13:46:41 +00:00
if ( headwordsDlg )
headwordsDlg - > hide ( ) ;
2014-04-16 16:18:28 +00:00
if ( ftsDlg )
ftsDlg - > hide ( ) ;
2014-06-23 15:11:15 +00:00
if ( helpWindow )
helpWindow - > hide ( ) ;
2011-06-24 19:03:03 +00:00
}
2010-04-27 07:49:37 +00:00
if ( shown )
2012-12-29 10:01:36 +00:00
{
2014-03-03 13:46:41 +00:00
if ( headwordsDlg )
headwordsDlg - > show ( ) ;
2014-04-16 16:18:28 +00:00
if ( ftsDlg )
ftsDlg - > show ( ) ;
2010-04-27 07:49:37 +00:00
focusTranslateLine ( ) ;
2022-10-01 01:16:36 +00:00
2012-12-29 10:01:36 +00:00
}
2009-04-20 12:25:26 +00:00
}
2009-04-21 18:27:26 +00:00
void MainWindow : : installHotKeys ( )
{
hotkeyWrapper . reset ( ) ; // Remove the old one
2022-09-11 01:36:52 +00:00
if ( cfg . preferences . enableMainWindowHotkey | |
cfg . preferences . enableClipboardHotkey )
2009-04-21 18:27:26 +00:00
{
try
{
2022-11-29 03:54:31 +00:00
hotkeyWrapper = std : : make_shared < HotkeyWrapper > ( this ) ;
2009-04-21 18:27:26 +00:00
}
catch ( HotkeyWrapper : : exInit & )
{
2013-02-01 12:36:01 +00:00
QMessageBox : : critical ( this , " GoldenDict " ,
2009-04-21 18:27:26 +00:00
tr ( " Failed to initialize hotkeys monitoring mechanism.<br> "
" Make sure your XServer has RECORD extension turned on. " ) ) ;
return ;
}
2010-03-19 14:17:45 +00:00
if ( cfg . preferences . enableMainWindowHotkey )
hotkeyWrapper - > setGlobalKey ( cfg . preferences . mainWindowHotkey . key1 ,
cfg . preferences . mainWindowHotkey . key2 ,
cfg . preferences . mainWindowHotkey . modifiers ,
0 ) ;
2009-04-21 18:27:26 +00:00
2022-09-11 01:36:52 +00:00
if ( cfg . preferences . enableClipboardHotkey & & scanPopup . get ( ) )
{
hotkeyWrapper - > setGlobalKey ( cfg . preferences . clipboardHotkey . key1 ,
cfg . preferences . clipboardHotkey . key2 ,
cfg . preferences . clipboardHotkey . modifiers ,
1 ) ;
}
2009-04-21 18:27:26 +00:00
2022-12-26 02:08:17 +00:00
connect ( hotkeyWrapper . get ( ) ,
& HotkeyWrapper : : hotkeyActivated ,
this ,
& MainWindow : : hotKeyActivated ,
Qt : : AutoConnection ) ;
2009-04-21 18:27:26 +00:00
}
}
void MainWindow : : hotKeyActivated ( int hk )
{
if ( ! hk )
2011-06-24 19:03:03 +00:00
toggleMainWindow ( ) ;
2009-04-21 18:27:26 +00:00
else
if ( scanPopup . get ( ) )
2022-05-23 03:27:57 +00:00
{
# ifdef HAVE_X11
// When the user requests translation with the Ctrl+C+C hotkey in certain apps
// on some GNU/Linux systems, GoldenDict appears to handle Ctrl+C+C before the
// active application finishes handling Ctrl+C. As a result, GoldenDict finds
// the clipboard empty, silently cancels the translation request, and users report
// that Ctrl+C+C is broken in these apps. Slightly delay handling the clipboard
// hotkey to give the active application more time and thus work around the issue.
QTimer : : singleShot ( 10 , scanPopup . get ( ) , SLOT ( translateWordFromClipboard ( ) ) ) ;
# else
2009-04-21 18:27:26 +00:00
scanPopup - > translateWordFromClipboard ( ) ;
2022-05-23 03:27:57 +00:00
# endif
}
2009-04-21 18:27:26 +00:00
}
2009-04-20 19:54:34 +00:00
void MainWindow : : prepareNewReleaseChecks ( )
{
if ( cfg . preferences . checkForNewReleases )
{
QDateTime now = QDateTime : : currentDateTime ( ) ;
if ( ! cfg . timeForNewReleaseCheck . isValid ( ) | |
now . daysTo ( cfg . timeForNewReleaseCheck ) > 2 )
{
// The date is invalid, or the check is set to happen more than 2 days
// in the future -- fix that.
cfg . timeForNewReleaseCheck = now . addDays ( 2 ) ;
}
int secsToCheck = now . secsTo ( cfg . timeForNewReleaseCheck ) ;
if ( secsToCheck < 1 )
secsToCheck = 1 ;
newReleaseCheckTimer . setSingleShot ( true ) ;
newReleaseCheckTimer . start ( secsToCheck * 1000 ) ;
}
else
newReleaseCheckTimer . stop ( ) ; // In case it was started before
}
void MainWindow : : checkForNewRelease ( )
{
2013-06-07 12:59:18 +00:00
if ( latestReleaseReply )
2017-03-20 14:31:02 +00:00
{
disconnect ( latestReleaseReply , 0 , 0 , 0 ) ;
2013-06-07 12:59:18 +00:00
latestReleaseReply - > deleteLater ( ) ;
2017-03-20 14:31:02 +00:00
}
2013-06-11 04:08:25 +00:00
latestReleaseReply = 0 ;
2009-04-20 19:54:34 +00:00
QNetworkRequest req (
2022-10-15 09:28:55 +00:00
QUrl ( " https://github.com/xiaoyifang/goldendict/releases " ) ) ;
2009-04-20 19:54:34 +00:00
latestReleaseReply = articleNetMgr . get ( req ) ;
2022-12-26 02:08:17 +00:00
connect ( latestReleaseReply ,
& QNetworkReply : : finished ,
this ,
& MainWindow : : latestReleaseReplyReady ,
Qt : : QueuedConnection ) ;
2009-04-20 19:54:34 +00:00
}
void MainWindow : : latestReleaseReplyReady ( )
{
2013-06-07 12:59:18 +00:00
if ( ! latestReleaseReply )
2009-04-20 19:54:34 +00:00
return ; // Some stray signal
bool success = false ;
QString latestVersion , downloadUrl ;
// See if we succeeded
if ( latestReleaseReply - > error ( ) = = QNetworkReply : : NoError )
{
2022-10-15 09:28:55 +00:00
QString latestReleaseInfo = QString : : fromUtf8 ( latestReleaseReply - > readAll ( ) ) ;
2022-12-24 22:01:50 +00:00
QRegularExpression firstReleaseAnchor ( R " (<a \ s+[^>]*?class= \" Link--primary \" [^>]*?>[^<]*?< \ /a>) " ,QRegularExpression::DotMatchesEverythingOption|QRegularExpression::CaseInsensitiveOption) ;
2022-10-15 09:28:55 +00:00
auto match = firstReleaseAnchor . match ( latestReleaseInfo ) ;
if ( match . hasMatch ( ) ) {
auto releaseAnchor = match . captured ( ) ;
2022-12-24 22:01:50 +00:00
QRegularExpression extractReleaseRx ( R " (<a \ s+.*?href= \" ([^ \" ]*) \ " .*?>(.*?)< \ /a>) " , QRegularExpression : : DotMatchesEverythingOption | QRegularExpression : : CaseInsensitiveOption ) ;
2022-10-15 09:28:55 +00:00
auto matchParts = extractReleaseRx . match ( releaseAnchor ) ;
if ( matchParts . hasMatch ( ) ) {
latestVersion = matchParts . captured ( 2 ) ;
QString prefix ( " GoldenDict-v " ) ;
if ( latestVersion . startsWith ( prefix ) ) {
latestVersion = latestVersion . mid ( prefix . length ( ) ) ;
}
downloadUrl = matchParts . captured ( 1 ) ;
if ( downloadUrl . startsWith ( " / " ) ) {
downloadUrl = latestReleaseReply - > request ( ) . url ( ) . host ( ) + downloadUrl ;
}
success = true ;
}
2009-04-20 19:54:34 +00:00
}
}
2017-03-20 14:31:02 +00:00
disconnect ( latestReleaseReply , 0 , 0 , 0 ) ;
2013-06-07 12:59:18 +00:00
latestReleaseReply - > deleteLater ( ) ;
latestReleaseReply = 0 ;
2009-04-20 19:54:34 +00:00
if ( ! success )
{
// Failed -- reschedule to check in two hours
newReleaseCheckTimer . start ( 2 * 60 * 60 * 1000 ) ;
2014-05-10 21:02:31 +00:00
GD_DPRINTF ( " Failed to check program version, retry in two hours \n " ) ;
2009-04-20 19:54:34 +00:00
}
else
{
// Success -- reschedule for a normal check and save config
cfg . timeForNewReleaseCheck = QDateTime ( ) ;
prepareNewReleaseChecks ( ) ;
Config : : save ( cfg ) ;
2014-05-10 21:02:31 +00:00
GD_DPRINTF ( " Program version's check successful, current version is %ls \n " ,
latestVersion . toStdWString ( ) . c_str ( ) ) ;
2009-04-20 19:54:34 +00:00
}
2009-05-14 17:59:49 +00:00
if ( success & & latestVersion > PROGRAM_VERSION & & latestVersion ! = cfg . skippedRelease )
2009-04-20 19:54:34 +00:00
{
QMessageBox msg ( QMessageBox : : Information ,
tr ( " New Release Available " ) ,
tr ( " Version <b>%1</b> of GoldenDict is now available for download.<br> "
" Click <b>Download</b> to get to the download page. " ) . arg ( latestVersion ) ,
QMessageBox : : NoButton ,
this ) ;
QPushButton * dload = msg . addButton ( tr ( " Download " ) , QMessageBox : : AcceptRole ) ;
2009-05-14 17:59:49 +00:00
QPushButton * skip = msg . addButton ( tr ( " Skip This Release " ) , QMessageBox : : DestructiveRole ) ;
2009-04-20 19:54:34 +00:00
msg . addButton ( QMessageBox : : Cancel ) ;
msg . exec ( ) ;
if ( msg . clickedButton ( ) = = dload )
QDesktopServices : : openUrl ( QUrl ( downloadUrl ) ) ;
2009-05-14 17:59:49 +00:00
else
if ( msg . clickedButton ( ) = = skip )
{
cfg . skippedRelease = latestVersion ;
Config : : save ( cfg ) ;
}
2009-04-20 19:54:34 +00:00
}
}
2009-02-08 20:20:02 +00:00
void MainWindow : : trayIconActivated ( QSystemTrayIcon : : ActivationReason r )
{
2010-09-16 18:52:40 +00:00
switch ( r ) {
case QSystemTrayIcon : : Trigger :
// Left click toggles the visibility of main window
2013-05-31 05:28:36 +00:00
# ifdef HAVE_X11
2013-03-11 12:56:26 +00:00
toggleMainWindow ( false , true ) ;
# else
2010-09-16 18:52:40 +00:00
toggleMainWindow ( ) ;
2013-03-11 12:56:26 +00:00
# endif
2010-09-16 18:52:40 +00:00
break ;
2009-07-31 11:40:54 +00:00
2010-09-16 18:52:40 +00:00
case QSystemTrayIcon : : MiddleClick :
// Middle mouse click on Tray translates selection
// it is functional like as stardict
if ( scanPopup . get ( ) ) {
scanPopup - > translateWordFromSelection ( ) ;
}
break ;
default :
break ;
2009-07-31 11:40:54 +00:00
2010-09-16 18:52:40 +00:00
}
2009-02-08 20:20:02 +00:00
}
void MainWindow : : scanEnableToggled ( bool on )
{
2009-04-21 18:27:26 +00:00
2009-02-08 20:20:02 +00:00
if ( scanPopup )
{
if ( on )
2013-04-10 13:31:44 +00:00
{
2009-02-08 20:20:02 +00:00
scanPopup - > enableScanning ( ) ;
2014-05-12 13:53:13 +00:00
# ifdef Q_OS_MAC
2013-12-26 14:13:10 +00:00
if ( ! MacMouseOver : : isAXAPIEnabled ( ) )
2013-04-10 13:31:44 +00:00
mainStatusBar - > showMessage ( tr ( " Accessibility API is not enabled " ) , 10000 ,
2022-01-27 00:23:57 +00:00
QPixmap ( " :/icons/error.svg " ) ) ;
2013-04-10 13:31:44 +00:00
# endif
2022-11-19 15:40:57 +00:00
enableScanningAction - > setIcon ( QIcon ( " :/icons/wizard-selected.svg " ) ) ;
2013-04-10 13:31:44 +00:00
}
2009-02-08 20:20:02 +00:00
else
2022-01-25 14:54:17 +00:00
{
2009-02-08 20:20:02 +00:00
scanPopup - > disableScanning ( ) ;
2022-11-19 15:40:57 +00:00
enableScanningAction - > setIcon ( QIcon ( " :/icons/wizard.svg " ) ) ;
2022-01-25 14:54:17 +00:00
}
2009-02-08 20:20:02 +00:00
}
2009-04-18 18:47:01 +00:00
2009-02-08 20:20:02 +00:00
updateTrayIcon ( ) ;
}
void MainWindow : : showMainWindow ( )
2009-02-05 20:55:00 +00:00
{
2009-04-20 12:25:26 +00:00
toggleMainWindow ( true ) ;
2009-02-05 20:55:00 +00:00
}
2009-02-08 14:02:27 +00:00
void MainWindow : : visitHomepage ( )
{
2010-11-06 19:40:07 +00:00
QDesktopServices : : openUrl ( QUrl ( " http://goldendict.org/ " ) ) ;
2009-02-08 14:02:27 +00:00
}
2011-07-10 07:36:43 +00:00
void MainWindow : : openConfigFolder ( )
{
QDesktopServices : : openUrl ( QUrl : : fromLocalFile ( Config : : getConfigDir ( ) ) ) ;
}
2009-02-08 14:02:27 +00:00
void MainWindow : : visitForum ( )
{
2010-11-06 19:40:07 +00:00
QDesktopServices : : openUrl ( QUrl ( " http://goldendict.org/forum/ " ) ) ;
2009-02-08 14:02:27 +00:00
}
void MainWindow : : showAbout ( )
{
2022-11-19 10:06:30 +00:00
About about ( this , & dictionaries ) ;
2009-04-12 22:02:15 +00:00
2009-02-08 14:02:27 +00:00
about . show ( ) ;
about . exec ( ) ;
}
2009-10-12 12:41:20 +00:00
void MainWindow : : showDictBarNamesTriggered ( )
{
bool show = showDictBarNamesAction . isChecked ( ) ;
dictionaryBar . setToolButtonStyle ( show ? Qt : : ToolButtonTextBesideIcon :
Qt : : ToolButtonIconOnly ) ;
cfg . showingDictBarNames = show ;
}
2011-05-22 02:42:05 +00:00
void MainWindow : : useSmallIconsInToolbarsTriggered ( )
{
bool useSmallIcons = useSmallIconsInToolbarsAction . isChecked ( ) ;
int extent = useSmallIcons ? QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_SmallIconSize ) :
QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_ToolBarIconSize ) ;
navToolbar - > setIconSize ( QSize ( extent , extent ) ) ;
2013-01-23 13:40:49 +00:00
// additional fix for #176
menuButton - > setIconSize ( QSize ( extent , extent ) ) ;
2011-05-22 02:42:05 +00:00
updateDictionaryBar ( ) ;
cfg . usingSmallIconsInToolbars = useSmallIcons ;
2013-02-27 13:12:46 +00:00
if ( scanPopup . get ( ) )
scanPopup - > setDictionaryIconSize ( ) ;
2011-05-22 02:42:05 +00:00
}
2011-06-26 11:53:28 +00:00
void MainWindow : : toggleMenuBarTriggered ( bool announce )
{
2013-01-04 21:42:25 +00:00
cfg . preferences . hideMenubar = ! toggleMenuBarAction . isChecked ( ) ;
2011-06-26 11:53:28 +00:00
2011-06-27 18:54:15 +00:00
if ( announce )
2011-06-26 11:53:28 +00:00
{
2011-06-27 18:54:15 +00:00
if ( cfg . preferences . hideMenubar )
{
mainStatusBar - > showMessage (
2011-07-09 11:55:44 +00:00
tr ( " You have chosen to hide a menubar. Use %1 to show it back. " )
. arg ( QString ( " <b>%1</b> " ) ) . arg ( tr ( " Ctrl+M " ) ) ,
10000 ,
QPixmap ( " :/icons/warning.png " ) ) ;
2011-06-27 18:54:15 +00:00
}
else
{
mainStatusBar - > clearMessage ( ) ;
}
2011-06-26 11:53:28 +00:00
}
2013-01-06 12:42:18 +00:00
// Obtain from the menubar all the actions with shortcuts
// and either add them to the main window or remove them,
// depending on the menubar state.
QList < QMenu * > allMenus = menuBar ( ) - > findChildren < QMenu * > ( ) ;
QListIterator < QMenu * > menuIter ( allMenus ) ;
while ( menuIter . hasNext ( ) )
{
QMenu * menu = menuIter . next ( ) ;
QList < QAction * > allMenuActions = menu - > actions ( ) ;
QListIterator < QAction * > actionsIter ( allMenuActions ) ;
while ( actionsIter . hasNext ( ) )
{
QAction * action = actionsIter . next ( ) ;
if ( ! action - > shortcut ( ) . isEmpty ( ) )
{
if ( cfg . preferences . hideMenubar )
{
// add all menubar actions to the main window,
// before we hide the menubar
addAction ( action ) ;
}
else
{
// remove all menubar actions from the main window
removeAction ( action ) ;
}
}
}
}
2011-06-26 11:53:28 +00:00
menuBar ( ) - > setVisible ( ! cfg . preferences . hideMenubar ) ;
2012-12-29 18:46:43 +00:00
beforeOptionsSeparator - > setVisible ( cfg . preferences . hideMenubar ) ;
menuButtonAction - > setVisible ( cfg . preferences . hideMenubar ) ;
2011-06-26 11:53:28 +00:00
}
2012-12-03 21:57:00 +00:00
void MainWindow : : on_clearHistory_triggered ( )
2009-10-21 19:37:07 +00:00
{
history . clear ( ) ;
2011-03-30 04:47:55 +00:00
history . save ( ) ;
2009-10-21 19:37:07 +00:00
}
2012-12-03 21:57:00 +00:00
void MainWindow : : on_newTab_triggered ( )
2011-06-25 07:34:28 +00:00
{
addNewTab ( ) ;
}
2009-04-18 18:47:01 +00:00
void MainWindow : : setAutostart ( bool autostart )
{
2018-05-06 12:26:55 +00:00
# if defined Q_OS_WIN32
2009-04-18 18:47:01 +00:00
QSettings reg ( " HKEY_CURRENT_USER \\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Run " ,
QSettings : : NativeFormat ) ;
2009-04-19 21:32:18 +00:00
if ( autostart ) {
QString app_fname = QString ( " \" %1 \" " ) . arg ( QCoreApplication : : applicationFilePath ( ) ) ;
app_fname . replace ( " / " , " \\ " ) ;
reg . setValue ( QCoreApplication : : applicationName ( ) , app_fname ) ;
}
else {
2009-04-18 18:47:01 +00:00
reg . remove ( QCoreApplication : : applicationName ( ) ) ;
2009-04-19 21:32:18 +00:00
}
2009-04-18 18:47:01 +00:00
reg . sync ( ) ;
2018-05-06 12:26:55 +00:00
# elif defined HAVE_X11
const QString destinationPath = QDir : : homePath ( ) + " /.config/autostart/goldendict-owned-by-preferences.desktop " ;
if ( autostart = = QFile : : exists ( destinationPath ) )
return ; // Nothing to do.
if ( autostart )
{
2021-02-25 10:31:18 +00:00
const QString sourcePath = Config : : getProgramDataDir ( ) + " ../applications/org.goldendict.GoldenDict.desktop " ;
2018-05-06 12:26:55 +00:00
QFile : : copy ( sourcePath , destinationPath ) ;
}
else
QFile : : remove ( destinationPath ) ;
2009-04-18 18:47:01 +00:00
# endif
}
2012-12-03 21:57:00 +00:00
void MainWindow : : on_actionCloseToTray_triggered ( )
2009-04-18 18:47:01 +00:00
{
2009-04-20 12:25:26 +00:00
toggleMainWindow ( ! cfg . preferences . enableTrayIcon ) ;
2009-04-18 18:47:01 +00:00
}
2009-04-30 19:57:25 +00:00
2012-12-03 21:57:00 +00:00
void MainWindow : : on_pageSetup_triggered ( )
2009-05-01 12:20:33 +00:00
{
2009-11-11 09:40:32 +00:00
if ( getPrinter ( ) . isValid ( ) )
2009-05-24 18:51:45 +00:00
{
2009-11-11 09:40:32 +00:00
QPageSetupDialog dialog ( & getPrinter ( ) , this ) ;
2009-05-01 12:20:33 +00:00
2009-05-24 18:51:45 +00:00
dialog . exec ( ) ;
}
else
QMessageBox : : critical ( this , tr ( " Page Setup " ) ,
tr ( " No printer is available. Please install one first. " ) ) ;
2009-05-01 12:20:33 +00:00
}
2012-12-03 21:57:00 +00:00
void MainWindow : : on_printPreview_triggered ( )
2009-05-01 12:20:33 +00:00
{
2022-03-29 12:47:41 +00:00
QPrinter printer ;
QPrintPreviewDialog dialog ( & printer , this ) ;
2022-03-30 09:19:08 +00:00
auto combox = dialog . findChild < QComboBox * > ( ) ;
int index = combox - > findText ( " 100% " ) ;
combox - > setCurrentIndex ( index ) ;
2009-05-01 12:20:33 +00:00
2022-12-26 02:08:17 +00:00
connect ( & dialog , & QPrintPreviewDialog : : paintRequested , this , & MainWindow : : printPreviewPaintRequested ) ;
2009-05-01 12:20:33 +00:00
2013-02-27 23:37:36 +00:00
dialog . showMaximized ( ) ;
2009-05-01 12:20:33 +00:00
dialog . exec ( ) ;
}
2012-12-03 21:57:00 +00:00
void MainWindow : : on_print_triggered ( )
2009-05-01 12:20:33 +00:00
{
2009-11-11 09:40:32 +00:00
QPrintDialog dialog ( & getPrinter ( ) , this ) ;
2009-05-02 21:46:43 +00:00
2009-05-01 12:20:33 +00:00
dialog . setWindowTitle ( tr ( " Print Article " ) ) ;
2009-05-02 21:46:43 +00:00
2009-05-01 12:20:33 +00:00
if ( dialog . exec ( ) ! = QDialog : : Accepted )
return ;
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-05-01 12:20:33 +00:00
2011-07-01 19:22:40 +00:00
view - > print ( & getPrinter ( ) ) ;
2009-05-01 12:20:33 +00:00
}
void MainWindow : : printPreviewPaintRequested ( QPrinter * printer )
{
2011-07-01 19:22:40 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-05-01 12:20:33 +00:00
2011-07-01 19:22:40 +00:00
view - > print ( printer ) ;
2009-05-01 12:20:33 +00:00
}
2013-05-27 11:18:13 +00:00
static void filterAndCollectResources ( QString & html , QRegExp & rx , const QString & sep ,
const QString & folder , set < QByteArray > & resourceIncluded ,
vector < pair < QUrl , QString > > & downloadResources )
{
int pos = 0 ;
while ( ( pos = rx . indexIn ( html , pos ) ) ! = - 1 )
{
2013-05-31 04:20:25 +00:00
QUrl url ( rx . cap ( 1 ) ) ;
2013-05-27 11:18:13 +00:00
QString host = url . host ( ) ;
2022-09-22 12:13:57 +00:00
QString resourcePath = Utils : : Url : : path ( url ) ;
2013-05-27 11:18:13 +00:00
if ( ! host . startsWith ( ' / ' ) )
host . insert ( 0 , ' / ' ) ;
if ( ! resourcePath . startsWith ( ' / ' ) )
resourcePath . insert ( 0 , ' / ' ) ;
QCryptographicHash hash ( QCryptographicHash : : Md5 ) ;
hash . addData ( rx . cap ( ) . toUtf8 ( ) ) ;
if ( resourceIncluded . insert ( hash . result ( ) ) . second )
{
2018-07-07 09:33:15 +00:00
// Gather resource information (url, filename) to be download later
2013-05-27 11:18:13 +00:00
downloadResources . push_back ( pair < QUrl , QString > ( url , folder + host + resourcePath ) ) ;
}
// Modify original url, set to the native one
2022-09-21 12:30:36 +00:00
resourcePath = QString : : fromLatin1 ( QUrl : : toPercentEncoding ( resourcePath , " / " ) ) ;
2013-05-27 11:18:13 +00:00
QString newUrl = sep + QDir ( folder ) . dirName ( ) + host + resourcePath + sep ;
html . replace ( pos , rx . cap ( ) . length ( ) , newUrl ) ;
pos + = newUrl . length ( ) ;
}
}
2012-12-03 21:57:00 +00:00
void MainWindow : : on_saveArticle_triggered ( )
2009-05-01 11:17:29 +00:00
{
2022-03-30 16:03:42 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-05-01 11:17:29 +00:00
2015-07-02 15:16:14 +00:00
QString fileName = view - > getTitle ( ) . simplified ( ) ;
2015-03-22 16:48:56 +00:00
// Replace reserved filename characters
2022-12-24 22:01:50 +00:00
QRegularExpression rxName ( R " ([/ \\ \ ? \ *: \ |<>]) " ) ;
2015-07-02 15:16:14 +00:00
fileName . replace ( rxName , " _ " ) ;
2015-03-22 16:48:56 +00:00
fileName + = " .html " ;
2013-04-12 06:57:41 +00:00
QString savePath ;
2009-05-01 11:17:29 +00:00
2022-03-30 16:03:42 +00:00
if ( cfg . articleSavePath . isEmpty ( ) )
2013-04-12 06:57:41 +00:00
savePath = QDir : : homePath ( ) ;
else
{
savePath = QDir : : fromNativeSeparators ( cfg . articleSavePath ) ;
2022-03-30 16:03:42 +00:00
if ( ! QDir ( savePath ) . exists ( ) )
2013-04-12 06:57:41 +00:00
savePath = QDir : : homePath ( ) ;
}
2009-05-02 21:46:43 +00:00
2013-05-27 11:18:13 +00:00
QFileDialog : : Options options = QFileDialog : : HideNameFilterDetails ;
QString selectedFilter ;
QStringList filters ;
filters . push_back ( tr ( " Article, Complete (*.html) " ) ) ;
filters . push_back ( tr ( " Article, HTML Only (*.html) " ) ) ;
2013-04-12 06:57:41 +00:00
fileName = savePath + " / " + fileName ;
2022-03-30 16:03:42 +00:00
fileName = QFileDialog : : getSaveFileName ( this ,
tr ( " Save Article As " ) ,
2013-05-27 11:18:13 +00:00
fileName ,
filters . join ( " ;; " ) ,
2022-03-30 16:03:42 +00:00
& selectedFilter ,
options ) ;
2013-05-27 11:18:13 +00:00
2022-08-12 10:51:38 +00:00
// The " (*.html)" part of filters[i] is absent from selectedFilter in Qt 5.
bool const complete = filters . at ( 0 ) . startsWith ( selectedFilter ) ;
2009-05-01 11:17:29 +00:00
2022-03-30 16:03:42 +00:00
if ( fileName . isEmpty ( ) )
return ;
2009-05-01 11:17:29 +00:00
2022-03-30 16:03:42 +00:00
view - > toHtml (
[ = ] ( QString & html ) mutable
2013-04-12 06:57:41 +00:00
{
2022-03-30 16:03:42 +00:00
QFile file ( fileName ) ;
if ( ! file . open ( QIODevice : : WriteOnly ) )
2022-03-30 15:08:24 +00:00
{
2022-03-30 16:03:42 +00:00
QMessageBox : : critical ( this , tr ( " Error " ) , tr ( " Can't save article: %1 " ) . arg ( file . errorString ( ) ) ) ;
}
else
{
QFileInfo fi ( fileName ) ;
cfg . articleSavePath = QDir : : toNativeSeparators ( fi . absoluteDir ( ) . absolutePath ( ) ) ;
2013-05-27 11:18:13 +00:00
2022-03-30 16:03:42 +00:00
// Convert internal links
2015-07-03 14:40:28 +00:00
2022-03-30 16:03:42 +00:00
QRegExp rx3 ( " href= \" (bword:|gdlookup://localhost/) ( [ ^ \ " ]+) \" " ) ;
int pos = 0 ;
QRegularExpression anchorRx ( " (g[0-9a-f]{32}_) [ 0 - 9 a - f ] + _ " ) ;
while ( ( pos = rx3 . indexIn ( html , pos ) ) ! = - 1 )
2016-05-11 14:44:07 +00:00
{
2022-03-30 16:03:42 +00:00
QString name = QUrl : : fromPercentEncoding ( rx3 . cap ( 2 ) . simplified ( ) . toLatin1 ( ) ) ;
QString anchor ;
name . replace ( " ?gdanchor= " , " # " ) ;
int n = name . indexOf ( ' # ' ) ;
if ( n > 0 )
2022-03-30 15:08:24 +00:00
{
2022-03-30 16:03:42 +00:00
anchor = name . mid ( n ) ;
name . truncate ( n ) ;
anchor . replace ( anchorRx , " \\ 1 " ) ; // MDict anchors
2022-03-30 15:08:24 +00:00
}
2022-03-30 16:03:42 +00:00
name . replace ( rxName , " _ " ) ;
name = QString ( " href= \" " ) + QUrl : : toPercentEncoding ( name ) + " .html " + anchor + " \" " ;
html . replace ( pos , rx3 . cap ( ) . length ( ) , name ) ;
pos + = name . length ( ) ;
2016-05-11 14:44:07 +00:00
}
2015-07-03 14:40:28 +00:00
2022-03-30 16:03:42 +00:00
// MDict anchors
QRegularExpression anchorLinkRe (
2022-12-24 22:01:50 +00:00
R " ((< \ s*a \ s+[^>]* \b (?:name|id) \b \ s*= \ s*[ " ' ] * g [ 0 - 9 a - f ] { 32 } _ ) ( [ 0 - 9 a - f ] + _ ) ( ? = [ ^ " '])) " ,
2022-06-01 13:15:09 +00:00
QRegularExpression : : PatternOption : : CaseInsensitiveOption | QRegularExpression : : UseUnicodePropertiesOption ) ;
2022-03-30 16:03:42 +00:00
html . replace ( anchorLinkRe , " \\ 1 " ) ;
2016-05-11 14:44:07 +00:00
2022-03-30 16:03:42 +00:00
if ( complete )
2013-05-27 11:18:13 +00:00
{
2022-03-30 16:03:42 +00:00
QString folder = fi . absoluteDir ( ) . absolutePath ( ) + " / " + fi . baseName ( ) + " _files " ;
QRegExp rx1 ( " \" ((?:bres|gico|gdau|qrcx|gdvideo) : / / [ ^ \ " ]+) \" " ) ;
QRegExp rx2 ( " '((?:bres|gico|gdau|qrcx|gdvideo) : / / [ ^ ' ] + ) ' " ) ;
set < QByteArray > resourceIncluded ;
vector < pair < QUrl , QString > > downloadResources ;
filterAndCollectResources ( html , rx1 , " \" " , folder , resourceIncluded , downloadResources ) ;
filterAndCollectResources ( html , rx2 , " ' " , folder , resourceIncluded , downloadResources ) ;
ArticleSaveProgressDialog * progressDialog = new ArticleSaveProgressDialog ( this ) ;
// reserve '1' for saving main html file
int maxVal = 1 ;
// Pull and save resources to files
for ( vector < pair < QUrl , QString > > : : const_iterator i = downloadResources . begin ( ) ;
i ! = downloadResources . end ( ) ;
+ + i )
2013-05-27 11:18:13 +00:00
{
2022-03-30 16:03:42 +00:00
ResourceToSaveHandler * handler = view - > saveResource ( i - > first , i - > second ) ;
if ( ! handler - > isEmpty ( ) )
2022-03-27 15:36:14 +00:00
{
2022-03-30 16:03:42 +00:00
maxVal + = 1 ;
2022-12-26 02:08:17 +00:00
connect ( handler , & ResourceToSaveHandler : : done , progressDialog , & ArticleSaveProgressDialog : : perform ) ;
2022-03-27 15:36:14 +00:00
}
2013-05-27 11:18:13 +00:00
}
2022-03-30 16:03:42 +00:00
progressDialog - > setLabelText ( tr ( " Saving article... " ) ) ;
progressDialog - > setRange ( 0 , maxVal ) ;
progressDialog - > setValue ( 0 ) ;
progressDialog - > show ( ) ;
2013-05-27 11:18:13 +00:00
2022-03-30 16:03:42 +00:00
file . write ( html . toUtf8 ( ) ) ;
progressDialog - > perform ( ) ;
2022-03-28 02:30:23 +00:00
}
2022-03-30 16:03:42 +00:00
else
{
file . write ( html . toUtf8 ( ) ) ;
}
2013-05-27 11:18:13 +00:00
}
2022-03-30 16:03:42 +00:00
} ) ;
2009-05-01 11:17:29 +00:00
}
2012-12-03 21:57:00 +00:00
void MainWindow : : on_rescanFiles_triggered ( )
2009-05-08 10:47:34 +00:00
{
hotkeyWrapper . reset ( ) ; // No hotkeys while we're editing dictionaries
scanPopup . reset ( ) ; // No scan popup either. No one should use dictionaries.
2014-04-10 14:00:12 +00:00
closeHeadwordsDialog ( ) ;
2014-04-16 16:18:28 +00:00
closeFullTextSearchDialog ( ) ;
2009-05-08 10:47:34 +00:00
2014-04-17 14:18:15 +00:00
ftsIndexing . stopIndexing ( ) ;
ftsIndexing . clearDictionaries ( ) ;
2009-05-08 10:47:34 +00:00
2009-05-15 12:15:08 +00:00
groupInstances . clear ( ) ; // Release all the dictionaries they hold
2013-02-19 18:56:05 +00:00
dictionaries . clear ( ) ;
dictionariesUnmuted . clear ( ) ;
dictionaryBar . setDictionaries ( dictionaries ) ;
2009-05-15 12:15:08 +00:00
2009-05-08 10:47:34 +00:00
loadDictionaries ( this , true , cfg , dictionaries , dictNetMgr ) ;
2022-03-22 12:20:48 +00:00
dictMap = Dictionary : : dictToMap ( dictionaries ) ;
2010-09-16 18:52:40 +00:00
2014-04-17 14:31:51 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + )
2017-03-09 16:11:17 +00:00
{
2014-04-17 14:31:51 +00:00
dictionaries [ x ] - > setFTSParameters ( cfg . preferences . fts ) ;
2017-03-09 16:11:17 +00:00
dictionaries [ x ] - > setSynonymSearchEnabled ( cfg . preferences . synonymSearchEnabled ) ;
}
2014-04-17 14:31:51 +00:00
2014-04-17 14:18:15 +00:00
ftsIndexing . setDictionaries ( dictionaries ) ;
2014-04-16 16:18:28 +00:00
ftsIndexing . doIndexing ( ) ;
2010-09-16 18:52:40 +00:00
2009-05-08 10:47:34 +00:00
updateGroupList ( ) ;
makeScanPopup ( ) ;
installHotKeys ( ) ;
2013-05-30 13:54:47 +00:00
2021-06-10 16:13:11 +00:00
updateSuggestionList ( ) ;
2009-05-08 10:47:34 +00:00
}
2012-12-29 11:19:49 +00:00
void MainWindow : : on_alwaysOnTop_triggered ( bool checked )
{
cfg . preferences . alwaysOnTop = checked ;
bool wasVisible = isVisible ( ) ;
Qt : : WindowFlags flags = this - > windowFlags ( ) ;
if ( checked )
{
setWindowFlags ( flags | Qt : : CustomizeWindowHint | Qt : : WindowStaysOnTopHint ) ;
mainStatusBar - > showMessage (
tr ( " The main window is set to be always on top. " ) ,
10000 ,
QPixmap ( " :/icons/warning.png " ) ) ;
}
else
{
setWindowFlags ( flags ^ ( Qt : : CustomizeWindowHint | Qt : : WindowStaysOnTopHint ) ) ;
mainStatusBar - > clearMessage ( ) ;
}
if ( wasVisible )
{
show ( ) ;
}
installHotKeys ( ) ;
}
2009-04-30 19:57:25 +00:00
void MainWindow : : zoomin ( )
{
cfg . preferences . zoomFactor + = 0.1 ;
applyZoomFactor ( ) ;
}
void MainWindow : : zoomout ( )
{
cfg . preferences . zoomFactor - = 0.1 ;
applyZoomFactor ( ) ;
}
void MainWindow : : unzoom ( )
{
cfg . preferences . zoomFactor = 1 ;
applyZoomFactor ( ) ;
}
void MainWindow : : applyZoomFactor ( )
2019-01-31 12:45:30 +00:00
{
// Always call this function synchronously to potentially disable a zoom action,
// which is being repeatedly triggered. When the action is disabled, its
// triggered() signal is no longer emitted, which in turn improves performance.
adjustCurrentZoomFactor ( ) ;
// Scaling article views asynchronously dramatically improves performance when
// a zoom action is triggered repeatedly while many or large articles are open
// in the main window or in scan popup.
// Multiple zoom action signals are processed before (often slow) article view
// scaling is requested. Multiple scaling requests then ask for the same zoom factor,
// so all of them except for the first one don't change anything and run very fast.
// In effect, some intermediate zoom factors are skipped when scaling is slow.
// The slower the scaling, the more steps are skipped.
2022-12-26 02:08:17 +00:00
QTimer : : singleShot ( 0 , this , & MainWindow : : scaleArticlesByCurrentZoomFactor ) ;
2019-01-31 12:45:30 +00:00
}
void MainWindow : : adjustCurrentZoomFactor ( )
2009-04-30 19:57:25 +00:00
{
2012-12-03 13:13:13 +00:00
if ( cfg . preferences . zoomFactor > = 5 )
cfg . preferences . zoomFactor = 5 ;
2013-03-13 20:19:38 +00:00
else if ( cfg . preferences . zoomFactor < = 0.1 )
cfg . preferences . zoomFactor = 0.1 ;
2009-04-30 22:09:04 +00:00
2012-12-03 13:13:13 +00:00
zoomIn - > setEnabled ( cfg . preferences . zoomFactor < 5 ) ;
2013-03-13 20:19:38 +00:00
zoomOut - > setEnabled ( cfg . preferences . zoomFactor > 0.1 ) ;
2022-02-18 16:16:11 +00:00
zoomBase - > setEnabled ( ! qFuzzyCompare ( cfg . preferences . zoomFactor , 1.0 ) ) ;
2019-01-31 12:45:30 +00:00
}
2009-04-30 22:09:04 +00:00
2019-01-31 12:45:30 +00:00
void MainWindow : : scaleArticlesByCurrentZoomFactor ( )
{
2009-04-30 19:57:25 +00:00
for ( int i = 0 ; i < ui . tabWidget - > count ( ) ; i + + )
{
ArticleView & view =
dynamic_cast < ArticleView & > ( * ( ui . tabWidget - > widget ( i ) ) ) ;
view . setZoomFactor ( cfg . preferences . zoomFactor ) ;
}
2009-04-30 20:20:05 +00:00
if ( scanPopup . get ( ) )
scanPopup - > applyZoomFactor ( ) ;
2009-04-30 19:57:25 +00:00
}
2010-07-02 11:19:02 +00:00
void MainWindow : : doWordsZoomIn ( )
{
+ + cfg . preferences . wordsZoomLevel ;
applyWordsZoomLevel ( ) ;
}
void MainWindow : : doWordsZoomOut ( )
{
- - cfg . preferences . wordsZoomLevel ;
applyWordsZoomLevel ( ) ;
}
void MainWindow : : doWordsZoomBase ( )
{
cfg . preferences . wordsZoomLevel = 0 ;
applyWordsZoomLevel ( ) ;
}
void MainWindow : : applyWordsZoomLevel ( )
{
QFont font ( wordListDefaultFont ) ;
int ps = font . pointSize ( ) ;
if ( cfg . preferences . wordsZoomLevel ! = 0 )
{
ps + = cfg . preferences . wordsZoomLevel ;
if ( ps < 1 )
ps = 1 ;
font . setPointSize ( ps ) ;
}
2012-12-24 11:30:03 +00:00
if ( wordList - > font ( ) . pointSize ( ) ! = ps )
wordList - > setFont ( font ) ;
2010-07-02 11:19:02 +00:00
font = translateLineDefaultFont ;
ps = font . pointSize ( ) ;
if ( cfg . preferences . wordsZoomLevel ! = 0 )
{
ps + = cfg . preferences . wordsZoomLevel ;
if ( ps < 1 )
ps = 1 ;
font . setPointSize ( ps ) ;
}
2012-12-24 11:30:03 +00:00
if ( translateLine - > font ( ) . pointSize ( ) ! = ps )
translateLine - > setFont ( font ) ;
2019-01-26 19:11:27 +00:00
font = groupListDefaultFont ;
ps = font . pointSize ( ) ;
if ( cfg . preferences . wordsZoomLevel ! = 0 )
{
ps + = cfg . preferences . wordsZoomLevel ;
if ( ps < 1 )
ps = 1 ;
font . setPointSize ( ps ) ;
}
if ( groupList - > font ( ) . pointSize ( ) ! = ps )
{
2022-02-27 06:26:49 +00:00
disconnect ( groupList , & GroupComboBox : : currentIndexChanged ,
this , & MainWindow : : currentGroupChanged ) ;
2019-01-26 19:11:27 +00:00
int n = groupList - > currentIndex ( ) ;
groupList - > clear ( ) ;
groupList - > setFont ( font ) ;
groupList - > fill ( groupInstances ) ;
groupList - > setCurrentIndex ( n ) ;
2022-04-01 15:32:11 +00:00
connect ( groupList , & GroupComboBox : : currentIndexChanged ,
this , & MainWindow : : currentGroupChanged ) ;
2019-01-26 19:11:27 +00:00
}
2010-07-02 11:19:02 +00:00
wordsZoomBase - > setEnabled ( cfg . preferences . wordsZoomLevel ! = 0 ) ;
2022-11-14 12:35:42 +00:00
if ( ! cfg . preferences . searchInDock )
{
// Invalidating navToolbar's layout displays translateBoxWidget w/o the need to press the toolbar
// extension button when Words Zoom level decreases enough for translateBoxWidget to fit in the toolbar.
navToolbar - > layout ( ) - > invalidate ( ) ;
}
2017-07-14 13:02:21 +00:00
if ( scanPopup . get ( ) )
scanPopup - > applyWordsZoomLevel ( ) ;
2010-07-02 11:19:02 +00:00
}
2010-11-20 13:43:55 +00:00
void MainWindow : : messageFromAnotherInstanceReceived ( QString const & message )
{
if ( message = = " bringToFront " )
2011-11-16 13:02:56 +00:00
{
2010-11-20 13:43:55 +00:00
toggleMainWindow ( true ) ;
2011-11-16 13:02:56 +00:00
return ;
}
if ( message . left ( 15 ) = = " translateWord: " )
{
if ( scanPopup . get ( ) )
scanPopup - > translateWord ( message . mid ( 15 ) ) ;
else
wordReceived ( message . mid ( 15 ) ) ;
}
2017-03-10 13:29:23 +00:00
else
if ( message . left ( 10 ) = = " setGroup: " )
{
setGroupByName ( message . mid ( 10 ) , true ) ;
}
else
if ( message . left ( 15 ) = = " setPopupGroup: " )
{
setGroupByName ( message . mid ( 15 ) , false ) ;
}
2010-11-20 13:43:55 +00:00
else
qWarning ( ) < < " Unknown message received from another instance: " < < message ;
}
2011-07-01 19:22:40 +00:00
ArticleView * MainWindow : : getCurrentArticleView ( )
{
if ( QWidget * cw = ui . tabWidget - > currentWidget ( ) )
{
2013-06-30 07:41:23 +00:00
return dynamic_cast < ArticleView * > ( cw ) ;
2011-07-01 19:22:40 +00:00
}
return 0 ;
}
2011-11-16 12:52:25 +00:00
2021-06-10 16:13:11 +00:00
void MainWindow : : phraseReceived ( Config : : InputPhrase const & phrase )
{
toggleMainWindow ( true ) ;
2021-06-30 16:49:58 +00:00
setTranslateBoxTextAndKeepSuffix ( phrase . phrase , EscapeWildcards , NoPopupChange ) ;
2021-06-10 16:13:11 +00:00
translateBoxSuffix = phrase . punctuationSuffix ;
respondToTranslationRequest ( phrase , false ) ;
}
2011-11-16 12:52:25 +00:00
void MainWindow : : wordReceived ( const QString & word )
{
2021-06-10 16:13:11 +00:00
phraseReceived ( Config : : InputPhrase : : fromPhrase ( word ) ) ;
2011-11-16 12:52:25 +00:00
}
2012-02-16 14:56:25 +00:00
2014-11-17 14:37:19 +00:00
void MainWindow : : headwordReceived ( const QString & word , const QString & ID )
{
2021-06-10 16:13:11 +00:00
toggleMainWindow ( true ) ;
2021-06-30 16:49:58 +00:00
setTranslateBoxTextAndClearSuffix ( word , EscapeWildcards , NoPopupChange ) ;
2021-06-10 16:13:11 +00:00
respondToTranslationRequest ( Config : : InputPhrase : : fromPhrase ( word ) ,
2021-06-29 08:59:16 +00:00
false , ArticleView : : scrollToFromDictionaryId ( ID ) ) ;
2014-11-17 14:37:19 +00:00
}
2017-05-05 14:39:51 +00:00
void MainWindow : : updateFavoritesMenu ( )
{
if ( ui . favoritesPane - > toggleViewAction ( ) - > isChecked ( ) )
{
ui . showHideFavorites - > setText ( tr ( " &Hide " ) ) ;
}
else
{
ui . showHideFavorites - > setText ( tr ( " &Show " ) ) ;
}
}
2013-01-17 09:08:53 +00:00
void MainWindow : : updateHistoryMenu ( )
2012-02-16 14:56:25 +00:00
{
2013-01-17 09:08:53 +00:00
if ( ui . historyPane - > toggleViewAction ( ) - > isChecked ( ) )
{
ui . showHideHistory - > setText ( tr ( " &Hide " ) ) ;
}
else
{
ui . showHideHistory - > setText ( tr ( " &Show " ) ) ;
}
}
2012-09-12 12:32:29 +00:00
2017-05-05 14:39:51 +00:00
void MainWindow : : on_showHideFavorites_triggered ( )
{
ui . favoritesPane - > toggleViewAction ( ) - > trigger ( ) ;
ui . favoritesPane - > raise ( ) ; // useful when the Pane is tabbed.
}
2013-01-17 09:08:53 +00:00
void MainWindow : : on_showHideHistory_triggered ( )
{
ui . historyPane - > toggleViewAction ( ) - > trigger ( ) ;
ui . historyPane - > raise ( ) ; // useful when the Pane is tabbed.
2012-02-16 14:56:25 +00:00
}
2012-02-17 12:01:52 +00:00
2012-12-03 21:57:00 +00:00
void MainWindow : : on_exportHistory_triggered ( )
2012-02-17 12:01:52 +00:00
{
2012-09-10 18:00:29 +00:00
QString exportPath ;
2012-09-12 17:19:21 +00:00
if ( cfg . historyExportPath . isEmpty ( ) )
2012-09-10 18:00:29 +00:00
exportPath = QDir : : homePath ( ) ;
else
{
2012-09-12 17:19:21 +00:00
exportPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
2012-09-10 18:00:29 +00:00
if ( ! QDir ( exportPath ) . exists ( ) )
exportPath = QDir : : homePath ( ) ;
}
2012-02-17 12:01:52 +00:00
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Export history to file " ) ,
2012-09-10 18:00:29 +00:00
exportPath ,
2012-02-17 12:01:52 +00:00
tr ( " Text files (*.txt);;All files (*.*) " ) ) ;
if ( fileName . size ( ) = = 0 )
return ;
2012-09-12 17:19:21 +00:00
cfg . historyExportPath = QDir : : toNativeSeparators ( QFileInfo ( fileName ) . absoluteDir ( ) . absolutePath ( ) ) ;
2012-02-17 12:01:52 +00:00
QFile file ( fileName ) ;
for ( ; ; )
{
if ( ! file . open ( QFile : : WriteOnly | QIODevice : : Text ) )
break ;
// Write UTF-8 BOM
QByteArray line ;
line . append ( 0xEF ) . append ( 0xBB ) . append ( 0xBF ) ;
if ( file . write ( line ) ! = line . size ( ) )
break ;
// Write history
QList < History : : Item > const & items = history . getItems ( ) ;
QList < History : : Item > : : const_iterator i ;
for ( i = items . constBegin ( ) ; i ! = items . constEnd ( ) ; + + i )
{
line = i - > word . toUtf8 ( ) ;
line . replace ( ' \n ' , ' ' ) ;
line . replace ( ' \r ' , ' ' ) ;
line + = " \n " ;
if ( file . write ( line ) ! = line . size ( ) )
break ;
}
if ( i ! = items . constEnd ( ) )
break ;
file . close ( ) ;
mainStatusBar - > showMessage ( tr ( " History export complete " ) , 5000 ) ;
return ;
}
2012-09-10 13:01:35 +00:00
QString errStr = QString ( tr ( " Export error: " ) ) + file . errorString ( ) ;
2012-02-17 12:01:52 +00:00
file . close ( ) ;
2022-01-27 00:23:57 +00:00
mainStatusBar - > showMessage ( errStr , 10000 , QPixmap ( " :/icons/error.svg " ) ) ;
2012-02-17 12:01:52 +00:00
}
2012-09-10 13:01:35 +00:00
2013-01-17 09:08:53 +00:00
// TODO: consider moving parts of this method into History class.
2012-12-03 21:57:00 +00:00
void MainWindow : : on_importHistory_triggered ( )
2012-09-10 13:01:35 +00:00
{
2012-09-10 18:00:29 +00:00
QString importPath ;
2012-09-12 17:19:21 +00:00
if ( cfg . historyExportPath . isEmpty ( ) )
2012-09-10 18:00:29 +00:00
importPath = QDir : : homePath ( ) ;
else
{
2012-09-12 17:19:21 +00:00
importPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
2012-09-10 18:00:29 +00:00
if ( ! QDir ( importPath ) . exists ( ) )
importPath = QDir : : homePath ( ) ;
}
2012-09-10 13:01:35 +00:00
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Import history from file " ) ,
2012-09-10 18:00:29 +00:00
importPath ,
2012-09-10 13:01:35 +00:00
tr ( " Text files (*.txt);;All files (*.*) " ) ) ;
if ( fileName . size ( ) = = 0 )
return ;
2012-09-11 12:58:27 +00:00
QFileInfo fileInfo ( fileName ) ;
2012-09-12 17:19:21 +00:00
cfg . historyExportPath = QDir : : toNativeSeparators ( fileInfo . absoluteDir ( ) . absolutePath ( ) ) ;
2012-09-10 13:01:35 +00:00
QString errStr ;
QFile file ( fileName ) ;
for ( ; ; )
{
if ( ! file . open ( QFile : : ReadOnly | QIODevice : : Text ) )
break ;
QTextStream fileStream ( & file ) ;
QString itemStr , trimmedStr ;
QList < QString > itemList ;
history . clear ( ) ;
do
{
itemStr = fileStream . readLine ( ) ;
if ( fileStream . status ( ) > = QTextStream : : ReadCorruptData )
break ;
trimmedStr = itemStr . trimmed ( ) ;
if ( trimmedStr . isEmpty ( ) )
continue ;
2013-01-17 14:24:13 +00:00
if ( ( unsigned ) trimmedStr . size ( ) < = history . getMaxItemLength ( ) )
2012-09-10 13:01:35 +00:00
itemList . prepend ( trimmedStr ) ;
} while ( ! fileStream . atEnd ( ) & & itemList . size ( ) < ( int ) history . getMaxSize ( ) ) ;
2012-09-10 16:23:58 +00:00
history . enableAdd ( true ) ;
2013-01-17 09:08:53 +00:00
2012-09-10 13:01:35 +00:00
for ( QList < QString > : : const_iterator i = itemList . constBegin ( ) ; i ! = itemList . constEnd ( ) ; + + i )
history . addItem ( History : : Item ( 1 , * i ) ) ;
2013-01-17 09:08:53 +00:00
history . enableAdd ( cfg . preferences . storeHistory ) ;
2012-09-10 13:01:35 +00:00
if ( file . error ( ) ! = QFile : : NoError )
break ;
if ( fileStream . status ( ) > = QTextStream : : ReadCorruptData )
{
errStr = QString ( tr ( " Import error: invalid data in file " ) ) ;
2022-01-27 00:23:57 +00:00
mainStatusBar - > showMessage ( errStr , 10000 , QPixmap ( " :/icons/error.svg " ) ) ;
2012-09-10 13:01:35 +00:00
}
else
mainStatusBar - > showMessage ( tr ( " History import complete " ) , 5000 ) ;
return ;
}
errStr = QString ( tr ( " Import error: " ) ) + file . errorString ( ) ;
file . close ( ) ;
2022-01-27 00:23:57 +00:00
mainStatusBar - > showMessage ( errStr , 10000 , QPixmap ( " :/icons/error.svg " ) ) ;
2012-09-10 13:01:35 +00:00
}
2017-05-05 14:39:51 +00:00
void MainWindow : : on_exportFavorites_triggered ( )
{
QString exportPath ;
if ( cfg . historyExportPath . isEmpty ( ) )
exportPath = QDir : : homePath ( ) ;
else
{
exportPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
if ( ! QDir ( exportPath ) . exists ( ) )
exportPath = QDir : : homePath ( ) ;
}
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Export Favorites to file " ) ,
exportPath ,
tr ( " XML files (*.xml);;All files (*.*) " ) ) ;
if ( fileName . size ( ) = = 0 )
return ;
cfg . historyExportPath = QDir : : toNativeSeparators ( QFileInfo ( fileName ) . absoluteDir ( ) . absolutePath ( ) ) ;
QFile file ( fileName ) ;
for ( ; ; )
{
if ( ! file . open ( QFile : : WriteOnly | QIODevice : : Text ) )
break ;
QByteArray data ;
ui . favoritesPaneWidget - > getDataInXml ( data ) ;
if ( file . write ( data ) ! = data . size ( ) )
break ;
file . close ( ) ;
mainStatusBar - > showMessage ( tr ( " Favorites export complete " ) , 5000 ) ;
return ;
}
QString errStr = QString ( tr ( " Export error: " ) ) + file . errorString ( ) ;
file . close ( ) ;
2022-01-27 00:23:57 +00:00
mainStatusBar - > showMessage ( errStr , 10000 , QPixmap ( " :/icons/error.svg " ) ) ;
2017-05-05 14:39:51 +00:00
}
void MainWindow : : on_ExportFavoritesToList_triggered ( )
{
QString exportPath ;
if ( cfg . historyExportPath . isEmpty ( ) )
exportPath = QDir : : homePath ( ) ;
else
{
exportPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
if ( ! QDir ( exportPath ) . exists ( ) )
exportPath = QDir : : homePath ( ) ;
}
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Export Favorites to file as plain list " ) ,
exportPath ,
tr ( " Text files (*.txt);;All files (*.*) " ) ) ;
if ( fileName . size ( ) = = 0 )
return ;
cfg . historyExportPath = QDir : : toNativeSeparators ( QFileInfo ( fileName ) . absoluteDir ( ) . absolutePath ( ) ) ;
QFile file ( fileName ) ;
for ( ; ; )
{
if ( ! file . open ( QFile : : WriteOnly | QIODevice : : Text ) )
break ;
// Write UTF-8 BOM
QByteArray line ;
line . append ( 0xEF ) . append ( 0xBB ) . append ( 0xBF ) ;
if ( file . write ( line ) ! = line . size ( ) )
break ;
// Write Favorites
QString data ;
ui . favoritesPaneWidget - > getDataInPlainText ( data ) ;
line = data . toUtf8 ( ) ;
if ( file . write ( line ) ! = line . size ( ) )
break ;
file . close ( ) ;
mainStatusBar - > showMessage ( tr ( " Favorites export complete " ) , 5000 ) ;
return ;
}
QString errStr = QString ( tr ( " Export error: " ) ) + file . errorString ( ) ;
file . close ( ) ;
2022-01-27 00:23:57 +00:00
mainStatusBar - > showMessage ( errStr , 10000 , QPixmap ( " :/icons/error.svg " ) ) ;
2017-05-05 14:39:51 +00:00
}
void MainWindow : : on_importFavorites_triggered ( )
{
QString importPath ;
if ( cfg . historyExportPath . isEmpty ( ) )
importPath = QDir : : homePath ( ) ;
else
{
importPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
if ( ! QDir ( importPath ) . exists ( ) )
importPath = QDir : : homePath ( ) ;
}
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Import Favorites from file " ) ,
importPath ,
2022-11-12 04:31:03 +00:00
tr ( " XML files (*.xml);;Txt files (*.txt);;All files (*.*) " ) ) ;
2017-05-05 14:39:51 +00:00
if ( fileName . size ( ) = = 0 )
return ;
QFileInfo fileInfo ( fileName ) ;
cfg . historyExportPath = QDir : : toNativeSeparators ( fileInfo . absoluteDir ( ) . absolutePath ( ) ) ;
QString errStr ;
QFile file ( fileName ) ;
for ( ; ; )
{
if ( ! file . open ( QFile : : ReadOnly | QIODevice : : Text ) )
break ;
if ( file . error ( ) ! = QFile : : NoError )
break ;
QByteArray data = file . readAll ( ) ;
2022-11-12 04:31:03 +00:00
if ( fileInfo . suffix ( ) . compare ( " txt " , Qt : : CaseInsensitive ) = = 0 ) {
if ( ! ui . favoritesPaneWidget - > setDataFromTxt ( QString : : fromUtf8 ( data . data ( ) , data . size ( ) ) ) )
break ;
}
else {
if ( ! ui . favoritesPaneWidget - > setDataFromXml ( QString : : fromUtf8 ( data . data ( ) , data . size ( ) ) ) )
break ;
}
2017-05-05 14:39:51 +00:00
file . close ( ) ;
mainStatusBar - > showMessage ( tr ( " Favorites import complete " ) , 5000 ) ;
return ;
}
if ( file . error ( ) ! = QFile : : NoError )
errStr = QString ( tr ( " Import error: " ) ) + file . errorString ( ) ;
else
errStr = QString ( tr ( " Data parsing error " ) ) ;
file . close ( ) ;
2022-01-27 00:23:57 +00:00
mainStatusBar - > showMessage ( errStr , 10000 , QPixmap ( " :/icons/error.svg " ) ) ;
2017-05-05 14:39:51 +00:00
}
2012-09-10 16:23:58 +00:00
void MainWindow : : fillWordListFromHistory ( )
{
ui . wordList - > setUpdatesEnabled ( false ) ;
ui . wordList - > clear ( ) ;
QList < History : : Item > const & items = history . getItems ( ) ;
for ( int x = 0 ; x < items . size ( ) ; + + x )
{
History : : Item const * i = & items [ x ] ;
QListWidgetItem * s = new QListWidgetItem ( i - > word , ui . wordList ) ;
if ( s - > text ( ) . at ( 0 ) . direction ( ) = = QChar : : DirR )
s - > setTextAlignment ( Qt : : AlignRight ) ;
if ( s - > text ( ) . at ( 0 ) . direction ( ) = = QChar : : DirL )
s - > setTextAlignment ( Qt : : AlignLeft ) ;
ui . wordList - > addItem ( s ) ;
}
ui . wordList - > setUpdatesEnabled ( true ) ;
}
2012-09-12 12:32:29 +00:00
void MainWindow : : focusWordList ( )
{
if ( ui . wordList - > count ( ) > 0 )
ui . wordList - > setFocus ( ) ;
}
2012-09-12 14:11:30 +00:00
2012-11-12 13:52:54 +00:00
void MainWindow : : addWordToHistory ( const QString & word )
{
2013-01-17 09:08:53 +00:00
history . addItem ( History : : Item ( 1 , word . trimmed ( ) ) ) ;
2012-11-12 13:52:54 +00:00
}
2012-09-12 14:11:30 +00:00
void MainWindow : : forceAddWordToHistory ( const QString & word )
{
history . enableAdd ( true ) ;
history . addItem ( History : : Item ( 1 , word . trimmed ( ) ) ) ;
history . enableAdd ( cfg . preferences . storeHistory ) ;
}
2012-09-16 10:19:47 +00:00
void MainWindow : : setExpandMode ( bool expand )
{
articleMaker . setExpandOptionalParts ( expand ) ;
}
void MainWindow : : switchExpandOptionalPartsMode ( )
{
ArticleView * view = getCurrentArticleView ( ) ;
if ( view )
view - > switchExpandOptionalParts ( ) ;
}
2012-09-18 23:01:31 +00:00
2012-09-24 12:51:51 +00:00
void MainWindow : : foundDictsPaneClicked ( QListWidgetItem * item )
2012-09-19 12:44:36 +00:00
{
2018-02-08 15:01:02 +00:00
Qt : : KeyboardModifiers m = QApplication : : keyboardModifiers ( ) ;
if ( ( m & ( Qt : : ControlModifier | Qt : : ShiftModifier ) )
| | ( m = = Qt : : AltModifier ) )
2012-09-19 12:44:36 +00:00
{
QString id = item - > data ( Qt : : UserRole ) . toString ( ) ;
emit clickOnDictPane ( id ) ;
}
2022-01-17 13:50:54 +00:00
2022-01-23 06:19:20 +00:00
jumpToDictionary ( item , true ) ;
2012-09-19 12:44:36 +00:00
}
2012-09-25 13:13:35 +00:00
void MainWindow : : showDictionaryInfo ( const QString & id )
{
2015-03-23 15:58:49 +00:00
QWidget * owner = 0 ;
if ( sender ( ) - > objectName ( ) . compare ( " EditDictionaries " ) = = 0 )
owner = qobject_cast < QWidget * > ( sender ( ) ) ;
if ( owner = = 0 )
owner = this ;
2012-09-25 13:13:35 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + )
{
if ( dictionaries [ x ] - > getId ( ) = = id . toUtf8 ( ) . data ( ) )
{
2012-12-20 13:25:03 +00:00
DictInfo infoMsg ( cfg , this ) ;
2012-09-25 13:13:35 +00:00
infoMsg . showInfo ( dictionaries [ x ] ) ;
2013-06-11 15:40:45 +00:00
int result = infoMsg . exec ( ) ;
if ( result = = DictInfo : : OPEN_FOLDER )
{
openDictionaryFolder ( id ) ;
}
else if ( result = = DictInfo : : EDIT_DICTIONARY )
{
2013-06-11 19:31:58 +00:00
editDictionary ( dictionaries [ x ] . get ( ) ) ;
2013-06-11 15:40:45 +00:00
}
2014-02-28 12:36:28 +00:00
else if ( result = = DictInfo : : SHOW_HEADWORDS )
{
2015-03-23 15:58:49 +00:00
showDictionaryHeadwords ( owner , dictionaries [ x ] . get ( ) ) ;
2014-02-28 12:36:28 +00:00
}
2013-06-11 15:40:45 +00:00
2012-09-25 13:13:35 +00:00
break ;
}
}
}
2014-03-01 13:11:14 +00:00
void MainWindow : : showDictionaryHeadwords ( const QString & id )
{
2015-03-23 15:58:49 +00:00
QWidget * owner = 0 ;
if ( sender ( ) - > objectName ( ) . compare ( " EditDictionaries " ) = = 0 )
owner = qobject_cast < QWidget * > ( sender ( ) ) ;
if ( owner = = 0 )
owner = this ;
2014-03-01 13:11:14 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + )
{
if ( dictionaries [ x ] - > getId ( ) = = id . toUtf8 ( ) . data ( ) )
{
2015-03-23 15:58:49 +00:00
showDictionaryHeadwords ( owner , dictionaries [ x ] . get ( ) ) ;
2014-03-01 13:11:14 +00:00
break ;
}
}
}
2015-03-23 15:58:49 +00:00
void MainWindow : : showDictionaryHeadwords ( QWidget * owner , Dictionary : : Class * dict )
2014-03-01 13:11:14 +00:00
{
2015-03-23 15:58:49 +00:00
if ( owner & & owner ! = this )
{
DictHeadwords headwords ( owner , cfg , dict ) ;
headwords . exec ( ) ;
return ;
}
2014-03-03 13:46:41 +00:00
if ( headwordsDlg = = 0 )
{
headwordsDlg = new DictHeadwords ( this , cfg , dict ) ;
2014-05-11 11:52:25 +00:00
addGlobalActionsToDialog ( headwordsDlg ) ;
2019-07-09 15:00:23 +00:00
addGroupComboBoxActionsToDialog ( headwordsDlg , groupList ) ;
2022-12-26 02:08:17 +00:00
connect ( headwordsDlg , & DictHeadwords : : headwordSelected , this , & MainWindow : : headwordReceived ) ;
connect ( headwordsDlg , & DictHeadwords : : closeDialog , this , & MainWindow : : closeHeadwordsDialog , Qt : : QueuedConnection ) ;
2014-03-03 13:46:41 +00:00
}
else
headwordsDlg - > setup ( dict ) ;
2014-03-01 13:11:14 +00:00
2014-03-03 13:46:41 +00:00
headwordsDlg - > show ( ) ;
}
2014-03-01 13:11:14 +00:00
2014-03-03 13:46:41 +00:00
void MainWindow : : closeHeadwordsDialog ( )
{
if ( headwordsDlg )
{
delete headwordsDlg ;
headwordsDlg = NULL ;
}
2014-03-01 13:11:14 +00:00
}
2014-03-04 16:58:51 +00:00
void MainWindow : : focusHeadwordsDialog ( )
{
if ( headwordsDlg )
2014-05-11 11:52:25 +00:00
{
2014-03-04 16:58:51 +00:00
headwordsDlg - > activateWindow ( ) ;
2014-05-11 11:52:25 +00:00
if ( ftsDlg )
ftsDlg - > lower ( ) ;
}
}
void MainWindow : : focusArticleView ( )
{
ArticleView * view = getCurrentArticleView ( ) ;
if ( view )
{
if ( ! isActiveWindow ( ) )
activateWindow ( ) ;
view - > focus ( ) ;
}
2014-03-04 16:58:51 +00:00
}
2013-06-11 19:31:58 +00:00
void MainWindow : : editDictionary ( Dictionary : : Class * dict )
2013-06-11 15:40:45 +00:00
{
QString dictFilename = dict - > getMainFilename ( ) ;
if ( ! cfg . editDictionaryCommandLine . isEmpty ( ) & & ! dictFilename . isEmpty ( ) )
{
QString command ( cfg . editDictionaryCommandLine ) ;
command . replace ( " %GDDICT% " , " \" " + dictFilename + " \" " ) ;
2018-07-18 16:14:24 +00:00
if ( command . contains ( " %GDWORD% " ) )
{
QString headword = unescapeTabHeader ( ui . tabWidget - > tabText ( ui . tabWidget - > currentIndex ( ) ) ) ;
command . replace ( " %GDWORD% " , headword ) ;
}
2022-01-08 13:16:22 +00:00
if ( ! QProcess : : startDetached ( command , QStringList ( ) ) )
2013-06-11 15:40:45 +00:00
QApplication : : beep ( ) ;
}
}
2013-06-09 13:31:57 +00:00
void MainWindow : : openDictionaryFolder ( const QString & id )
{
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + )
{
if ( dictionaries [ x ] - > getId ( ) = = id . toUtf8 ( ) . data ( ) )
{
if ( dictionaries [ x ] - > getDictionaryFilenames ( ) . size ( ) > 0 )
{
QString fileName = FsEncoding : : decode ( dictionaries [ x ] - > getDictionaryFilenames ( ) [ 0 ] . c_str ( ) ) ;
2013-06-11 10:11:10 +00:00
2022-02-16 00:18:14 +00:00
QString folder = QFileInfo ( fileName ) . absoluteDir ( ) . absolutePath ( ) ;
if ( ! folder . isEmpty ( ) )
QDesktopServices : : openUrl ( QUrl : : fromLocalFile ( folder ) ) ;
2013-06-09 13:31:57 +00:00
}
break ;
}
}
}
2012-09-24 12:51:51 +00:00
void MainWindow : : foundDictsContextMenuRequested ( const QPoint & pos )
{
QListWidgetItem * item = ui . dictsList - > itemAt ( pos ) ;
if ( item )
{
QString id = item - > data ( Qt : : UserRole ) . toString ( ) ;
2013-06-09 13:31:57 +00:00
Dictionary : : Class * pDict = NULL ;
2012-11-29 17:12:54 +00:00
2013-06-09 13:31:57 +00:00
for ( unsigned i = 0 ; i < dictionaries . size ( ) ; i + + )
2012-11-29 17:12:54 +00:00
{
2013-06-09 13:31:57 +00:00
if ( id . compare ( dictionaries [ i ] - > getId ( ) . c_str ( ) ) = = 0 )
2012-11-29 17:12:54 +00:00
{
2013-06-09 13:31:57 +00:00
pDict = dictionaries [ i ] . get ( ) ;
break ;
2012-11-29 17:12:54 +00:00
}
}
2013-06-09 13:31:57 +00:00
if ( pDict = = NULL )
return ;
if ( ! pDict - > isLocalDictionary ( ) )
2012-11-28 19:32:37 +00:00
{
2013-05-30 15:45:20 +00:00
if ( scanPopup )
scanPopup . get ( ) - > blockSignals ( true ) ;
2012-11-28 19:32:37 +00:00
showDictionaryInfo ( id ) ;
2013-05-30 15:45:20 +00:00
if ( scanPopup )
scanPopup . get ( ) - > blockSignals ( false ) ;
2012-11-28 19:32:37 +00:00
}
else
{
QMenu menu ( ui . dictsList ) ;
QAction * infoAction = menu . addAction ( tr ( " Dictionary info " ) ) ;
2013-06-09 13:31:57 +00:00
2014-03-01 13:11:14 +00:00
QAction * headwordsAction = NULL ;
if ( pDict - > getWordCount ( ) > 0 )
headwordsAction = menu . addAction ( tr ( " Dictionary headwords " ) ) ;
2013-06-09 13:31:57 +00:00
QAction * openDictFolderAction = menu . addAction ( tr ( " Open dictionary folder " ) ) ;
QAction * editAction = NULL ;
QString dictFilename = pDict - > getMainFilename ( ) ;
if ( ! cfg . editDictionaryCommandLine . isEmpty ( ) & & ! dictFilename . isEmpty ( ) )
editAction = menu . addAction ( tr ( " Edit dictionary " ) ) ;
2012-11-28 19:32:37 +00:00
QAction * result = menu . exec ( ui . dictsList - > mapToGlobal ( pos ) ) ;
if ( result & & result = = infoAction )
{
2013-05-30 15:45:20 +00:00
if ( scanPopup )
scanPopup . get ( ) - > blockSignals ( true ) ;
2012-11-28 19:32:37 +00:00
showDictionaryInfo ( id ) ;
2013-05-30 15:45:20 +00:00
if ( scanPopup )
scanPopup . get ( ) - > blockSignals ( false ) ;
2012-11-28 19:32:37 +00:00
}
else
2014-03-01 13:11:14 +00:00
if ( result & & result = = headwordsAction )
{
if ( scanPopup )
scanPopup . get ( ) - > blockSignals ( true ) ;
2015-03-23 15:58:49 +00:00
showDictionaryHeadwords ( this , pDict ) ;
2014-03-01 13:11:14 +00:00
if ( scanPopup )
scanPopup . get ( ) - > blockSignals ( false ) ;
}
else
2013-06-09 13:31:57 +00:00
if ( result & & result = = openDictFolderAction )
{
openDictionaryFolder ( id ) ;
}
else
2012-11-28 19:32:37 +00:00
if ( result & & result = = editAction )
{
2013-06-11 19:31:58 +00:00
editDictionary ( pDict ) ;
2012-11-28 19:32:37 +00:00
}
}
2012-09-24 12:51:51 +00:00
}
}
2012-11-26 13:13:13 +00:00
void MainWindow : : sendWordToInputLine ( const QString & word )
{
2021-06-30 16:49:58 +00:00
setTranslateBoxTextAndClearSuffix ( word , EscapeWildcards , NoPopupChange ) ;
2012-11-26 13:13:13 +00:00
}
2013-02-22 12:44:23 +00:00
void MainWindow : : storeResourceSavePath ( const QString & newPath )
{
cfg . resourceSavePath = newPath ;
}
2014-04-03 14:21:02 +00:00
void MainWindow : : proxyAuthentication ( const QNetworkProxy & ,
QAuthenticator * authenticator )
2013-06-08 14:36:05 +00:00
{
2014-04-03 14:21:02 +00:00
QNetworkProxy proxy = QNetworkProxy : : applicationProxy ( ) ;
QString * userStr , * passwordStr ;
if ( cfg . preferences . proxyServer . useSystemProxy )
{
userStr = & cfg . preferences . proxyServer . systemProxyUser ;
passwordStr = & cfg . preferences . proxyServer . systemProxyPassword ;
}
else
{
userStr = & cfg . preferences . proxyServer . user ;
passwordStr = & cfg . preferences . proxyServer . password ;
}
if ( proxy . user ( ) . isEmpty ( ) & & ! userStr - > isEmpty ( ) )
{
authenticator - > setUser ( * userStr ) ;
authenticator - > setPassword ( * passwordStr ) ;
proxy . setUser ( * userStr ) ;
proxy . setPassword ( * passwordStr ) ;
QNetworkProxy : : setApplicationProxy ( proxy ) ;
}
else
{
QDialog dlg ;
Ui : : Dialog ui ;
ui . setupUi ( & dlg ) ;
dlg . adjustSize ( ) ;
ui . userEdit - > setText ( * userStr ) ;
ui . passwordEdit - > setText ( * passwordStr ) ;
if ( dlg . exec ( ) = = QDialog : : Accepted )
{
* userStr = ui . userEdit - > text ( ) ;
* passwordStr = ui . passwordEdit - > text ( ) ;
authenticator - > setUser ( * userStr ) ;
authenticator - > setPassword ( * passwordStr ) ;
proxy . setUser ( * userStr ) ;
proxy . setPassword ( * passwordStr ) ;
QNetworkProxy : : setApplicationProxy ( proxy ) ;
}
}
2013-06-08 14:36:05 +00:00
}
2014-04-16 16:18:28 +00:00
void MainWindow : : showFullTextSearchDialog ( )
{
if ( ! ftsDlg )
{
2014-04-17 14:18:15 +00:00
ftsDlg = new FTS : : FullTextSearchDialog ( this , cfg , dictionaries , groupInstances , ftsIndexing ) ;
2014-05-11 11:52:25 +00:00
addGlobalActionsToDialog ( ftsDlg ) ;
2019-07-09 15:00:23 +00:00
addGroupComboBoxActionsToDialog ( ftsDlg , groupList ) ;
2014-04-16 16:18:28 +00:00
2018-04-10 14:49:52 +00:00
connect ( ftsDlg , SIGNAL ( showTranslationFor ( QString , QStringList , QRegExp , bool ) ) ,
this , SLOT ( showTranslationFor ( QString , QStringList , QRegExp , bool ) ) ) ;
2022-12-26 02:08:17 +00:00
connect ( ftsDlg ,
& FTS : : FullTextSearchDialog : : closeDialog ,
this ,
& MainWindow : : closeFullTextSearchDialog ,
Qt : : QueuedConnection ) ;
2014-04-16 16:18:28 +00:00
connect ( & configEvents , SIGNAL ( mutedDictionariesChanged ( ) ) ,
ftsDlg , SLOT ( updateDictionaries ( ) ) ) ;
2014-05-11 11:52:25 +00:00
unsigned group = groupInstances . empty ( ) ? 0
: groupInstances [ groupList - > currentIndex ( ) ] . id ;
ftsDlg - > setCurrentGroup ( group ) ;
}
2014-04-16 16:18:28 +00:00
2014-04-23 13:47:56 +00:00
if ( ! ftsDlg - > isVisible ( ) )
ftsDlg - > show ( ) ;
else
2014-05-11 11:52:25 +00:00
{
2014-04-23 13:47:56 +00:00
ftsDlg - > activateWindow ( ) ;
2014-05-11 11:52:25 +00:00
if ( headwordsDlg )
headwordsDlg - > lower ( ) ;
}
2014-04-16 16:18:28 +00:00
}
void MainWindow : : closeFullTextSearchDialog ( )
{
if ( ftsDlg )
{
ftsDlg - > stopSearch ( ) ;
delete ftsDlg ;
ftsDlg = 0 ;
}
}
2014-06-23 15:11:15 +00:00
void MainWindow : : showGDHelp ( )
{
if ( ! helpWindow )
2014-06-24 13:55:06 +00:00
{
2014-06-23 15:11:15 +00:00
helpWindow = new Help : : HelpWindow ( this , cfg ) ;
2014-06-24 13:55:06 +00:00
if ( helpWindow - > getHelpEngine ( ) )
{
2022-12-26 02:08:17 +00:00
connect ( helpWindow , & Help : : HelpWindow : : needClose , this , & MainWindow : : hideGDHelp ) ;
2014-06-24 13:55:06 +00:00
helpWindow - > showHelpFor ( " Content " ) ;
helpWindow - > show ( ) ;
}
else
{
delete helpWindow ;
helpWindow = 0 ;
}
2014-06-23 15:11:15 +00:00
}
else
{
2014-06-24 13:55:06 +00:00
helpWindow - > show ( ) ;
helpWindow - > activateWindow ( ) ;
2014-06-23 15:11:15 +00:00
}
}
void MainWindow : : hideGDHelp ( )
{
if ( helpWindow )
helpWindow - > hide ( ) ;
}
2014-06-24 13:55:06 +00:00
void MainWindow : : showGDHelpForID ( QString const & id )
{
if ( ! helpWindow )
showGDHelp ( ) ;
if ( helpWindow )
{
helpWindow - > showHelpFor ( id ) ;
if ( ! helpWindow - > isVisible ( ) )
{
helpWindow - > show ( ) ;
helpWindow - > activateWindow ( ) ;
}
}
}
void MainWindow : : closeGDHelp ( )
{
if ( helpWindow )
{
delete helpWindow ;
helpWindow = 0 ;
}
}
2015-11-01 12:22:18 +00:00
void MainWindow : : showFTSIndexingName ( QString const & name )
{
if ( name . isEmpty ( ) )
mainStatusBar - > setBackgroundMessage ( QString ( ) ) ;
else
mainStatusBar - > setBackgroundMessage ( tr ( " Now indexing for full-text search: " ) + name ) ;
}
2017-11-07 14:46:59 +00:00
QString MainWindow : : unescapeTabHeader ( QString const & header )
{
// Reset table header to original headword
QString escaped = header ;
escaped . replace ( " && " , " & " ) ;
if ( escaped . startsWith ( QChar ( 0x202E ) ) )
escaped = escaped . mid ( 1 ) ;
if ( escaped . endsWith ( QChar ( 0x202C ) ) )
escaped . chop ( 1 ) ;
return escaped ;
}
2017-05-05 14:39:51 +00:00
void MainWindow : : addCurrentTabToFavorites ( )
{
QString folder ;
Instances : : Group const * igrp = groupInstances . findGroup ( cfg . lastMainGroupId ) ;
if ( igrp )
folder = igrp - > favoritesFolder ;
QString headword = ui . tabWidget - > tabText ( ui . tabWidget - > currentIndex ( ) ) ;
2017-11-07 14:46:59 +00:00
ui . favoritesPaneWidget - > addHeadword ( folder , unescapeTabHeader ( headword ) ) ;
2017-10-23 14:21:43 +00:00
addToFavorites - > setIcon ( blueStarIcon ) ;
2017-12-07 15:47:53 +00:00
addToFavorites - > setToolTip ( tr ( " Remove current tab from Favorites " ) ) ;
}
void MainWindow : : handleAddToFavoritesButton ( )
{
QString folder ;
Instances : : Group const * igrp = groupInstances . findGroup ( cfg . lastMainGroupId ) ;
if ( igrp )
folder = igrp - > favoritesFolder ;
QString headword = unescapeTabHeader ( ui . tabWidget - > tabText ( ui . tabWidget - > currentIndex ( ) ) ) ;
if ( ui . favoritesPaneWidget - > isHeadwordPresent ( folder , headword ) )
{
QMessageBox mb ( QMessageBox : : Question , " GoldenDict " , tr ( " Remove headword \" %1 \" from Favorites? " ) . arg ( headword ) ,
QMessageBox : : Yes | QMessageBox : : No , this ) ;
if ( mb . exec ( ) = = QMessageBox : : Yes )
{
if ( ui . favoritesPaneWidget - > removeHeadword ( folder , headword ) )
{
addToFavorites - > setIcon ( starIcon ) ;
addToFavorites - > setToolTip ( tr ( " Add current tab to Favorites " ) ) ;
}
}
}
else
{
ui . favoritesPaneWidget - > addHeadword ( folder , headword ) ;
addToFavorites - > setIcon ( blueStarIcon ) ;
addToFavorites - > setToolTip ( tr ( " Remove current tab from Favorites " ) ) ;
}
2017-05-05 14:39:51 +00:00
}
2017-05-15 15:08:06 +00:00
void MainWindow : : addWordToFavorites ( QString const & word , unsigned groupId )
{
QString folder ;
Instances : : Group const * igrp = groupInstances . findGroup ( groupId ) ;
if ( igrp )
folder = igrp - > favoritesFolder ;
ui . favoritesPaneWidget - > addHeadword ( folder , word ) ;
}
2022-06-01 15:11:41 +00:00
void MainWindow : : addBookmarkToFavorite ( QString const & text )
{
// get current tab word.
QString word = unescapeTabHeader ( ui . tabWidget - > tabText ( ui . tabWidget - > currentIndex ( ) ) ) ;
const auto bookmark = QString ( " %1~~~%2 " ) . arg ( word , text ) ;
ui . favoritesPaneWidget - > addHeadword ( nullptr , bookmark ) ;
}
2017-11-07 14:45:21 +00:00
void MainWindow : : addAllTabsToFavorites ( )
{
QString folder ;
Instances : : Group const * igrp = groupInstances . findGroup ( cfg . lastMainGroupId ) ;
if ( igrp )
folder = igrp - > favoritesFolder ;
for ( int i = 0 ; i < ui . tabWidget - > count ( ) ; i + + )
{
QString headword = ui . tabWidget - > tabText ( i ) ;
2017-11-07 14:46:59 +00:00
ui . favoritesPaneWidget - > addHeadword ( folder , unescapeTabHeader ( headword ) ) ;
2017-11-07 14:45:21 +00:00
}
addToFavorites - > setIcon ( blueStarIcon ) ;
2017-12-07 15:47:53 +00:00
addToFavorites - > setToolTip ( tr ( " Remove current tab from Favorites " ) ) ;
2017-11-07 14:45:21 +00:00
}
2017-10-23 14:21:43 +00:00
bool MainWindow : : isWordPresentedInFavorites ( QString const & word , unsigned groupId )
{
QString folder ;
Instances : : Group const * igrp = groupInstances . findGroup ( groupId ) ;
if ( igrp )
folder = igrp - > favoritesFolder ;
return ui . favoritesPaneWidget - > isHeadwordPresent ( folder , word ) ;
}
2017-03-10 13:29:23 +00:00
void MainWindow : : setGroupByName ( QString const & name , bool main_window )
{
if ( main_window )
{
int i ;
for ( i = 0 ; i < groupList - > count ( ) ; i + + )
{
if ( groupList - > itemText ( i ) = = name )
{
groupList - > setCurrentIndex ( i ) ;
break ;
}
}
if ( i > = groupList - > count ( ) )
gdWarning ( " Group \" %s \" for main window is not found \n " , name . toUtf8 ( ) . data ( ) ) ;
}
else
{
emit setPopupGroupByName ( name ) ;
}
}
2017-05-05 14:39:51 +00:00
void MainWindow : : headwordFromFavorites ( QString const & headword ,
QString const & favoritesFolder )
{
if ( ! favoritesFolder . isEmpty ( ) )
{
// Find group by it Favorites folder
for ( Instances : : Groups : : size_type i = 0 ; i < groupInstances . size ( ) ; i + + )
{
if ( groupInstances [ i ] . favoritesFolder = = favoritesFolder )
{
// Group found. Select it and stop search.
if ( groupList - > currentIndex ( ) ! = ( int ) i )
2017-05-12 19:32:37 +00:00
{
2017-05-05 14:39:51 +00:00
groupList - > setCurrentIndex ( i ) ;
2017-05-12 19:32:37 +00:00
// Restore focus on Favorites tree
ui . favoritesPaneWidget - > setFocusOnTree ( ) ;
}
2017-05-05 14:39:51 +00:00
break ;
}
}
}
2017-05-12 19:32:37 +00:00
// Show headword without lost of focus on Favorites tree
2022-06-01 15:11:41 +00:00
// bookmark cases: the favorite item may like this "word~~~selectedtext"
auto words = headword . split ( " ~~~ " ) ;
setTranslateBoxTextAndClearSuffix ( words [ 0 ] , EscapeWildcards , DisablePopup ) ;
//must be a bookmark.
if ( words . size ( ) > 1 )
{
auto view = getCurrentArticleView ( ) ;
if ( view )
{
view - > setDelayedHighlightText ( words [ 1 ] ) ; // findText( words[ 1 ], QWebEnginePage::FindCaseSensitively );
}
}
showTranslationFor ( words [ 0 ] ) ;
2017-05-05 14:39:51 +00:00
}
2012-09-18 23:01:31 +00:00
# ifdef Q_OS_WIN32
2012-09-24 13:20:58 +00:00
bool MainWindow : : isGoldenDictWindow ( HWND hwnd )
{
2014-04-11 16:44:04 +00:00
return hwnd = = ( HWND ) winId ( ) | | hwnd = = ( HWND ) ui . centralWidget - > winId ( ) ;
2012-09-24 13:20:58 +00:00
}
2012-09-18 23:01:31 +00:00
# endif