gwenhywfar  5.10.1
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 
35  GWEN_Gui_SetName(_gui, "qt4-gui");
36 }
37 
38 
39 
41 }
42 
43 
44 
45 void QT4_Gui::pushParentWidget(QWidget *w) {
46  if (_parentWidget)
47  _pushedParents.push_back(_parentWidget);
48  _parentWidget=w;
49 }
50 
51 
52 
54  if (!_pushedParents.empty()) {
55  _parentWidget=_pushedParents.back();
56  _pushedParents.pop_back();
57  }
58  else
59  _parentWidget=NULL;
60 }
61 
62 
63 
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 
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 
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 
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 
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 
191 int QT4_Gui::getFileName(const char *caption,
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) {
240  str=QFileDialog::getOpenFileName(owner, sCaption, sPath, sPatterns);
241  break;
242 
244  str=QFileDialog::getSaveFileName(owner, sCaption, sPath, sPatterns);
245  break;
246 
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 
void popParentWidget()
Definition: qt4_gui.cpp:53
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:277
virtual int closeDialog(GWEN_DIALOG *dlg)
Definition: qt4_gui.cpp:166
static QT4_GuiDialog * getDialog(GWEN_DIALOG *dlg)
#define NULL
Definition: binreloc.c:300
void GWEN_Gui_AddFlags(GWEN_GUI *gui, uint32_t fl)
Definition: gui.c:211
#define GWEN_LOGDOMAIN
Definition: logger.h:35
int runDialog(bool untilEnd)
struct GWEN_DIALOG GWEN_DIALOG
Definition: dialog.h:54
virtual int execDialog(GWEN_DIALOG *dlg, uint32_t guiid)
Definition: qt4_gui.cpp:135
QT4_Gui()
Definition: qt4_gui.cpp:29
void GWEN_Buffer_Reset(GWEN_BUFFER *bf)
Definition: buffer.c:650
void pushParentWidget(QWidget *w)
Definition: qt4_gui.cpp:45
static QString extractHtml(const char *text)
Definition: qt4_gui.cpp:64
GWENHYWFAR_API void GWEN_Gui_UseDialogs(GWEN_GUI *gui)
Definition: gui_dialogs.c:29
GWEN_GUI_FILENAME_TYPE
Definition: gui.h:949
#define GWEN_ERROR_ABORTED
Definition: error.h:63
A C++ binding for the C module GWEN_GUI.
Definition: cppgui.hpp:39
#define GWEN_ERROR_GENERIC
Definition: error.h:62
#define GWEN_GUI_FLAGS_DIALOGSUPPORTED
Definition: gui.h:1001
virtual int getFileName(const char *caption, GWEN_GUI_FILENAME_TYPE fnt, uint32_t flags, const char *patterns, GWEN_BUFFER *pathBuffer, uint32_t guiid)
Definition: qt4_gui.cpp:191
bool setup(QWidget *parentWindow)
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
virtual int openDialog(GWEN_DIALOG *dlg, uint32_t guiid)
Definition: qt4_gui.cpp:149
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
virtual ~QT4_Gui()
Definition: qt4_gui.cpp:40
virtual int runDialog(GWEN_DIALOG *dlg, int untilEnd)
Definition: qt4_gui.cpp:180
void GWEN_Gui_SetName(GWEN_GUI *gui, const char *name)
Definition: gui.c:227
GWEN_GUI * _gui
Definition: cppgui.hpp:67
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989