goldendict-ng/scanpopup.hh

238 lines
7.1 KiB
C++
Raw Normal View History

2012-02-20 21:47:14 +00:00
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __SCANPOPUP_HH_INCLUDED__
#define __SCANPOPUP_HH_INCLUDED__
#include "article_netmgr.hh"
#include "ui/articleview.h"
#include "wordfinder.hh"
#include "keyboardstate.hh"
#include "config.hh"
#include "ui_scanpopup.h"
#include <QDialog>
#include <QClipboard>
#include "history.hh"
#include "dictionarybar.hh"
#include "mainstatusbar.hh"
#ifdef HAVE_X11
#include "scanflag.hh"
#endif
/// This is a popup dialog to show translations when clipboard scanning mode
/// is enabled.
class ScanPopup: public QMainWindow, KeyboardState
{
Q_OBJECT
public:
ScanPopup( QWidget * parent,
Config::Class & cfg,
ArticleNetworkAccessManager &,
AudioPlayerPtr const &,
std::vector< sptr< Dictionary::Class > > const & allDictionaries,
Instances::Groups const &,
History & );
~ScanPopup();
// update dictionary bar, group data and possibly other data
void refresh();
/// Applies current zoom factor to the popup's view. Should be called when
/// it's changed.
void applyZoomFactor();
void applyWordsZoomLevel();
/// Translate the word
void translateWord( QString const & word );
void setDictionaryIconSize();
void saveConfigData();
#ifdef HAVE_X11
/// Interaction with scan flag window
void showScanFlag();
void hideScanFlag();
QTimer selectionDelayTimer;
#endif
signals:
/// Forwarded from the dictionary bar, so that main window could act on this.
void editGroupRequested( unsigned id );
/// Send word to main window
void sendPhraseToMainWindow( Config::InputPhrase const & phrase );
/// Close opened menus when window hide
void closeMenu();
void inspectSignal(QWebEnginePage * page);
/// Signal to switch expand optional parts mode
void switchExpandMode();
/// Signal to add word to history even if history is disabled
void forceAddWordToHistory( const QString & word);
/// Retranslate signal from dictionary bar
void showDictionaryInfo( QString const & id );
void openDictionaryFolder( QString const & id );
/// Put translated word into history
void sendWordToHistory( QString const & word );
/// Put translated word into Favorites
void sendWordToFavorites( QString const & word, unsigned groupId );
/// Check is word already presented in Favorites
bool isWordPresentedInFavorites( QString const & word, unsigned groupId );
#ifdef Q_OS_WIN32
/// Ask for source window is current translate tab
bool isGoldenDictWindow( HWND hwnd );
#endif
public slots:
void requestWindowFocus();
void inspectElementWhenPinned( QWebEnginePage * page );
/// Translates the word from the clipboard, showing the window etc.
void translateWordFromClipboard();
/// Translates the word from the clipboard selection
void translateWordFromSelection();
/// From the dictionary bar.
void editGroupRequested();
void setGroupByName( QString const & name );
#ifdef HAVE_X11
void showEngagePopup();
#endif
private:
Allow customizing unpinned scan popup window flags on X11 with Qt5 My tests in many desktop environments and window managers indicate that no single configuration works perfectly in all environments. There are also behavior differences between Qt::Popup and Qt::Tool flags, which are not exactly bugs, so I suppose users might subjectively prefer different options. Customizing the flags allows the user to prevent unpinned scan popup window flickering with Qt5 on Linux. In a way adding these options fixes issue #645, which is: the scan popup window blinks rapidly, barely noticeably in some applications, such as Calibre ebook-viewer and Chromium. In this case the scan popup window usually ends up hidden when selection ends, unless it was finished with a jerk. I have tested the new options in 9 desktop environments and window managers: at least one configuration for each eliminates #645 and makes the scan popup window work the same as with Qt4 in this regard: the popup window remains visible, text in the popup's translation line keeps up with the text selection in the external application, and the selected text is being translated on the fly. Moreover, for each tested DE/WM, at least one configuration makes the scan popup window work perfectly as far as I am concerned. This issue was partially worked around with a 200ms scan popup delay timer in the recent commit 58e41fe3ceb769cab37608e36637044e597ba1f8 for the duplicate issue #854. However the timer solution is incomplete because it requires the user to select text quickly and without delays. If global mouse selection does not change for 200ms while the left mouse button is held down, the user will likely not see the scan popup when (s)he finishes selection, and will have to try selecting again - hopefully faster this time. The 200ms delay is no longer critically important after this commit, but it is still beneficial: the lookup query changes less often, which in turn reduces article definition update frequency. So the delay improves the UI (perhaps subjectively) and performance.
2018-04-09 17:50:23 +00:00
Qt::WindowFlags unpinnedWindowFlags() const;
// Translates the word from the clipboard or the clipboard selection
void translateWordFromClipboard(QClipboard::Mode m);
// Hides the popup window, effectively closing it.
void hideWindow();
// Grabs mouse and installs global event filter to track it thoroughly.
void interceptMouse();
// Ungrabs mouse and uninstalls global event filter.
void uninterceptMouse();
void updateDictionaryBar();
Config::Class & cfg;
std::vector< sptr< Dictionary::Class > > const & allDictionaries;
std::vector< sptr< Dictionary::Class > > dictionariesUnmuted;
Instances::Groups const & groups;
History & history;
Ui::ScanPopup ui;
ArticleView * definition;
QAction escapeAction, switchExpandModeAction, focusTranslateLineAction;
2014-04-16 16:18:28 +00:00
QAction openSearchAction;
Config::InputPhrase pendingInputPhrase, inputPhrase;
QString translateBoxSuffix; ///< A punctuation suffix that corresponds to translateBox's text.
WordFinder wordFinder;
Config::Events configEvents;
DictionaryBar dictionaryBar;
MainStatusBar * mainStatusBar;
/// Fonts saved before words zooming is in effect, so it could be reset back.
QFont wordListDefaultFont, translateLineDefaultFont, groupListDefaultFont;
#ifdef HAVE_X11
ScanFlag * scanFlag;
#endif
bool mouseEnteredOnce;
bool mouseIntercepted;
QPoint startPos; // For window moving
QByteArray pinnedGeometry;
QTimer hideTimer; // When mouse leaves the window, a grace period is
// given for it to return back. If it doesn't before
// this timer expires, the window gets hidden.
QTimer mouseGrabPollTimer;
QIcon starIcon, blueStarIcon;
void handleInputWord( QString const & , bool forcePopup = false );
void engagePopup( bool forcePopup, bool giveFocus = false );
vector< sptr< Dictionary::Class > > const & getActiveDicts();
virtual bool eventFilter( QObject * watched, QEvent * event );
/// Called from event filter or from mouseGrabPoll to handle mouse event
/// while it is being intercepted.
void reactOnMouseMove( QPoint const & p );
virtual void mousePressEvent( QMouseEvent * );
virtual void mouseMoveEvent( QMouseEvent * );
virtual void mouseReleaseEvent( QMouseEvent * );
virtual void leaveEvent( QEvent * event );
2022-02-27 05:17:37 +00:00
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
virtual void enterEvent( QEnterEvent * event );
#else
virtual void enterEvent( QEvent * event );
2022-02-27 05:17:37 +00:00
#endif
virtual void showEvent( QShowEvent * );
virtual void closeEvent( QCloseEvent * );
virtual void moveEvent( QMoveEvent * );
/// Returns inputWord, chopped with appended ... if it's too long/
QString elideInputWord();
void updateBackForwardButtons();
void showTranslationFor( Config::InputPhrase const & inputPhrase );
void updateSuggestionList();
void updateSuggestionList( QString const & text );
private slots:
void mouseHovered( QString const & , bool forcePopup);
void currentGroupChanged( int );
void prefixMatchFinished();
void on_pronounceButton_clicked();
void pinButtonClicked( bool checked );
void on_showDictionaryBar_clicked( bool checked );
void showStatusBarMessage ( QString const &, int, QPixmap const & );
void on_sendWordButton_clicked();
void on_sendWordToFavoritesButton_clicked();
void on_goBackButton_clicked();
void on_goForwardButton_clicked();
void hideTimerExpired();
/// Called repeatedly once the popup is initially engaged and we monitor the
/// mouse as it may move away from the window. This simulates mouse grab, in
2017-09-16 13:14:26 +00:00
/// essence, but seems more reliable. Once the mouse enters the window, the
/// polling stops.
void mouseGrabPoll();
void pageLoaded( ArticleView * );
2009-05-16 11:14:43 +00:00
void escapePressed();
void mutedDictionariesChanged();
void switchExpandOptionalPartsMode();
void translateInputChanged(QString const & text);
void translateInputFinished();
void wordListItemActivated( QListWidgetItem * );
void focusTranslateLine();
void typingEvent( QString const & );
void alwaysOnTopClicked( bool checked );
2017-11-07 14:46:59 +00:00
void titleChanged( ArticleView *, QString const & title );
};
#endif