1 /***************************************************************************
2 begin : Mon Feb 15 2010
3 copyright : (C) 2010 by Martin Preuss
4 email : martin@libchipcard.de
5
6 ***************************************************************************
7 * Please see toplevel file COPYING for license details *
8 ***************************************************************************/
9
10 #include "qt5_gui_dialog.hpp"
11 #include "qt5dialogbox.hpp"
12
13 #include <gwenhywfar/dialog_be.h>
14 #include <gwenhywfar/widget_be.h>
15 #include <gwenhywfar/debug.h>
16
17 #include <QApplication>
18 #include <QLabel>
19 #include <QPushButton>
20 #include <QLineEdit>
21 #include <QTextEdit>
22 #include <QTextBrowser>
23 #include <QComboBox>
24 #include <QGroupBox>
25 #include <QProgressBar>
26 #include <QHBoxLayout>
27 #include <QVBoxLayout>
28 #include <QGridLayout>
29 #include <QFrame>
30 #include <QTreeWidget>
31 #include <QTabWidget>
32 #include <QCheckBox>
33 #include <QStackedWidget>
34 #include <QScrollArea>
35 #include <QSpinBox>
36 #include <QRadioButton>
37 #include <QHeaderView>
38
39 #include <QDebug>
40
41 #include <list>
42 #include <string>
43
44
45 #define QT5_DIALOG_STRING_TITLE 0
46 #define QT5_DIALOG_STRING_VALUE 1
47
48
49 #include "w_widget.cpp"
50 #include "w_dialog.cpp"
51 #include "w_vlayout.cpp"
52 #include "w_hlayout.cpp"
53 #include "w_gridlayout.cpp"
54 #include "w_label.cpp"
55 #include "w_lineedit.cpp"
56 #include "w_pushbutton.cpp"
57 #include "w_hline.cpp"
58 #include "w_vline.cpp"
59 #include "w_textedit.cpp"
60 #include "w_combobox.cpp"
61 #include "w_tabbook.cpp"
62 #include "w_checkbox.cpp"
63 #include "w_groupbox.cpp"
64 #include "w_widgetstack.cpp"
65 #include "w_textbrowser.cpp"
66 #include "w_scrollarea.cpp"
67 #include "w_progressbar.cpp"
68 #include "w_listbox.cpp"
69 #include "w_radiobutton.cpp"
70 #include "w_spinbox.cpp"
71
72
73
QT5_GuiDialog(QT5_Gui * gui,GWEN_DIALOG * dlg)74 QT5_GuiDialog::QT5_GuiDialog(QT5_Gui *gui, GWEN_DIALOG *dlg)
75 :CppDialog(dlg)
76 ,_gui(gui)
77 ,_mainWidget(NULL) {
78
79 }
80
81
82
~QT5_GuiDialog()83 QT5_GuiDialog::~QT5_GuiDialog() {
84 if (_mainWidget)
85 _mainWidget->unlinkFromDialog();
86
87 }
88
89
90
getDialog(GWEN_DIALOG * dlg)91 QT5_GuiDialog *QT5_GuiDialog::getDialog(GWEN_DIALOG *dlg) {
92 CppDialog *cppDlg;
93
94 cppDlg=CppDialog::getDialog(dlg);
95 if (cppDlg)
96 return dynamic_cast<QT5_GuiDialog*>(cppDlg);
97 return NULL;
98 }
99
100
101
execute()102 int QT5_GuiDialog::execute() {
103 QT5_DialogBox *dialogBox;
104 int rv;
105
106 dialogBox=dynamic_cast<QT5_DialogBox*>(getMainWindow());
107 if (dialogBox==NULL) {
108 DBG_INFO(0, "Dialog's main widget is not derived from class FXDialogBox");
109 return GWEN_ERROR_GENERIC;
110 }
111
112 /* execute dialog */
113 rv=dialogBox->exec();
114 GWEN_Dialog_EmitSignalToAll(_dialog, GWEN_DialogEvent_TypeFini, "");
115
116 if (rv==QT5_DialogBox::Accepted) {
117 /* accepted */
118 return 1;
119 }
120 else
121 return 0;
122 }
123
124
125
openDialog()126 int QT5_GuiDialog::openDialog() {
127 QT5_DialogBox *dialogBox;
128
129 dialogBox=dynamic_cast<QT5_DialogBox*>(getMainWindow());
130 if (dialogBox==NULL) {
131 DBG_INFO(0, "Dialog's main widget is not derived from class FXDialogBox");
132 return GWEN_ERROR_GENERIC;
133 }
134
135 /* show dialog */
136 dialogBox->show();
137 /* gui update */
138 qApp->processEvents();
139
140 return 0;
141 }
142
143
144
closeDialog()145 int QT5_GuiDialog::closeDialog() {
146 QT5_DialogBox *dialogBox;
147
148 dialogBox=dynamic_cast<QT5_DialogBox*>(getMainWindow());
149 if (dialogBox==NULL) {
150 DBG_INFO(0, "Dialog's main widget is not derived from class FXDialogBox");
151 return GWEN_ERROR_GENERIC;
152 }
153
154 /* let dialog write its settings */
155 GWEN_Dialog_EmitSignalToAll(_dialog, GWEN_DialogEvent_TypeFini, "");
156
157 /* hide dialog */
158 dialogBox->hide();
159 /* gui update */
160 qApp->processEvents();
161
162 delete _mainWidget;
163 _mainWidget=NULL;
164
165 return 0;
166 }
167
168
169
runDialog(bool untilEnd)170 int QT5_GuiDialog::runDialog(bool untilEnd) {
171 QT5_DialogBox *dialogBox;
172
173 dialogBox=dynamic_cast<QT5_DialogBox*>(getMainWindow());
174 if (dialogBox==NULL) {
175 DBG_INFO(0, "Dialog's main widget is not derived from class FXDialogBox");
176 return GWEN_ERROR_GENERIC;
177 }
178
179 if (untilEnd) {
180 dialogBox->cont();
181 }
182 else {
183 {
184 /* gui update */
185 qApp->processEvents();
186 }
187 }
188
189 return 0;
190 }
191
192
193
setIntProperty(GWEN_WIDGET * w,GWEN_DIALOG_PROPERTY prop,int index,int value,int doSignal)194 int QT5_GuiDialog::setIntProperty(GWEN_WIDGET *w,
195 GWEN_DIALOG_PROPERTY prop,
196 int index,
197 int value,
198 int doSignal) {
199 return GWEN_Widget_SetIntProperty(w, prop, index, value, doSignal);
200 }
201
202
203
getIntProperty(GWEN_WIDGET * w,GWEN_DIALOG_PROPERTY prop,int index,int defaultValue)204 int QT5_GuiDialog::getIntProperty(GWEN_WIDGET *w,
205 GWEN_DIALOG_PROPERTY prop,
206 int index,
207 int defaultValue) {
208 return GWEN_Widget_GetIntProperty(w, prop, index, defaultValue);
209 }
210
211
212
setCharProperty(GWEN_WIDGET * w,GWEN_DIALOG_PROPERTY prop,int index,const char * value,int doSignal)213 int QT5_GuiDialog::setCharProperty(GWEN_WIDGET *w,
214 GWEN_DIALOG_PROPERTY prop,
215 int index,
216 const char *value,
217 int doSignal) {
218 return GWEN_Widget_SetCharProperty(w, prop, index, value, doSignal);
219 }
220
221
222
getCharProperty(GWEN_WIDGET * w,GWEN_DIALOG_PROPERTY prop,int index,const char * defaultValue)223 const char *QT5_GuiDialog::getCharProperty(GWEN_WIDGET *w,
224 GWEN_DIALOG_PROPERTY prop,
225 int index,
226 const char *defaultValue) {
227 return GWEN_Widget_GetCharProperty(w, prop, index, defaultValue);
228 }
229
230
231
setupTree(GWEN_WIDGET * w)232 int QT5_GuiDialog::setupTree(GWEN_WIDGET *w) {
233 int rv;
234 Qt5_W_Widget *xw=NULL;
235
236 switch(GWEN_Widget_GetType(w)) {
237 case GWEN_Widget_TypeDialog:
238 xw=new Qt5_W_Dialog(w);
239 break;
240 case GWEN_Widget_TypeVLayout:
241 xw=new Qt5_W_VLayout(w);
242 break;
243 case GWEN_Widget_TypeHLayout:
244 xw=new Qt5_W_HLayout(w);
245 break;
246 case GWEN_Widget_TypeGridLayout:
247 xw=new Qt5_W_GridLayout(w);
248 break;
249 case GWEN_Widget_TypeLabel:
250 xw=new Qt5_W_Label(w);
251 break;
252 case GWEN_Widget_TypeLineEdit:
253 xw=new Qt5_W_LineEdit(w);
254 break;
255 case GWEN_Widget_TypeVSpacer:
256 case GWEN_Widget_TypeHSpacer:
257 /* abuse widget */
258 xw=new Qt5_W_Widget(w);
259 break;
260 case GWEN_Widget_TypePushButton:
261 xw=new Qt5_W_PushButton(w);
262 break;
263 case GWEN_Widget_TypeHLine:
264 xw=new Qt5_W_HLine(w);
265 break;
266 case GWEN_Widget_TypeVLine:
267 xw=new Qt5_W_VLine(w);
268 break;
269 case GWEN_Widget_TypeTextEdit:
270 xw=new Qt5_W_TextEdit(w);
271 break;
272 case GWEN_Widget_TypeComboBox:
273 xw=new Qt5_W_ComboBox(w);
274 break;
275 case GWEN_Widget_TypeTabBook:
276 xw=new Qt5_W_TabBook(w);
277 break;
278 case GWEN_Widget_TypeTabPage:
279 xw=new Qt5_W_VLayout(w);
280 break;
281 case GWEN_Widget_TypeCheckBox:
282 xw=new Qt5_W_CheckBox(w);
283 break;
284 case GWEN_Widget_TypeGroupBox:
285 xw=new Qt5_W_GroupBox(w);
286 break;
287 case GWEN_Widget_TypeWidgetStack:
288 xw=new Qt5_W_WidgetStack(w);
289 break;
290 case GWEN_Widget_TypeTextBrowser:
291 xw=new Qt5_W_TextBrowser(w);
292 break;
293 case GWEN_Widget_TypeScrollArea:
294 xw=new Qt5_W_ScrollArea(w);
295 break;
296 case GWEN_Widget_TypeProgressBar:
297 xw=new Qt5_W_ProgressBar(w);
298 break;
299 case GWEN_Widget_TypeListBox:
300 xw=new Qt5_W_ListBox(w);
301 break;
302 case GWEN_Widget_TypeRadioButton:
303 xw=new Qt5_W_RadioButton(w);
304 break;
305 case GWEN_Widget_TypeSpinBox:
306 xw=new Qt5_W_SpinBox(w);
307 break;
308 default:
309 DBG_ERROR(GWEN_LOGDOMAIN, "Unhandled widget type %d (%s)",
310 GWEN_Widget_GetType(w), GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
311 break;
312 }
313
314 if (xw==NULL) {
315 DBG_ERROR(GWEN_LOGDOMAIN, "No widget created.");
316 return GWEN_ERROR_INTERNAL;
317 }
318
319 rv=xw->setup();
320 if (rv<0) {
321 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
322 return rv;
323 }
324 else {
325 GWEN_WIDGET *wChild;
326
327 if (GWEN_Widget_GetType(w)==GWEN_Widget_TypeDialog)
328 _mainWidget=(QT5_DialogBox*) GWEN_Widget_GetImplData(xw->getCInterface(), QT5_DIALOG_WIDGET_REAL);
329
330 /* handle children */
331 wChild=GWEN_Widget_Tree_GetFirstChild(w);
332 while(wChild) {
333 /* recursion */
334 rv=setupTree(wChild);
335 if (rv<0) {
336 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
337 return rv;
338 }
339 wChild=GWEN_Widget_Tree_GetNext(wChild);
340 }
341 }
342
343 return 0;
344 }
345
346
setup(GWEN_UNUSED QWidget * parentWindow)347 bool QT5_GuiDialog::setup(GWEN_UNUSED QWidget *parentWindow) {
348 GWEN_WIDGET_TREE *wtree;
349 GWEN_WIDGET *w;
350 int rv;
351
352 wtree=GWEN_Dialog_GetWidgets(_dialog);
353 if (wtree==NULL) {
354 DBG_ERROR(GWEN_LOGDOMAIN, "No widget tree in dialog");
355 return false;
356 }
357 w=GWEN_Widget_Tree_GetFirst(wtree);
358 if (w==NULL) {
359 DBG_ERROR(GWEN_LOGDOMAIN, "No widgets in dialog");
360 return false;
361 }
362
363 rv=setupTree(w);
364 if (rv<0) {
365 DBG_ERROR(GWEN_LOGDOMAIN, "here (%d)", rv);
366 return false;
367 }
368
369 _mainWidget=(QT5_DialogBox*) GWEN_Widget_GetImplData(w, QT5_DIALOG_WIDGET_REAL);
370
371 rv=GWEN_Dialog_EmitSignalToAll(_dialog, GWEN_DialogEvent_TypeInit, "");
372 if (rv<0) {
373 DBG_INFO(0, "Error initializing dialog: %d", rv);
374 return false;
375 }
376
377
378 return true;
379 }
380
381
382
383
384
385