Skip to content

Commit

Permalink
Merge pull request #195 from dakcarto/feature_freeze-thaw-labels_6
Browse files Browse the repository at this point in the history
Update to freeze/thaw label tool
  • Loading branch information
NathanW2 committed Jul 20, 2012
2 parents ecb7b24 + 28021ef commit 23ba58d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
5 changes: 3 additions & 2 deletions doc/CONTRIBUTORS
Expand Up @@ -29,9 +29,10 @@ Giuseppe Sucameli
Horst Duester
Hyao (IRC nickname)
Ivan Lucena
Jean-Denis Giguere
Jean-Denis Giguere
Jeremy Palmer
Jerrit Collord
Larry Shaffer
Luiz Motta
Magnus Homann
Marco Pasetti
Expand All @@ -53,5 +54,5 @@ Tamas Szekeres
Tom Russo
Tyler Mitchell
Vita Cizek
Yann Chemin
Yann Chemin
Includes Map icons CC-0 from SJJB Management
60 changes: 56 additions & 4 deletions src/app/qgsmaptoolfreezelabels.cpp
Expand Up @@ -258,6 +258,7 @@ void QgsMapToolFreezeLabels::freezeThawLabels( const QgsRectangle& ext, QMouseEv

bool doThaw = e->modifiers() & Qt::ShiftModifier ? true : false;
bool toggleThawOrFreeze = e->modifiers() & Qt::AltModifier ? true : false;
bool doHide = e->modifiers() & Qt::ControlModifier ? true : false;

// get list of all drawn labels from all layers within, or touching, chosen extent
bool labelChanged = false;
Expand Down Expand Up @@ -314,9 +315,8 @@ void QgsMapToolFreezeLabels::freezeThawLabels( const QgsRectangle& ext, QMouseEv
QString labelStringID = QString("%0|%1").arg(mCurrentLabelPos.layerID, QString::number( mCurrentLabelPos.featureId ) );

// thaw label
if ( mCurrentLabelPos.isFrozen && ( doThaw || toggleThawOrFreeze ) )
if ( mCurrentLabelPos.isFrozen && !doHide && ( doThaw || toggleThawOrFreeze ) )
{

// thaw previously frozen label (set attribute table fields to NULL)
if ( freezeThawLabel( vlayer, mCurrentLabelPos, false ) )
{
Expand All @@ -329,9 +329,8 @@ void QgsMapToolFreezeLabels::freezeThawLabels( const QgsRectangle& ext, QMouseEv
}

// freeze label
if ( !mCurrentLabelPos.isFrozen && ( !doThaw || toggleThawOrFreeze ) )
if ( !mCurrentLabelPos.isFrozen && !doHide && ( !doThaw || toggleThawOrFreeze ) )
{

// freeze label's location, and optionally rotation, to attribute table
if ( freezeThawLabel( vlayer, mCurrentLabelPos, true ) )
{
Expand All @@ -342,6 +341,20 @@ void QgsMapToolFreezeLabels::freezeThawLabels( const QgsRectangle& ext, QMouseEv
QgsDebugMsg( QString( "Freeze failed for layer, label: %0, %1" ).arg( labellyr, labeltxt ) );
}
}

// hide label
if ( doHide )
{
// write 0 font size to attribute table
if ( hideLabel( vlayer, mCurrentLabelPos ) )
{
labelChanged = true;
}
else
{
QgsDebugMsg( QString( "Hide failed for layer, label: %0, %1" ).arg( labellyr, labeltxt ) );
}
}
}

if ( labelChanged )
Expand Down Expand Up @@ -479,3 +492,42 @@ bool QgsMapToolFreezeLabels::freezeThawLabel( QgsVectorLayer* vlayer,
}
return true;
}

bool QgsMapToolFreezeLabels::hideLabel( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos )
{
// skip diagrams
if ( labelpos.isDiagram )
{
QgsDebugMsg( QString( "Label is diagram, skipping" ) );
return false;
}
// verify attribute table has proper fields setup
bool sizeColOk;
int sizeCol;

QVariant sizeColumn = vlayer->customProperty( "labeling/dataDefinedProperty0" );
if ( !sizeColumn.isValid() )
{
QgsDebugMsg( QString( "Size column not set" ) );
return false;
}
sizeCol = sizeColumn.toInt( &sizeColOk );
if ( !sizeColOk )
{
QgsDebugMsg( QString( "Size column not convertible to integer" ) );
return false;
}

// edit attribute table
int fid = labelpos.featureId;

vlayer->beginEditCommand( tr( "Label hidden" ) );
if ( !vlayer->changeAttributeValue( fid, sizeCol, 0, false ) )
{
QgsDebugMsg( QString( "Failed write to attribute table" ) );
return false;
}
vlayer->endEditCommand();
return true;
}
3 changes: 3 additions & 0 deletions src/app/qgsmaptoolfreezelabels.h
Expand Up @@ -95,6 +95,9 @@ class QgsMapToolFreezeLabels: public QgsMapToolLabel
bool freezeThawLabel( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
bool freeze );

//! Hide chosen label by setting font size to 0
bool hideLabel( QgsVectorLayer* vlayer, const QgsLabelPosition& labelpos );
};

#endif // QGSMAPTOOLFREEZELABELS_H
10 changes: 3 additions & 7 deletions src/ui/qgisapp.ui
Expand Up @@ -1716,13 +1716,9 @@
<property name="text">
<string>Freeze or Thaw Labels</string>
</property>
<property name="whatsThis">
<string>Freeze (write label location and optionally rotation) to attribute table.

Click on individual labels, or draw a marquee (labels touching will be included). Actions on in-memory attribute table fields are immediate.

Hold Shift key down to Thaw (write NULLs to attribute table), reverting label to dynamic.
Hold Alt key down to toggle selected labels between Frozen and Thawed states.</string>
<property name="toolTip">
<string>Freeze or Thaw Labels
Shift thaws, Alt toggles, Ctl (Cmd) hides</string>
</property>
</action>
<action name="mActionShowFrozenLabels">
Expand Down

0 comments on commit 23ba58d

Please sign in to comment.