1 /***************************************************************************
2     begin       : Mon Mar 01 2004
3     copyright   : (C) 2004-2010 by Martin Preuss
4     email       : martin@libchipcard.de
5 
6  ***************************************************************************
7  *          Please see toplevel file COPYING for license details           *
8  ***************************************************************************/
9 
10 
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14 
15 #include "qt4_gui.hpp"
16 #include "qt4_gui_dialog.hpp"
17 
18 #include <gwenhywfar/debug.h>
19 
20 #include <QMessageBox>
21 #include <QApplication>
22 #include <QFileDialog>
23 
24 #include <assert.h>
25 
26 
27 
28 
QT4_Gui()29 QT4_Gui::QT4_Gui()
30   :CppGui()
31   ,_parentWidget(NULL) {
32 
33   GWEN_Gui_AddFlags(_gui, GWEN_GUI_FLAGS_DIALOGSUPPORTED);
34   GWEN_Gui_UseDialogs(_gui);
35   GWEN_Gui_SetName(_gui, "qt4-gui");
36 }
37 
38 
39 
~QT4_Gui()40 QT4_Gui::~QT4_Gui() {
41 }
42 
43 
44 
pushParentWidget(QWidget * w)45 void QT4_Gui::pushParentWidget(QWidget *w) {
46   if (_parentWidget)
47     _pushedParents.push_back(_parentWidget);
48   _parentWidget=w;
49 }
50 
51 
52 
popParentWidget()53 void QT4_Gui::popParentWidget() {
54   if (!_pushedParents.empty()) {
55     _parentWidget=_pushedParents.back();
56     _pushedParents.pop_back();
57   }
58   else
59     _parentWidget=NULL;
60 }
61 
62 
63 
extractHtml(const char * text)64 QString QT4_Gui::extractHtml(const char *text) {
65   const char *p=0;
66   const char *p2=0;
67 
68   if (text==NULL)
69     return QString("");
70 
71   /* find begin of HTML area */
72   p=text;
73   while ((p=strchr(p, '<'))) {
74     const char *t;
75 
76     t=p;
77     t++;
78     if (toupper(*t)=='H') {
79       t++;
80       if (toupper(*t)=='T') {
81         t++;
82         if (toupper(*t)=='M') {
83           t++;
84           if (toupper(*t)=='L') {
85             t++;
86             if (toupper(*t)=='>') {
87               break;
88             }
89           }
90         }
91       }
92     }
93     p++;
94   } /* while */
95 
96   /* find end of HTML area */
97   if (p) {
98     p+=6; /* skip "<html>" */
99     p2=p;
100     while ((p2=strchr(p2, '<'))) {
101       const char *t;
102 
103       t=p2;
104       t++;
105       if (toupper(*t)=='/') {
106         t++;
107         if (toupper(*t)=='H') {
108           t++;
109           if (toupper(*t)=='T') {
110             t++;
111             if (toupper(*t)=='M') {
112               t++;
113               if (toupper(*t)=='L') {
114                 t++;
115                 if (toupper(*t)=='>') {
116                   break;
117                 }
118               }
119             }
120           }
121         }
122       }
123       p2++;
124     } /* while */
125   }
126 
127   if (p && p2)
128     return QLatin1String("<qt>")+QString::fromUtf8(p, p2-p)+QLatin1String("</qt>");
129 
130   return QString::fromUtf8(text);
131 }
132 
133 
134 
execDialog(GWEN_DIALOG * dlg,uint32_t guiid)135 int QT4_Gui::execDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
136   QT4_GuiDialog qt4Dlg(this, dlg);
137   QWidget *owner=qApp->activeWindow();
138 
139   /* setup widget tree for the dialog */
140   if (!(qt4Dlg.setup(owner))) {
141     return GWEN_ERROR_GENERIC;
142   }
143 
144   return qt4Dlg.execute();
145 }
146 
147 
148 
openDialog(GWEN_DIALOG * dlg,uint32_t guiid)149 int QT4_Gui::openDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
150   QT4_GuiDialog *qt4Dlg;
151   QWidget *owner=qApp->activeWindow();
152 
153   qt4Dlg=new QT4_GuiDialog(this, dlg);
154 
155   /* setup widget tree for the dialog */
156   if (!(qt4Dlg->setup(owner))) {
157     delete qt4Dlg;
158     return GWEN_ERROR_GENERIC;
159   }
160 
161   return qt4Dlg->openDialog();
162 }
163 
164 
165 
closeDialog(GWEN_DIALOG * dlg)166 int QT4_Gui::closeDialog(GWEN_DIALOG *dlg) {
167   QT4_GuiDialog *qt4Dlg;
168   int rv;
169 
170   qt4Dlg=QT4_GuiDialog::getDialog(dlg);
171   assert(qt4Dlg);
172 
173   rv=qt4Dlg->closeDialog();
174   delete qt4Dlg;
175   return rv;
176 }
177 
178 
179 
runDialog(GWEN_DIALOG * dlg,int untilEnd)180 int QT4_Gui::runDialog(GWEN_DIALOG *dlg, int untilEnd) {
181   QT4_GuiDialog *qt4Dlg;
182 
183   qt4Dlg=QT4_GuiDialog::getDialog(dlg);
184   assert(qt4Dlg);
185 
186   return qt4Dlg->runDialog((untilEnd==0)?false:true);
187 }
188 
189 
190 
getFileName(const char * caption,GWEN_GUI_FILENAME_TYPE fnt,uint32_t flags,const char * patterns,GWEN_BUFFER * pathBuffer,uint32_t guiid)191 int QT4_Gui::getFileName(const char *caption,
192                          GWEN_GUI_FILENAME_TYPE fnt,
193                          uint32_t flags,
194                          const char *patterns,
195                          GWEN_BUFFER *pathBuffer,
196                          uint32_t guiid) {
197   QString sCaption;
198   QString sPatterns;
199   QString sPath;
200   QString str;
201   QWidget *owner=qApp->activeWindow();
202 
203   if (caption)
204     sCaption=QString::fromUtf8(caption);
205 
206   if (patterns) {
207     const char *s1;
208     const char *s2;
209 
210     s1=patterns;
211     while(s1 && *s1) {
212       s2=strchr(s1, '\t');
213       if (s2) {
214         str=QString::fromUtf8(s1, s2-s1);
215         str.replace(',', ' ');
216         str.replace(';', ' ');
217         /* skip tab */
218         s2++;
219       }
220       else {
221         str=QString::fromUtf8(s1);
222         str.replace(',', ' ');
223         str.replace(';', ' ');
224         s2=NULL;
225       }
226 
227       if (!str.isEmpty())
228         sPatterns+=";;";
229       sPatterns+=str;
230 
231       s1=s2;
232     }
233   }
234 
235   if (GWEN_Buffer_GetUsedBytes(pathBuffer))
236     sPath=QString::fromUtf8(GWEN_Buffer_GetStart(pathBuffer));
237 
238   switch(fnt) {
239   case GWEN_Gui_FileNameType_OpenFileName:
240     str=QFileDialog::getOpenFileName(owner, sCaption, sPath, sPatterns);
241     break;
242 
243   case GWEN_Gui_FileNameType_SaveFileName:
244     str=QFileDialog::getSaveFileName(owner, sCaption, sPath, sPatterns);
245     break;
246 
247   case GWEN_Gui_FileNameType_OpenDirectory:
248     str=QFileDialog::getExistingDirectory(owner, sCaption, sPath);
249     break;
250   }
251 
252   if (str.isEmpty()) {
253     DBG_ERROR(GWEN_LOGDOMAIN, "Empty filename returned.");
254     return GWEN_ERROR_ABORTED;
255   }
256   else {
257     GWEN_Buffer_Reset(pathBuffer);
258     GWEN_Buffer_AppendString(pathBuffer, str.toUtf8());
259     return 0;
260   }
261 }
262 
263 
264 
265 
266 
267 
268 
269 
270 
271