Skip to content
Permalink
Browse files
Point text annotations can be created
  • Loading branch information
nyalldawson committed Sep 8, 2021
1 parent 310e463 commit 303cb455e0739fe72961b1b405a7671a333477d2
@@ -48,6 +48,12 @@ Emitted by the tool when a new annotation item has been created.
Clients should connect to this signal and call :py:func:`~QgsCreateAnnotationItemMapTool.takeCreatedItem` to take the newly created item from the map tool.
%End

protected:

QgsAnnotationLayer *targetLayer();
%Docstring
Returns the target layer for newly created items.
%End

};

@@ -432,6 +432,8 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();

#include "qgssublayersdialog.h"
#include "ogr/qgsvectorlayersaveasdialog.h"
#include "qgsannotationitemguiregistry.h"
#include "qgscreateannotationitemmaptool.h"

#include "pointcloud/qgspointcloudelevationpropertieswidget.h"
#include "pointcloud/qgspointcloudlayerstylewidget.h"
@@ -751,6 +753,76 @@ void QgisApp::showGeoreferencer()
}
#endif

void QgisApp::annotationItemTypeAdded( int id )
{
if ( QgsGui::annotationItemGuiRegistry()->itemMetadata( id )->flags() & Qgis::AnnotationItemGuiFlag::FlagNoCreationTools )
return;

QString name = QgsGui::annotationItemGuiRegistry()->itemMetadata( id )->visibleName();
QString groupId = QgsGui::annotationItemGuiRegistry()->itemMetadata( id )->groupId();
QToolButton *groupButton = nullptr;
if ( !groupId.isEmpty() )
{
// find existing group toolbutton and submenu, or create new ones if this is the first time the group has been encountered
const QgsAnnotationItemGuiGroup &group = QgsGui::annotationItemGuiRegistry()->itemGroup( groupId );
QIcon groupIcon = group.icon.isNull() ? QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicShape.svg" ) ) : group.icon;
QString groupText = tr( "Create %1" ).arg( group.name );
if ( mAnnotationItemGroupToolButtons.contains( groupId ) )
{
groupButton = mAnnotationItemGroupToolButtons.value( groupId );
}
else
{
QToolButton *groupToolButton = new QToolButton( mAnnotationsToolBar );
groupToolButton->setIcon( groupIcon );
groupToolButton->setCheckable( true );
groupToolButton->setPopupMode( QToolButton::InstantPopup );
groupToolButton->setAutoRaise( true );
groupToolButton->setToolButtonStyle( Qt::ToolButtonIconOnly );
groupToolButton->setToolTip( groupText );
mAnnotationsToolBar->addWidget( groupToolButton );
mAnnotationItemGroupToolButtons.insert( groupId, groupToolButton );
groupButton = groupToolButton;
}
}

// update UI for new item type
QAction *action = new QAction( tr( "Create %1" ).arg( name ), this );
action->setToolTip( tr( "Create %1" ).arg( name ) );
action->setCheckable( true );
action->setData( id );
action->setIcon( QgsGui::annotationItemGuiRegistry()->itemMetadata( id )->creationIcon() );

mMapToolGroup->addAction( action );

if ( groupButton )
groupButton->addAction( action );
else
{
mAnnotationsToolBar->addAction( action );
}

connect( action, &QAction::triggered, this, [this, id]()
{
QgsCreateAnnotationItemMapTool *tool = QgsGui::annotationItemGuiRegistry()->itemMetadata( id )->createMapTool( mMapCanvas, mAdvancedDigitizingDockWidget );
mMapCanvas->setMapTool( tool );
connect( tool, &QgsMapTool::deactivated, tool, &QObject::deleteLater );
connect( tool, &QgsCreateAnnotationItemMapTool::itemCreated, this, [ = ]
{
QgsAnnotationItem *item = tool->takeCreatedItem();
if ( QgsAnnotationLayer *layer = qobject_cast< QgsAnnotationLayer * >( activeLayer() ) )
{
layer->addItem( item );
}
else
{
QgsProject::instance()->mainAnnotationLayer()->addItem( item );
}
// TODO -- possibly automatically deactive the tool now?
} );
} );
}

