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 
QT5_Gui()29 QT5_Gui::QT5_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, "qt5-gui");
36 }
37 
38 
39 
~QT5_Gui()40 QT5_Gui::~QT5_Gui() {
41 }
42 
43 
44 
pushParentWidget(QWidget * w)45 void QT5_Gui::pushParentWidget(QWidget *w) {
46   if (_parentWidget)
47     _pushedParents.push_back(_parentWidget);
48   _parentWidget=w;
49 }
50 
51 
52 
popParentWidget()53 void QT5_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 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 
execDialog(GWEN_DIALOG * dlg,GWEN_UNUSED uint32_t guiid)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 
openDialog(GWEN_DIALOG * dlg,GWEN_UNUSED uint32_t guiid)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 
closeDialog(GWEN_DIALOG * dlg)107 int QT5_Gui::closeDialog(GWEN_DIALOG *dlg) {
108   QT5_GuiDialog *qtDlg = QT5_GuiDialog::getDialog(dlg);
109   assert(qtDlg);
110 
111   int rv = qtDlg->closeDialog();
112   delete qtDlg;
113   return rv;
114 }
115 
116 
117 
runDialog(GWEN_DIALOG * dlg,int untilEnd)118 int QT5_Gui::runDialog(GWEN_DIALOG *dlg, int untilEnd) {
119   QT5_GuiDialog *qtDlg = QT5_GuiDialog::getDialog(dlg);
120   assert(qtDlg);
121 
122   return qtDlg->runDialog((untilEnd==0)?false:true);
123 }
124 
125 
126 
getFileName(const char * caption,GWEN_GUI_FILENAME_TYPE fnt,GWEN_UNUSED uint32_t flags,const char * patterns,GWEN_BUFFER * pathBuffer,GWEN_UNUSED uint32_t guiid)127 int QT5_Gui::getFileName(const char *caption,
128                          GWEN_GUI_FILENAME_TYPE fnt,
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);
148     //! @todo Create pattern correctly
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) {
175   case GWEN_Gui_FileNameType_OpenFileName:
176     str=QFileDialog::getOpenFileName(owner, sCaption, sPath, sPatterns);
177     break;
178 
179   case GWEN_Gui_FileNameType_SaveFileName:
180     str=QFileDialog::getSaveFileName(owner, sCaption, sPath, sPatterns);
181     break;
182 
183   case GWEN_Gui_FileNameType_OpenDirectory:
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 
207