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

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