feature: enhance clipboard data processing

when copied text from some applications such as Kindle PC, the copied text will have extra informations added by
the application . This option used to strip the added text away
This commit is contained in:
Xiao YiFang 2022-12-13 21:42:53 +08:00
parent e107350b51
commit 41d05cb27a
4 changed files with 32 additions and 3 deletions

View file

@ -182,14 +182,24 @@ InputPhrase Preferences::sanitizeInputPhrase( QString const & inputPhrase ) cons
{ {
InputPhrase result; InputPhrase result;
if( limitInputPhraseLength && inputPhrase.size() > inputPhraseLengthLimit ) QString _phase = inputPhrase;
if( stripClipboard )
{
auto parts = inputPhrase.split( QChar::LineFeed, Qt::SkipEmptyParts );
if( !parts.empty() )
{
_phase = parts[ 0 ];
}
}
if( limitInputPhraseLength && _phase.size() > inputPhraseLengthLimit )
{ {
gdDebug( "Ignoring an input phrase %d symbols long. The configured maximum input phrase length is %d symbols.", gdDebug( "Ignoring an input phrase %d symbols long. The configured maximum input phrase length is %d symbols.",
inputPhrase.size(), inputPhraseLengthLimit ); _phase.size(), inputPhraseLengthLimit );
return result; return result;
} }
const QString withPunct = inputPhrase.simplified(); const QString withPunct = _phase.simplified();
result.phrase = gd::toQString( Folding::trimWhitespaceOrPunct( gd::toWString( withPunct ) ) ); result.phrase = gd::toQString( Folding::trimWhitespaceOrPunct( gd::toWString( withPunct ) ) );
if ( !result.isValid() ) if ( !result.isValid() )
return result; // The suffix of an invalid input phrase must be empty. return result; // The suffix of an invalid input phrase must be empty.
@ -267,6 +277,7 @@ Preferences::Preferences():
, inputPhraseLengthLimit( 1000 ) , inputPhraseLengthLimit( 1000 )
, maxDictionaryRefsInContextMenu ( 20 ) , maxDictionaryRefsInContextMenu ( 20 )
, synonymSearchEnabled( true ) , synonymSearchEnabled( true )
, stripClipboard( false )
{ {
} }
@ -1005,6 +1016,9 @@ Class load()
if ( !preferences.namedItem( "synonymSearchEnabled" ).isNull() ) if ( !preferences.namedItem( "synonymSearchEnabled" ).isNull() )
c.preferences.synonymSearchEnabled = ( preferences.namedItem( "synonymSearchEnabled" ).toElement().text() == "1" ); c.preferences.synonymSearchEnabled = ( preferences.namedItem( "synonymSearchEnabled" ).toElement().text() == "1" );
if ( !preferences.namedItem( "stripClipboard" ).isNull() )
c.preferences.stripClipboard = ( preferences.namedItem( "stripClipboard" ).toElement().text() == "1" );
QDomNode fts = preferences.namedItem( "fullTextSearch" ); QDomNode fts = preferences.namedItem( "fullTextSearch" );
if ( !fts.isNull() ) if ( !fts.isNull() )
@ -1960,6 +1974,10 @@ void save( Class const & c )
opt.appendChild( dd.createTextNode( c.preferences.synonymSearchEnabled ? "1" : "0" ) ); opt.appendChild( dd.createTextNode( c.preferences.synonymSearchEnabled ? "1" : "0" ) );
preferences.appendChild( opt ); preferences.appendChild( opt );
opt = dd.createElement( "stripClipboard" );
opt.appendChild( dd.createTextNode( c.preferences.stripClipboard ? "1" : "0" ) );
preferences.appendChild( opt );
{ {
QDomNode hd = dd.createElement( "fullTextSearch" ); QDomNode hd = dd.createElement( "fullTextSearch" );
preferences.appendChild( hd ); preferences.appendChild( hd );

View file

@ -372,6 +372,7 @@ struct Preferences
unsigned short maxDictionaryRefsInContextMenu; unsigned short maxDictionaryRefsInContextMenu;
bool synonymSearchEnabled; bool synonymSearchEnabled;
bool stripClipboard;
QString addonStyle; QString addonStyle;

View file

@ -227,6 +227,8 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
ui.synonymSearchEnabled->setChecked( p.synonymSearchEnabled ); ui.synonymSearchEnabled->setChecked( p.synonymSearchEnabled );
ui.stripClipboard->setChecked( p.stripClipboard );
ui.maxDictsInContextMenu->setValue( p.maxDictionaryRefsInContextMenu ); ui.maxDictsInContextMenu->setValue( p.maxDictionaryRefsInContextMenu );
// Different platforms have different keys available // Different platforms have different keys available
@ -443,6 +445,7 @@ Config::Preferences Preferences::getPreferences()
p.inputPhraseLengthLimit = ui.inputPhraseLengthLimit->value(); p.inputPhraseLengthLimit = ui.inputPhraseLengthLimit->value();
p.ignoreDiacritics = ui.ignoreDiacritics->isChecked(); p.ignoreDiacritics = ui.ignoreDiacritics->isChecked();
p.ignorePunctuation = ui.ignorePunctuation->isChecked(); p.ignorePunctuation = ui.ignorePunctuation->isChecked();
p.stripClipboard = ui.stripClipboard->isChecked();
p.synonymSearchEnabled = ui.synonymSearchEnabled->isChecked(); p.synonymSearchEnabled = ui.synonymSearchEnabled->isChecked();

View file

@ -1743,6 +1743,13 @@ from Stardict, Babylon and GLS dictionaries</string>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="stripClipboard">
<property name="text">
<string>When using clipboard,strip everything after newline</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer_17"> <spacer name="verticalSpacer_17">
<property name="orientation"> <property name="orientation">