Skip to content
Permalink
Browse files

Show breadcrumb and back button for rule based labeling (fixes #15153)

Following the way how rule based rendering widget was adjusted...
  • Loading branch information
wonder-sk committed Jul 6, 2016
1 parent e00ba6d commit 1c91ae77338d2d8a9263596bc6fe4b8280480dd2
@@ -77,9 +77,9 @@ class QgsPanelWidget : public QWidget
signals:

/**
* Emiited when the panel is accpeted by the user.
* Emitted when the panel is accepted by the user.
* @param panel The panel widget that was accepted.
* @note This argument is normally raised with emit panelAccpeted(this)
* @note This argument is normally raised with emit panelAccepted(this)
* so that callers can retrive the widget easier in calling code.
*/
void panelAccepted( QgsPanelWidget* panel );
@@ -93,7 +93,7 @@ class QgsPanelWidget : public QWidget
void showPanel( QgsPanelWidget* panel );

/**
* Emiited when the widget state changes.
* Emitted when the widget state changes.
* Connect to this to pull any changes off the widget when needed.
* As panels are non blocking "dialogs" you should listen to this signal
* to give the user feedback when something changes.
@@ -114,7 +114,7 @@ class QgsPanelWidget : public QWidget
void openPanel( QgsPanelWidget* panel );

/**
* Accept the panel. Causes panelAccepted to be emiited.
* Accept the panel. Causes panelAccepted to be emitted.
* Widgets are normally removed form the interface using the panel manager or the caller.
*/
void acceptPanel();
@@ -86,8 +86,8 @@ void QgsLabelingWidget::setLayer( QgsMapLayer* mapLayer )

void QgsLabelingWidget::setDockMode( bool enabled )
{
mDockMode = enabled;
mLabelGui->setDockMode( mDockMode );
QgsPanelWidget::setDockMode( enabled );
mLabelGui->setDockMode( enabled );
}

void QgsLabelingWidget::adaptToLayer()
@@ -155,7 +155,9 @@ void QgsLabelingWidget::labelModeChanged( int index )
delete mWidget;
mWidget = nullptr;

QgsRuleBasedLabelingWidget* ruleWidget = new QgsRuleBasedLabelingWidget( mLayer, mCanvas, this, mDockMode );
QgsRuleBasedLabelingWidget* ruleWidget = new QgsRuleBasedLabelingWidget( mLayer, mCanvas, this );
ruleWidget->setDockMode( dockMode() );
connect( ruleWidget, SIGNAL( showPanel( QgsPanelWidget* ) ), this, SLOT( openPanel( QgsPanelWidget* ) ) );
connect( ruleWidget, SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );
mWidget = ruleWidget;
mStackedWidget->addWidget( mWidget );
@@ -63,8 +63,6 @@ class QgsLabelingWidget : public QgsMapLayerConfigWidget, private Ui::QgsLabelin
QgsVectorLayer* mLayer;
QgsMapCanvas* mCanvas;

bool mDockMode;

QWidget* mWidget;
QgsLabelingGui* mLabelGui;
QScopedPointer< QgsAbstractVectorLayerLabeling > mOldSettings;
@@ -24,14 +24,12 @@
#include <QClipboard>
#include <QMessageBox>

QgsRuleBasedLabelingWidget::QgsRuleBasedLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent, bool dockMode )
: QWidget( parent )
QgsRuleBasedLabelingWidget::QgsRuleBasedLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent )
: QgsPanelWidget( parent )
, mLayer( layer )
, mCanvas( canvas )
, mRootRule( nullptr )
, mModel( nullptr )
, mRuleProps( nullptr )
, mDockMode( dockMode )
{
setupUi( this );

@@ -78,11 +76,6 @@ QgsRuleBasedLabelingWidget::~QgsRuleBasedLabelingWidget()
delete mRootRule;
}

void QgsRuleBasedLabelingWidget::setDockMode( bool enabled )
{
mDockMode = enabled;
}

void QgsRuleBasedLabelingWidget::writeSettingsToLayer()
{
// also clear old-style labeling config
@@ -93,83 +86,46 @@ void QgsRuleBasedLabelingWidget::writeSettingsToLayer()

void QgsRuleBasedLabelingWidget::addRule()
{
if ( mRuleProps )
mStackedWidget->removeWidget( mRuleProps );

delete mRuleProps;
mRuleProps = nullptr;

// TODO Delete rule
QgsRuleBasedLabeling::Rule* newrule = new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings );
mRuleProps = new QgsLabelingRulePropsDialog( newrule, mLayer, this, mCanvas, mDockMode );
mRuleProps->setCurrentMode( QgsLabelingRulePropsDialog::Adding );

mStackedWidget->addWidget( mRuleProps );
mStackedWidget->setCurrentWidget( mRuleProps );

connect( mRuleProps, SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );
connect( mRuleProps, SIGNAL( accepted() ), this, SLOT( saveRule() ) );
connect( mRuleProps, SIGNAL( rejected() ), this, SLOT( rejectRule() ) );
addNewRule( newrule );
}

