-
Notifications
You must be signed in to change notification settings - Fork 5
/
document_view.h
150 lines (120 loc) · 4.94 KB
/
document_view.h
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
// Copyright...
#ifndef PDFSKETCH_DOCUMENT_VIEW_H__
#define PDFSKETCH_DOCUMENT_VIEW_H__
#include <set>
#include <stdlib.h>
#include <vector>
#include <poppler-document.h>
#include "graphic.h"
#include "scroll_bar_view.h"
#include "toolbox.h"
#include "undo_manager.h"
#include "view.h"
namespace pdfsketch {
class DocumentView : public View,
public GraphicDelegate {
public:
DocumentView() {}
virtual std::string Name() const { return "DocumentView"; }
virtual void DrawRect(cairo_t* cr, const Rect& rect);
void LoadFromPDF(const char* pdf_doc, size_t pdf_doc_length);
void GetPDFData(const char** out_buf, size_t* out_len) const;
void SetZoom(double zoom);
void ExportPDF(std::vector<char>* out);
void Serialize(pdfsketchproto::Document* msg) const {
SerializeGraphics(false, msg);
}
void SetToolbox(Toolbox* toolbox) {
toolbox_ = toolbox;
}
void SetUndoManager(UndoManager* undo_manager) {
undo_manager_ = undo_manager;
}
void AddGraphic(std::shared_ptr<Graphic> graphic) {
InsertGraphicAfter(graphic, NULL);
}
void InsertImage(const char* data, size_t length);
// GraphicDelegate methods
virtual void SetNeedsDisplayInPageRect(int page, const Rect& rect);
virtual Point ConvertPointFromGraphic(int page, const Point& point) {
return ConvertPointFromPage(point, page);
}
virtual Point ConvertPointToGraphic(int page, const Point& point) {
return ConvertPointToPage(point, page);
}
virtual double GetZoom() { return zoom_; }
virtual View* OnMouseDown(const MouseInputEvent& event);
virtual void OnMouseDrag(const MouseInputEvent& event);
virtual void OnMouseUp(const MouseInputEvent& event);
virtual std::string OnCopy();
virtual bool OnPaste(const std::string& str);
void MoveGraphicsUndo(const std::set<Graphic*>& graphics,
double dx, double dy, int dpage);
void SetGraphicFrameUndo(Graphic* gr, Rect frame);
virtual bool OnKeyText(const KeyboardInputEvent& event);
virtual bool OnKeyDown(const KeyboardInputEvent& event);
virtual bool OnKeyUp(const KeyboardInputEvent& event);
private:
void SerializeGraphics(bool selected_only,
pdfsketchproto::Document* msg) const;
void UpdateSize();
Size PageSize(int page) const; // graphic/PDF coords
Rect PageRect(int page) const; // view coords
// Returns the lowest/hightest page number that may intersect with 'rect'
int MinPageForRect(const Rect& rect) const;
int MaxPageForRect(const Rect& rect) const;
// point may be outside page
int PageForPoint(const Point& point) const;
// Convert from local view coordinates to/from page coordinates
Point ConvertPointToPage(const Point& point, int page) const;
Point ConvertPointFromPage(const Point& point, int page) const;
Rect ConvertRectToPage(const Rect& rect, int page) const {
return Rect(ConvertPointToPage(rect.UpperLeft(), page),
ConvertPointToPage(rect.LowerRight(), page));
}
void GetVisibleCenterPageAndPoint(Point* out_point,
int* out_page) const;
void InsertGraphicAfter(std::shared_ptr<Graphic> graphic,
Graphic* upper_sibling);
void InsertGraphicAfterUndo(std::shared_ptr<Graphic> graphic,
Graphic* upper_sibling);
bool GraphicIsSelected(Graphic* graphic) {
return selected_graphics_.find(graphic) != selected_graphics_.end();
}
std::shared_ptr<Graphic> SharedPtrForGraphic(Graphic* graphic) const;
void RemoveGraphicsUndo(std::set<Graphic*> graphics);
// Apply proto_msg to gr
void RestoreGraphicUndo(Graphic* gr, const std::string& proto_msg);
UndoManager* undo_manager_{nullptr};
// Returns the shard_ptr of the removed graphic, incase you want to
// move it somewhere. If you ignore the return value, graphic may
// be deleted.
std::shared_ptr<Graphic> RemoveGraphic(Graphic* graphic);
// poppler::SimpleDocument* doc_;
std::vector<char> poppler_doc_data_;
std::unique_ptr<poppler::document> poppler_doc_;
float cached_surface_device_zoom_{1.0};
Rect cached_subrect_;
cairo_surface_t* cached_surface_{nullptr}; // TODO(adlr): free in dtor
// Cached page top/bottoms
std::vector<std::pair<double, double>> page_y_;
double zoom_{1.0};
Toolbox* toolbox_{nullptr};
std::shared_ptr<Graphic> top_graphic_;
Graphic* bottom_graphic_{nullptr};
Graphic* placing_graphic_{nullptr};
Graphic* editing_graphic_{nullptr};
// When editing starts, we keep a checkpoint here for undo purposes:
std::unique_ptr<pdfsketchproto::Graphic> editing_checkpoint_;
// If true, the editing graphic is handling the current mouse drag event.
bool editing_graphic_handling_drag_{false};
std::set<Graphic*> selected_graphics_;
Graphic* resizing_graphic_{nullptr};
Point last_move_pos_;
int last_move_page_{0};
Point start_move_pos_;
int start_move_page_{0};
Rect resize_graphic_original_frame_;
};
} // namespace pdfsketch
#endif // PDFSKETCH_DOCUMENT_VIEW_H__