30
30
#include < QVBoxLayout>
31
31
#include < QMessageBox>
32
32
33
+ // #include "modeltest.h"
34
+
33
35
QgsRendererV2Widget* QgsRuleBasedRendererV2Widget::create ( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer )
34
36
{
35
37
return new QgsRuleBasedRendererV2Widget ( layer, style, renderer );
@@ -59,6 +61,7 @@ QgsRuleBasedRendererV2Widget::QgsRuleBasedRendererV2Widget( QgsVectorLayer* laye
59
61
setupUi ( this );
60
62
61
63
mModel = new QgsRuleBasedRendererV2Model ( mRenderer );
64
+ // new ModelTest( mModel, this ); // for model validity checking
62
65
viewRules->setModel ( mModel );
63
66
64
67
mRefineMenu = new QMenu ( btnRefineRule );
@@ -152,8 +155,10 @@ void QgsRuleBasedRendererV2Widget::editRule( const QModelIndex& index )
152
155
void QgsRuleBasedRendererV2Widget::removeRule ()
153
156
{
154
157
QItemSelection sel = viewRules->selectionModel ()->selection ();
158
+ QgsDebugMsg ( QString (" REMOVE RULES!!! ranges: %1" ).arg ( sel.count ()) );
155
159
foreach ( QItemSelectionRange range, sel )
156
160
{
161
+ QgsDebugMsg ( QString ( " RANGE: r %1 - %2" ).arg (range.top ()).arg (range.bottom ()) );
157
162
if ( range.isValid () )
158
163
mModel ->removeRows ( range.top (), range.bottom () - range.top () + 1 , range.parent () );
159
164
}
@@ -471,9 +476,12 @@ Qt::ItemFlags QgsRuleBasedRendererV2Model::flags( const QModelIndex &index ) con
471
476
if ( !index .isValid () )
472
477
return Qt::ItemIsDropEnabled;
473
478
479
+ // allow drop only at first column
480
+ Qt::ItemFlag drop = (index .column () == 0 ? Qt::ItemIsDropEnabled : Qt::NoItemFlags);
481
+
474
482
return Qt::ItemIsEnabled | Qt::ItemIsSelectable |
475
483
Qt::ItemIsEditable |
476
- Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled ;
484
+ Qt::ItemIsDragEnabled | drop ;
477
485
}
478
486
479
487
QVariant QgsRuleBasedRendererV2Model::data ( const QModelIndex &index, int role ) const
@@ -545,16 +553,13 @@ int QgsRuleBasedRendererV2Model::columnCount( const QModelIndex & ) const
545
553
546
554
QModelIndex QgsRuleBasedRendererV2Model::index ( int row, int column, const QModelIndex &parent ) const
547
555
{
548
- if ( !hasIndex ( row, column, parent ) )
549
- return QModelIndex ();
550
-
551
- QgsRuleBasedRendererV2::Rule* parentRule = ruleForIndex ( parent );
552
-
553
- QgsRuleBasedRendererV2::Rule* childRule = parentRule->children ()[row];
554
- if ( childRule )
556
+ if ( hasIndex ( row, column, parent ) )
557
+ {
558
+ QgsRuleBasedRendererV2::Rule* parentRule = ruleForIndex ( parent );
559
+ QgsRuleBasedRendererV2::Rule* childRule = parentRule->children ()[row];
555
560
return createIndex ( row, column, childRule );
556
- else
557
- return QModelIndex ();
561
+ }
562
+ return QModelIndex ();
558
563
}
559
564
560
565
QModelIndex QgsRuleBasedRendererV2Model::parent ( const QModelIndex &index ) const
@@ -568,7 +573,8 @@ QModelIndex QgsRuleBasedRendererV2Model::parent( const QModelIndex &index ) cons
568
573
if ( parentRule == mR ->rootRule () )
569
574
return QModelIndex ();
570
575
571
- int row = parentRule->children ().indexOf ( childRule );
576
+ // this is right: we need to know row number of our parent (in our grandparent)
577
+ int row = parentRule->parent ()->children ().indexOf ( parentRule );
572
578
573
579
return createIndex ( row, 0 , parentRule );
574
580
}
@@ -654,7 +660,7 @@ bool QgsRuleBasedRendererV2Model::dropMimeData( const QMimeData *data,
654
660
if ( !data->hasFormat ( " application/vnd.text.list" ) )
655
661
return false ;
656
662
657
- if ( column > 0 )
663
+ if ( parent. column () > 0 )
658
664
return false ;
659
665
660
666
QByteArray encodedData = data->data ( " application/vnd.text.list" );
@@ -708,15 +714,21 @@ bool QgsRuleBasedRendererV2Model::removeRows( int row, int count, const QModelIn
708
714
709
715
QgsDebugMsg ( QString ( " Called: row %1 count %2 parent ~~%3~~" ).arg ( row ).arg ( count ).arg ( parentRule->dump () ) );
710
716
711
- emit beginRemoveRows ( parent, row, row + count - 1 );
717
+ beginRemoveRows ( parent, row, row + count - 1 );
712
718
713
719
for ( int i = 0 ; i < count; i++ )
714
720
{
715
- QgsRuleBasedRendererV2::Rule* r = parentRule->children ()[row];
716
- parentRule->removeChild ( r );
721
+ if ( row < parentRule->children ().count () )
722
+ {
723
+ // QgsRuleBasedRendererV2::Rule* r = parentRule->children()[row];
724
+ parentRule->removeChildAt ( row );
725
+ // parentRule->takeChildAt( row );
726
+ }
727
+ else
728
+ QgsDebugMsg ( " trying to remove invalid index - this should not happen!" );
717
729
}
718
730
719
- emit endRemoveRows ();
731
+ endRemoveRows ();
720
732
721
733
return true ;
722
734
}
0 commit comments