1 // Object Viewer Qt GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2012-2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "widget_properties.h"
21 #include "widget_info_tree.h"
22 #include "widget_info_serializer.h"
23 #include "new_property_widget.h"
24 #include "new_widget_widget.h"
25 #include <QMessageBox>
28 CWidgetProperties::CWidgetProperties( QWidget
*parent
) :
31 newPropertyWidget
= new NewPropertyWidget();
32 newWidgetWidget
= new NewWidgetWidget();
35 connect( rmWButton
, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveWButtonClicked() ) );
36 connect( rmPButton
, SIGNAL( clicked( bool ) ), this, SLOT( onRemovePButtonClicked() ) );
37 connect( addPButton
, SIGNAL( clicked( bool ) ), this, SLOT( onAddPButtonClicked() ) );
38 connect( addWButton
, SIGNAL( clicked( bool ) ), this, SLOT( onAddWButtonClicked() ) );
39 connect( saveButton
, SIGNAL( clicked( bool ) ), this, SLOT( onSaveButtonClicked() ) );
40 connect( newPropertyWidget
, SIGNAL( propertyAdded() ), this, SLOT( onPropertyAdded() ) );
41 connect( newWidgetWidget
, SIGNAL( widgetAdded() ), this, SLOT( onWidgetAdded() ) );
44 CWidgetProperties::~CWidgetProperties()
46 delete newPropertyWidget
;
47 newPropertyWidget
= NULL
;
48 delete newWidgetWidget
;
49 newWidgetWidget
= NULL
;
53 void CWidgetProperties::setupWidgetInfo( CWidgetInfoTree
*tree
)
57 onListSelectionChanged( 0 );
58 connect( widgetList
, SIGNAL( currentRowChanged( int ) ), this, SLOT( onListSelectionChanged( int ) ) );
61 void CWidgetProperties::onListSelectionChanged( int i
)
65 if( i
>= widgetList
->count() )
68 QListWidgetItem
*item
= widgetList
->item( i
);
69 setPropsOf( item
->text().toUtf8().constData() );
73 void CWidgetProperties::onRemoveWButtonClicked()
75 if( widgetList
->count() == 0 )
78 QString widgetName
= widgetList
->currentItem()->text();
80 int reply
= QMessageBox::question( this,
81 tr( "Removing a widget" ),
82 tr( "Are you sure you want to remove %1?" ).arg( widgetName
),
83 QMessageBox::Yes
| QMessageBox::Cancel
86 if( reply
!= QMessageBox::Yes
)
89 tree
->removeNode( widgetName
.toUtf8().constData() );
91 widgetPropTree
->clear();
95 void CWidgetProperties::onRemovePButtonClicked()
97 QTreeWidgetItem
*item
= widgetPropTree
->currentItem();
98 CWidgetInfoTreeNode
*node
= tree
->findNodeByName( widgetList
->currentItem()->text().toUtf8().constData() );
100 if( ( item
== NULL
) || ( node
== NULL
) )
103 std::string name
= item
->text( 0 ).toUtf8().constData();
105 std::vector
< SPropEntry
>::const_iterator itr
= node
->getInfo().findProp( name
);
106 if( itr
== node
->getInfo().props
.end() )
109 int reply
= QMessageBox::question( this,
110 tr( "Removing a property" ),
111 tr( "Are you sure you want to remove" ).arg( QString( name
.c_str() ) ),
112 QMessageBox::Yes
| QMessageBox::Cancel
115 if( reply
!= QMessageBox::Yes
)
118 SPropEntry prop
= *itr
;
119 tree
->removePropertyFromAll( prop
);
121 onListSelectionChanged( widgetList
->currentRow() );
125 void CWidgetProperties::onAddWButtonClicked()
127 newWidgetWidget
->setWidgetInfoTree( tree
);
128 newWidgetWidget
->fillWidgetList( widgetNames
);
129 newWidgetWidget
->show();
132 void CWidgetProperties::onAddPButtonClicked()
134 if( widgetList
->count() == 0 )
137 if( ( widgetList
->currentRow() >= widgetList
->count() ) || ( widgetList
->currentRow() < 0 ) )
140 CWidgetInfoTreeNode
*node
= tree
->findNodeByName( widgetList
->currentItem()->text().toUtf8().constData() );
144 newPropertyWidget
->setWidgetInfo( node
);
145 newPropertyWidget
->show();
149 void CWidgetProperties::onSaveButtonClicked()
151 CWidgetInfoSerializer serializer
;
152 serializer
.serialize( tree
);
155 void CWidgetProperties::onPropertyAdded()
157 onListSelectionChanged( widgetList
->currentRow() );
160 void CWidgetProperties::onWidgetAdded()
163 Q_EMIT
treeChanged();
166 void CWidgetProperties::buildWidgetList()
170 tree
->getNames( widgetNames
);
171 std::sort( widgetNames
.begin(), widgetNames
.end() );
172 for( std::vector
< std::string
>::const_iterator itr
= widgetNames
.begin(); itr
!= widgetNames
.end(); ++itr
)
173 widgetList
->addItem( itr
->c_str() );
174 widgetList
->setCurrentRow( 0 );
177 void CWidgetProperties::setPropsOf( const char *name
)
179 CWidgetInfoTreeNode
*node
= tree
->findNodeByName( name
);
183 widgetPropTree
->clear();
185 const std::vector
< SPropEntry
> &v
= node
->getInfo().props
;
186 for( std::vector
< SPropEntry
>::const_iterator itr2
= v
.begin(); itr2
!= v
.end(); ++itr2
)
188 SPropEntry e
= *itr2
;
189 QTreeWidgetItem
*item
= new QTreeWidgetItem
;
190 item
->setText( 0, e
.propName
.c_str() );
191 item
->setText( 1, e
.propType
.c_str() );
192 item
->setText( 2, e
.propDefault
.c_str() );
193 widgetPropTree
->addTopLevelItem( item
);