1 /***************************************************************************
2   DIA_dialogFactory.cpp
3   (C) 2007 Mean Fixounet@free.fr
4 ***************************************************************************/
5 
6 /***************************************************************************
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  ***************************************************************************/
14 
15 #include <QDialogButtonBox>
16 #include <QGridLayout>
17 #include <QSpacerItem>
18 #include <QTabWidget>
19 
20 #include "T_dialogFactory.h"
21 #include "ADM_default.h"
22 #include "DIA_factory.h"
23 #include "DIA_coreToolkit.h"
24 #include "DIA_coreUI_internal.h"
25 #include "ADM_toolkitQt.h"
26 #include "ADM_dialogFactoryQt4.h"
27 
28 static void insertTab(uint32_t index, diaElemTabs *tab, QTabWidget *wtab);
29 /**
30     \fn qt4DiaFactoryRun(const char *title,uint32_t nb,diaElem **elems)
31     \brief  Run a dialog made of nb elems, each elem being described in the **elems
32     @return 0 on failure, 1 on success
33 */
34 
35 
36 
37 /**
38     \fn qt4DiaFactoryRun(const char *title,uint32_t nb,diaElem **elems)
39     \brief  Run a dialog made of nb elems, each elem being described in the **elems
40     @return 0 on failure, 1 on success
41 */
42 class factoryCookie
43 {
44 public:
factoryCookie(const char * title)45         factoryCookie(const char *title)
46         {
47             dialog=new QDialog(qtLastRegisteredDialog());
48             dialog->setWindowTitle(QString::fromUtf8(title));
49             vboxlayout = new QVBoxLayout(dialog);
50             tabWidget=NULL;
51             layout=NULL;
52         }
~factoryCookie()53         virtual ~factoryCookie()
54         {
55             if(vboxlayout)
56                 delete vboxlayout;
57             if(dialog)
58                 delete dialog;
59             dialog=NULL;
60             vboxlayout=NULL;
61         }
62 public:
63         QDialog     *dialog;
64         QVBoxLayout *vboxlayout;
65         QLayout     *layout;
66         QTabWidget  *tabWidget;
67         std::vector <diaElem *>items;
68 };
69 /**
70  *
71  * @param title
72  * @param nb
73  * @param elems
74  * @return
75  */
qt4DiaFactoryPrepare(const char * title,uint32_t nb,diaElem ** elems)76 void * qt4DiaFactoryPrepare(const char *title,uint32_t nb,diaElem **elems)
77 {
78   factoryCookie *cookie=new factoryCookie(title);
79 
80   ADM_assert(title);
81   ADM_assert(nb);
82   ADM_assert(elems);
83 
84     int currentLayout = 0;
85     int  v=0;
86     for(int i=0;i<nb;i++)
87     {
88        ADM_assert(elems[i]);
89        if (elems[i]->getRequiredLayout() != currentLayout)
90        {
91            if (cookie->layout)
92                cookie->vboxlayout->addLayout(cookie->layout);
93 
94            switch (elems[i]->getRequiredLayout())
95            {
96                case FAC_QT_GRIDLAYOUT:
97                    cookie->layout = new QGridLayout();
98                    break;
99                case FAC_QT_VBOXLAYOUT:
100                    cookie->layout = new QVBoxLayout();
101                    break;
102            }
103 
104            currentLayout = elems[i]->getRequiredLayout();
105            v = 0;
106        }
107 
108        elems[i]->setMe( (void *)(cookie->dialog),cookie->layout,v);
109        v+=elems[i]->getSize();
110     }
111 
112     for(int i=0;i<nb;i++)
113     {
114         ADM_assert(elems[i]);
115         elems[i]->finalize();
116         cookie->items.push_back(elems[i]);
117     }
118     return cookie;
119 }
120 /**
121  *
122  * @param finish
123  * @return
124  */
qt4DiaFactoryFinish(void * finish)125 bool  qt4DiaFactoryFinish(void *finish)
126 {
127   bool  r=false;
128   factoryCookie *cookie=(factoryCookie *)  finish;
129   QSpacerItem *spacer = new QSpacerItem(20, 16, QSizePolicy::Minimum, QSizePolicy::Fixed);
130   QDialogButtonBox *buttonBox = new QDialogButtonBox();
131 
132   // Add buttons
133    buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
134 
135    QObject::connect(buttonBox, SIGNAL(accepted()), cookie->dialog, SLOT(accept()));
136    QObject::connect(buttonBox, SIGNAL(rejected()), cookie->dialog, SLOT(reject()));
137 
138    if (cookie->layout)
139            cookie->vboxlayout->addLayout(cookie->layout);
140 
141    cookie->vboxlayout->addItem(spacer);
142    cookie->vboxlayout->addWidget(buttonBox);
143 
144    cookie->dialog->setLayout(cookie->vboxlayout);
145 
146    qtRegisterDialog(cookie->dialog);
147 
148   if(cookie->dialog->exec()==QDialog::Accepted)
149   {
150      int nb=cookie->items.size();
151      for(int i=0;i<nb;i++)
152      {
153         ADM_assert(cookie->items[i]);
154         cookie->items[i]->getMe();
155       }
156      r=true;
157   }
158 
159   qtUnregisterDialog(cookie->dialog);
160 
161   delete cookie;
162   return r;
163 }
164 
165 /**
166     \fn shortkey(const char *in)
167     \brief translate gtk like accelerator (_) to QT4 like accelerator (&)
168 */
shortkey(const char * in)169 const char *shortkey(const char *in)
170 {
171         QString escaped = QString::fromUtf8(in);
172 
173         escaped.replace("&", "&&");
174         escaped.replace("_", "&");
175 
176         return ADM_strdup(escaped.toUtf8().constData());
177 }
178 
179 
180 /**
181  *         \fn qt4DiaFactoryRunTabs
182  */
qt4DiaFactoryTabsPrepare(const char * title,uint32_t nb,diaElemTabs ** tabs)183 void  *qt4DiaFactoryTabsPrepare(const char *title,uint32_t nb,diaElemTabs **tabs)
184 {
185     factoryCookie *cookie=new factoryCookie(title);
186 
187     ADM_assert(title);
188     ADM_assert(nb);
189     ADM_assert(tabs);
190 
191     cookie->layout  = new QGridLayout();
192     cookie->tabWidget = new QTabWidget();
193 
194 
195     for(int i=0;i<nb;i++)
196     {
197         ADM_assert(tabs[i]);
198         insertTab(i,tabs[i],cookie->tabWidget);
199          for(int j=0;j<tabs[i]->nbElems;j++)
200              cookie->items.push_back(tabs[i]->dias[j]);
201     }
202     return cookie;
203 }
204  /**
205   *
206   * @param f
207   * @return
208   */
qt4DiaFactoryTabsFinish(void * f)209 bool qt4DiaFactoryTabsFinish(void *f)
210 {
211     bool r=false;
212     factoryCookie *cookie=(factoryCookie *)f;
213 
214     QDialogButtonBox *buttonBox = new QDialogButtonBox();
215     QObject::connect(buttonBox, SIGNAL(accepted()), cookie->dialog, SLOT(accept()));
216     QObject::connect(buttonBox, SIGNAL(rejected()), cookie->dialog, SLOT(reject()));
217     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
218     QSpacerItem *spacer = new QSpacerItem(20, 16, QSizePolicy::Minimum, QSizePolicy::Fixed);
219     cookie->vboxlayout->addLayout(cookie->layout);
220     cookie->vboxlayout->addWidget(cookie->tabWidget,0,0);
221     cookie->vboxlayout->addItem(spacer);
222     cookie->vboxlayout->addWidget(buttonBox,1,0);
223 
224      cookie->dialog->setLayout(cookie->vboxlayout);
225 
226      // Expand to see all tabs but still allow the window to be resized smaller
227      cookie->tabWidget->setUsesScrollButtons(false);
228      cookie->dialog->adjustSize();
229      cookie->tabWidget->setUsesScrollButtons(true);
230      // Squeeze all the air vertically
231      cookie->dialog->resize(cookie->dialog->width(), cookie->dialog->minimumSizeHint().height());
232 
233     qtRegisterDialog(cookie->dialog);
234 
235     if(cookie->dialog->exec()==QDialog::Accepted)
236     {
237         // Read tabs
238         int n=cookie->items.size();
239         for(int i=0;i<n;i++)
240             cookie->items[i]->getMe();
241         r=true;
242     }
243     qtUnregisterDialog(cookie->dialog);
244     delete cookie;
245     return r;
246 }
insertTab(uint32_t index,diaElemTabs * tab,QTabWidget * wtab)247 void insertTab(uint32_t index, diaElemTabs *tab, QTabWidget *wtab)
248 {
249 
250   QWidget *wid=new QWidget;
251   QSpacerItem *spacerItem = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
252   int currentLayout = 0;
253   QVBoxLayout *vboxLayout = new QVBoxLayout(wid);
254   QLayout *layout = NULL;
255 
256   /* First compute the size of our window */
257   int vsize=0;
258   for(int i=0;i<tab->nbElems;i++)
259   {
260     ADM_assert(tab->dias[i]);
261      vsize+=tab->dias[i]->getSize();
262   }
263 
264     int  v=0;
265 
266     for(int i=0;i<tab->nbElems;i++)
267     {
268         ADM_assert(tab->dias[i]);
269 
270         if (tab->dias[i]->getRequiredLayout() != currentLayout)
271         {
272             if (layout)
273                 vboxLayout->addLayout(layout);
274 
275             switch (tab->dias[i]->getRequiredLayout())
276             {
277                 case FAC_QT_GRIDLAYOUT:
278                     layout = new QGridLayout();
279                     break;
280                 case FAC_QT_VBOXLAYOUT:
281                     layout = new QVBoxLayout();
282                     break;
283             }
284 
285             currentLayout = tab->dias[i]->getRequiredLayout();
286             v = 0;
287         }
288 
289         tab->dias[i]->setMe( wid,layout,v);
290         v+=tab->dias[i]->getSize();
291     }
292 
293   wtab->addTab(wid,QString::fromUtf8(tab->title));
294   for(int i=0;i<tab->nbElems;i++)
295   {
296     tab->dias[i]->finalize();
297   }
298 
299   if (layout)
300         vboxLayout->addLayout(layout);
301 
302   vboxLayout->addItem(spacerItem);
303 }
304 /**
305  *
306  * @param title
307  * @param nb
308  * @param elems
309  * @return
310  */
qt4DiaFactoryRun(const char * title,uint32_t nb,diaElem ** elems)311 uint8_t qt4DiaFactoryRun(const char *title,uint32_t nb,diaElem **elems)
312 {
313     void *cookie=qt4DiaFactoryPrepare(title,nb,elems);
314     return qt4DiaFactoryFinish(cookie);
315 }
316 
317 /**
318  *         \fn qt4DiaFactoryRunTabs
319  */
qt4DiaFactoryRunTabs(const char * title,uint32_t nb,diaElemTabs ** tabs)320 uint8_t qt4DiaFactoryRunTabs(const char *title,uint32_t nb,diaElemTabs **tabs)
321 {
322     void *cookie=qt4DiaFactoryTabsPrepare(title,nb,tabs);
323     return qt4DiaFactoryTabsFinish(cookie);
324 }
325 /**
326  *
327  */
328 /**
329  *  \fn gtkFactoryGetVersion
330  *         \brief returns the version this has been compiled with
331  */
qt4FactoryGetVersion(uint32_t * maj,uint32_t * minor,uint32_t * patch)332 void      qt4FactoryGetVersion(uint32_t *maj,uint32_t *minor,uint32_t *patch)
333 {
334         *maj=ADM_COREUI_MAJOR;
335         *minor=ADM_COREUI_MINOR;
336         *patch=ADM_COREUI_PATCH;
337 
338 }
339 extern CREATE_BITRATE_T     qt4CreateBitrate;
340 extern DELETE_DIA_ELEM_T    qt4DestroyBitrate;
341 extern CREATE_BAR_T         qt4CreateBar;
342 extern DELETE_DIA_ELEM_T    qt4DestroyBar;
343 extern CREATE_FLOAT_T       qt4CreateFloat;
344 extern DELETE_DIA_ELEM_T    qt4DestroyFloat;
345 extern CREATE_FRAME_T       qt4CreateFrame;
346 extern DELETE_DIA_ELEM_T    qt4DestroyFrame;
347 extern CREATE_HEX_T         qt4CreateHex;
348 extern DELETE_DIA_ELEM_T    qt4DestroyHex;
349 extern CREATE_INTEGER_T     qt4CreateInteger;
350 extern CREATE_UINTEGER_T    qt4CreateUInteger;
351 extern DELETE_DIA_ELEM_T    qt4DestroyInteger;
352 extern DELETE_DIA_ELEM_T    qt4DestroyUInteger;
353 extern CREATE_MATRIX_T      qt4CreateMatrix;
354 extern DELETE_DIA_ELEM_T    qt4DestroyMatrix;
355 extern CREATE_NOTCH_T       qt4CreateNotch;
356 extern DELETE_DIA_ELEM_T    qt4DestroyNotch;
357 extern CREATE_READONLYTEXT_T qt4CreateRoText;
358 extern CREATE_TEXT_T       qt4CreateText;
359 extern DELETE_DIA_ELEM_T   qt4DestroyRoText;
360 extern DELETE_DIA_ELEM_T   qt4DestroyText;
361 extern CREATE_BUTTON_T     qt4CreateButton;
362 extern DELETE_DIA_ELEM_T   qt4DestroyButton;
363 extern CREATE_FILE_T       qt4CreateFile;
364 extern DELETE_DIA_ELEM_T   qt4DestroyFile;
365 extern CREATE_DIR_T        qt4CreateDir;
366 extern DELETE_DIA_ELEM_T   qt4DestroyDir;
367 extern CREATE_MENUDYNAMIC_T     qt4CreateMenuDynamic;
368 extern CREATE_MENU_T       qt4CreateMenu;
369 extern DELETE_DIA_ELEM_T   qt4DestroyMenu;
370 extern DELETE_DIA_ELEM_T   qt4DestroyMenuDynamic;
371 extern CREATE_USLIDER_T    qt4CreateUSlider;
372 extern DELETE_DIA_ELEM_T   qt4DestroyUSlider;
373 extern CREATE_SLIDER_T     qt4CreateSlider;
374 extern DELETE_DIA_ELEM_T   qt4DestroySlider;
375 extern CREATE_THREADCOUNT_T qt4CreateThreadCount;
376 extern DELETE_DIA_ELEM_T    qt4DestroyThreadCount;
377 extern CREATE_TOGGLE_UINT   qt4CreateToggleUint;
378 extern DELETE_DIA_ELEM_T    qt4DestroyToggleUint;
379 extern CREATE_TOGGLE_INT    qt4CreateToggleInt;
380 extern DELETE_DIA_ELEM_T    qt4DestroyToggleInt;
381 extern CREATE_TOGGLE_T      qt4CreateToggle;
382 extern DELETE_DIA_ELEM_T    qt4DestroyToggle;
383 extern CREATE_TIMESTAMP_T   qt4CreateTimeStamp;
384 extern DELETE_DIA_ELEM_T    qt4DestroyTimeStamp;
385 
386 //************
387 static FactoryDescriptor Qt4FactoryDescriptor=
388 {
389         &qt4FactoryGetVersion,
390         &qt4DiaFactoryRun,
391         &qt4DiaFactoryRunTabs,
392         &qt4DiaFactoryTabsPrepare,
393         &qt4DiaFactoryTabsFinish,
394         &qt4DiaFactoryPrepare,
395         &qt4DiaFactoryFinish,
396 
397         // Buttons
398         &qt4CreateButton,
399         &qt4DestroyButton,
400         // Bar
401         &qt4CreateBar,
402         &qt4DestroyBar,
403         // Float
404         &qt4CreateFloat,
405         &qt4DestroyFloat,
406         // Integer
407         &qt4CreateInteger,
408         &qt4DestroyInteger,
409         // UInteger
410         &qt4CreateUInteger,
411         &qt4DestroyUInteger,
412         // Notch
413         &qt4CreateNotch,
414         &qt4DestroyNotch,
415         // RoText
416         &qt4CreateRoText,
417         &qt4DestroyRoText,
418         // Text
419         &qt4CreateText,
420         &qt4DestroyText,
421         // Hex
422         &qt4CreateHex,
423         &qt4DestroyHex,
424         // Matrix
425         &qt4CreateMatrix,
426         &qt4DestroyMatrix,
427         // Menu
428         &qt4CreateMenu,
429         &qt4DestroyMenu,
430         &qt4CreateMenuDynamic,
431         &qt4DestroyMenuDynamic,
432         // ThreadCount
433         &qt4CreateThreadCount,
434         &qt4DestroyThreadCount,
435         // Bitrate
436         &qt4CreateBitrate,
437         &qt4DestroyBitrate,
438         // File
439         &qt4CreateFile,
440         &qt4DestroyFile,
441         // Dir
442         &qt4CreateDir,
443         &qt4DestroyDir,
444         // Frame
445         &qt4CreateFrame,
446         &qt4DestroyFrame,
447     // Toggle uint/int
448         &qt4CreateToggleUint,
449         &qt4DestroyToggleUint,
450         &qt4CreateToggleInt,
451         &qt4DestroyToggleInt,
452         // Regular toggle
453         &qt4CreateToggle,
454         &qt4DestroyToggle,
455         // Slider
456         &qt4CreateUSlider,
457         &qt4DestroyUSlider,
458         &qt4CreateSlider,
459         &qt4DestroySlider,
460         // Timestamp
461         &qt4CreateTimeStamp,
462         &qt4DestroyTimeStamp
463 };
464 
465 /**
466  *         \fn InitFactory
467  *  \brief Install our factory hooks
468  */
InitFactory(void)469 void InitFactory(void)
470 {
471         DIA_factoryInit(&Qt4FactoryDescriptor);
472 
473 
474 }
475 
476 //EOF
477