Okular

sourcereference.cpp
1 /*
2  SPDX-FileCopyrightText: 2007, 2008 Pino Toscano <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "sourcereference.h"
8 #include "sourcereference_p.h"
9 
10 #include <KLocalizedString>
11 #include <QString>
12 #include <QUrl>
13 
14 using namespace Okular;
15 
16 class SourceReference::Private
17 {
18 public:
19  Private()
20  : row(0)
21  , column(0)
22  {
23  }
24 
25  QString filename;
26  int row;
27  int column;
28 };
29 
30 SourceReference::SourceReference(const QString &fileName, int row, int column)
31  : d(new Private)
32 {
33  d->filename = fileName;
34  d->row = row;
35  d->column = column;
36 }
37 
39 {
40  delete d;
41 }
42 
44 {
45  return d->filename;
46 }
47 
49 {
50  return d->row;
51 }
52 
54 {
55  return d->column;
56 }
57 
58 bool Okular::extractLilyPondSourceReference(const QUrl &url, QString *file, int *row, int *col)
59 {
60  // Example URL is: textedit:///home/foo/bar.ly:42:42:42
61  // The three numbers are apparently: line:beginning of column:end of column
62 
63  if (url.scheme() != QStringLiteral("textedit")) {
64  return false;
65  }
66 
67  // There can be more, in case the filename contains :
68  if (url.fileName().count(QLatin1Char(':')) < 3) {
69  return false;
70  }
71 
72  QStringList parts(url.path().split(QLatin1Char(':')));
73 
74  bool ok;
75  // Take out the things we need
76  int columnEnd = parts.takeLast().toInt(&ok); // apparently we don't use this
77  Q_UNUSED(columnEnd);
78  if (!ok) {
79  return false;
80  }
81 
82  *col = parts.takeLast().toInt(&ok);
83  if (!ok) {
84  return false;
85  }
86 
87  *row = parts.takeLast().toInt(&ok);
88  if (!ok) {
89  return false;
90  }
91 
92  // In case the path itself contains :, we need to reconstruct it after removing all the numbers
93  *file = parts.join(QLatin1Char(':'));
94  return (!file->isEmpty());
95 }
96 
97 QString Okular::sourceReferenceToolTip(const QString &source, int row, int col)
98 {
99  Q_UNUSED(row);
100  Q_UNUSED(col);
101  return i18nc("'source' is a source file", "Source: %1", source);
102 }
~SourceReference()
Destroys the source reference.
The documentation to the global Okular namespace.
Definition: action.h:16
QString scheme() const const
QString fileName() const
Returns the filename of the source.
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString fileName(QUrl::ComponentFormattingOptions options) const const
int row() const
Returns the row of the position in the source file.
QString path(QUrl::ComponentFormattingOptions options) const const
int count() const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
int column() const
Returns the column of the position in the source file.
SourceReference(const QString &fileName, int row, int column=0)
Creates a reference to the row row and column column of the source fileName.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Oct 3 2022 04:10:48 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.