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 );
160 QTreeView::keyPressEvent( event );
165 if ( event->button() == Qt::LeftButton )
168 mDragStartPosition =
event->pos();
170 QTreeView::mousePressEvent( event );
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 );
189 QTreeView::mouseReleaseEvent( event );
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 );
331 Qt::ItemFlags
flags = QAbstractItemModel::flags( index );
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 );
413 return QAbstractItemModel::headerData( section, orientation, role );
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 );
Qt::DropActions supportedDropActions() const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual ~QgsColorSchemeList()
bool exportColorsToGpl(QFile &file)
Export colors to a GPL palette file from the list.
void addColor(const QColor &color, const QString &label=QString())
Add a color to the list.
void colorSelected(const QColor &color)
Emitted when a color is selected from the list.
QString context() const
Get the current color scheme context for the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void removeSelection()
Removes any selected colors from the list.
Abstract base class for color schemes.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex parent(const QModelIndex &index) const override
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
void setScheme(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the color scheme to show in the widget.
Qt::ItemFlags flags(const QModelIndex &index) const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
void setScheme(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the color scheme to show in the list.
void pasteColors()
Pastes colors from clipboard to the list.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
QgsColorSchemeList(QWidget *parent=0, QgsColorScheme *scheme=0, const QString &context=QString(), const QColor &baseColor=QColor())
Construct a new color swatch grid.
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
QgsColorSwatchDelegate(QWidget *parent=0)
bool importColorsFromGpl(QFile &file)
Import colors from a GPL palette file to the list.
void mouseReleaseEvent(QMouseEvent *event) override
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
virtual bool isEditable() const
Returns whether the color scheme is editable.
void mousePressEvent(QMouseEvent *event) override
static QMimeData * colorListToMimeData(const QgsNamedColorList colorList, const bool allFormats=true)
Creates mime data from a list of named colors.
static bool saveColorsToGpl(QFile &file, const QString paletteName, QgsNamedColorList colors)
Exports colors to a gpl GIMP palette file.
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
bool isDirty() const
Returns whether the color scheme model has been modified.
bool isDirty() const
Returns whether the color scheme list has been modified.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool saveColorsToScheme()
Saves the current colors shown in the list back to a color scheme, if supported by the color scheme...
void addColor(const QColor &color, const QString &label=QString())
Adds a color to the list.
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowAlpha=false)
Return a color selection from a color dialog.
void copyColors()
Copies colors from the list to the clipboard.
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
QStringList mimeTypes() const override
QgsColorSchemeModel(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor(), QObject *parent=0)
Constructor.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QgsNamedColorList colors() const
Returns a list of colors shown in the widget.
void keyPressEvent(QKeyEvent *event) override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the colors for the scheme.
QColor baseColor() const
Get the base color for the color scheme used by the model.
A delegate for showing a color swatch in a list.
QMimeData * mimeData(const QModelIndexList &indexes) const override
A model for colors in a color scheme.
static QgsNamedColorList colorListFromMimeData(const QMimeData *data)
Attempts to parse mime data as a list of named colors.