forked from adlr/PDFSketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_view.cc
815 lines (739 loc) · 26.2 KB
/
document_view.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Copyright...
#include "document_view.h"
#include <stdio.h>
#include <cairo.h>
#include <cairo-pdf.h>
#include <google/protobuf/text_format.h>
#include <poppler-page.h>
#include <poppler-page-renderer.h>
#include "graphic_factory.h"
#include "rectangle.h"
using std::make_pair;
using std::pair;
using std::set;
using std::shared_ptr;
using std::string;
using std::unique_ptr;
using std::vector;
namespace pdfsketch {
namespace {
const double kSpacing = 20.0; // between pages
} // namespace {}
void DocumentView::LoadFromPDF(const char* pdf_doc, size_t pdf_doc_length) {
poppler_doc_data_.clear();
poppler_doc_data_.insert(poppler_doc_data_.begin(),
pdf_doc,
pdf_doc + pdf_doc_length);
poppler_doc_.reset(poppler::document::load_from_raw_data(&poppler_doc_data_[0], poppler_doc_data_.size()));
UpdateSize();
SetNeedsDisplay();
}
void DocumentView::GetPDFData(const char** out_buf,
size_t* out_len) const {
if (!poppler_doc_.get())
return;
// *out_buf = doc_->buf();
// *out_len = doc_->buf_len();
*out_buf = &poppler_doc_data_[0];
*out_len = poppler_doc_data_.size();
}
void DocumentView::UpdateSize() {
// Update bounds
double max_page_width = 0.0; // w/o spacing
double total_height = kSpacing; // w/ spacing
for (int i = 0; i < poppler_doc_->pages(); i++) {
Size size = PageSize(i).ScaledBy(zoom_).RoundedUp();
max_page_width = std::max(max_page_width, size.width_);
total_height += size.height_ + kSpacing;
}
SetSize(Size(max_page_width + 2 * kSpacing, total_height));
// invalidate cache
cached_subrect_ = Rect();
}
namespace {
template<typename Set, typename Key>
bool SetContainsKey(const Set& the_set, const Key& the_key) {
return the_set.find(the_key) != the_set.end();
}
} // namespace {}
void DocumentView::SerializeGraphics(
bool selected_only,
pdfsketchproto::Document* msg) const {
for (Graphic* gr = bottom_graphic_; gr; gr = gr->upper_sibling_) {
if (selected_only && !SetContainsKey(selected_graphics_, gr))
continue;
pdfsketchproto::Graphic* gr_msg = msg->add_graphic();
gr->Serialize(gr_msg);
}
}
void DocumentView::SetZoom(double zoom) {
zoom_ = zoom;
UpdateSize();
}
Size DocumentView::PageSize(int page) const {
if (!poppler_doc_.get())
return Size();
unique_ptr<poppler::page> ppage(poppler_doc_->create_page(page));
if (!ppage.get())
printf("Bug - null page3\n");
poppler::rectf rect = ppage->page_rect();
return Size(rect.width(), rect.height());
// return Size(doc_->GetPageWidth(page), doc_->GetPageHeight(page));
}
Rect DocumentView::PageRect(int page) const {
double existing_pages_height = kSpacing;
for (int i = 0; i < page; i++) {
unique_ptr<poppler::page> ppage(poppler_doc_->create_page(i));
if (!ppage.get())
printf("Bug - null page\n");
poppler::rectf rect = ppage->page_rect();
existing_pages_height += ceil(rect.height() * zoom_) + kSpacing;
}
Size page_size = PageSize(page).ScaledBy(zoom_).RoundedUp();
double offset = static_cast<int>(size_.width_ / 2 - page_size.width_ / 2);
return Rect(offset, existing_pages_height,
page_size.width_, page_size.height_);
}
int DocumentView::PageForPoint(const Point& point) const {
for (int i = 0; i < poppler_doc_->pages(); i++) {
Rect page_rect = PageRect(i);
if (point.y_ <= page_rect.Bottom())
return i;
}
return poppler_doc_->pages() - 1;
}
Point DocumentView::ConvertPointToPage(const Point& point, int page) const {
Rect page_rect = PageRect(page);
return point.TranslatedBy(-page_rect.Left(), -page_rect.Top()).
ScaledBy(1 / zoom_);
}
Point DocumentView::ConvertPointFromPage(const Point& point, int page) const {
Rect page_rect = PageRect(page);
return point.ScaledBy(zoom_).TranslatedBy(page_rect.Left(), page_rect.Top());
}
void DocumentView::GetVisibleCenterPageAndPoint(Point* out_point,
int* out_page) const {
Point center = VisibleSubrect().Center();
int placeholder;
if (!out_page)
out_page = &placeholder;
*out_page = PageForPoint(center);
if (out_point)
*out_point = ConvertPointToPage(center, *out_page);
}
void DocumentView::InsertGraphicAfter(shared_ptr<Graphic> graphic,
Graphic* upper_sibling) {
graphic->SetDelegate(this);
if (!upper_sibling) {
if (top_graphic_) {
top_graphic_->upper_sibling_ = graphic.get();
} else {
bottom_graphic_ = graphic.get();
}
graphic->lower_sibling_ = top_graphic_;
graphic->upper_sibling_ = NULL;
top_graphic_ = graphic;
} else {
if (upper_sibling->lower_sibling_.get())
graphic->lower_sibling_ = upper_sibling->lower_sibling_;
else
graphic->lower_sibling_.reset();
graphic->upper_sibling_ = upper_sibling;
upper_sibling->lower_sibling_ = graphic;
}
graphic->SetNeedsDisplay(GraphicIsSelected(graphic.get()));
}
void DocumentView::InsertGraphicAfterUndo(shared_ptr<Graphic> graphic,
Graphic* upper_sibling) {
InsertGraphicAfter(graphic, upper_sibling);
set<Graphic*> gr;
gr.insert(graphic.get());
undo_manager_->AddClosure([this, gr] () {
RemoveGraphicsUndo(gr);
});
}
shared_ptr<Graphic> DocumentView::RemoveGraphic(Graphic* graphic) {
graphic->SetNeedsDisplay(GraphicIsSelected(graphic));
if (GraphicIsSelected(graphic)) {
selected_graphics_.erase(selected_graphics_.find(graphic));
}
shared_ptr<Graphic> ret;
if (graphic->upper_sibling_) {
ret = graphic->upper_sibling_->lower_sibling_;
graphic->upper_sibling_->lower_sibling_ = graphic->lower_sibling_;
} else if (top_graphic_.get() == graphic) {
ret = top_graphic_;
top_graphic_ = graphic->lower_sibling_;
} else {
printf("top graphic is wrong!\n");
}
if (graphic->lower_sibling_) {
graphic->lower_sibling_->upper_sibling_ = graphic->upper_sibling_;
} else if (bottom_graphic_ == graphic) {
bottom_graphic_ = graphic->upper_sibling_;
} else {
printf("bottom graphic is wrong!\n");
}
graphic->upper_sibling_ = NULL;
graphic->lower_sibling_.reset();
return ret;
}
void DocumentView::RestoreGraphicUndo(Graphic* gr, const string& proto_msg) {
pdfsketchproto::Graphic old_msg;
gr->Serialize(&old_msg);
string old_str;
if (!old_msg.SerializeToString(&old_str)) {
printf("Graphic serialize protobuf error\n");
}
if (proto_msg != old_str)
undo_manager_->AddClosure([this, gr, old_str] () {
RestoreGraphicUndo(gr, old_str);
});
pdfsketchproto::Graphic msg;
msg.ParseFromString(proto_msg);
gr->Restore(msg);
gr->SetNeedsDisplay(false);
}
void DocumentView::DrawRect(cairo_t* cr, const Rect& rect) {
// Do some caching
if (VisibleSubrect() != cached_subrect_ && poppler_doc_.get()) {
cached_subrect_ = VisibleSubrect();
if (cached_surface_) {
cairo_surface_finish(cached_surface_);
cairo_surface_destroy(cached_surface_);
cached_surface_ = NULL;
}
// Create new cache
double width = cached_subrect_.size_.width_;
double height = cached_subrect_.size_.height_;
cairo_user_to_device_distance(cr, &width, &height);
// Avoid divide by zero:
float device_zoom = cached_subrect_.size_.width_ > 0 ?
(width / cached_subrect_.size_.width_) : 1.0;
cached_surface_device_zoom_ = device_zoom;
cached_surface_ =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t* cache_cr = cairo_create(cached_surface_);
cairo_translate(cache_cr, -cached_subrect_.Left() * device_zoom, -cached_subrect_.Top() * device_zoom);
poppler::page_renderer renderer;
for (int i = 0; i < poppler_doc_->pages(); i++) {
Rect page_rect = PageRect(i);
if (!cached_subrect_.Intersects(page_rect.ScaledBy(device_zoom)))
continue;
// draw this page
cairo_save(cache_cr);
cairo_scale(cache_cr, device_zoom, device_zoom);
cairo_set_source_rgb(cache_cr, 0.0, 0.0, 0.0);
cairo_set_line_width(cache_cr, 1.0);
Rect outline = page_rect.InsetBy(-0.5);
outline.CairoRectangle(cache_cr);
cairo_stroke(cache_cr);
cairo_set_source_rgb(cache_cr, 1.0, 1.0, 1.0);
page_rect.CairoRectangle(cache_cr);
cairo_fill(cache_cr);
cairo_translate(cache_cr, page_rect.origin_.x_, page_rect.origin_.y_);
cairo_scale(cache_cr, zoom_, zoom_);
unique_ptr<poppler::page> page(poppler_doc_->create_page(i));
if (!page.get())
printf("BUG- null page in render\n");
renderer.cairo_render_page(cache_cr,
page.get(),
false); // TODO(adlr): rotation?
cairo_restore(cache_cr);
}
cairo_destroy(cache_cr);
}
if (cached_surface_) {
cairo_pattern_t* old_pattern = cairo_pattern_reference(cairo_get_source(cr));
cairo_save(cr);
cairo_scale(cr, 1.0 / cached_surface_device_zoom_,
1.0 / cached_surface_device_zoom_);
cairo_set_source_surface(cr, cached_surface_,
cached_subrect_.Left() * cached_surface_device_zoom_,
cached_subrect_.Top() * cached_surface_device_zoom_);
cairo_paint(cr);
cairo_set_source(cr, old_pattern);
cairo_pattern_destroy(old_pattern);
cairo_restore(cr);
}
if (!poppler_doc_.get()) {
double border = 15.0;
cairo_set_line_width(cr, 2.0);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
for (int i = 0; i < 3; i++) {
Rect page(0.0, double(i) * size_.height_ / 3.0,
size_.width_, size_.height_ / 3.0);
page = page.InsetBy(border + 1);
cairo_set_source_rgb(cr, i == 0, i == 1, i == 2);
page.CairoRectangle(cr);
cairo_stroke(cr);
}
return;
}
for (int i = 0; i < poppler_doc_->pages(); i++) {
Rect page_rect = PageRect(i);
if (!rect.Intersects(page_rect))
continue;
// draw this page
cairo_save(cr);
page_rect.CairoRectangle(cr);
cairo_clip(cr);
cairo_translate(cr, page_rect.origin_.x_, page_rect.origin_.y_);
cairo_scale(cr, zoom_, zoom_);
// Draw graphics
for (Graphic* gr = bottom_graphic_; gr; gr = gr->upper_sibling_) {
if (gr->Page() != i)
continue;
gr->Draw(cr, GraphicIsSelected(gr));
}
cairo_restore(cr);
}
// draw knobs
for (Graphic* gr = bottom_graphic_; gr; gr = gr->upper_sibling_) {
if (selected_graphics_.find(gr) == selected_graphics_.end())
continue;
cairo_save(cr);
Rect page_rect = PageRect(gr->Page());
cairo_translate(cr, page_rect.origin_.x_, page_rect.origin_.y_);
cairo_scale(cr, zoom_, zoom_);
gr->DrawKnobs(cr);
cairo_restore(cr);
}
}
namespace {
cairo_status_t HandleCairoStreamWrite(void* closure,
const unsigned char *data,
unsigned int length) {
vector<char>* out = reinterpret_cast<vector<char>*>(closure);
out->insert(out->end(), data, data + length);
return CAIRO_STATUS_SUCCESS;
}
} // namespace {}
void DocumentView::ExportPDF(vector<char>* out) {
if (!poppler_doc_.get()) {
printf("can't export w/o a doc\n");
return;
}
cairo_surface_t *surface = cairo_pdf_surface_create_for_stream(
HandleCairoStreamWrite, out, 6 * 72, 6 * 72);
cairo_t* cr = cairo_create(surface);
poppler::page_renderer renderer;
for (int i = 0; i < poppler_doc_->pages(); i++) {
Size pg_size = PageSize(i);
cairo_pdf_surface_set_size(surface, pg_size.width_, pg_size.height_);
// for each page:
cairo_save(cr);
unique_ptr<poppler::page> page(poppler_doc_->create_page(i));
if (!page.get())
printf("BUG: NULL page during export\n");
renderer.cairo_render_page(cr,
page.get(),
true); // TODO(adlr): rotation?
// Draw graphics
for (Graphic* gr = bottom_graphic_; gr; gr = gr->upper_sibling_) {
if (gr->Page() != i)
continue;
gr->Draw(cr, false);
}
cairo_restore(cr);
cairo_surface_show_page(surface);
}
cairo_destroy(cr);
cairo_surface_finish(surface);
cairo_surface_destroy(surface);
}
View* DocumentView::OnMouseDown(const MouseInputEvent& event) {
if (!selected_graphics_.empty()) {
// See if we hit a knob
for (Graphic* gr = top_graphic_.get(); gr; gr = gr->lower_sibling_.get()) {
if (selected_graphics_.find(gr) == selected_graphics_.end())
continue;
Point pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
gr->Page());
int knob = kKnobNone;
if ((knob = gr->PointInKnob(pos)) != kKnobNone) {
resize_graphic_original_frame_ = gr->Frame();
resizing_graphic_ = gr;
gr->BeginResize(pos, knob);
return this;
}
}
}
if (editing_graphic_) {
// See if we hit the editing graphic
Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
editing_graphic_->Page());
if (editing_graphic_->frame_.Contains(page_pos) &&
editing_graphic_->OnMouseDown(page_pos)) {
editing_graphic_handling_drag_ = true;
return this;
}
// didn't hit editing graphic. stop editing.
// Make an undo op if there was a change
pdfsketchproto::Graphic temp_msg;
editing_graphic_->Serialize(&temp_msg);
editing_graphic_->EndEditing();
string old_graphic_state;
if (!editing_checkpoint_->SerializeToString(&old_graphic_state)) {
printf("error serializing!\n");
}
string new_graphic_state;
if (!temp_msg.SerializeToString(&new_graphic_state)) {
printf("error serializing!\n");
}
if (old_graphic_state != new_graphic_state) {
Graphic* editing_graphic = editing_graphic_;
undo_manager_->AddClosure([this, editing_graphic, old_graphic_state] () {
RestoreGraphicUndo(editing_graphic, old_graphic_state);
});
}
editing_graphic_->SetNeedsDisplay(false);
editing_graphic_ = NULL;
}
if (!toolbox_)
return this;
if (toolbox_->CurrentTool() == Toolbox::ARROW) {
// See if we hit a graphic
for (Graphic* gr = top_graphic_.get(); gr; gr = gr->lower_sibling_.get()) {
Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
gr->Page());
if (gr->frame_.Contains(page_pos)) {
if (event.ClickCount() == 1) {
if (!GraphicIsSelected(gr)) {
if (!(event.modifiers() & KeyboardInputEvent::kShift))
selected_graphics_.clear();
selected_graphics_.insert(gr);
}
start_move_page_ = last_move_page_ =
PageForPoint(event.position());
start_move_pos_ = last_move_pos_ =
ConvertPointToPage(event.position(), start_move_page_);
} else if (event.ClickCount() == 2 &&
gr->Editable()) {
if (editing_graphic_) {
printf("Already editing!\n");
return this;
}
for (auto sel_gr : selected_graphics_) {
sel_gr->SetNeedsDisplay(false);
}
selected_graphics_.clear();
editing_graphic_ = gr;
editing_checkpoint_.reset(new pdfsketchproto::Graphic);
gr->Serialize(editing_checkpoint_.get());
gr->BeginEditing(undo_manager_);
}
gr->SetNeedsDisplay(true);
return this;
}
}
// Didn't hit any graphics. Select none.
for (auto gr : selected_graphics_)
gr->SetNeedsDisplay(true);
selected_graphics_.clear();
return this;
}
if (!selected_graphics_.empty() &&
!(event.modifiers() & KeyboardInputEvent::kShift)) {
for (auto gr : selected_graphics_)
gr->SetNeedsDisplay(true);
selected_graphics_.clear();
}
shared_ptr<Graphic> gr = GraphicFactory::NewGraphic(toolbox_->CurrentTool());
AddGraphic(gr);
int page = PageForPoint(event.position());
Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
page);
gr->Place(page, page_pos);
placing_graphic_ = gr.get();
return this;
}
void DocumentView::OnMouseDrag(const MouseInputEvent& event) {
if (resizing_graphic_) {
Point pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
resizing_graphic_->Page());
resizing_graphic_->UpdateResize(pos);
return;
}
if (placing_graphic_) {
Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
placing_graphic_->Page());
placing_graphic_->PlaceUpdate(page_pos);
return;
}
if (editing_graphic_ && editing_graphic_handling_drag_) {
Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
editing_graphic_->Page());
editing_graphic_->OnMouseDrag(page_pos);
return;
}
if (!selected_graphics_.empty()) {
// Did page change?
int new_page = PageForPoint(event.position());
if (new_page != last_move_page_) {
float dpage = new_page - last_move_page_;
// Move graphics to new page
for (auto gr : selected_graphics_) {
gr->SetNeedsDisplay(true);
gr->SetPage(gr->Page() + dpage);
}
last_move_page_ = new_page;
}
// Move
Point pos = ConvertPointToPage(event.position(), new_page);
double dx = pos.x_ - last_move_pos_.x_;
double dy = pos.y_ - last_move_pos_.y_;
for (set<Graphic*>::iterator it = selected_graphics_.begin(),
e = selected_graphics_.end(); it != e; ++it) {
(*it)->SetNeedsDisplay(true);
(*it)->frame_.origin_ = (*it)->frame_.origin_.TranslatedBy(dx, dy);
(*it)->SetNeedsDisplay(true);
}
last_move_pos_ = pos;
}
}
shared_ptr<Graphic> DocumentView::SharedPtrForGraphic(
Graphic* graphic) const {
if (!top_graphic_.get()) {
printf("no graphics!\n");
return shared_ptr<Graphic>();
}
if (graphic == top_graphic_.get())
return top_graphic_;
for (Graphic* gr = top_graphic_.get(); gr->lower_sibling_.get(); gr = gr->lower_sibling_.get()) {
if (gr->lower_sibling_.get() == graphic)
return gr->lower_sibling_;
}
printf("Error: %s called for missing graphic!\n", __func__);
return shared_ptr<Graphic>();
}
void DocumentView::OnMouseUp(const MouseInputEvent& event) {
if (resizing_graphic_) {
resizing_graphic_->EndResize();
Rect original_frame = resize_graphic_original_frame_;
Graphic* resizing_graphic = resizing_graphic_;
if (original_frame != resizing_graphic_->Frame()) {
// Generate undo op
undo_manager_->AddClosure(
[this, resizing_graphic, original_frame] () {
SetGraphicFrameUndo(resizing_graphic, original_frame);
});
}
resizing_graphic_ = NULL;
return;
}
if (placing_graphic_) {
placing_graphic_->SetNeedsDisplay(true);
if (placing_graphic_->PlaceComplete()) {
RemoveGraphic(placing_graphic_);
} else {
if (undo_manager_) {
set<Graphic*> gr;
gr.insert(placing_graphic_);
undo_manager_->AddClosure(
[this, gr] () {
RemoveGraphicsUndo(gr);
});
}
if (placing_graphic_->Editable()) {
editing_checkpoint_.reset(new pdfsketchproto::Graphic);
placing_graphic_->Serialize(editing_checkpoint_.get());
placing_graphic_->BeginEditing(undo_manager_);
editing_graphic_ = placing_graphic_;
} else {
selected_graphics_.insert(placing_graphic_);
}
}
placing_graphic_ = NULL;
//toolbox_->SelectTool(Toolbox::ARROW);
return;
}
if (editing_graphic_ && editing_graphic_handling_drag_) {
editing_graphic_->OnMouseUp();
editing_graphic_handling_drag_ = false;
return;
}
// Moving
if (last_move_pos_ == start_move_pos_ &&
last_move_page_ == start_move_page_)
return;
if (!undo_manager_) {
printf("no undo manager\n");
return;
}
// Generate undo op
double dx = start_move_pos_.x_ - last_move_pos_.x_;
double dy = start_move_pos_.y_ - last_move_pos_.y_;
int dpage = start_move_page_ - last_move_page_;
auto current_selected_graphics = selected_graphics_;
undo_manager_->AddClosure(
[this, current_selected_graphics, dx, dy, dpage] () {
MoveGraphicsUndo(current_selected_graphics, dx, dy, dpage);
});
}
string DocumentView::OnCopy() {
// if (editing_graphic_)
// return editing_graphic_->OnCopy();
printf("called doc view on copy\n");
string ret;
if (selected_graphics_.empty())
return ret;
pdfsketchproto::Document msg;
SerializeGraphics(true, &msg);
google::protobuf::TextFormat::PrintToString(msg, &ret);
return ret;
}
bool DocumentView::OnPaste(const string& str) {
pdfsketchproto::Document msg;
if (google::protobuf::TextFormat::ParseFromString(str, &msg)) {
// When pasting graphics, they all go onto the currently visible
// page and if necessary will be moved to somewhere visible.
int page = 0;
GetVisibleCenterPageAndPoint(nullptr, &page);
Rect visible = ConvertRectToPage(VisibleSubrect(), page);
Rect page_rect(PageSize(page));
Rect safe_rect = visible.Intersect(page_rect);
// Success in parsing
selected_graphics_.clear();
ScopedUndoAggregator undo_aggregator(undo_manager_);
for (int i = 0; i < msg.graphic_size(); i++) {
const pdfsketchproto::Graphic& gr = msg.graphic(i);
shared_ptr<Graphic> new_graphic(GraphicFactory::NewGraphic(gr));
// Set to current page
new_graphic->SetPage(page);
// Move the graphic a tad when pasting
new_graphic->frame_ = new_graphic->frame_.TranslatedBy(10.0, 10.0);
if (!new_graphic->frame_.Intersects(safe_rect)) {
Point gr_center = new_graphic->frame_.Center();
gr_center.x_ = std::min(gr_center.x_, safe_rect.Right());
gr_center.x_ = std::max(gr_center.x_, safe_rect.Left());
gr_center.y_ = std::min(gr_center.y_, safe_rect.Bottom());
gr_center.y_ = std::max(gr_center.y_, safe_rect.Top());
new_graphic->frame_.SetCenter(gr_center);
}
InsertGraphicAfterUndo(new_graphic, NULL);
selected_graphics_.insert(new_graphic.get());
}
} else {
// Create a new text graphic or pass to editing.
if (editing_graphic_)
return editing_graphic_->OnPaste(str);
shared_ptr<Graphic> new_text(GraphicFactory::NewText(str));
Point page_center;
int page = 0;
GetVisibleCenterPageAndPoint(&page_center, &page);
new_text->page_ = page;
new_text->frame_.origin_ = page_center.TranslatedBy(
-new_text->frame_.size_.width_ / 2.0,
-new_text->frame_.size_.height_ / 2.0);
InsertGraphicAfterUndo(new_text, NULL);
selected_graphics_.clear();
selected_graphics_.insert(new_text.get());
}
return true;
}
void DocumentView::MoveGraphicsUndo(const std::set<Graphic*>& graphics,
double dx, double dy, int dpage) {
printf("MoveGraphicsUndo: %f %f %d\n", dx, dy, dpage);
for (auto it = graphics.begin(), e = graphics.end();
it != e; ++it) {
(*it)->SetNeedsDisplay(true);
(*it)->frame_.origin_ = (*it)->frame_.origin_.TranslatedBy(dx, dy);
(*it)->SetPage((*it)->Page() + dpage);
(*it)->SetNeedsDisplay(true);
}
if (!undo_manager_)
return;
undo_manager_->AddClosure(
[this, graphics, dx, dy, dpage] () {
MoveGraphicsUndo(graphics, -dx, -dy, -dpage);
});
}
void DocumentView::SetGraphicFrameUndo(Graphic* gr, Rect frame) {
Rect prev_frame = gr->Frame();
gr->SetNeedsDisplay(true);
gr->frame_ = frame;
gr->SetNeedsDisplay(true);
undo_manager_->AddClosure([this, gr, prev_frame] () {
SetGraphicFrameUndo(gr, prev_frame);
});
}
void DocumentView::RemoveGraphicsUndo(set<Graphic*> graphics) {
set<pair<shared_ptr<Graphic>, Graphic*> > pairs;
for (set<Graphic*>::iterator it = graphics.begin(),
e = graphics.end(); it != e; ++it) {
(*it)->SetNeedsDisplay(true);
Graphic* upper_sibling = (*it)->upper_sibling_;
pairs.insert(make_pair(RemoveGraphic(*it), upper_sibling));
}
if (undo_manager_)
undo_manager_->AddClosure(
[this, pairs, graphics] () {
for (auto graphic : pairs)
InsertGraphicAfter(graphic.first, graphic.second);
if (undo_manager_)
undo_manager_->AddClosure(
[this, graphics] () {
RemoveGraphicsUndo(graphics);
});
});
}
bool DocumentView::OnKeyText(const KeyboardInputEvent& event) {
if (editing_graphic_) {
editing_graphic_->OnKeyText(event);
return true;
}
return false;
}
bool DocumentView::OnKeyDown(const KeyboardInputEvent& event) {
if ((event.modifiers() & KeyboardInputEvent::kControl) &&
(event.keycode() == 90)) {
// Ctrl-Z hit
if (event.modifiers() & KeyboardInputEvent::kShift)
undo_manager_->PerformRedo();
else
undo_manager_->PerformUndo();
return true;
}
if (editing_graphic_) {
editing_graphic_->OnKeyDown(event);
return true;
}
if (event.keycode() == 8 || event.keycode() == 46) { // backspace, delete
// delete selected graphics
RemoveGraphicsUndo(selected_graphics_);
}
if (!selected_graphics_.empty() && (event.keycode() == 37 ||
event.keycode() == 38 ||
event.keycode() == 39 ||
event.keycode() == 40)) {
float dx = event.keycode() == 37 ? -1.0 :
(event.keycode() == 39 ? 1.0 : 0.0);
float dy = event.keycode() == 38 ? -1.0 :
(event.keycode() == 40 ? 1.0 : 0.0);
if (event.modifiers() & KeyboardInputEvent::kShift) {
dx *= 10.0;
dy *= 10.0;
}
for (auto gr : selected_graphics_) {
gr->SetNeedsDisplay(true);
gr->SetFrame(gr->Frame().TranslatedBy(dx, dy));
gr->SetNeedsDisplay(true);
}
}
return true;
}
bool DocumentView::OnKeyUp(const KeyboardInputEvent& event) {
if (editing_graphic_) {
editing_graphic_->OnKeyUp(event);
return true;
}
return true;
}
void DocumentView::SetNeedsDisplayInPageRect(int page, const Rect& rect) {
Rect local(ConvertPointFromPage(rect.UpperLeft(), page),
ConvertPointFromPage(rect.LowerRight(), page));
SetNeedsDisplayInRect(local);
}
} // namespace pdfsketch