Skip to content
Permalink
Browse files

[FEATURE] Make the custom dash pattern dialog a style dock widget

  • Loading branch information
nirvn committed Apr 28, 2019
1 parent 5a088bf commit cd5642aca0dab70b104e2325ca0e7448dddbd9a5
@@ -414,13 +414,23 @@ Decodes a render unit from a string.
.. seealso:: :py:func:`encodeUnit`
%End


static QString toString( QgsUnitTypes::RenderUnit unit );
%Docstring
Returns a translated string representing a render ``unit``.

.. versionadded:: 3.0
%End

static QString toAbbreviatedString( QgsUnitTypes::RenderUnit unit );
%Docstring
Returns a translated abbreviation representing a render unit.

:param unit: unit to convert to string

.. versionadded:: 3.8
%End



static QString encodeUnit( QgsUnitTypes::LayoutUnit unit );
@@ -8,7 +8,41 @@



class QgsDashSpaceDialog: QDialog


class QgsDashSpaceWidget: QgsPanelWidget
{
%Docstring
A widget to enter a custom dash space pattern for lines

.. versionadded:: 3.8
%End

%TypeHeaderCode
#include "qgsdashspacedialog.h"
%End
public:

QgsDashSpaceWidget( const QVector<qreal> &vectorPattern, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsDashSpaceWidget
%End

QVector<qreal> dashDotVector() const;
%Docstring
Returns the dash pattern as a list of numbers
%End

void setUnit( QgsUnitTypes::RenderUnit unit );
%Docstring
Sets the unit type used for the dash space pattern (used to update interface labels)

:param unit: the unit type
%End

};

class QgsDashSpaceDialog : QDialog
{
%Docstring
A dialog to enter a custom dash space pattern for lines
@@ -25,6 +59,18 @@ Constructor for QgsDashSpaceDialog
%End

QVector<qreal> dashDotVector() const;
%Docstring
Returns the dash pattern as a list of numbers
%End

void setUnit( QgsUnitTypes::RenderUnit unit );
%Docstring
Sets the unit type used for the dash space pattern (used to update interface labels)

:param unit: the unit type

.. versionadded:: 3.8
%End

};

@@ -186,6 +186,39 @@ QString QgsUnitTypes::toString( DistanceUnit unit )
return QString();
}

QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::RenderUnit unit )
{
switch ( unit )
{
case RenderMillimeters:
return QObject::tr( "mm", "render" );

case RenderMapUnits:
return QObject::tr( "map units", "render" );

case RenderPixels:
return QObject::tr( "px", "render" );

case RenderPercentage:
return QObject::tr( "%", "render" );

case RenderPoints:
return QObject::tr( "pt", "render" );

case RenderInches:
return QObject::tr( "in", "render" );

case RenderUnknownUnit:
return QObject::tr( "unknown", "render" );

case RenderMetersInMapUnits:
return QObject::tr( "m", "render" );

}

return QString();
}

QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::DistanceUnit unit )
{
switch ( unit )
@@ -414,12 +414,21 @@ class CORE_EXPORT QgsUnitTypes
*/
Q_INVOKABLE static QgsUnitTypes::RenderUnit decodeRenderUnit( const QString &string, bool *ok SIP_OUT = nullptr );


/**
* Returns a translated string representing a render \a unit.
* \since QGIS 3.0
*/
static QString toString( QgsUnitTypes::RenderUnit unit );

/**
* Returns a translated abbreviation representing a render unit.
* \param unit unit to convert to string
*
* \since QGIS 3.8
*/
Q_INVOKABLE static QString toAbbreviatedString( QgsUnitTypes::RenderUnit unit );


// LAYOUT UNITS

@@ -15,53 +15,59 @@

#include "qgsdashspacedialog.h"
#include "qgsapplication.h"

#include <QDialogButtonBox>
#include <QFile>

QgsDashSpaceDialog::QgsDashSpaceDialog( const QVector<qreal> &v, QWidget *parent, Qt::WindowFlags f ): QDialog( parent, f )
QgsDashSpaceWidget::QgsDashSpaceWidget( const QVector<qreal> &vectorPattern, QWidget *parent ) : QgsPanelWidget( parent )
{
setupUi( this );
connect( mAddButton, &QPushButton::clicked, this, &QgsDashSpaceDialog::mAddButton_clicked );
connect( mRemoveButton, &QPushButton::clicked, this, &QgsDashSpaceDialog::mRemoveButton_clicked );

mAddButton->setIcon( QgsApplication::getThemeIcon( "symbologyAdd.svg" ) );
mRemoveButton->setIcon( QgsApplication::getThemeIcon( "symbologyRemove.svg" ) );

double dash = 0;
double space = 0;
for ( int i = 0; i < ( v.size() - 1 ); ++i )
for ( int i = 0; i < ( vectorPattern.size() - 1 ); ++i )
{
dash = v.at( i );
dash = vectorPattern.at( i );
++i;
space = v.at( i );
space = vectorPattern.at( i );
QTreeWidgetItem *entry = new QTreeWidgetItem();
entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
entry->setText( 0, QString::number( dash ) );
entry->setText( 1, QString::number( space ) );
mDashSpaceTreeWidget->addTopLevelItem( entry );
}

connect( mAddButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mAddButton_clicked );
connect( mRemoveButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mRemoveButton_clicked );
connect( mDashSpaceTreeWidget, &QTreeWidget::itemChanged, this, [ this ] { emit widgetChanged(); } );
}

void QgsDashSpaceDialog::mAddButton_clicked()
void QgsDashSpaceWidget::mAddButton_clicked()
{
//add new (default) item
QTreeWidgetItem *entry = new QTreeWidgetItem();
entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
entry->setText( 0, QStringLiteral( "5" ) );
entry->setText( 1, QStringLiteral( "2" ) );
mDashSpaceTreeWidget->addTopLevelItem( entry );
emit widgetChanged();
}

void QgsDashSpaceDialog::mRemoveButton_clicked()
void QgsDashSpaceWidget::mRemoveButton_clicked()
{
//get active item
QTreeWidgetItem *currentItem = mDashSpaceTreeWidget->currentItem();
if ( currentItem )
{
mDashSpaceTreeWidget->takeTopLevelItem( mDashSpaceTreeWidget->indexOfTopLevelItem( currentItem ) );
}
emit widgetChanged();
}

QVector<qreal> QgsDashSpaceDialog::dashDotVector() const
QVector<qreal> QgsDashSpaceWidget::dashDotVector() const
{
QVector<qreal> dashVector;
int nTopLevelItems = mDashSpaceTreeWidget->topLevelItemCount();
@@ -76,3 +82,29 @@ QVector<qreal> QgsDashSpaceDialog::dashDotVector() const
return dashVector;
}

void QgsDashSpaceWidget::setUnit( QgsUnitTypes::RenderUnit unit )
{
QTreeWidgetItem *headerItem = mDashSpaceTreeWidget->headerItem();
headerItem->setText( 0, QStringLiteral( "%1 (%2)" ).arg( tr( "Dash" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
headerItem->setText( 1, QStringLiteral( "%1 (%2)" ).arg( tr( "Space" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
}

QgsDashSpaceDialog::QgsDashSpaceDialog( const QVector<qreal> &v, QWidget *parent, Qt::WindowFlags f ) : QDialog( parent, f )
{
QVBoxLayout *vLayout = new QVBoxLayout();
mWidget = new QgsDashSpaceWidget( v );
vLayout->addWidget( mWidget );
QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
vLayout->addWidget( bbox );
setLayout( vLayout );
}

QVector<qreal> QgsDashSpaceDialog::dashDotVector() const
{
return mWidget->dashDotVector();
}

void QgsDashSpaceDialog::setUnit( QgsUnitTypes::RenderUnit unit )
{
mWidget->setUnit( unit );
}
@@ -16,27 +16,69 @@
#ifndef QGSDASHSPACEDIALOG_H
#define QGSDASHSPACEDIALOG_H

#include "ui_qgsdashspacedialogbase.h"
#include "ui_qgsdashspacewidgetbase.h"

#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgspanelwidget.h"
#include "qgsunittypes.h"

#include <QDialog>

/**
* \ingroup gui
* A dialog to enter a custom dash space pattern for lines
* A widget to enter a custom dash space pattern for lines
* \since QGIS 3.8
*/
class GUI_EXPORT QgsDashSpaceDialog: public QDialog, private Ui::QgsDashSpaceDialogBase
class GUI_EXPORT QgsDashSpaceWidget: public QgsPanelWidget, private Ui::QgsDashSpaceWidgetBase
{
Q_OBJECT
public:

//! Constructor for QgsDashSpaceDialog
QgsDashSpaceDialog( const QVector<qreal> &v, QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags f = nullptr );
//! Constructor for QgsDashSpaceWidget
QgsDashSpaceWidget( const QVector<qreal> &vectorPattern, QWidget *parent SIP_TRANSFERTHIS = nullptr );

//! Returns the dash pattern as a list of numbers
QVector<qreal> dashDotVector() const;

/**
* Sets the unit type used for the dash space pattern (used to update interface labels)
* \param unit the unit type
*/
void setUnit( QgsUnitTypes::RenderUnit unit );

private slots:
void mAddButton_clicked();
void mRemoveButton_clicked();

};

/**
* \ingroup gui
* A dialog to enter a custom dash space pattern for lines
*/
class GUI_EXPORT QgsDashSpaceDialog : public QDialog
{
Q_OBJECT
public:

//! Constructor for QgsDashSpaceDialog
QgsDashSpaceDialog( const QVector<qreal> &v, QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags f = nullptr );

//! Returns the dash pattern as a list of numbers
QVector<qreal> dashDotVector() const;

/**
* Sets the unit type used for the dash space pattern (used to update interface labels)
* \param unit the unit type
* \since QGIS 3.8
*/
void setUnit( QgsUnitTypes::RenderUnit unit );

private:

QgsDashSpaceWidget *mWidget = nullptr;

};

#endif // QGSDASHSPACEDIALOG_H
@@ -364,7 +364,24 @@ void QgsSimpleLineSymbolLayerWidget::mCustomCheckBox_stateChanged( int state )

void QgsSimpleLineSymbolLayerWidget::mChangePatternButton_clicked()
{
QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
if ( panel && panel->dockMode() )
{
QgsDashSpaceWidget *widget = new QgsDashSpaceWidget( mLayer->customDashVector(), panel );
widget->setPanelTitle( tr( "Custom Dash Pattern" ) );
widget->setUnit( mDashPatternUnitWidget->unit() );
connect( widget, &QgsPanelWidget::widgetChanged, this, [ this, widget ]()
{
mLayer->setCustomDashVector( widget->dashDotVector() );
updatePatternIcon();
} );
connect( widget, &QgsPanelWidget::widgetChanged, this, &QgsSymbolLayerWidget::changed );
panel->openPanel( widget );
return;
}

QgsDashSpaceDialog d( mLayer->customDashVector() );
d.setUnit( mDashPatternUnitWidget->unit() );
if ( d.exec() == QDialog::Accepted )
{
mLayer->setCustomDashVector( d.dashDotVector() );

0 comments on commit cd5642a

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