opt: add translation windows when invoked from console (#1173)

* opt: add an extra console argument to specify the translation windows

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

---------

Co-authored-by: YiFang Xiao <yifang.xiao@noreply.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
xiaoyifang 2023-09-30 11:40:40 +08:00 committed by GitHub
parent 3b70c29f01
commit ef6e878a31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 5 deletions

View file

@ -148,6 +148,7 @@ struct GDOptions
bool logFile = false;
bool togglePopup = false;
QString word, groupName, popupGroupName;
QString window;
inline bool needSetGroup() const
{
@ -220,6 +221,14 @@ void processCommandLine( QCoreApplication * app, GDOptions * result )
QObject::tr( "Change the group of popup." ),
"popupGroupName" );
QCommandLineOption window_popupOption( QStringList() << "s"
<< "scanpopup",
QObject::tr( "Force the word to be translated in scanpopup" ) );
QCommandLineOption window_mainWindowOption( QStringList() << "m"
<< "main-window",
QObject::tr( "Force the word to be translated in the mainwindow" ) );
QCommandLineOption togglePopupOption( QStringList() << "t"
<< "toggle-scan-popup",
QObject::tr( "Toggle scan popup." ) );
@ -231,6 +240,8 @@ void processCommandLine( QCoreApplication * app, GDOptions * result )
qcmd.addOption( logFileOption );
qcmd.addOption( groupNameOption );
qcmd.addOption( popupGroupNameOption );
qcmd.addOption( window_popupOption );
qcmd.addOption( window_mainWindowOption );
qcmd.addOption( togglePopupOption );
qcmd.addOption( notts );
qcmd.addOption( resetState );
@ -253,7 +264,12 @@ void processCommandLine( QCoreApplication * app, GDOptions * result )
if ( qcmd.isSet( popupGroupNameOption ) ) {
result->popupGroupName = qcmd.value( popupGroupNameOption );
}
if ( qcmd.isSet( window_popupOption ) ) {
result->window = "popup";
}
if ( qcmd.isSet( window_mainWindowOption ) ) {
result->window = "main";
}
if ( qcmd.isSet( togglePopupOption ) ) {
result->togglePopup = true;
}
@ -414,6 +430,7 @@ int main( int argc, char ** argv )
if ( app.isRunning() ) {
bool wasMessage = false;
//TODO .all the following messages can be combined into one.
if ( gdcl.needSetGroup() ) {
app.sendMessage( QString( "setGroup: " ) + gdcl.getGroupName() );
wasMessage = true;
@ -424,6 +441,11 @@ int main( int argc, char ** argv )
wasMessage = true;
}
if ( !gdcl.window.isEmpty() ) {
app.sendMessage( QString( "window:" ) + gdcl.window );
wasMessage = true;
}
if ( gdcl.needTranslateWord() ) {
app.sendMessage( QString( "translateWord: " ) + gdcl.wordToTranslate() );
wasMessage = true;

View file

@ -3548,11 +3548,28 @@ void MainWindow::messageFromAnotherInstanceReceived( QString const & message )
return;
}
QString prefix = "window:";
if ( message.left( prefix.size() ) == prefix ) {
consoleWindowOnce = message.mid( prefix.size() );
}
if ( message.left( 15 ) == "translateWord: " ) {
if ( scanPopup )
scanPopup->translateWord( message.mid( 15 ) );
else
wordReceived( message.mid( 15 ) );
auto word = message.mid( 15 );
if ( ( consoleWindowOnce == "popup" ) && scanPopup ) {
scanPopup->translateWord( word );
}
else if ( consoleWindowOnce == "main" ) {
wordReceived( word );
}
else {
//default logic
if ( scanPopup && enableScanningAction->isChecked() )
scanPopup->translateWord( word );
else
wordReceived( word );
}
consoleWindowOnce.clear();
}
else if ( message.left( 10 ) == "setGroup: " ) {
setGroupByName( message.mid( 10 ), true );

View file

@ -149,6 +149,9 @@ private:
ScanPopup * scanPopup = nullptr;
//only used once, when used ,reset to empty.
QString consoleWindowOnce;
sptr< HotkeyWrapper > hotkeyWrapper;
sptr< QPrinter > printer; // The printer we use for all printing operations