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" );
402 case Qt::TextAlignmentRole:
406 return QVariant( Qt::AlignHCenter | Qt::AlignVCenter );
408 return QVariant( Qt::AlignLeft | Qt::AlignVCenter );
421 return Qt::CopyAction | Qt::MoveAction;
425 return Qt::CopyAction;
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 )
483 if ( droppedColors.
length() == 0 )
491 for ( ; colorIt != droppedColors.
constEnd(); ++colorIt )
496 int existingIndex = mColors.
indexOf( color );
497 if ( existingIndex >= 0 )
499 if ( existingIndex < beginRow )
514 for ( ; colorIt != droppedColors.
constEnd(); ++colorIt )
534 mColors = scheme->
fetchColors( mContext, mBaseColor );
550 if ( row >= mColors.
count() )
555 for (
int i = row + count - 1; i >= row; --i )
576 for (
int i = row; i < row + count; ++i )
579 mColors.
insert( i, newColor );
596 int existingIndex = mColors.
indexOf( newColor );
597 if ( existingIndex >= 0 )
626 if ( option.state & QStyle::State_Selected )
629 if ( option.state & QStyle::State_Active )
636 QPalette::Highlight ) ) );
647 QRect rect = option.rect;
649 rect.
setLeft( option.rect.center().x() - 15 );
651 rect.
adjust( 0, 1, 0, 1 );
655 painter->
setPen( Qt::NoPen );
656 if ( color.
alpha() < 255 )
670 const QPixmap& QgsColorSwatchDelegate::transparentBackground()
const
674 if ( transpBkgrd.
isNull() )
684 return QSize( 30, 32 );
690 if ( event->
type() == QEvent::MouseButtonDblClick )
692 if ( !index.
model()->
flags( index ).testFlag( Qt::ItemIsEditable ) )
699 if ( !newColor.isValid() )
704 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.
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)
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
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.
QgsColorSwatchDelegate(QWidget *parent=0)
bool importColorsFromGpl(QFile &file)
Import colors from a GPL palette file to the list.
void mouseReleaseEvent(QMouseEvent *event) override
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
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)
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)
virtual QModelIndexList selectedIndexes() const
QgsColorSchemeModel(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor(), QObject *parent=0)
Constructor.
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.