47 #include <KDABLibFakes>
50 using namespace KDChart;
53 class AttributesModel::Private
68 AttributesModel::Private::Private()
71 palette(
Palette::defaultPalette() )
100 if ( mapA.count() != mapB.count() ) {
105 for ( ; itA != mapA.constEnd(); ++itA, ++itB ) {
106 if ( itA->count() != itB->count() ) {
111 for ( ; it2A != itA->constEnd(); ++it2A, ++it2B ) {
112 if ( it2A.key() != it2B.key() ) {
125 if ( other ==
this ) {
128 if ( !other ||
d->paletteType != other->d->
paletteType ) {
133 if (
d->dataMap.count() != other->d->dataMap.count() ) {
138 for ( ; itA !=
d->dataMap.constEnd(); ++itA, ++itB ) {
139 if ( itA->count() != itB->count() ) {
144 for ( ; it2A != itA->constEnd(); ++it2A, ++it2B ) {
145 if ( it2A->count() != it2B->count() ) {
150 for ( ; it3A != it2A->constEnd(); ++it3A, ++it3B ) {
151 if ( it3A.key() != it3B.key() ) {
162 if ( !compareHeaderDataMaps(
d->horizontalHeaderDataMap, other->d->horizontalHeaderDataMap ) ||
163 !compareHeaderDataMaps(
d->verticalHeaderDataMap, other->d->verticalHeaderDataMap ) ) {
168 if (
d->modelDataMap.count() != other->d->modelDataMap.count() ) {
173 for ( ; itA !=
d->modelDataMap.constEnd(); ++itA, ++itB ) {
174 if ( itA.key() != itB.key() ) {
186 int role,
const QVariant& a,
const QVariant& b )
const
194 return (a.value<QBrush>() ==
197 return (a.value<QPen>() ==
234 return (a.value<
bool>() ==
250 if ( sourceModel() ) {
251 const QVariant sourceData = sourceModel()->headerData( section, orientation, role );
252 if ( sourceData.isValid() ) {
259 d->horizontalHeaderDataMap :
d->verticalHeaderDataMap;
261 if ( mapIt != map.constEnd() ) {
264 if ( dataMapIt != dataMap.constEnd() ) {
265 return dataMapIt.value();
277 const int dataset = section /
d->dataDimension;
280 case Qt::DisplayRole:
282 return QLatin1String( orientation == Qt::Vertical ?
"Series " :
"Item " ) + QString::number( dataset ) ;
284 return d->palette.getBrush( dataset );
289 return QPen( brush.color() );
309 v = defaultsForRole( role );
324 v =
headerData( column, Qt::Horizontal, role );
337 if ( index.isValid() ) {
338 Q_ASSERT( index.model() == this );
340 if ( !sourceModel() ) {
344 if ( index.isValid() ) {
345 const QVariant sourceData = sourceModel()->data(
mapToSource( index ), role );
346 if ( sourceData.isValid() ) {
352 if (
d->dataMap.contains( index.column() ) ) {
354 if ( colDataMap.contains( index.row() ) ) {
356 if ( dataMap.contains( role ) ) {
357 const QVariant v = dataMap[ role ];
365 if ( index.isValid() ) {
366 return data( index.column(), role );
396 QVariant AttributesModel::defaultsForRole(
int role )
const
399 return d->defaultsMap.value( role );
405 return sourceModel()->setData(
mapToSource(index), value, role );
409 dataMap.insert( role, value );
417 return setData( index, QVariant(), role );
421 const QVariant & value,
int role )
423 if ( sourceModel() &&
headerData( section, orientation, role ) == value ) {
428 return sourceModel()->setHeaderData( section, orientation, value, role );
431 = orientation == Qt::Horizontal ?
d->horizontalHeaderDataMap :
d->verticalHeaderDataMap;
434 dataMap.insert( role, value );
435 if ( sourceModel() ) {
436 int numRows =
rowCount( QModelIndex() );
438 if ( orientation == Qt::Horizontal && numRows > 0 )
440 index( numRows - 1, section, QModelIndex() ) );
441 else if ( orientation == Qt::Vertical && numCols > 0 )
443 index( section, numCols - 1, QModelIndex() ) );
444 emit headerDataChanged( orientation, section, section );
448 if ( section != -1 && numRows > 0 )
449 emit dataChanged(
index( 0, section, QModelIndex() ),
450 index( numRows - 1, section, QModelIndex() ) );
458 return setHeaderData ( section, orientation, QVariant(), role );
463 if (
d->paletteType == type ) {
466 d->paletteType = type;
478 qWarning(
"Unknown palette type!" );
484 return d->paletteType;
489 d->modelDataMap.insert( role, value );
490 int numRows = rowCount( QModelIndex() );
491 int numCols = columnCount( QModelIndex() );
492 if ( sourceModel() && numRows > 0 && numCols > 0 ) {
493 emit attributesChanged( index( 0, 0, QModelIndex() ),
494 index( numRows - 1, numCols - 1, QModelIndex() ) );
503 return d->modelDataMap.value( role, QVariant() );
508 if ( sourceModel() ) {
509 return sourceModel()->rowCount(
mapToSource(index) );
517 if ( sourceModel() ) {
518 return sourceModel()->columnCount(
mapToSource(index) );
526 if ( this->sourceModel() != 0 )
528 disconnect( this->sourceModel(), SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex&)),
529 this, SLOT( slotDataChanged(
const QModelIndex&,
const QModelIndex&)));
530 disconnect( this->sourceModel(), SIGNAL( rowsInserted(
const QModelIndex&,
int,
int ) ),
531 this, SLOT( slotRowsInserted(
const QModelIndex&,
int,
int ) ) );
532 disconnect( this->sourceModel(), SIGNAL( rowsRemoved(
const QModelIndex&,
int,
int ) ),
533 this, SLOT( slotRowsRemoved(
const QModelIndex&,
int,
int ) ) );
534 disconnect( this->sourceModel(), SIGNAL( rowsAboutToBeInserted(
const QModelIndex&,
int,
int ) ),
535 this, SLOT( slotRowsAboutToBeInserted(
const QModelIndex&,
int,
int ) ) );
536 disconnect( this->sourceModel(), SIGNAL( rowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ),
537 this, SLOT( slotRowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ) );
538 disconnect( this->sourceModel(), SIGNAL( columnsInserted(
const QModelIndex&,
int,
int ) ),
539 this, SLOT( slotColumnsInserted(
const QModelIndex&,
int,
int ) ) );
540 disconnect( this->sourceModel(), SIGNAL( columnsRemoved(
const QModelIndex&,
int,
int ) ),
541 this, SLOT( slotColumnsRemoved(
const QModelIndex&,
int,
int ) ) );
542 disconnect( this->sourceModel(), SIGNAL( columnsAboutToBeInserted(
const QModelIndex&,
int,
int ) ),
543 this, SLOT( slotColumnsAboutToBeInserted(
const QModelIndex&,
int,
int ) ) );
544 disconnect( this->sourceModel(), SIGNAL( columnsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ),
545 this, SLOT( slotColumnsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ) );
546 disconnect( this->sourceModel(), SIGNAL( modelReset() ),
547 this, SIGNAL( modelReset() ) );
548 disconnect( this->sourceModel(), SIGNAL( layoutChanged() ),
549 this, SIGNAL( layoutChanged() ) );
551 QAbstractProxyModel::setSourceModel( sourceModel );
552 if ( this->sourceModel() != NULL )
554 connect( this->sourceModel(), SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex&)),
555 this, SLOT( slotDataChanged(
const QModelIndex&,
const QModelIndex&)));
556 connect( this->sourceModel(), SIGNAL( rowsInserted(
const QModelIndex&,
int,
int ) ),
557 this, SLOT( slotRowsInserted(
const QModelIndex&,
int,
int ) ) );
558 connect( this->sourceModel(), SIGNAL( rowsRemoved(
const QModelIndex&,
int,
int ) ),
559 this, SLOT( slotRowsRemoved(
const QModelIndex&,
int,
int ) ) );
560 connect( this->sourceModel(), SIGNAL( rowsAboutToBeInserted(
const QModelIndex&,
int,
int ) ),
561 this, SLOT( slotRowsAboutToBeInserted(
const QModelIndex&,
int,
int ) ) );
562 connect( this->sourceModel(), SIGNAL( rowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ),
563 this, SLOT( slotRowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ) );
564 connect( this->sourceModel(), SIGNAL( columnsInserted(
const QModelIndex&,
int,
int ) ),
565 this, SLOT( slotColumnsInserted(
const QModelIndex&,
int,
int ) ) );
566 connect( this->sourceModel(), SIGNAL( columnsRemoved(
const QModelIndex&,
int,
int ) ),
567 this, SLOT( slotColumnsRemoved(
const QModelIndex&,
int,
int ) ) );
568 connect( this->sourceModel(), SIGNAL( columnsAboutToBeInserted(
const QModelIndex&,
int,
int ) ),
569 this, SLOT( slotColumnsAboutToBeInserted(
const QModelIndex&,
int,
int ) ) );
570 connect( this->sourceModel(), SIGNAL( columnsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ),
571 this, SLOT( slotColumnsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ) );
572 connect( this->sourceModel(), SIGNAL( modelReset() ),
573 this, SIGNAL( modelReset() ) );
574 connect( this->sourceModel(), SIGNAL( layoutChanged() ),
575 this, SIGNAL( layoutChanged() ) );
579 void AttributesModel::slotRowsAboutToBeInserted(
const QModelIndex& parent,
int start,
int end )
584 void AttributesModel::slotColumnsAboutToBeInserted(
const QModelIndex& parent,
int start,
int end )
589 void AttributesModel::slotRowsInserted(
const QModelIndex& parent,
int start,
int end )
597 void AttributesModel::slotColumnsInserted(
const QModelIndex& parent,
int start,
int end )
605 void AttributesModel::slotRowsAboutToBeRemoved(
const QModelIndex& parent,
int start,
int end )
610 void AttributesModel::slotColumnsAboutToBeRemoved(
const QModelIndex& parent,
int start,
int end )
615 void AttributesModel::slotRowsRemoved(
const QModelIndex& parent,
int start,
int end )
623 void AttributesModel::removeEntriesFromDataMap(
int start,
int end )
627 if ( it !=
d->dataMap.end() ) {
630 for (
int i = start; i < end && it !=
d->dataMap.end(); ++i ) {
631 d->dataMap[ i ] = it.value();
632 indexesToDel << it.key();
635 if ( indexesToDel.isEmpty() ) {
636 for (
int i = start; i < end; ++i ) {
640 for (
int i = 0; i < indexesToDel.count(); ++i ) {
641 d->dataMap.remove( indexesToDel[ i ] );
646 void AttributesModel::removeEntriesFromDirectionDataMaps( Qt::Orientation dir,
int start,
int end )
649 = dir == Qt::Horizontal ?
d->horizontalHeaderDataMap :
d->verticalHeaderDataMap;
652 if ( it != sectionDataMap.end() )
655 for (
int i = start; i < end && it != sectionDataMap.end(); ++i )
657 sectionDataMap[ i ] = it.value();
658 indexesToDel << it.key();
661 if ( indexesToDel.isEmpty() )
663 for (
int i = start; i < end; ++i )
668 for (
int i = 0; i < indexesToDel.count(); ++i )
670 sectionDataMap.remove( indexesToDel[ i ] );
675 void AttributesModel::slotColumnsRemoved(
const QModelIndex& parent,
int start,
int end )
680 Q_ASSERT_X( sourceModel(),
"removeColumn",
"This should only be triggered if a valid source Model exists!" );
681 for (
int i = start; i <= end; ++i ) {
682 d->verticalHeaderDataMap.remove( start );
684 removeEntriesFromDataMap( start, end );
685 removeEntriesFromDirectionDataMaps( Qt::Horizontal, start, end );
686 removeEntriesFromDirectionDataMaps( Qt::Vertical, start, end );
691 void AttributesModel::slotDataChanged(
const QModelIndex& topLeft,
const QModelIndex& bottomRight )
698 if ( value.isValid() ) {
699 d->defaultsMap.insert( role, value );
703 if ( it !=
d->defaultsMap.end() ) {
704 d->defaultsMap.erase( it );
714 d->dataDimension = dimension;
719 return d->dataDimension;