33 #include <QMessageBox>
34 #include <QStandardItemModel>
35 #include <QStandardItem>
43 , mMimeFormat(
"application/x-qgsgraduatedsymbolrendererv2model" )
51 beginRemoveRows( QModelIndex(), 0, mRenderer->
ranges().size() - 1 );
57 beginInsertRows( QModelIndex(), 0, renderer->
ranges().size() - 1 );
65 if ( !mRenderer )
return;
66 int idx = mRenderer->
ranges().size();
67 beginInsertRows( QModelIndex(), idx, idx );
78 int idx = mRenderer->
ranges().size();
79 beginInsertRows( QModelIndex(), idx, idx );
86 if ( !index.isValid() || !mRenderer || mRenderer->
ranges().size() <= index.row() )
91 return mRenderer->
ranges().value( index.row() );
96 if ( !index.isValid() )
98 return Qt::ItemIsDropEnabled;
101 Qt::ItemFlags
flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
103 if ( index.column() == 2 )
105 flags |= Qt::ItemIsEditable;
113 return Qt::MoveAction;
118 if ( !index.isValid() || !mRenderer )
return QVariant();
122 if ( role == Qt::CheckStateRole && index.column() == 0 )
124 return range.
renderState() ? Qt::Checked : Qt::Unchecked;
126 else if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
128 switch ( index.column() )
133 if ( decimalPlaces < 0 ) decimalPlaces = 0;
134 return QString::number( range.
lowerValue(),
'f', decimalPlaces ) +
" - " + QString::number( range.
upperValue(),
'f', decimalPlaces );
136 case 2:
return range.
label();
137 default:
return QVariant();
140 else if ( role == Qt::DecorationRole && index.column() == 0 && range.
symbol() )
144 else if ( role == Qt::TextAlignmentRole )
146 return ( index.column() == 0 ) ? Qt::AlignHCenter : Qt::AlignLeft;
148 else if ( role == Qt::EditRole )
150 switch ( index.column() )
153 case 2:
return range.
label();
154 default:
return QVariant();
163 if ( !index.isValid() )
166 if ( index.column() == 0 && role == Qt::CheckStateRole )
169 emit dataChanged( index, index );
173 if ( role != Qt::EditRole )
176 switch ( index.column() )
188 emit dataChanged( index, index );
194 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
196 QStringList lst; lst <<
tr(
"Symbol" ) <<
tr(
"Values" ) <<
tr(
"Legend" );
197 return lst.value( section );
204 if ( parent.isValid() || !mRenderer )
208 return mRenderer->
ranges().size();
219 if ( hasIndex( row, column, parent ) )
221 return createIndex( row, column );
223 return QModelIndex();
229 return QModelIndex();
235 types << mMimeFormat;
241 QMimeData *
mimeData =
new QMimeData();
242 QByteArray encodedData;
244 QDataStream stream( &encodedData, QIODevice::WriteOnly );
247 foreach (
const QModelIndex &
index, indexes )
249 if ( !index.isValid() || index.column() != 0 )
252 stream << index.row();
254 mimeData->setData( mMimeFormat, encodedData );
262 if ( action != Qt::MoveAction )
return true;
264 if ( !data->hasFormat( mMimeFormat ) )
return false;
266 QByteArray encodedData = data->data( mMimeFormat );
267 QDataStream stream( &encodedData, QIODevice::ReadOnly );
270 while ( !stream.atEnd() )
277 int to = parent.row();
280 if ( to == -1 ) to = mRenderer->
ranges().size();
281 for (
int i = rows.size() - 1; i >= 0; i-- )
283 QgsDebugMsg( QString(
"move %1 to %2" ).arg( rows[i] ).arg( to ) );
286 if ( rows[i] < t ) t--;
289 for (
int j = 0; j < i; j++ )
291 if ( to < rows[j] && rows[i] > rows[j] ) rows[j] += 1;
294 if ( rows[i] < to ) to--;
296 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->
ranges().size(), 0 ) );
303 for (
int i = rows.size() - 1; i >= 0; i-- )
305 beginRemoveRows( QModelIndex(), rows[i], rows[i] );
313 beginRemoveRows( QModelIndex(), 0, mRenderer->
ranges().size() - 1 );
329 else if ( column == 2 )
334 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->
ranges().size(), 0 ) );
340 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->
ranges().size(), 0 ) );
345 emit dataChanged( createIndex( 0, 2 ), createIndex( mRenderer->
ranges().size(), 2 ) );
350 : QProxyStyle( style )
355 if ( element == QStyle::PE_IndicatorItemViewItemDrop && !option->rect.isNull() )
357 QStyleOption opt( *option );
358 opt.rect.setLeft( 0 );
360 opt.rect.setHeight( 0 );
361 if ( widget ) opt.rect.setRight( widget->width() );
398 mExpressionWidget->setLayer(
mLayer );
400 cboGraduatedColorRamp->populate(
mStyle );
407 if ( defaultColorRamp !=
"" )
409 int index = cboGraduatedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
411 cboGraduatedColorRamp->setCurrentIndex( index );
419 connect( mExpressionWidget, SIGNAL( fieldChanged( QString ) ),
this, SLOT(
graduatedColumnChanged( QString ) ) );
420 connect( viewGraduated, SIGNAL( doubleClicked(
const QModelIndex & ) ),
this, SLOT(
rangesDoubleClicked(
const QModelIndex & ) ) );
421 connect( viewGraduated, SIGNAL( clicked(
const QModelIndex & ) ),
this, SLOT(
rangesClicked(
const QModelIndex & ) ) );
422 connect( viewGraduated, SIGNAL( customContextMenuRequested(
const QPoint& ) ),
this, SLOT(
contextMenuViewCategories(
const QPoint& ) ) );
424 connect( btnGraduatedClassify, SIGNAL( clicked() ),
this, SLOT(
classifyGraduated() ) );
426 connect( btnGraduatedDelete, SIGNAL( clicked() ),
this, SLOT(
deleteClasses() ) );
427 connect( btnDeleteAllClasses, SIGNAL( clicked() ),
this, SLOT(
deleteAllClasses() ) );
428 connect( btnGraduatedAdd, SIGNAL( clicked() ),
this, SLOT(
addClass() ) );
429 connect( cbxLinkBoundaries, SIGNAL( toggled(
bool ) ),
this, SLOT(
toggleBoundariesLink(
bool ) ) );
437 QMenu* advMenu =
new QMenu;
446 btnAdvanced->setMenu( advMenu );
464 connect( spinGraduatedClasses, SIGNAL( valueChanged(
int ) ),
this, SLOT(
classifyGraduated() ) );
465 connect( cboGraduatedMode, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT(
classifyGraduated() ) );
466 connect( cboGraduatedColorRamp, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT(
reapplyColorRamp() ) );
467 connect( cbxInvertedColorRamp, SIGNAL( toggled(
bool ) ),
this, SLOT(
reapplyColorRamp() ) );
468 connect( spinPrecision, SIGNAL( valueChanged(
int ) ),
this, SLOT(
labelFormatChanged() ) );
469 connect( cbxTrimTrailingZeroes, SIGNAL( toggled(
bool ) ),
this, SLOT(
labelFormatChanged() ) );
470 connect( txtFormat, SIGNAL( textChanged( QString ) ),
this, SLOT(
labelFormatChanged() ) );
480 disconnect( spinGraduatedClasses, SIGNAL( valueChanged(
int ) ),
this, SLOT(
classifyGraduated() ) );
481 disconnect( cboGraduatedMode, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT(
classifyGraduated() ) );
482 disconnect( cboGraduatedColorRamp, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT(
reapplyColorRamp() ) );
483 disconnect( cbxInvertedColorRamp, SIGNAL( toggled(
bool ) ),
this, SLOT(
reapplyColorRamp() ) );
484 disconnect( spinPrecision, SIGNAL( valueChanged(
int ) ),
this, SLOT(
labelFormatChanged() ) );
485 disconnect( cbxTrimTrailingZeroes, SIGNAL( toggled(
bool ) ),
this, SLOT(
labelFormatChanged() ) );
486 disconnect( txtFormat, SIGNAL( textChanged( QString ) ),
this, SLOT(
labelFormatChanged() ) );
489 disconnect(
mModel, SIGNAL( dataChanged( QModelIndex, QModelIndex ) ),
this, SLOT(
modelDataChanged() ) );
504 if ( nclasses && updateCount )
509 mExpressionWidget->setField( attrName );
527 txtFormat->setText( labelFormat.
format() );
528 spinPrecision->setValue( labelFormat.
precision() );
532 viewGraduated->setModel(
mModel );
533 viewGraduated->resizeColumnToContents( 0 );
534 viewGraduated->resizeColumnToContents( 1 );
535 viewGraduated->resizeColumnToContents( 2 );
547 QString attrName = mExpressionWidget->currentField();
549 int nclasses = spinGraduatedClasses->value();
551 QSharedPointer<QgsVectorColorRampV2> ramp( cboGraduatedColorRamp->currentColorRamp() );
554 if ( cboGraduatedColorRamp->count() == 0 )
555 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
557 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"The selected color ramp is not available." ) );
562 if ( cboGraduatedMode->currentIndex() == 0 )
564 else if ( cboGraduatedMode->currentIndex() == 2 )
566 else if ( cboGraduatedMode->currentIndex() == 3 )
568 else if ( cboGraduatedMode->currentIndex() == 4 )
577 if ( QMessageBox::Cancel == QMessageBox::question(
this,
tr(
"Warning" ),
tr(
"Natural break classification (Jenks) is O(n2) complexity, your classification may take a long time.\nPress cancel to abort breaks calculation or OK to continue." ), QMessageBox::Cancel, QMessageBox::Ok ) )
587 QApplication::setOverrideCursor( Qt::WaitCursor );
590 QApplication::restoreOverrideCursor();
609 QItemSelectionModel* m = viewGraduated->selectionModel();
610 QModelIndexList i = m->selectedRows();
611 if ( m && i.size() > 0 )
637 btnChangeGraduatedSymbol->setIcon( icon );
641 int QgsRendererV2PropertiesDialog::currentRangeRow()
643 QModelIndex idx = viewGraduated->selectionModel()->currentIndex();
644 if ( !idx.isValid() )
653 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
655 foreach ( QModelIndex r, selectedRows )
659 rows.append( r.row() );
668 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
669 QModelIndexList::const_iterator sIt = selectedRows.constBegin();
671 for ( ; sIt != selectedRows.constEnd(); ++sIt )
680 if ( idx.isValid() && idx.column() == 0 )
682 if ( idx.isValid() && idx.column() == 1 )
688 if ( !idx.isValid() )
696 QItemSelectionModel* m = viewGraduated->selectionModel();
697 QModelIndexList selectedIndexes = m->selectedRows( 1 );
698 if ( m && selectedIndexes.size() > 0 )
708 foreach ( QModelIndex idx, selectedIndexes )
712 int rangeIdx = idx.row();
744 if ( decimalPlaces < 0 ) decimalPlaces = 0;
748 if ( dialog.exec() == QDialog::Accepted )
750 double lowerValue = dialog.
lowerValue().toDouble();
751 double upperValue = dialog.
upperValue().toDouble();
756 if ( cbxLinkBoundaries->isChecked() )
763 if ( rangeIdx < mRenderer->ranges().
size() - 1 )
791 for (
int i = 1;i < ranges.size();++i )
793 if ( ranges[i] < ranges[i-1] )
810 int result = QMessageBox::warning(
812 tr(
"Linked range warning" ),
813 tr(
"Rows will be reordered before linking boundaries. Continue?" ),
814 QMessageBox::Ok | QMessageBox::Cancel );
815 if ( result != QMessageBox::Ok )
817 cbxLinkBoundaries->setChecked(
false );
834 if ( item->column() == 2 )
836 QString label = item->text();
837 int idx = item->row();
861 spinPrecision->value(),
862 cbxTrimTrailingZeroes->isChecked() );
872 QItemSelectionModel* m = viewGraduated->selectionModel();
873 QModelIndexList selectedIndexes = m->selectedRows( 1 );
874 if ( m && selectedIndexes.size() > 0 )
877 QModelIndexList::const_iterator indexIt = selectedIndexes.constBegin();
878 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
880 QStringList list = m->model()->data( *indexIt ).toString().split(
" " );
881 if ( list.size() < 3 )
886 double lowerBound = list.at( 0 ).toDouble();
887 double upperBound = list.at( 2 ).toDouble();
891 selectedSymbols.append( s );
900 for ( QgsRangeList::const_iterator it = ranges.begin(); it != ranges.end(); ++it )
926 viewGraduated->selectionModel()->clear();
929 cbxLinkBoundaries->setChecked(
false );
944 if ( event->key() == Qt::Key_C &&
event->modifiers() == Qt::ControlModifier )
949 else if ( event->key() == Qt::Key_V &&
event->modifiers() == Qt::ControlModifier )
951 QgsRangeList::const_iterator rIt =
mCopyBuffer.constBegin();