Skip to content
Permalink
Browse files

Followup legend text rendering

- Use QgsFontButton for setting legend text format
- Tweak some capitalization and layouts
  • Loading branch information
nyalldawson committed Apr 20, 2018
1 parent 6c89265 commit c961b1f24bf8d11ef48d1c6ddfcb29fb9133b1ae
@@ -26,16 +26,17 @@
#include "qgssymbollayerutils.h"
#include "qgstextformatwidget.h"
#include "qgsvectorlayer.h"

#include "qgsfontbutton.h"

QgsVectorLayerLegendWidget::QgsVectorLayerLegendWidget( QWidget *parent )
: QWidget( parent )
{
mLegendTreeView = new QTreeView;
mLegendTreeView->setRootIsDecorated( false );

mTextOnSymbolFormatButton = new QPushButton( tr( "Set Text Format…" ) );
connect( mTextOnSymbolFormatButton, &QPushButton::clicked, this, &QgsVectorLayerLegendWidget::openTextFormatWidget );
mTextOnSymbolFormatButton = new QgsFontButton( nullptr, tr( "Legend Text Format" ) );
mTextOnSymbolFormatButton->setText( tr( "Text Format" ) );
mTextOnSymbolFormatButton->setMode( QgsFontButton::ModeTextRenderer );

mTextOnSymbolFromExpressionButton = new QPushButton( tr( "Set Labels from Expression…" ) );
connect( mTextOnSymbolFromExpressionButton, &QPushButton::clicked, this, &QgsVectorLayerLegendWidget::labelsFromExpression );
@@ -45,21 +46,29 @@ QgsVectorLayerLegendWidget::QgsVectorLayerLegendWidget( QWidget *parent )
QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addWidget( mTextOnSymbolFormatButton );
buttonsLayout->addWidget( mTextOnSymbolFromExpressionButton );
buttonsLayout->addStretch();

QVBoxLayout *groupLayout = new QVBoxLayout;
groupLayout->addWidget( mLegendTreeView );
groupLayout->addLayout( buttonsLayout );

mTextOnSymbolGroupBox->setTitle( tr( "Text on Symbols" ) );
mTextOnSymbolGroupBox->setTitle( tr( "Text on symbols" ) );
mTextOnSymbolGroupBox->setCheckable( true );
mTextOnSymbolGroupBox->setLayout( groupLayout );
mTextOnSymbolGroupBox->setCollapsed( true );
mTextOnSymbolGroupBox->setCollapsed( false );

QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin( 0 );
layout->setContentsMargins( 0, 0, 0, 0 );
layout->addWidget( mTextOnSymbolGroupBox );
setLayout( layout );
}

void QgsVectorLayerLegendWidget::setMapCanvas( QgsMapCanvas *canvas )
{
mCanvas = canvas;
mTextOnSymbolFormatButton->setMapCanvas( mCanvas );
}

void QgsVectorLayerLegendWidget::setLayer( QgsVectorLayer *layer )
{
@@ -70,7 +79,7 @@ void QgsVectorLayerLegendWidget::setLayer( QgsVectorLayer *layer )
return;

mTextOnSymbolGroupBox->setChecked( legend->textOnSymbolEnabled() );
mTextOnSymbolTextFormat = legend->textOnSymbolTextFormat();
mTextOnSymbolFormatButton->setTextFormat( legend->textOnSymbolTextFormat() );
populateLegendTreeView( legend->textOnSymbolContent() );
}

@@ -116,7 +125,7 @@ void QgsVectorLayerLegendWidget::applyToLayer()
{
QgsDefaultVectorLayerLegend *legend = new QgsDefaultVectorLayerLegend( mLayer );
legend->setTextOnSymbolEnabled( mTextOnSymbolGroupBox->isChecked() );
legend->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
legend->setTextOnSymbolTextFormat( mTextOnSymbolFormatButton->textFormat() );

QHash<QString, QString> content;
if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mLegendTreeView->model() ) )
@@ -134,25 +143,6 @@ void QgsVectorLayerLegendWidget::applyToLayer()
mLayer->setLegend( legend );
}


