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