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 */
2023-07-27 00:48:50 +00:00
# include <Qt>
2023-06-03 12:51:38 +00:00
# include <QScopeGuard>
2014-05-20 13:59:56 +00:00
# ifndef NO_EPWING_SUPPORT
2023-06-03 12:51:38 +00:00
# include "dict/epwing_book.hh"
2014-05-20 13:59:56 +00:00
# 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"
2023-04-17 22:51:08 +00:00
# include "dict/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"
2013-05-30 13:24:21 +00:00
# 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>
2023-04-17 15:30:43 +00:00
# include <QStyleFactory>
2023-06-05 13:22:20 +00:00
# include "weburlrequestinterceptor.hh"
# include "folding.hh"
2013-05-30 13:24:21 +00:00
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"
2013-01-17 09:08:53 +00:00
# include "historypanewidget.hh"
2021-11-27 07:17:33 +00:00
# include "utils.hh"
2023-04-22 00:43:50 +00:00
# include "help.hh"
2014-04-03 14:21:02 +00:00
# include "ui_authentication.h"
2023-04-17 20:12:27 +00:00
# include "resourceschemehandler.hh"
2023-06-05 13:22:20 +00:00
# include <QListWidgetItem>
2009-01-28 20:55:45 +00:00
2023-04-18 00:41:47 +00:00
# include "globalregex.hh"
2022-11-19 18:48:26 +00:00
2012-06-03 18:29:49 +00:00
# ifdef Q_OS_MAC
2023-06-03 12:51:38 +00:00
# include "macos/macmouseover.hh"
2012-06-03 18:29:49 +00:00
# endif
2012-09-18 23:01:31 +00:00
# ifdef Q_OS_WIN32
2023-06-03 12:51:38 +00:00
# include <windows.h>
2012-09-18 23:01:31 +00:00
# endif
2022-05-23 12:14:26 +00:00
# include <QWebEngineSettings>
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
2023-06-03 12:51:38 +00:00
# if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
# include <QGuiApplication>
# else
# include <QX11Info>
# endif
# include <X11/Xlib.h>
# 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 : : map ;
using std : : pair ;
2022-12-15 07:11:09 +00:00
# ifndef QT_NO_SSL
2013-06-07 13:00:30 +00:00
2023-06-03 12:51:38 +00:00
class InitSSLRunnable : public QRunnable
2013-06-07 13:00:30 +00:00
{
2022-12-29 07:07:40 +00:00
void run ( ) override
2013-06-07 13:00:30 +00:00
{
/// This action force SSL library initialisation which may continue a few seconds
QSslConfiguration : : setDefaultConfiguration ( QSslConfiguration : : defaultConfiguration ( ) ) ;
}
} ;
# endif
2023-06-09 11:56:51 +00:00
namespace {
QString ApplicationSettingName = " GoldenDict " ;
}
void MainWindow : : changeWebEngineViewFont ( ) const
2022-05-23 12:14:26 +00:00
{
2023-07-16 10:54:07 +00:00
if ( cfg . preferences . customFonts . standard . isEmpty ( ) ) {
2023-05-15 23:27:19 +00:00
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > resetFontFamily ( QWebEngineSettings : : StandardFont ) ;
2022-05-23 12:14:26 +00:00
}
2023-06-03 12:51:38 +00:00
else {
2023-05-15 23:27:19 +00:00
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > setFontFamily ( QWebEngineSettings : : StandardFont ,
2023-07-16 10:54:07 +00:00
cfg . preferences . customFonts . standard ) ;
}
if ( cfg . preferences . customFonts . serif . isEmpty ( ) ) {
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > resetFontFamily ( QWebEngineSettings : : SerifFont ) ;
}
else {
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > setFontFamily ( QWebEngineSettings : : SerifFont ,
cfg . preferences . customFonts . serif ) ;
}
if ( cfg . preferences . customFonts . sansSerif . isEmpty ( ) ) {
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > resetFontFamily ( QWebEngineSettings : : SansSerifFont ) ;
}
else {
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > setFontFamily ( QWebEngineSettings : : SansSerifFont ,
cfg . preferences . customFonts . sansSerif ) ;
}
if ( cfg . preferences . customFonts . monospace . isEmpty ( ) ) {
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > resetFontFamily ( QWebEngineSettings : : FixedFont ) ;
}
else {
QWebEngineProfile : : defaultProfile ( ) - > settings ( ) - > setFontFamily ( QWebEngineSettings : : FixedFont ,
cfg . preferences . customFonts . monospace ) ;
2022-05-23 12:14:26 +00:00
}
}
2009-04-12 20:46:25 +00:00
MainWindow : : MainWindow ( Config : : Class & cfg_ ) :
2023-06-03 12:51:38 +00:00
trayIcon ( nullptr ) ,
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 ) ,
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 ) ,
2023-03-21 07:58:58 +00:00
articleMaker ( dictionaries , groupInstances , cfg . preferences ) ,
2023-05-22 23:50:37 +00:00
articleNetMgr ( this ,
dictionaries ,
articleMaker ,
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 ) ,
2023-05-22 23:50:37 +00:00
wordListSelChanged ( false ) ,
wasMaximized ( false ) ,
blockUpdateWindowTitle ( false ) ,
2023-06-03 12:51:38 +00:00
headwordsDlg ( nullptr ) ,
2023-05-22 23:50:37 +00:00
ftsIndexing ( dictionaries ) ,
2023-06-03 12:51:38 +00:00
ftsDlg ( nullptr ) ,
2023-05-22 23:50:37 +00:00
starIcon ( " :/icons/star.svg " ) ,
blueStarIcon ( " :/icons/star_blue.svg " )
2009-01-28 20:55:45 +00:00
{
2023-06-03 12:51:38 +00:00
if ( QThreadPool : : globalInstance ( ) - > maxThreadCount ( ) < MIN_THREAD_COUNT )
2014-04-16 16:18:28 +00:00
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
2023-06-03 12:51:38 +00:00
GlobalBroadcaster : : instance ( ) - > setPreference ( & cfg . preferences ) ;
2021-08-05 06:57:22 +00:00
2023-06-03 12:51:38 +00:00
localSchemeHandler = new LocalSchemeHandler ( articleNetMgr , this ) ;
2023-05-15 23:27:19 +00:00
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " gdlookup " , localSchemeHandler ) ;
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " bword " , localSchemeHandler ) ;
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " entry " , localSchemeHandler ) ;
2022-05-13 16:00:23 +00:00
iframeSchemeHandler = new IframeSchemeHandler ( this ) ;
2023-05-15 23:27:19 +00:00
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( " ifr " , iframeSchemeHandler ) ;
2022-05-13 16:00:23 +00:00
QStringList localSchemes = { " gdau " , " gico " , " qrcx " , " bres " , " gdprg " , " gdvideo " , " gdpicture " , " gdtts " } ;
2023-06-03 12:51:38 +00:00
resourceSchemeHandler = new ResourceSchemeHandler ( articleNetMgr , this ) ;
for ( const auto & localScheme : localSchemes ) {
QWebEngineProfile : : defaultProfile ( ) - > installUrlSchemeHandler ( localScheme . toLatin1 ( ) , resourceSchemeHandler ) ;
2021-09-21 10:22:08 +00:00
}
2021-08-05 06:57:22 +00:00
2023-06-03 12:51:38 +00:00
QWebEngineProfile : : defaultProfile ( ) - > setUrlRequestInterceptor ( new WebUrlRequestInterceptor ( this ) ) ;
2021-12-11 16:34:37 +00:00
2023-06-03 12:51:38 +00:00
if ( ! cfg . preferences . hideGoldenDictHeader ) {
QWebEngineProfile : : defaultProfile ( ) - > setHttpUserAgent ( QWebEngineProfile : : defaultProfile ( ) - > httpUserAgent ( )
+ " GoldenDict/WebEngine " ) ;
2022-02-08 04:55:15 +00:00
}
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 ) ;
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
2023-06-03 12:51:38 +00:00
setStatusBar ( nullptr ) ;
2011-06-27 18:54:15 +00:00
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
2023-06-03 12:51:38 +00:00
QWidget * translateBoxWidget = new QWidget ( this ) ;
2013-02-07 15:24:24 +00:00
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 " ) ;
2023-03-04 05:21:21 +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
2023-04-29 08:04:56 +00:00
connect ( navPronounce , & QAction : : triggered , [ this ] ( ) {
getCurrentArticleView ( ) - > playSound ( ) ;
} ) ;
2009-04-10 21:07:03 +00:00
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 " ) ) ;
2023-06-03 12:51:38 +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 " ) ;
2023-06-03 12:51:38 +00:00
beforeOptionsSeparator - > setVisible ( cfg . preferences . hideMenubar ) ;
2012-12-29 18:46:43 +00:00
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 ) ;
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +00:00
menuButtonAction = navToolbar - > addWidget ( menuButton ) ;
2012-12-29 18:46:43 +00:00
menuButtonAction - > setVisible ( cfg . preferences . hideMenubar ) ;
2012-12-24 11:30:03 +00:00
// Make the search pane's titlebar
2023-05-22 23:50:37 +00:00
// groupLabel.setText( tr( "Look up in:" ) );
2012-12-24 11:30:03 +00:00
groupListInDock = new GroupComboBox ( & searchPaneTitleBar ) ;
2023-06-10 06:45:48 +00:00
searchPaneTitleBarLayout . setContentsMargins ( 3 , 5 , 3 , 5 ) ;
2023-05-22 23:50:37 +00:00
// searchPaneTitleBarLayout.addWidget( &groupLabel );
2012-12-24 11:30:03 +00:00
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 ) {
2023-06-03 12:51:38 +00:00
groupList = groupListInDock ;
2012-12-24 11:30:03 +00:00
translateLine = ui . translateLine ;
}
2023-06-03 12:51:38 +00:00
else {
groupList = groupListInToolbar ;
2012-12-24 11:30:03 +00:00
translateLine = translateBox - > translateLine ( ) ;
}
2023-06-05 13:22:20 +00:00
connect ( & wordFinder , & WordFinder : : updated , this , & MainWindow : : prefixMatchUpdated ) ;
connect ( & wordFinder , & WordFinder : : finished , this , & MainWindow : : prefixMatchFinished ) ;
2013-01-25 15:42:44 +00:00
2012-12-24 11:30:03 +00:00
2023-06-03 12:51:38 +00:00
groupList - > setFocusPolicy ( Qt : : ClickFocus ) ;
2023-06-05 13:22:20 +00:00
ui . wordList - > setFocusPolicy ( Qt : : ClickFocus ) ;
2012-12-24 11:30:03 +00:00
2023-06-05 13:22:20 +00:00
wordListDefaultFont = ui . wordList - > font ( ) ;
2012-12-24 11:30:03 +00:00
translateLineDefaultFont = translateLine - > font ( ) ;
2023-06-03 12:51:38 +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 ) ;
2023-06-03 12:51:38 +00:00
dictsPaneTitleBarLayout . setContentsMargins ( 5 , 5 , 5 , 5 ) ;
2012-12-24 11:30:03 +00:00
dictsPaneTitleBar . setLayout ( & dictsPaneTitleBarLayout ) ;
2023-06-03 12:51:38 +00:00
dictsPaneTitleBar . setObjectName ( " dictsPaneTitleBar " ) ;
2012-12-24 11:30:03 +00:00
ui . dictsPane - > setTitleBarWidget ( & dictsPaneTitleBar ) ;
ui . dictsList - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2023-04-29 10:04:57 +00:00
connect ( ui . dictsPane , & QDockWidget : : visibilityChanged , this , & MainWindow : : dictsPaneVisibilityChanged ) ;
2012-12-24 11:30:03 +00:00
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 " ) ) ;
2023-06-03 12:51:38 +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 " ) ) ,
2023-06-03 12:51:38 +00:00
& 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
2023-06-11 07:44:07 +00:00
addGlobalAction ( & escAction , [ this ] ( ) {
handleEsc ( ) ;
} ) ;
2010-04-27 07:49:37 +00:00
escAction . setShortcut ( QKeySequence ( " Esc " ) ) ;
2023-06-11 07:44:07 +00:00
addGlobalAction ( & focusTranslateLineAction , [ this ] ( ) {
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
2023-06-11 07:44:07 +00:00
addGlobalAction ( & focusHeadwordsDlgAction , [ this ] ( ) {
focusHeadwordsDialog ( ) ;
} ) ;
2014-03-04 16:58:51 +00:00
focusHeadwordsDlgAction . setShortcut ( QKeySequence ( " Ctrl+D " ) ) ;
2023-06-11 07:44:07 +00:00
addGlobalAction ( & focusArticleViewAction , [ this ] ( ) {
focusArticleView ( ) ;
} ) ;
2014-05-11 11:52:25 +00:00
focusArticleViewAction . setShortcut ( QKeySequence ( " Ctrl+N " ) ) ;
2023-06-11 07:44:07 +00:00
addGlobalAction ( ui . fullTextSearchAction , [ this ] ( ) {
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
2023-06-03 12:51:38 +00:00
tabListMenu = new MRUQMenu ( tr ( " Opened tabs " ) , ui . tabWidget ) ;
2011-11-02 23:37:50 +00:00
2023-04-12 08:11:36 +00:00
connect ( tabListMenu , & MRUQMenu : : requestTabChange , ui . tabWidget , & MainTabWidget : : setCurrentIndex ) ;
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 " ) ) ;
2023-06-03 12:51:38 +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 " ) ) ;
2023-06-03 12:51:38 +00:00
closeAllTabAction . setText ( tr ( " Close all tabs " ) ) ;
2010-09-16 18:52:40 +00:00
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 ) ;
2023-06-03 12:51:38 +00:00
closeRestTabAction . setText ( tr ( " Close all tabs except current " ) ) ;
2010-09-16 18:52:40 +00:00
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
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
2023-06-03 12:51:38 +00:00
tabMenu = new QMenu ( this ) ;
2010-09-16 18:52:40 +00:00
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 ( ) ) ;
2013-01-17 09:08:53 +00:00
ui . menuView - > addAction ( ui . historyPane - > toggleViewAction ( ) ) ;
2022-12-27 06:52:12 +00:00
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 ) ;
2023-06-03 12:51:38 +00:00
if ( cfg . lastMainGroupId = = Instances : : Group : : AllGroupId ) {
if ( igrp )
2012-09-26 13:13:47 +00:00
igrp - > checkMutedDictionaries ( & cfg . mutedDictionaries ) ;
dictionaryBar . setMutedDictionaries ( & cfg . mutedDictionaries ) ;
}
2023-06-03 12:51:38 +00:00
else {
2012-09-26 13:13:47 +00:00
Config : : Group * grp = cfg . getGroup ( cfg . lastMainGroupId ) ;
2023-06-03 12:51:38 +00:00
if ( igrp & & grp )
2012-09-26 13:13:47 +00:00
igrp - > checkMutedDictionaries ( & grp - > mutedDictionaries ) ;
2023-07-17 00:24:02 +00:00
dictionaryBar . setMutedDictionaries ( grp ? & grp - > mutedDictionaries : nullptr ) ;
2012-09-26 13:13:47 +00:00
}
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
2023-04-29 10:04:57 +00:00
connect ( this , & MainWindow : : clickOnDictPane , & dictionaryBar , & DictionaryBar : : dictsPaneClicked ) ;
2012-09-19 12:44:36 +00:00
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
2023-06-03 12:51:38 +00:00
connect ( & dictionaryBar , & DictionaryBar : : showDictionaryHeadwords , this , & MainWindow : : showDictionaryHeadwords ) ;
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 ) ;
2023-06-03 12:51:38 +00:00
connect ( ui . showHideFavorites , & QAction : : triggered , this , & MainWindow : : toggle_favoritesPane ) ;
2017-05-05 14:39:51 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . favoritesPaneWidget ,
2023-06-03 12:51:38 +00:00
& 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 ) ;
2022-12-27 06:52:12 +00:00
connect ( ui . showHideHistory , & QAction : : triggered , this , & MainWindow : : toggle_historyPane ) ;
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
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . enableTrayIcon ) {
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 ) ;
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +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 ) ;
2023-04-17 02:23:39 +00:00
connect ( ui . showReference , & QAction : : triggered , [ ] ( ) {
2023-04-22 00:43:50 +00:00
Help : : openHelpWebpage ( ) ;
2023-04-17 02:23:39 +00:00
} ) ;
2009-02-05 20:55:00 +00:00
2023-06-03 12:51:38 +00:00
connect ( groupListInDock , & GroupComboBox : : currentIndexChanged , this , & MainWindow : : currentGroupChanged ) ;
2012-12-24 11:30:03 +00:00
2023-06-03 12:51:38 +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
2023-06-05 13:22:20 +00:00
connect ( translateBox - > translateLine ( ) , & QLineEdit : : textEdited , this , & MainWindow : : translateInputChanged ) ;
2012-12-24 11:30:03 +00:00
2023-05-30 17:22:24 +00:00
connect ( ui . translateLine , & QLineEdit : : returnPressed , [ this ] ( ) {
translateInputFinished ( true ) ;
} ) ;
connect ( translateBox - > translateLine ( ) , & QLineEdit : : returnPressed , [ this ] ( ) {
translateInputFinished ( true ) ;
} ) ;
2012-12-24 11:30:03 +00:00
2022-12-26 02:08:17 +00:00
connect ( ui . wordList , & QListWidget : : itemSelectionChanged , this , & MainWindow : : wordListSelectionChanged ) ;
2012-12-24 21:56:31 +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 . 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
2011-06-07 08:26:49 +00:00
ui . wordList - > viewport ( ) - > installEventFilter ( this ) ;
2012-12-24 11:30:03 +00:00
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 ) ;
2023-05-28 16:01:21 +00:00
connect ( GlobalBroadcaster : : instance ( ) ,
& GlobalBroadcaster : : indexingDictionary ,
this ,
& MainWindow : : showFTSIndexingName ) ;
2015-11-01 12:22:18 +00:00
2023-07-13 06:49:41 +00:00
connect ( & GlobalBroadcaster : : instance ( ) - > pronounce_engine ,
& PronounceEngine : : emitAudio ,
this ,
[ this ] ( auto audioUrl ) {
auto view = getCurrentArticleView ( ) ;
2023-07-19 05:44:26 +00:00
if ( ( cfg . preferences . pronounceOnLoadMain | | cfg . preferences . pronounceOnLoadPopup ) & & view ! = nullptr ) {
2023-07-13 06:49:41 +00:00
view - > openLink ( QUrl : : fromEncoded ( audioUrl . toUtf8 ( ) ) , { } ) ;
}
} ) ;
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 ,
2023-06-03 12:51:38 +00:00
& 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
2023-06-03 12:51:38 +00:00
// historyChanged();
2009-10-21 19:37:07 +00:00
2023-04-17 09:11:18 +00:00
setWindowTitle ( " GoldenDict-ng " ) ;
2013-01-31 13:01:08 +00:00
blockUpdateWindowTitle = true ;
2009-01-29 19:16:25 +00:00
2010-09-16 18:52:40 +00:00
// Create tab list menu
createTabList ( ) ;
2023-03-20 14:54:04 +00:00
2023-07-20 12:10:44 +00:00
# if defined( Q_OS_LINUX )
# if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
defaultInterfaceStyle = QApplication : : style ( ) - > name ( ) ;
# else
defaultInterfaceStyle = QApplication : : style ( ) - > objectName ( ) ;
# endif
# elif defined( Q_OS_MAC )
defaultInterfaceStyle = " Fusion " ;
# endif
updateAppearances ( cfg . preferences . addonStyle ,
cfg . preferences . displayStyle ,
cfg . preferences . darkMode
# if !defined( Q_OS_WIN )
,
cfg . preferences . interfaceStyle
# endif
) ;
2009-02-08 18:35:29 +00:00
// Show the initial welcome text
2023-07-20 12:10:44 +00:00
addNewTab ( ) ;
2023-07-11 13:36:23 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
history . enableAdd ( false ) ;
blockUpdateWindowTitle = true ;
view - > showDefinition ( tr ( " Welcome! " ) , Instances : : Group : : HelpGroupId ) ;
history . enableAdd ( cfg . preferences . storeHistory ) ;
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
2023-02-23 00:44:29 +00:00
// Scanpopup related
2023-06-03 12:51:38 +00:00
scanPopup =
new ScanPopup ( nullptr , cfg , articleNetMgr , audioPlayerFactory . player ( ) , dictionaries , groupInstances , history ) ;
2023-02-23 00:44:29 +00:00
2023-06-03 12:51:38 +00:00
scanPopup - > setStyleSheet ( styleSheet ( ) ) ;
2023-02-23 00:44:29 +00:00
2023-06-11 11:51:24 +00:00
connect ( scanPopup , & ScanPopup : : editGroupRequest , this , & MainWindow : : editDictionaries , Qt : : QueuedConnection ) ;
2023-02-23 00:44:29 +00:00
2023-05-28 16:01:54 +00:00
connect ( scanPopup , & ScanPopup : : sendPhraseToMainWindow , this , [ this ] ( QString const & word ) {
wordReceived ( word ) ;
2023-05-09 13:02:20 +00:00
} ) ;
2023-02-23 00:44:29 +00:00
2023-06-03 12:51:38 +00:00
connect ( scanPopup , & ScanPopup : : inspectSignal , this , & MainWindow : : inspectElement ) ;
2023-02-23 00:44:29 +00:00
connect ( scanPopup , & ScanPopup : : forceAddWordToHistory , this , & MainWindow : : forceAddWordToHistory ) ;
connect ( scanPopup , & ScanPopup : : showDictionaryInfo , this , & MainWindow : : showDictionaryInfo ) ;
2023-06-03 12:51:38 +00:00
connect ( scanPopup , & ScanPopup : : openDictionaryFolder , this , & MainWindow : : openDictionaryFolder ) ;
2023-02-23 00:44:29 +00:00
connect ( scanPopup , & ScanPopup : : sendWordToHistory , this , & MainWindow : : addWordToHistory ) ;
connect ( this , & MainWindow : : setPopupGroupByName , scanPopup , & ScanPopup : : setGroupByName ) ;
connect ( scanPopup , & ScanPopup : : sendWordToFavorites , this , & MainWindow : : addWordToFavorites ) ;
2023-03-07 03:40:47 +00:00
# ifdef Q_OS_MAC
2023-06-03 12:51:38 +00:00
macClipboard = new gd_clipboard ( this ) ;
connect ( macClipboard , & gd_clipboard : : changed , this , & MainWindow : : clipboardChange ) ;
2023-03-07 03:40:47 +00:00
# endif
connect ( enableScanningAction , & QAction : : toggled , this , [ = ] ( bool on ) {
2023-06-03 12:51:38 +00:00
if ( on ) {
2023-03-07 03:40:47 +00:00
enableScanningAction - > setIcon ( QIcon ( " :/icons/wizard-selected.svg " ) ) ;
}
2023-06-03 12:51:38 +00:00
else {
2023-03-07 03:40:47 +00:00
enableScanningAction - > setIcon ( QIcon ( " :/icons/wizard.svg " ) ) ;
}
# ifdef Q_OS_MAC
2023-06-03 12:51:38 +00:00
if ( ! MacMouseOver : : isAXAPIEnabled ( ) )
2023-03-07 03:40:47 +00:00
mainStatusBar - > showMessage ( tr ( " Accessibility API is not enabled " ) , 10000 , QPixmap ( " :/icons/error.svg " ) ) ;
2023-06-03 12:51:38 +00:00
if ( on ) {
2023-03-07 03:40:47 +00:00
macClipboard - > start ( ) ;
}
2023-06-03 12:51:38 +00:00
else {
2023-03-07 03:40:47 +00:00
macClipboard - > stop ( ) ;
}
# else
2023-06-03 12:51:38 +00:00
if ( on ) {
connect ( QApplication : : clipboard ( ) , & QClipboard : : changed , this , & MainWindow : : clipboardChange ) ;
}
else {
disconnect ( QApplication : : clipboard ( ) , & QClipboard : : changed , this , & MainWindow : : clipboardChange ) ;
}
2023-03-07 03:40:47 +00:00
# endif
installHotKeys ( ) ;
updateTrayIcon ( ) ;
} ) ;
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . startWithScanPopupOn ) {
2023-03-07 03:40:47 +00:00
enableScanningAction - > trigger ( ) ;
}
2023-02-23 00:44:29 +00:00
updateSearchPaneAndBar ( cfg . preferences . searchInDock ) ;
ui . searchPane - > setVisible ( cfg . preferences . searchInDock ) ;
2019-05-13 15:17:27 +00:00
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
2023-06-03 12:51:38 +00:00
setAutostart ( cfg . preferences . autoStart ) ;
2009-04-18 18:47:01 +00:00
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 ) {
show ( ) ;
2009-05-13 13:47:51 +00:00
focusTranslateLine ( ) ;
}
2009-04-20 19:54:34 +00:00
2011-06-26 11:53:28 +00:00
if ( cfg . preferences . hideMenubar ) {
toggleMenuBarTriggered ( false ) ;
}
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
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . startWithScanPopupOn & & ! MacMouseOver : : isAXAPIEnabled ( ) )
mainStatusBar - > showMessage ( tr ( " Accessibility API is not enabled " ) , 10000 , 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
2023-06-03 12:51:38 +00:00
if ( layoutDirection ( ) = = Qt : : RightToLeft ) {
2014-03-20 13:44:46 +00:00
// 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
2023-06-03 12:51:38 +00:00
inspector . reset ( new ArticleInspector ( this ) ) ;
2022-08-02 14:27:14 +00:00
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\ */
2023-06-03 12:51:38 +00:00
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 \" " ) ;
2022-11-28 01:24:49 +00:00
urlRegistry . endGroup ( ) ;
# endif
2023-02-23 00:44:29 +00:00
2023-07-28 03:20:18 +00:00
// restore should be called after all UI initialized.
if ( cfg . mainWindowState . size ( ) & & ! cfg . resetState )
restoreState ( cfg . mainWindowState ) ;
if ( cfg . mainWindowGeometry . size ( ) )
restoreGeometry ( cfg . mainWindowGeometry ) ;
2023-02-23 00:44:29 +00:00
useSmallIconsInToolbarsTriggered ( ) ;
2023-07-16 07:33:22 +00:00
if ( cfg . preferences . checkForNewReleases ) {
QTimer : : singleShot ( 0 , this , & MainWindow : : checkNewRelease ) ;
}
2022-08-02 14:27:14 +00:00
}
2023-06-05 13:22:20 +00:00
void MainWindow : : prefixMatchUpdated ( )
{
updateMatchResults ( false ) ;
}
void MainWindow : : prefixMatchFinished ( )
{
updateMatchResults ( true ) ;
}
void MainWindow : : updateMatchResults ( bool finished )
{
WordFinder : : SearchResults const & results = wordFinder . getResults ( ) ;
if ( cfg . preferences . searchInDock ) {
ui . wordList - > setUpdatesEnabled ( false ) ;
//clear all existed items
ui . wordList - > clear ( ) ;
for ( const auto & result : results ) {
auto i = new QListWidgetItem ( result . first , ui . wordList ) ;
i - > setToolTip ( result . first ) ;
if ( result . second ) {
QFont f = i - > font ( ) ;
f . setItalic ( true ) ;
i - > setFont ( f ) ;
}
i - > setTextAlignment ( Qt : : AlignLeft ) ;
}
if ( ui . wordList - > count ( ) ) {
ui . wordList - > scrollToItem ( ui . wordList - > item ( 0 ) , QAbstractItemView : : PositionAtTop ) ;
ui . wordList - > setCurrentItem ( nullptr , QItemSelectionModel : : Clear ) ;
}
ui . wordList - > setUpdatesEnabled ( true ) ;
ui . wordList - > unsetCursor ( ) ;
}
else {
QStringList _results ;
_results . clear ( ) ;
for ( const auto & result : results ) {
_results < < result . first ;
}
translateBox - > setModel ( _results ) ;
}
if ( finished ) {
refreshTranslateLine ( ) ;
if ( ! wordFinder . getErrorString ( ) . isEmpty ( ) )
emit showStatusBarMessage ( tr ( " WARNING: %1 " ) . arg ( wordFinder . getErrorString ( ) ) ,
20000 ,
QPixmap ( " :/icons/error.svg " ) ) ;
}
}
void MainWindow : : refreshTranslateLine ( )
{
if ( ! translateLine )
return ;
// Visually mark the input line to mark if there's no results
bool setMark = wordFinder . getResults ( ) . empty ( ) & & ! wordFinder . wasSearchUncertain ( ) ;
if ( translateLine - > property ( " noResults " ) . toBool ( ) ! = setMark ) {
translateLine - > setProperty ( " noResults " , setMark ) ;
Utils : : Widget : : setNoResultColor ( translateLine , setMark ) ;
}
}
2023-06-03 12:51:38 +00:00
void MainWindow : : clipboardChange ( QClipboard : : Mode m )
2022-08-02 14:27:14 +00:00
{
2023-06-03 12:51:38 +00:00
if ( ! scanPopup ) {
2023-03-07 01:42:23 +00:00
return ;
}
2023-02-23 00:44:29 +00:00
2023-06-03 12:51:38 +00:00
# if defined( HAVE_X11 )
if ( m = = QClipboard : : Clipboard ) {
if ( ! cfg . preferences . trackClipboardScan )
return ;
scanPopup - > translateWordFromClipboard ( ) ;
return ;
}
2022-11-19 18:48:26 +00:00
2023-06-03 12:51:38 +00:00
if ( m = = QClipboard : : Selection ) {
2022-11-20 05:39:41 +00:00
2023-06-03 12:51:38 +00:00
// Multiple ways to stopping a word from showing up when selecting
2022-11-20 05:39:41 +00:00
2023-06-03 12:51:38 +00:00
// Explicitly disabled on preferences
if ( ! cfg . preferences . trackSelectionScan )
return ;
2022-11-20 05:39:41 +00:00
2023-06-03 12:51:38 +00:00
// Explicitly disabled on preferences to ignore gd's own selection
2022-12-29 02:19:58 +00:00
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . ignoreOwnClipboardChanges & & QApplication : : clipboard ( ) - > ownsSelection ( ) ) {
return ;
}
2022-12-29 02:19:58 +00:00
2023-06-03 12:51:38 +00:00
// Keyboard Modifier
if ( cfg . preferences . enableScanPopupModifiers
& & ! KeyboardState : : checkModifiersPressed ( cfg . preferences . scanPopupModifiers ) ) {
return ;
}
2022-11-20 05:39:41 +00:00
2023-06-03 12:51:38 +00:00
// Show a Flag instead of translate directly.
// And hand over the control of showing the popup to scanFlag
if ( cfg . preferences . showScanFlag ) {
scanPopup - > showScanFlag ( ) ;
return ;
}
2022-11-20 05:39:41 +00:00
2023-06-03 12:51:38 +00:00
// Use delay show to prevent multiple popups while selection in progress
scanPopup - > selectionDelayTimer . start ( ) ;
}
# elif defined( Q_OS_MAC )
scanPopup - > translateWord ( macClipboard - > text ( ) ) ;
2022-11-20 02:54:22 +00:00
# else
2023-06-03 12:51:38 +00:00
scanPopup - > translateWordFromClipboard ( ) ;
2022-11-20 02:54:22 +00:00
# endif
2009-01-28 20:55:45 +00:00
}
2011-11-02 23:37:50 +00:00
void MainWindow : : ctrlTabPressed ( )
{
2023-06-03 12:51:38 +00:00
emit fillWindowsMenu ( ) ;
tabListButton - > click ( ) ;
2011-11-02 23:37:50 +00:00
}
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 ( ) ;
2023-06-03 12:51:38 +00:00
if ( ftsDlg )
2019-07-09 15:00:23 +00:00
removeGroupComboBoxActionsFromDialog ( ftsDlg , groupList ) ;
2023-06-03 12:51:38 +00:00
if ( headwordsDlg )
2019-07-09 15:00:23 +00:00
removeGroupComboBoxActionsFromDialog ( headwordsDlg , groupList ) ;
2013-01-05 11:26:55 +00:00
if ( searchInDock ) {
cfg . preferences . searchInDock = true ;
2012-12-24 11:30:03 +00:00
2013-01-21 20:50:57 +00:00
navToolbar - > setAllowedAreas ( Qt : : AllToolBarAreas ) ;
2023-06-03 12:51:38 +00:00
groupList = groupListInDock ;
2012-12-24 11:30:03 +00:00
translateLine = ui . translateLine ;
2013-02-07 15:24:24 +00:00
translateBoxToolBarAction - > setVisible ( false ) ;
2023-06-05 13:22:20 +00:00
translateBox - > setPopupEnabled ( 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 ) ;
}
}
2023-06-03 12:51:38 +00:00
groupList = groupListInToolbar ;
2012-12-24 11:30:03 +00:00
translateLine = translateBox - > translateLine ( ) ;
2013-02-07 15:24:24 +00:00
translateBoxToolBarAction - > setVisible ( true ) ;
2012-12-24 11:30:03 +00:00
}
2023-06-03 12:51:38 +00:00
if ( ftsDlg )
2019-07-09 15:00:23 +00:00
addGroupComboBoxActionsToDialog ( ftsDlg , groupList ) ;
2023-06-03 12:51:38 +00:00
if ( headwordsDlg )
2019-07-09 15:00:23 +00:00
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 ;
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
2023-05-28 16:01:54 +00:00
setInputLineText ( text , WildcardPolicy : : WildcardsAreAlreadyEscaped , DisablePopup ) ;
2013-01-23 19:11:39 +00:00
focusTranslateLine ( ) ;
2012-12-24 11:30:03 +00:00
}
2023-06-03 12:51:38 +00:00
void MainWindow : : mousePressEvent ( QMouseEvent * event )
2009-07-31 11:40:54 +00:00
{
2023-06-03 12:51:38 +00:00
if ( handleBackForwardMouseButtons ( event ) ) {
2011-06-07 08:26:49 +00:00
return ;
}
2023-06-03 12:51:38 +00:00
if ( event - > button ( ) ! = Qt : : MiddleButton )
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 ) ;
2023-05-28 16:01:54 +00:00
setInputLineText ( str , WildcardPolicy : : 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 ) ;
2023-06-03 12:51:38 +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.
2023-06-03 12:51:38 +00:00
while ( ui . tabWidget - > count ( ) ) {
2009-05-23 14:19:57 +00:00
QWidget * w = ui . tabWidget - > widget ( 0 ) ;
ui . tabWidget - > removeTab ( 0 ) ;
delete w ;
}
2012-02-16 14:56:25 +00:00
2023-07-11 16:54:15 +00:00
if ( scanPopup ) {
delete scanPopup ;
scanPopup = nullptr ;
}
2014-05-20 13:59:56 +00:00
# ifndef NO_EPWING_SUPPORT
Epwing : : finalize ( ) ;
# endif
2009-02-06 17:04:11 +00:00
}
2023-06-11 07:44:07 +00:00
void MainWindow : : addGlobalAction ( QAction * action , const std : : function < void ( ) > & slotFunc )
2014-05-11 11:52:25 +00:00
{
action - > setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
2023-06-11 07:44:07 +00:00
connect ( action , & QAction : : triggered , this , slotFunc ) ;
2014-05-11 11:52:25 +00:00
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 ( ) ;
2023-06-03 12:51:38 +00:00
for ( QList < QAction * > : : iterator it = actions . begin ( ) ; it ! = actions . end ( ) ; + + it )
2019-07-09 15:00:23 +00:00
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
{
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . clearNetworkCacheOnExit )
if ( QAbstractNetworkCache * cache = articleNetMgr . cache ( ) )
2020-11-12 15:57:10 +00:00
cache - > clear ( ) ;
2023-06-03 12:51:38 +00:00
try {
2010-06-28 15:14:07 +00:00
// Save MainWindow state and geometry
2023-06-09 11:56:04 +00:00
cfg . mainWindowState = saveState ( ) ;
2010-06-28 15:14:07 +00:00
cfg . mainWindowGeometry = saveGeometry ( ) ;
2018-03-27 18:01:33 +00:00
// Save popup window state and geometry
2023-06-03 12:51:38 +00:00
if ( scanPopup )
2023-03-07 01:42:23 +00:00
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 ) ;
}
2023-06-03 12:51:38 +00:00
catch ( std : : exception & e ) {
2015-03-30 15:01:34 +00:00
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
}
2023-06-03 12:51:38 +00:00
catch ( std : : exception & e ) {
2018-03-27 14:46:03 +00:00
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 ;
2023-06-03 12:51:38 +00:00
printer = std : : make_shared < QPrinter > ( QPrinter : : HighResolution ) ;
2009-11-11 09:40:32 +00:00
return * printer ;
}
2023-07-20 12:10:44 +00:00
void MainWindow : : updateAppearances ( QString const & addonStyle ,
QString const & displayStyle ,
bool const & darkMode
# if !defined( Q_OS_WIN )
,
const QString & interfaceStyle
# endif
)
2009-05-11 11:03:36 +00:00
{
2023-06-03 12:51:38 +00:00
# ifdef Q_OS_WIN32
if ( darkMode ) {
//https://forum.qt.io/topic/101391/windows-10-dark-theme
2022-12-22 12:55:42 +00:00
# ifdef Q_OS_WIN32
2022-12-11 01:22:54 +00:00
qApp - > setStyle ( QStyleFactory : : create ( " Fusion " ) ) ;
2023-06-03 12:51:38 +00:00
# endif
2022-12-11 01:22:54 +00:00
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 ) ;
}
2023-06-03 12:51:38 +00:00
else {
2022-12-13 12:01:18 +00:00
qApp - > setStyle ( new QProxyStyle ( ) ) ;
2022-12-11 01:22:54 +00:00
qApp - > setPalette ( QPalette ( ) ) ;
}
2023-06-03 12:51:38 +00:00
# endif
2022-12-11 01:22:54 +00:00
2023-07-20 12:10:44 +00:00
# if !defined( Q_OS_WIN )
if ( interfaceStyle = = " Default " ) {
QApplication : : setStyle ( QStyleFactory : : create ( defaultInterfaceStyle ) ) ;
}
else {
if ( QStyleFactory : : keys ( ) . contains ( interfaceStyle ) ) {
QApplication : : setStyle ( QStyleFactory : : create ( interfaceStyle ) ) ;
}
}
# endif
2023-04-17 17:40:05 +00:00
QFile builtInCssFile ( " :qt-style.css " ) ;
2009-05-11 11:03:36 +00:00
builtInCssFile . open ( QFile : : ReadOnly ) ;
QByteArray css = builtInCssFile . readAll ( ) ;
2023-06-03 12:51:38 +00:00
# if defined( Q_OS_WIN )
2023-04-17 17:40:05 +00:00
QFile winCssFile ( " :qt-style-win.css " ) ;
2022-12-25 06:31:07 +00:00
winCssFile . open ( QFile : : ReadOnly ) ;
css + = winCssFile . readAll ( ) ;
2023-02-12 11:29:02 +00:00
// Load an additional stylesheet
// Dark Mode doesn't work nice with custom qt style sheets,
2023-06-03 12:51:38 +00:00
if ( ! darkMode ) {
2023-04-17 17:40:05 +00:00
QFile additionalStyle ( QString ( " :qt-%1.css " ) . arg ( displayStyle ) ) ;
2023-06-03 12:51:38 +00:00
if ( additionalStyle . open ( QFile : : ReadOnly ) ) {
2023-02-12 11:29:02 +00:00
css + = additionalStyle . readAll ( ) ;
}
}
2022-12-25 06:31:07 +00:00
# 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 ( ) ;
2023-06-03 12:51:38 +00:00
if ( ! addonStyle . isEmpty ( ) ) {
QString name = Config : : getStylesDir ( ) + addonStyle + QDir : : separator ( ) + " qt-style.css " ;
2012-12-10 14:14:13 +00:00
QFile addonCss ( name ) ;
2023-06-03 12:51:38 +00:00
if ( addonCss . open ( QFile : : ReadOnly ) )
2012-12-10 14:14:13 +00:00
css + = addonCss . readAll ( ) ;
}
2023-06-03 12:51:38 +00:00
# ifdef Q_OS_WIN32
if ( darkMode ) {
2022-12-11 01:22:54 +00:00
css + = " QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; } " ;
}
2023-06-03 12:51:38 +00:00
# endif
2022-12-11 01:22:54 +00:00
2023-07-20 12:10:44 +00:00
if ( ! css . isEmpty ( ) ) {
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 ( )
{
2023-06-03 12:51:38 +00:00
if ( ! trayIcon & & cfg . preferences . enableTrayIcon ) {
2009-02-05 20:55:00 +00:00
// Need to show it
2023-06-03 12:51:38 +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
}
2023-06-03 12:51:38 +00:00
else if ( trayIcon & & ! cfg . preferences . enableTrayIcon ) {
2009-02-05 20:55:00 +00:00
// Need to hide it
delete trayIcon ;
2023-07-17 00:24:02 +00:00
trayIcon = nullptr ;
2009-02-05 20:55:00 +00:00
}
2023-06-03 12:51:38 +00:00
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 ( ) ?
2023-06-03 12:51:38 +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
2023-04-17 09:11:18 +00:00
trayIcon - > setToolTip ( " GoldenDict-ng " ) ;
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
}
2023-06-03 12:51:38 +00:00
void MainWindow : : wheelEvent ( QWheelEvent * ev )
2013-01-01 23:05:29 +00:00
{
2023-06-03 12:51:38 +00:00
if ( ev - > modifiers ( ) . testFlag ( Qt : : ControlModifier ) ) {
if ( ev - > angleDelta ( ) . y ( ) > 0 ) {
zoomin ( ) ;
2013-01-01 23:05:29 +00:00
}
2023-06-03 12:51:38 +00:00
else if ( ev - > angleDelta ( ) . y ( ) < 0 ) {
zoomout ( ) ;
2013-01-01 23:05:29 +00:00
}
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 ) {
2023-06-03 12:51:38 +00:00
if ( ! cfg . preferences . searchInDock )
2017-09-01 16:13:29 +00:00
translateBox - > setPopupEnabled ( false ) ;
2022-02-18 12:22:56 +00:00
# ifdef Q_OS_MACOS
2023-06-03 12:51:38 +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.
2023-06-03 12:51:38 +00:00
Q_ASSERT ( ! QApplication : : quitOnLastWindowClosed ( ) ) ;
Q_ASSERT ( ! testAttribute ( Qt : : WA_DeleteOnClose ) ) ;
2021-11-28 15:30:35 +00:00
# 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 {
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 ( )
{
2023-06-03 12:51:38 +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 ( )
{
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . proxyServer . enabled & & cfg . preferences . proxyServer . useSystemProxy ) {
QList < QNetworkProxy > proxies = QNetworkProxyFactory : : systemProxyForQuery ( ) ;
if ( ! cfg . preferences . proxyServer . systemProxyUser . isEmpty ( ) ) {
2014-04-03 14:21:02 +00:00
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 ) {
2023-06-03 12:51:38 +00:00
switch ( cfg . preferences . proxyServer . type ) {
2009-04-13 17:31:05 +00:00
case Config : : ProxyServer : : Socks5 :
type = QNetworkProxy : : Socks5Proxy ;
2023-06-03 12:51:38 +00:00
break ;
2009-04-13 17:31:05 +00:00
case Config : : ProxyServer : : HttpConnect :
type = QNetworkProxy : : HttpProxy ;
2023-06-03 12:51:38 +00:00
break ;
2009-04-13 17:31:05 +00:00
case Config : : ProxyServer : : HttpGet :
type = QNetworkProxy : : HttpCachingProxy ;
2023-06-03 12:51:38 +00:00
break ;
2009-04-13 17:31:05 +00:00
default :
2023-06-03 12:51:38 +00:00
break ;
2009-04-13 17:31:05 +00:00
}
}
QNetworkProxy proxy ( type ) ;
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . proxyServer . enabled ) {
proxy . setHostName ( cfg . preferences . proxyServer . host ) ;
proxy . setPort ( cfg . preferences . proxyServer . port ) ;
2009-04-18 18:47:01 +00:00
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . proxyServer . user . size ( ) )
proxy . setUser ( cfg . preferences . proxyServer . user ) ;
2009-04-18 18:47:01 +00:00
2023-06-03 12:51:38 +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 ;
2023-06-03 12:51:38 +00:00
if ( QAbstractNetworkCache * abstractCache = articleNetMgr . cache ( ) ) {
2020-11-12 15:57:10 +00:00
QNetworkDiskCache * const diskCache = qobject_cast < QNetworkDiskCache * > ( abstractCache ) ;
Q_ASSERT_X ( diskCache , Q_FUNC_INFO , " Unexpected network cache type. " ) ;
diskCache - > setMaximumCacheSize ( maxCacheSizeInBytes ) ;
return ;
}
2023-06-03 12:51:38 +00:00
if ( maxCacheSizeInBytes = = 0 )
2020-11-12 15:57:10 +00:00
return ; // There is currently no cache and it is not needed.
2023-04-25 14:09:13 +00:00
QString cacheDirectory = Config : : getCacheDir ( ) ;
2023-05-19 03:01:13 +00:00
if ( ! QDir ( ) . mkpath ( cacheDirectory ) ) {
2023-04-25 14:09:13 +00:00
cacheDirectory = QStandardPaths : : writableLocation ( QStandardPaths : : CacheLocation ) ;
gdWarning ( " Cannot create a cache directory %s. use default cache path. " , cacheDirectory . toUtf8 ( ) . constData ( ) ) ;
2020-11-12 15:57:10 +00:00
}
2023-04-25 14:09:13 +00:00
2020-11-12 15:57:10 +00:00
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 ( )
{
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
2023-06-03 12:51:38 +00:00
dictMap = Dictionary : : dictToMap ( dictionaries ) ;
2022-03-13 16:17:34 +00:00
2023-06-03 12:51:38 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + ) {
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 ;
2023-06-03 12:51:38 +00:00
for ( unsigned x = dictionaries . size ( ) ; x - - ; ) {
2009-01-28 20:55:45 +00:00
articleCount + = dictionaries [ x ] - > getArticleCount ( ) ;
wordCount + = dictionaries [ x ] - > getWordCount ( ) ;
}
2023-06-03 12:51:38 +00:00
mainStatusBar - > showMessage (
tr ( " %1 dictionaries, %2 articles, %3 words " ) . arg ( dictionaries . size ( ) ) . arg ( articleCount ) . 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
2023-05-22 23:50:37 +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.
2023-06-03 12:51:38 +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 " ) ;
2023-06-03 12:51:38 +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
}
2023-04-25 00:12:21 +00:00
GlobalBroadcaster : : instance ( ) - > groupFolderMap . clear ( ) ;
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 ) ) ;
2023-04-25 00:12:21 +00:00
GlobalBroadcaster : : instance ( ) - > groupFolderMap . insert ( cfg . groups [ x ] . id , cfg . groups [ x ] . favoritesFolder ) ;
}
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
2023-06-03 12:51:38 +00:00
for ( int i = 0 ; i < ui . tabWidget - > count ( ) ; + + i ) {
ArticleView & view = dynamic_cast < ArticleView & > ( * ( ui . tabWidget - > widget ( i ) ) ) ;
2013-06-10 21:39:40 +00:00
view . reload ( ) ;
}
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +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
2023-07-17 00:24:02 +00:00
dictionaryBar . setMutedDictionaries ( nullptr ) ;
2012-09-26 13:59:48 +00:00
if ( grp ) { // Should always be !0, but check as a safeguard
2023-06-03 12:51:38 +00:00
if ( currentId = = Instances : : Group : : AllGroupId )
2012-09-26 13:13:47 +00:00
dictionaryBar . setMutedDictionaries ( & cfg . mutedDictionaries ) ;
2023-06-03 12:51:38 +00:00
else {
2012-09-26 13:13:47 +00:00
Config : : Group * grp = cfg . getGroup ( currentId ) ;
2023-07-17 00:24:02 +00:00
dictionaryBar . setMutedDictionaries ( grp ? & grp - > mutedDictionaries : nullptr ) ;
2012-09-26 13:13:47 +00:00
}
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 ( ) ?
2023-06-03 12:51:38 +00:00
QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_SmallIconSize ) :
QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_ToolBarIconSize ) ;
2022-01-23 11:41:50 +00:00
dictionaryBar . setDictionaryIconSize ( extent ) ;
2011-05-22 02:42:05 +00:00
}
2009-09-21 17:50:03 +00:00
}
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
2023-06-03 12:51:38 +00:00
if ( current < 0 | | current > = ( int ) groupInstances . size ( ) ) {
2009-01-28 20:55:45 +00:00
// This shouldn't ever happen
return dictionaries ;
}
2012-09-26 13:13:47 +00:00
Config : : MutedDictionaries const * mutedDictionaries = dictionaryBar . getMutedDictionaries ( ) ;
2023-07-17 00:24:02 +00:00
if ( ! dictionaryBar . toggleViewAction ( ) - > isChecked ( ) | | mutedDictionaries = = nullptr )
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 ( ) ) ;
2023-06-03 12:51:38 +00:00
for ( unsigned x = 0 ; x < activeDicts . size ( ) ; + + x )
if ( ! mutedDictionaries - > contains ( QString : : fromStdString ( activeDicts [ x ] - > getId ( ) ) ) )
2009-09-23 18:44:38 +00:00
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 ( )
{
2023-06-03 12:51: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
2023-06-03 12:51:38 +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 " ) ) ;
2023-06-03 12:51:38 +00:00
tabListButton - > setPopupMode ( QToolButton : : InstantPopup ) ;
ui . tabWidget - > setCornerWidget ( tabListButton ) ;
tabListButton - > setFocusPolicy ( Qt : : NoFocus ) ;
2010-09-16 18:52:40 +00:00
}
void MainWindow : : fillWindowsMenu ( )
{
tabListMenu - > clear ( ) ;
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . mruTabOrder ) {
for ( int i = 0 ; i < mruList . count ( ) ; i + + ) {
QAction * act = tabListMenu - > addAction ( ui . tabWidget - > tabIcon ( ui . tabWidget - > indexOf ( mruList . at ( i ) ) ) ,
ui . tabWidget - > tabText ( ui . tabWidget - > indexOf ( mruList . at ( i ) ) ) ) ;
2011-11-02 23:37:50 +00:00
//remember the index of the Tab to be later used in ctrlReleased()
2023-06-03 12:51:38 +00:00
act - > setData ( ui . tabWidget - > indexOf ( mruList . at ( i ) ) ) ;
2011-11-02 23:37:50 +00:00
2023-06-03 12:51:38 +00:00
if ( ui . tabWidget - > currentIndex ( ) = = ui . tabWidget - > indexOf ( mruList . at ( i ) ) ) {
2011-11-02 23:37:50 +00:00
QFont f ( act - > font ( ) ) ;
f . setBold ( true ) ;
act - > setFont ( f ) ;
}
}
2023-06-03 12:51:38 +00:00
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 {
2023-06-03 12:51:38 +00:00
for ( int i = 0 ; i < ui . tabWidget - > count ( ) ; i + + ) {
QAction * act = tabListMenu - > addAction ( ui . tabWidget - > tabIcon ( i ) , ui . tabWidget - > tabText ( i ) ) ;
2011-11-02 23:37:50 +00:00
act - > setData ( i ) ;
2023-06-03 12:51:38 +00:00
if ( ui . tabWidget - > currentIndex ( ) = = i ) {
2011-11-02 23:37:50 +00:00
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
}
2023-06-03 12:51:38 +00:00
void MainWindow : : switchToWindow ( QAction * act )
2010-09-16 18:52:40 +00:00
{
int idx = act - > data ( ) . toInt ( ) ;
2023-06-03 12:51:38 +00:00
ui . tabWidget - > setCurrentIndex ( idx ) ;
2010-09-16 18:52:40 +00:00
}
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
{
2023-06-03 12:51:38 +00:00
ArticleView * view = new ArticleView ( this ,
articleNetMgr ,
audioPlayerFactory . player ( ) ,
dictionaries ,
groupInstances ,
false ,
cfg ,
2014-04-16 16:18:28 +00:00
* ui . searchInPageAction ,
2023-03-16 11:55:47 +00:00
translateLine ,
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
2023-06-03 12:51:38 +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 ( 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 ) ;
2023-06-03 12:51:38 +00:00
int index = cfg . preferences . newTabsOpenAfterCurrentOne ? ui . tabWidget - > currentIndex ( ) + 1 : ui . tabWidget - > count ( ) ;
2009-04-13 12:51:25 +00:00
QString escaped = name ;
escaped . replace ( " & " , " && " ) ;
ui . tabWidget - > insertTab ( index , view , escaped ) ;
2023-06-03 12:51:38 +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 ) ;
2023-06-03 12:51:38 +00:00
mruList . removeOne ( w ) ;
2011-11-02 23:37:50 +00:00
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 ) {
2023-06-03 12:51:38 +00:00
ui . tabWidget - > setCurrentWidget ( mruList . at ( 0 ) ) ;
2012-03-27 01:38:41 +00:00
}
2023-06-03 12:51:38 +00:00
else if ( ui . tabWidget - > count ( ) > 1 ) {
2015-11-21 11:10:15 +00:00
//activate neighboring tab
int n = x > = ui . tabWidget - > count ( ) - 1 ? x - 1 : x + 1 ;
2023-06-03 12:51:38 +00:00
if ( n > = 0 )
2015-11-21 11:10:15 +00:00
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 ( )
{
2023-06-03 12:51:38 +00:00
while ( ui . tabWidget - > count ( ) > 1 )
2010-09-16 18:52:40 +00:00
closeCurrentTab ( ) ;
// close last tab
closeCurrentTab ( ) ;
}
void MainWindow : : closeRestTabs ( )
{
if ( ui . tabWidget - > count ( ) < 2 )
return ;
int idx = ui . tabWidget - > currentIndex ( ) ;
2023-06-03 12:51:38 +00:00
for ( int i = 0 ; i < idx ; i + + )
tabCloseRequested ( 0 ) ;
2010-09-16 18:52:40 +00:00
2023-06-03 12:51:38 +00:00
ui . tabWidget - > setCurrentIndex ( 0 ) ;
2010-09-16 18:52:40 +00:00
2023-06-03 12:51:38 +00:00
while ( ui . tabWidget - > count ( ) > 1 )
tabCloseRequested ( 1 ) ;
2010-09-16 18:52:40 +00:00
}
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 ) ;
}
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
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +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.
2023-06-03 12:51:38 +00:00
QString escaped ;
if ( title ! = nullptr & & title . contains ( " : " ) ) {
2022-01-18 14:26:52 +00:00
//check if the title is url.
2023-06-03 12:51:38 +00:00
QUrl url ( title ) ;
escaped = Utils : : Url : : queryItemValue ( url , " word " ) ;
}
else {
2022-01-18 14:26:52 +00:00
escaped = title ;
}
2009-04-10 21:37:16 +00:00
escaped . replace ( " & " , " && " ) ;
2022-07-29 15:48:04 +00:00
int index = ui . tabWidget - > indexOf ( view ) ;
2023-06-03 12:51:38 +00:00
if ( ! escaped . isEmpty ( ) )
2022-08-21 06:10:42 +00:00
ui . tabWidget - > setTabText ( index , escaped ) ;
2017-11-07 14:46:59 +00:00
2023-06-03 12:51:38 +00:00
if ( index = = ui . tabWidget - > currentIndex ( ) ) {
2022-07-29 15:48:04 +00:00
// Set icon for "Add to Favorites" action
2023-06-03 12:51:38 +00:00
if ( isWordPresentedInFavorites ( title , cfg . lastMainGroupId ) ) {
2022-07-29 15:48:04 +00:00
addToFavorites - > setIcon ( blueStarIcon ) ;
addToFavorites - > setToolTip ( tr ( " Remove current tab from Favorites " ) ) ;
}
2023-06-03 12:51:38 +00:00
else {
2022-07-29 15:48:04 +00:00
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 ( )
{
2023-06-03 12:51:38 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2011-07-01 19:22:40 +00:00
if ( view ) {
2013-01-31 13:01:08 +00:00
QString str = view - > getTitle ( ) ;
2023-05-19 11:41:27 +00:00
if ( ! str . isEmpty ( ) ) {
2023-06-03 12:51:38 +00:00
if ( ! blockUpdateWindowTitle )
2023-04-17 09:11:18 +00:00
setWindowTitle ( tr ( " %1 - %2 " ) . arg ( str , " GoldenDict-ng " ) ) ;
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
{
2023-06-03 12:51:38 +00:00
if ( view ! = getCurrentArticleView ( ) )
2014-02-09 15:00:48 +00:00
return ; // It was background action
2011-06-07 11:27:19 +00:00
updateBackForwardButtons ( ) ;
2009-04-10 21:07:03 +00:00
updatePronounceAvailability ( ) ;
}
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
{
2023-06-03 12:51:38 +00:00
if ( message . isEmpty ( ) )
2015-11-01 12:22:18 +00:00
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 ( ) ;
2023-06-03 12:51:38 +00:00
if ( mruList . size ( ) > 1 ) {
2013-05-29 06:17:12 +00:00
int from = mruList . indexOf ( ui . tabWidget - > widget ( ui . tabWidget - > currentIndex ( ) ) ) ;
2023-06-03 12:51:38 +00:00
if ( from > 0 )
2013-05-29 06:17:12 +00:00
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 ( ) ) ;
2023-06-03 12:51:38 +00:00
if ( isWordPresentedInFavorites ( unescapeTabHeader ( headword ) , cfg . lastMainGroupId ) ) {
2017-12-07 15:47:53 +00:00
addToFavorites - > setIcon ( blueStarIcon ) ;
addToFavorites - > setToolTip ( tr ( " Remove current tab from Favorites " ) ) ;
}
2023-06-03 12:51:38 +00:00
else {
2017-12-07 15:47:53 +00:00
addToFavorites - > setIcon ( starIcon ) ;
addToFavorites - > setToolTip ( tr ( " Add current tab to Favorites " ) ) ;
}
2023-07-17 00:24:02 +00:00
auto view = getCurrentArticleView ( ) ;
if ( view ) {
groupList - > setCurrentGroup ( view - > getCurrentGroupId ( ) ) ;
}
2009-04-10 21:07:03 +00:00
}
2023-06-03 12:51:38 +00:00
void MainWindow : : tabMenuRequested ( QPoint pos )
2010-09-16 18:52:40 +00:00
{
2023-06-03 12:51:38 +00:00
// // do not show this menu for single tab
// if ( ui.tabWidget->count() < 2 )
// return;
2010-09-16 18:52:40 +00:00
2023-06-03 12:51:38 +00:00
tabMenu - > popup ( ui . tabWidget - > mapToGlobal ( pos ) ) ;
2010-09-16 18:52:40 +00:00
}
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
2023-06-03 12:51:38 +00:00
updateDictionaryBar ( ) ; // Updates dictionary bar contents if it's shown
2009-09-23 18:44:38 +00:00
applyMutedDictionariesState ( ) ; // Visibility change affects searches and results
}
2023-06-03 12:51:38 +00:00
void MainWindow : : showDictsPane ( )
2011-07-31 00:11:07 +00:00
{
2023-06-03 12:51:38 +00:00
if ( ! ui . dictsPane - > isVisible ( ) )
2011-07-31 00:11:07 +00:00
ui . dictsPane - > show ( ) ;
}
2011-06-05 11:49:50 +00:00
void MainWindow : : dictsPaneVisibilityChanged ( bool visible )
{
2023-06-03 12:51:38 +00:00
if ( visible ) {
2011-06-05 11:49:50 +00:00
updateFoundInDictsList ( ) ;
}
}
void MainWindow : : updateFoundInDictsList ( )
{
2023-06-03 12:51:38 +00:00
if ( ! ui . dictsList - > isVisible ( ) ) {
2011-06-05 11:49:50 +00:00
// nothing to do, the list is not visible
return ;
}
ui . dictsList - > clear ( ) ;
2023-06-03 12:51:38 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2011-06-05 11:49:50 +00:00
2011-07-01 19:22:40 +00:00
if ( view ) {
2023-06-03 12:51:38 +00:00
QStringList ids = view - > getArticlesList ( ) ;
2011-07-03 12:27:08 +00:00
QString activeId = view - > getActiveArticleId ( ) ;
2011-06-05 11:49:50 +00:00
2023-06-03 12:51:38 +00:00
for ( QStringList : : const_iterator i = ids . constBegin ( ) ; i ! = ids . constEnd ( ) ; + + i ) {
2011-06-05 11:49:50 +00:00
// Find this dictionary
2023-06-03 12:51:38 +00:00
for ( unsigned x = dictionaries . size ( ) ; x - - ; ) {
if ( dictionaries [ x ] - > getId ( ) = = i - > toUtf8 ( ) . data ( ) ) {
2011-06-05 11:49:50 +00:00
QString dictName = QString : : fromUtf8 ( dictionaries [ x ] - > getName ( ) . c_str ( ) ) ;
2023-06-03 12:51:38 +00:00
QString dictId = QString : : fromUtf8 ( dictionaries [ x ] - > getId ( ) . c_str ( ) ) ;
2011-06-05 11:49:50 +00:00
QListWidgetItem * item =
2023-06-03 12:51:38 +00:00
new QListWidgetItem ( dictionaries [ x ] - > getIcon ( ) , dictName , ui . dictsList , QListWidgetItem : : Type ) ;
item - > setData ( Qt : : UserRole , QVariant ( dictId ) ) ;
item - > setToolTip ( dictName ) ;
2011-06-05 11:49:50 +00:00
ui . dictsList - > addItem ( item ) ;
2023-06-03 12:51:38 +00:00
if ( dictId = = activeId ) {
ui . dictsList - > setCurrentItem ( item ) ;
2011-07-03 12:27:08 +00:00
}
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.
2023-06-03 12:51:38 +00:00
if ( ui . dictsList - > count ( ) > 0 & & ui . dictsList - > selectedItems ( ) . empty ( ) ) {
ui . dictsList - > setCurrentRow ( 0 ) ;
2022-01-17 13:50:54 +00:00
}
2011-06-05 11:49:50 +00:00
}
}
2011-06-07 11:27:19 +00:00
void MainWindow : : updateBackForwardButtons ( )
{
2023-06-03 12:51:38 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2011-07-01 19:22:40 +00:00
2023-06-03 12:51:38 +00:00
if ( view ) {
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 ( )
{
2023-06-03 12:51:38 +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
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 ( ) ;
2012-12-22 17:46:53 +00:00
{ // Limit existence of newCfg
2023-06-03 12:51:38 +00:00
Config : : Class newCfg = cfg ;
EditDictionaries dicts ( this , newCfg , dictionaries , groupInstances , dictNetMgr ) ;
2009-01-28 20:55:45 +00:00
2023-06-03 12:51:38 +00:00
connect ( & dicts , & EditDictionaries : : showDictionaryInfo , this , & MainWindow : : showDictionaryInfo ) ;
2013-07-07 08:09:46 +00:00
2023-06-03 12:51:38 +00:00
connect ( & dicts , & EditDictionaries : : showDictionaryHeadwords , this , & MainWindow : : showDictionaryHeadwords ) ;
2014-03-01 13:11:14 +00:00
2023-06-03 12:51:38 +00:00
if ( editDictionaryGroup ! = Instances : : Group : : NoGroupId )
dicts . editGroup ( editDictionaryGroup ) ;
2010-05-08 14:01:59 +00:00
2023-06-03 12:51:38 +00:00
dicts . restoreGeometry ( cfg . dictionariesDialogGeometry ) ;
dicts . exec ( ) ;
cfg . dictionariesDialogGeometry = newCfg . dictionariesDialogGeometry = dicts . saveGeometry ( ) ;
2009-04-30 19:57:25 +00:00
2023-06-03 12:51:38 +00:00
if ( dicts . areDictionariesChanged ( ) | | dicts . areGroupsChanged ( ) ) {
ftsIndexing . stopIndexing ( ) ;
ftsIndexing . clearDictionaries ( ) ;
// Set muted dictionaries from old groups
2023-06-16 00:37:33 +00:00
for ( auto & group : newCfg . groups ) {
unsigned id = group . id ;
2023-06-03 12:51:38 +00:00
if ( id ! = Instances : : Group : : NoGroupId ) {
Config : : Group const * grp = cfg . getGroup ( id ) ;
if ( grp ) {
2023-06-16 00:37:33 +00:00
group . mutedDictionaries = grp - > mutedDictionaries ;
group . popupMutedDictionaries = grp - > popupMutedDictionaries ;
2023-06-03 12:51:38 +00:00
}
2012-09-26 13:13:47 +00:00
}
}
2023-06-03 12:51:38 +00:00
cfg = newCfg ;
2009-04-30 19:57:25 +00:00
2023-06-03 12:51:38 +00:00
updateGroupList ( ) ;
2013-09-24 13:55:12 +00:00
2023-06-03 12:51:38 +00:00
Config : : save ( cfg ) ;
2022-03-03 15:59:42 +00:00
2023-06-03 12:51:38 +00:00
updateSuggestionList ( ) ;
2022-03-03 15:59:42 +00:00
2023-06-16 00:37:33 +00:00
for ( auto & dictionary : dictionaries ) {
dictionary - > setFTSParameters ( cfg . preferences . fts ) ;
dictionary - > setSynonymSearchEnabled ( cfg . preferences . synonymSearchEnabled ) ;
2023-06-03 12:51:38 +00:00
}
2009-04-30 19:57:25 +00:00
2023-06-03 12:51:38 +00:00
ftsIndexing . setDictionaries ( dictionaries ) ;
ftsIndexing . doIndexing ( ) ;
}
2012-12-22 17:46:53 +00:00
}
2023-02-23 00:44:29 +00:00
scanPopup - > refresh ( ) ;
2009-04-30 15:29:03 +00:00
installHotKeys ( ) ;
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
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
preferences . show ( ) ;
2023-06-03 12:51:38 +00:00
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
2023-06-03 12:51:38 +00:00
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 ;
2023-06-03 12:51:38 +00:00
p . hideMenubar = cfg . preferences . hideMenubar ;
p . searchInDock = cfg . preferences . searchInDock ;
p . alwaysOnTop = cfg . preferences . alwaysOnTop ;
2022-11-19 15:30:31 +00:00
2023-06-03 12:51:38 +00:00
p . proxyServer . systemProxyUser = cfg . preferences . proxyServer . systemProxyUser ;
2014-04-03 14:21:02 +00:00
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 ;
2023-05-17 14:29:56 +00:00
2014-04-17 14:31:51 +00:00
p . fts . searchMode = cfg . preferences . fts . searchMode ;
2023-07-20 12:10:44 +00:00
// See if we need to update Appearances
if ( cfg . preferences . displayStyle ! = p . displayStyle | | cfg . preferences . darkMode ! = p . darkMode
# if !defined( Q_OS_WIN )
| | cfg . preferences . interfaceStyle ! = p . interfaceStyle
# endif
) {
updateAppearances ( p . addonStyle ,
p . displayStyle ,
p . darkMode
# if !defined( Q_OS_WIN )
,
p . interfaceStyle
# endif
) ;
2012-09-16 10:19:47 +00:00
}
2009-05-11 11:03:36 +00:00
2023-07-20 12:10:44 +00:00
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . historyStoreInterval ! = p . historyStoreInterval )
2013-02-05 12:51:23 +00:00
history . setSaveInterval ( p . historyStoreInterval ) ;
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . favoritesStoreInterval ! = p . favoritesStoreInterval )
2017-05-13 10:18:25 +00:00
ui . favoritesPaneWidget - > setSaveInterval ( p . favoritesStoreInterval ) ;
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . maxNetworkCacheSize ! = p . maxNetworkCacheSize )
2020-11-12 15:57:10 +00:00
setupNetworkCache ( p . maxNetworkCacheSize ) ;
2023-03-21 07:58:58 +00:00
bool needReload =
( cfg . preferences . displayStyle ! = p . displayStyle | | cfg . preferences . addonStyle ! = p . addonStyle
| | cfg . preferences . darkReaderMode ! = p . darkReaderMode
| | cfg . preferences . collapseBigArticles ! = p . collapseBigArticles
| | cfg . preferences . articleSizeLimit ! = p . articleSizeLimit
| | cfg . preferences . alwaysExpandOptionalParts ! = p . alwaysExpandOptionalParts // DSL format's special feature
) ;
// This line must be here because the components below require cfg's value to reconfigure
// After this point, p must not be accessed.
2009-05-11 11:03:36 +00:00
cfg . preferences = p ;
2009-04-18 18:47:01 +00:00
2023-03-21 07:58:58 +00:00
// Loop through all tabs and reload pages due to ArticleMaker's change.
2023-06-03 12:51:38 +00:00
for ( int x = 0 ; x < ui . tabWidget - > count ( ) ; + + x ) {
ArticleView & view = dynamic_cast < ArticleView & > ( * ( ui . tabWidget - > widget ( x ) ) ) ;
2023-03-21 07:58:58 +00:00
view . setSelectionBySingleClick ( p . selectWordBySingleClick ) ;
2023-06-03 12:51:38 +00:00
if ( needReload ) {
2023-03-21 07:58:58 +00:00
view . reload ( ) ;
}
}
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
2023-06-03 12:51:38 +00:00
ui . tabWidget - > setHideSingleTab ( cfg . preferences . hideSingleTab ) ;
2011-06-23 14:17:09 +00:00
2009-04-20 19:54:34 +00:00
setAutostart ( cfg . preferences . autoStart ) ;
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
2023-06-03 12:51:38 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + ) {
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
2023-02-23 00:44:29 +00:00
scanPopup - > refresh ( ) ;
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
{
2023-07-17 00:24:02 +00:00
unsigned grg_id = groupList - > getCurrentGroup ( ) ;
cfg . lastMainGroupId = grg_id ;
Instances : : Group const * igrp = groupInstances . findGroup ( grg_id ) ;
if ( grg_id = = Instances : : Group : : AllGroupId ) {
2023-06-03 12:51:38 +00:00
if ( igrp )
2012-09-26 13:13:47 +00:00
igrp - > checkMutedDictionaries ( & cfg . mutedDictionaries ) ;
dictionaryBar . setMutedDictionaries ( & cfg . mutedDictionaries ) ;
}
2023-06-03 12:51:38 +00:00
else {
2023-07-17 00:24:02 +00:00
Config : : Group * grp = cfg . getGroup ( grg_id ) ;
2023-06-03 12:51:38 +00:00
if ( grp ) {
if ( igrp )
2012-09-26 13:13:47 +00:00
igrp - > checkMutedDictionaries ( & grp - > mutedDictionaries ) ;
dictionaryBar . setMutedDictionaries ( & grp - > mutedDictionaries ) ;
}
else
2023-07-17 00:24:02 +00:00
dictionaryBar . setMutedDictionaries ( nullptr ) ;
2012-09-26 13:13:47 +00:00
}
2009-02-06 17:04:11 +00:00
2023-06-03 12:51:38 +00:00
if ( igrp ) {
2023-07-17 00:24:02 +00:00
GlobalBroadcaster : : instance ( ) - > currentGroupId = grg_id ;
2022-11-24 12:34:21 +00:00
}
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 ( ) ;
2023-07-17 00:24:02 +00:00
if ( auto view = getCurrentArticleView ( ) ) {
view - > setCurrentGroupId ( grg_id ) ;
QString word = Folding : : unescapeWildcardSymbols ( view - > getWord ( ) ) ;
respondToTranslationRequest ( word , false ) ;
}
2010-11-15 15:22:35 +00:00
updateCurrentGroupProperty ( ) ;
2014-04-16 16:18:28 +00:00
2023-06-03 12:51:38 +00:00
if ( ftsDlg )
2023-07-17 00:24:02 +00:00
ftsDlg - > setCurrentGroup ( grg_id ) ;
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
2023-06-03 12:51:38 +00:00
Instances : : Group * grp = groupInstances . findGroup ( groupList - > getCurrentGroup ( ) ) ;
2010-11-15 15:22:35 +00:00
2023-06-03 12:51:38 +00:00
if ( grp & & translateLine - > property ( " currentGroup " ) . toString ( ) ! = grp - > name ) {
2012-12-24 11:30:03 +00:00
translateLine - > setProperty ( " currentGroup " , grp - > name ) ;
2023-06-05 13:22:20 +00:00
ui . 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
2023-06-03 12:51:38 +00:00
if ( ss . contains ( " currentGroup " ) )
2010-11-15 15:22:35 +00:00
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 ) ;
2023-03-24 11:52:38 +00:00
// Save translate line text. Later it can be passed to external applications.
GlobalBroadcaster : : instance ( ) - > translateLineText = newValue ;
2021-06-10 16:13:11 +00:00
}
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.
2023-06-05 13:22:20 +00:00
if ( ui . wordList - > selectionModel ( ) - > hasSelection ( ) )
2023-07-17 00:24:02 +00:00
ui . wordList - > setCurrentItem ( nullptr , 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 ( ) ;
2023-06-05 13:22:20 +00:00
ui . wordList - > clear ( ) ;
ui . 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 ( ) ) {
translateLine - > setProperty ( " noResults " , false ) ;
2023-03-22 01:01:22 +00:00
Utils : : Widget : : setNoResultColor ( translateLine , false ) ;
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
2023-06-05 13:22:20 +00:00
ui . wordList - > setCursor ( Qt : : WaitCursor ) ;
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 ( ) ) ;
2023-05-28 16:01:54 +00:00
respondToTranslationRequest ( word , checkModifiers ) ;
2021-06-10 16:13:11 +00:00
}
2009-03-29 17:38:54 +00:00
2023-06-03 12:51:38 +00:00
void MainWindow : : respondToTranslationRequest ( QString const & word , bool checkModifiers , QString const & scrollTo )
2021-06-10 16:13:11 +00:00
{
2023-06-03 12:51:38 +00:00
if ( ! word . isEmpty ( ) ) {
2009-05-05 20:12:20 +00:00
Qt : : KeyboardModifiers mods = QApplication : : keyboardModifiers ( ) ;
2023-06-03 12:51:38 +00:00
if ( checkModifiers & & ( mods & ( Qt : : ControlModifier | Qt : : ShiftModifier ) ) )
2009-05-05 20:12:20 +00:00
addNewTab ( ) ;
2023-05-28 16:01:54 +00:00
showTranslationFor ( word , 0 , scrollTo ) ;
2009-05-12 13:25:18 +00:00
2023-06-03 12:51:38 +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
}
2023-05-28 16:01:54 +00:00
void MainWindow : : setInputLineText ( QString text , WildcardPolicy wildcardPolicy , TranslateBoxPopup popupAction )
2021-06-10 16:13:11 +00:00
{
2023-06-03 12:51:38 +00:00
if ( wildcardPolicy = = WildcardPolicy : : EscapeWildcards )
2021-06-30 16:49:58 +00:00
text = Folding : : escapeWildcardSymbols ( text ) ;
2023-06-07 06:34:14 +00:00
if ( popupAction = = NoPopupChange | | cfg . preferences . searchInDock ) {
2021-06-10 16:13:11 +00:00
translateLine - > setText ( text ) ;
2023-06-07 06:34:14 +00:00
}
else {
2021-06-18 18:33:24 +00:00
translateBox - > setText ( text , popupAction = = EnablePopup ) ;
2023-06-07 06:34:14 +00:00
}
GlobalBroadcaster : : instance ( ) - > translateLineText = text ;
2021-06-10 16:13:11 +00:00
}
2010-04-27 07:49:37 +00:00
void MainWindow : : handleEsc ( )
2009-04-08 22:25:46 +00:00
{
2023-06-03 12:51:38 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2011-07-01 19:22:40 +00:00
if ( view & & view - > closeSearch ( ) )
2009-05-16 11:14:43 +00:00
return ;
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . escKeyHidesMainWindow ) {
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
2023-06-03 12:51:38 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2011-07-01 19:22:40 +00:00
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
}
2023-06-03 12:51:38 +00:00
bool MainWindow : : handleBackForwardMouseButtons ( QMouseEvent * event )
{
2011-06-07 08:26:49 +00:00
if ( event - > button ( ) = = Qt : : XButton1 ) {
backClicked ( ) ;
return true ;
}
2023-06-03 12:51:38 +00:00
else if ( event - > button ( ) = = Qt : : XButton2 ) {
2011-06-07 08:26:49 +00:00
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 ) {
2023-07-27 00:06:48 +00:00
QKeyEvent * ke = dynamic_cast < QKeyEvent * > ( ev ) ;
2013-01-23 18:36:45 +00:00
// Handle F3/Shift+F3 shortcuts
2023-07-27 00:06:48 +00:00
int const key = ke - > key ( ) ;
if ( key = = Qt : : Key_F3 ) {
2023-06-03 12:51:38 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
if ( view & & view - > handleF3 ( obj , ev ) )
2013-01-23 18:36:45 +00:00
return true ;
}
2023-05-12 01:07:13 +00:00
//workaround to fix #660
2023-07-27 00:06:48 +00:00
if ( obj = = this & & ev - > type ( ) = = QEvent : : KeyPress
& & ( key = = Qt : : Key_Up | | key = = Qt : : Key_Down | | key = = Qt : : Key_Space | | key = = Qt : : Key_PageUp
| | key = = Qt : : Key_PageDown ) ) {
2023-05-12 01:07:13 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
if ( view ) {
view - > focus ( ) ;
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 ) ) {
2023-06-03 12:51:38 +00:00
if ( ! cfg . preferences . searchInDock ) {
translateBox - > setPopupEnabled ( false ) ;
return false ;
2012-12-25 09:25:54 +00:00
}
}
2023-06-03 12:51:38 +00:00
if ( obj = = this & & ev - > type ( ) = = QEvent : : WindowStateChange ) {
2023-07-27 00:29:51 +00:00
auto stev = dynamic_cast < QWindowStateChangeEvent * > ( ev ) ;
2023-06-03 12:51:38 +00:00
wasMaximized = ( stev - > oldState ( ) = = Qt : : WindowMaximized & & isMinimized ( ) ) ;
2013-01-23 20:36:53 +00:00
}
2011-06-07 08:26:49 +00:00
if ( ev - > type ( ) = = QEvent : : MouseButtonPress ) {
2023-07-27 00:06:48 +00:00
auto event = static_cast < QMouseEvent * > ( ev ) ;
2012-12-25 20:38:28 +00:00
2011-06-07 08:26:49 +00:00
return handleBackForwardMouseButtons ( event ) ;
}
2023-06-03 12:51:38 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress ) {
2023-06-05 13:22:20 +00:00
auto keyevent = dynamic_cast < QKeyEvent * > ( ev ) ;
2022-07-14 15:14:28 +00:00
2023-07-27 00:06:48 +00:00
bool const handleCtrlTab = ( obj = = translateLine | | obj = = ui . wordList | | obj = = ui . historyList
2023-07-27 00:29:51 +00:00
| | obj = = ui . favoritesTree | | obj = = ui . dictsList | | obj = = groupList ) ;
2022-07-14 15:14:28 +00:00
2023-06-03 12:51:38 +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
}
2023-06-03 12:51:38 +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 ;
}
2023-07-27 00:06:48 +00:00
// Handle only Ctrl+Shift+Tab here because Ctrl+Tab was already handled before
if ( handleCtrlTab & & keyevent - > matches ( QKeySequence : : PreviousChild ) ) {
2022-07-14 15:14:28 +00:00
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 ) {
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 ) {
2023-06-05 13:22:20 +00:00
if ( keyEvent - > matches ( QKeySequence : : MoveToNextLine ) & & ui . wordList - > count ( ) ) {
ui . wordList - > setFocus ( Qt : : ShortcutFocusReason ) ;
ui . wordList - > setCurrentRow ( 0 , QItemSelectionModel : : ClearAndSelect ) ;
2012-12-24 11:30:03 +00:00
return true ;
}
2009-03-29 17:38:54 +00:00
}
}
2012-12-24 11:30:03 +00:00
2023-06-10 02:36:59 +00:00
if ( ev - > type ( ) = = QEvent : : FocusIn ) {
// select all on mouse click
if ( const auto focusEvent = dynamic_cast < QFocusEvent * > ( ev ) ; focusEvent - > reason ( ) = = Qt : : MouseFocusReason ) {
QTimer : : singleShot ( 0 , this , SLOT ( focusTranslateLine ( ) ) ) ;
}
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
}
2023-06-05 13:22:20 +00:00
else if ( obj = = ui . wordList ) {
2023-04-17 15:19:29 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress ) {
2009-03-29 17:38:54 +00:00
QKeyEvent * keyEvent = static_cast < QKeyEvent * > ( ev ) ;
2023-06-05 13:22:20 +00:00
if ( keyEvent - > matches ( QKeySequence : : MoveToPreviousLine ) & & ! ui . wordList - > currentRow ( ) ) {
ui . wordList - > setCurrentRow ( 0 , QItemSelectionModel : : Clear ) ;
2012-12-24 11:30:03 +00:00
translateLine - > setFocus ( Qt : : ShortcutFocusReason ) ;
2009-03-29 17:38:54 +00:00
return true ;
}
if ( keyEvent - > matches ( QKeySequence : : InsertParagraphSeparator ) & & ui . wordList - > selectedItems ( ) . size ( ) ) {
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
}
2011-06-05 11:49:50 +00:00
}
}
2023-04-17 15:19:29 +00:00
if ( ev - > type ( ) = = QEvent : : KeyPress & & obj ! = translateLine ) {
2011-06-05 11:49:50 +00:00
2023-06-03 12:51:38 +00:00
if ( const auto key_event = dynamic_cast < QKeyEvent * > ( ev ) ; key_event - > modifiers ( ) = = Qt : : NoModifier ) {
2023-04-17 15:19:29 +00:00
const QString text = key_event - > text ( ) ;
2009-12-13 17:15:34 +00:00
2023-06-03 12:51:38 +00:00
if ( Utils : : ignoreKeyEvent ( key_event ) | | key_event - > key ( ) = = Qt : : Key_Return
| | key_event - > key ( ) = = Qt : : Key_Enter )
2009-12-13 17:15:34 +00:00
return false ; // Those key have other uses than to start typing
2023-04-17 15:19:29 +00:00
// or don't make sense
if ( ! text . isEmpty ( ) ) {
2009-12-13 17:15:34 +00:00
typingEvent ( text ) ;
return true ;
}
2009-03-29 17:38:54 +00:00
}
}
2023-04-17 15:19:29 +00:00
return QMainWindow : : eventFilter ( obj , ev ) ;
2009-03-29 17:38:54 +00:00
}
2009-01-28 20:55:45 +00:00
void MainWindow : : wordListItemActivated ( QListWidgetItem * item )
{
2023-05-30 17:24:48 +00:00
if ( wordListSelChanged ) {
2012-11-25 11:37:57 +00:00
wordListSelChanged = false ;
2023-05-30 17:24:48 +00:00
}
2012-12-24 11:30:03 +00:00
else {
2023-05-30 17:24:48 +00:00
respondToTranslationRequest ( item - > text ( ) , true ) ;
2012-12-24 11:30:03 +00:00
}
2009-01-28 20:55:45 +00:00
}
2009-02-08 14:02:27 +00:00
void MainWindow : : wordListSelectionChanged ( )
{
2023-06-05 13:22:20 +00:00
QList < QListWidgetItem * > selected = ui . 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 ( ) ;
2023-06-03 12:51:38 +00:00
if ( selected . size ( ) ) {
2023-05-10 23:17:34 +00:00
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 ,
2023-01-02 14:00:42 +00:00
Contexts const & contexts )
2009-02-08 15:49:17 +00:00
{
2023-06-03 12:51:38 +00:00
createNewTab ( ! cfg . preferences . newTabsOpenInBackground , " " ) - > 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 ,
2023-01-02 14:00:42 +00:00
Contexts const & contexts )
2009-02-08 15:49:17 +00:00
{
2023-06-03 12:51:38 +00:00
createNewTab ( ! cfg . preferences . newTabsOpenInBackground , word ) - > 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
{
2023-06-03 12:51:38 +00:00
if ( view ! = getCurrentArticleView ( ) )
2014-02-09 15:00:48 +00:00
return ; // It was background action
2011-07-03 12:27:08 +00:00
// select the row with the corresponding id
2023-06-03 12:51:38 +00:00
for ( int i = 0 ; i < ui . dictsList - > count ( ) ; + + i ) {
2011-07-03 12:27:08 +00:00
QListWidgetItem * w = ui . dictsList - > item ( i ) ;
2023-06-03 12:51:38 +00:00
QString dictId = w - > data ( Qt : : UserRole ) . toString ( ) ;
2011-07-03 12:27:08 +00:00
2023-06-03 12:51:38 +00:00
if ( dictId = = id ) {
2011-07-03 12:27:08 +00:00
// update the current row, but only if necessary
2023-06-03 12:51:38 +00:00
if ( i ! = ui . dictsList - > currentRow ( ) ) {
ui . dictsList - > setCurrentRow ( i ) ;
2011-07-03 12:27:08 +00:00
}
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 " ) {
2023-06-03 12:51:38 +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 ( ) ;
2023-04-17 15:19:29 +00:00
if ( translateLine - > isEnabled ( ) ) {
translateLine - > clear ( ) ;
translateLine - > setFocus ( ) ;
// Escaping the typed-in characters is the user's responsibility.
2023-05-28 16:01:54 +00:00
setInputLineText ( t , WildcardPolicy : : WildcardsAreAlreadyEscaped , EnablePopup ) ;
2023-04-17 15:19:29 +00:00
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 ) ;
2023-05-28 16:01:54 +00:00
setInputLineText ( word , WildcardPolicy : : EscapeWildcards , DisablePopup ) ;
2013-01-17 09:08:53 +00:00
showTranslationFor ( word ) ;
history . enableAdd ( cfg . preferences . storeHistory ) ;
}
2023-05-28 16:01:54 +00:00
void MainWindow : : showTranslationFor ( QString const & word , unsigned inGroup , QString const & scrollTo )
2009-01-28 20:55:45 +00:00
{
2023-06-03 12:51:38 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2009-01-28 20:55:45 +00:00
2009-04-10 21:07:03 +00:00
navPronounce - > setEnabled ( false ) ;
2023-06-03 12:51:38 +00:00
unsigned group = inGroup ? inGroup : ( groupInstances . empty ( ) ? 0 : groupInstances [ groupList - > currentIndex ( ) ] . id ) ;
2009-10-21 19:37:07 +00:00
2023-05-28 16:01:54 +00:00
view - > showDefinition ( word , group , scrollTo ) ;
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
2023-05-28 16:01:54 +00:00
void MainWindow : : showTranslationForDicts ( QString const & inWord ,
QStringList const & dictIDs ,
QRegExp const & searchRegExp ,
bool ignoreDiacritics )
2014-04-16 16:18:28 +00:00
{
2023-05-28 16:01:54 +00:00
ArticleView * view = getCurrentArticleView ( ) ;
2014-04-16 16:18:28 +00:00
navPronounce - > setEnabled ( false ) ;
2023-05-28 16:01:54 +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
}
2009-04-20 12:25:26 +00:00
void MainWindow : : toggleMainWindow ( bool onlyShow )
{
2010-04-27 07:49:37 +00:00
bool shown = false ;
2023-06-03 12:51:38 +00:00
if ( ! cfg . preferences . searchInDock )
2017-09-01 16:13:29 +00:00
translateBox - > setPopupEnabled ( false ) ;
2023-06-07 12:54:31 +00:00
qDebug ( ) < < " Current state: " < < isVisible ( ) < < isMinimized ( ) < < isActiveWindow ( ) < < onlyShow ;
if ( ! isVisible ( ) ) {
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
}
2023-06-03 12:51:38 +00:00
else if ( isMinimized ( ) ) {
if ( wasMaximized )
2013-01-23 20:36:53 +00:00
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
}
2023-06-03 12:51:38 +00:00
else if ( ! isActiveWindow ( ) ) {
2019-01-31 14:59:24 +00:00
qApp - > setActiveWindow ( this ) ;
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . raiseWindowOnSearch ) {
2023-03-17 01:54:45 +00:00
raise ( ) ;
activateWindow ( ) ;
}
2010-04-27 07:49:37 +00:00
shown = true ;
2009-04-20 12:25:26 +00:00
}
2023-06-03 12:51:38 +00:00
else if ( ! onlyShow ) {
2022-12-29 16:36:01 +00:00
2023-06-03 12:51:38 +00:00
// On Windows and Linux, a hidden window won't show a task bar icon
// When trayicon is enabled, the duplication is unneeded
2022-12-29 16:36:01 +00:00
2023-06-03 12:51:38 +00:00
// On macOS, a hidden window will still show on the Dock,
// but click it won't bring it back, thus we can only minimize it.
2022-12-29 16:36:01 +00:00
# ifdef Q_OS_MAC
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . enableTrayIcon )
showMinimized ( ) ;
2022-12-29 16:36:01 +00:00
# else
2023-06-03 12:51:38 +00:00
if ( cfg . preferences . enableTrayIcon )
hide ( ) ;
2011-06-24 19:03:03 +00:00
else
2023-06-03 12:51:38 +00:00
showMinimized ( ) ;
2022-12-29 16:36:01 +00:00
# endif
2014-03-03 13:46:41 +00:00
2023-06-03 12:51:38 +00:00
if ( headwordsDlg )
2014-03-03 13:46:41 +00:00
headwordsDlg - > hide ( ) ;
2014-04-16 16:18:28 +00:00
2023-06-03 12:51:38 +00:00
if ( ftsDlg )
2014-04-16 16:18:28 +00:00
ftsDlg - > hide ( ) ;
2011-06-24 19:03:03 +00:00
}
2010-04-27 07:49:37 +00:00
if ( shown ) {
2023-06-03 12:51:38 +00:00
if ( headwordsDlg )
2014-03-03 13:46:41 +00:00
headwordsDlg - > show ( ) ;
2023-06-03 12:51:38 +00:00
if ( ftsDlg )
2014-04-16 16:18:28 +00:00
ftsDlg - > show ( ) ;
2010-04-27 07:49:37 +00:00
focusTranslateLine ( ) ;
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 {
2023-06-03 12:51:38 +00:00
hotkeyWrapper = std : : make_shared < HotkeyWrapper > ( this ) ;
2009-04-21 18:27:26 +00:00
}
2023-06-03 12:51:38 +00:00
catch ( HotkeyWrapper : : exInit & ) {
QMessageBox : : critical ( this ,
" GoldenDict " ,
tr ( " Failed to initialize hotkeys monitoring mechanism.<br> "
" Make sure your XServer has RECORD extension turned on. " ) ) ;
2009-04-21 18:27:26 +00:00
return ;
}
2010-03-19 14:17:45 +00:00
if ( cfg . preferences . enableMainWindowHotkey )
2023-06-03 12:51:38 +00:00
hotkeyWrapper - > setGlobalKey ( cfg . preferences . mainWindowHotkey , 0 ) ;
2009-04-21 18:27:26 +00:00
2023-02-23 00:44:29 +00:00
if ( cfg . preferences . enableClipboardHotkey & & ! enableScanningAction - > isChecked ( ) ) {
2023-06-03 12:51:38 +00:00
hotkeyWrapper - > setGlobalKey ( cfg . preferences . clipboardHotkey , 1 ) ;
2022-09-11 01:36:52 +00:00
}
2009-04-21 18:27:26 +00:00
2022-12-26 02:08:17 +00:00
connect ( hotkeyWrapper . get ( ) ,
2023-06-03 12:51:38 +00:00
& 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 ( ) ;
2023-06-03 12:51:38 +00:00
else if ( scanPopup ) {
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.
2023-02-23 00:44:29 +00:00
QTimer : : singleShot ( 10 , scanPopup , SLOT ( translateWordFromClipboard ( ) ) ) ;
2022-05-23 03:27:57 +00:00
# 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
}
2023-07-16 07:33:22 +00:00
void MainWindow : : checkNewRelease ( )
2009-04-20 19:54:34 +00:00
{
2023-07-16 07:33:22 +00:00
// Limit release check to 1 per day.
if ( cfg . timeForNewReleaseCheck < QDateTime : : currentDateTime ( ) . addDays ( 1 ) ) {
return ;
2017-03-20 14:31:02 +00:00
}
2009-04-20 19:54:34 +00:00
2023-07-16 07:33:22 +00:00
QNetworkRequest github_release_api ;
github_release_api . setUrl ( QUrl ( " https://api.github.com/repos/xiaoyifang/goldendict-ng/releases/latest " ) ) ;
github_release_api . setRawHeader ( QByteArray ( " Accept " ) , QByteArray ( " application/vnd.github+json " ) ) ;
// github_release_api.setRawHeader( QByteArray( "Authorization" ), QByteArray( "" ) );
github_release_api . setRawHeader ( QByteArray ( " X-GitHub-Api-Version " ) , QByteArray ( " 2022-11-28 " ) ) ;
2009-04-20 19:54:34 +00:00
2023-07-16 07:33:22 +00:00
auto * github_reply = dictNetMgr . get ( github_release_api ) ; // will be marked as deleteLater when reply finished.
2009-04-20 19:54:34 +00:00
2023-07-16 07:33:22 +00:00
QObject : : connect ( github_reply , & QNetworkReply : : finished , [ github_reply , this ] ( ) {
if ( github_reply - > error ( ) ! = QNetworkReply : : NoError ) {
qWarning ( ) < < " Version check failed: " < < github_reply - > errorString ( ) ;
}
else {
auto latest_release = QJsonDocument : : fromJson ( github_reply - > readAll ( ) ) ;
if ( ! latest_release . isNull ( ) ) {
const QJsonValue tag_name = latest_release [ " tag_name " ] ;
const QJsonValue html_url = latest_release [ " html_url " ] ;
if ( tag_name . isString ( ) & & html_url . isString ( ) ) {
QString latestVersion = tag_name . toString ( ) . mid ( 1 , 8 ) ;
QString downloadUrl = html_url . toString ( ) ;
if ( latestVersion > PROGRAM_VERSION & & latestVersion ! = cfg . skippedRelease ) {
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 ) ;
QPushButton * skip = msg . addButton ( tr ( " Skip This Release " ) , QMessageBox : : DestructiveRole ) ;
msg . addButton ( QMessageBox : : Cancel ) ;
msg . exec ( ) ;
if ( msg . clickedButton ( ) = = dload )
QDesktopServices : : openUrl ( QUrl ( downloadUrl ) ) ;
else if ( msg . clickedButton ( ) = = skip ) {
cfg . skippedRelease = latestVersion ;
}
}
2022-10-15 09:28:55 +00:00
}
}
2009-04-20 19:54:34 +00:00
}
2023-07-16 07:33:22 +00:00
cfg . timeForNewReleaseCheck = QDateTime : : currentDateTime ( ) ;
2009-04-20 19:54:34 +00:00
Config : : save ( cfg ) ;
2023-07-16 07:33:22 +00:00
github_reply - > deleteLater ( ) ;
} ) ;
2009-04-20 19:54:34 +00:00
}
2009-02-08 20:20:02 +00:00
void MainWindow : : trayIconActivated ( QSystemTrayIcon : : ActivationReason r )
{
2023-06-03 12:51:38 +00:00
switch ( r ) {
2010-09-16 18:52:40 +00:00
case QSystemTrayIcon : : Trigger :
// Left click toggles the visibility of main window
toggleMainWindow ( ) ;
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
2023-06-03 12:51:38 +00:00
scanPopup - > translateWordFromSelection ( ) ;
2010-09-16 18:52:40 +00:00
break ;
default :
break ;
}
2009-02-08 20:20:02 +00:00
}
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 ( )
{
2023-01-06 01:34:53 +00:00
QDesktopServices : : openUrl ( QApplication : : organizationDomain ( ) ) ;
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 ( )
{
2023-01-06 01:34:53 +00:00
QDesktopServices : : openUrl ( QUrl ( " https://github.com/xiaoyifang/goldendict/discussions " ) ) ;
2009-02-08 14:02:27 +00:00
}
void MainWindow : : showAbout ( )
{
2023-06-03 12:51:38 +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 ( ) ;
2023-06-03 12:51:38 +00:00
int extent = useSmallIcons ? QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_SmallIconSize ) :
QApplication : : style ( ) - > pixelMetric ( QStyle : : PM_ToolBarIconSize ) ;
2011-05-22 02:42:05 +00:00
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
2023-02-23 00:44:29 +00:00
scanPopup - > setDictionaryIconSize ( ) ;
2011-05-22 02:42:05 +00:00
}
2023-06-03 12:51:38 +00:00
void MainWindow : : toggleMenuBarTriggered ( bool announce )
2011-06-26 11:53:28 +00:00
{
2013-01-04 21:42:25 +00:00
cfg . preferences . hideMenubar = ! toggleMenuBarAction . isChecked ( ) ;
2011-06-26 11:53:28 +00:00
2023-06-03 12:51:38 +00:00
if ( announce ) {
if ( cfg . preferences . hideMenubar ) {
mainStatusBar - > showMessage ( 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
}
2023-06-03 12:51:38 +00:00
else {
2011-06-27 18:54:15 +00:00
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.
2023-06-03 12:51:38 +00:00
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 ( ) ) {
2013-01-06 12:42:18 +00:00
QAction * action = actionsIter . next ( ) ;
2023-06-03 12:51:38 +00:00
if ( ! action - > shortcut ( ) . isEmpty ( ) ) {
if ( cfg . preferences . hideMenubar ) {
2013-01-06 12:42:18 +00:00
// 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 ) ;
2023-06-03 12:51:38 +00:00
beforeOptionsSeparator - > setVisible ( cfg . preferences . hideMenubar ) ;
2012-12-29 18:46:43 +00:00
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 ( ) ;
}
2023-06-03 12:51:38 +00:00
void MainWindow : : setAutostart ( bool autostart )
2009-04-18 18:47:01 +00:00
{
2018-05-06 12:26:55 +00:00
# if defined Q_OS_WIN32
2023-06-03 12:51:38 +00:00
QSettings reg ( " HKEY_CURRENT_USER \\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Run " , QSettings : : NativeFormat ) ;
if ( autostart ) {
QString app_fname = QString ( " \" %1 \" " ) . arg ( QCoreApplication : : applicationFilePath ( ) ) ;
app_fname . replace ( " / " , " \\ " ) ;
2023-06-09 11:56:51 +00:00
reg . setValue ( ApplicationSettingName , app_fname ) ;
2023-06-03 12:51:38 +00:00
}
else {
2023-06-09 11:56:51 +00:00
reg . remove ( ApplicationSettingName ) ;
2023-06-03 12:51:38 +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 " ;
2023-06-03 12:51:38 +00:00
if ( autostart = = QFile : : exists ( destinationPath ) )
2018-05-06 12:26:55 +00:00
return ; // Nothing to do.
2023-06-03 12:51:38 +00:00
if ( autostart ) {
2023-07-24 10:01:32 +00:00
const QString sourcePath =
2023-07-25 12:04:49 +00:00
Config : : getProgramDataDir ( ) + " ../applications/io.github.xiaoyifang.goldendict_ng.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 ( ) ) {
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 ;
2023-06-03 12:51:38 +00:00
QPrintPreviewDialog dialog ( & printer , this ) ;
auto combox = dialog . findChild < QComboBox * > ( ) ;
int index = combox - > findText ( " 100% " ) ;
2022-03-30 09:19:08 +00:00
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
2023-06-03 12:51:38 +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 )
2023-06-03 12:51:38 +00:00
return ;
2009-05-01 12:20:33 +00:00
2023-06-03 12:51:38 +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 )
{
2023-06-03 12:51:38 +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
}
2023-06-03 12:51:38 +00:00
static void filterAndCollectResources ( QString & html ,
QRegExp & rx ,
const QString & sep ,
const QString & folder ,
set < QByteArray > & resourceIncluded ,
2013-05-27 11:18:13 +00:00
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 ) ) ;
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +00:00
downloadResources . push_back ( pair < QUrl , QString > ( url , folder + host + resourcePath ) ) ;
2013-05-27 11:18:13 +00:00
}
// Modify original url, set to the native one
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +00:00
if ( cfg . articleSavePath . isEmpty ( ) )
2013-04-12 06:57:41 +00:00
savePath = QDir : : homePath ( ) ;
2023-06-03 12:51:38 +00:00
else {
2013-04-12 06:57:41 +00:00
savePath = QDir : : fromNativeSeparators ( cfg . articleSavePath ) ;
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +00:00
if ( fileName . isEmpty ( ) )
2022-03-30 16:03:42 +00:00
return ;
2009-05-01 11:17:29 +00:00
2022-03-30 16:03:42 +00:00
view - > toHtml ( [ = ] ( QString & html ) mutable {
QFile file ( fileName ) ;
2023-06-03 12:51:38 +00:00
if ( ! file . open ( QIODevice : : WriteOnly ) ) {
2022-03-30 16:03:42 +00:00
QMessageBox : : critical ( this , tr ( " Error " ) , tr ( " Can't save article: %1 " ) . arg ( file . errorString ( ) ) ) ;
}
2023-06-03 12:51:38 +00:00
else {
2022-03-30 16:03:42 +00:00
QFileInfo fi ( fileName ) ;
cfg . articleSavePath = QDir : : toNativeSeparators ( fi . absoluteDir ( ) . absolutePath ( ) ) ;
2023-07-20 08:02:22 +00:00
2022-03-30 16:03:42 +00:00
// Convert internal links
2023-07-20 08:02:22 +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 ] + _ " ) ;
2023-06-03 12:51:38 +00:00
while ( ( pos = rx3 . indexIn ( html , pos ) ) ! = - 1 ) {
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 ( ' # ' ) ;
2023-06-03 12:51:38 +00:00
if ( n > 0 ) {
2022-03-30 16:03:42 +00:00
anchor = name . mid ( n ) ;
name . truncate ( n ) ;
anchor . replace ( anchorRx , " \\ 1 " ) ; // MDict anchors
2016-05-11 14:44:07 +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 ( ) ;
2023-07-20 08:02:22 +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 ] + _ ) ( ? = [ ^ " '])) " ,
2023-06-03 12:51:38 +00:00
QRegularExpression : : PatternOption : : CaseInsensitiveOption | QRegularExpression : : UseUnicodePropertiesOption ) ;
2022-03-30 16:03:42 +00:00
html . replace ( anchorLinkRe , " \\ 1 " ) ;
2023-07-20 08:02:22 +00:00
2023-06-03 12:51:38 +00:00
if ( complete ) {
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 ;
2023-07-20 08:02:22 +00:00
2022-03-30 16:03:42 +00:00
filterAndCollectResources ( html , rx1 , " \" " , folder , resourceIncluded , downloadResources ) ;
filterAndCollectResources ( html , rx2 , " ' " , folder , resourceIncluded , downloadResources ) ;
2023-07-20 08:02:22 +00:00
2022-03-30 16:03:42 +00:00
ArticleSaveProgressDialog * progressDialog = new ArticleSaveProgressDialog ( this ) ;
// reserve '1' for saving main html file
2023-06-03 12:51:38 +00:00
int maxVal = 1 ;
2023-07-20 08:02:22 +00:00
2022-03-30 16:03:42 +00:00
// Pull and save resources to files
2023-06-03 12:51:38 +00:00
for ( vector < pair < QUrl , QString > > : : const_iterator i = downloadResources . begin ( ) ;
i ! = downloadResources . end ( ) ;
+ + i ) {
2022-03-30 16:03:42 +00:00
ResourceToSaveHandler * handler = view - > saveResource ( i - > first , i - > second ) ;
2023-06-03 12:51:38 +00:00
if ( ! handler - > isEmpty ( ) ) {
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 ) ;
2013-05-27 11:18:13 +00:00
}
2022-03-30 16:03:42 +00:00
}
2023-04-25 13:08:51 +00:00
progressDialog - > setLabelText ( tr ( " Saving article... " ) ) ;
progressDialog - > setRange ( 0 , maxVal ) ;
progressDialog - > setValue ( 0 ) ;
progressDialog - > show ( ) ;
2023-07-20 08:02:22 +00:00
2023-04-25 13:08:51 +00:00
file . write ( html . toUtf8 ( ) ) ;
2022-03-30 16:03:42 +00:00
progressDialog - > perform ( ) ;
2013-05-27 11:18:13 +00:00
}
2023-07-20 08:02:22 +00:00
else {
2022-03-30 16:03:42 +00:00
file . write ( html . toUtf8 ( ) ) ;
2023-07-20 08:02:22 +00:00
}
2017-05-05 14:39:51 +00:00
mainStatusBar - > showMessage ( tr ( " Save article complete " ) , 5000 ) ;
2023-07-20 08:02:22 +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
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 ) ;
2023-06-03 12:51:38 +00:00
dictMap = Dictionary : : dictToMap ( dictionaries ) ;
2010-09-16 18:52:40 +00:00
2023-06-03 12:51:38 +00:00
for ( unsigned x = 0 ; x < dictionaries . size ( ) ; x + + ) {
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 ( ) ;
2023-02-23 00:44:29 +00:00
scanPopup - > refresh ( ) ;
2009-05-08 10:47:34 +00:00
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 )
{
2023-06-03 12:51:38 +00:00
cfg . preferences . alwaysOnTop = checked ;
2012-12-29 11:19:49 +00:00
2023-06-03 12:51:38 +00:00
bool wasVisible = isVisible ( ) ;
2012-12-29 11:19:49 +00:00
2023-06-03 12:51:38 +00:00
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 ( ) ;
}
2012-12-29 11:19:49 +00:00
2023-06-03 12:51:38 +00:00
if ( wasVisible ) {
show ( ) ;
}
2012-12-29 11:19:49 +00:00
2023-06-03 12:51:38 +00:00
installHotKeys ( ) ;
2012-12-29 11:19:49 +00:00
}
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 + + ) {
2023-06-03 12:51:38 +00:00
ArticleView & view = dynamic_cast < ArticleView & > ( * ( ui . tabWidget - > widget ( i ) ) ) ;
2009-04-30 19:57:25 +00:00
view . setZoomFactor ( cfg . preferences . zoomFactor ) ;
}
2009-04-30 20:20:05 +00:00
2023-02-23 00:44:29 +00:00
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 ) ;
}
2023-06-05 13:22:20 +00:00
if ( ui . wordList - > font ( ) . pointSize ( ) ! = ps )
ui . 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 ) {
2023-06-03 12:51:38 +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 ) ;
2023-06-03 12:51:38 +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
2023-06-03 12:51:38 +00:00
if ( ! cfg . preferences . searchInDock ) {
2022-11-14 12:35:42 +00:00
// 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
2023-02-23 00:44:29 +00:00
scanPopup - > applyWordsZoomLevel ( ) ;
2010-07-02 11:19:02 +00:00
}
2010-11-20 13:43:55 +00:00
void MainWindow : : messageFromAnotherInstanceReceived ( QString const & message )
{
2023-04-18 06:52:28 +00:00
if ( message = = " bringToFront " ) {
2010-11-20 13:43:55 +00:00
toggleMainWindow ( true ) ;
2011-11-16 13:02:56 +00:00
return ;
}
2023-04-18 06:52:28 +00:00
if ( message = = " toggleScanPopup " ) {
enableScanningAction - > trigger ( ) ;
return ;
}
2023-06-03 12:51:38 +00:00
if ( message . left ( 15 ) = = " translateWord: " ) {
if ( scanPopup )
2011-11-16 13:02:56 +00:00
scanPopup - > translateWord ( message . mid ( 15 ) ) ;
else
wordReceived ( message . mid ( 15 ) ) ;
}
2023-06-03 12:51:38 +00:00
else if ( message . left ( 10 ) = = " setGroup: " ) {
2017-03-10 13:29:23 +00:00
setGroupByName ( message . mid ( 10 ) , true ) ;
}
2023-06-03 12:51:38 +00:00
else if ( message . left ( 15 ) = = " setPopupGroup: " ) {
2017-03-10 13:29:23 +00:00
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
}
2023-07-17 00:24:02 +00:00
return nullptr ;
2011-07-01 19:22:40 +00:00
}
2011-11-16 12:52:25 +00:00
2023-06-03 12:51:38 +00:00
void MainWindow : : wordReceived ( const QString & word )
2011-11-16 12:52:25 +00:00
{
2023-05-28 16:01:54 +00:00
toggleMainWindow ( true ) ;
setInputLineText ( word , WildcardPolicy : : EscapeWildcards , NoPopupChange ) ;
respondToTranslationRequest ( word , false ) ;
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 ) ;
2023-05-28 16:01:54 +00:00
setInputLineText ( word , WildcardPolicy : : EscapeWildcards , NoPopupChange ) ;
2023-06-03 12:51:38 +00:00
respondToTranslationRequest ( word , false , ArticleView : : scrollToFromDictionaryId ( ID ) ) ;
2014-11-17 14:37:19 +00:00
}
2017-05-05 14:39:51 +00:00
void MainWindow : : updateFavoritesMenu ( )
{
2022-12-27 10:26:40 +00:00
if ( ui . favoritesPane - > isVisible ( ) ) {
2017-05-05 14:39:51 +00:00
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
{
2023-06-03 12:51:38 +00:00
if ( ui . historyPane - > isVisible ( ) ) {
2013-01-17 09:08:53 +00:00
ui . showHideHistory - > setText ( tr ( " &Hide " ) ) ;
}
2023-06-03 12:51:38 +00:00
else {
2013-01-17 09:08:53 +00:00
ui . showHideHistory - > setText ( tr ( " &Show " ) ) ;
}
}
2012-09-12 12:32:29 +00:00
2022-12-27 10:26:40 +00:00
void MainWindow : : toggle_favoritesPane ( )
2017-05-05 14:39:51 +00:00
{
2023-06-03 12:51:38 +00:00
if ( ui . favoritesPane - > isVisible ( ) ) {
2022-12-27 10:26:40 +00:00
ui . favoritesPane - > hide ( ) ;
}
2023-06-03 12:51:38 +00:00
else {
2022-12-27 10:26:40 +00:00
ui . favoritesPane - > show ( ) ;
ui . favoritesPane - > raise ( ) ; // useful when the Pane is tabbed.
}
2017-05-05 14:39:51 +00:00
}
2022-12-27 06:52:12 +00:00
void MainWindow : : toggle_historyPane ( )
2013-01-17 09:08:53 +00:00
{
2023-06-03 12:51:38 +00:00
if ( ui . historyPane - > isVisible ( ) ) {
2022-12-27 06:52:12 +00:00
ui . historyPane - > hide ( ) ;
}
2023-06-03 12:51:38 +00:00
else {
2022-12-27 06:52:12 +00:00
ui . historyPane - > show ( ) ;
ui . historyPane - > raise ( ) ;
}
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
{
2023-06-03 12:51:38 +00:00
QString exportPath ;
if ( cfg . historyExportPath . isEmpty ( ) )
exportPath = QDir : : homePath ( ) ;
else {
exportPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
if ( ! QDir ( exportPath ) . exists ( ) )
exportPath = QDir : : homePath ( ) ;
}
2012-09-10 18:00:29 +00:00
2023-06-03 12:51:38 +00:00
QString fileName = QFileDialog : : getSaveFileName ( this ,
tr ( " Export history to file " ) ,
exportPath ,
tr ( " Text files (*.txt);;All files (*.*) " ) ) ;
if ( fileName . size ( ) = = 0 )
return ;
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
cfg . historyExportPath = QDir : : toNativeSeparators ( QFileInfo ( fileName ) . absoluteDir ( ) . absolutePath ( ) ) ;
QFile file ( fileName ) ;
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
if ( ! file . open ( QFile : : WriteOnly | QIODevice : : Text ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
return ;
}
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
auto guard = qScopeGuard ( [ & file ] ( ) {
file . close ( ) ;
} ) ;
Q_UNUSED ( guard )
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
// Write UTF-8 BOM
QByteArray line ;
line . append ( 0xEF ) . append ( 0xBB ) . append ( 0xBF ) ;
if ( file . write ( line ) ! = line . size ( ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
return ;
}
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
// Write history
QList < History : : Item > const & items = history . getItems ( ) ;
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
QList < History : : Item > : : const_iterator i ;
for ( i = items . constBegin ( ) ; i ! = items . constEnd ( ) ; + + i ) {
line = i - > word . toUtf8 ( ) ;
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
line . replace ( ' \n ' , ' ' ) ;
line . replace ( ' \r ' , ' ' ) ;
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
line + = " \n " ;
2012-02-17 12:01:52 +00:00
2023-06-03 12:51:38 +00:00
if ( file . write ( line ) ! = line . size ( ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
return ;
2012-02-17 12:01:52 +00:00
}
2023-06-03 12:51:38 +00:00
}
mainStatusBar - > showMessage ( tr ( " History export complete " ) , 5000 ) ;
2012-02-17 12:01:52 +00:00
}
2012-09-10 13:01:35 +00:00
2012-12-03 21:57:00 +00:00
void MainWindow : : on_importHistory_triggered ( )
2012-09-10 13:01:35 +00:00
{
2023-06-03 12:51:38 +00:00
QString importPath ;
if ( cfg . historyExportPath . isEmpty ( ) )
importPath = QDir : : homePath ( ) ;
else {
importPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
if ( ! QDir ( importPath ) . exists ( ) )
importPath = QDir : : homePath ( ) ;
}
2012-09-10 18:00:29 +00:00
2023-06-09 13:57:09 +00:00
QString fileName = QFileDialog : : getOpenFileName ( this ,
tr ( " Import history from file " ) ,
importPath ,
tr ( " Text files (*.txt);;All files (*.*) " ) ) ;
if ( fileName . size ( ) = = 0 )
return ;
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
QFileInfo fileInfo ( fileName ) ;
cfg . historyExportPath = QDir : : toNativeSeparators ( fileInfo . absoluteDir ( ) . absolutePath ( ) ) ;
QString errStr ;
QFile file ( fileName ) ;
2023-06-03 12:51:38 +00:00
2023-06-09 13:57:09 +00:00
if ( ! file . open ( QFile : : ReadOnly | QIODevice : : Text ) ) {
errStr = QString ( tr ( " Import error: " ) ) + file . errorString ( ) ;
errorMessageOnStatusBar ( errStr ) ;
return ;
}
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
QTextStream fileStream ( & file ) ;
QString itemStr , trimmedStr ;
QList < QString > itemList ;
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
do {
itemStr = fileStream . readLine ( ) ;
if ( fileStream . status ( ) > = QTextStream : : ReadCorruptData )
break ;
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
trimmedStr = itemStr . trimmed ( ) ;
if ( trimmedStr . isEmpty ( ) )
continue ;
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
if ( ( unsigned ) trimmedStr . size ( ) < = history . getMaxItemLength ( ) )
itemList . prepend ( trimmedStr ) ;
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
} while ( ! fileStream . atEnd ( ) & & itemList . size ( ) < ( int ) history . getMaxSize ( ) ) ;
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
history . enableAdd ( true ) ;
2013-01-17 09:08:53 +00:00
2023-06-09 13:57:09 +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
2023-06-09 13:57:09 +00:00
history . enableAdd ( cfg . preferences . storeHistory ) ;
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
if ( file . error ( ) ! = QFile : : NoError ) {
errStr = QString ( tr ( " Import error: " ) ) + file . errorString ( ) ;
errorMessageOnStatusBar ( errStr ) ;
return ;
}
2012-09-10 13:01:35 +00:00
2023-06-09 13:57:09 +00:00
if ( fileStream . status ( ) > = QTextStream : : ReadCorruptData ) {
errStr = QString ( tr ( " Import error: invalid data in file " ) ) ;
errorMessageOnStatusBar ( errStr ) ;
return ;
}
//even without this line, the destructor of QFile will close the file as documented.
file . close ( ) ;
mainStatusBar - > showMessage ( tr ( " History import complete " ) , 5000 ) ;
2023-06-03 12:51:38 +00:00
}
2023-06-09 13:57:09 +00:00
2023-06-03 12:51:38 +00:00
void MainWindow : : errorMessageOnStatusBar ( const QString & errStr )
{
this - > 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 ;
2023-06-03 12:51:38 +00:00
if ( cfg . historyExportPath . isEmpty ( ) )
2017-05-05 14:39:51 +00:00
exportPath = QDir : : homePath ( ) ;
2023-06-03 12:51:38 +00:00
else {
2017-05-05 14:39:51 +00:00
exportPath = QDir : : fromNativeSeparators ( cfg . historyExportPath ) ;
2023-06-03 12:51:38 +00:00
if ( ! QDir ( exportPath ) . exists ( ) )
2017-05-05 14:39:51 +00:00
exportPath = QDir : : homePath ( ) ;
}
2023-06-03 12:51:38 +00:00
QString fileName = QFileDialog : : getSaveFileName ( this ,
tr ( " Export Favorites to file " ) ,
2017-05-05 14:39:51 +00:00
exportPath ,
tr ( " XML files (*.xml);;All files (*.*) " ) ) ;
2023-06-03 12:51:38 +00:00
if ( fileName . size ( ) = = 0 )
2017-05-05 14:39:51 +00:00
return ;
cfg . historyExportPath = QDir : : toNativeSeparators ( QFileInfo ( fileName ) . absoluteDir ( ) . absolutePath ( ) ) ;
QFile file ( fileName ) ;
2023-06-03 12:51:38 +00:00
if ( ! file . open ( QFile : : WriteOnly | QIODevice : : Text ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
return ;
}
2017-05-05 14:39:51 +00:00
QByteArray data ;
ui . favoritesPaneWidget - > getDataInXml ( data ) ;
2023-06-03 12:51:38 +00:00
if ( file . write ( data ) ! = data . size ( ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
return ;
}
2017-05-05 14:39:51 +00:00
file . close ( ) ;
mainStatusBar - > showMessage ( tr ( " Favorites export complete " ) , 5000 ) ;
}
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 ( ) ;
}
2023-06-03 12:51:38 +00:00
QString fileName = QFileDialog : : getSaveFileName ( this ,
tr ( " Export Favorites to file as plain list " ) ,
2017-05-05 14:39:51 +00:00
exportPath ,
tr ( " Text files (*.txt);;All files (*.*) " ) ) ;
2023-06-03 12:51:38 +00:00
if ( fileName . size ( ) = = 0 )
2017-05-05 14:39:51 +00:00
return ;
cfg . historyExportPath = QDir : : toNativeSeparators ( QFileInfo ( fileName ) . absoluteDir ( ) . absolutePath ( ) ) ;
QFile file ( fileName ) ;
2023-06-03 12:51:38 +00:00
if ( ! file . open ( QFile : : WriteOnly | QIODevice : : Text ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
return ;
}
2017-05-05 14:39:51 +00:00
2023-06-03 12:51:38 +00:00
// Write UTF-8 BOM
QByteArray line ;
line . append ( 0xEF ) . append ( 0xBB ) . append ( 0xBF ) ;
if ( file . write ( line ) ! = line . size ( ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
return ;
}
2017-05-05 14:39:51 +00:00
2023-06-03 12:51:38 +00:00
// Write Favorites
QString data ;
ui . favoritesPaneWidget - > getDataInPlainText ( data ) ;
2017-05-05 14:39:51 +00:00
2023-06-03 12:51:38 +00:00
line = data . toUtf8 ( ) ;
if ( file . write ( line ) ! = line . size ( ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Export error: " ) ) + file . errorString ( ) ) ;
2017-05-05 14:39:51 +00:00
return ;
}
2023-06-03 12:51:38 +00:00
2017-05-05 14:39:51 +00:00
file . close ( ) ;
2023-06-03 12:51:38 +00:00
mainStatusBar - > showMessage ( tr ( " Favorites export complete " ) , 5000 ) ;
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 (*.*) " ) ) ;
2023-06-03 12:51:38 +00:00
if ( fileName . size ( ) = = 0 )
2017-05-05 14:39:51 +00:00
return ;
QFileInfo fileInfo ( fileName ) ;
cfg . historyExportPath = QDir : : toNativeSeparators ( fileInfo . absoluteDir ( ) . absolutePath ( ) ) ;
QString errStr ;
QFile file ( fileName ) ;
2023-06-03 12:51:38 +00:00
if ( ! file . open ( QFile : : ReadOnly | QIODevice : : Text ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Import error: " ) ) + file . errorString ( ) ) ;
return ;
}
2017-05-05 14:39:51 +00:00
2023-06-03 12:51:38 +00:00
QByteArray data = file . readAll ( ) ;
2017-05-05 14:39:51 +00:00
2023-06-03 12:51:38 +00:00
if ( fileInfo . suffix ( ) . compare ( " txt " , Qt : : CaseInsensitive ) = = 0 ) {
if ( ! ui . favoritesPaneWidget - > setDataFromTxt ( QString : : fromUtf8 ( data . data ( ) , data . size ( ) ) ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Data parsing error " ) ) ) ;
return ;
2022-11-12 04:31:03 +00:00
}
2023-06-03 12:51:38 +00:00
}
else {
if ( ! ui . favoritesPaneWidget - > setDataFromXml ( QString : : fromUtf8 ( data . data ( ) , data . size ( ) ) ) ) {
errorMessageOnStatusBar ( QString ( tr ( " Data parsing error " ) ) ) ;
return ;
2022-11-12 04:31:03 +00:00
}
2017-05-05 14:39:51 +00:00
}
file . close ( ) ;
2023-06-03 12:51:38 +00:00
mainStatusBar - > showMessage ( tr ( " Favorites import complete " ) , 5000 ) ;
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 ( ) ;
2023-06-03 12:51:38 +00:00
for ( const auto & item : items ) {
History : : Item const * i = & item ;
auto 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 ) ;
2012-09-10 16:23:58 +00:00
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 )
{
2023-05-28 16:01:54 +00:00
// skip epwing reference link. epwing reference link has the pattern of r%dAt%d
2023-03-20 15:47:23 +00:00
if ( QRegularExpressionMatch m = RX : : Epwing : : refWord . match ( word ) ; m . hasMatch ( ) )
2023-03-20 12:04:43 +00:00
return ;
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
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 )
{
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 ) {
2023-04-12 19:52:35 +00:00
showDictionaryHeadwords ( 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 ;
}
}
}
2023-04-12 19:52:35 +00:00
void MainWindow : : showDictionaryHeadwords ( Dictionary : : Class * dict )
2014-03-01 13:11:14 +00:00
{
2015-03-23 15:58:49 +00:00
2023-04-12 19:52:35 +00:00
QWidget * owner = qobject_cast < QWidget * > ( sender ( ) ) ;
2015-03-23 15:58:49 +00:00
2023-04-12 19:52:35 +00:00
// DictHeadwords internally check parent== mainwindow to know why it is requested.
// If the DictHeadwords is requested by Edit->Dictionaries->ShowHeadWords, (owner = "EditDictionaries")
// it will be a modal dialog. When click a word, that word will NOT be queried.
// In all other cases, just set owner = mainwindow(this),
2015-03-23 15:58:49 +00:00
2023-04-12 19:52:35 +00:00
if ( owner - > objectName ( ) = = " EditDictionaries " ) {
2015-03-23 15:58:49 +00:00
DictHeadwords headwords ( owner , cfg , dict ) ;
headwords . exec ( ) ;
}
2023-04-12 19:52:35 +00:00
else {
if ( headwordsDlg = = nullptr ) {
headwordsDlg = new DictHeadwords ( this , cfg , dict ) ;
addGlobalActionsToDialog ( headwordsDlg ) ;
addGroupComboBoxActionsToDialog ( headwordsDlg , groupList ) ;
connect ( headwordsDlg , & DictHeadwords : : headwordSelected , this , & MainWindow : : headwordReceived ) ;
connect ( headwordsDlg ,
& DictHeadwords : : closeDialog ,
this ,
& MainWindow : : closeHeadwordsDialog ,
Qt : : QueuedConnection ) ;
}
else {
headwordsDlg - > setup ( dict ) ;
}
headwordsDlg - > show ( ) ;
2014-03-03 13:46:41 +00:00
}
}
2014-03-01 13:11:14 +00:00
2023-04-12 19:52:35 +00:00
2014-03-03 13:46:41 +00:00
void MainWindow : : closeHeadwordsDialog ( )
{
if ( headwordsDlg ) {
delete headwordsDlg ;
2023-07-17 00:24:02 +00:00
headwordsDlg = nullptr ;
2014-03-03 13:46:41 +00:00
}
2014-03-01 13:11:14 +00:00
}
2014-03-04 16:58:51 +00:00
void MainWindow : : focusHeadwordsDialog ( )
{
if ( headwordsDlg ) {
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 ( ) ) {
2023-04-14 03:53:23 +00:00
if ( ! dictionaries [ x ] - > getDictionaryFilenames ( ) . empty ( ) ) {
QDesktopServices : : openUrl ( QUrl : : fromLocalFile ( dictionaries [ x ] - > getContainingFolder ( ) ) ) ;
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 ( ) ;
2023-07-17 00:24:02 +00:00
Dictionary : : Class * pDict = nullptr ;
2012-11-29 17:12:54 +00:00
2013-06-09 13:31:57 +00:00
for ( unsigned i = 0 ; i < dictionaries . size ( ) ; i + + ) {
if ( id . compare ( dictionaries [ i ] - > getId ( ) . c_str ( ) ) = = 0 ) {
pDict = dictionaries [ i ] . get ( ) ;
break ;
2012-11-29 17:12:54 +00:00
}
}
2023-07-17 00:24:02 +00:00
if ( pDict = = nullptr )
2013-06-09 13:31:57 +00:00
return ;
if ( ! pDict - > isLocalDictionary ( ) ) {
2013-05-30 15:45:20 +00:00
if ( scanPopup )
2023-02-23 00:44:29 +00:00
scanPopup - > blockSignals ( true ) ;
2012-11-28 19:32:37 +00:00
showDictionaryInfo ( id ) ;
2013-05-30 15:45:20 +00:00
if ( scanPopup )
2023-02-23 00:44:29 +00:00
scanPopup - > 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
2023-07-17 00:24:02 +00:00
QAction * headwordsAction = nullptr ;
2014-03-01 13:11:14 +00:00
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 " ) ) ;
2023-07-17 00:24:02 +00:00
QAction * editAction = nullptr ;
2013-06-09 13:31:57 +00:00
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 )
2023-02-23 00:44:29 +00:00
scanPopup - > blockSignals ( true ) ;
2012-11-28 19:32:37 +00:00
showDictionaryInfo ( id ) ;
2013-05-30 15:45:20 +00:00
if ( scanPopup )
2023-02-23 00:44:29 +00:00
scanPopup - > blockSignals ( false ) ;
2012-11-28 19:32:37 +00:00
}
2014-03-01 13:11:14 +00:00
else if ( result & & result = = headwordsAction ) {
if ( scanPopup )
2023-02-23 00:44:29 +00:00
scanPopup - > blockSignals ( true ) ;
2023-04-12 19:52:35 +00:00
showDictionaryHeadwords ( pDict ) ;
2014-03-01 13:11:14 +00:00
if ( scanPopup )
2023-02-23 00:44:29 +00:00
scanPopup - > blockSignals ( false ) ;
2014-03-01 13:11:14 +00:00
}
2013-06-09 13:31:57 +00:00
else if ( result & & result = = openDictFolderAction ) {
openDictionaryFolder ( id ) ;
}
2012-11-28 19:32:37 +00:00
else 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 )
{
2023-05-28 16:01:54 +00:00
setInputLineText ( word , WildcardPolicy : : 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 ) ;
2023-04-20 08:56:14 +00:00
ftsDlg - > setSearchText ( translateLine - > text ( ) ) ;
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
2023-05-28 16:01:54 +00:00
connect ( ftsDlg , & FTS : : FullTextSearchDialog : : showTranslationFor , this , & MainWindow : : showTranslationForDicts ) ;
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 ( ) ) ) ;
2023-07-20 08:02:22 +00:00
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 {
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 ;
2023-07-17 00:24:02 +00:00
ftsDlg = nullptr ;
2014-04-16 16:18:28 +00:00
}
}
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 ( " && " , " & " ) ;
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
}
2023-04-25 00:12:21 +00:00
void MainWindow : : addWordToFavorites ( QString const & word , unsigned groupId , bool exist )
2017-05-15 15:08:06 +00:00
{
QString folder ;
Instances : : Group const * igrp = groupInstances . findGroup ( groupId ) ;
if ( igrp )
folder = igrp - > favoritesFolder ;
2023-04-25 00:12:21 +00:00
if ( ! exist )
ui . favoritesPaneWidget - > addHeadword ( folder , word ) ;
else
ui . favoritesPaneWidget - > removeHeadword ( folder , word ) ;
2017-05-15 15:08:06 +00:00
}
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 ) {
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 ( " ~~~ " ) ;
2023-05-28 16:01:54 +00:00
setInputLineText ( words [ 0 ] , WildcardPolicy : : EscapeWildcards , DisablePopup ) ;
2022-06-01 15:11:41 +00:00
//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
}