daa4b2105d81fbc040be1ecf2beb4d4381b5e22e
[ipdf/code.git] / src / controlpanel.cpp
1 /**
2  * Definitions for Qt4 based control panel
3  */
4
5 #include "controlpanel.h"
6
7 #ifndef CONTROLPANEL_DISABLED
8
9 #include "view.h"
10 #include "screen.h"
11 #include "document.h"
12 #include <string>
13 #include <algorithm>
14
15 using namespace std;
16
17 namespace IPDF
18 {
19         
20         
21 ControlPanel::ControlPanel(RunArgs & args, QWidget * p) : QMainWindow(p), 
22         m_view(args.view), m_doc(args.doc), m_screen(args.screen), m_width(300), m_height(300),
23         m_state(ControlPanel::ABOUT), m_on_ok(NULL)
24 {
25         // Size
26         resize(m_width,m_height);
27
28         
29         // Main menues
30         CreateMainMenu();
31         CreateViewMenu();
32         CreateDocumentMenu();
33         CreateScreenMenu();
34         
35         CreateLayout();
36         
37         UpdateAll();
38 }
39
40 void ControlPanel::CreateLayout()
41 {
42         m_text_edit = new QTextEdit(this);
43         m_text_edit->setGeometry(10,35,m_width-20,m_height-100);
44         
45         m_ok_button = new QPushButton("OK", this);
46         m_ok_button->setGeometry(10,35+m_height-90, m_width-20, 50);
47         connect(m_ok_button, SIGNAL(clicked()), this, SLOT(PressOK()));
48 }
49
50 QMenu * ControlPanel::CreateMainMenu()
51 {
52         QMenu * main = menuBar()->addMenu("&Main");
53         
54         QAction * about = new QAction("&About", this);
55         main->addAction(about);
56         connect(about, SIGNAL(triggered()), this, SLOT(StateAbout()));
57         
58         
59         // Quit entry
60         QAction * quit = new QAction("&Quit", this);
61         main->addAction(quit);
62         connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
63         return main;
64 }
65
66 QMenu * ControlPanel::CreateDocumentMenu()
67 {
68         QMenu * document = menuBar()->addMenu("&Document");
69         
70         m_document_set_font = new QAction("&Set Insertion Font", this);
71         document->addAction(m_document_set_font);
72         connect(m_document_set_font, SIGNAL(triggered()), this, SLOT(SetDocumentFont()));
73         
74         m_document_insert_text = new QAction("&Insert Text", this);
75         document->addAction(m_document_insert_text);
76         connect(m_document_insert_text, SIGNAL(triggered()), this, SLOT(StateInsertText()));
77         
78         m_document_load_svg = new QAction("&Load SVG From File", this);
79         document->addAction(m_document_load_svg);
80         connect(m_document_load_svg, SIGNAL(triggered()), this, SLOT(LoadSVGIntoDocument()));
81         
82         m_document_parse_svg = new QAction("&Input SVG Manually", this);
83         document->addAction(m_document_parse_svg);
84         connect(m_document_parse_svg, SIGNAL(triggered()), this, SLOT(StateParseSVG()));
85         
86         
87         return document;
88 }
89
90 QMenu * ControlPanel::CreateViewMenu()
91 {
92         QMenu * view = menuBar()->addMenu("&View");
93         
94         m_view_set_bounds = new QAction("&Set bounds", this);
95         view->addAction(m_view_set_bounds);
96         connect(m_view_set_bounds, SIGNAL(triggered()), this, SLOT(SetViewBounds()));
97         
98         m_view_show_bezier_bounds = new QAction("&Show Bezier Bounds", this);
99         m_view_show_bezier_bounds->setCheckable(true);
100         view->addAction(m_view_show_bezier_bounds);
101         connect(m_view_show_bezier_bounds, SIGNAL(triggered()), this, SLOT(ToggleShowBezierBounds()));
102         
103         m_view_show_bezier_type = new QAction("&Show Bezier Type", this);
104         m_view_show_bezier_type->setCheckable(true);
105         view->addAction(m_view_show_bezier_type);
106         connect(m_view_show_bezier_type, SIGNAL(triggered()), this, SLOT(ToggleShowBezierType()));
107         
108         m_view_show_fill_bounds = new QAction("&Show Fill Bounds", this);
109         m_view_show_fill_bounds->setCheckable(true);
110         view->addAction(m_view_show_fill_bounds);
111         connect(m_view_show_fill_bounds, SIGNAL(triggered()), this, SLOT(ToggleShowFillBounds()));
112         
113         m_view_show_fill_points = new QAction("&Show Fill Points", this);
114         m_view_show_fill_points->setCheckable(true);
115         view->addAction(m_view_show_fill_points);
116         connect(m_view_show_fill_bounds, SIGNAL(triggered()), this, SLOT(ToggleShowFillPoints()));
117         
118         m_view_enable_shading = new QAction("&Enable Shading", this);
119         m_view_enable_shading->setCheckable(true);
120         view->addAction(m_view_enable_shading);
121         connect(m_view_enable_shading, SIGNAL(triggered()), this, SLOT(ToggleEnableShading()));
122         
123         return view;
124 }
125
126
127
128 QMenu * ControlPanel::CreateScreenMenu()
129 {
130         QMenu * screen = menuBar()->addMenu("&Screen");
131         
132         m_screen_gpu_rendering = new QAction("&GPU Rendering", this);
133         m_screen_gpu_rendering->setCheckable(true);
134         m_screen_gpu_rendering->setToolTip("Uses the GPU for Rendering");
135         
136         m_screen_cpu_rendering = new QAction("&CPU Rendering", this);
137         m_screen_cpu_rendering->setCheckable(true);
138         m_screen_gpu_rendering->setToolTip("Uses the CPU for Rendering");
139         
140         m_screen_lazy_rendering = new QAction("&Lazy Rendering", this);
141         m_screen_lazy_rendering->setCheckable(true);
142                 
143         screen->addAction(m_screen_gpu_rendering);
144         screen->addAction(m_screen_cpu_rendering);
145         
146         screen->addAction(m_screen_lazy_rendering);
147         connect(m_screen_lazy_rendering, SIGNAL(triggered()), this, SLOT(ToggleLazyRendering()));
148         
149         connect(m_screen_gpu_rendering, SIGNAL(triggered()), this, SLOT(SetGPURendering()));
150         connect(m_screen_cpu_rendering, SIGNAL(triggered()), this, SLOT(SetCPURendering()));
151         
152         m_screen_show_debug = new QAction("&Print Debug Info", this);
153         m_screen_show_debug->setCheckable(true);
154         
155         screen->addAction(m_screen_show_debug);
156         connect(m_screen_show_debug, SIGNAL(triggered()), this, SLOT(ToggleScreenDebugFont()));
157         
158         return screen;
159 }
160
161 void ControlPanel::paintEvent(QPaintEvent * e)
162 {
163 //      Debug("Called");
164         
165 }
166
167 void ControlPanel::ChangeState(State next_state)
168 {
169         m_state = next_state;
170         UpdateAll();
171 }
172
173
174 void ControlPanel::UpdateAll()
175 {
176         bool using_gpu_rendering = m_view.UsingGPURendering();
177         m_screen_gpu_rendering->setChecked(using_gpu_rendering);
178         m_screen_cpu_rendering->setChecked(!using_gpu_rendering);       
179         m_screen_show_debug->setChecked(m_screen.DebugFontShown());
180         m_screen_lazy_rendering->setChecked(m_view.UsingLazyRendering());
181         
182         m_view_show_bezier_bounds->setChecked(m_view.ShowingBezierBounds());
183         m_view_show_bezier_type->setChecked(m_view.ShowingBezierType());
184         m_view_show_fill_bounds->setChecked(m_view.ShowingFillBounds());
185         m_view_show_fill_points->setChecked(m_view.ShowingFillPoints());
186         m_view_enable_shading->setChecked(m_view.PerformingShading());
187         
188         
189         // update things based on state
190         const char * title;
191         const char * tooltip;
192         switch (m_state)
193         {
194                 case INSERT_TEXT:
195                         title = "Insert Text";
196                         tooltip = "Type text to insert, press OK, simple.";
197                         m_text_edit->show();
198                         m_ok_button->show();
199                         m_on_ok = &ControlPanel::InsertTextIntoDocument;
200                         if (m_text_edit->toPlainText() == "")
201                                 m_text_edit->setText("The quick brown\nfox jumps over\nthe lazy dog.");
202                         break;
203                 case PARSE_SVG:
204                         title = "Parse SVG";
205                         tooltip = "Enter valid SVG and press OK to insert.";
206                         m_text_edit->show();
207                         m_ok_button->show();
208                         m_on_ok = &ControlPanel::InsertSVGIntoDocument;
209                         if (m_text_edit->toPlainText() == "")
210                                 m_text_edit->setText("<svg width=\"104\" height=\"186\">\n<path d = \"m 57,185\n\t c 0,0 57,-13 32,-43\n\t -25,-30 -53,2 -25, -30\n\t 28,-32 52,17 28,-32\n\t -24,-50 -16,44 -35,12\n\t-19,-32 13,-64 13,-64\n\t 0,0 40,-50 -0,-14\n\t -40,36 -94,68 -59,109\n\t 35,41 45,62 45,62 z\"/>\n</svg>");
211                         
212                         break;
213                 case ABOUT:
214                 default:
215                         title = "IPDF Control Panel";
216                         tooltip = "This is the IPDF Control Panel\nDo you feel in control?";
217                         m_text_edit->hide();
218                         m_ok_button->hide();
219                         m_on_ok = NULL;
220                         break;
221         }
222         
223         // Title
224         setWindowTitle(title);
225         // Tooltip
226         setToolTip(tooltip);
227 }
228
229 void ControlPanel::ToggleShowBezierBounds()
230 {
231         bool state = m_view.ShowingBezierBounds();
232         m_view.ShowBezierBounds(!state);
233         UpdateAll();
234 }
235 void ControlPanel::ToggleShowBezierType()
236 {
237         bool state = m_view.ShowingBezierType();
238         m_view.ShowBezierType(!state);
239         UpdateAll();
240 }
241 void ControlPanel::ToggleShowFillBounds()
242 {
243         bool state = m_view.ShowingFillBounds();
244         m_view.ShowFillBounds(!state);
245         UpdateAll();
246 }
247
248 void ControlPanel::ToggleShowFillPoints()
249 {
250         bool state = m_view.ShowingFillPoints();
251         m_view.ShowFillPoints(!state);
252         UpdateAll();
253 }
254
255 void ControlPanel::ToggleEnableShading()
256 {
257         bool state = m_view.PerformingShading();
258         m_view.PerformShading(!state);
259         UpdateAll();
260 }
261
262 void ControlPanel::SetGPURendering()
263 {
264         m_view.SetGPURendering(true);
265         UpdateAll();
266 }
267
268 void ControlPanel::SetCPURendering()
269 {
270         m_view.SetGPURendering(false);
271         UpdateAll();
272 }
273
274 void ControlPanel::ToggleLazyRendering()
275 {
276         bool state = m_view.UsingLazyRendering();
277         m_view.SetLazyRendering(!state);
278         UpdateAll();
279 }
280
281 void ControlPanel::ToggleScreenDebugFont()
282 {
283         bool state = m_screen.DebugFontShown();
284         m_screen.ShowDebugFont(!state);
285         UpdateAll();
286         
287 }
288
289 void ControlPanel::SetViewBounds()
290 {
291         bool ok;
292         Real xx = QInputDialog::getDouble(this, "View X Coordinate", "Enter X coordinate:", 0, -2e-30, 2e30,30,&ok);
293         
294         Real yy = QInputDialog::getDouble(this, "View Y Coordinate", "Enter Y coordinate:", 0, -2e-30, 2e30,30,&ok);
295         
296         Real w = QInputDialog::getDouble(this, "View Width", "Enter Width:", 1, -2e-30, 2e30,30,&ok);
297         
298         Real h = QInputDialog::getDouble(this, "View Height", "Enter Height:", 1, -2e-30, 2e30,30,&ok);
299         m_view.SetBounds(Rect(xx,yy,w,h));
300         
301 }
302
303 void ControlPanel::InsertTextIntoDocument()
304 {
305         const Rect & bounds = m_view.GetBounds();
306         Real xx = bounds.x;
307         Real yy = bounds.y + bounds.h/Real(2);
308         
309         string msg = m_text_edit->toPlainText().toStdString();
310         Real scale = bounds.h / Real(2);
311         Debug("Insert \"%s\" at %f, %f, scale %f", msg.c_str(), Float(xx), Float(yy), Float(scale));
312         m_doc.AddText(msg, scale, xx, yy);
313         m_view.ForceRenderDirty();
314         m_view.ForceBufferDirty();
315         m_view.ForceBoundsDirty();
316 }
317 void ControlPanel::InsertSVGIntoDocument()
318 {
319         Rect bounds(m_view.GetBounds());
320         bounds.x += bounds.w/Real(2);
321         bounds.y += bounds.h/Real(2);
322         
323         bounds.w /= Real(m_screen.ViewportWidth());
324         bounds.h /= Real(m_screen.ViewportHeight());
325         
326         m_doc.ParseSVG(m_text_edit->toPlainText().toStdString(), bounds);
327         m_view.ForceRenderDirty();
328         m_view.ForceBufferDirty();
329         m_view.ForceBoundsDirty();
330 }
331
332 void ControlPanel::LoadSVGIntoDocument()
333 {
334
335         QString filename = QFileDialog::getOpenFileName(this, "Open SVG", "svg-tests", "Image Files (*.svg)");
336         if (filename == "")
337                 return;
338         
339         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
340                 Rect bounds(0,0,1,1);
341         #else
342         Rect bounds(m_view.GetBounds());
343         #endif
344         bounds.x += bounds.w/Real(2);
345         bounds.y += bounds.h/Real(2);
346         
347         bounds.w /= Real(m_screen.ViewportWidth());
348         bounds.h /= Real(m_screen.ViewportHeight());
349         
350         m_doc.LoadSVG(filename.toStdString(), bounds);
351         m_view.ForceRenderDirty();
352         m_view.ForceBufferDirty();
353         m_view.ForceBoundsDirty();
354 }
355
356 void ControlPanel::SetDocumentFont()
357 {
358         QString filename = QFileDialog::getOpenFileName(this, "Set Font", "fonts", "True Type Fonts (*.ttf)");
359         if (filename != "")
360                 m_doc.SetFont(filename.toStdString());
361 }
362
363 ControlPanel * ControlPanel::g_panel = NULL;
364
365 int ControlPanel::Run(void * args)
366 {
367         ControlPanel::RunArgs * a = (ControlPanel::RunArgs*)args;
368         QApplication app(a->argc, a->argv);
369         g_panel = new ControlPanel(*a);
370         g_panel->show();
371         int result = app.exec();
372         a->screen.RequestQuit();
373         delete g_panel;
374         return result;
375 }
376         
377         
378 }
379
380 #endif //CONTROLPANEL_ENABLED
381

UCC git Repository :: git.ucc.asn.au