Commit b52b2330 authored by Dirk Wilden's avatar Dirk Wilden

removed EVERY model reset :)

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@6063 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 81a3e9b0
......@@ -134,14 +134,11 @@ bool DataControlPlugin::initializeToolbox(QWidget*& _widget)
view_->setSelectionMode(QAbstractItemView::ExtendedSelection);
connect( model_,SIGNAL(dataChangedInside(BaseObject*,int) ),
this, SLOT( slotDataChanged(BaseObject*,int)) );
connect( model_,SIGNAL(dataChangedInside(int,int,const QVariant&) ),
this, SLOT( slotDataChanged(int,int,const QVariant&)) );
connect( model_ , SIGNAL( modelAboutToBeReset() ),
this , SLOT(slotModelAboutToReset() ) );
connect( model_ , SIGNAL( modelReset() ),
this , SLOT( slotModelResetComplete() ) );
connect( model_,SIGNAL( moveBaseObject(int,int) ),
this, SLOT( slotMoveBaseObject(int,int) ) );
connect( view_,SIGNAL(customContextMenuRequested ( const QPoint & ) ),
this,SLOT(slotCustomContextMenuRequested ( const QPoint & ) ));
......@@ -199,6 +196,16 @@ void DataControlPlugin::slotObjectSelectionChanged( int _identifier )
model_->objectChanged( _identifier );
//check for changes in the tree
BaseObject* object = 0;
if ( PluginFunctions::getObject( _identifier, object) ){
propagateUpwards(object->parent(), 2); // 2 = source-target
if ( object->isGroup() )
propagateDownwards(object, 2); // 2 = source-target
}
emit updateView();
}
......@@ -210,7 +217,19 @@ void DataControlPlugin::slotObjectSelectionChanged( int _identifier )
* @param _identifier id of an object
*/
void DataControlPlugin::slotVisibilityChanged( int _identifier ){
//inform the model
model_->objectChanged( _identifier );
//check for changes in the tree
BaseObject* obj = 0;
if ( PluginFunctions::getObject( _identifier, obj) ){
propagateUpwards(obj->parent(), 1); // 1 = visibilty
if ( obj->isGroup() )
propagateDownwards(obj, 1); // 1 = visibilty
}
}
......@@ -233,8 +252,6 @@ void DataControlPlugin::slotObjectPropertiesChanged( int _identifier ){
*/
void DataControlPlugin::slotObjectUpdated( int _identifier ){
if (_identifier == -1)
model_->objectChanged( _identifier );
}
......@@ -244,8 +261,12 @@ void DataControlPlugin::slotObjectUpdated( int _identifier ){
*
* @param _id id of an object
*/
void DataControlPlugin::fileOpened(int){
model_->objectChanged(-1);
void DataControlPlugin::fileOpened(int _id){
BaseObject* obj = 0;
if ( PluginFunctions::getObject(_id, obj) )
model_->objectAdded(obj);
}
......@@ -255,8 +276,18 @@ void DataControlPlugin::fileOpened(int){
*
* @param _id id of an object
*/
void DataControlPlugin::addedEmptyObject(int){
model_->objectChanged(-1);
void DataControlPlugin::addedEmptyObject(int _id){
fileOpened(_id);
}
//******************************************************************************
/** \brief an object was deleted. delete it internally
*
* @param _id id of the object
*/
void DataControlPlugin::objectDeleted(int _id){
model_->objectDeleted(_id);
}
//******************************************************************************
......@@ -294,94 +325,81 @@ void DataControlPlugin::slotKeyEvent( QKeyEvent* _event )
/** \brief emit the right updates when the model changed
*
* @param topLeft index in the model
* @param
* @param _column hmm
*/
void DataControlPlugin::slotDataChanged ( BaseObject* _obj, int _column)
void DataControlPlugin::slotDataChanged ( int _id, int _column, const QVariant& _value)
{
if (_obj != 0){
// std::cerr << "slotDataChanged obj " << _obj->id() << " column " << _column << std::endl;
switch ( _column ) {
// Name
case 0:
emit objectPropertiesChanged( _obj->id() );
view_->expandToDepth(0);
break;
// show/hide
case 1:
emit visibilityChanged( _obj->id() );
emit updateView();
break;
//get the corresponding baseObject
BaseObject* obj = 0;
if ( !PluginFunctions::getObject( _id, obj) )
return;
// source
case 2:
emit objectSelectionChanged( _obj->id() );
break;
switch ( _column ) {
// Name
case 0:
obj->setName( _value.toString() );
emit objectPropertiesChanged( obj->id() );
break;
// show/hide
case 1:
obj->visible( _value.toBool() );
emit visibilityChanged( obj->id() );
break;
// source
case 2:
obj->source( _value.toBool() );
emit objectSelectionChanged( obj->id() );
break;
// target
case 3:
obj->target( _value.toBool() );
emit objectSelectionChanged( obj->id() );
break;
// target
case 3:
emit objectSelectionChanged( _obj->id() );
break;
default:
break;
}
default:
break;
}
}
//******************************************************************************
/** \brief Store the expanded status of all objects when the model wants to reset
*
/** \brief Gets called when an object was moved via drag n drop
*
* @param _id id of the object
* @param _parentId id of the new parent
*/
void DataControlPlugin::slotModelAboutToReset(){
isExpanded_.clear();
QVector< BaseObject* > stack;
stack.push_back( PluginFunctions::objectRoot() );
BaseObject* item;
do{ // Store the expanded state of all objects
item = stack.front();
stack.pop_front();
for(int i=0; i < item->childCount(); i++)
stack.push_back( item->child(i) );
isExpanded_[ item ] = view_->isExpanded( model_->getModelIndex(item, 0 ) );
void DataControlPlugin::slotMoveBaseObject(int _id, int _newParentId){
} while ( !stack.isEmpty() );
BaseObject* obj = 0;
}
if ( !PluginFunctions::getObject(_id, obj) )
return;
BaseObject* parent = 0;
//******************************************************************************
if ( !PluginFunctions::getObject(_newParentId, parent) )
return;
/** \brief restore the expanded status of all objects after reset
*
*/
void DataControlPlugin::slotModelResetComplete(){
BaseObject* oldParent = obj->parent();
// first expandAll so that alle ModelIndices in the TreeModel are recreated
view_->expandAll();
//remove from old parent
oldParent->removeChild( obj );
// and then restore the expanded state
std::map< BaseObject*, bool>::iterator it;
//set new parent
obj->setParent( parent );
parent->appendChild( obj );
for ( it=isExpanded_.begin() ; it != isExpanded_.end(); it++ ){
QModelIndex index = model_->getModelIndex( (*it).first, 0 );
// and inform everyone that the parent changed
emit objectPropertiesChanged( _id );
if (index.isValid())
view_->setExpanded( index, (*it).second);
}
//if oldParent is an empty group -> delete it
if ( oldParent != PluginFunctions::objectRoot() && oldParent->childCount() == 0 )
emit deleteObject( oldParent->id() );
}
......@@ -407,6 +425,9 @@ void DataControlPlugin::loadIniFileOptionsLast( INIFile& _ini ) {
// Get the primary group names to the file
_ini.get_entry(rootGroup,"Groups","rootGroup");
//list of groups
QVector< BaseObject* > groups;
// Go over one level of the groups
while ( rootGroup.size() > 0 ) {
QString current = rootGroup[0];
......@@ -440,6 +461,16 @@ void DataControlPlugin::loadIniFileOptionsLast( INIFile& _ini ) {
parentItem->appendChild(group);
group->setParent(parentItem);
emit emptyObjectAdded( group->id() );
// in the groups vector we only need the lowest groups
// because they are used recursively
int p = groups.indexOf( group->parent() );
if ( p > -1 )
groups.remove( p );
groups.push_back( group );
}
// process children
......@@ -449,11 +480,17 @@ void DataControlPlugin::loadIniFileOptionsLast( INIFile& _ini ) {
childItem->parent()->removeChild(childItem);
childItem->setParent(group);
group->appendChild(childItem);
//inform everyone that the parent changed
emit objectPropertiesChanged( childItem->id() );
}
}
}
emit updatedObject(-1);
for (int i=0; i < groups.count(); i++){
propagateUpwards( groups[i], 1);
propagateUpwards( groups[i], 2);
}
}
......@@ -528,5 +565,114 @@ void DataControlPlugin::saveIniFileOptions( INIFile& _ini ) {
}
//******************************************************************************
/** \brief Recursively update a column up to the root of the tree
*
* @param _obj object to start with
*/
void DataControlPlugin::propagateUpwards(BaseObject* _obj, int _column ){
if ( _obj == PluginFunctions::objectRoot() || (!_obj->isGroup()) )
return;
QList< BaseObject* > children = _obj->getLeafs();
bool changed = false;
bool value = false;
bool value2 = false;
switch ( _column ){
case 1: //VISIBILTY
for (int i=0; i < children.size(); i++)
value |= children[i]->visible();
_obj->visible( value );
changed = true;
break;
case 2: //SOURCE-TARGET
for (int i=0; i < children.size(); i++){
value |= children[i]->source();
value2 |= children[i]->target();
}
if (_obj->source() != value){
_obj->source( value );
changed = true;
}
if (_obj->target() != value2){
_obj->target( value2 );
changed = true;
}
break;
default:
break;
}
if ( changed )
propagateUpwards( _obj->parent(), _column );
}
//******************************************************************************
/** \brief Recursively update a column up to the root of the tree
*
* @param _obj object to start with
*/
void DataControlPlugin::propagateDownwards(BaseObject* _obj, int _column ){
for (int i=0; i < _obj->childCount(); i++){
BaseObject* current = _obj->child(i);
bool changed = false;
switch ( _column ){
case 1: //VISIBILTY
if ( current->visible() != _obj->visible() ){
current->visible( _obj->visible() );
changed = true;
}
break;
case 2: //SOURCE-TARGET
if ( current->source() != _obj->source() ){
current->source( _obj->source() );
changed = true;
}
if ( current->target() != _obj->target() ){
current->target( _obj->target() );
changed = true;
}
break;
default:
break;
}
if ( changed && current->isGroup() ){
propagateDownwards(current, _column);
}
}
}
Q_EXPORT_PLUGIN2( datacontrolplugin , DataControlPlugin );
......@@ -104,6 +104,7 @@ class DataControlPlugin : public QObject, BaseInterface, ToolboxInterface, KeyIn
// LoadSaveInterface
void fileOpened(int _id);
void addedEmptyObject(int _id);
void objectDeleted(int _id);
public :
/// Destructor
......@@ -149,14 +150,14 @@ class DataControlPlugin : public QObject, BaseInterface, ToolboxInterface, KeyIn
private slots:
/// Gets called when the data in the table has changed
void slotDataChanged ( BaseObject* _obj, int _column );
void slotDataChanged (int _id, int _column, const QVariant& _value);
void slotModelAboutToReset();
void slotModelResetComplete();
/// Gets called when an object was moved via drag n drop
void slotMoveBaseObject (int _id, int _newParentId);
private:
std::map< BaseObject*, bool > isExpanded_;
void propagateDownwards(BaseObject* _obj, int _column );
void propagateUpwards(BaseObject* _obj, int _column );
/** @} */
......
......@@ -468,7 +468,7 @@ void DataControlPlugin::showAll() {
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, type); o_it != PluginFunctions::objectsEnd(); ++o_it){
o_it->show();
visibilityChanged( o_it->id() );
emit visibilityChanged( o_it->id() );
}
emit updateView();
......
......@@ -50,6 +50,7 @@
*
*/
void DataControlPlugin::slotPopupRemove ( ) {
QItemSelectionModel* selection = view_->selectionModel();
if (selection == 0) return;
......@@ -65,10 +66,9 @@ void DataControlPlugin::slotPopupRemove ( ) {
QString text;
for ( int i = 0 ; i < indexList.size() ; ++i) {
BaseObject* deleteItem = model_->getItem( indexList[i] );
text += deleteItem->name() + "\n";
}
for ( int i = 0 ; i < indexList.size() ; ++i)
text += model_->itemName( indexList[i] ) + "\n";
msgBox.setDetailedText(text);
if ( msgBox.exec() == QMessageBox::No)
......@@ -76,24 +76,14 @@ void DataControlPlugin::slotPopupRemove ( ) {
for ( int i = 0 ; i < indexList.size() ; ++i) {
BaseObject* deleteItem = model_->getItem( indexList[i] );
// Skip the root item
if ( model_->isRoot( deleteItem ) )
continue;
int id = model_->itemId( indexList[i] );
// remove the whole subtree below this item
deleteItem->deleteSubtree();
// remove the item itself from the parent
deleteItem->parent()->removeChild(deleteItem);
// delete it
delete deleteItem;
if ( id != -1 )
emit deleteObject( id );
}
emit updateView();
emit updatedObject(-1);
}
......@@ -108,20 +98,29 @@ void DataControlPlugin::slotUngroup ( ) {
// Get all selected rows
QModelIndexList indexList = selection->selectedRows ( 0 );
//remove complete group if selected item was a group
BaseObject* group = model_->getItem( indexList[0]);
//get the object
int id = model_->itemId( indexList[0] );
BaseObject* group = 0;
for (int i=0; i < group->childCount(); i++){
group->child(i)->setParent(group->parent());
group->parent()->appendChild( group->child(i) );
}
if ( id > 0 && PluginFunctions::getObject(id, group ) ){
//iterate over children
for (int i=group->childCount()-1; i >= 0; i--){
BaseObject* child = group->child(i);
group->parent()->removeChild(group);
// remove it from the old parent
group->removeChild( child );
delete group;
// then change the parent
child->setParent(group->parent());
child->parent()->appendChild( child );
// and inform everyone that the parent changed
emit objectPropertiesChanged( child->id() );
}
//because the parent of all items in the group changed
emit objectPropertiesChanged( -1 );
//delete the group
emit deleteObject( group->id() );
}
}
......@@ -140,11 +139,14 @@ void DataControlPlugin::slotCopy() {
for ( int i = 0 ; i < indexList.size() ; ++i) {
BaseObject* copyItem = model_->getItem( indexList[i] );
int id = model_->itemId( indexList[i] );
if ( id > 0){
int newObject;
int newObject;
emit copyObject( copyItem->id(), newObject );
emit copyObject( id, newObject );
}
}
......@@ -158,6 +160,7 @@ void DataControlPlugin::slotCopy() {
*
*/
void DataControlPlugin::slotGroup() {
QItemSelectionModel* selection = view_->selectionModel();
// Get all selected rows
......@@ -165,22 +168,39 @@ void DataControlPlugin::slotGroup() {
//check if all objects have the same parent
//abort if the parents differ
bool visible = (model_->getItem( indexList[0]))->visible();
bool target = (model_->getItem( indexList[0]))->target();
bool source = (model_->getItem( indexList[0]))->source();
BaseObject* parent = (model_->getItem( indexList[0]))->parent();
int id = model_->itemId( indexList[0] );
BaseObject* obj0 = 0;
if (id == -1 || !PluginFunctions::getObject(id, obj0) ){
std::cerr << "Group Objects: Unable to get object" << std::endl;
return;
}
bool visible = obj0->visible();
bool target = obj0->target();
bool source = obj0->source();
BaseObject* parent = obj0->parent();
for ( int i = 1 ; i < indexList.size() ; ++i) {
BaseObject* item = model_->getItem( indexList[i] );
if (parent != item->parent()){
//get an object
id = model_->itemId( indexList[i] );
BaseObject* item = 0;
PluginFunctions::getObject(id, item );
//compare parent
if ( parent != item->parent() ){
emit log("Cannot group Objects with different parents");
return;
}
//remember if at least on child was target/source
visible |= (model_->getItem( indexList[i]))->visible();
target |= (model_->getItem( indexList[i]))->target();
source |= (model_->getItem( indexList[i]))->source();
visible |= item->visible();
target |= item->target();
source |= item->source();
}
//create new group
......@@ -191,12 +211,24 @@ void DataControlPlugin::slotGroup() {
parent->appendChild( dynamic_cast< BaseObject* >( groupItem ) );
groupItem->setParent( parent );
emit emptyObjectAdded( groupItem->id() );
//append new children to group
for ( int i = 0 ; i < indexList.size() ; ++i) {
BaseObject* item = model_->getItem( indexList[i] );
//get an object
id = model_->itemId( indexList[i] );
BaseObject* item = 0;
PluginFunctions::getObject(id, item );
//and move it into the group
item->parent()->removeChild(item);