libyui-mga  1.0.4
 All Classes Functions Enumerations Enumerator
YMGAAboutDialog.cc
1 /*
2  Copyright 2014 by Matteo Pasotti
3 
4  This library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) version 3.0 of the License. This library
8  is distributed in the hope that it will be useful, but WITHOUT ANY
9  WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11  License for more details. You should have received a copy of the GNU
12  Lesser General Public License along with this library; if not, write
13  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14  Floor, Boston, MA 02110-1301 USA
15 */
16 
17 /*-/
18 
19  File: YMGAAboutDialog.cc
20 
21  Author: Matteo Pasotti <matteo.pasotti@gmail.com>
22 
23 /-*/
24 
25 #define YUILogComponent "mga-ui"
26 #include "YUILog.h"
27 
28 #include <yui/YUI.h>
29 #include <yui/YOptionalWidgetFactory.h>
30 #include <yui/YApplication.h>
31 #include <yui/YWidgetFactory.h>
32 #include <yui/YDialog.h>
33 #include <yui/YLayoutBox.h>
34 #include <yui/YLabel.h>
35 #include <yui/YPushButton.h>
36 #include <yui/YEvent.h>
37 #include <yui/YImage.h>
38 #include <yui/YRichText.h>
39 #include <yui/YDumbTab.h>
40 #include <yui/YReplacePoint.h>
41 
42 #include <boost/algorithm/string/trim.hpp>
43 
44 #include "YMGAAboutDialog.h"
45 
46 using std::endl;
47 
49 public:
51  virtual ~YMGAAboutDialogPrivate(){}
52 
53  std::string appName;
54  std::string appVersion;
55  std::string appLicense;
56  std::string appAuthors;
57  std::string appDescription;
58  std::string appLogo;
59  std::string appIcon;
60  std::string appCredits;
61  std::string appInformation;
62 
63  YDialog* mainDialog;
64 };
65 
66 /**
67  * The constructor. Note that this object is not a widget, so you must deleted it, if allocated on the heap.
68  * @param name the application name
69  * @param version the application version
70  * @param license the application license, the short length one (e.g. GPLv2, GPLv3, LGPLv2+, etc)
71  * @param authors the string providing the list of authors; it could be html-formatted
72  * @param description the string providing a brief description of the application
73  * @param logo the string providing the file path for the application logo (high-res image)
74  * @param icon the string providing the file path for the application icon (low-res image)
75  * @param credits optional, the application credits, they can be html-formatted
76  * @param information optional, other extra informations, they can be html-formatted
77  */
78 YMGAAboutDialog::YMGAAboutDialog(const std::string& name,
79  const std::string& version,
80  const std::string& license,
81  const std::string& authors,
82  const std::string& description,
83  const std::string& logo,
84  const std::string& icon,
85  const std::string& credits,
86  const std::string& information
87  ) :
88  priv ( new YMGAAboutDialogPrivate())
89 {
90  YUI_CHECK_NEW ( priv );
91 
92  priv->appName = name;
93  priv->appVersion = version;
94  priv->appLicense = license;
95  priv->appAuthors = authors;
96  priv->appDescription = description;
97  priv->appLogo = logo;
98  priv->appIcon = icon;
99  priv->appCredits = credits;
100  priv->appInformation = information;
101  boost::algorithm::trim(priv->appIcon);
102 }
103 
104 YMGAAboutDialog::~YMGAAboutDialog()
105 {
106  delete priv;
107 }
108 
109 /**
110  * invoked by pressing the information button inside the Classic dialog, it shows a popup dialog providing the information passed to the constructor
111  * @see YMGAAboutDialog()
112  * @see Classic()
113  */
114 void YMGAAboutDialog::showInformation()
115 {
116  auto infoDialog = YUI::widgetFactory()->createPopupDialog();
117  auto vbox = YUI::widgetFactory()->createVBox(infoDialog);
118  auto tophbox = YUI::widgetFactory()->createHBox(vbox);
119  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,10.0);
120  YUI::widgetFactory()->createLabel(tophbox,"Information");
121  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,10.0);
122  auto bottomhbox = YUI::widgetFactory()->createHBox(vbox);
123  YUI::widgetFactory()->createSpacing(bottomhbox,YD_HORIZ,false,1.0);
124  auto rtcontent = YUI::widgetFactory()->createRichText(bottomhbox);
125  YUI::widgetFactory()->createSpacing(bottomhbox,YD_HORIZ,false,1.0);
126  rtcontent->setText(priv->appInformation);
127  auto closeButton = YUI::widgetFactory()->createPushButton(vbox,"Close");
128  while(true)
129  {
130  YEvent* event = infoDialog->waitForEvent();
131  if(event)
132  {
133  // window manager "close window" button
134  if ( event->eventType() == YEvent::CancelEvent
135  || event->widget() == closeButton )
136  break; // leave event loop
137  }
138  }
139  infoDialog->destroy();
140 }
141 
142 /**
143  * invoked by pressing the credits button inside the Classic dialog, it shows a popup dialog providing the credit list passed to the constructor
144  * @see YMGAAboutDialog()
145  * @see Classic()
146  */
147 void YMGAAboutDialog::showCredits()
148 {
149  auto creditDialog = YUI::widgetFactory()->createPopupDialog();
150  auto vbox = YUI::widgetFactory()->createVBox(creditDialog);
151  auto tophbox = YUI::widgetFactory()->createHBox(vbox);
152  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,16.0);
153  YUI::widgetFactory()->createLabel(tophbox,"Credits");
154  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,16.0);
155  auto rtcontent = YUI::widgetFactory()->createLabel(vbox,"");
156  rtcontent->setText(priv->appCredits);
157  auto closeButton = YUI::widgetFactory()->createPushButton(vbox,"Close");
158  while(true)
159  {
160  YEvent* event = creditDialog->waitForEvent();
161  if(event)
162  {
163  // window manager "close window" button
164  if ( event->eventType() == YEvent::CancelEvent
165  || event->widget() == closeButton )
166  break; // leave event loop
167  }
168  }
169  creditDialog->destroy();
170 }
171 
172 /**
173  * it generates the layout and the widgets providing the authors info
174  * @param rpoint the replace point inside the Tabbed dialog
175  * @see YMGAAboutDialog()
176  * @see Tabbed()
177  */
178 
179 void YMGAAboutDialog::genAuthorsTab(YReplacePoint* rpoint)
180 {
181  rpoint->deleteChildren();
182  auto hbox = YUI::widgetFactory()->createHBox(rpoint);
183  YUI::widgetFactory()->createRichText(hbox,priv->appAuthors);
184  rpoint->showChild();
185  priv->mainDialog->recalcLayout();
186 }
187 
188 /**
189  * it generates the layout and the widgets providing the contributors info
190  * @param rpoint the replace point inside the Tabbed dialog
191  * @see YMGAAboutDialog()
192  * @see Tabbed()
193  */
194 void YMGAAboutDialog::genContributorsTab(YReplacePoint* rpoint)
195 {
196  rpoint->deleteChildren();
197  auto hbox = YUI::widgetFactory()->createHBox(rpoint);
198  YUI::widgetFactory()->createRichText(hbox,priv->appDescription);
199  rpoint->showChild();
200  priv->mainDialog->recalcLayout();
201 }
202 
203 /**
204  * it generates the layout and the widgets providing extra information
205  * @param rpoint the replace point inside the Tabbed dialog
206  * @see YMGAAboutDialog()
207  * @see Tabbed()
208  */
209 void YMGAAboutDialog::genInformationTab(YReplacePoint* rpoint)
210 {
211  rpoint->deleteChildren();
212  auto hbox = YUI::widgetFactory()->createHBox(rpoint);
213  YUI::widgetFactory()->createRichText(hbox,priv->appInformation);
214  rpoint->showChild();
215  priv->mainDialog->recalcLayout();
216 }
217 
218 /**
219  * it builds the tabbed version for the about dialog box.
220  * @see YMGAAboutDialog()
221  * @see Classic()
222  */
223 void YMGAAboutDialog::Tabbed()
224 {
225  std::string oldTitle = YUI::app()->applicationTitle();
226  YUI::app()->setApplicationTitle("About " + priv->appName);
227  if(priv->appIcon.length())
228  YUI::app()->setApplicationIcon(priv->appIcon);
229  priv->mainDialog = YUI::widgetFactory()->createPopupDialog();
230  auto vbox = YUI::widgetFactory()->createVBox(priv->mainDialog);
231 
232  /*
233  * Layout
234  * ______________________
235  * | ICON APPNAME VER |
236  * |___CREDITS__________|
237  * |_a_|_b_|_c__________|
238  * | |
239  * | |
240  * |____________________|
241  */
242 
243  YUI::widgetFactory()->createSpacing(vbox,YD_VERT,false,1.0);
244  auto upperhbox = YUI::widgetFactory()->createHBox(vbox);
245 
246  // the logo, if defined, if available
247  if(priv->appLogo.length())
248  {
249  YUI::widgetFactory()->createSpacing(upperhbox,YD_HORIZ,false,3.0);
250  YUI::widgetFactory()->createImage(upperhbox,priv->appLogo);
251  YUI::widgetFactory()->createSpacing(upperhbox,YD_HORIZ,false,3.0);
252  }
253 
254  // app name and version
255  YUI::widgetFactory()->createSpacing(upperhbox,YD_HORIZ,false,3.0);
256  YUI::widgetFactory()->createLabel(upperhbox,priv->appName + " " + priv->appVersion);
257  YUI::widgetFactory()->createSpacing(upperhbox,YD_HORIZ,false,5.0);
258  YUI::widgetFactory()->createSpacing(vbox,YD_VERT,false,1.0);
259 
260  // credits
261  auto tophbox = YUI::widgetFactory()->createHBox(vbox);
262  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,3.0);
263  YUI::widgetFactory()->createLabel(tophbox,priv->appCredits);
264  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,3.0);
265  YUI::widgetFactory()->createSpacing(vbox,YD_VERT,false,0.5);
266 
267  // license
268  auto licensehbox = YUI::widgetFactory()->createHBox(vbox);
269  YUI::widgetFactory()->createSpacing(licensehbox,YD_HORIZ,false,3.0);
270  YUI::widgetFactory()->createLabel(licensehbox,priv->appLicense);
271  YUI::widgetFactory()->createSpacing(licensehbox,YD_HORIZ,false,3.0);
272  YUI::widgetFactory()->createSpacing(vbox,YD_VERT,false,0.5);
273 
274  if(YUI::optionalWidgetFactory()->hasDumbTab())
275  {
276  auto dumbTab = YUI::optionalWidgetFactory()->createDumbTab(vbox);
277 
278  if(priv->appAuthors.length())
279  {
280  dumbTab->addItem(new YItem("Authors"));
281  }
282  if(priv->appDescription.length())
283  {
284  dumbTab->addItem(new YItem("Description"));
285  }
286  if(priv->appInformation.length())
287  {
288  dumbTab->addItem(new YItem("Information"));
289  }
290 
291  auto bottomvbox = YUI::widgetFactory()->createVBox(vbox);
292  auto rpoint = YUI::widgetFactory()->createReplacePoint(bottomvbox);
293 
294  if(priv->appAuthors.length())
295  {
296  this->genAuthorsTab(rpoint);
297  }
298 
299  auto cancelButton = YUI::widgetFactory()->createPushButton(vbox,"Close");
300 
301  while(true)
302  {
303  YEvent* event = priv->mainDialog->waitForEvent();
304  if(event)
305  {
306  // window manager "close window" button
307  if ( event->eventType() == YEvent::CancelEvent
308  || event->widget() == cancelButton )
309  break; // leave event loop
310  if ( event->eventType() == YEvent::MenuEvent )
311  {
312  if( event->item()->label().replace(event->item()->label().find("&"),1,"").compare("Authors")==0 )
313  {
314  this->genAuthorsTab(rpoint);
315  }
316  else if( event->item()->label().replace(event->item()->label().find("&"),1,"").compare("Description")==0 )
317  {
318  this->genContributorsTab(rpoint);
319  }
320  else if( event->item()->label().replace(event->item()->label().find("&"),1,"").compare("Information")==0 )
321  {
322  this->genInformationTab(rpoint);
323  }
324  }
325  }
326  }
327  priv->mainDialog->destroy();
328  }
329  else
330  {
331  // handle it
332  }
333  YUI::app()->setApplicationTitle(oldTitle);
334 }
335 
336 /**
337  * it builds the classic version for the about dialog box.
338  * @see YMGAAboutDialog()
339  * @see Tabbed()
340  */
341 void YMGAAboutDialog::Classic()
342 {
343  YPushButton* creditsButton = nullptr;
344  YPushButton* infoButton = nullptr;
345  std::string oldTitle = YUI::app()->applicationTitle();
346  YUI::app()->setApplicationTitle("About " + priv->appName);
347  if(priv->appIcon.length())
348  YUI::app()->setApplicationIcon(priv->appIcon);
349  priv->mainDialog = YUI::widgetFactory()->createPopupDialog();
350  auto vbox = YUI::widgetFactory()->createVBox(priv->mainDialog);
351  auto tophbox = YUI::widgetFactory()->createHBox(vbox);
352 
353  // logo, if defined, if available
354  if(priv->appLogo.length())
355  {
356  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,2.0);
357  YUI::widgetFactory()->createImage(tophbox,priv->appLogo);
358  }
359 
360  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,8.0);
361  auto headvbox = YUI::widgetFactory()->createVBox(tophbox);
362  YUI::widgetFactory()->createSpacing(tophbox,YD_HORIZ,false,12.0);
363 
364  // app name and version
365  auto lblAppName = YUI::widgetFactory()->createLabel(headvbox, "");
366  lblAppName->setValue(priv->appName);
367  auto lblAppVersion = YUI::widgetFactory()->createLabel(headvbox, "");
368  lblAppVersion->setValue(priv->appVersion);
369  auto lblLicense = YUI::widgetFactory()->createLabel(headvbox, "");
370  lblLicense->setValue(priv->appLicense);
371 
372  auto midhbox = YUI::widgetFactory()->createHBox(vbox);
373  // app description
374  auto toprightvbox = YUI::widgetFactory()->createVBox(midhbox);
375  toprightvbox->setWeight(YD_HORIZ, 5);
376  YUI::widgetFactory()->createSpacing(toprightvbox,YD_HORIZ,false,5.0);
377  auto rt = YUI::widgetFactory()->createRichText(toprightvbox,"");
378  YUI::widgetFactory()->createSpacing(toprightvbox,YD_HORIZ,false,5.0);
379  rt->setValue(priv->appDescription);
380 
381  // info button, if information are defined
382  auto bottomhbox = YUI::widgetFactory()->createHBox(vbox);
383  if(priv->appInformation.length())
384  {
385  infoButton = YUI::widgetFactory()->createPushButton(bottomhbox, "Info");
386  }
387 
388  // credits button, if credits are defined
389  if(!priv->appCredits.empty())
390  {
391  creditsButton = YUI::widgetFactory()->createPushButton(bottomhbox, "Credits");
392  }
393 
394  auto cancelButton = YUI::widgetFactory()->createPushButton(bottomhbox, "Close");
395  while(true)
396  {
397  YEvent* event = priv->mainDialog->waitForEvent();
398  if(event)
399  {
400  // window manager "close window" button
401  if ( event->eventType() == YEvent::CancelEvent
402  || event->widget() == cancelButton )
403  break; // leave event loop
404  else if( ( infoButton != nullptr ) && event->widget() == infoButton )
405  {
406  this->showInformation();
407  }
408  else if( ( creditsButton != nullptr ) && event->widget() == creditsButton )
409  {
410  this->showCredits();
411  }
412  }
413  }
414  priv->mainDialog->destroy();
415  YUI::app()->setApplicationTitle(oldTitle);
416 }
417 
418 /**
419  * it actually shows the about dialog
420  * @param type optional, DLG_MODE: defaulting to CLASSIC if not defined
421  * @see Classic()
422  * @see Tabbed()
423  * @see YMGAAboutDialog::DLG_MODE
424  */
425 void YMGAAboutDialog::show(YMGAAboutDialog::DLG_MODE type)
426 {
427  if(type == TABBED)
428  {
429  this->Tabbed();
430  }
431  else
432  {
433  this->Classic();
434  }
435 }
YMGAAboutDialog(const std::string &name, const std::string &version, const std::string &license, const std::string &authors, const std::string &description, const std::string &logo, const std::string &icon=std::string(), const std::string &credits=std::string(), const std::string &information=std::string())
The constructor.
void show(DLG_MODE type=TABBED)
it actually shows the about dialog