Okular

documentcommands.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Jon Mease <jon.mease@gmail.com>
3
4 Work sponsored by the LiMux project of the city of Munich:
5 SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include "documentcommands_p.h"
11
12#include "annotations.h"
13#include "debug_p.h"
14#include "document_p.h"
15#include "form.h"
16#include "page.h"
17#include "page_p.h"
18#include "utils_p.h"
19
20#include <KLocalizedString>
21
22namespace Okular
23{
24void moveViewportIfBoundingRectNotFullyVisible(Okular::NormalizedRect boundingRect, DocumentPrivate *docPriv, int pageNumber)
25{
26 const Rotation pageRotation = docPriv->m_parent->page(pageNumber)->rotation();
27 const QTransform rotationMatrix = Okular::buildRotationMatrix(pageRotation);
28 boundingRect.transform(rotationMatrix);
29 if (!docPriv->isNormalizedRectangleFullyVisible(boundingRect, pageNumber)) {
30 DocumentViewport searchViewport(pageNumber);
31 searchViewport.rePos.enabled = true;
32 searchViewport.rePos.normalizedX = (boundingRect.left + boundingRect.right) / 2.0;
33 searchViewport.rePos.normalizedY = (boundingRect.top + boundingRect.bottom) / 2.0;
34 docPriv->m_parent->setViewport(searchViewport, nullptr, true);
35 }
36}
37
38Okular::NormalizedRect buildBoundingRectangleForButtons(const QList<Okular::FormFieldButton *> &formButtons)
39{
40 // Initialize coordinates of the bounding rect
41 double left = 1.0;
42 double top = 1.0;
43 double right = 0.0;
44 double bottom = 0.0;
45
46 for (const FormFieldButton *formButton : formButtons) {
47 left = qMin<double>(left, formButton->rect().left);
48 top = qMin<double>(top, formButton->rect().top);
49 right = qMax<double>(right, formButton->rect().right);
50 bottom = qMax<double>(bottom, formButton->rect().bottom);
51 }
52 Okular::NormalizedRect boundingRect(left, top, right, bottom);
53 return boundingRect;
54}
55
56AddAnnotationCommand::AddAnnotationCommand(Okular::DocumentPrivate *docPriv, Okular::Annotation *annotation, int pageNumber)
57 : m_docPriv(docPriv)
58 , m_annotation(annotation)
59 , m_pageNumber(pageNumber)
60 , m_done(false)
61{
62 setText(i18nc("Add an annotation to the page", "add annotation"));
63}
64
65AddAnnotationCommand::~AddAnnotationCommand()
66{
67 if (!m_done) {
68 delete m_annotation;
69 }
70}
71
72void AddAnnotationCommand::undo()
73{
74 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
75 m_docPriv->performRemovePageAnnotation(m_pageNumber, m_annotation);
76 m_done = false;
77}
78
79void AddAnnotationCommand::redo()
80{
81 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
82 m_docPriv->performAddPageAnnotation(m_pageNumber, m_annotation);
83 m_done = true;
84}
85
86bool AddAnnotationCommand::refreshInternalPageReferences(const QVector<Okular::Page *> &newPagesVector)
87{
88 if (m_done) {
89 // We don't always update m_annotation because even if the annotation has been added to the document
90 // it can have been removed later so the annotation pointer is stored inside a following RemoveAnnotationCommand
91 // and thus doesn't need updating because it didn't change
92 // because of the document reload
93 auto a = newPagesVector[m_pageNumber]->annotation(m_annotation->uniqueName());
94 if (a) {
95 m_annotation = a;
96 }
97 }
98
99 return true;
100}
101
102RemoveAnnotationCommand::RemoveAnnotationCommand(Okular::DocumentPrivate *doc, Okular::Annotation *annotation, int pageNumber)
103 : m_docPriv(doc)
104 , m_annotation(annotation)
105 , m_pageNumber(pageNumber)
106 , m_done(false)
107{
108 setText(i18nc("Remove an annotation from the page", "remove annotation"));
109}
110
111RemoveAnnotationCommand::~RemoveAnnotationCommand()
112{
113 if (m_done) {
114 delete m_annotation;
115 }
116}
117
118void RemoveAnnotationCommand::undo()
119{
120 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
121 m_docPriv->performAddPageAnnotation(m_pageNumber, m_annotation);
122 m_done = false;
123}
124
125void RemoveAnnotationCommand::redo()
126{
127 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
128 m_docPriv->performRemovePageAnnotation(m_pageNumber, m_annotation);
129 m_done = true;
130}
131
132bool RemoveAnnotationCommand::refreshInternalPageReferences(const QVector<Okular::Page *> &newPagesVector)
133{
134 if (!m_done) {
135 // We don't always update m_annotation because it can happen that the annotation remove has been undo
136 // and that annotation addition has also been undone so the annotation pointer is stored inside
137 // a previous AddAnnotationCommand and thus doesn't need updating because it didn't change
138 // because of the document reload
139 auto a = newPagesVector[m_pageNumber]->annotation(m_annotation->uniqueName());
140 if (a) {
141 m_annotation = a;
142 }
143 }
144
145 return true;
146}
147
148ModifyAnnotationPropertiesCommand::ModifyAnnotationPropertiesCommand(DocumentPrivate *docPriv, Annotation *annotation, int pageNumber, const QDomNode &oldProperties, const QDomNode &newProperties)
149 : m_docPriv(docPriv)
150 , m_annotation(annotation)
151 , m_pageNumber(pageNumber)
152 , m_prevProperties(oldProperties)
153 , m_newProperties(newProperties)
154{
155 setText(i18nc("Modify an annotation's internal properties (Color, line-width, etc.)", "modify annotation properties"));
156}
157
158void ModifyAnnotationPropertiesCommand::undo()
159{
160 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
161 m_annotation->setAnnotationProperties(m_prevProperties);
162 m_docPriv->performModifyPageAnnotation(m_pageNumber, m_annotation, true);
163}
164
165void ModifyAnnotationPropertiesCommand::redo()
166{
167 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
168 m_annotation->setAnnotationProperties(m_newProperties);
169 m_docPriv->performModifyPageAnnotation(m_pageNumber, m_annotation, true);
170}
171
172bool ModifyAnnotationPropertiesCommand::refreshInternalPageReferences(const QVector<Okular::Page *> &newPagesVector)
173{
174 // Same reason for not unconditionally updating m_annotation, the annotation pointer can be stored in an add/Remove command
175 auto a = newPagesVector[m_pageNumber]->annotation(m_annotation->uniqueName());
176 if (a) {
177 m_annotation = a;
178 }
179
180 return true;
181}
182
183TranslateAnnotationCommand::TranslateAnnotationCommand(DocumentPrivate *docPriv, Annotation *annotation, int pageNumber, const Okular::NormalizedPoint &delta, bool completeDrag)
184 : m_docPriv(docPriv)
185 , m_annotation(annotation)
186 , m_pageNumber(pageNumber)
187 , m_delta(delta)
188 , m_completeDrag(completeDrag)
189{
190 setText(i18nc("Translate an annotation's position on the page", "translate annotation"));
191}
192
193void TranslateAnnotationCommand::undo()
194{
195 moveViewportIfBoundingRectNotFullyVisible(translateBoundingRectangle(minusDelta()), m_docPriv, m_pageNumber);
196 m_annotation->translate(minusDelta());
197 m_docPriv->performModifyPageAnnotation(m_pageNumber, m_annotation, true);
198}
199
200void TranslateAnnotationCommand::redo()
201{
202 moveViewportIfBoundingRectNotFullyVisible(translateBoundingRectangle(m_delta), m_docPriv, m_pageNumber);
203 m_annotation->translate(m_delta);
204 m_docPriv->performModifyPageAnnotation(m_pageNumber, m_annotation, true);
205}
206
207int TranslateAnnotationCommand::id() const
208{
209 return 1;
210}
211
212bool TranslateAnnotationCommand::mergeWith(const QUndoCommand *uc)
213{
214 TranslateAnnotationCommand *tuc = (TranslateAnnotationCommand *)uc;
215
216 if (tuc->m_annotation != m_annotation) {
217 return false;
218 }
219
220 if (m_completeDrag) {
221 return false;
222 }
223 m_delta = Okular::NormalizedPoint(tuc->m_delta.x + m_delta.x, tuc->m_delta.y + m_delta.y);
224 m_completeDrag = tuc->m_completeDrag;
225 return true;
226}
227
228Okular::NormalizedPoint TranslateAnnotationCommand::minusDelta()
229{
230 return Okular::NormalizedPoint(-m_delta.x, -m_delta.y);
231}
232
233Okular::NormalizedRect TranslateAnnotationCommand::translateBoundingRectangle(const Okular::NormalizedPoint &delta)
234{
235 Okular::NormalizedRect annotBoundingRect = m_annotation->boundingRectangle();
236 annotBoundingRect.left = annotBoundingRect.left + delta.x;
237 annotBoundingRect.right = annotBoundingRect.right + delta.x;
238 annotBoundingRect.top = annotBoundingRect.top + delta.y;
239 annotBoundingRect.bottom = annotBoundingRect.bottom + delta.y;
240
241 return annotBoundingRect;
242}
243
244bool TranslateAnnotationCommand::refreshInternalPageReferences(const QVector<Page *> &newPagesVector)
245{
246 // Same reason for not unconditionally updating m_annotation, the annotation pointer can be stored in an add/Remove command
247 auto a = newPagesVector[m_pageNumber]->annotation(m_annotation->uniqueName());
248 if (a) {
249 m_annotation = a;
250 }
251
252 return true;
253}
254
255AdjustAnnotationCommand::AdjustAnnotationCommand(Okular::DocumentPrivate *docPriv, Okular::Annotation *annotation, int pageNumber, const Okular::NormalizedPoint &delta1, const Okular::NormalizedPoint &delta2, bool completeDrag)
256 : m_docPriv(docPriv)
257 , m_annotation(annotation)
258 , m_pageNumber(pageNumber)
259 , m_delta1(delta1)
260 , m_delta2(delta2)
261 , m_completeDrag(completeDrag)
262{
263 setText(i18nc("Change an annotation's size", "adjust annotation"));
264}
265
266void AdjustAnnotationCommand::undo()
267{
268 const NormalizedPoint minusDelta1 = Okular::NormalizedPoint(-m_delta1.x, -m_delta1.y);
269 const NormalizedPoint minusDelta2 = Okular::NormalizedPoint(-m_delta2.x, -m_delta2.y);
270 moveViewportIfBoundingRectNotFullyVisible(adjustBoundingRectangle(minusDelta1, minusDelta2), m_docPriv, m_pageNumber);
271 m_annotation->adjust(minusDelta1, minusDelta2);
272 m_docPriv->performModifyPageAnnotation(m_pageNumber, m_annotation, true);
273}
274
275void AdjustAnnotationCommand::redo()
276{
277 moveViewportIfBoundingRectNotFullyVisible(adjustBoundingRectangle(m_delta1, m_delta2), m_docPriv, m_pageNumber);
278 m_annotation->adjust(m_delta1, m_delta2);
279 m_docPriv->performModifyPageAnnotation(m_pageNumber, m_annotation, true);
280}
281
282int AdjustAnnotationCommand::id() const
283{
284 return 5;
285}
286
287bool AdjustAnnotationCommand::mergeWith(const QUndoCommand *uc)
288{
289 AdjustAnnotationCommand *tuc = (AdjustAnnotationCommand *)uc;
290
291 if (tuc->m_annotation != m_annotation) {
292 return false;
293 }
294
295 if (m_completeDrag) {
296 return false;
297 }
298 m_delta1 = Okular::NormalizedPoint(tuc->m_delta1.x + m_delta1.x, tuc->m_delta1.y + m_delta1.y);
299 m_delta2 = Okular::NormalizedPoint(tuc->m_delta2.x + m_delta2.x, tuc->m_delta2.y + m_delta2.y);
300 m_completeDrag = tuc->m_completeDrag;
301 return true;
302}
303
304Okular::NormalizedRect AdjustAnnotationCommand::adjustBoundingRectangle(const Okular::NormalizedPoint &delta1, const Okular::NormalizedPoint &delta2)
305{
306 Okular::NormalizedRect annotBoundingRect = m_annotation->boundingRectangle();
307 annotBoundingRect.left = annotBoundingRect.left + delta1.x;
308 annotBoundingRect.right = annotBoundingRect.right + delta2.x;
309 annotBoundingRect.top = annotBoundingRect.top + delta1.y;
310 annotBoundingRect.bottom = annotBoundingRect.bottom + delta2.y;
311
312 return annotBoundingRect;
313}
314
315bool AdjustAnnotationCommand::refreshInternalPageReferences(const QVector<Page *> &newPagesVector)
316{
317 // Same reason for not unconditionally updating m_annotation, the annotation pointer can be stored in an add/Remove command
318 auto a = newPagesVector[m_pageNumber]->annotation(m_annotation->uniqueName());
319 if (a) {
320 m_annotation = a;
321 }
322
323 return true;
324}
325
326EditTextCommand::EditTextCommand(const QString &newContents, int newCursorPos, const QString &prevContents, int prevCursorPos, int prevAnchorPos)
327 : m_newContents(newContents)
328 , m_newCursorPos(newCursorPos)
329 , m_prevContents(prevContents)
330 , m_prevCursorPos(prevCursorPos)
331 , m_prevAnchorPos(prevAnchorPos)
332{
333 setText(i18nc("Generic text edit command", "edit text"));
334
335 //// Determine edit type
336 // If There was a selection then edit was not a simple single character backspace, delete, or insert
337 if (m_prevCursorPos != m_prevAnchorPos) {
338 qCDebug(OkularCoreDebug) << "OtherEdit, selection";
339 m_editType = OtherEdit;
340 } else if (newContentsRightOfCursor() == oldContentsRightOfCursor() && newContentsLeftOfCursor() == oldContentsLeftOfCursor().left(oldContentsLeftOfCursor().length() - 1) &&
341 QStringView {oldContentsLeftOfCursor()}.right(1) != QLatin1Char('\n')) {
342 qCDebug(OkularCoreDebug) << "CharBackspace";
343 m_editType = CharBackspace;
344 } else if (newContentsLeftOfCursor() == oldContentsLeftOfCursor() && newContentsRightOfCursor() == oldContentsRightOfCursor().right(oldContentsRightOfCursor().length() - 1) &&
345 QStringView {oldContentsRightOfCursor()}.left(1) != QLatin1Char('\n')) {
346 qCDebug(OkularCoreDebug) << "CharDelete";
347 m_editType = CharDelete;
348 } else if (newContentsRightOfCursor() == oldContentsRightOfCursor() && newContentsLeftOfCursor().left(newContentsLeftOfCursor().length() - 1) == oldContentsLeftOfCursor() &&
349 QStringView {newContentsLeftOfCursor()}.right(1) != QLatin1Char('\n')) {
350 qCDebug(OkularCoreDebug) << "CharInsert";
351 m_editType = CharInsert;
352 } else {
353 qCDebug(OkularCoreDebug) << "OtherEdit";
354 m_editType = OtherEdit;
355 }
356}
357
358bool EditTextCommand::mergeWith(const QUndoCommand *uc)
359{
360 EditTextCommand *euc = (EditTextCommand *)uc;
361
362 // Only attempt merge of euc into this if our new state matches euc's old state and
363 // the editTypes match and are not type OtherEdit
364 if (m_newContents == euc->m_prevContents && m_newCursorPos == euc->m_prevCursorPos && m_editType == euc->m_editType && m_editType != OtherEdit) {
365 m_newContents = euc->m_newContents;
366 m_newCursorPos = euc->m_newCursorPos;
367 return true;
368 }
369 return false;
370}
371
372QString EditTextCommand::oldContentsLeftOfCursor()
373{
374 return m_prevContents.left(m_prevCursorPos);
375}
376
377QString EditTextCommand::oldContentsRightOfCursor()
378{
379 return m_prevContents.right(m_prevContents.length() - m_prevCursorPos);
380}
381
382QString EditTextCommand::newContentsLeftOfCursor()
383{
384 return m_newContents.left(m_newCursorPos);
385}
386
387QString EditTextCommand::newContentsRightOfCursor()
388{
389 return m_newContents.right(m_newContents.length() - m_newCursorPos);
390}
391
392EditAnnotationContentsCommand::EditAnnotationContentsCommand(DocumentPrivate *docPriv, Annotation *annotation, int pageNumber, const QString &newContents, int newCursorPos, const QString &prevContents, int prevCursorPos, int prevAnchorPos)
393 : EditTextCommand(newContents, newCursorPos, prevContents, prevCursorPos, prevAnchorPos)
394 , m_docPriv(docPriv)
395 , m_annotation(annotation)
396 , m_pageNumber(pageNumber)
397{
398 setText(i18nc("Edit an annotation's text contents", "edit annotation contents"));
399}
400
401void EditAnnotationContentsCommand::undo()
402{
403 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
404 m_docPriv->performSetAnnotationContents(m_prevContents, m_annotation, m_pageNumber);
405 Q_EMIT m_docPriv->m_parent->annotationContentsChangedByUndoRedo(m_annotation, m_prevContents, m_prevCursorPos, m_prevAnchorPos);
406}
407
408void EditAnnotationContentsCommand::redo()
409{
410 moveViewportIfBoundingRectNotFullyVisible(m_annotation->boundingRectangle(), m_docPriv, m_pageNumber);
411 m_docPriv->performSetAnnotationContents(m_newContents, m_annotation, m_pageNumber);
412 Q_EMIT m_docPriv->m_parent->annotationContentsChangedByUndoRedo(m_annotation, m_newContents, m_newCursorPos, m_newCursorPos);
413}
414
415int EditAnnotationContentsCommand::id() const
416{
417 return 2;
418}
419
420bool EditAnnotationContentsCommand::mergeWith(const QUndoCommand *uc)
421{
422 EditAnnotationContentsCommand *euc = (EditAnnotationContentsCommand *)uc;
423 // Only attempt merge of euc into this if they modify the same annotation
424 if (m_annotation == euc->m_annotation) {
425 return EditTextCommand::mergeWith(uc);
426 } else {
427 return false;
428 }
429}
430
431bool EditAnnotationContentsCommand::refreshInternalPageReferences(const QVector<Page *> &newPagesVector)
432{
433 auto a = newPagesVector[m_pageNumber]->annotation(m_annotation->uniqueName());
434 if (a) {
435 m_annotation = a;
436 }
437
438 return true;
439}
440
441EditFormTextCommand::EditFormTextCommand(Okular::DocumentPrivate *docPriv, Okular::FormFieldText *form, int pageNumber, const QString &newContents, int newCursorPos, const QString &prevContents, int prevCursorPos, int prevAnchorPos)
442 : EditTextCommand(newContents, newCursorPos, prevContents, prevCursorPos, prevAnchorPos)
443 , m_docPriv(docPriv)
444 , m_form(form)
445 , m_pageNumber(pageNumber)
446{
447 setText(i18nc("Edit an form's text contents", "edit form contents"));
448}
449
450void EditFormTextCommand::undo()
451{
452 moveViewportIfBoundingRectNotFullyVisible(m_form->rect(), m_docPriv, m_pageNumber);
453 m_form->setText(m_prevContents);
454 Q_EMIT m_docPriv->m_parent->formTextChangedByUndoRedo(m_pageNumber, m_form, m_prevContents, m_prevCursorPos, m_prevAnchorPos);
455}
456
457void EditFormTextCommand::redo()
458{
459 moveViewportIfBoundingRectNotFullyVisible(m_form->rect(), m_docPriv, m_pageNumber);
460 m_form->setText(m_newContents);
461 Q_EMIT m_docPriv->m_parent->formTextChangedByUndoRedo(m_pageNumber, m_form, m_newContents, m_newCursorPos, m_newCursorPos);
462}
463
464int EditFormTextCommand::id() const
465{
466 return 3;
467}
468
469bool EditFormTextCommand::mergeWith(const QUndoCommand *uc)
470{
471 EditFormTextCommand *euc = (EditFormTextCommand *)uc;
472 // Only attempt merge of euc into this if they modify the same form
473 if (m_form == euc->m_form) {
474 return EditTextCommand::mergeWith(uc);
475 } else {
476 return false;
477 }
478}
479
480bool EditFormTextCommand::refreshInternalPageReferences(const QVector<Page *> &newPagesVector)
481{
482 m_form = dynamic_cast<FormFieldText *>(Okular::PagePrivate::findEquivalentForm(newPagesVector[m_pageNumber], m_form));
483
484 return m_form;
485}
486
487EditFormListCommand::EditFormListCommand(Okular::DocumentPrivate *docPriv, FormFieldChoice *form, int pageNumber, const QList<int> &newChoices, const QList<int> &prevChoices)
488 : m_docPriv(docPriv)
489 , m_form(form)
490 , m_pageNumber(pageNumber)
491 , m_newChoices(newChoices)
492 , m_prevChoices(prevChoices)
493{
494 setText(i18nc("Edit a list form's choices", "edit list form choices"));
495}
496
497void EditFormListCommand::undo()
498{
499 moveViewportIfBoundingRectNotFullyVisible(m_form->rect(), m_docPriv, m_pageNumber);
500 m_form->setCurrentChoices(m_prevChoices);
501 Q_EMIT m_docPriv->m_parent->formListChangedByUndoRedo(m_pageNumber, m_form, m_prevChoices);
502 m_docPriv->notifyFormChanges(m_pageNumber);
503}
504
505void EditFormListCommand::redo()
506{
507 moveViewportIfBoundingRectNotFullyVisible(m_form->rect(), m_docPriv, m_pageNumber);
508 m_form->setCurrentChoices(m_newChoices);
509 Q_EMIT m_docPriv->m_parent->formListChangedByUndoRedo(m_pageNumber, m_form, m_newChoices);
510 m_docPriv->notifyFormChanges(m_pageNumber);
511}
512
513bool EditFormListCommand::refreshInternalPageReferences(const QVector<Page *> &newPagesVector)
514{
515 m_form = dynamic_cast<FormFieldChoice *>(Okular::PagePrivate::findEquivalentForm(newPagesVector[m_pageNumber], m_form));
516
517 return m_form;
518}
519
520EditFormComboCommand::EditFormComboCommand(Okular::DocumentPrivate *docPriv, FormFieldChoice *form, int pageNumber, const QString &newContents, int newCursorPos, const QString &prevContents, int prevCursorPos, int prevAnchorPos)
521 : EditTextCommand(newContents, newCursorPos, prevContents, prevCursorPos, prevAnchorPos)
522 , m_docPriv(docPriv)
523 , m_form(form)
524 , m_pageNumber(pageNumber)
525 , m_newIndex(-1)
526 , m_prevIndex(-1)
527{
528 setText(i18nc("Edit a combo form's selection", "edit combo form selection"));
529
530 // Determine new and previous choice indices (if any)
531 for (int i = 0; i < m_form->choices().size(); i++) {
532 if (m_form->choices().at(i) == m_prevContents) {
533 m_prevIndex = i;
534 }
535
536 if (m_form->choices().at(i) == m_newContents) {
537 m_newIndex = i;
538 }
539 }
540}
541
542void EditFormComboCommand::undo()
543{
544 if (m_prevIndex != -1) {
545 m_form->setCurrentChoices(QList<int>() << m_prevIndex);
546 } else {
547 m_form->setEditChoice(m_prevContents);
548 }
549 moveViewportIfBoundingRectNotFullyVisible(m_form->rect(), m_docPriv, m_pageNumber);
550 Q_EMIT m_docPriv->m_parent->formComboChangedByUndoRedo(m_pageNumber, m_form, m_prevContents, m_prevCursorPos, m_prevAnchorPos);
551}
552
553void EditFormComboCommand::redo()
554{
555 if (m_newIndex != -1) {
556 m_form->setCurrentChoices(QList<int>() << m_newIndex);
557 } else {
558 m_form->setEditChoice(m_newContents);
559 }
560 moveViewportIfBoundingRectNotFullyVisible(m_form->rect(), m_docPriv, m_pageNumber);
561 Q_EMIT m_docPriv->m_parent->formComboChangedByUndoRedo(m_pageNumber, m_form, m_newContents, m_newCursorPos, m_newCursorPos);
562}
563
564int EditFormComboCommand::id() const
565{
566 return 4;
567}
568
569bool EditFormComboCommand::mergeWith(const QUndoCommand *uc)
570{
571 EditFormComboCommand *euc = (EditFormComboCommand *)uc;
572 // Only attempt merge of euc into this if they modify the same form
573 if (m_form == euc->m_form) {
574 bool shouldMerge = EditTextCommand::mergeWith(uc);
575 if (shouldMerge) {
576 m_newIndex = euc->m_newIndex;
577 }
578 return shouldMerge;
579 } else {
580 return false;
581 }
582}
583
584bool EditFormComboCommand::refreshInternalPageReferences(const QVector<Page *> &newPagesVector)
585{
586 m_form = dynamic_cast<FormFieldChoice *>(Okular::PagePrivate::findEquivalentForm(newPagesVector[m_pageNumber], m_form));
587
588 return m_form;
589}
590
591EditFormButtonsCommand::EditFormButtonsCommand(Okular::DocumentPrivate *docPriv, int pageNumber, const QList<FormFieldButton *> &formButtons, const QList<bool> &newButtonStates)
592 : m_docPriv(docPriv)
593 , m_pageNumber(pageNumber)
594 , m_formButtons(formButtons)
595 , m_newButtonStates(newButtonStates)
596 , m_prevButtonStates(QList<bool>())
597{
598 setText(i18nc("Edit the state of a group of form buttons", "edit form button states"));
599 for (const FormFieldButton *formButton : std::as_const(m_formButtons)) {
600 m_prevButtonStates.append(formButton->state());
601 m_pageNumbers.append(formButton->page()->number());
602 }
603}
604
605void EditFormButtonsCommand::undo()
606{
607 clearFormButtonStates();
608 QSet<int> extraPages;
609 for (int i = 0; i < m_formButtons.size(); i++) {
610 bool checked = m_prevButtonStates.at(i);
611 if (checked) {
612 m_formButtons.at(i)->setState(checked);
613 }
614 if (m_pageNumbers.at(i) != m_pageNumber) {
615 extraPages << m_pageNumbers.at(i);
616 }
617 }
618
619 Okular::NormalizedRect boundingRect = buildBoundingRectangleForButtons(m_formButtons);
620 moveViewportIfBoundingRectNotFullyVisible(boundingRect, m_docPriv, m_pageNumber);
621 Q_EMIT m_docPriv->m_parent->formButtonsChangedByUndoRedo(m_pageNumber, m_formButtons);
622 m_docPriv->notifyFormChanges(m_pageNumber);
623 for (auto page : std::as_const(extraPages)) {
624 m_docPriv->notifyFormChanges(page);
625 }
626}
627
628void EditFormButtonsCommand::redo()
629{
630 clearFormButtonStates();
631 QSet<int> extraPages;
632 for (int i = 0; i < m_formButtons.size(); i++) {
633 bool checked = m_newButtonStates.at(i);
634 if (checked) {
635 m_formButtons.at(i)->setState(checked);
636 }
637 if (m_pageNumbers.at(i) != m_pageNumber) {
638 extraPages << m_pageNumbers.at(i);
639 }
640 }
641
642 Okular::NormalizedRect boundingRect = buildBoundingRectangleForButtons(m_formButtons);
643 moveViewportIfBoundingRectNotFullyVisible(boundingRect, m_docPriv, m_pageNumber);
644 Q_EMIT m_docPriv->m_parent->formButtonsChangedByUndoRedo(m_pageNumber, m_formButtons);
645 m_docPriv->notifyFormChanges(m_pageNumber);
646 for (auto page : std::as_const(extraPages)) {
647 m_docPriv->notifyFormChanges(page);
648 }
649}
650
651bool EditFormButtonsCommand::refreshInternalPageReferences(const QVector<Okular::Page *> &newPagesVector)
652{
653 const QList<FormFieldButton *> oldFormButtons = m_formButtons;
654 m_formButtons.clear();
655 for (int i = 0; i < oldFormButtons.size(); i++) {
656 FormFieldButton *button = dynamic_cast<FormFieldButton *>(Okular::PagePrivate::findEquivalentForm(newPagesVector[m_pageNumbers[i]], oldFormButtons[i]));
657 if (!button) {
658 return false;
659 }
660 m_formButtons << button;
661 }
662
663 return true;
664}
665
666void EditFormButtonsCommand::clearFormButtonStates()
667{
668 for (FormFieldButton *formButton : std::as_const(m_formButtons)) {
669 formButton->setState(false);
670 }
671}
672
673}
Annotation struct holds properties shared by all annotations.
Definition annotations.h:99
Interface of a text form field.
Definition form.h:310
NormalizedPoint is a helper class which stores the coordinates of a normalized point.
Definition area.h:117
double x
The normalized x coordinate.
Definition area.h:166
double y
The normalized y coordinate.
Definition area.h:171
A NormalizedRect is a rectangle which can be defined by two NormalizedPoints.
Definition area.h:189
double bottom
The normalized bottom coordinate.
Definition area.h:435
double right
The normalized right coordinate.
Definition area.h:430
double left
The normalized left coordinate.
Definition area.h:420
double top
The normalized top coordinate.
Definition area.h:425
void transform(const QTransform &matrix)
Transforms the normalized rectangle with the operations defined by matrix.
Definition area.cpp:253
QString i18nc(const char *context, const char *text, const TYPE &arg...)
global.h
Definition action.h:17
void clear()
qsizetype size() const const
QString left(qsizetype n) const const
QString right(qsizetype n) const const
QStringView left(qsizetype length) const const
QStringView right(qsizetype length) const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.