void QgsRuleBasedLabelingWidget::saveRuleEdit()
{
QModelIndex index = viewRules->selectionModel()->currentIndex();
mModel->updateRule( index.parent(), index.row() );
if ( mRuleProps )
mStackedWidget->removeWidget( mRuleProps );

delete mRuleProps;
mRuleProps = nullptr;
mStackedWidget->setCurrentIndex( 0 );
emit widgetChanged();
}

void QgsRuleBasedLabelingWidget::saveRule()
{
if ( mRuleProps )
mStackedWidget->removeWidget( mRuleProps );

delete mRuleProps;
mRuleProps = nullptr;
mStackedWidget->setCurrentIndex( 0 );
emit widgetChanged();
}

void QgsRuleBasedLabelingWidget::addNewRule( QgsRuleBasedLabeling::Rule* newrule )
{
if ( currentRule() )
QgsRuleBasedLabeling::Rule* current = currentRule();
if ( current )
{
// add after this rule
QModelIndex currentIndex = viewRules->selectionModel()->currentIndex();
mModel->insertRule( currentIndex.parent(), currentIndex.row() + 1, newrule );
viewRules->selectionModel()->select( mModel->index( currentIndex.row() + 1, 0 ), QItemSelectionModel::ClearAndSelect );
QModelIndex newindex = mModel->index( currentIndex.row() + 1, 0, currentIndex.parent() );
viewRules->selectionModel()->setCurrentIndex( newindex, QItemSelectionModel::ClearAndSelect );
}
else
{
// append to root rule
int rows = mModel->rowCount();
mModel->insertRule( QModelIndex(), rows, newrule );
viewRules->selectionModel()->select( mModel->index( rows, 0 ), QItemSelectionModel::ClearAndSelect );
QModelIndex newindex = mModel->index( rows, 0 );
viewRules->selectionModel()->setCurrentIndex( newindex, QItemSelectionModel::ClearAndSelect );
}
editRule();
}

void QgsRuleBasedLabelingWidget::rejectRule()
void QgsRuleBasedLabelingWidget::ruleWidgetPanelAccepted( QgsPanelWidget* panel )
{
if ( mRuleProps->currentMode() == QgsLabelingRulePropsDialog::Adding )
removeRule();
QgsLabelingRulePropsWidget* widget = qobject_cast<QgsLabelingRulePropsWidget*>( panel );
widget->apply();

mStackedWidget->setCurrentIndex( 0 );

if ( mRuleProps )
mStackedWidget->removeWidget( mRuleProps );
QModelIndex index = viewRules->selectionModel()->currentIndex();
mModel->updateRule( index.parent(), index.row() );

delete mRuleProps;
mRuleProps = nullptr;
emit widgetChanged();
}

void QgsRuleBasedLabelingWidget::liveUpdateRuleFromPanel()
{
ruleWidgetPanelAccepted( qobject_cast<QgsPanelWidget*>( sender() ) );
}


void QgsRuleBasedLabelingWidget::editRule()
{
editRule( viewRules->selectionModel()->currentIndex() );
@@ -180,23 +136,14 @@ void QgsRuleBasedLabelingWidget::editRule( const QModelIndex& index )
if ( !index.isValid() )
return;

if ( mRuleProps )
mStackedWidget->removeWidget( mRuleProps );

delete mRuleProps;
mRuleProps = nullptr;

QgsRuleBasedLabeling::Rule* rule = mModel->ruleForIndex( index );
mRuleProps = new QgsLabelingRulePropsDialog( rule, mLayer, this, mCanvas, mDockMode );
mRuleProps->setCurrentMode( QgsLabelingRulePropsDialog::Editing );

connect( mRuleProps, SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );

mStackedWidget->addWidget( mRuleProps );
mStackedWidget->setCurrentWidget( mRuleProps );

connect( mRuleProps, SIGNAL( accepted() ), this, SLOT( saveRuleEdit() ) );
connect( mRuleProps, SIGNAL( rejected() ), this, SLOT( rejectRule() ) );
QgsLabelingRulePropsWidget* widget = new QgsLabelingRulePropsWidget( rule, mLayer, this, mCanvas );
widget->setDockMode( true );
widget->setPanelTitle( tr( "Edit rule" ) );
connect( widget, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( ruleWidgetPanelAccepted( QgsPanelWidget* ) ) );
connect( widget, SIGNAL( widgetChanged() ), this, SLOT( liveUpdateRuleFromPanel() ) );
openPanel( widget );
}

