libyui  3.2.4
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
YDialogSpy.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YDialogSpy.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include <sstream>
26 
27 #define YUILogComponent "ui-dialog-spy"
28 #include "YUILog.h"
29 
30 #include <YDialogSpy.h>
31 #include <YWidgetFactory.h>
32 #include <YDialog.h>
33 #include <YEvent.h>
34 #include <YTable.h>
35 #include <YTree.h>
36 #include <YTreeItem.h>
37 #include <YLayoutBox.h>
38 #include <YAlignment.h>
39 #include <YButtonBox.h>
40 #include <YPushButton.h>
41 #include <YReplacePoint.h>
42 #include <YUI.h>
43 
44 #define TREE_VWEIGHT 40
45 #define PROP_VWEIGHT 60
46 
47 #define DIA_HEIGHT 24
48 
49 #define TREE_HEIGHT 10
50 #define TREE_WIDTH 50
51 
52 #define PROP_HEIGHT 12
53 #define PROP_WIDTH 50
54 
55 
56 /**
57  * Custom tree item class to map tree items to widgets
58  **/
60 {
61 public:
62  YWidgetTreeItem( YWidget * widget,
63  bool isOpen )
64  : YTreeItem( "", isOpen )
65  , _widget( widget )
66  {
67  setWidgetLabel();
68  }
69 
71  YWidget * widget,
72  bool isOpen )
73  : YTreeItem( parent, "", isOpen )
74  , _widget( widget )
75  {
76  setWidgetLabel();
77  }
78 
79  virtual ~YWidgetTreeItem() {}
80  YWidget * widget() const { return _widget; }
81 
82 
83 protected:
84 
85  void setWidgetLabel()
86  {
87  std::ostringstream str;
88  str << _widget;
89  setLabel( str.str() );
90  }
91 
92 private:
93  YWidget * _widget;
94 };
95 
96 
97 static void fillTree( YWidgetTreeItem * parent,
98  YWidgetListConstIterator begin,
99  YWidgetListConstIterator end,
100  int treeLevel );
101 
102 
103 
104 
106 {
108  : targetDialog( 0 )
109  , spyDialog( 0 )
110  , widgetTree( 0 )
111  , propButton( 0 )
112  , propReplacePoint( 0 )
113  , propTable( 0 )
114  , closeButton( 0 )
115  {}
116 
117  YDialog * targetDialog; // Dialog that is being inspected
118  YDialog * spyDialog; // Debug dialog that shows widget data
119  YTree * widgetTree; // Tree widget to show widget hierarchy
120  YPushButton * propButton;
121  YReplacePoint * propReplacePoint;
122  YTable * propTable;
123  YPushButton * closeButton;
124 };
125 
126 
127 
129  : priv( new YDialogSpyPrivate() )
130 {
131  if ( ! targetDialog )
132  targetDialog = YDialog::topmostDialog();
133 
134  priv->targetDialog = targetDialog;
136 
137  priv->spyDialog = fac->createPopupDialog();
138  YAlignment * diaMin = fac->createMinHeight( priv->spyDialog, DIA_HEIGHT );
139  YLayoutBox * vbox = fac->createVBox( diaMin );
140 
141  YAlignment * minSize = fac->createMinSize( vbox, TREE_WIDTH, TREE_HEIGHT );
142  minSize->setWeight( YD_VERT, TREE_VWEIGHT );
143  priv->widgetTree = fac->createTree( minSize, "Widget &Tree", false );
144  priv->widgetTree->setNotify( true );
145 
146  YWidgetTreeItem * rootItem = new YWidgetTreeItem( targetDialog, true );
147  YUI_CHECK_NEW( rootItem );
148  fillTree( rootItem, targetDialog->childrenBegin(), targetDialog->childrenEnd(), 1 );
149  priv->widgetTree->addItem( rootItem );
150  priv->widgetTree->rebuildTree();
151 
152  YAlignment * alignment = fac->createLeft( vbox );
153  priv->propButton = fac->createPushButton( alignment, "&Properties >>>" );
154  priv->propReplacePoint = fac->createReplacePoint( vbox );
155  fac->createEmpty( priv->propReplacePoint );
156 
157  YButtonBox * buttonBox = fac->createButtonBox( vbox );
158  priv->closeButton = fac->createPushButton( buttonBox, "&Close" );
159  priv->closeButton->setRole( YOKButton );
160 }
161 
162 
164 {
165  if ( priv->spyDialog )
166  priv->spyDialog->destroy();
167 }
168 
169 
171 {
172  return priv->propTable != 0;
173 }
174 
175 
177 {
178  if ( ! propertiesShown() )
179  {
180  priv->propReplacePoint->deleteChildren();
181  priv->propReplacePoint->setWeight( YD_VERT, PROP_VWEIGHT );
182 
184  YAlignment * minSize = fac->createMinSize( priv->propReplacePoint,
185  PROP_WIDTH, PROP_HEIGHT );
186  YTableHeader * header = new YTableHeader();
187  YUI_CHECK_NEW( header );
188  header->addColumn( "Property" );
189  header->addColumn( "Value" );
190  header->addColumn( "Type" );
191 
192  priv->propTable = fac->createTable( minSize, header );
193  // priv->propTable->setKeepSorting( true );
194 
195  priv->propButton->setLabel( "<<< &Properties" );
196  priv->propReplacePoint->showChild();
197  priv->spyDialog->recalcLayout();
198  }
199 }
200 
201 
203 {
204  if ( propertiesShown() )
205  {
206  priv->propReplacePoint->deleteChildren();
207  priv->propReplacePoint->setWeight( YD_VERT, 0 );
208  priv->propTable = 0;
209  YUI::widgetFactory()->createEmpty( priv->propReplacePoint );
210 
211  priv->propButton->setLabel( "&Properties >>>" );
212  priv->propReplacePoint->showChild();
213  priv->spyDialog->recalcLayout();
214  }
215 }
216 
217 
219 {
220  if ( ! priv->propTable )
221  return;
222 
223  priv->propTable->deleteAllItems();
224 
225  if ( widget )
226  {
227  YPropertySet propSet = widget->propertySet();
228  YItemCollection items;
229  items.reserve( propSet.size() );
230 
231  for ( YPropertySet::const_iterator it = propSet.propertiesBegin();
232  it != propSet.propertiesEnd();
233  ++it )
234  {
235  YProperty prop = *it;
236  YPropertyValue propVal = widget->getProperty( prop.name() );
237  std::string propValStr;
238 
239  switch ( prop.type() )
240  {
241  case YStringProperty:
242  propValStr = propVal.stringVal();
243  break;
244 
245  case YBoolProperty:
246  propValStr = propVal.boolVal() ? "true" : "false";
247  break;
248 
249  case YIntegerProperty:
250  {
251  std::ostringstream str;
252  str << propVal.integerVal();
253  propValStr = str.str();
254  }
255  break;
256 
257  default:
258  propValStr = "???";
259  break;
260  }
261 
262  YTableItem * item = new YTableItem( prop.name(), propValStr, prop.typeAsStr() );
263  YUI_CHECK_NEW( item );
264  items.push_back( item );
265  }
266 
267  priv->propTable->addItems( items );
268  priv->propTable->deselectAllItems();
269  }
270 }
271 
272 
273 void fillTree( YWidgetTreeItem * parent,
274  YWidgetListConstIterator begin,
275  YWidgetListConstIterator end,
276  int treeLevel )
277 {
278  for ( YWidgetListConstIterator it = begin; it != end; ++it )
279  {
280  YWidget * widget = *it;
281  YWidgetTreeItem * item = new YWidgetTreeItem( parent, widget, treeLevel < 4 );
282 
283  if ( widget->hasChildren() )
284  fillTree( item, widget->childrenBegin(), widget->childrenEnd(), treeLevel+1 );
285  }
286 }
287 
288 
290 {
291  YUI_CHECK_PTR( priv->spyDialog );
292 
293  while ( true )
294  {
295  bool updateProp = false;
296  YEvent * event = priv->spyDialog->waitForEvent();
297  yuiMilestone() << "dialog: " << priv->spyDialog->preferredHeight();
298  yuiMilestone() << "tree: " << priv->widgetTree->preferredHeight();
299 
300  if ( event )
301  {
302  if ( event->widget() == priv->closeButton ||
303  event->eventType() == YEvent::CancelEvent ) // window manager "close window" button
304  {
305  priv->targetDialog->highlight( 0 );
306  return;
307  }
308 
309  if ( event->widget() == priv->propButton )
310  {
311  if ( propertiesShown() )
312  hideProperties();
313  else
314  {
315  showProperties();
316  updateProp = true;
317  }
318  }
319 
320  if ( event->widget() == priv->widgetTree || updateProp )
321  {
322  YWidgetTreeItem * item = (YWidgetTreeItem *) priv->widgetTree->selectedItem();
323  yuiDebug() << "Highlighting " << item << std::endl;
324 
325  if ( item )
326  {
327  priv->targetDialog->highlight( item->widget() );
328  showProperties( item->widget() );
329  }
330  }
331  }
332  }
333 }
334 
335 
337 {
338  try
339  {
340  YDialogSpy dialogSpy( dialog );
341  dialogSpy.exec();
342  }
343  catch ( YUIException & exception )
344  {
345  YUI_CAUGHT( exception );
346  }
347 }
static YWidgetFactory * widgetFactory()
Return the widget factory that provides all the createXY() methods for standard (mandatory, i.e.
Definition: YUI.cc:126
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
A vertical or horizontal stacking of widgets, implementing HBox and VBox.
Definition: YLayoutBox.h:37
A placeholder that can have its contents exchanged, using ReplaceWidget.
Definition: YReplacePoint.h:33
YPropertyType type() const
Returns the type of this property.
Definition: YProperty.h:72
Transport class for the value of simple properties.
Definition: YProperty.h:104
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:38
void hideProperties()
Hide the "Properties" sub-window.
Definition: YDialogSpy.cc:202
Helper class for YTable for table column properties:
Definition: YTableHeader.h:43
A set of properties to check names and types against.
Definition: YProperty.h:184
Abstract base class for events to be returned upon UI::UserInput() and related functions.
Definition: YEvent.h:43
void deleteChildren()
Delete all children and remove them from the children manager's list.
Definition: YWidget.cc:200
const_iterator propertiesBegin() const
Returns an iterator that points to the first property in this set.
Definition: YProperty.cc:139
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:167
void showProperties()
Show the "Properties" sub-window.
Definition: YDialogSpy.cc:176
bool propertiesShown() const
Return 'true' if the "Properties" sub-window is currently shown, 'false' if not.
Definition: YDialogSpy.cc:170
void addColumn(const std::string &header, YAlignmentType alignment=YAlignBegin)
Add a column with the specified colum header text and alignment.
Definition: YTableHeader.cc:62
Table: Selection list with multiple columns.
Definition: YTable.h:55
An interactive dialog debugger: Show the structure and content of a dialog and its widgets...
Definition: YDialogSpy.h:43
std::string name() const
Returns the name of this property.
Definition: YProperty.h:67
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:393
A push button; may have an icon, and a F-key shortcut.
Definition: YPushButton.h:37
int size() const
Returns the number of properties in this set.
Definition: YProperty.h:255
virtual void setRole(YButtonRole role)
Set a predefined role for this button.
Definition: YPushButton.cc:154
virtual void deleteAllItems()
Delete all items.
YWidgetListConstIterator childrenBegin() const
Return an iterator that points to the first child or to childrenEnd() if there are no children...
Definition: YWidget.h:212
bool hasChildren() const
Returns 'true' if this widget has any children.
Definition: YWidget.h:192
void setWeight(YUIDimension dim, int weight)
Set a weight in the specified dimension.
Definition: YWidget.cc:579
virtual int preferredHeight()=0
Preferred height of the widget.
Implementation of all the alignment widgets:
Definition: YAlignment.h:41
virtual YItem * selectedItem()
Return the (first) selected item or 0 if none is selected.
YTreeItem(const std::string &label, bool isOpen=false)
Constructors for toplevel items.
Definition: YTreeItem.cc:28
virtual ~YDialogSpy()
Destructor.
Definition: YDialogSpy.cc:163
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:453
virtual void highlight(YWidget *child)
Highlight a child widget of this dialog.
Definition: YDialog.h:312
Custom tree item class to map tree items to widgets.
Definition: YDialogSpy.cc:59
YDialogSpy(YDialog *dialog=0)
Constructor: Create a YDialogSpy for the specified dialog.
Definition: YDialogSpy.cc:128
Class for widget properties.
Definition: YProperty.h:51
virtual void addItem(YItem *item_disown)
Add one item.
void setLabel(const std::string &newLabel)
Set this item's label.
Definition: YItem.h:87
static void showDialogSpy(YDialog *dialog=0)
Show a YDialogSpy for the specified dialog.
Definition: YDialogSpy.cc:336
const_iterator propertiesEnd() const
Returns an iterator that points after the last property in this set.
Definition: YProperty.cc:145
void exec()
Execute the event loop.
Definition: YDialogSpy.cc:289
bool isOpen() const
Return 'true' if this tree item should be displayed open (with its children visible) by default...
Definition: YTreeItem.cc:99
virtual void deselectAllItems()
Deselect all items.
Tree: List box that displays a (scrollable) list of hierarchical items from which the user can select...
Definition: YTree.h:56
virtual void rebuildTree()=0
Rebuild the displayed tree from the internally stored YTreeItems.
virtual YTreeItem * parent() const
Returns this item's parent item or 0 if it is a toplevel item.
Definition: YTreeItem.h:129
void setNotify(bool notify=true)
Sets the Notify property.
Definition: YWidget.cc:517
void recalcLayout()
Recalculate the layout of the dialog and of all its children after children have been added or remove...
Definition: YDialog.cc:370
A window in the desktop environment.
Definition: YDialog.h:47
Abstract widget factory for mandatory widgets.
Container widget for dialog buttons that abstracts the button order depending on the desktop environm...
Definition: YButtonBox.h:148
virtual void setLabel(const std::string &label)
Set the label (the text on the button).
Definition: YPushButton.cc:80
Item class for YTable items.
Definition: YTableItem.h:58
Abstract base class of all UI widgets.
Definition: YWidget.h:54
static YDialog * topmostDialog(bool doThrow=true)
Alias for currentDialog().
Definition: YDialog.h:200
Base class for UI Exceptions.
Definition: YUIException.h:281
YWidgetListConstIterator childrenEnd() const
Return an interator that points after the last child.
Definition: YWidget.h:218
std::string typeAsStr() const
Returns the type of this property as string.
Definition: YProperty.h:82
Item class for tree items.
Definition: YTreeItem.h:37
bool destroy(bool doThrow=true)
Close and delete this dialog (and all its children) if it is the topmost dialog.
Definition: YDialog.cc:252
YEvent * waitForEvent(int timeout_millisec=0)
Wait for a user event.
Definition: YDialog.cc:379
virtual int preferredHeight()
Preferred height of the widget.
virtual void showChild()
Show a newly added child.