Support colours in shading
[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_object_bounds = new QAction("&Show Object Bounds", this);
99         m_view_show_object_bounds->setCheckable(true);
100         view->addAction(m_view_show_object_bounds);
101         connect(m_view_show_object_bounds, SIGNAL(triggered()), this, SLOT(ToggleShowObjectBounds()));
102         
103         m_view_enable_shading = new QAction("&Enable Shading", this);
104         m_view_enable_shading->setCheckable(true);
105         view->addAction(m_view_enable_shading);
106         connect(m_view_enable_shading, SIGNAL(triggered()), this, SLOT(ToggleEnableShading()));
107         
108         return view;
109 }
110
111
112
113 QMenu * ControlPanel::CreateScreenMenu()
114 {
115         QMenu * screen = menuBar()->addMenu("&Screen");
116         
117         m_screen_gpu_rendering = new QAction("&GPU Rendering", this);
118         m_screen_gpu_rendering->setCheckable(true);
119         m_screen_gpu_rendering->setToolTip("Uses the GPU for Rendering");
120         
121         m_screen_cpu_rendering = new QAction("&CPU Rendering", this);
122         m_screen_cpu_rendering->setCheckable(true);
123         m_screen_gpu_rendering->setToolTip("Uses the CPU for Rendering");
124                 
125         screen->addAction(m_screen_gpu_rendering);
126         screen->addAction(m_screen_cpu_rendering);
127         
128         connect(m_screen_gpu_rendering, SIGNAL(triggered()), this, SLOT(SetGPURendering()));
129         connect(m_screen_cpu_rendering, SIGNAL(triggered()), this, SLOT(SetCPURendering()));
130         
131         m_screen_show_debug = new QAction("&Print Debug Info", this);
132         m_screen_show_debug->setCheckable(true);
133         
134         screen->addAction(m_screen_show_debug);
135         connect(m_screen_show_debug, SIGNAL(triggered()), this, SLOT(ToggleScreenDebugFont()));
136         
137         return screen;
138 }
139
140 void ControlPanel::paintEvent(QPaintEvent * e)
141 {
142 //      Debug("Called");
143         
144 }
145
146 void ControlPanel::ChangeState(State next_state)
147 {
148         m_state = next_state;
149         UpdateAll();
150 }
151
152
153 void ControlPanel::UpdateAll()
154 {
155         bool using_gpu_rendering = m_view.UsingGPURendering();
156         m_screen_gpu_rendering->setChecked(using_gpu_rendering);
157         m_screen_cpu_rendering->setChecked(!using_gpu_rendering);       
158         m_screen_show_debug->setChecked(m_screen.DebugFontShown());
159         
160         m_view_show_object_bounds->setChecked(m_view.ShowingObjectBounds());
161         m_view_enable_shading->setChecked(m_view.PerformingShading());
162         
163         // update things based on state
164         const char * title;
165         const char * tooltip;
166         switch (m_state)
167         {
168                 case INSERT_TEXT:
169                         title = "Insert Text";
170                         tooltip = "Type text to insert, press OK, simple.";
171                         m_text_edit->show();
172                         m_ok_button->show();
173                         m_on_ok = &ControlPanel::InsertTextIntoDocument;
174                         if (m_text_edit->toPlainText() == "")
175                                 m_text_edit->setText("The quick brown\nfox jumps over\nthe lazy dog.");
176                         break;
177                 case PARSE_SVG:
178                         title = "Parse SVG";
179                         tooltip = "Enter valid SVG and press OK to insert.";
180                         m_text_edit->show();
181                         m_ok_button->show();
182                         m_on_ok = &ControlPanel::InsertSVGIntoDocument;
183                         if (m_text_edit->toPlainText() == "")
184                                 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>");
185                         
186                         break;
187                 case ABOUT:
188                 default:
189                         title = "IPDF Control Panel";
190                         tooltip = "This is the IPDF Control Panel\nDo you feel in control?";
191                         m_text_edit->hide();
192                         m_ok_button->hide();
193                         m_on_ok = NULL;
194                         break;
195         }
196         
197         // Title
198         setWindowTitle(title);
199         // Tooltip
200         setToolTip(tooltip);
201 }
202
203 void ControlPanel::ToggleShowObjectBounds()
204 {
205         bool state = m_view.ShowingObjectBounds();
206         m_view.ShowObjectBounds(!state);
207         UpdateAll();
208 }
209
210 void ControlPanel::ToggleEnableShading()
211 {
212         bool state = m_view.PerformingShading();
213         m_view.PerformShading(!state);
214         UpdateAll();
215 }
216
217 void ControlPanel::SetGPURendering()
218 {
219         m_view.SetGPURendering(true);
220         UpdateAll();
221 }
222
223 void ControlPanel::SetCPURendering()
224 {
225         m_view.SetGPURendering(false);
226         UpdateAll();
227 }
228
229 void ControlPanel::ToggleScreenDebugFont()
230 {
231         bool state = m_screen.DebugFontShown();
232         m_screen.ShowDebugFont(!state);
233         UpdateAll();
234         
235 }
236
237 void ControlPanel::SetViewBounds()
238 {
239         bool ok;
240         Real xx = QInputDialog::getDouble(this, "View X Coordinate", "Enter X coordinate:", 0, -2e-30, 2e30,30,&ok);
241         
242         Real yy = QInputDialog::getDouble(this, "View Y Coordinate", "Enter Y coordinate:", 0, -2e-30, 2e30,30,&ok);
243         
244         Real w = QInputDialog::getDouble(this, "View Width", "Enter Width:", 1, -2e-30, 2e30,30,&ok);
245         
246         Real h = QInputDialog::getDouble(this, "View Height", "Enter Height:", 1, -2e-30, 2e30,30,&ok);
247         m_view.SetBounds(Rect(xx,yy,w,h));
248         
249 }
250
251 void ControlPanel::InsertTextIntoDocument()
252 {
253         const Rect & bounds = m_view.GetBounds();
254         Real xx = bounds.x;
255         Real yy = bounds.y + bounds.h/Real(2);
256         
257         string msg = m_text_edit->toPlainText().toStdString();
258         Real scale = bounds.h / Real(2);
259         Debug("Insert \"%s\" at %f, %f, scale %f", msg.c_str(), Float(xx), Float(yy), Float(scale));
260         m_doc.AddText(msg, scale, xx, yy);
261         m_view.ForceRenderDirty();
262         m_view.ForceBufferDirty();
263         m_view.ForceBoundsDirty();
264 }
265 void ControlPanel::InsertSVGIntoDocument()
266 {
267         Rect bounds(m_view.GetBounds());
268         bounds.w /= Real(m_screen.ViewportWidth());
269         bounds.h /= Real(m_screen.ViewportHeight());
270         
271         m_doc.ParseSVG(m_text_edit->toPlainText().toStdString(), bounds);
272         m_view.ForceRenderDirty();
273         m_view.ForceBufferDirty();
274         m_view.ForceBoundsDirty();
275 }
276
277 void ControlPanel::LoadSVGIntoDocument()
278 {
279
280         QString filename = QFileDialog::getOpenFileName(this, "Open SVG", "svg-tests", "Image Files (*.svg)");
281         if (filename == "")
282                 return;
283         
284         Rect bounds(m_view.GetBounds());
285         bounds.w /= Real(m_screen.ViewportWidth());
286         bounds.h /= Real(m_screen.ViewportHeight());
287         
288         m_doc.LoadSVG(filename.toStdString(), bounds);
289         m_view.ForceRenderDirty();
290         m_view.ForceBufferDirty();
291         m_view.ForceBoundsDirty();
292 }
293
294 void ControlPanel::SetDocumentFont()
295 {
296         QString filename = QFileDialog::getOpenFileName(this, "Set Font", "fonts", "True Type Fonts (*.ttf)");
297         if (filename != "")
298                 m_doc.SetFont(filename.toStdString());
299 }
300
301 ControlPanel * ControlPanel::g_panel = NULL;
302
303 int ControlPanel::Run(void * args)
304 {
305         ControlPanel::RunArgs * a = (ControlPanel::RunArgs*)args;
306         QApplication app(a->argc, a->argv);
307         g_panel = new ControlPanel(*a);
308         g_panel->show();
309         int result = app.exec();
310         a->screen.RequestQuit();
311         delete g_panel;
312         return result;
313 }
314         
315         
316 }
317
318 #endif //CONTROLPANEL_ENABLED
319

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