29#include "moc_qgsstackeddiagramproperties.cpp"
36 , mMapCanvas( canvas )
44 connect( mSubDiagramsView, &QAbstractItemView::doubleClicked,
this,
static_cast<void (
QgsStackedDiagramProperties::* )(
const QModelIndex & )
>( &QgsStackedDiagramProperties::editSubDiagramRenderer ) );
46 connect( mAddSubDiagramButton, &QPushButton::clicked,
this, &QgsStackedDiagramProperties::addSubDiagramRenderer );
47 connect( mEditSubDiagramButton, &QAbstractButton::clicked,
this,
static_cast<void (
QgsStackedDiagramProperties::* )()
>( &QgsStackedDiagramProperties::editSubDiagramRenderer ) );
48 connect( mRemoveSubDiagramButton, &QPushButton::clicked,
this, &QgsStackedDiagramProperties::removeSubDiagramRenderer );
54 mStackedDiagramSpacingSpinBox->setClearValue( 0 );
62 mSubDiagramsView->setModel( mModel );
63 mSubDiagramsView->resizeColumnToContents( 0 );
72void QgsStackedDiagramProperties::addSubDiagramRenderer()
75 std::unique_ptr<QgsDiagramRenderer> renderer;
76 std::unique_ptr<QgsSingleCategoryDiagramRenderer> dr = std::make_unique<QgsSingleCategoryDiagramRenderer>();
77 renderer = std::move( dr );
79 QItemSelectionModel *sel = mSubDiagramsView->selectionModel();
80 const QModelIndex index = sel->currentIndex();
82 if ( index.isValid() )
85 const QModelIndex currentIndex = mSubDiagramsView->selectionModel()->currentIndex();
87 const QModelIndex newIndex = mModel->index( currentIndex.row() + 1, 0 );
88 mSubDiagramsView->selectionModel()->setCurrentIndex( newIndex, QItemSelectionModel::ClearAndSelect );
93 appendSubDiagramRenderer( renderer.release() );
95 editSubDiagramRenderer();
100 const int rows = mModel->
rowCount();
102 const QModelIndex newIndex = mModel->index( rows, 0 );
103 mSubDiagramsView->selectionModel()->setCurrentIndex( newIndex, QItemSelectionModel::ClearAndSelect );
106void QgsStackedDiagramProperties::editSubDiagramRenderer()
108 editSubDiagramRenderer( mSubDiagramsView->selectionModel()->currentIndex() );
111void QgsStackedDiagramProperties::editSubDiagramRenderer(
const QModelIndex &index )
113 if ( !index.isValid() )
124 widget->layout()->setContentsMargins( 0, 0, 0, 0 );
127 if ( !couldBeFirstSubDiagram( index ) )
140 dlg.syncToRenderer( renderer );
141 dlg.syncToSettings( &dls );
142 if ( !couldBeFirstSubDiagram( index ) )
144 dlg.setAllowedToEditDiagramLayerSettings(
false );
149 const QModelIndex index = mSubDiagramsView->selectionModel()->currentIndex();
150 if ( dlg.isAllowedToEditDiagramLayerSettings() )
158void QgsStackedDiagramProperties::removeSubDiagramRenderer()
160 const QItemSelection sel = mSubDiagramsView->selectionModel()->selection();
161 const auto constSel = sel;
162 for (
const QItemSelectionRange &range : constSel )
164 if ( range.isValid() )
165 mModel->
removeRows( range.top(), range.bottom() - range.top() + 1, range.parent() );
168 mSubDiagramsView->selectionModel()->clear();
178 mStackedDiagramModeComboBox->setCurrentIndex( settingList.at( 0 ).stackedDiagramMode );
179 mStackedDiagramSpacingSpinBox->setValue( settingList.at( 0 ).stackedDiagramSpacing() );
180 mStackedDiagramSpacingUnitComboBox->setUnit( settingList.at( 0 ).stackedDiagramSpacingUnit() );
185 const QList<QgsDiagramRenderer *> renderers = stackedDiagramRenderer->
renderers();
188 appendSubDiagramRenderer( renderer->
clone() );
194 appendSubDiagramRenderer( dr->
clone() );
204 std::unique_ptr<QgsDiagramSettings> ds = std::make_unique<QgsDiagramSettings>();
206 ds->setStackedDiagramSpacingUnit( mStackedDiagramSpacingUnitComboBox->unit() );
207 ds->setStackedDiagramSpacing( mStackedDiagramSpacingSpinBox->value() );
214 const QList<QgsDiagramRenderer *> renderers = mModel->
subRenderers();
218 if ( !ds1.isEmpty() )
220 ds->categoryAttributes += ds1.at( 0 ).categoryAttributes;
221 ds->categoryLabels += ds1.at( 0 ).categoryLabels;
222 ds->categoryColors += ds1.at( 0 ).categoryColors;
239bool QgsStackedDiagramProperties::couldBeFirstSubDiagram(
const QModelIndex &index )
const
241 if ( !index.isValid() )
249 const QList<QgsDiagramRenderer *> renderers = mModel->
subRenderers();
251 for (
int i = 0; i < index.row(); i++ )
255 if ( !ds.isEmpty() && ds.at( 0 ).enabled )
269void QgsStackedDiagramProperties::subDiagramWidgetPanelAccepted(
QgsPanelWidget *panel )
273 std::unique_ptr<QgsDiagramRenderer> renderer = widget->createRenderer();
275 const QModelIndex index = mSubDiagramsView->selectionModel()->currentIndex();
282void QgsStackedDiagramProperties::liveUpdateSubDiagramFromPanel()
284 subDiagramWidgetPanelAccepted( qobject_cast<QgsPanelWidget *>( sender() ) );
295 setWindowModality( Qt::WindowModal );
298 QVBoxLayout *layout =
new QVBoxLayout(
this );
300 scrollArea->setFrameShape( QFrame::NoFrame );
301 layout->addWidget( scrollArea );
303 buttonBox =
new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
307 scrollArea->setWidget( mPropsWidget );
308 layout->addWidget( buttonBox );
309 this->setWindowTitle(
"Edit Sub Diagram" );
313 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
314 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsStackedDiagramPropertiesDialog::showHelp );
330 mRenderer = mPropsWidget->createRenderer();
331 mDiagramLayerSettings = mPropsWidget->createDiagramLayerSettings();
337 return mRenderer.release();
342 return mDiagramLayerSettings;
355void QgsStackedDiagramPropertiesDialog::showHelp()
357 QgsHelp::openHelp( QStringLiteral(
"working_with_vector/vector_properties.html#diagrams-properties" ) );
363 : QAbstractTableModel( parent )
374 const Qt::ItemFlag checkable = ( index.column() == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags );
376 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | checkable;
381 if ( !index.isValid() )
386 if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
388 switch ( index.column() )
395 return tr(
"Pie Chart" );
399 return tr(
"Text Diagram" );
403 return tr(
"Histogram" );
407 return tr(
"Stacked Bars" );
411 return tr(
"Stacked Diagram" );
420 return tr(
"(no diagram)" );
425 return tr(
"(no renderer)" );
430 return tr(
"Fixed" );
432 return tr(
"Scaled" );
434 return tr(
"Unknown" );
446 return tr(
"Right" );
460 else if ( role == Qt::TextAlignmentRole )
462 return index.column() == 0 ?
static_cast<Qt::Alignment::Int
>( Qt::AlignCenter ) :
static_cast<Qt::Alignment::Int
>( Qt::AlignLeft );
464 else if ( role == Qt::CheckStateRole )
466 if ( index.column() != 0 )
479 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 4 )
482 lst << tr(
"Enabled" ) << tr(
"Diagram type" ) << tr(
"Size" ) << tr(
"Orientation" );
501 if ( !index.isValid() )
506 if ( role == Qt::CheckStateRole )
511 ds.
enabled = ( value.toInt() == Qt::Checked );
524 emit dataChanged( index, index );
536 beginRemoveRows( parent, row, row + count - 1 );
546 if ( index.isValid() )
553 beginInsertRows( QModelIndex(), index, index );
560 if ( !index.isValid() )
565 emit dataChanged( index, index );
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ MetersInMapUnits
Meters value as Map units.
Stores the settings for rendering of all diagrams for a layer.
void syncToSettings(const QgsDiagramLayerSettings *dls)
Updates the widget to reflect the diagram layer settings.
void setDockMode(bool dockMode) override
Sets the widget in dock mode.
bool isAllowedToEditDiagramLayerSettings() const
Returns whether this widget is allowed to edit diagram layer settings.
void syncToRenderer(const QgsDiagramRenderer *dr)
Updates the widget to reflect the diagram renderer.
void auxiliaryFieldCreated()
void setAllowedToEditDiagramLayerSettings(bool allowed)
Sets whether the widget should show diagram layer settings.
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
virtual QString rendererName() const =0
QgsDiagram * diagram() const
virtual QList< QgsDiagramSettings > diagramSettings() const =0
Returns list with all diagram settings in the renderer.
void setDiagram(QgsDiagram *d)
virtual QgsDiagramRenderer * clone() const =0
Returns new instance that is equivalent to this one.
Stores the settings for rendering a single diagram.
StackedDiagramMode
Orientation of the stacked diagrams.
virtual QString diagramName() const =0
Gets a descriptive name for this diagram type.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
static const QString DIAGRAM_NAME_HISTOGRAM
Alters the size of rendered diagrams using a linear scaling.
void setDiagramSettings(const QgsDiagramSettings &s)
static const QString DIAGRAM_RENDERER_NAME_LINEARLY_INTERPOLATED
Map canvas is a class for displaying all GIS data types on a canvas.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
static const QString DIAGRAM_NAME_PIE
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setDirty(bool b=true)
Flag the project as dirty (modified).
Renders the diagrams for all features with the same settings.
void setDiagramSettings(const QgsDiagramSettings &s)
static const QString DIAGRAM_RENDERER_NAME_SINGLE_CATEGORY
static const QString DIAGRAM_NAME_STACKED_BAR
Dialog for editing sub diagrams.
QgsStackedDiagramPropertiesDialog(QgsVectorLayer *layer, QWidget *parent=nullptr, QgsMapCanvas *mapCanvas=nullptr)
Constructor for QgsStackedDiagramPropertiesDialog.
QgsDiagramLayerSettings diagramLayerSettings() const
Gets diagram layer settings built from the diagram properties widget.
void setAllowedToEditDiagramLayerSettings(bool allowed) const
Delegates to the main widget to set whether the widget should show diagram layer settings to be edite...
void syncToRenderer(const QgsDiagramRenderer *dr) const
Delegates to the diagram properties widget to sync with the given renderer.
void syncToSettings(const QgsDiagramLayerSettings *dls) const
Delegates to the diagram properties widget to sync with the given diagram layer settings.
bool isAllowedToEditDiagramLayerSettings() const
Returns whether the main widget is allowed to edit diagram layer settings.
void accept() override
Applies changes from the widget to the internal renderer and diagram layer settings.
QgsDiagramRenderer * renderer()
Gets a renderer object built from the diagram properties widget.
Model for sub diagrams in a stacked diagram view.
~QgsStackedDiagramPropertiesModel() override
void updateSubDiagram(const QModelIndex &index, QgsDiagramRenderer *dr)
Replaces the diagram located at index by dr. Takes ownership.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void insertSubDiagram(const int index, QgsDiagramRenderer *newSubDiagram)
Inserts a new diagram at the specified position. Takes ownership.
QList< QgsDiagramRenderer * > mRenderers
QgsDiagramLayerSettings mDiagramLayerSettings
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QgsDiagramRenderer * subDiagramForIndex(const QModelIndex &index) const
Returns the diagram renderer at the specified index. Does not transfer ownership.
void updateDiagramLayerSettings(QgsDiagramLayerSettings dls)
Sets the diagram layer settings for the model.
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsDiagramLayerSettings diagramLayerSettings() const
Returns the diagram layer settings from the model.
QList< QgsDiagramRenderer * > subRenderers() const
Returns the list of diagram renderers from the model. Does not transfer ownership.
int columnCount(const QModelIndex &=QModelIndex()) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QgsStackedDiagramPropertiesModel(QObject *parent=nullptr)
constructor
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &=QModelIndex()) const override
void syncToLayer()
Updates the widget to reflect the layer's current diagram settings.
void auxiliaryFieldCreated()
QgsStackedDiagramProperties(QgsVectorLayer *layer, QWidget *parent, QgsMapCanvas *canvas)
Renders diagrams using mixed diagram render types.
void setDiagramSettings(const QgsDiagramSettings &s)
static const QString DIAGRAM_RENDERER_NAME_STACKED
void addRenderer(QgsDiagramRenderer *renderer)
Adds a renderer to the stacked renderer object.
QList< QgsDiagramRenderer * > renderers(bool sortByDiagramMode=false) const
Returns an ordered list with the renderers of the stacked renderer object.
A diagram composed of several subdiagrams, located side by side.
static const QString DIAGRAM_NAME_STACKED
static const QString DIAGRAM_NAME_TEXT
Represents a vector layer which manages a vector based data sets.
const QgsDiagramLayerSettings * diagramLayerSettings() const
void setDiagramLayerSettings(const QgsDiagramLayerSettings &s)
void setDiagramRenderer(QgsDiagramRenderer *r)
Sets diagram rendering object (takes ownership)
const QgsDiagramRenderer * diagramRenderer() const