Okular

event.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Intevation GmbH <intevation@intevation.de>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "event_p.h"
8#include <QApplication>
9
10#include "../form.h"
11
12using namespace Okular;
13
14class Event::Private
15{
16public:
17 explicit Private(EventType eventType)
18 : m_target(nullptr)
19 , m_targetPage(nullptr)
20 , m_source(nullptr)
21 , m_sourcePage(nullptr)
22 , m_eventType(eventType)
23 , m_returnCode(false)
24 , m_shiftModifier(false)
25 , m_willCommit(false)
26 , m_selStart(-1)
27 , m_selEnd(-1)
28 {
29 }
30
31 void *m_target;
32 Page *m_targetPage;
33 FormField *m_source;
34 Page *m_sourcePage;
35 EventType m_eventType;
36 QString m_targetName;
37 QVariant m_value;
38 bool m_returnCode;
39 bool m_shiftModifier;
40 bool m_willCommit;
41 QString m_change;
42 int m_selStart;
43 int m_selEnd;
44};
45
46Event::Event()
47 : d(new Private(UnknownEvent))
48{
49}
50
51Event::Event(EventType eventType)
52 : d(new Private(eventType))
53{
54}
55
56Event::EventType Event::eventType() const
57{
58 return d->m_eventType;
59}
60
61QString Event::name() const
62{
63 switch (d->m_eventType) {
64 case FieldCalculate:
65 return QStringLiteral("Calculate");
66 case FieldFormat:
67 return QStringLiteral("Format");
68 case FieldKeystroke:
69 return QStringLiteral("Keystroke");
70 case FieldFocus:
71 return QStringLiteral("Focus");
72 case FieldValidate:
73 return QStringLiteral("Validate");
74 case DocOpen:
75 return QStringLiteral("Open");
76 case DocWillClose:
77 return QStringLiteral("WillClose");
78 case DocWillSave:
79 return QStringLiteral("WillSave");
80 case DocWillPrint:
81 return QStringLiteral("WillPrint");
82 case DocDidSave:
83 return QStringLiteral("DidSave");
84 case DocDidPrint:
85 return QStringLiteral("DidPrint");
86 case FieldMouseUp:
87 return QStringLiteral("MouseUp");
88 case FieldMouseDown:
89 return QStringLiteral("MouseDown");
90 case FieldMouseEnter:
91 return QStringLiteral("MouseEnter");
92 case FieldMouseExit:
93 return QStringLiteral("MouseExit");
94 case UnknownEvent:
95 default:
96 return QStringLiteral("Unknown");
97 }
98}
99
100QString Event::type() const
101{
102 switch (d->m_eventType) {
103 case FieldCalculate:
104 case FieldFormat:
105 case FieldKeystroke:
106 case FieldFocus:
107 case FieldValidate:
108 case FieldMouseUp:
109 case FieldMouseDown:
110 case FieldMouseEnter:
111 case FieldMouseExit:
112 return QStringLiteral("Field");
113 case DocOpen:
114 case DocWillClose:
115 case DocWillSave:
116 case DocWillPrint:
117 case DocDidSave:
118 case DocDidPrint:
119 return QStringLiteral("Doc");
120 case UnknownEvent:
121 default:
122 return QStringLiteral("Unknown");
123 }
124}
125
126QString Event::targetName() const
127{
128 if (!d->m_targetName.isNull()) {
129 return d->m_targetName;
130 }
131
132 return QStringLiteral("JavaScript for: ") + type() + name();
133}
134
135void Event::setTargetName(const QString &val)
136{
137 d->m_targetName = val;
138}
139
140FormField *Event::source() const
141{
142 return d->m_source;
143}
144
145void Event::setSource(FormField *val)
146{
147 d->m_source = val;
148}
149
150Page *Event::sourcePage() const
151{
152 return d->m_sourcePage;
153}
154
155void Event::setSourcePage(Page *val)
156{
157 d->m_sourcePage = val;
158}
159
160void *Event::target() const
161{
162 return d->m_target;
163}
164
165void Event::setTarget(void *target)
166{
167 d->m_target = target;
168}
169
170Page *Event::targetPage() const
171{
172 return d->m_targetPage;
173}
174
175void Event::setTargetPage(Page *val)
176{
177 d->m_targetPage = val;
178}
179
180QVariant Event::value() const
181{
182 return d->m_value;
183}
184
185void Event::setValue(const QVariant &val)
186{
187 d->m_value = val;
188}
189
190bool Event::returnCode() const
191{
192 return d->m_returnCode;
193}
194
195void Event::setReturnCode(bool returnCode)
196{
197 d->m_returnCode = returnCode;
198}
199
200bool Event::shiftModifier() const
201{
202 return d->m_shiftModifier;
203}
204
205void Event::setShiftModifier(bool shiftModifier)
206{
207 d->m_shiftModifier = shiftModifier;
208}
209
210bool Event::willCommit() const
211{
212 return d->m_willCommit;
213}
214
215void Event::setWillCommit(bool willCommit)
216{
217 d->m_willCommit = willCommit;
218}
219
220QString Event::change() const
221{
222 return d->m_change;
223}
224
225void Event::setChange(const QString &change)
226{
227 d->m_change = change;
228}
229
230int Event::selStart() const
231{
232 return d->m_selStart;
233}
234
235void Event::setSelStart(const int selStart)
236{
237 d->m_selStart = selStart;
238}
239
240int Event::selEnd() const
241{
242 return d->m_selEnd;
243}
244
245void Event::setSelEnd(const int selEnd)
246{
247 d->m_selEnd = selEnd;
248}
249
250// static
251std::shared_ptr<Event> Event::createFormCalculateEvent(FormField *target, Page *targetPage, FormField *source, Page *sourcePage, const QString &targetName)
252{
253 std::shared_ptr<Event> ret(new Event(Event::FieldCalculate));
254 ret->setSource(source);
255 ret->setSourcePage(sourcePage);
256 ret->setTarget(target);
257 ret->setTargetPage(targetPage);
258 ret->setTargetName(targetName);
259
260 ret->setValue(target->value());
261 return ret;
262}
263
264// static
265std::shared_ptr<Event> Event::createFormatEvent(FormField *target, Page *targetPage, const QString &targetName)
266{
267 std::shared_ptr<Event> ret(new Event(Event::FieldFormat));
268 ret->setTarget(target);
269 ret->setTargetPage(targetPage);
270 ret->setTargetName(targetName);
271
272 ret->setValue(target->value());
273 return ret;
274}
275
276// static
277std::shared_ptr<Event> Event::createKeystrokeEvent(FormField *target, Page *targetPage)
278{
279 std::shared_ptr<Event> ret(new Event(Event::FieldKeystroke));
280 ret->setTarget(target);
281 ret->setTargetPage(targetPage);
282
283 ret->setReturnCode(true);
284 ret->setValue(target->value());
285 return ret;
286}
287
288std::shared_ptr<Event> Event::createFormFocusEvent(FormField *target, Page *targetPage, const QString &targetName)
289{
290 std::shared_ptr<Event> ret(new Event(Event::FieldFocus));
291 ret->setTarget(target);
292 ret->setTargetPage(targetPage);
293 ret->setTargetName(targetName);
294 ret->setShiftModifier(QApplication::keyboardModifiers() & Qt::ShiftModifier);
295
296 ret->setValue(target->value());
297 return ret;
298}
299
300std::shared_ptr<Event> Event::createFormValidateEvent(FormField *target, Page *targetPage, const QString &targetName)
301{
302 std::shared_ptr<Event> ret(new Event(Event::FieldValidate));
303 ret->setTarget(target);
304 ret->setTargetPage(targetPage);
305 ret->setTargetName(targetName);
306 ret->setShiftModifier(QApplication::keyboardModifiers() & Qt::ShiftModifier);
307
308 ret->setValue(target->value());
309 ret->setReturnCode(true);
310 return ret;
311}
312
313std::shared_ptr<Event> Event::createFieldMouseEvent(FormField *target, Page *targetPage, Event::EventType fieldMouseEventType)
314{
315 Q_ASSERT(fieldMouseEventType >= Okular::Event::FieldMouseDown && fieldMouseEventType <= Okular::Event::FieldMouseUp);
316 std::shared_ptr<Event> ret = std::make_shared<Event>(fieldMouseEventType);
317 ret->setTarget(target);
318 ret->setTargetPage(targetPage);
319 ret->setShiftModifier(QApplication::keyboardModifiers() & Qt::ShiftModifier);
320 return ret;
321}
322
323std::shared_ptr<Event> Event::createDocEvent(Event::EventType type)
324{
325 std::shared_ptr<Event> ret = std::make_shared<Event>(type);
326 // TODO set target name for DocOpen event only.
327 return ret;
328}
The base interface of a form field.
Definition form.h:40
virtual QVariant value() const
Returns the value associated wit the form field.
Definition form.cpp:88
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
global.h
Definition action.h:17
Qt::KeyboardModifiers keyboardModifiers()
ShiftModifier
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.