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