22 #include <QColorDialog> 27 #ifdef ENABLE_MODELTEST 28 #include "modeltest.h" 36 #ifdef ENABLE_MODELTEST 37 new ModelTest( mModel,
this );
62 mModel->
setScheme( scheme, context, baseColor );
87 qSort( rowsToRemove.
begin(), rowsToRemove.
end(), qGreater<int>() );
88 Q_FOREACH (
int row, rowsToRemove )
103 if ( pastedColors.
length() == 0 )
111 for ( ; colorIt != pastedColors.
constEnd(); ++colorIt )
128 Q_FOREACH (
int row, rowsToCopy )
130 colorsToCopy << mModel->
colors().
at( row );
141 if (( event->
key() == Qt::Key_Backspace ||
event->key() == Qt::Key_Delete ) )
152 qSort( rowsToRemove.
begin(), rowsToRemove.
end(), qGreater<int>() );
153 Q_FOREACH (
int row, rowsToRemove )
165 if ( event->
button() == Qt::LeftButton )
168 mDragStartPosition =
event->pos();
175 if (( event->
button() == Qt::LeftButton ) &&
203 if ( importedColors.
length() == 0 )
211 for ( ; colorIt != importedColors.
constEnd(); ++colorIt )
241 , mContext( context )
242 , mBaseColor( baseColor )
247 mColors = scheme->
fetchColors( context, baseColor );
264 if ( !parent.
isValid() && row >= 0 && row < mColors.
size() )
286 return mColors.
size();
309 case Qt::DisplayRole:
314 return namedColor.first;
316 return namedColor.second;
321 case Qt::TextAlignmentRole:
322 return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
335 return flags | Qt::ItemIsDropEnabled;
344 flags = flags | Qt::ItemIsEditable;
346 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
348 return flags | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
388 case Qt::DisplayRole:
393 return tr(
"Color" );
395 return tr(
"Label" );
401 case Qt::TextAlignmentRole:
405 return QVariant( Qt::AlignHCenter | Qt::AlignVCenter );
407 return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
420 return Qt::CopyAction | Qt::MoveAction;
424 return Qt::CopyAction;
437 types <<
"text/plain";
438 types <<
"application/x-color";
439 types <<
"application/x-colorobject-list";
447 QModelIndexList::const_iterator indexIt = indexes.
constBegin();
448 for ( ; indexIt != indexes.constEnd(); ++indexIt )
450 if (( *indexIt ).column() > 0 )
453 colorList << qMakePair( mColors[( *indexIt ).row()].
first, mColors[( *indexIt ).row()].second );
469 if ( action == Qt::IgnoreAction )
482 if ( droppedColors.
length() == 0 )
490 for ( ; colorIt != droppedColors.
constEnd(); ++colorIt )
495 int existingIndex = mColors.
indexOf( color );
496 if ( existingIndex >= 0 )
498 if ( existingIndex < beginRow )
513 for ( ; colorIt != droppedColors.
constEnd(); ++colorIt )
533 mColors = scheme->
fetchColors( mContext, mBaseColor );
549 if ( row >= mColors.
count() )
554 for (
int i = row + count - 1; i >= row; --i )
575 for (
int i = row; i < row + count; ++i )
578 mColors.
insert( i, newColor );
595 int existingIndex = mColors.
indexOf( newColor );
596 if ( existingIndex >= 0 )
625 if ( option.state & QStyle::State_Selected )
628 if ( option.state & QStyle::State_Active )
635 QPalette::Highlight ) ) );
646 QRect rect = option.rect;
648 rect.
setLeft( option.rect.center().x() - 15 );
650 rect.
adjust( 0, 1, 0, 1 );
654 painter->
setPen( Qt::NoPen );
655 if ( color.
alpha() < 255 )
669 const QPixmap& QgsColorSwatchDelegate::transparentBackground()
const 673 if ( transpBkgrd.
isNull() )
683 return QSize( 30, 32 );
689 if ( event->
type() == QEvent::MouseButtonDblClick )
691 if ( !index.
model()->
flags( index ).testFlag( Qt::ItemIsEditable ) )
698 if ( !newColor.isValid() )
703 return model->
setData( index, newColor, Qt::EditRole );
bool insertRow(int row, const QModelIndex &parent)
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.
void setSelectionMode(QAbstractItemView::SelectionMode mode)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void removeSelection()
Removes any selected colors from the list.
void setRenderHint(RenderHint hint, bool on)
Abstract base class for color schemes.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool removeRow(int row, const QModelIndex &parent)
const T & at(int i) const
void setDragDropMode(DragDropMode behavior)
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.
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
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QString tr(const char *sourceText, const char *disambiguation, int n)
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.
bool importColorsFromGpl(QFile &file)
Import colors from a GPL palette file to the list.
void mouseReleaseEvent(QMouseEvent *event) override
virtual bool event(QEvent *e)
int indexOf(const T &value, int from) const
QList< T > fromSet(const QSet< T > &set)
void drawRect(const QRectF &rectangle)
const char * name() const
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
int count(const T &value) const
virtual bool isEditable() const
Returns whether the color scheme is editable.
void mousePressEvent(QMouseEvent *event) override
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
void setPen(const QColor &color)
bool isDirty() const
Returns whether the color scheme model has been modified.
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
bool isDirty() const
Returns whether the color scheme list has been modified.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void setBrush(const QBrush &brush)
virtual QVariant data(const QModelIndex &index, int role) const =0
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void setMimeData(QMimeData *src, Mode mode)
virtual void mouseReleaseEvent(QMouseEvent *event)
QModelIndex createIndex(int row, int column, void *ptr) const
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...
void addColor(const QColor &color, const QString &label=QString())
Adds a color to the list.
void beginInsertRows(const QModelIndex &parent, int first, int last)
virtual bool event(QEvent *event)
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowAlpha=false)
Return a color selection from a color dialog.
const QAbstractItemModel * model() const
void copyColors()
Copies colors from the list to the clipboard.
void insert(int i, const T &value)
static QMimeData * colorListToMimeData(const QgsNamedColorList &colorList, const bool allFormats=true)
Creates mime data from a list of named colors.
virtual void setModel(QAbstractItemModel *model)
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
QStringList mimeTypes() const override
void adjust(int dx1, int dy1, int dx2, int dy2)
QgsColorSchemeList(QWidget *parent=nullptr, QgsColorScheme *scheme=nullptr, const QString &context=QString(), const QColor &baseColor=QColor())
Construct a new color swatch grid.
virtual QModelIndexList selectedIndexes() const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual void mousePressEvent(QMouseEvent *event)
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
const QPoint & pos() const
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setDefaultDropAction(Qt::DropAction dropAction)
const_iterator constEnd() const
const_iterator constBegin() const
void setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate)
virtual void keyPressEvent(QKeyEvent *event)
void setRootIsDecorated(bool show)
void setSize(const QSize &size)
static bool saveColorsToGpl(QFile &file, const QString &paletteName, const QgsNamedColorList &colors)
Exports colors to a gpl GIMP palette file.
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
void setDropIndicatorShown(bool enable)
void setDragEnabled(bool enable)
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.