gwenhywfar  5.10.1
qt5_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  (C) 2016 by Christian David
5  email : martin@libchipcard.de
6  christian-david@web.de
7 
8  ***************************************************************************
9  * Please see toplevel file COPYING for license details *
10  ***************************************************************************/
11 
12 
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16 
17 #include "qt5_gui.hpp"
18 #include "qt5_gui_dialog.hpp"
19 
20 #include <gwenhywfar/debug.h>
21 
22 #include <QMessageBox>
23 #include <QApplication>
24 #include <QFileDialog>
25 
26 #include <assert.h>
27 
28 
30  :CppGui()
31  ,_parentWidget(NULL) {
32 
35  GWEN_Gui_SetName(_gui, "qt5-gui");
36 }
37 
38 
39 
41 }
42 
43 
44 
45 void QT5_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 QT5_Gui::extractHtml(const char *text) {
65  QString str = QString::fromUtf8(text);
66  const int start = str.indexOf(QStringLiteral("<html>"), 0, Qt::CaseInsensitive);
67  if (start != -1) {
68  const int end = str.indexOf(QStringLiteral("</html>"), start, Qt::CaseInsensitive);
69  if (end != -1) {
70  return str.mid(start, end+7);
71  }
72  }
73  return str;
74 }
75 
76 
77 
78 int QT5_Gui::execDialog(GWEN_DIALOG *dlg, GWEN_UNUSED uint32_t guiid) {
79  QT5_GuiDialog qt5Dlg(this, dlg);
80  QWidget *owner = qApp->activeWindow();
81 
82  /* setup widget tree for the dialog */
83  if (!(qt5Dlg.setup(owner))) {
84  return GWEN_ERROR_GENERIC;
85  }
86 
87  return qt5Dlg.execute();
88 }
89 
90 
91 
92 int QT5_Gui::openDialog(GWEN_DIALOG *dlg, GWEN_UNUSED uint32_t guiid) {
93  QT5_GuiDialog *qtDlg = new QT5_GuiDialog(this, dlg);
94  QWidget *owner = qApp->activeWindow();
95 
96  /* setup widget tree for the dialog */
97  if (!(qtDlg->setup(owner))) {
98  delete qtDlg;
99  return GWEN_ERROR_GENERIC;
100  }
101 
102  return qtDlg->openDialog();
103 }
104 
105 
106 
109  assert(qtDlg);
110 
111  int rv = qtDlg->closeDialog();
112  delete qtDlg;
113  return rv;
114 }
115 
116 
117 
118 int QT5_Gui::runDialog(GWEN_DIALOG *dlg, int untilEnd) {
120  assert(qtDlg);
121 
122  return qtDlg->runDialog((untilEnd==0)?false:true);
123 }
124 
125 
126 
127 int QT5_Gui::getFileName(const char *caption,
129  GWEN_UNUSED uint32_t flags,
130  const char *patterns,
131  GWEN_BUFFER *pathBuffer,
132  GWEN_UNUSED uint32_t guiid) {
133  QString sCaption;
134  QString sPatterns;
135  QString sPath;
136  QString str;
137  QWidget *owner = qApp->activeWindow();
138 
139  if (caption)
140  sCaption=QString::fromUtf8(caption);
141 
142  if (patterns) {
143  const char *s1;
144  const char *s2;
145 
146  s1=patterns;
147  qDebug("Patterns example: '%s'", patterns);
149  while(s1 && *s1) {
150  s2=strchr(s1, '\t');
151  if (s2) {
152  str=QString::fromUtf8(s1, s2-s1);
153  /* skip tab */
154  s2++;
155  }
156  else {
157  str=QString::fromUtf8(s1);
158  s2=NULL;
159  }
160  str.replace(',', ' ');
161  str.replace(';', ' ');
162 
163  if (!str.isEmpty())
164  sPatterns+=";;";
165  sPatterns+=str;
166 
167  s1=s2;
168  }
169  }
170 
171  if (GWEN_Buffer_GetUsedBytes(pathBuffer))
172  sPath=QString::fromUtf8(GWEN_Buffer_GetStart(pathBuffer));
173 
174  switch(fnt) {
176  str=QFileDialog::getOpenFileName(owner, sCaption, sPath, sPatterns);
177  break;
178 
180  str=QFileDialog::getSaveFileName(owner, sCaption, sPath, sPatterns);
181  break;
182 
184  str=QFileDialog::getExistingDirectory(owner, sCaption, sPath);
185  break;
186  }
187 
188  if (str.isEmpty()) {
189  DBG_ERROR(GWEN_LOGDOMAIN, "Empty filename returned.");
190  return GWEN_ERROR_ABORTED;
191  }
192  else {
193  GWEN_Buffer_Reset(pathBuffer);
194  GWEN_Buffer_AppendString(pathBuffer, str.toUtf8());
195  return 0;
196  }
197 }
198 
199 
200 
201 
202 
203 
204 
205 
206 
virtual int closeDialog(GWEN_DIALOG *dlg)
Definition: qt5_gui.cpp:107
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
static QT5_GuiDialog * getDialog(GWEN_DIALOG *dlg)
void popParentWidget()
Definition: qt5_gui.cpp:53
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:277
int runDialog(bool untilEnd)
#define NULL
Definition: binreloc.c:300
virtual int execDialog(GWEN_DIALOG *dlg, uint32_t guiid)
Definition: qt5_gui.cpp:78
void GWEN_Gui_AddFlags(GWEN_GUI *gui, uint32_t fl)
Definition: gui.c:211
void pushParentWidget(QWidget *w)
Definition: qt5_gui.cpp:45
static QString extractHtml(const char *text)
Definition: qt5_gui.cpp:64
#define GWEN_LOGDOMAIN
Definition: logger.h:35
struct GWEN_DIALOG GWEN_DIALOG
Definition: dialog.h:54
void GWEN_Buffer_Reset(GWEN_BUFFER *bf)
Definition: buffer.c:650
GWENHYWFAR_API void GWEN_Gui_UseDialogs(GWEN_GUI *gui)
Definition: gui_dialogs.c:29
GWEN_GUI_FILENAME_TYPE
Definition: gui.h:949
bool setup(QWidget *parentWindow)
#define GWEN_ERROR_ABORTED
Definition: error.h:63
A C++ binding for the C module GWEN_GUI.
Definition: cppgui.hpp:39
QT5_Gui()
Definition: qt5_gui.cpp:29
#define GWEN_ERROR_GENERIC
Definition: error.h:62
#define GWEN_GUI_FLAGS_DIALOGSUPPORTED
Definition: gui.h:1001
virtual ~QT5_Gui()
Definition: qt5_gui.cpp:40
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
virtual int getFileName(const char *caption, GWEN_GUI_FILENAME_TYPE fnt, uint32_t flags, const char *patterns, GWEN_BUFFER *pathBuffer, uint32_t guiid)
Definition: qt5_gui.cpp:127
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
virtual int openDialog(GWEN_DIALOG *dlg, uint32_t guiid)
Definition: qt5_gui.cpp:92
void GWEN_Gui_SetName(GWEN_GUI *gui, const char *name)
Definition: gui.c:227
virtual int runDialog(GWEN_DIALOG *dlg, int untilEnd)
Definition: qt5_gui.cpp:118
GWEN_GUI * _gui
Definition: cppgui.hpp:67
#define GWEN_UNUSED
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989