void QgsRuleBasedLabelingWidget::removeRule()
@@ -622,23 +569,15 @@ void QgsRuleBasedLabelingModel::updateRule( const QModelIndex& parent, int row )

/////////

QgsLabelingRulePropsDialog::QgsLabelingRulePropsDialog( QgsRuleBasedLabeling::Rule* rule, QgsVectorLayer* layer, QWidget* parent, QgsMapCanvas* mapCanvas, bool dockMode )
: QDialog( parent )
QgsLabelingRulePropsWidget::QgsLabelingRulePropsWidget( QgsRuleBasedLabeling::Rule* rule, QgsVectorLayer* layer, QWidget* parent, QgsMapCanvas* mapCanvas )
: QgsPanelWidget( parent )
, mRule( rule )
, mLayer( layer )
, mLabelingGui( nullptr )
, mSettings( nullptr )
, mMapCanvas( mapCanvas )
, mDockMode( dockMode )
, mCurrentMode( Adding )
{
setupUi( this );
#ifdef Q_OS_MAC
setWindowModality( Qt::WindowModal );
#endif

connect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );

editFilter->setText( mRule->filterExpression() );
editFilter->setToolTip( mRule->filterExpression() );
@@ -668,7 +607,6 @@ QgsLabelingRulePropsDialog::QgsLabelingRulePropsDialog( QgsRuleBasedLabeling::Ru
}

mLabelingGui = new QgsLabelingGui( nullptr, mMapCanvas, mSettings, this );
mLabelingGui->setDockMode( mDockMode );
mLabelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
QVBoxLayout* l = new QVBoxLayout;
l->addWidget( mLabelingGui );
@@ -680,21 +618,25 @@ QgsLabelingRulePropsDialog::QgsLabelingRulePropsDialog( QgsRuleBasedLabeling::Ru
connect( btnExpressionBuilder, SIGNAL( clicked() ), this, SLOT( buildExpression() ) );
connect( btnTestFilter, SIGNAL( clicked() ), this, SLOT( testFilter() ) );
connect( editFilter, SIGNAL( textEdited( QString ) ), this, SIGNAL( widgetChanged() ) );
connect( editDescription, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
connect( groupScale, SIGNAL( toggled( bool ) ), this, SIGNAL( widgetChanged() ) );
connect( mScaleRangeWidget, SIGNAL( rangeChanged( double, double ) ), this, SIGNAL( widgetChanged() ) );
connect( groupSettings, SIGNAL( toggled( bool ) ), this, SIGNAL( widgetChanged() ) );
connect( mLabelingGui, SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );
connect( this, SIGNAL( widgetChanged() ), this, SLOT( updateRule() ) );

QSettings settings;
restoreGeometry( settings.value( "/Windows/QgsLabelingRulePropsDialog/geometry" ).toByteArray() );
}

QgsLabelingRulePropsDialog::~QgsLabelingRulePropsDialog()
QgsLabelingRulePropsWidget::~QgsLabelingRulePropsWidget()
{
delete mSettings;
QSettings settings;
settings.setValue( "/Windows/QgsLabelingRulePropsDialog/geometry", saveGeometry() );
}

void QgsLabelingRulePropsDialog::testFilter()
void QgsLabelingRulePropsWidget::setDockMode( bool dockMode )
{
QgsPanelWidget::setDockMode( dockMode );
mLabelingGui->setDockMode( dockMode );
}

void QgsLabelingRulePropsWidget::testFilter()
{
QgsExpression filter( editFilter->text() );
if ( filter.hasParserError() )
@@ -746,7 +688,7 @@ void QgsLabelingRulePropsDialog::testFilter()
QMessageBox::information( this, tr( "Filter" ), tr( "Filter returned %n feature(s)", "number of filtered features", count ) );
}

void QgsLabelingRulePropsDialog::buildExpression()
void QgsLabelingRulePropsWidget::buildExpression()
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
@@ -769,7 +711,7 @@ void QgsLabelingRulePropsDialog::buildExpression()
editFilter->setText( dlg.expressionText() );
}

void QgsLabelingRulePropsDialog::updateRule()
void QgsLabelingRulePropsWidget::apply()
{
mRule->setFilterExpression( editFilter->text() );
mRule->setDescription( editDescription->text() );
@@ -778,9 +720,3 @@ void QgsLabelingRulePropsDialog::updateRule()
mRule->setScaleMaxDenom( groupScale->isChecked() ? mScaleRangeWidget->maximumScaleDenom() : 0 );
mRule->setSettings( groupSettings->isChecked() ? new QgsPalLayerSettings( mLabelingGui->layerSettings() ) : nullptr );
}

void QgsLabelingRulePropsDialog::accept()
{
updateRule();
QDialog::accept();
}

0 comments on commit 1c91ae7

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