Skip to content

Commit

Permalink
New GUI control for an editable list of colors from a color scheme.
Browse files Browse the repository at this point in the history
Supports drag and drop reordering, drag and drop colors from external
apps (on supported OS).
  • Loading branch information
nyalldawson committed Aug 16, 2014
1 parent 6312ff5 commit cd54d6a
Show file tree
Hide file tree
Showing 8 changed files with 888 additions and 126 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -36,6 +36,7 @@
%Include qgscolorbuttonv2.sip
%Include qgscolordialog.sip
%Include qgscolorswatchgrid.sip
%Include qgscolorschemelist.sip
%Include qgscomposerview.sip
%Include qgscredentialdialog.sip
%Include qgsdetaileditemdata.sip
Expand Down
140 changes: 140 additions & 0 deletions python/gui/qgscolorschemelist.sip
@@ -0,0 +1,140 @@
/** \ingroup gui
* \class QgsColorSwatchDelegate
* A delegate for showing a color swatch in a list
* @see QgsColorSchemeList
* @note introduced in QGIS 2.5
*/
class QgsColorSwatchDelegate : QAbstractItemDelegate
{
%TypeHeaderCode
#include <qgscolorschemelist.h>
%End

public:
QgsColorSwatchDelegate( QWidget *parent /TransferThis/ = 0 );
void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
QSize sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
bool editorEvent( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index );
};

/** \ingroup gui
* \class QgsColorSchemeModel
* A model for colors in a color scheme
* @see QgsColorSchemeList
* @note introduced in QGIS 2.5
*/
class QgsColorSchemeModel: QAbstractItemModel
{
%TypeHeaderCode
#include <qgscolorschemelist.h>
%End

public:

/**Constructor
* @param scheme color scheme for list
* @param context context string for color scheme
* @param baseColor base color for color scheme
* @param parent parent object
*/
explicit QgsColorSchemeModel( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor(), QObject* parent /TransferThis/ = 0 );

~QgsColorSchemeModel();

//reimplemented QAbstractItemModel methods
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
QModelIndex parent( const QModelIndex &index ) const;
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
Qt::ItemFlags flags( const QModelIndex & index ) const;
bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
Qt::DropActions supportedDropActions() const;
QStringList mimeTypes() const;
bool removeRows( int row, int count, const QModelIndex & parent = QModelIndex() );
bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() );
QMimeData *mimeData( const QModelIndexList &indexes ) const;
bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );

/**Returns a list of colors shown in the widget
* @returns colors shown in the widget
*/
QgsNamedColorList colors() const;

/**Sets the color scheme to show in the widget
* @param scheme color scheme
* @param context context for color scheme
* @param baseColor base color for color scheme
*/
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );

/**Get the current color scheme context for the model
* @returns context string which is passed to scheme for color generation
* @see baseColor
*/
QString context() const;

/**Get the base color for the color scheme used by the model
* @returns base color which is passed to scheme for color generation
* @see context
*/
QColor baseColor() const;

/**Add a color to the list
* @param color color to add
* @param label label for color
*/
void addColor( const QColor color, const QString label = QString() );

};

/** \ingroup gui
* \class QgsColorSchemeList
* An editable list of color swatches, taken from an associated QgsColorScheme.
* @see QgsColorSchemeList
* @note introduced in QGIS 2.5
*/
class QgsColorSchemeList: QTreeView
{
%TypeHeaderCode
#include <qgscolorschemelist.h>
%End

public:

/**Construct a new color swatch grid.
* @param parent parent widget
* @param scheme QgsColorScheme for colors to show in the list
* @param context context string provided to color scheme
* @param baseColor base color for color scheme
*/
QgsColorSchemeList( QWidget *parent /TransferThis/ = 0, QgsColorScheme* scheme = 0, const QString context = QString(), const QColor baseColor = QColor() );

virtual ~QgsColorSchemeList();

/**Sets the color scheme to show in the list
* @param scheme QgsColorScheme for colors to show in the list
* @param context context string provided to color scheme
* @param baseColor base color for color scheme
*/
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );

/**Saves the current colors shown in the list back to a color scheme, if supported
* by the color scheme.
* @note this method is only effective if the color scheme is editable
*/
bool saveColorsToScheme();

/**Removes any selected colors from the list
*/
void removeSelection();

/**Adds a color to the list
* @param color color to add
* @param label optional label for color
*/
void addColor( const QColor color, const QString label = QString() );

};

111 changes: 9 additions & 102 deletions src/app/qgsoptions.cpp
Expand Up @@ -36,6 +36,7 @@
#include "qgsrasterpyramidsoptionswidget.h"
#include "qgsdialog.h"
#include "qgscomposer.h"
#include "qgscolorschemeregistry.h"

#include <QInputDialog>
#include <QFileDialog>
Expand Down Expand Up @@ -687,33 +688,14 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
//
// Color palette
//
QList< QVariant > customColorVariants = settings.value( QString( "/colors/palettecolors" ) ).toList();
QList< QVariant > customColorLabels = settings.value( QString( "/colors/palettelabels" ) ).toList();

