libyui  3.10.0
YPopupInternal.cc
1 /*
2  Copyright (C) 2016 SUSE LLC
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 #include <YUI.h>
18 #include <YWidgetFactory.h>
19 #include <YDialog.h>
20 #include <YLayoutBox.h>
21 #include <YAlignment.h>
22 #include <YButtonBox.h>
23 #include <YPushButton.h>
24 #include <YInputField.h>
25 #include <YSpacing.h>
26 #include <YEvent.h>
27 
28 #define YUILogComponent "ui-popup"
29 #include "YUILog.h"
30 
31 #include <YPopupInternal.h>
32 
33 using std::string;
34 
35 
36 void YPopupInternal::message(const string & label)
37 {
38  auto f = YUI::widgetFactory();
39 
40  auto popup = f->createPopupDialog();
41  auto mb = f->createMarginBox(popup, 1, 0.1);
42  auto vbox = f->createVBox(mb);
43  f->createLabel(vbox, label);
44 
45  auto bbox = f->createButtonBox(vbox);
46  auto okButton = f->createPushButton(bbox, "OK");
47  okButton->setRole(YOKButton);
48  okButton->setDefaultButton();
49 
50  while (true)
51  {
52  auto event = popup->waitForEvent();
53  if (event && (event->widget() == okButton || event->eventType() == YEvent::CancelEvent))
54  {
55  break;
56  }
57  }
58 
59  popup->destroy();
60 }
61 
62 /**
63  * Helper method for adding new input fields
64  * @param parent Where to add the widget
65  * @param val The initial value
66  */
67 static void addTextField(YWidget *parent, const string &val)
68 {
69  auto new_item = YUI::widgetFactory()->createInputField(parent, "");
70  new_item->setProperty("Value", YPropertyValue(val));
71  new_item->setProperty("HStretch", YPropertyValue(true));
72 }
73 
74 bool YPopupInternal::editStringArray(StringArray &array, const string &label)
75 {
76  auto f = YUI::widgetFactory();
77 
78  auto popup = f->createPopupDialog();
79  auto mb = f->createMarginBox(popup, 1, 0.1);
80  auto vbox = f->createVBox(mb);
81  f->createHeading(vbox, label);
82  YWidget *arrayBox = f->createVBox(vbox);
83 
84  // access by reference
85  for(auto&& str: array) addTextField(arrayBox, str);
86 
87  auto addButton = f->createPushButton(vbox, "Add Item");
88 
89  auto spacing = f->createVSpacing(vbox, 1);
90  spacing->setProperty("VStretch", YPropertyValue(true));
91 
92  auto bbox = f->createButtonBox(vbox);
93  auto okButton = f->createPushButton(bbox, "OK");
94  okButton->setRole(YOKButton);
95  okButton->setDefaultButton();
96  auto cancelButton = f->createPushButton(bbox, "Cancel");
97  cancelButton->setRole(YCancelButton);
98 
99  bool ret;
100 
101  while (true)
102  {
103  auto event = popup->waitForEvent();
104 
105  if (!event) continue;
106 
107  // cancel button or the window manager close button
108  if (event->widget() == cancelButton || event->eventType() == YEvent::CancelEvent)
109  {
110  ret = false;
111  break;
112  }
113  else if (event->widget() == okButton)
114  {
115  array.clear();
116 
117  // put all input field values into the target array
118  for(auto&& widget: *arrayBox)
119  {
120  auto input = dynamic_cast<YInputField*>(widget);
121  if (input) array.push_back(input->value());
122  }
123 
124  ret = true;
125  break;
126  }
127  else if (event->widget() == addButton)
128  {
129  addTextField(arrayBox, "");
130  popup->recalcLayout();
131  }
132  else
133  yuiWarning() << "Unknown event " << event << endl;
134  }
135 
136  popup->destroy();
137 
138  return ret;
139 }
140 
141 YPopupInternal::StringArray YPopupInternal::editNewStringArray(const string &label)
142 {
143  YPopupInternal::StringArray ret { "", "", "" };
144 
145  if (editStringArray(ret, label))
146  return ret;
147  else
148  // empty array
149  return StringArray();
150 }
InputField: General purpose one line input field for entering text and other data.
Definition: YInputField.h:47
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YInputField.cc:151
static void message(const std::string &label)
Display a simple popup dialog with OK button.
static bool editStringArray(StringArray &array, const std::string &label)
Display a popup dialog with several input fields.
static StringArray editNewStringArray(const std::string &label)
Display a popup dialog with 3 initially empty input fields.
Transport class for the value of simple properties.
Definition: YProperty.h:105
static YWidgetFactory * widgetFactory()
Return the widget factory that provides all the createXY() methods for standard (mandatory,...
Definition: YUI.cc:132
Abstract base class of all UI widgets.
Definition: YWidget.h:55