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
remotes/origin/Applications/19.04
Joao Netto 7 years ago committed by Albert Astals Cid
parent 241bc6525a
commit 716b234100
  1. 79
      autotests/mainshelltest.cpp
  2. 2
      core/document.cpp
  3. 2
      generators/poppler/generator_pdf.cpp
  4. 15
      part.cpp
  5. 4
      part.h
  6. 3
      shell/main.cpp
  7. 8
      shell/okular_main.cpp
  8. 3
      shell/shell.cpp
  9. 37
      shell/shellutils.cpp
  10. 3
      shell/shellutils.h
  11. 6
      ui/findbar.cpp
  12. 1
      ui/findbar.h

@ -19,6 +19,7 @@
#include "../shell/shell.h"
#include "../shell/shellutils.h"
#include "../core/document_p.h"
#include "../ui/findbar.h"
#include "../ui/presentationwidget.h"
#include "../part.h"
#include "../settings.h"
@ -40,6 +41,9 @@ public:
QWidget *presentationWidget(Okular::Part *part) const {
return part->m_presentationWidget;
}
FindBar *findWidget(Okular::Part *part) const {
return part->m_findBar;
}
};
}
@ -175,6 +179,7 @@ void MainShellTest::testShell_data()
QTest::addColumn<uint>("externalProcessExpectedPage");
QTest::addColumn<bool>("externalProcessExpectPresentation");
QTest::addColumn<bool>("externalProcessExpectPrintDialog");
QTest::addColumn<QString>("externalProcessExpectFind");
const QStringList contentsEpub = QStringList(QStringLiteral(KDESRCDIR "data/contents.epub"));
const QStringList file1 = QStringList(QStringLiteral(KDESRCDIR "data/file1.pdf"));
@ -183,31 +188,33 @@ void MainShellTest::testShell_data()
file1AndToc << QStringLiteral(KDESRCDIR "data/tocreload.pdf");
const QString tocReload = QStringLiteral(KDESRCDIR "data/tocreload.pdf");
const QString optionsPage2 = ShellUtils::serializeOptions(false, false, false, false, false, QStringLiteral("2"));
const QString optionsPage2Presentation = ShellUtils::serializeOptions(true, false, false, false, false, QStringLiteral("2"));
const QString optionsPrint = ShellUtils::serializeOptions(false, true, false, false, false, QString());
const QString optionsUnique = ShellUtils::serializeOptions(false, false, false, true, false, QString());
QTest::newRow("just show shell") << QStringList() << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("open file") << file1 << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files no tabs") << file1AndToc << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files with tabs") << file1AndToc << QString() << true << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files sequence no tabs") << file1 << QString() << false << tocReload << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files sequence with tabs") << file1 << QString() << true << tocReload << 0u << false << false << false << 0u << false << false;
QTest::newRow("open file page number") << contentsEpub << optionsPage2 << false << QString() << 1u << false << false << false << 0u << false << false;
QTest::newRow("open file page number and presentation") << contentsEpub << optionsPage2Presentation << false << QString() << 1u << true << false << false << 0u << false << false;
QTest::newRow("open file print") << file1 << optionsPrint << false << QString() << 0u << false << true << false << 0u << false << false;
QTest::newRow("open two files unique") << file1 << optionsUnique << false << tocReload << 0u << false << false << true << 0u << false << false;
QTest::newRow("open two files unique tabs") << file1 << optionsUnique << true << tocReload << 0u << false << false << true << 0u << false << false;
QTest::newRow("page number attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << false << false;
QTest::newRow("presentation attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << true << false;
QTest::newRow("print attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << true << false << 2u << false << true;
QTest::newRow("page number attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 3u << false << false;
QTest::newRow("presentation attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << true << false;
QTest::newRow("print attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << false << true;
QTest::newRow("page number attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 3u << false << false;
QTest::newRow("presentation attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << true << false;
QTest::newRow("print attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << false << true;
const QString optionsPage2 = ShellUtils::serializeOptions(false, false, false, false, false, QStringLiteral("2"), QString());
const QString optionsPage2Presentation = ShellUtils::serializeOptions(true, false, false, false, false, QStringLiteral("2"), QString());
const QString optionsPrint = ShellUtils::serializeOptions(false, true, false, false, false, QString(), QString());
const QString optionsUnique = ShellUtils::serializeOptions(false, false, false, true, false, QString(), QString());
const QString optionsFind = ShellUtils::serializeOptions(false, false, false, false ,false , QString(), QStringLiteral("si:next-testing parameters!"));
QTest::newRow("just show shell") << QStringList() << QString() << false << QString() << 0u << false << false << false << 0u << false << false << QString();
QTest::newRow("open file") << file1 << QString() << false << QString() << 0u << false << false << false << 0u << false << false << QString();
QTest::newRow("two files no tabs") << file1AndToc << QString() << false << QString() << 0u << false << false << false << 0u << false << false << QString();
QTest::newRow("two files with tabs") << file1AndToc << QString() << true << QString() << 0u << false << false << false << 0u << false << false << QString();
QTest::newRow("two files sequence no tabs") << file1 << QString() << false << tocReload << 0u << false << false << false << 0u << false << false << QString();
QTest::newRow("two files sequence with tabs") << file1 << QString() << true << tocReload << 0u << false << false << false << 0u << false << false << QString();
QTest::newRow("open file page number") << contentsEpub << optionsPage2 << false << QString() << 1u << false << false << false << 0u << false << false << QString();
QTest::newRow("open file page number and presentation") << contentsEpub << optionsPage2Presentation << false << QString() << 1u << true << false << false << 0u << false << false << QString();
QTest::newRow("open file find") << file1 << optionsFind << false << QString() << 0u << false << false << false << 0u << false << false << QString("si:next-testing parameters!");
QTest::newRow("open file print") << file1 << optionsPrint << false << QString() << 0u << false << true << false << 0u << false << false << QString();
QTest::newRow("open two files unique") << file1 << optionsUnique << false << tocReload << 0u << false << false << true << 0u << false << false << QString();
QTest::newRow("open two files unique tabs") << file1 << optionsUnique << true << tocReload << 0u << false << false << true << 0u << false << false << QString();
QTest::newRow("page number attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << false << false << QString();
QTest::newRow("presentation attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << true << false << QString();
QTest::newRow("print attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << true << false << 2u << false << true << QString();
QTest::newRow("page number attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 3u << false << false << QString();
QTest::newRow("presentation attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << true << false << QString();
QTest::newRow("print attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << false << true << QString();
QTest::newRow("page number attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 3u << false << false << QString();
QTest::newRow("presentation attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << true << false << QString();
QTest::newRow("print attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << false << true << QString();
}
void MainShellTest::testShell()
@ -223,6 +230,8 @@ void MainShellTest::testShell()
QFETCH(uint, externalProcessExpectedPage);
QFETCH(bool, externalProcessExpectPresentation);
QFETCH(bool, externalProcessExpectPrintDialog);
QFETCH(QString, externalProcessExpectFind);
QScopedPointer<ClosePrintDialogHelper> helper;
@ -246,6 +255,11 @@ void MainShellTest::testShell()
QVERIFY(part);
QCOMPARE(part->url().url(), QStringLiteral("file://%1").arg(paths[0]));
QCOMPARE(partDocument(part)->currentPage(), expectedPage);
// Testing if the bar is shown or hidden as expected
QCOMPARE(findWidget(part)->isHidden(), externalProcessExpectFind.isEmpty());
// Checking if the encryption/decryption worked
QCOMPARE(externalProcessExpectFind, ShellUtils::find(serializedOptions));
}
else if (paths.count() == 2)
{
@ -405,9 +419,9 @@ void MainShellTest::testFileRemembersPagePosition()
const QStringList paths = QStringList(QStringLiteral(KDESRCDIR "data/contents.epub"));
QString serializedOptions;
if (mode == 1 || mode == 3)
serializedOptions = ShellUtils::serializeOptions(false, false, false, false, false, QString());
serializedOptions = ShellUtils::serializeOptions(false, false, false, false, false, QString(), QString());
else
serializedOptions = ShellUtils::serializeOptions(false, false, false, true, false, QString());
serializedOptions = ShellUtils::serializeOptions(false, false, false, true, false, QString(), QString());
Okular::Settings::self()->setShellOpenFileInTabs(mode == 3);
@ -458,10 +472,11 @@ void MainShellTest::test2FilesError_data()
{
QTest::addColumn<QString>("serializedOptions");
QTest::newRow("startInPresentation") << ShellUtils::serializeOptions(true, false, false, false, false, QString());
QTest::newRow("showPrintDialog") << ShellUtils::serializeOptions(false, true, false, false, false, QString());
QTest::newRow("unique") << ShellUtils::serializeOptions(false, false, false, true, false, QString());
QTest::newRow("pageNumger") << ShellUtils::serializeOptions(false, false, false, false, false, QStringLiteral("3"));
QTest::newRow("startInPresentation") << ShellUtils::serializeOptions(true, false, false, false, false, QString(), QString());
QTest::newRow("showPrintDialog") << ShellUtils::serializeOptions(false, true, false, false, false, QString(), QString());
QTest::newRow("unique") << ShellUtils::serializeOptions(false, false, false, true, false, QString(), QString());
QTest::newRow("pageNumber") << ShellUtils::serializeOptions(false, false, false, false, false, QStringLiteral("3"), QString());
QTest::newRow("find") << ShellUtils::serializeOptions(false, false, false, false, false, QString(), QStringLiteral("silly"));
}
void MainShellTest::test2FilesError()
@ -488,7 +503,7 @@ void MainShellTest::testSessionRestore_data()
QStringList twoDocPaths( oneDocPaths );
twoDocPaths << QStringLiteral(KDESRCDIR "data/formSamples.pdf");
const QString options = ShellUtils::serializeOptions(false, false, false, false, false, QString());
const QString options = ShellUtils::serializeOptions(false, false, false, false, false, QString(), QString());
QTest::newRow("1 doc, 1 window, tabs") << oneDocPaths << options << true << true;
QTest::newRow("2 docs, 1 window, tabs") << twoDocPaths << options << true << true;

@ -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* );

Loading…
Cancel
Save