QList<QTreeWidgetItem *> customColors;
int colorIndex = 0;
for ( QList< QVariant >::iterator it = customColorVariants.begin();
it != customColorVariants.end(); ++it )
//find custom color scheme from registry
QList<QgsCustomColorScheme *> customSchemes;
QgsColorSchemeRegistry::instance()->schemes( customSchemes );
if ( customSchemes.length() > 0 )
{
QColor color = ( *it ).value<QColor>();
QString label;
if ( customColorLabels.length() > colorIndex )
{
label = customColorLabels.at( colorIndex ).toString();
}

QTreeWidgetItem* item = new QTreeWidgetItem();
item->setData( 0, PaletteLabelRole, label );
item->setText( 1, label );
setPaletteColor( item, color );
customColors.append( item );

colorIndex++;
mTreeCustomColors->setScheme( customSchemes.at( 0 ) );
}
mTreeCustomColors->clear();
mTreeCustomColors->insertTopLevelItems( 0, customColors );
mTreeCustomColors->resizeColumnToContents( 0 );
mTreeCustomColors->setColumnWidth( 0, mTreeCustomColors->columnWidth( 0 ) + 20 );

//
// Composer settings
Expand Down Expand Up @@ -1329,19 +1311,7 @@ void QgsOptions::saveOptions()
//
// Color palette
//
QList< QVariant > customColors;
QList< QVariant > customColorLabels;
int colorCount = mTreeCustomColors->topLevelItemCount();
for ( int i = 0; i < colorCount; i++ )
{
QTreeWidgetItem* item = mTreeCustomColors->topLevelItem( i );
QVariant label = item->data( 0, PaletteLabelRole );
QVariant color = item->data( 0, PaletteColorRole );
customColors.append( color );
customColorLabels.append( label );
}
settings.setValue( QString( "/colors/palettecolors" ), customColors );
settings.setValue( QString( "/colors/palettelabels" ), customColorLabels );
mTreeCustomColors->saveColorsToScheme();

//
// Composer settings
Expand Down Expand Up @@ -2114,73 +2084,10 @@ void QgsOptions::on_mButtonAddColor_clicked()
}
activateWindow();

QString newLabel = QInputDialog::getText( this, tr( "Color label" ),
tr( "Please enter a label for the color" ) );
activateWindow();

QTreeWidgetItem* item = new QTreeWidgetItem();
item->setData( 0, PaletteLabelRole, newLabel );
item->setText( 1, newLabel );
setPaletteColor( item, newColor );
mTreeCustomColors->addTopLevelItem( item );
mTreeCustomColors->resizeColumnToContents( 0 );
mTreeCustomColors->setColumnWidth( 0, mTreeCustomColors->columnWidth( 0 ) + 20 );
mTreeCustomColors->addColor( newColor );
}

void QgsOptions::on_mButtonRemoveColor_clicked()
{
QTreeWidgetItem* item = mTreeCustomColors->currentItem();
if ( !item )
return;
int index = mTreeCustomColors->indexOfTopLevelItem( item );
mTreeCustomColors->takeTopLevelItem( index );
}

void QgsOptions::on_mTreeCustomColors_itemDoubleClicked( QTreeWidgetItem* item, int column )
{
if ( column == 0 )
{
QColor newColor = QColorDialog::getColor( item->data( 0, PaletteColorRole ).value<QColor>(), this->parentWidget(), tr( "Select color" ), QColorDialog::ShowAlphaChannel );
if ( !newColor.isValid() )
{
return;
}
setPaletteColor( item, newColor );
}
else
{
bool ok;
QString label = item->data( 0, PaletteLabelRole ).toString();
QString newLabel = QInputDialog::getText( this, tr( "Color label" ),
tr( "Please enter a label for the color" ),
QLineEdit::Normal, label, &ok );
if ( !ok )
{
return;
}

item->setText( 1, newLabel );
item->setData( 0, PaletteLabelRole, newLabel );
}
}

void QgsOptions::setPaletteColor( QTreeWidgetItem* item, QColor color )
{
QSize iconSize( 16, 16 );
QPixmap pixmap( iconSize );
pixmap.fill( QColor( 0, 0, 0, 0 ) );
QRect rect( 1, 1, iconSize.width() - 2, iconSize.height() - 2 );

// draw a slightly rounded rectangle
QPainter p;
p.begin( &pixmap );
p.setPen( Qt::NoPen );
p.setRenderHint( QPainter::Antialiasing );
p.setBrush( color );
p.drawRoundedRect( rect, 2, 2 );
p.end();

item->setIcon( 0, QIcon( pixmap ) );
item->setData( 0, PaletteColorRole, color );
item->setText( 0, color.name() );
mTreeCustomColors->removeSelection();
}
3 changes: 0 additions & 3 deletions src/app/qgsoptions.h
Expand Up @@ -243,7 +243,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption

void on_mButtonAddColor_clicked();
void on_mButtonRemoveColor_clicked();
void on_mTreeCustomColors_itemDoubleClicked( QTreeWidgetItem* item, int column );

private:
QStringList i18nList();
Expand All @@ -260,8 +259,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption

void saveDefaultDatumTransformations();

void setPaletteColor( QTreeWidgetItem *item, QColor color );

protected:
QgisAppStyleSheet* mStyleSheetBuilder;
QMap<QString, QVariant> mStyleSheetNewOpts;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -128,6 +128,7 @@ qgscolorbutton.cpp
qgscolorbuttonv2.cpp
qgscolordialog.cpp
qgscolorswatchgrid.cpp
qgscolorschemelist.cpp
qgscodeeditor.cpp
qgscodeeditorpython.cpp
qgscodeeditorsql.cpp
Expand Down Expand Up @@ -226,6 +227,7 @@ SET(QGIS_GUI_MOC_HDRS
qgscolorbutton.h
qgscolorbuttonv2.h
qgscolorswatchgrid.h
qgscolorschemelist.h

raster/qgsrasterminmaxwidget.h
raster/qgspalettedrendererwidget.h
Expand Down

0 comments on commit cd54d6a

Please sign in to comment.