25#include <QColorDialog>
33#include "moc_qgscolorschemelist.cpp"
35using namespace Qt::StringLiterals;
37#ifdef ENABLE_MODELTEST
46#ifdef ENABLE_MODELTEST
47 new ModelTest( mModel,
this );
52 setItemDelegateForColumn( 0, mSwatchDelegate );
54 setRootIsDecorated(
false );
55 setSelectionMode( QAbstractItemView::ExtendedSelection );
56 setSelectionBehavior( QAbstractItemView::SelectRows );
57 setDragEnabled(
true );
58 setAcceptDrops(
true );
59 setDragDropMode( QTreeView::DragDrop );
60 setDropIndicatorShown(
true );
61 setDefaultDropAction( Qt::CopyAction );
67 mModel->setScheme(
scheme, context, baseColor );
72 if ( !mScheme || !mScheme->isEditable() )
77 mScheme->setColors( mModel->colors(), mModel->context(), mModel->baseColor() );
84 const auto constSelectedIndexes = selectedIndexes();
85 for (
const QModelIndex &index : constSelectedIndexes )
90 QList<int> rowsToRemove = qgis::setToList( qgis::listToSet( rows ) );
93 std::sort( rowsToRemove.begin(), rowsToRemove.end(), std::greater<int>() );
94 const auto constRowsToRemove = rowsToRemove;
95 for (
const int row : constRowsToRemove )
97 mModel->removeRow( row );
103 mModel->addColor( color, label, allowDuplicate );
110 if ( pastedColors.length() == 0 )
117 QgsNamedColorList::const_iterator colorIt = pastedColors.constBegin();
118 for ( ; colorIt != pastedColors.constEnd(); ++colorIt )
127 const auto constSelectedIndexes = selectedIndexes();
128 for (
const QModelIndex &index : constSelectedIndexes )
133 const QList<int> rowsToCopy = qgis::setToList( qgis::listToSet( rows ) );
136 const auto constRowsToCopy = rowsToCopy;
137 for (
const int row : constRowsToCopy )
139 colorsToCopy << mModel->colors().at( row );
144 QApplication::clipboard()->setMimeData( mimeData );
150 const QString lastDir = s.
value( u
"/UI/lastGplPaletteDir"_s, QDir::homePath() ).toString();
151 const QString filePath = QFileDialog::getOpenFileName(
this, tr(
"Select Palette File" ), lastDir, u
"GPL (*.gpl);;All files (*.*)"_s );
153 if ( filePath.isEmpty() )
159 const QFileInfo fileInfo( filePath );
160 if ( !fileInfo.exists() || !fileInfo.isReadable() )
162 QMessageBox::critical(
nullptr, tr(
"Import Colors" ), tr(
"Error, file does not exist or is not readable." ) );
166 s.
setValue( u
"/UI/lastGplPaletteDir"_s, fileInfo.absolutePath() );
167 QFile file( filePath );
171 QMessageBox::critical(
nullptr, tr(
"Import Colors" ), tr(
"Error, no colors found in palette file." ) );
179 const QString lastDir = s.
value( u
"/UI/lastGplPaletteDir"_s, QDir::homePath() ).toString();
180 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Palette file" ), lastDir, u
"GPL (*.gpl)"_s );
182 if ( fileName.isEmpty() )
188 if ( !fileName.endsWith(
".gpl"_L1, Qt::CaseInsensitive ) )
190 fileName +=
".gpl"_L1;
193 const QFileInfo fileInfo( fileName );
194 s.
setValue( u
"/UI/lastGplPaletteDir"_s, fileInfo.absolutePath() );
196 QFile file( fileName );
200 QMessageBox::critical(
nullptr, tr(
"Export Colors" ), tr(
"Error writing palette file." ) );
208 if ( ( event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete ) )
211 const auto constSelectedIndexes = selectedIndexes();
212 for (
const QModelIndex &index : constSelectedIndexes )
217 QList<int> rowsToRemove = qgis::setToList( qgis::listToSet( rows ) );
220 std::sort( rowsToRemove.begin(), rowsToRemove.end(), std::greater<int>() );
221 const auto constRowsToRemove = rowsToRemove;
222 for (
const int row : constRowsToRemove )
224 mModel->removeRow( row );
229 QTreeView::keyPressEvent( event );
234 if ( event->button() == Qt::LeftButton )
237 mDragStartPosition =
event->pos();
239 QTreeView::mousePressEvent( event );
244 if ( ( event->button() == Qt::LeftButton ) && ( event->pos() - mDragStartPosition ).manhattanLength() <= QApplication::startDragDistance() )
250 if ( selectedIndexes().length() == mModel->columnCount() )
252 const QModelIndex selectedColor = selectedIndexes().at( 0 );
253 emit
colorSelected( mModel->colors().at( selectedColor.row() ).first );
257 QTreeView::mouseReleaseEvent( event );
271 if ( importedColors.length() == 0 )
278 QgsNamedColorList::const_iterator colorIt = importedColors.constBegin();
279 for ( ; colorIt != importedColors.constEnd(); ++colorIt )
299 return mModel->isDirty();
312 : QAbstractItemModel(
parent )
328 return QModelIndex();
331 if ( !
parent.isValid() && row >= 0 && row < mColors.size() )
334 return createIndex( row, column );
338 return QModelIndex();
346 return QModelIndex();
353 return mColors.size();
370 if ( !
index.isValid() )
373 const QPair<QColor, QString> namedColor = mColors.at(
index.row() );
376 case Qt::DisplayRole:
378 switch (
index.column() )
381 return namedColor.first;
383 return namedColor.second;
388 case Qt::TextAlignmentRole:
389 return static_cast<Qt::Alignment::Int
>( Qt::AlignLeft | Qt::AlignVCenter );
398 Qt::ItemFlags
flags = QAbstractItemModel::flags(
index );
400 if ( !
index.isValid() )
402 return flags | Qt::ItemIsDropEnabled;
405 switch (
index.column() )
409 if ( mScheme && mScheme->isEditable() )
413 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
415 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
423 if ( !mScheme || !mScheme->isEditable() )
426 if ( !
index.isValid() )
429 if (
index.row() >= mColors.length() )
432 switch (
index.column() )
435 mColors[
index.row()].first = value.value<QColor>();
441 mColors[
index.row()].second = value.toString();
455 case Qt::DisplayRole:
460 return tr(
"Color" );
462 return tr(
"Label" );
468 case Qt::TextAlignmentRole:
472 return static_cast<Qt::Alignment::Int
>( Qt::AlignHCenter | Qt::AlignVCenter );
474 return static_cast<Qt::Alignment::Int
>( Qt::AlignLeft | Qt::AlignVCenter );
479 return QAbstractItemModel::headerData( section, orientation, role );
485 if ( mScheme && mScheme->isEditable() )
487 return Qt::CopyAction | Qt::MoveAction;
491 return Qt::CopyAction;
497 if ( !mScheme || !mScheme->isEditable() )
499 return QStringList();
503 types << u
"text/xml"_s;
504 types << u
"text/plain"_s;
505 types << u
"application/x-color"_s;
506 types << u
"application/x-colorobject-list"_s;
514 QModelIndexList::const_iterator indexIt = indexes.constBegin();
515 for ( ; indexIt != indexes.constEnd(); ++indexIt )
517 if ( ( *indexIt ).column() > 0 )
520 colorList << qMakePair( mColors[( *indexIt ).row()].first, mColors[( *indexIt ).row()].second );
531 if ( !mScheme || !mScheme->isEditable() )
536 if ( action == Qt::IgnoreAction )
546 int beginRow = row != -1 ? row :
rowCount( QModelIndex() );
549 if ( droppedColors.length() == 0 )
556 QgsNamedColorList::const_iterator colorIt = droppedColors.constBegin();
557 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
560 const QPair<QColor, QString> color = qMakePair( ( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second :
QgsSymbolLayerUtils::colorToName( ( *colorIt ).first ) );
562 const int existingIndex = mColors.indexOf( color );
563 if ( existingIndex >= 0 )
565 if ( existingIndex < beginRow )
571 beginRemoveRows(
parent, existingIndex, existingIndex );
572 mColors.removeAt( existingIndex );
578 insertRows( beginRow, droppedColors.length(), QModelIndex() );
579 colorIt = droppedColors.constBegin();
580 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
582 const QModelIndex colorIdx =
index( beginRow, 0, QModelIndex() );
583 setData( colorIdx, QVariant( ( *colorIt ).first ) );
584 const QModelIndex labelIdx =
index( beginRow, 1, QModelIndex() );
600 mColors = scheme->
fetchColors( mContext, mBaseColor );
606 if ( !mScheme || !mScheme->isEditable() )
616 if ( row >= mColors.count() )
621 for (
int i = row + count - 1; i >= row; --i )
623 beginRemoveRows(
parent, i, i );
624 mColors.removeAt( i );
636 if ( !mScheme || !mScheme->isEditable() )
641 beginInsertRows( QModelIndex(), row, row + count - 1 );
642 for (
int i = row; i < row + count; ++i )
644 const QPair<QColor, QString> newColor;
645 mColors.insert( i, newColor );
654 if ( !mScheme || !mScheme->isEditable() )
659 if ( !allowDuplicate )
664 const int existingIndex = mColors.indexOf( newColor );
665 if ( existingIndex >= 0 )
667 beginRemoveRows( QModelIndex(), existingIndex, existingIndex );
668 mColors.removeAt( existingIndex );
675 const QModelIndex colorIdx =
index( row, 0, QModelIndex() );
676 setData( colorIdx, QVariant( color ) );
677 const QModelIndex labelIdx =
index( row, 1, QModelIndex() );
678 setData( labelIdx, QVariant( label ) );
687 : QAbstractItemDelegate( parent )
694 if ( option.state & QStyle::State_Selected )
696 painter->setPen( QPen( Qt::NoPen ) );
697 if ( option.state & QStyle::State_Active )
699 painter->setBrush( QBrush( option.widget->palette().highlight() ) );
703 painter->setBrush( QBrush( option.widget->palette().color( QPalette::Inactive, QPalette::Highlight ) ) );
705 painter->drawRect( option.rect );
708 QColor color = index.model()->data( index, Qt::DisplayRole ).value<QColor>();
709 if ( !color.isValid() )
714 QRect rect = option.rect;
717 const int cornerSize = iconSize / 6;
719 rect.setLeft( option.rect.center().x() - iconSize / 2 );
721 rect.setSize( QSize( iconSize, iconSize ) );
722 rect.adjust( 0, 1, 0, 1 );
725 painter->setRenderHint( QPainter::Antialiasing );
726 painter->setPen( Qt::NoPen );
727 if ( color.alpha() < 255 )
730 const QBrush checkBrush = QBrush( transparentBackground() );
731 painter->setBrush( checkBrush );
732 painter->drawRoundedRect( rect, cornerSize, cornerSize );
734 painter->setBrush( color );
735 painter->drawRoundedRect( rect, cornerSize, cornerSize );
737 const QRectF clipRect( rect.left(), rect.top(),
static_cast<qreal
>( rect.width() ) / 2.0, rect.height() );
738 painter->setClipRect( clipRect );
739 color.setAlpha( 255 );
740 painter->setBrush( color );
741 painter->drawRoundedRect( rect, cornerSize, cornerSize );
745 painter->setBrush( color );
746 painter->drawRoundedRect( rect, cornerSize, cornerSize );
750QPixmap QgsColorSwatchDelegate::transparentBackground()
const
752 static QPixmap sTranspBkgrd;
754 if ( sTranspBkgrd.isNull() )
765 return QSize( iconSize, iconSize * 32 / 30.0 );
771 if ( event->type() == QEvent::MouseButtonDblClick )
773 if ( !index.model()->flags( index ).testFlag( Qt::ItemIsEditable ) )
779 const QColor color = index.model()->data( index, Qt::DisplayRole ).value<QColor>();
782 if ( panel && panel->dockMode() )
787 colorWidget->setProperty(
"index", index );
789 panel->openPanel( colorWidget );
794 if ( !newColor.isValid() )
799 return model->setData( index, newColor, Qt::EditRole );
805void QgsColorSwatchDelegate::colorChanged()
809 const QModelIndex index = colorWidget->property(
"index" ).toModelIndex();
810 const_cast<QAbstractItemModel *
>( index.model() )->setData( index, colorWidget->color(), Qt::EditRole );
static const double UI_SCALE_FACTOR
UI scaling factor.
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
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 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 removeSelection()
Removes any selected colors from the list.
bool importColorsFromGpl(QFile &file)
Import colors from a GPL palette file to the list.
void copyColors()
Copies colors from the list to the clipboard.
bool isDirty() const
Returns whether the color scheme list has been modified.
void mouseReleaseEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
QgsColorScheme * scheme()
Returns the scheme currently selected in the list.
void addColor(const QColor &color, const QString &label=QString(), bool allowDuplicate=false)
Adds a color to the list.
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file.
void colorSelected(const QColor &color)
Emitted when a color is selected from the list.
void keyPressEvent(QKeyEvent *event) override
QgsColorSchemeList(QWidget *parent=nullptr, QgsColorScheme *scheme=nullptr, const QString &context=QString(), const QColor &baseColor=QColor())
Construct a new color swatch grid.
bool saveColorsToScheme()
Saves the current colors shown in the list back to a color scheme, if supported by the color scheme.
void showImportColorsDialog()
Displays a file picker dialog allowing users to import colors into the list from a file.
A model for colors in a color scheme.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void addColor(const QColor &color, const QString &label=QString(), bool allowDuplicate=false)
Add a color to the list.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QColor baseColor() const
Gets the base color for the color scheme used by the model.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Qt::DropActions supportedDropActions() const override
QModelIndex parent(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Qt::ItemFlags flags(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.
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QMimeData * mimeData(const QModelIndexList &indexes) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QgsColorSchemeModel(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor(), QObject *parent=nullptr)
Constructor.
QStringList mimeTypes() const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
QString context() const
Gets the current color scheme context for the model.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Abstract base class for color schemes.
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
A delegate for showing a color swatch in a list.
QgsColorSwatchDelegate(QWidget *parent=nullptr)
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Scoped object for saving and restoring a QPainter object's state.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static bool saveColorsToGpl(QFile &file, const QString &paletteName, const QgsNamedColorList &colors)
Exports colors to a gpl GIMP palette file.
static QgsNamedColorList colorListFromMimeData(const QMimeData *data)
Attempts to parse mime data as a list of named colors.
static QMimeData * colorListToMimeData(const QgsNamedColorList &colorList, bool allFormats=true)
Creates mime data from a list of named colors.
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette 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.