Skip to content
Permalink
Browse files

composer legend styles, fixes partialy #6960

  • Loading branch information
blazek committed Mar 26, 2013
1 parent 53c47dd commit ee84bb1f2cbef3e49db4b1cc6dfc9a00713dcb70
@@ -5,6 +5,7 @@ class QgsComposerLegend : QgsComposerItem
{
%TypeHeaderCode
#include <qgscomposerlegend.h>
#include <qgscomposerlegendstyle.h>
%End

public:
@@ -30,33 +31,18 @@ class QgsComposerLegend : QgsComposerItem
void setTitle( const QString& t );
QString title() const;

QFont titleFont() const;
void setTitleFont( const QFont& f );

QFont groupFont() const;
void setGroupFont( const QFont& f );

QFont layerFont() const;
void setLayerFont( const QFont& f );

QFont itemFont() const;
void setItemFont( const QFont& f );
/*
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const;
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style);
QFont styleFont( QgsComposerLegendStyle::Style s ) const;
void setStyleFont( QgsComposerLegendStyle::Style s, const QFont& f );
void setStyleMargin( QgsComposerLegendStyle::Style s, double margin );
void setStyleMargin( QgsComposerLegendStyle::Style s, QgsComposerLegendStyle::Side side, double margin );
*/

double boxSpace() const;
void setBoxSpace( double s );

double groupSpace() const;
void setGroupSpace( double s );

double layerSpace() const;
void setLayerSpace( double s );

double symbolSpace() const;
void setSymbolSpace( double s );

double iconLabelSpace() const;
void setIconLabelSpace( double s );

double symbolWidth() const;
void setSymbolWidth( double w );

@@ -36,6 +36,75 @@

#include <QMessageBox>

QgsComposerLegendWidgetStyleDelegate::QgsComposerLegendWidgetStyleDelegate( QObject *parent ): QItemDelegate( parent )
{
}

QWidget *QgsComposerLegendWidgetStyleDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem & item, const QModelIndex & index ) const
{
Q_UNUSED( item );
Q_UNUSED( index );
QgsDebugMsg( "Entered" );

QComboBox *editor = new QComboBox( parent );
QList<QgsComposerLegendStyle::Style> list;
list << QgsComposerLegendStyle::Group << QgsComposerLegendStyle::Subgroup << QgsComposerLegendStyle::Hidden;
foreach ( QgsComposerLegendStyle::Style s, list )
{
editor->addItem( QgsComposerLegendStyle::styleLabel( s ), s );
}

return editor;
}

void QgsComposerLegendWidgetStyleDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QgsDebugMsg( "Entered" );
//int s = index.model()->data(index, Qt::UserRole).toInt();
QComboBox *comboBox = static_cast<QComboBox*>( editor );

const QStandardItemModel * standardModel = dynamic_cast<const QStandardItemModel*>( index.model() );
if ( standardModel )
{
QModelIndex firstColumnIndex = standardModel->index( index.row(), 0, index.parent() );
QStandardItem* firstColumnItem = standardModel->itemFromIndex( firstColumnIndex );

QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( firstColumnItem );
if ( cItem )
{
comboBox->setCurrentIndex( comboBox->findData( cItem->style() ) );
}
}
}

void QgsComposerLegendWidgetStyleDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QgsDebugMsg( "Entered" );
QComboBox *comboBox = static_cast<QComboBox*>( editor );
QgsComposerLegendStyle::Style s = ( QgsComposerLegendStyle::Style ) comboBox->itemData( comboBox->currentIndex() ).toInt();
model->setData( index, s, Qt::UserRole );
model->setData( index, QgsComposerLegendStyle::styleLabel( s ), Qt::DisplayRole );
QStandardItemModel * standardModel = dynamic_cast<QStandardItemModel*>( model );
if ( standardModel )
{
QModelIndex firstColumnIndex = standardModel->index( index.row(), 0, index.parent() );
QStandardItem* firstColumnItem = standardModel->itemFromIndex( firstColumnIndex );

QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( firstColumnItem );
if ( cItem )
{
cItem->setStyle( s );
}
}
}

void QgsComposerLegendWidgetStyleDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & index ) const
{
Q_UNUSED( index );
editor->setGeometry( option.rect );
}


QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): mLegend( legend )
{
setupUi( this );
@@ -54,9 +123,15 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): m

