New lua versions
[ryzomcore.git] / studio / src / plugins / gui_editor / widget_properties.cpp
blobfa09f40ad6e02cdfd5896e6009625764bfb9254d
1 // Object Viewer Qt GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2012-2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
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>
27 namespace GUIEditor{
28 CWidgetProperties::CWidgetProperties( QWidget *parent ) :
29 QWidget( parent )
31 newPropertyWidget = new NewPropertyWidget();
32 newWidgetWidget = new NewWidgetWidget();
34 setupUi( this );
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;
50 tree = NULL;
53 void CWidgetProperties::setupWidgetInfo( CWidgetInfoTree *tree )
55 this->tree = tree;
56 buildWidgetList();
57 onListSelectionChanged( 0 );
58 connect( widgetList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onListSelectionChanged( int ) ) );
61 void CWidgetProperties::onListSelectionChanged( int i )
63 if( i < 0 )
64 return;
65 if( i >= widgetList->count() )
66 return;
68 QListWidgetItem *item = widgetList->item( i );
69 setPropsOf( item->text().toUtf8().constData() );
73 void CWidgetProperties::onRemoveWButtonClicked()
75 if( widgetList->count() == 0 )
76 return;
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 )
87 return;
89 tree->removeNode( widgetName.toUtf8().constData() );
90 Q_EMIT treeChanged();
91 widgetPropTree->clear();
92 buildWidgetList();
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 ) )
101 return;
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() )
107 return;
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 )
116 return;
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 )
135 return;
137 if( ( widgetList->currentRow() >= widgetList->count() ) || ( widgetList->currentRow() < 0 ) )
138 return;
140 CWidgetInfoTreeNode *node = tree->findNodeByName( widgetList->currentItem()->text().toUtf8().constData() );
141 if( node == NULL )
142 return;
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()
162 buildWidgetList();
163 Q_EMIT treeChanged();
166 void CWidgetProperties::buildWidgetList()
168 widgetList->clear();
169 widgetNames.clear();
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 );
180 if( node == NULL )
181 return;
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 );