QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsprocessingmodelcomponent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingmodelcomponent.cpp
3  -------------------------------
4  begin : June 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
20 #include "qgssymbollayerutils.h"
21 
23 
24 QgsProcessingModelComponent::QgsProcessingModelComponent( const QString &description )
25  : mDescription( description )
26 {}
27 
28 QString QgsProcessingModelComponent::description() const
29 {
30  return mDescription;
31 }
32 
33 void QgsProcessingModelComponent::setDescription( const QString &description )
34 {
35  mDescription = description;
36 }
37 
38 QPointF QgsProcessingModelComponent::position() const
39 {
40  return mPosition;
41 }
42 
43 void QgsProcessingModelComponent::setPosition( QPointF position )
44 {
45  mPosition = position;
46 }
47 
48 QSizeF QgsProcessingModelComponent::size() const
49 {
50  return mSize;
51 }
52 
53 void QgsProcessingModelComponent::setSize( QSizeF size )
54 {
55  mSize = size;
56 }
57 
58 QColor QgsProcessingModelComponent::color() const
59 {
60  return mColor;
61 }
62 
63 void QgsProcessingModelComponent::setColor( const QColor &color )
64 {
65  mColor = color;
66 }
67 
68 bool QgsProcessingModelComponent::linksCollapsed( Qt::Edge edge ) const
69 {
70  switch ( edge )
71  {
72  case Qt::TopEdge:
73  return mTopEdgeLinksCollapsed;
74 
75  case Qt::BottomEdge:
76  return mBottomEdgeLinksCollapsed;
77 
78  case Qt::LeftEdge:
79  case Qt::RightEdge:
80  return false;
81  }
82  return false;
83 }
84 
85 void QgsProcessingModelComponent::setLinksCollapsed( Qt::Edge edge, bool collapsed )
86 {
87  switch ( edge )
88  {
89  case Qt::TopEdge:
90  mTopEdgeLinksCollapsed = collapsed;
91  break;
92 
93  case Qt::BottomEdge:
94  mBottomEdgeLinksCollapsed = collapsed;
95  break;
96 
97  case Qt::LeftEdge:
98  case Qt::RightEdge:
99  break;
100  }
101 }
102 
103 void QgsProcessingModelComponent::setComment( const QgsProcessingModelComment & )
104 {
105 
106 }
107 
108 void QgsProcessingModelComponent::saveCommonProperties( QVariantMap &map ) const
109 {
110  map.insert( QStringLiteral( "component_pos_x" ), mPosition.x() );
111  map.insert( QStringLiteral( "component_pos_y" ), mPosition.y() );
112  map.insert( QStringLiteral( "component_description" ), mDescription );
113  map.insert( QStringLiteral( "component_width" ), mSize.width() );
114  map.insert( QStringLiteral( "component_height" ), mSize.height() );
115  map.insert( QStringLiteral( "parameters_collapsed" ), mTopEdgeLinksCollapsed );
116  map.insert( QStringLiteral( "outputs_collapsed" ), mBottomEdgeLinksCollapsed );
117  map.insert( QStringLiteral( "color" ), mColor.isValid() ? QgsSymbolLayerUtils::encodeColor( mColor ) : QString() );
118  const QgsProcessingModelComment *thisComment = comment();
119  if ( thisComment )
120  map.insert( QStringLiteral( "comment" ), thisComment->toVariant() );
121 }
122 
123 void QgsProcessingModelComponent::restoreCommonProperties( const QVariantMap &map )
124 {
125  QPointF pos;
126  pos.setX( map.value( QStringLiteral( "component_pos_x" ) ).toDouble() );
127  pos.setY( map.value( QStringLiteral( "component_pos_y" ) ).toDouble() );
128  mPosition = pos;
129  mDescription = map.value( QStringLiteral( "component_description" ) ).toString();
130  mSize.setWidth( map.value( QStringLiteral( "component_width" ), QString::number( DEFAULT_COMPONENT_WIDTH ) ).toDouble() );
131  mSize.setHeight( map.value( QStringLiteral( "component_height" ), QString::number( DEFAULT_COMPONENT_HEIGHT ) ).toDouble() );
132  mColor = map.value( QStringLiteral( "color" ) ).toString().isEmpty() ? QColor() : QgsSymbolLayerUtils::decodeColor( map.value( QStringLiteral( "color" ) ).toString() );
133  mTopEdgeLinksCollapsed = map.value( QStringLiteral( "parameters_collapsed" ) ).toBool();
134  mBottomEdgeLinksCollapsed = map.value( QStringLiteral( "outputs_collapsed" ) ).toBool();
135  QgsProcessingModelComment *thisComment = comment();
136  if ( thisComment )
137  thisComment->loadVariant( map.value( QStringLiteral( "comment" ) ).toMap() );
138 }
139 
140 void QgsProcessingModelComponent::copyNonDefinitionProperties( const QgsProcessingModelComponent &other )
141 {
142  setPosition( other.position() );
143  setSize( other.size() );
144  setLinksCollapsed( Qt::TopEdge, other.linksCollapsed( Qt::TopEdge ) );
145  setLinksCollapsed( Qt::BottomEdge, other.linksCollapsed( Qt::BottomEdge ) );
146  QgsProcessingModelComment *thisComment = comment();
147  const QgsProcessingModelComment *otherComment = other.comment();
148  if ( thisComment && otherComment )
149  {
150  if ( !otherComment->position().isNull() )
151  thisComment->setPosition( otherComment->position() );
152  else
153  thisComment->setPosition( other.position() + QPointF( size().width(), -1.5 * size().height() ) );
154  thisComment->setSize( otherComment->size() );
155  }
156 }
157 
static QString encodeColor(const QColor &color)