24 #include <QColorDialog> 28 #include <QFileDialog> 29 #include <QMessageBox> 31 #ifdef ENABLE_MODELTEST 32 #include "modeltest.h" 40 #ifdef ENABLE_MODELTEST 41 new ModelTest( mModel,
this );
46 setItemDelegateForColumn( 0, mSwatchDelegate );
48 setRootIsDecorated(
false );
49 setSelectionMode( QAbstractItemView::ExtendedSelection );
50 setSelectionBehavior( QAbstractItemView::SelectRows );
51 setDragEnabled(
true );
52 setAcceptDrops(
true );
53 setDragDropMode( QTreeView::DragDrop );
54 setDropIndicatorShown(
true );
55 setDefaultDropAction( Qt::CopyAction );
61 mModel->
setScheme( scheme, context, baseColor );
78 Q_FOREACH (
const QModelIndex &index, selectedIndexes() )
83 QList<int> rowsToRemove = QList<int>::fromSet( rows.toSet() );
86 std::sort( rowsToRemove.begin(), rowsToRemove.end(), std::greater<int>() );
87 Q_FOREACH (
int row, rowsToRemove )
89 mModel->removeRow( row );
95 mModel->
addColor( color, label, allowDuplicate );
102 if ( pastedColors.length() == 0 )
109 QgsNamedColorList::const_iterator colorIt = pastedColors.constBegin();
110 for ( ; colorIt != pastedColors.constEnd(); ++colorIt )
119 Q_FOREACH (
const QModelIndex &index, selectedIndexes() )
124 QList<int> rowsToCopy = QList<int>::fromSet( rows.toSet() );
127 Q_FOREACH (
int row, rowsToCopy )
129 colorsToCopy << mModel->
colors().at( row );
134 QApplication::clipboard()->setMimeData( mimeData );
140 QString lastDir = s.
value( QStringLiteral(
"/UI/lastGplPaletteDir" ), QDir::homePath() ).toString();
141 QString filePath = QFileDialog::getOpenFileName(
this, tr(
"Select Palette File" ), lastDir, QStringLiteral(
"GPL (*.gpl);;All files (*.*)" ) );
143 if ( filePath.isEmpty() )
149 QFileInfo fileInfo( filePath );
150 if ( !fileInfo.exists() || !fileInfo.isReadable() )
152 QMessageBox::critical(
nullptr, tr(
"Import Colors" ), tr(
"Error, file does not exist or is not readable." ) );
156 s.
setValue( QStringLiteral(
"/UI/lastGplPaletteDir" ), fileInfo.absolutePath() );
157 QFile file( filePath );
161 QMessageBox::critical(
nullptr, tr(
"Import Colors" ), tr(
"Error, no colors found in palette file." ) );
169 QString lastDir = s.
value( QStringLiteral(
"/UI/lastGplPaletteDir" ), QDir::homePath() ).toString();
170 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Palette file" ), lastDir, QStringLiteral(
"GPL (*.gpl)" ) );
172 if ( fileName.isEmpty() )
178 if ( !fileName.endsWith( QLatin1String(
".gpl" ), Qt::CaseInsensitive ) )
180 fileName += QLatin1String(
".gpl" );
183 QFileInfo fileInfo( fileName );
184 s.
setValue( QStringLiteral(
"/UI/lastGplPaletteDir" ), fileInfo.absolutePath() );
186 QFile file( fileName );
190 QMessageBox::critical(
nullptr, tr(
"Export Colors" ), tr(
"Error writing palette file." ) );
198 if ( ( event->key() == Qt::Key_Backspace ||
event->key() == Qt::Key_Delete ) )
201 Q_FOREACH (
const QModelIndex &index, selectedIndexes() )
206 QList<int> rowsToRemove = QList<int>::fromSet( rows.toSet() );
209 std::sort( rowsToRemove.begin(), rowsToRemove.end(), std::greater<int>() );
210 Q_FOREACH (
int row, rowsToRemove )
212 mModel->removeRow( row );
217 QTreeView::keyPressEvent( event );
222 if ( event->button() == Qt::LeftButton )
225 mDragStartPosition =
event->pos();
227 QTreeView::mousePressEvent( event );
232 if ( ( event->button() == Qt::LeftButton ) &&
233 ( event->pos() - mDragStartPosition ).manhattanLength() <= QApplication::startDragDistance() )
239 if ( selectedIndexes().length() == mModel->
columnCount() )
241 QModelIndex selectedColor = selectedIndexes().at( 0 );
246 QTreeView::mouseReleaseEvent( event );
260 if ( importedColors.length() == 0 )
267 QgsNamedColorList::const_iterator colorIt = importedColors.constBegin();
268 for ( ; colorIt != importedColors.constEnd(); ++colorIt )
301 : QAbstractItemModel( parent )
303 , mContext( context )
304 , mBaseColor( baseColor )
309 mColors = scheme->
fetchColors( context, baseColor );
318 return QModelIndex();
321 if ( !parent.isValid() && row >= 0 && row < mColors.size() )
324 return createIndex( row, column );
328 return QModelIndex();
336 return QModelIndex();
341 if ( !parent.isValid() )
343 return mColors.size();
360 if ( !index.isValid() )
363 QPair< QColor, QString > namedColor = mColors.at( index.row() );
366 case Qt::DisplayRole:
368 switch ( index.column() )
371 return namedColor.first;
373 return namedColor.second;
378 case Qt::TextAlignmentRole:
379 return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
388 Qt::ItemFlags
flags = QAbstractItemModel::flags( index );
390 if ( ! index.isValid() )
392 return flags | Qt::ItemIsDropEnabled;
395 switch ( index.column() )
401 flags = flags | Qt::ItemIsEditable;
403 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
405 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
416 if ( !index.isValid() )
419 if ( index.row() >= mColors.length() )
422 switch ( index.column() )
425 mColors[ index.row()].first = value.value<QColor>();
426 emit dataChanged( index, index );
431 mColors[ index.row()].second = value.toString();
432 emit dataChanged( index, index );
445 case Qt::DisplayRole:
450 return tr(
"Color" );
452 return tr(
"Label" );
458 case Qt::TextAlignmentRole:
462 return QVariant( Qt::AlignHCenter | Qt::AlignVCenter );
464 return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
469 return QAbstractItemModel::headerData( section, orientation, role );
477 return Qt::CopyAction | Qt::MoveAction;
481 return Qt::CopyAction;
489 return QStringList();
493 types << QStringLiteral(
"text/xml" );
494 types << QStringLiteral(
"text/plain" );
495 types << QStringLiteral(
"application/x-color" );
496 types << QStringLiteral(
"application/x-colorobject-list" );
504 QModelIndexList::const_iterator indexIt = indexes.constBegin();
505 for ( ; indexIt != indexes.constEnd(); ++indexIt )
507 if ( ( *indexIt ).column() > 0 )
510 colorList << qMakePair( mColors[( *indexIt ).row()].first, mColors[( *indexIt ).row()].second );
526 if ( action == Qt::IgnoreAction )
531 if ( parent.isValid() )
536 int beginRow = row != -1 ? row :
rowCount( QModelIndex() );
539 if ( droppedColors.length() == 0 )
546 QgsNamedColorList::const_iterator colorIt = droppedColors.constBegin();
547 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
550 QPair< QColor, QString > color = qMakePair( ( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second :
QgsSymbolLayerUtils::colorToName( ( *colorIt ).first ) );
552 int existingIndex = mColors.indexOf( color );
553 if ( existingIndex >= 0 )
555 if ( existingIndex < beginRow )
561 beginRemoveRows( parent, existingIndex, existingIndex );
562 mColors.removeAt( existingIndex );
568 insertRows( beginRow, droppedColors.length(), QModelIndex() );
569 colorIt = droppedColors.constBegin();
570 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
572 QModelIndex colorIdx =
index( beginRow, 0, QModelIndex() );
573 setData( colorIdx, QVariant( ( *colorIt ).first ) );
574 QModelIndex labelIdx =
index( beginRow, 1, QModelIndex() );
590 mColors = scheme->
fetchColors( mContext, mBaseColor );
601 if ( parent.isValid() )
606 if ( row >= mColors.count() )
611 for (
int i = row + count - 1; i >= row; --i )
613 beginRemoveRows( parent, i, i );
614 mColors.removeAt( i );
631 beginInsertRows( QModelIndex(), row, row + count - 1 );
632 for (
int i = row; i < row + count; ++i )
634 QPair< QColor, QString > newColor;
635 mColors.insert( i, newColor );
649 if ( !allowDuplicate )
654 int existingIndex = mColors.indexOf( newColor );
655 if ( existingIndex >= 0 )
657 beginRemoveRows( QModelIndex(), existingIndex, existingIndex );
658 mColors.removeAt( existingIndex );
665 QModelIndex colorIdx =
index( row, 0, QModelIndex() );
666 setData( colorIdx, QVariant( color ) );
667 QModelIndex labelIdx =
index( row, 1, QModelIndex() );
668 setData( labelIdx, QVariant( label ) );
677 : QAbstractItemDelegate( parent )
685 if ( option.state & QStyle::State_Selected )
687 painter->setPen( QPen( Qt::NoPen ) );
688 if ( option.state & QStyle::State_Active )
690 painter->setBrush( QBrush( option.widget->palette().highlight() ) );
694 painter->setBrush( QBrush( option.widget->palette().color( QPalette::Inactive,
695 QPalette::Highlight ) ) );
697 painter->drawRect( option.rect );
700 QColor color = index.model()->data( index, Qt::DisplayRole ).value<QColor>();
701 if ( !color.isValid() )
706 QRect rect = option.rect;
708 const int cornerSize = iconSize / 6;
710 rect.setLeft( option.rect.center().x() - iconSize / 2 );
712 rect.setSize( QSize( iconSize, iconSize ) );
713 rect.adjust( 0, 1, 0, 1 );
716 painter->setRenderHint( QPainter::Antialiasing );
717 painter->setPen( Qt::NoPen );
718 if ( color.alpha() < 255 )
721 QBrush checkBrush = QBrush( transparentBackground() );
722 painter->setBrush( checkBrush );
723 painter->drawRoundedRect( rect, cornerSize, cornerSize );
727 painter->setBrush( color );
728 painter->drawRoundedRect( rect, cornerSize, cornerSize );
732 QPixmap QgsColorSwatchDelegate::transparentBackground()
const 734 static QPixmap sTranspBkgrd;
736 if ( sTranspBkgrd.isNull() )
747 return QSize( iconSize, iconSize * 32 / 30.0 );
753 if ( event->type() == QEvent::MouseButtonDblClick )
755 if ( !index.model()->flags( index ).testFlag( Qt::ItemIsEditable ) )
761 QColor color = index.model()->data( index, Qt::DisplayRole ).value<QColor>();
764 if ( panel && panel->dockMode() )
769 colorWidget->setProperty(
"index", index );
771 panel->openPanel( colorWidget );
776 if ( !newColor.isValid() )
781 return model->setData( index, newColor, Qt::EditRole );
787 void QgsColorSwatchDelegate::colorChanged()
791 QModelIndex index = colorWidget->property(
"index" ).toModelIndex();
792 const_cast< QAbstractItemModel *
>( index.model() )->setData( index, colorWidget->color(), Qt::EditRole );
Qt::DropActions supportedDropActions() const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), bool allowOpacity=false)
Returns a color selection from a color dialog.
bool exportColorsToGpl(QFile &file)
Export colors to a GPL palette file from the list.
void colorSelected(const QColor &color)
Emitted when a color is selected from the list.
QgsNamedColorList colors() const
Returns a list of colors shown in the widget.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void removeSelection()
Removes any selected colors from the list.
static const double UI_SCALE_FACTOR
UI scaling factor.
This class is a composition of two QSettings instances:
QColor baseColor() const
Gets the base color for the color scheme used by the model.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Abstract base class for color schemes.
QString context() const
Gets the current color scheme context for the model.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
static QgsNamedColorList colorListFromMimeData(const QMimeData *data)
Attempts to parse mime data as a list of named colors.
QModelIndex parent(const QModelIndex &index) const override
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 addColor(const QColor &color, const QString &label=QString(), bool allowDuplicate=false)
Adds a color to the list.
void pasteColors()
Pastes colors from clipboard to the list.
QgsColorSchemeModel(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor(), QObject *parent=nullptr)
Constructor.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
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 &name)
Helper to get a theme icon as a pixmap.
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
QgsColorScheme * scheme()
Returns the scheme currently selected in the list.
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file...
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
void mousePressEvent(QMouseEvent *event) override
void showImportColorsDialog()
Displays a file picker dialog allowing users to import colors into the list from a file...
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
bool isDirty() const
Returns whether the color scheme list has been modified.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QgsColorSwatchDelegate(QWidget *parent=nullptr)
bool saveColorsToScheme()
Saves the current colors shown in the list back to a color scheme, if supported by the color scheme...
virtual bool isEditable() const
Returns whether the color scheme is editable.
static bool saveColorsToGpl(QFile &file, const QString &paletteName, const QgsNamedColorList &colors)
Exports colors to a gpl GIMP palette file.
bool isDirty() const
Returns whether the color scheme model has been modified.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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
QgsColorSchemeList(QWidget *parent=nullptr, QgsColorScheme *scheme=nullptr, const QString &context=QString(), const QColor &baseColor=QColor())
Construct a new color swatch grid.
static QMimeData * colorListToMimeData(const QgsNamedColorList &colorList, bool allFormats=true)
Creates mime data from a list of named colors.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void keyPressEvent(QKeyEvent *event) override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void addColor(const QColor &color, const QString &label=QString(), bool allowDuplicate=false)
Add a color to the list.
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the colors for the scheme.
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.