File indexing completed on 2024-04-28 04:32:34

0001 /*
0002     SPDX-FileCopyrightText: 2004-2005 Enrico Ros <eros.kde@email.it>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "action.h"
0008 
0009 // kde includes
0010 #include <KLocalizedString>
0011 
0012 // local includes
0013 #include "document.h"
0014 #include "movie.h"
0015 #include "sound.h"
0016 #include "sourcereference_p.h"
0017 
0018 using namespace Okular;
0019 
0020 class Okular::ActionPrivate
0021 {
0022 public:
0023     ActionPrivate()
0024     {
0025     }
0026 
0027     virtual ~ActionPrivate()
0028     {
0029         qDeleteAll(m_nextActions);
0030     }
0031 
0032     ActionPrivate(const ActionPrivate &) = delete;
0033     ActionPrivate &operator=(const ActionPrivate &) = delete;
0034 
0035     QVariant m_nativeId;
0036     QVector<Action *> m_nextActions;
0037 };
0038 
0039 Action::Action(ActionPrivate &dd)
0040     : d_ptr(&dd)
0041 {
0042 }
0043 
0044 Action::~Action()
0045 {
0046     delete d_ptr;
0047 }
0048 
0049 QString Action::actionTip() const
0050 {
0051     return QLatin1String("");
0052 }
0053 
0054 void Action::setNativeId(const QVariant &id)
0055 {
0056     Q_D(Action);
0057     d->m_nativeId = id;
0058 }
0059 
0060 QVariant Action::nativeId() const
0061 {
0062     Q_D(const Action);
0063     return d->m_nativeId;
0064 }
0065 
0066 QVector<Action *> Action::nextActions() const
0067 {
0068     Q_D(const Action);
0069     return d->m_nextActions;
0070 }
0071 
0072 void Action::setNextActions(const QVector<Action *> &actions)
0073 {
0074     Q_D(Action);
0075     qDeleteAll(d->m_nextActions);
0076     d->m_nextActions = actions;
0077 }
0078 
0079 // GotoAction
0080 
0081 class Okular::GotoActionPrivate : public Okular::ActionPrivate
0082 {
0083 public:
0084     GotoActionPrivate(const QString &fileName, const DocumentViewport &viewport)
0085         : ActionPrivate()
0086         , m_extFileName(fileName)
0087         , m_vp(viewport)
0088     {
0089     }
0090 
0091     GotoActionPrivate(const QString &fileName, const QString &namedDestination)
0092         : ActionPrivate()
0093         , m_extFileName(fileName)
0094         , m_dest(namedDestination)
0095     {
0096     }
0097 
0098     QString m_extFileName;
0099     DocumentViewport m_vp;
0100     QString m_dest;
0101 };
0102 
0103 GotoAction::GotoAction(const QString &fileName, const DocumentViewport &viewport)
0104     : Action(*new GotoActionPrivate(fileName, viewport))
0105 {
0106 }
0107 
0108 GotoAction::GotoAction(const QString &fileName, const QString &namedDestination)
0109     : Action(*new GotoActionPrivate(fileName, namedDestination))
0110 {
0111 }
0112 
0113 GotoAction::~GotoAction()
0114 {
0115 }
0116 
0117 Action::ActionType GotoAction::actionType() const
0118 {
0119     return Goto;
0120 }
0121 
0122 QString GotoAction::actionTip() const
0123 {
0124     Q_D(const GotoAction);
0125     return d->m_extFileName.isEmpty() ? (d->m_vp.isValid() ? i18n("Go to page %1", d->m_vp.pageNumber + 1) : QLatin1String("")) : i18n("Open external file");
0126 }
0127 
0128 bool GotoAction::isExternal() const
0129 {
0130     Q_D(const GotoAction);
0131     return !d->m_extFileName.isEmpty();
0132 }
0133 
0134 QString GotoAction::fileName() const
0135 {
0136     Q_D(const GotoAction);
0137     return d->m_extFileName;
0138 }
0139 
0140 DocumentViewport GotoAction::destViewport() const
0141 {
0142     Q_D(const GotoAction);
0143     return d->m_vp;
0144 }
0145 
0146 QString GotoAction::destinationName() const
0147 {
0148     Q_D(const GotoAction);
0149     return d->m_dest;
0150 }
0151 
0152 // ExecuteAction
0153 
0154 class Okular::ExecuteActionPrivate : public Okular::ActionPrivate
0155 {
0156 public:
0157     ExecuteActionPrivate(const QString &file, const QString &parameters)
0158         : ActionPrivate()
0159         , m_fileName(file)
0160         , m_parameters(parameters)
0161     {
0162     }
0163 
0164     QString m_fileName;
0165     QString m_parameters;
0166 };
0167 
0168 ExecuteAction::ExecuteAction(const QString &file, const QString &parameters)
0169     : Action(*new ExecuteActionPrivate(file, parameters))
0170 {
0171 }
0172 
0173 ExecuteAction::~ExecuteAction()
0174 {
0175 }
0176 
0177 Action::ActionType ExecuteAction::actionType() const
0178 {
0179     return Execute;
0180 }
0181 
0182 QString ExecuteAction::actionTip() const
0183 {
0184     Q_D(const Okular::ExecuteAction);
0185     return i18n("Execute '%1'...", d->m_fileName);
0186 }
0187 
0188 QString ExecuteAction::fileName() const
0189 {
0190     Q_D(const Okular::ExecuteAction);
0191     return d->m_fileName;
0192 }
0193 
0194 QString ExecuteAction::parameters() const
0195 {
0196     Q_D(const Okular::ExecuteAction);
0197     return d->m_parameters;
0198 }
0199 
0200 // BrowseAction
0201 
0202 class Okular::BrowseActionPrivate : public Okular::ActionPrivate
0203 {
0204 public:
0205     explicit BrowseActionPrivate(const QUrl &url)
0206         : ActionPrivate()
0207         , m_url(url)
0208     {
0209     }
0210 
0211     QUrl m_url;
0212 };
0213 
0214 BrowseAction::BrowseAction(const QUrl &url)
0215     : Action(*new BrowseActionPrivate(url))
0216 {
0217 }
0218 
0219 BrowseAction::~BrowseAction()
0220 {
0221 }
0222 
0223 Action::ActionType BrowseAction::actionType() const
0224 {
0225     return Browse;
0226 }
0227 
0228 QString BrowseAction::actionTip() const
0229 {
0230     Q_D(const Okular::BrowseAction);
0231     QString source;
0232     int row = 0, col = 0;
0233     if (extractLilyPondSourceReference(d->m_url, &source, &row, &col)) {
0234         return sourceReferenceToolTip(source, row, col);
0235     }
0236     return d->m_url.toDisplayString();
0237 }
0238 
0239 QUrl BrowseAction::url() const
0240 {
0241     Q_D(const Okular::BrowseAction);
0242     return d->m_url;
0243 }
0244 
0245 // DocumentAction
0246 
0247 class Okular::DocumentActionPrivate : public Okular::ActionPrivate
0248 {
0249 public:
0250     explicit DocumentActionPrivate(enum DocumentAction::DocumentActionType documentActionType)
0251         : ActionPrivate()
0252         , m_type(documentActionType)
0253     {
0254     }
0255 
0256     DocumentAction::DocumentActionType m_type;
0257 };
0258 
0259 DocumentAction::DocumentAction(enum DocumentActionType documentActionType)
0260     : Action(*new DocumentActionPrivate(documentActionType))
0261 {
0262 }
0263 
0264 DocumentAction::~DocumentAction()
0265 {
0266 }
0267 
0268 DocumentAction::DocumentActionType DocumentAction::documentActionType() const
0269 {
0270     Q_D(const Okular::DocumentAction);
0271     return d->m_type;
0272 }
0273 
0274 Action::ActionType DocumentAction::actionType() const
0275 {
0276     return DocAction;
0277 }
0278 
0279 QString DocumentAction::actionTip() const
0280 {
0281     Q_D(const Okular::DocumentAction);
0282     switch (d->m_type) {
0283     case PageFirst:
0284         return i18n("First Page");
0285     case PagePrev:
0286         return i18n("Previous Page");
0287     case PageNext:
0288         return i18n("Next Page");
0289     case PageLast:
0290         return i18n("Last Page");
0291     case HistoryBack:
0292         return i18n("Back");
0293     case HistoryForward:
0294         return i18n("Forward");
0295     case Quit:
0296         return i18n("Quit");
0297     case Presentation:
0298         return i18n("Start Presentation");
0299     case EndPresentation:
0300         return i18n("End Presentation");
0301     case Find:
0302         return i18n("Find...");
0303     case GoToPage:
0304         return i18n("Go To Page...");
0305     case Close:
0306     default:;
0307     }
0308 
0309     return QString();
0310 }
0311 
0312 // SoundAction
0313 
0314 class Okular::SoundActionPrivate : public Okular::ActionPrivate
0315 {
0316 public:
0317     SoundActionPrivate(double volume, bool sync, bool repeat, bool mix, Okular::Sound *sound)
0318         : ActionPrivate()
0319         , m_volume(volume)
0320         , m_sync(sync)
0321         , m_repeat(repeat)
0322         , m_mix(mix)
0323         , m_sound(sound)
0324     {
0325     }
0326 
0327     ~SoundActionPrivate() override
0328     {
0329         delete m_sound;
0330     }
0331 
0332     double m_volume;
0333     bool m_sync : 1;
0334     bool m_repeat : 1;
0335     bool m_mix : 1;
0336     Okular::Sound *m_sound;
0337 };
0338 
0339 SoundAction::SoundAction(double volume, bool sync, bool repeat, bool mix, Okular::Sound *sound)
0340     : Action(*new SoundActionPrivate(volume, sync, repeat, mix, sound))
0341 {
0342 }
0343 
0344 SoundAction::~SoundAction()
0345 {
0346 }
0347 
0348 Action::ActionType SoundAction::actionType() const
0349 {
0350     return Sound;
0351 }
0352 
0353 QString SoundAction::actionTip() const
0354 {
0355     return i18n("Play sound...");
0356 }
0357 
0358 double SoundAction::volume() const
0359 {
0360     Q_D(const Okular::SoundAction);
0361     return d->m_volume;
0362 }
0363 
0364 bool SoundAction::synchronous() const
0365 {
0366     Q_D(const Okular::SoundAction);
0367     return d->m_sync;
0368 }
0369 
0370 bool SoundAction::repeat() const
0371 {
0372     Q_D(const Okular::SoundAction);
0373     return d->m_repeat;
0374 }
0375 
0376 bool SoundAction::mix() const
0377 {
0378     Q_D(const Okular::SoundAction);
0379     return d->m_mix;
0380 }
0381 
0382 Okular::Sound *SoundAction::sound() const
0383 {
0384     Q_D(const Okular::SoundAction);
0385     return d->m_sound;
0386 }
0387 
0388 // ScriptAction
0389 
0390 class Okular::ScriptActionPrivate : public Okular::ActionPrivate
0391 {
0392 public:
0393     ScriptActionPrivate(enum ScriptType type, const QString &script)
0394         : ActionPrivate()
0395         , m_scriptType(type)
0396         , m_script(script)
0397     {
0398     }
0399 
0400     ScriptType m_scriptType;
0401     QString m_script;
0402 };
0403 
0404 ScriptAction::ScriptAction(enum ScriptType type, const QString &script)
0405     : Action(*new ScriptActionPrivate(type, script))
0406 {
0407 }
0408 
0409 ScriptAction::~ScriptAction()
0410 {
0411 }
0412 
0413 Action::ActionType ScriptAction::actionType() const
0414 {
0415     return Script;
0416 }
0417 
0418 QString ScriptAction::actionTip() const
0419 {
0420     Q_D(const Okular::ScriptAction);
0421     switch (d->m_scriptType) {
0422     case JavaScript:
0423         return i18n("JavaScript Script");
0424     }
0425 
0426     return QString();
0427 }
0428 
0429 ScriptType ScriptAction::scriptType() const
0430 {
0431     Q_D(const Okular::ScriptAction);
0432     return d->m_scriptType;
0433 }
0434 
0435 QString ScriptAction::script() const
0436 {
0437     Q_D(const Okular::ScriptAction);
0438     return d->m_script;
0439 }
0440 
0441 // MovieAction
0442 
0443 class Okular::MovieActionPrivate : public Okular::ActionPrivate
0444 {
0445 public:
0446     explicit MovieActionPrivate(MovieAction::OperationType operation)
0447         : ActionPrivate()
0448         , m_operation(operation)
0449         , m_annotation(nullptr)
0450     {
0451     }
0452 
0453     MovieAction::OperationType m_operation;
0454     MovieAnnotation *m_annotation;
0455 };
0456 
0457 MovieAction::MovieAction(OperationType operation)
0458     : Action(*new MovieActionPrivate(operation))
0459 {
0460 }
0461 
0462 MovieAction::~MovieAction()
0463 {
0464 }
0465 
0466 Action::ActionType MovieAction::actionType() const
0467 {
0468     return Movie;
0469 }
0470 
0471 QString MovieAction::actionTip() const
0472 {
0473     return i18n("Play movie...");
0474 }
0475 
0476 MovieAction::OperationType MovieAction::operation() const
0477 {
0478     Q_D(const Okular::MovieAction);
0479     return d->m_operation;
0480 }
0481 
0482 void MovieAction::setAnnotation(MovieAnnotation *annotation)
0483 {
0484     Q_D(Okular::MovieAction);
0485     d->m_annotation = annotation;
0486 }
0487 
0488 MovieAnnotation *MovieAction::annotation() const
0489 {
0490     Q_D(const Okular::MovieAction);
0491     return d->m_annotation;
0492 }
0493 
0494 // RenditionAction
0495 
0496 class Okular::RenditionActionPrivate : public Okular::ActionPrivate
0497 {
0498 public:
0499     RenditionActionPrivate(RenditionAction::OperationType operation, Okular::Movie *movie, enum ScriptType scriptType, const QString &script)
0500         : ActionPrivate()
0501         , m_operation(operation)
0502         , m_movie(movie)
0503         , m_scriptType(scriptType)
0504         , m_script(script)
0505         , m_annotation(nullptr)
0506     {
0507     }
0508 
0509     RenditionAction::OperationType m_operation;
0510     Okular::Movie *m_movie;
0511     ScriptType m_scriptType;
0512     QString m_script;
0513     ScreenAnnotation *m_annotation;
0514 };
0515 
0516 RenditionAction::RenditionAction(OperationType operation, Okular::Movie *movie, enum ScriptType scriptType, const QString &script)
0517     : Action(*new RenditionActionPrivate(operation, movie, scriptType, script))
0518 {
0519 }
0520 
0521 RenditionAction::~RenditionAction()
0522 {
0523 }
0524 
0525 Action::ActionType RenditionAction::actionType() const
0526 {
0527     return Rendition;
0528 }
0529 
0530 QString RenditionAction::actionTip() const
0531 {
0532     Q_D(const Okular::RenditionAction);
0533 
0534     switch (d->m_operation) {
0535     default:
0536     case None:
0537         switch (d->m_scriptType) {
0538         case JavaScript:
0539             return i18n("JavaScript Script");
0540         default:
0541             return QString();
0542         }
0543     case Play:
0544         return i18n("Play movie");
0545     case Stop:
0546         return i18n("Stop movie");
0547     case Pause:
0548         return i18n("Pause movie");
0549     case Resume:
0550         return i18n("Resume movie");
0551     }
0552 }
0553 
0554 RenditionAction::OperationType RenditionAction::operation() const
0555 {
0556     Q_D(const Okular::RenditionAction);
0557     return d->m_operation;
0558 }
0559 
0560 Okular::Movie *RenditionAction::movie() const
0561 {
0562     Q_D(const Okular::RenditionAction);
0563     return d->m_movie;
0564 }
0565 
0566 ScriptType RenditionAction::scriptType() const
0567 {
0568     Q_D(const Okular::RenditionAction);
0569     return d->m_scriptType;
0570 }
0571 
0572 QString RenditionAction::script() const
0573 {
0574     Q_D(const Okular::RenditionAction);
0575     return d->m_script;
0576 }
0577 
0578 void RenditionAction::setAnnotation(ScreenAnnotation *annotation)
0579 {
0580     Q_D(Okular::RenditionAction);
0581     d->m_annotation = annotation;
0582 }
0583 
0584 ScreenAnnotation *RenditionAction::annotation() const
0585 {
0586     Q_D(const Okular::RenditionAction);
0587     return d->m_annotation;
0588 }
0589 
0590 BackendOpaqueAction::BackendOpaqueAction()
0591     : Action(*new ActionPrivate())
0592 {
0593 }
0594 
0595 Action::ActionType BackendOpaqueAction::actionType() const
0596 {
0597     return BackendOpaque;
0598 }