From d3b0b62cfc6dce9ea0bc27363935bc5adb7cac41 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Mon, 22 Sep 2008 13:41:28 +0000 Subject: [PATCH] Make Okular able to distinguish the LilyPond "Point and click" links, and make them act as source references when activated. FEATURE: 163569 svn path=/trunk/KDE/kdegraphics/okular/; revision=863572 --- core/action.cpp | 7 +++++++ core/document.cpp | 8 ++++++++ core/sourcereference.cpp | 38 +++++++++++++++++++++++++++++++++++++- core/sourcereference_p.h | 23 +++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 core/sourcereference_p.h diff --git a/core/action.cpp b/core/action.cpp index c672a3ec5..df665b9f8 100644 --- a/core/action.cpp +++ b/core/action.cpp @@ -14,6 +14,7 @@ // local includes #include "document.h" +#include "sourcereference_p.h" #include "sound.h" using namespace Okular; @@ -174,6 +175,12 @@ Action::ActionType BrowseAction::actionType() const QString BrowseAction::actionTip() const { Q_D( const Okular::BrowseAction ); + QString source; + int row = 0, col = 0; + if ( extractLilyPondSourceReference( d->m_url, &source, &row, &col ) ) + { + return sourceReferenceToolTip( source, row, col ); + } return d->m_url; } diff --git a/core/document.cpp b/core/document.cpp index be8db8f28..40e8ff8d3 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -65,6 +65,7 @@ #include "scripter.h" #include "settings.h" #include "sourcereference.h" +#include "sourcereference_p.h" #include "view.h" #include "view_p.h" @@ -2740,9 +2741,16 @@ void Document::processAction( const Action * action ) case Action::Browse: { const BrowseAction * browse = static_cast< const BrowseAction * >( action ); + QString lilySource; + int lilyRow = 0, lilyCol = 0; // if the url is a mailto one, invoke mailer if ( browse->url().startsWith( "mailto:", Qt::CaseInsensitive ) ) KToolInvocation::invokeMailer( browse->url() ); + else if ( extractLilyPondSourceReference( browse->url(), &lilySource, &lilyRow, &lilyCol ) ) + { + const SourceReference ref( lilySource, lilyRow, lilyCol ); + processSourceReference( &ref ); + } else { QString url = browse->url(); diff --git a/core/sourcereference.cpp b/core/sourcereference.cpp index d19f0804b..7090b83fe 100644 --- a/core/sourcereference.cpp +++ b/core/sourcereference.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2007 by Pino Toscano * + * Copyright (C) 2007,2008 by Pino Toscano * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -8,8 +8,10 @@ ***************************************************************************/ #include "sourcereference.h" +#include "sourcereference_p.h" #include +#include using namespace Okular; @@ -54,3 +56,37 @@ int SourceReference::column() const return d->column; } +bool Okular::extractLilyPondSourceReference( const QString &url, QString *file, int *row, int *col ) +{ + if ( !url.startsWith( QLatin1String( "textedit://" ) ) ) + return false; + + *row = 0; + *col = 0; + int lilyChar = 0; + typedef int *IntPtr; + const IntPtr int_data[] = { row, &lilyChar, col }; + int int_index = sizeof( int_data ) / sizeof( int* ) - 1; + int index_last = -1; + int index = url.lastIndexOf( QLatin1Char( ':' ), index_last ); + while ( index != -1 && int_index >= 0 ) + { + // read the current "chunk" + const QStringRef ref = url.midRef( index + 1, index_last - index - 1 ); + *int_data[ int_index ] = QString::fromRawData( ref.data(), ref.count() ).toInt(); + // find the previous "chunk" + index_last = index; + index = url.lastIndexOf( QLatin1Char( ':' ), index_last - 1 ); + --int_index; + } + // NOTE: 11 is the length of "textedit://" + *file = url.mid( 11, index_last != -1 ? index_last - 11 : -1 ); + return true; +} + +QString Okular::sourceReferenceToolTip( const QString &source, int row, int col ) +{ + Q_UNUSED( row ); + Q_UNUSED( col ); + return i18nc( "'source' is a source file", "Source: %1", source ); +} diff --git a/core/sourcereference_p.h b/core/sourcereference_p.h new file mode 100644 index 000000000..cf7b3e826 --- /dev/null +++ b/core/sourcereference_p.h @@ -0,0 +1,23 @@ +/*************************************************************************** + * Copyright (C) 2008 by Pino Toscano * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef OKULAR_SOURCEREFERENCE_P_H +#define OKULAR_SOURCEREFERENCE_P_H + +class QString; + +namespace Okular +{ + +bool extractLilyPondSourceReference( const QString &url, QString *file, int *row, int *col ); +QString sourceReferenceToolTip( const QString &source, int row, int col ); + +} + +#endif