if ( legend )
{
legend->model()->setHorizontalHeaderLabels( QStringList() << tr( "Item" ) << tr( "Style" ) );
mItemTreeView->setModel( legend->model() );
}

QgsComposerLegendWidgetStyleDelegate* styleDelegate = new QgsComposerLegendWidgetStyleDelegate();
mItemTreeView->setItemDelegateForColumn( 0, styleDelegate );
mItemTreeView->setItemDelegateForColumn( 1, styleDelegate );
mItemTreeView->setEditTriggers( QAbstractItemView::AllEditTriggers );

mItemTreeView->setDragEnabled( true );
mItemTreeView->setAcceptDrops( true );
mItemTreeView->setDropIndicatorShown( true );
@@ -93,10 +168,11 @@ void QgsComposerLegendWidget::setGuiElements()
mEqualColumnWidthCheckBox->setChecked( mLegend->equalColumnWidth() );
mSymbolWidthSpinBox->setValue( mLegend->symbolWidth() );
mSymbolHeightSpinBox->setValue( mLegend->symbolHeight() );
mGroupSpaceSpinBox->setValue( mLegend->groupSpace() );
mLayerSpaceSpinBox->setValue( mLegend->layerSpace() );
mSymbolSpaceSpinBox->setValue( mLegend->symbolSpace() );
mIconLabelSpaceSpinBox->setValue( mLegend->iconLabelSpace() );
mGroupSpaceSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::Group ).margin( QgsComposerLegendStyle::Top ) );
mLayerSpaceSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::Subgroup ).margin( QgsComposerLegendStyle::Top ) );
// We keep Symbol and SymbolLabel Top in sync for now
mSymbolSpaceSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::Symbol ).margin( QgsComposerLegendStyle::Top ) );
mIconLabelSpaceSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::SymbolLabel ).margin( QgsComposerLegendStyle::Left ) );
mBoxSpaceSpinBox->setValue( mLegend->boxSpace() );
mColumnSpaceSpinBox->setValue( mLegend->columnSpace() );
if ( mLegend->model() )
@@ -208,7 +284,7 @@ void QgsComposerLegendWidget::on_mGroupSpaceSpinBox_valueChanged( double d )
if ( mLegend )
{
mLegend->beginCommand( tr( "Legend group space" ), QgsComposerMergeCommand::LegendGroupSpace );
mLegend->setGroupSpace( d );
mLegend->rstyle( QgsComposerLegendStyle::Group ).setMargin( QgsComposerLegendStyle::Top, d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -220,7 +296,7 @@ void QgsComposerLegendWidget::on_mLayerSpaceSpinBox_valueChanged( double d )
if ( mLegend )
{
mLegend->beginCommand( tr( "Legend layer space" ), QgsComposerMergeCommand::LegendLayerSpace );
mLegend->setLayerSpace( d );
mLegend->rstyle( QgsComposerLegendStyle::Subgroup ).setMargin( QgsComposerLegendStyle::Top, d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -232,7 +308,9 @@ void QgsComposerLegendWidget::on_mSymbolSpaceSpinBox_valueChanged( double d )
if ( mLegend )
{
mLegend->beginCommand( tr( "Legend symbol space" ), QgsComposerMergeCommand::LegendSymbolSpace );
mLegend->setSymbolSpace( d );
// We keep Symbol and SymbolLabel Top in sync for now
mLegend->rstyle( QgsComposerLegendStyle::Symbol ).setMargin( QgsComposerLegendStyle::Top, d );
mLegend->rstyle( QgsComposerLegendStyle::SymbolLabel ).setMargin( QgsComposerLegendStyle::Top, d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -244,7 +322,7 @@ void QgsComposerLegendWidget::on_mIconLabelSpaceSpinBox_valueChanged( double d )
if ( mLegend )
{
mLegend->beginCommand( tr( "Legend icon label space" ), QgsComposerMergeCommand::LegendIconSymbolSpace );
mLegend->setIconLabelSpace( d );
mLegend->rstyle( QgsComposerLegendStyle::SymbolLabel ).setMargin( QgsComposerLegendStyle::Left, d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -258,14 +336,14 @@ void QgsComposerLegendWidget::on_mTitleFontButton_clicked()
bool ok;
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && defined(QT_MAC_USE_COCOA)
// Native Mac dialog works only for Qt Carbon
QFont newFont = QFontDialog::getFont( &ok, mLegend->titleFont(), 0, QString(), QFontDialog::DontUseNativeDialog );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::Title ).font(), 0, QString(), QFontDialog::DontUseNativeDialog );
#else
QFont newFont = QFontDialog::getFont( &ok, mLegend->titleFont() );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::Title ).font() );
#endif
if ( ok )
{
mLegend->beginCommand( tr( "Title font changed" ) );
mLegend->setTitleFont( newFont );
mLegend->setStyleFont( QgsComposerLegendStyle::Title, newFont );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -280,14 +358,14 @@ void QgsComposerLegendWidget::on_mGroupFontButton_clicked()
bool ok;
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && defined(QT_MAC_USE_COCOA)
// Native Mac dialog works only for Qt Carbon
QFont newFont = QFontDialog::getFont( &ok, mLegend->groupFont(), 0, QString(), QFontDialog::DontUseNativeDialog );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::Group ).font(), 0, QString(), QFontDialog::DontUseNativeDialog );
#else
QFont newFont = QFontDialog::getFont( &ok, mLegend->groupFont() );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::Group ).font() );
#endif
if ( ok )
{
mLegend->beginCommand( tr( "Legend group font changed" ) );
mLegend->setGroupFont( newFont );
mLegend->setStyleFont( QgsComposerLegendStyle::Group, newFont );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -302,14 +380,14 @@ void QgsComposerLegendWidget::on_mLayerFontButton_clicked()
bool ok;
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && defined(QT_MAC_USE_COCOA)
// Native Mac dialog works only for Qt Carbon
QFont newFont = QFontDialog::getFont( &ok, mLegend->layerFont(), 0, QString(), QFontDialog::DontUseNativeDialog );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::Subgroup ).font(), 0, QString(), QFontDialog::DontUseNativeDialog );
#else
QFont newFont = QFontDialog::getFont( &ok, mLegend->layerFont() );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::Subgroup ).font() );
#endif
if ( ok )
{
mLegend->beginCommand( tr( "Legend layer font changed" ) );
mLegend->setLayerFont( newFont );
mLegend->setStyleFont( QgsComposerLegendStyle::Subgroup, newFont );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -324,14 +402,14 @@ void QgsComposerLegendWidget::on_mItemFontButton_clicked()
bool ok;
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && defined(QT_MAC_USE_COCOA)
// Native Mac dialog works only for Qt Carbon
QFont newFont = QFontDialog::getFont( &ok, mLegend->itemFont(), 0, QString(), QFontDialog::DontUseNativeDialog );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::SymbolLabel ).font(), 0, QString(), QFontDialog::DontUseNativeDialog );
#else
QFont newFont = QFontDialog::getFont( &ok, mLegend->itemFont() );
QFont newFont = QFontDialog::getFont( &ok, mLegend->style( QgsComposerLegendStyle::SymbolLabel ).font() );
#endif
if ( ok )
{
mLegend->beginCommand( tr( "Legend item font changed" ) );
mLegend->setItemFont( newFont );
mLegend->setStyleFont( QgsComposerLegendStyle::SymbolLabel, newFont );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
@@ -20,9 +20,22 @@

#include "ui_qgscomposerlegendwidgetbase.h"
#include <QWidget>
#include <QItemDelegate>

class QgsComposerLegend;

class QgsComposerLegendWidgetStyleDelegate : public QItemDelegate
{
Q_OBJECT

public:
QgsComposerLegendWidgetStyleDelegate( QObject *parent = 0 );
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
void setEditorData( QWidget *editor, const QModelIndex &index ) const;
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
};

/** \ingroup MapComposer
* A widget for setting properties relating to a composer legend.
*/
@@ -146,6 +146,7 @@ SET(QGIS_CORE_SRCS
composer/qgsatlascomposition.cpp
composer/qgslegendmodel.cpp
composer/qgscomposerlegend.cpp
composer/qgscomposerlegendstyle.cpp
composer/qgspaperitem.cpp
composer/qgsscalebarstyle.cpp
composer/qgsdoubleboxscalebarstyle.cpp
@@ -321,6 +322,7 @@ SET(QGIS_CORE_MOC_HDRS

composer/qgsaddremoveitemcommand.h
composer/qgscomposerlegend.h
composer/qgscomposerlegendstyle.h
composer/qgscomposermap.h
composer/qgscomposerpicture.h
composer/qgscomposerscalebar.h

0 comments on commit ee84bb1

Please sign in to comment.
You can’t perform that action at this time.