Skip to content

Commit

Permalink
Followup legend text rendering
Browse files Browse the repository at this point in the history
- Use QgsFontButton for setting legend text format
- Tweak some capitalization and layouts
  • Loading branch information
nyalldawson committed Apr 20, 2018
1 parent 6c89265 commit c961b1f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 47 deletions.
42 changes: 16 additions & 26 deletions src/app/qgsvectorlayerlegendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 )
{
Expand All @@ -70,7 +79,7 @@ void QgsVectorLayerLegendWidget::setLayer( QgsVectorLayer *layer )
return;

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

Expand Down Expand Up @@ -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() ) )
Expand All @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/app/qgsvectorlayerlegendwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class QLabel;
class QPushButton;
class QTreeView;

class QgsFontButton;
class QgsCollapsibleGroupBox;
class QgsMapCanvas;
class QgsVectorLayer;
Expand All @@ -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; }
Expand All @@ -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
3 changes: 2 additions & 1 deletion src/gui/qgsfontbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 17 additions & 16 deletions src/ui/qgsvectorlayerpropertiesbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit c961b1f

Please sign in to comment.