void QgsVectorLayerLegendWidget::openTextFormatWidget()
{
QgsTextFormatWidget *textOnSymbolFormatWidget = new QgsTextFormatWidget( mTextOnSymbolTextFormat );
QDialogButtonBox *dialogButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget( textOnSymbolFormatWidget );
layout->addWidget( dialogButtonBox );
QDialog dlg;
connect( dialogButtonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
connect( dialogButtonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
dlg.setLayout( layout );
if ( !dlg.exec() )
return;

mTextOnSymbolTextFormat = textOnSymbolFormatWidget->format();
}


void QgsVectorLayerLegendWidget::labelsFromExpression()
{
QHash<QString, QString> content;
@@ -24,6 +24,7 @@ class QLabel;
class QPushButton;
class QTreeView;

class QgsFontButton;
class QgsCollapsibleGroupBox;
class QgsMapCanvas;
class QgsVectorLayer;
@@ -38,7 +39,7 @@ class QgsVectorLayerLegendWidget : public QWidget
explicit QgsVectorLayerLegendWidget( QWidget *parent = nullptr );

//! Sets pointer to map canvas
void setMapCanvas( QgsMapCanvas *canvas ) { mCanvas = canvas; }
void setMapCanvas( QgsMapCanvas *canvas );

//! Returns pointer to map canvas
QgsMapCanvas *mapCanvas() const { return mCanvas; }
@@ -50,22 +51,20 @@ class QgsVectorLayerLegendWidget : public QWidget
void applyToLayer();

private slots:
void openTextFormatWidget();
void labelsFromExpression();

private:
void populateLegendTreeView( const QHash<QString, QString> &content );

private:
QTreeView *mLegendTreeView = nullptr;
QPushButton *mTextOnSymbolFormatButton = nullptr;
QgsFontButton *mTextOnSymbolFormatButton = nullptr;
QPushButton *mTextOnSymbolFromExpressionButton = nullptr;
QgsCollapsibleGroupBox *mTextOnSymbolGroupBox = nullptr;
QLabel *mTextOnSymbolLabel = nullptr;

QgsMapCanvas *mCanvas = nullptr;
QgsVectorLayer *mLayer = nullptr;
QgsTextFormat mTextOnSymbolTextFormat;
};

#endif // QGSVECTORLAYERLEGENDWIDGET_H
@@ -50,7 +50,8 @@ QgsFontButton::QgsFontButton( QWidget *parent, const QString &dialogTitle )
//make sure height of button looks good under different platforms
QSize size = QToolButton::minimumSizeHint();
int fontHeight = Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 1.4;
mSizeHint = QSize( size.width(), std::max( size.height(), fontHeight ) );
int minWidth = Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 20;
mSizeHint = QSize( std::max( minWidth, size.width() ), std::max( size.height(), fontHeight ) );
}

QSize QgsFontButton::minimumSizeHint() const
@@ -345,7 +345,7 @@
</sizepolicy>
</property>
<property name="currentIndex">
<number>12</number>
<number>5</number>
</property>
<widget class="QWidget" name="mOptsPage_Information">
<layout class="QVBoxLayout" name="verticalLayout_5">
@@ -421,8 +421,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>653</width>
<height>542</height>
<width>285</width>
<height>405</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_9">
@@ -895,8 +895,8 @@ border-radius: 2px;</string>
<rect>
<x>0</x>
<y>0</y>
<width>104</width>
<height>102</height>
<width>653</width>
<height>536</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_23">
@@ -1290,8 +1290,8 @@ border-radius: 2px;</string>
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<height>30</height>
<width>653</width>
<height>536</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_21">
@@ -1464,8 +1464,8 @@ border-radius: 2px;</string>
<rect>
<x>0</x>
<y>0</y>
<width>653</width>
<height>542</height>
<width>671</width>
<height>522</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_32">
@@ -1878,10 +1878,10 @@ border-radius: 2px;</string>
<number>0</number>
</property>
<item>
<widget class="QgsVectorLayerLegendWidget" name="mLegendWidget" native="true"/>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<widget class="QgsVectorLayerLegendWidget" name="mLegendWidget" native="true"/>
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="groupBox_3">
<property name="title">
<string>Embedded widgets in legend</string>
</property>
@@ -1921,8 +1921,8 @@ border-radius: 2px;</string>
<rect>
<x>0</x>
<y>0</y>
<width>306</width>
<height>578</height>
<width>639</width>
<height>656</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_13">
@@ -2420,7 +2420,7 @@ border-radius: 2px;</string>
<header>qgscodeeditorhtml.h</header>
</customwidget>
<customwidget>
<class>QgsVectorLayerLegendWidget</class>
<class>QgsVectorLayerLegendWidget</class>
<extends>QWidget</extends>
<header>qgsvectorlayerlegendwidget.h</header>
<container>1</container>
@@ -2517,6 +2517,7 @@ border-radius: 2px;</string>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections>
<connection>

0 comments on commit c961b1f

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