Commit 716b2341 authored by Joao Oliveira's avatar Joao Oliveira Committed by Albert Astals Cid

Implemented find function from console

Summary:
Implemented feature request to find from console.

BUG: 362038

Reviewers: #okular, aacid

Reviewed By: #okular, aacid

Subscribers: ngraham, aacid, yurchor, okular-devel

Tags: #okular

Differential Revision: https://phabricator.kde.org/D18144
parent 241bc652
This diff is collapsed.
......@@ -3773,7 +3773,7 @@ void Document::searchText( int searchID, const QString & text, bool fromStart, Q
SearchType type, bool moveViewport, const QColor & color )
{
d->m_searchCancelled = false;
// safety checks: don't perform searches on empty or unsearchable docs
if ( !d->m_generator || !d->m_generator->hasFeature( Generator::TextExtraction ) || d->m_pagesVector.isEmpty() )
{
......
......@@ -2022,7 +2022,7 @@ bool PDFGenerator::save( const QString &fileName, SaveOptions options, QString *
{
Q_UNUSED(errorText);
Poppler::PDFConverter *pdfConv = pdfdoc->pdfConverter();
pdfConv->setOutputFileName( fileName );
if ( options & SaveChanges )
pdfConv->setPDFOptions( pdfConv->pdfOptions() | Poppler::PDFConverter::WithChanges );
......
......@@ -1634,6 +1634,14 @@ bool Part::openFile()
if ( keepFileOpen() )
m_keeper->open( fileNameToOpen );
#endif
// Tries to find the text passed from terminal after the file is open
if(!m_textToFindOnOpen.isEmpty())
{
m_findBar->startSearch(m_textToFindOnOpen);
m_textToFindOnOpen = QString();
}
}
if ( m_exportAsText ) m_exportAsText->setEnabled( ok && m_document->canExportToText() );
if ( m_exportAs ) m_exportAs->setEnabled( ok );
......@@ -1757,7 +1765,7 @@ bool Part::openUrl( const QUrl &_url, bool swapInsteadOfOpening )
resetStartArguments();
KMessageBox::error( widget(), i18n("Could not open %1", url.toDisplayString() ) );
}
return openOk;
}
......@@ -3658,6 +3666,11 @@ void Part::setReadWrite(bool readwrite)
ReadWritePart::setReadWrite( readwrite );
}
void Part::enableStartWithFind(const QString &text)
{
m_textToFindOnOpen = QString(text);
}
} // namespace Okular
#include "part.moc"
......
......@@ -169,6 +169,7 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
Q_SCRIPTABLE Q_NOREPLY void reload();
Q_SCRIPTABLE Q_NOREPLY void enableStartWithPrint();
Q_SCRIPTABLE Q_NOREPLY void enableExitAfterPrint();
Q_SCRIPTABLE Q_NOREPLY void enableStartWithFind(const QString &text);
Q_SIGNALS:
void enablePrintAction(bool enable);
......@@ -412,6 +413,9 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
QString m_registerDbusName;
// String to search in document startup
QString m_textToFindOnOpen;
private Q_SLOTS:
void slotAnnotationPreferences();
void slotHandleActivatedSourceReference(const QString& absFileName, int line, int col, bool *handled);
......
......@@ -53,6 +53,7 @@ int main(int argc, char** argv)
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("print-and-exit"), i18n("Start with print dialog and exit after printing")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("unique"), i18n("\"Unique instance\" control")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("noraise"), i18n("Not raise window")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("find"), i18n("Find a string on the text"), QStringLiteral("string")));
parser.addPositionalArgument(QStringLiteral("urls"), i18n("Documents to open. Specify '-' to read from stdin."));
parser.process(app);
......@@ -81,7 +82,7 @@ int main(int argc, char** argv)
break;
}
}
return app.exec();
}
......
......@@ -149,6 +149,12 @@ Status main(const QStringList &paths, const QString &serializedOptions)
return Error;
}
if(!ShellUtils::find(serializedOptions).isEmpty() && paths.count() > 1)
{
QTextStream stream(stderr);
stream << i18n( "Error: Can't open more than one document with the --find switch" ) << endl;
return Error;
}
// try to attach to existing session, unique or not
if (attachUniqueInstance(paths, serializedOptions) || attachExistingInstance(paths, serializedOptions))
{
......@@ -181,7 +187,7 @@ Status main(const QStringList &paths, const QString &serializedOptions)
shell->show();
}
}
return Success;
}
......
......@@ -681,12 +681,15 @@ void Shell::openNewTab( const QUrl& url, const QString &serializedOptions )
void Shell::applyOptionsToPart( QObject* part, const QString &serializedOptions )
{
KDocumentViewer* const doc = qobject_cast<KDocumentViewer*>(part);
const QString find = ShellUtils::find(serializedOptions);
if ( ShellUtils::startInPresentation(serializedOptions) )
doc->startPresentation();
if ( ShellUtils::showPrintDialog(serializedOptions) )
QMetaObject::invokeMethod( part, "enableStartWithPrint" );
if ( ShellUtils::showPrintDialogAndExit(serializedOptions) )
QMetaObject::invokeMethod( part, "enableExitAfterPrint" );
if(!find.isEmpty())
QMetaObject::invokeMethod( part, "enableStartWithFind", Q_ARG( const QString &, find ));
}
void Shell::connectPart( QObject* part )
......
......@@ -58,7 +58,7 @@ QUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& p
if ( !pageArg.isEmpty() )
{
url.setFragment( pageArg );
}
}
return url;
}
......@@ -70,19 +70,21 @@ QString serializeOptions(const QCommandLineParser &args)
const bool unique = args.isSet(QStringLiteral("unique")) && args.positionalArguments().count() <= 1;
const bool noRaise = args.isSet(QStringLiteral("noraise"));
const QString page = args.value(QStringLiteral("page"));
const QString find = args.value(QStringLiteral("find"));
return serializeOptions(startInPresentation, showPrintDialog, showPrintDialogAndExit, unique, noRaise, page);
return serializeOptions(startInPresentation, showPrintDialog, showPrintDialogAndExit, unique, noRaise, page, find);
}
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page)
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page, const QString &find)
{
return QStringLiteral("%1:%2:%3:%4:%5:%6").arg(startInPresentation).arg(showPrintDialog).arg(showPrintDialogAndExit).arg(unique).arg(noRaise).arg(page);
return QStringLiteral("%1:%2:%3:%4:%5:%6:%7").arg(startInPresentation).arg(showPrintDialog).arg(showPrintDialogAndExit).arg(unique).arg(noRaise).arg(page).arg(QString(find.toLatin1().toBase64()));
}
bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *print_and_exit, bool *unique, bool *noraise, QString *page)
bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *print_and_exit, bool *unique, bool *noraise, QString *page, QString *find)
{
const QStringList args = serializedOptions.split(QStringLiteral(":"));
if (args.count() == 6)
if (args.count() == 7)
{
*presentation = args[0] == QLatin1String("1");
*print = args[1] == QLatin1String("1");
......@@ -90,6 +92,7 @@ bool unserializeOptions(const QString &serializedOptions, bool *presentation, bo
*unique = args[3] == QLatin1String("1");
*noraise = args[4] == QLatin1String("1");
*page = args[5];
*find = args[6];
return true;
}
return false;
......@@ -99,43 +102,51 @@ bool startInPresentation(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &result, &dummy, &dummy, &dummy, &dummy, &dummyString) && result;
return unserializeOptions(serializedOptions, &result, &dummy, &dummy, &dummy, &dummy, &dummyString, &dummyString) && result;
}
bool showPrintDialog(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &dummy, &result, &dummy, &dummy, &dummy, &dummyString) && result;
return unserializeOptions(serializedOptions, &dummy, &result, &dummy, &dummy, &dummy, &dummyString, &dummyString) && result;
}
bool showPrintDialogAndExit(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &dummy, &dummy, &result, &dummy, &dummy, &dummyString) && result;
return unserializeOptions(serializedOptions, &dummy, &dummy, &result, &dummy, &dummy, &dummyString, &dummyString) && result;
}
bool unique(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &result, &dummy, &dummyString) && result;
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &result, &dummy, &dummyString, &dummyString) && result;
}
bool noRaise(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &result, &dummyString) && result;
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &result, &dummyString, &dummyString) && result;
}
QString page(const QString &serializedOptions)
{
QString result;
QString result, dummyString;
bool dummy;
unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &dummy, &result);
unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &dummy, &result, &dummyString);
return result;
}
QString find(const QString &serializedOptions)
{
QString result, dummyString;
bool dummy;
unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &dummy, &dummyString, &result);
return QString(QByteArray::fromBase64(result.toLatin1()));
}
}
......@@ -24,7 +24,7 @@ typedef bool (*FileExistFunc)( const QString& fileName );
FileExistFunc qfileExistFunc();
QUrl urlFromArg(const QString& _arg, FileExistFunc exist_func, const QString& pageArg = QString() );
QString serializeOptions(const QCommandLineParser &args);
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page);
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool showPrintDialogAndExit, bool unique, bool noRaise, const QString &page, const QString &find);
bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *print_and_exit, bool *unique, bool *noraise, QString *page);
bool unique(const QString &serializedOptions);
bool noRaise(const QString &serializedOptions);
......@@ -32,6 +32,7 @@ bool startInPresentation(const QString &serializedOptions);
bool showPrintDialog(const QString &serializedOptions);
bool showPrintDialogAndExit(const QString &serializedOptions);
QString page(const QString &serializedOptions);
QString find(const QString &serializedOptions);
}
......
......@@ -197,4 +197,10 @@ void FindBar::closeAndStopSearch()
close();
}
void FindBar::startSearch(const QString & findText)
{
m_search->lineEdit()->setText( findText );
show();
}
#include "moc_findbar.cpp"
......@@ -33,6 +33,7 @@ class FindBar
void focusAndSetCursor();
bool maybeHide();
void startSearch(const QString & findText);
Q_SIGNALS:
void forwardKeyPressEvent( QKeyEvent* );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment