22 #include <QColorDialog>
27 #ifdef ENABLE_MODELTEST
28 #include "modeltest.h"
36 #ifdef ENABLE_MODELTEST
37 new ModelTest( mModel,
this );
42 setItemDelegateForColumn( 0, mSwatchDelegate );
44 setRootIsDecorated(
false );
45 setSelectionMode( QAbstractItemView::ExtendedSelection );
46 setSelectionBehavior( QAbstractItemView::SelectRows );
47 setDragEnabled(
true );
48 setAcceptDrops(
true );
49 setDragDropMode( QTreeView::DragDrop );
50 setDropIndicatorShown(
true );
51 setDefaultDropAction( Qt::CopyAction );
62 mModel->
setScheme( scheme, context, baseColor );
79 foreach (
const QModelIndex &
index, selectedIndexes() )
84 QList<int> rowsToRemove = QList<int>::fromSet( rows.toSet() );
87 qSort( rowsToRemove.begin(), rowsToRemove.end(), qGreater<int>() );
88 foreach (
const int row, rowsToRemove )
90 mModel->removeRow( row );
103 if ( pastedColors.length() == 0 )
110 QgsNamedColorList::const_iterator colorIt = pastedColors.constBegin();
111 for ( ; colorIt != pastedColors.constEnd(); ++colorIt )
120 foreach (
const QModelIndex &
index, selectedIndexes() )
125 QList<int> rowsToCopy = QList<int>::fromSet( rows.toSet() );
128 foreach (
const int row, rowsToCopy )
130 colorsToCopy << mModel->
colors().at( row );
135 QApplication::clipboard()->setMimeData( mimeData );
141 if (( event->key() == Qt::Key_Backspace ||
event->key() == Qt::Key_Delete ) )
144 foreach (
const QModelIndex &
index, selectedIndexes() )
149 QList<int> rowsToRemove = QList<int>::fromSet( rows.toSet() );
152 qSort( rowsToRemove.begin(), rowsToRemove.end(), qGreater<int>() );
153 foreach (
const int row, rowsToRemove )
155 mModel->removeRow( row );
165 if ( event->button() == Qt::LeftButton )
168 mDragStartPosition =
event->pos();
175 if (( event->button() == Qt::LeftButton ) &&
176 ( event->pos() - mDragStartPosition ).manhattanLength() <= QApplication::startDragDistance() )
182 if ( selectedIndexes().length() == mModel->
columnCount() )
184 QModelIndex selectedColor = selectedIndexes().at( 0 );
203 if ( importedColors.length() == 0 )
210 QgsNamedColorList::const_iterator colorIt = importedColors.constBegin();
211 for ( ; colorIt != importedColors.constEnd(); ++colorIt )
239 : QAbstractItemModel( parent )
241 , mContext( context )
242 , mBaseColor( baseColor )
247 mColors = scheme->
fetchColors( context, baseColor );
261 return QModelIndex();
264 if ( !parent.isValid() && row >= 0 && row < mColors.size() )
267 return createIndex( row, column );
271 return QModelIndex();
279 return QModelIndex();
284 if ( !parent.isValid() )
286 return mColors.size();
303 if ( !index.isValid() )
306 QPair< QColor, QString > namedColor = mColors.at( index.row() );
309 case Qt::DisplayRole:
311 switch ( index.column() )
314 return namedColor.first;
316 return namedColor.second;
321 case Qt::TextAlignmentRole:
322 return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
333 if ( ! index.isValid() )
335 return flags | Qt::ItemIsDropEnabled;
338 switch ( index.column() )
344 flags = flags | Qt::ItemIsEditable;
346 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
348 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
359 if ( !index.isValid() )
362 if ( index.row() >= mColors.length() )
365 switch ( index.column() )
368 mColors[ index.row()].first = value.value<QColor>();
369 emit dataChanged( index, index );
374 mColors[ index.row()].second = value.toString();
375 emit dataChanged( index, index );
388 case Qt::DisplayRole:
393 return tr(
"Color" );
395 return tr(
"Label" );
402 case Qt::TextAlignmentRole:
406 return QVariant( Qt::AlignHCenter | Qt::AlignVCenter );
408 return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
421 return Qt::CopyAction | Qt::MoveAction;
425 return Qt::CopyAction;
433 return QStringList();
438 types <<
"text/plain";
439 types <<
"application/x-color";
440 types <<
"application/x-colorobject-list";
448 QModelIndexList::const_iterator indexIt = indexes.constBegin();
449 for ( ; indexIt != indexes.constEnd(); ++indexIt )
451 if (( *indexIt ).column() > 0 )
454 colorList << qMakePair( mColors[( *indexIt ).row()].first, mColors[( *indexIt ).row()].second );
470 if ( action == Qt::IgnoreAction )
475 if ( parent.isValid() )
480 int beginRow = row != -1 ? row :
rowCount( QModelIndex() );
483 if ( droppedColors.length() == 0 )
490 QgsNamedColorList::const_iterator colorIt = droppedColors.constBegin();
491 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
494 QPair< QColor, QString > color = qMakePair(( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second :
QgsSymbolLayerV2Utils::colorToName(( *colorIt ).first ) );
496 int existingIndex = mColors.indexOf( color );
497 if ( existingIndex >= 0 )
499 if ( existingIndex < beginRow )
505 beginRemoveRows( parent, existingIndex, existingIndex );
506 mColors.removeAt( existingIndex );
512 insertRows( beginRow, droppedColors.length(), QModelIndex() );
513 colorIt = droppedColors.constBegin();
514 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
516 QModelIndex colorIdx =
index( beginRow, 0, QModelIndex() );
517 setData( colorIdx, QVariant(( *colorIt ).first ) );
518 QModelIndex labelIdx =
index( beginRow, 1, QModelIndex() );
534 mColors = scheme->
fetchColors( mContext, mBaseColor );
545 if ( parent.isValid() )
550 if ( row >= mColors.count() )
555 for (
int i = row + count - 1; i >= row; --i )
557 beginRemoveRows( parent, i, i );
558 mColors.removeAt( i );
575 beginInsertRows( QModelIndex(), row, row + count - 1 );
576 for (
int i = row; i < row + count; ++i )
578 QPair< QColor, QString > newColor;
579 mColors.insert( i, newColor );
596 int existingIndex = mColors.indexOf( newColor );
597 if ( existingIndex >= 0 )
599 beginRemoveRows( QModelIndex(), existingIndex, existingIndex );
600 mColors.removeAt( existingIndex );
606 QModelIndex colorIdx =
index( row, 0, QModelIndex() );
607 setData( colorIdx, QVariant( color ) );
608 QModelIndex labelIdx =
index( row, 1, QModelIndex() );
609 setData( labelIdx, QVariant( label ) );
618 : QAbstractItemDelegate( parent )
626 if ( option.state & QStyle::State_Selected )
628 painter->setPen( QPen( Qt::NoPen ) );
629 if ( option.state & QStyle::State_Active )
631 painter->setBrush( QBrush( QPalette().highlight() ) );
635 painter->setBrush( QBrush( QPalette().color( QPalette::Inactive,
636 QPalette::Highlight ) ) );
638 painter->drawRect( option.rect );
641 QColor color = index.model()->data( index, Qt::DisplayRole ).value<QColor>();
642 if ( !color.isValid() )
647 QRect rect = option.rect;
649 rect.setLeft( option.rect.center().x() - 15 );
650 rect.setSize( QSize( 30, 30 ) );
651 rect.adjust( 0, 1, 0, 1 );
654 painter->setRenderHint( QPainter::Antialiasing );
655 painter->setPen( Qt::NoPen );
656 if ( color.alpha() < 255 )
659 QBrush checkBrush = QBrush( transparentBackground() );
660 painter->setBrush( checkBrush );
661 painter->drawRoundedRect( rect, 5, 5 );
665 painter->setBrush( color );
666 painter->drawRoundedRect( rect, 5, 5 );
670 const QPixmap& QgsColorSwatchDelegate::transparentBackground()
const
672 static QPixmap transpBkgrd;
674 if ( transpBkgrd.isNull() )
684 return QSize( 30, 32 );
690 if ( event->type() == QEvent::MouseButtonDblClick )
692 if ( !index.model()->flags( index ).testFlag( Qt::ItemIsEditable ) )
697 QColor color = index.model()->data( index, Qt::DisplayRole ).value<QColor>();
699 if ( !newColor.isValid() )
704 return model->setData( index, newColor, Qt::EditRole );