/*
* This function contains forced validation of CRS used in QGIS.
* There are 4 options depending on the settings:
@@ -1414,6 +1486,17 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
QgsCustomization::instance()->updateBrowserWidget( mBrowserWidget );
QgsCustomization::instance()->updateBrowserWidget( mBrowserWidget2 );


// populate annotation toolbar with initial items...
const QList< int > itemMetadataIds = QgsGui::annotationItemGuiRegistry()->itemMetadataIds();
for ( int id : itemMetadataIds )
{
annotationItemTypeAdded( id );
}
//..and listen out for new item types
connect( QgsGui::annotationItemGuiRegistry(), &QgsAnnotationItemGuiRegistry::typeAdded, this, &QgisApp::annotationItemTypeAdded );


// Create the plugin registry and load plugins
// load any plugins that were running in the last session
mSplash->showMessage( tr( "Restoring loaded plugins" ), Qt::AlignHCenter | Qt::AlignBottom, splashTextColor );
@@ -1988,6 +1988,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void showGeoreferencer();
#endif

void annotationItemTypeAdded( int id );

signals:

/**
@@ -2679,6 +2681,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QgsScopedOptionsWidgetFactory mBabelGpsDevicesWidgetFactory;
QgsScopedOptionsWidgetFactory m3DOptionsWidgetFactory;

QMap< QString, QToolButton * > mAnnotationItemGroupToolButtons;

class QgsCanvasRefreshBlocker
{
public:
@@ -19,7 +19,7 @@
#include "qgsannotationitem.h"

#include "qgsannotationitemwidget_impl.h"

#include "qgscreateannotationitemmaptool_impl.h"
//
// QgsAnnotationItemAbstractGuiMetadata
//
@@ -186,7 +186,7 @@ void QgsAnnotationItemGuiRegistry::addDefaultItems()
QgsAnnotationPolygonItemWidget *widget = new QgsAnnotationPolygonItemWidget( nullptr );
widget->setItem( item );
return widget;
} ) );
}, QString(), Qgis::AnnotationItemGuiFlag::FlagNoCreationTools ) );

addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linestring" ),
QObject::tr( "Line" ),
@@ -196,7 +196,7 @@ void QgsAnnotationItemGuiRegistry::addDefaultItems()
QgsAnnotationLineItemWidget *widget = new QgsAnnotationLineItemWidget( nullptr );
widget->setItem( item );
return widget;
} ) );
}, QString(), Qgis::AnnotationItemGuiFlag::FlagNoCreationTools ) );

addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "marker" ),
QObject::tr( "Marker" ),
@@ -206,15 +206,19 @@ void QgsAnnotationItemGuiRegistry::addDefaultItems()
QgsAnnotationMarkerItemWidget *widget = new QgsAnnotationMarkerItemWidget( nullptr );
widget->setItem( item );
return widget;
} ) );
}, QString(), Qgis::AnnotationItemGuiFlag::FlagNoCreationTools ) );

addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "pointtext" ),
QObject::tr( "Text at Point" ),
QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ),
QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabel.svg" ) ),
[ = ]( QgsAnnotationItem * item )->QgsAnnotationItemBaseWidget *
{
QgsAnnotationPointTextItemWidget *widget = new QgsAnnotationPointTextItemWidget( nullptr );
widget->setItem( item );
return widget;
}, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
[ = ]( QgsMapCanvas * canvas, QgsAdvancedDigitizingDockWidget * cadDockWidget )->QgsCreateAnnotationItemMapTool *
{
return new QgsCreatePointTextItemMapTool( canvas, cadDockWidget );
} ) );
}
@@ -14,9 +14,19 @@
***************************************************************************/

#include "qgscreateannotationitemmaptool.h"
#include "qgsmapcanvas.h"
#include "qgsannotationlayer.h"

QgsCreateAnnotationItemMapTool::QgsCreateAnnotationItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
: QgsMapToolAdvancedDigitizing( canvas, cadDockWidget )
{

}

QgsAnnotationLayer *QgsCreateAnnotationItemMapTool::targetLayer()
{
if ( QgsAnnotationLayer *res = qobject_cast< QgsAnnotationLayer * >( canvas()->currentLayer() ) )
return res;
else
return QgsProject::instance()->mainAnnotationLayer();
}
@@ -20,6 +20,7 @@
#include "qgsmaptooladvanceddigitizing.h"

class QgsAnnotationItem;
class QgsAnnotationLayer;

/**
* \class QgsCreateAnnotationItemMapTool
@@ -61,6 +62,12 @@ class GUI_EXPORT QgsCreateAnnotationItemMapTool: public QgsMapToolAdvancedDigiti
*/
void itemCreated();

protected:

/**
* Returns the target layer for newly created items.
*/
QgsAnnotationLayer *targetLayer();

};

@@ -14,8 +14,36 @@
***************************************************************************/

#include "qgscreateannotationitemmaptool_impl.h"
#include "qgsmapmouseevent.h"
#include "qgsannotationpointtextitem.h"
#include "qgsannotationlayer.h"

///@cond PRIVATE

QgsCreatePointTextItemMapTool::QgsCreatePointTextItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
: QgsCreateAnnotationItemMapTool( canvas, cadDockWidget )
{

}

QgsCreatePointTextItemMapTool::~QgsCreatePointTextItemMapTool() = default;

void QgsCreatePointTextItemMapTool::cadCanvasPressEvent( QgsMapMouseEvent *event )
{
if ( event->button() != Qt::LeftButton )
return;

const QgsPointXY layerPoint = toLayerCoordinates( targetLayer(), event->mapPoint() );

mCreatedItem = std::make_unique< QgsAnnotationPointTextItem >( tr( "Text" ), layerPoint );
mCreatedItem->setAlignment( Qt::AlignLeft );
emit itemCreated();
}

QgsAnnotationItem *QgsCreatePointTextItemMapTool::takeCreatedItem()
{
return mCreatedItem.release();
}


///@endcond PRIVATE
@@ -19,10 +19,32 @@
#include "qgis_sip.h"
#include "qgscreateannotationitemmaptool.h"

class QgsAnnotationPointTextItem;

#define SIP_NO_FILE

///@cond PRIVATE

class QgsCreatePointTextItemMapTool: public QgsCreateAnnotationItemMapTool
{
Q_OBJECT

public:

QgsCreatePointTextItemMapTool( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget );
~QgsCreatePointTextItemMapTool() override;

void cadCanvasPressEvent( QgsMapMouseEvent *event ) override;

QgsAnnotationItem *takeCreatedItem() override;


private:

std::unique_ptr< QgsAnnotationPointTextItem > mCreatedItem;

};

///@endcond PRIVATE

#endif // QGSCREATEANNOTATIONITEMMAPTOOLIMPL_H

0 comments on commit 303cb45

Please sign in to comment.