26#include <QColorDialog>
34#include "moc_qgscolorschemelist.cpp"
36using namespace Qt::StringLiterals;
38#ifdef ENABLE_MODELTEST
47#ifdef ENABLE_MODELTEST
48 new ModelTest( mModel,
this );
53 setItemDelegateForColumn( 0, mSwatchDelegate );
55 setRootIsDecorated(
false );
56 setSelectionMode( QAbstractItemView::ExtendedSelection );
57 setSelectionBehavior( QAbstractItemView::SelectRows );
58 setDragEnabled(
true );
59 setAcceptDrops(
true );
60 setDragDropMode( QTreeView::DragDrop );
61 setDropIndicatorShown(
true );
62 setDefaultDropAction( Qt::CopyAction );
68 mModel->setScheme(
scheme, context, baseColor );
73 if ( !mScheme || !mScheme->isEditable() )
78 mScheme->setColors( mModel->colors(), mModel->context(), mModel->baseColor() );
85 const auto constSelectedIndexes = selectedIndexes();
86 for (
const QModelIndex &index : constSelectedIndexes )
91 QList<int> rowsToRemove = qgis::setToList( qgis::listToSet( rows ) );
94 std::sort( rowsToRemove.begin(), rowsToRemove.end(), std::greater<int>() );
95 const auto constRowsToRemove = rowsToRemove;
96 for (
const int row : constRowsToRemove )
98 mModel->removeRow( row );
104 mModel->addColor( color, label, allowDuplicate );
111 if ( pastedColors.length() == 0 )
118 QgsNamedColorList::const_iterator colorIt = pastedColors.constBegin();
119 for ( ; colorIt != pastedColors.constEnd(); ++colorIt )
128 const auto constSelectedIndexes = selectedIndexes();
129 for (
const QModelIndex &index : constSelectedIndexes )
134 const QList<int> rowsToCopy = qgis::setToList( qgis::listToSet( rows ) );
137 const auto constRowsToCopy = rowsToCopy;
138 for (
const int row : constRowsToCopy )
140 colorsToCopy << mModel->colors().at( row );
145 QApplication::clipboard()->setMimeData( mimeData.release() );
151 const QString lastDir = s.
value( u
"/UI/lastGplPaletteDir"_s, QDir::homePath() ).toString();
152 const QString filePath = QFileDialog::getOpenFileName(
this, tr(
"Select Palette File" ), lastDir, u
"GPL (*.gpl);;All files (*.*)"_s );
154 if ( filePath.isEmpty() )
160 const QFileInfo fileInfo( filePath );
161 if ( !fileInfo.exists() || !fileInfo.isReadable() )
163 QMessageBox::critical(
nullptr, tr(
"Import Colors" ), tr(
"Error, file does not exist or is not readable." ) );
167 s.
setValue( u
"/UI/lastGplPaletteDir"_s, fileInfo.absolutePath() );
168 QFile file( filePath );
172 QMessageBox::critical(
nullptr, tr(
"Import Colors" ), tr(
"Error, no colors found in palette file." ) );
180 const QString lastDir = s.
value( u
"/UI/lastGplPaletteDir"_s, QDir::homePath() ).toString();
181 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Palette file" ), lastDir, u
"GPL (*.gpl)"_s );
183 if ( fileName.isEmpty() )
189 if ( !fileName.endsWith(
".gpl"_L1, Qt::CaseInsensitive ) )
191 fileName +=
".gpl"_L1;
194 const QFileInfo fileInfo( fileName );
195 s.
setValue( u
"/UI/lastGplPaletteDir"_s, fileInfo.absolutePath() );
197 QFile file( fileName );
201 QMessageBox::critical(
nullptr, tr(
"Export Colors" ), tr(
"Error writing palette file." ) );
209 if ( ( event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete ) )
212 const auto constSelectedIndexes = selectedIndexes();
213 for (
const QModelIndex &index : constSelectedIndexes )
218 QList<int> rowsToRemove = qgis::setToList( qgis::listToSet( rows ) );
221 std::sort( rowsToRemove.begin(), rowsToRemove.end(), std::greater<int>() );
222 const auto constRowsToRemove = rowsToRemove;
223 for (
const int row : constRowsToRemove )
225 mModel->removeRow( row );
230 QTreeView::keyPressEvent( event );
235 if ( event->button() == Qt::LeftButton )
238 mDragStartPosition =
event->pos();
240 QTreeView::mousePressEvent( event );
245 if ( ( event->button() == Qt::LeftButton ) && ( event->pos() - mDragStartPosition ).manhattanLength() <= QApplication::startDragDistance() )
251 if ( selectedIndexes().length() == mModel->columnCount() )
253 const QModelIndex selectedColor = selectedIndexes().at( 0 );
254 emit
colorSelected( mModel->colors().at( selectedColor.row() ).first );
258 QTreeView::mouseReleaseEvent( event );
272 if ( importedColors.length() == 0 )
279 QgsNamedColorList::const_iterator colorIt = importedColors.constBegin();
280 for ( ; colorIt != importedColors.constEnd(); ++colorIt )
300 return mModel->isDirty();
313 : QAbstractItemModel(
parent )
329 return QModelIndex();
332 if ( !
parent.isValid() && row >= 0 && row < mColors.size() )
335 return createIndex( row, column );
339 return QModelIndex();
347 return QModelIndex();
354 return mColors.size();
371 if ( !
index.isValid() )
374 const QPair<QColor, QString> namedColor = mColors.at(
index.row() );
377 case Qt::DisplayRole:
379 switch (
index.column() )
382 return namedColor.first;
384 return namedColor.second;
389 case Qt::TextAlignmentRole:
390 return static_cast<Qt::Alignment::Int
>( Qt::AlignLeft | Qt::AlignVCenter );
399 Qt::ItemFlags
flags = QAbstractItemModel::flags(
index );
401 if ( !
index.isValid() )
403 return flags | Qt::ItemIsDropEnabled;
406 switch (
index.column() )
410 if ( mScheme && mScheme->isEditable() )
414 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
416 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
424 if ( !mScheme || !mScheme->isEditable() )
427 if ( !
index.isValid() )
430 if (
index.row() >= mColors.length() )
433 switch (
index.column() )
436 mColors[
index.row()].first = value.value<QColor>();
442 mColors[
index.row()].second = value.toString();
456 case Qt::DisplayRole:
461 return tr(
"Color" );
463 return tr(
"Label" );
469 case Qt::TextAlignmentRole:
473 return static_cast<Qt::Alignment::Int
>( Qt::AlignHCenter | Qt::AlignVCenter );
475 return static_cast<Qt::Alignment::Int
>( Qt::AlignLeft | Qt::AlignVCenter );
480 return QAbstractItemModel::headerData( section, orientation, role );
486 if ( mScheme && mScheme->isEditable() )
488 return Qt::CopyAction | Qt::MoveAction;
492 return Qt::CopyAction;
498 if ( !mScheme || !mScheme->isEditable() )
500 return QStringList();
504 types << u
"text/xml"_s;
505 types << u
"text/plain"_s;
506 types << u
"application/x-color"_s;
507 types << u
"application/x-colorobject-list"_s;
515 QModelIndexList::const_iterator indexIt = indexes.constBegin();
516 for ( ; indexIt != indexes.constEnd(); ++indexIt )
518 if ( ( *indexIt ).column() > 0 )
521 colorList << qMakePair( mColors[( *indexIt ).row()].first, mColors[( *indexIt ).row()].second );
532 if ( !mScheme || !mScheme->isEditable() )
537 if ( action == Qt::IgnoreAction )
547 int beginRow = row != -1 ? row :
rowCount( QModelIndex() );
550 if ( droppedColors.length() == 0 )
557 QgsNamedColorList::const_iterator colorIt = droppedColors.constBegin();
558 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
561 const QPair<QColor, QString> color = qMakePair( ( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second :
QgsSymbolLayerUtils::colorToName( ( *colorIt ).first ) );
563 const int existingIndex = mColors.indexOf( color );
564 if ( existingIndex >= 0 )
566 if ( existingIndex < beginRow )
572 beginRemoveRows(
parent, existingIndex, existingIndex );
573 mColors.removeAt( existingIndex );
579 insertRows( beginRow, droppedColors.length(), QModelIndex() );
580 colorIt = droppedColors.constBegin();
581 for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
583 const QModelIndex colorIdx =
index( beginRow, 0, QModelIndex() );
584 setData( colorIdx, QVariant( ( *colorIt ).first ) );
585 const QModelIndex labelIdx =
index( beginRow, 1, QModelIndex() );
601 mColors = scheme->
fetchColors( mContext, mBaseColor );
607 if ( !mScheme || !mScheme->isEditable() )
617 if ( row >= mColors.count() )
622 for (
int i = row + count - 1; i >= row; --i )
624 beginRemoveRows(
parent, i, i );
625 mColors.removeAt( i );
637 if ( !mScheme || !mScheme->isEditable() )
642 beginInsertRows( QModelIndex(), row, row + count - 1 );
643 for (
int i = row; i < row + count; ++i )
645 const QPair<QColor, QString> newColor;
646 mColors.insert( i, newColor );
655 if ( !mScheme || !mScheme->isEditable() )
660 if ( !allowDuplicate )
665 const int existingIndex = mColors.indexOf( newColor );
666 if ( existingIndex >= 0 )
668 beginRemoveRows( QModelIndex(), existingIndex, existingIndex );
669 mColors.removeAt( existingIndex );
676 const QModelIndex colorIdx =
index( row, 0, QModelIndex() );
677 setData( colorIdx, QVariant( color ) );
678 const QModelIndex labelIdx =
index( row, 1, QModelIndex() );
679 setData( labelIdx, QVariant( label ) );
688 : 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 std::unique_ptr< QMimeData > colorListToMimeData(const QgsNamedColorList &colorList, bool allFormats=true)
Creates mime data from a list of named colors.
static QgsNamedColorList colorListFromMimeData(const QMimeData *data)
Attempts to parse mime data as 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.