19 #include <QTreeWidgetItem>
20 #include <QDialogButtonBox>
21 #include <QVBoxLayout>
25 const QString QgsProcessingHelpEditorWidget::ALGORITHM_DESCRIPTION = QStringLiteral(
"ALG_DESC" );
26 const QString QgsProcessingHelpEditorWidget::ALGORITHM_CREATOR = QStringLiteral(
"ALG_CREATOR" );
27 const QString QgsProcessingHelpEditorWidget::ALGORITHM_HELP_CREATOR = QStringLiteral(
"ALG_HELP_CREATOR" );
28 const QString QgsProcessingHelpEditorWidget::ALGORITHM_VERSION = QStringLiteral(
"ALG_VERSION" );
29 const QString QgsProcessingHelpEditorWidget::ALGORITHM_SHORT_DESCRIPTION = QStringLiteral(
"SHORT_DESCRIPTION" );
30 const QString QgsProcessingHelpEditorWidget::ALGORITHM_HELP_URL = QStringLiteral(
"HELP_URL" );
31 const QString QgsProcessingHelpEditorWidget::ALGORITHM_EXAMPLES = QStringLiteral(
"EXAMPLES" );
34 class QgsProcessingHelpEditorTreeItem :
public QTreeWidgetItem
38 QgsProcessingHelpEditorTreeItem(
const QString &name,
const QString &description )
41 , description( description )
43 setText( 0, description );
52 QgsProcessingHelpEditorWidget::QgsProcessingHelpEditorWidget( QWidget *parent )
54 , mCurrentName( ALGORITHM_DESCRIPTION )
58 connect( mElementTree, &QTreeWidget::currentItemChanged,
this, &QgsProcessingHelpEditorWidget::changeItem );
59 connect( mTextEdit, &QTextEdit::textChanged,
this, [ = ]
61 if ( !mCurrentName.isEmpty() )
63 if ( mEditStackedWidget->currentWidget() == mPagePlainText )
65 mHelpContent[ mCurrentName] = mTextEdit->toPlainText();
73 if ( !mCurrentName.isEmpty() )
75 if ( mEditStackedWidget->currentWidget() == mPageRichEdit )
77 mHelpContent[ mCurrentName] = mRichTextEdit->toHtml();
84 QgsProcessingHelpEditorWidget::~QgsProcessingHelpEditorWidget() =
default;
93 if (
const QgsProcessingModelAlgorithm *model =
dynamic_cast< const QgsProcessingModelAlgorithm *
>( mAlgorithm.get() ) )
95 mHelpContent = model->helpContent();
98 mEditStackedWidget->setCurrentWidget( mPageRichEdit );
99 if ( mHelpContent.contains( ALGORITHM_DESCRIPTION ) )
101 mRichTextEdit->setText( mHelpContent.value( ALGORITHM_DESCRIPTION ).toString() );
104 mElementTree->addTopLevelItem(
new QgsProcessingHelpEditorTreeItem( ALGORITHM_DESCRIPTION, tr(
"Algorithm description" ) ) );
105 mElementTree->addTopLevelItem(
new QgsProcessingHelpEditorTreeItem( ALGORITHM_SHORT_DESCRIPTION, tr(
"Short description" ) ) );
107 QgsProcessingHelpEditorTreeItem *parametersItem =
new QgsProcessingHelpEditorTreeItem( QString(), tr(
"Input parameters" ) );
108 mElementTree->addTopLevelItem( parametersItem );
110 const QList< const QgsProcessingParameterDefinition * > definitions = mAlgorithm->parameterDefinitions();
116 parametersItem->addChild(
new QgsProcessingHelpEditorTreeItem( definition->name(), definition->description() ) );
119 QgsProcessingHelpEditorTreeItem *outputsItem =
new QgsProcessingHelpEditorTreeItem( QString(), tr(
"Outputs" ) );
120 mElementTree->addTopLevelItem( outputsItem );
121 const QList< const QgsProcessingOutputDefinition * > outputs = mAlgorithm->outputDefinitions();
124 outputsItem->addChild(
new QgsProcessingHelpEditorTreeItem( output->name(), output->description() ) );
127 mElementTree->addTopLevelItem(
new QgsProcessingHelpEditorTreeItem( ALGORITHM_EXAMPLES, tr(
"Examples" ) ) );
129 mElementTree->addTopLevelItem(
new QgsProcessingHelpEditorTreeItem( ALGORITHM_CREATOR, tr(
"Algorithm author" ) ) );
130 mElementTree->addTopLevelItem(
new QgsProcessingHelpEditorTreeItem( ALGORITHM_HELP_CREATOR, tr(
"Help author" ) ) );
131 mElementTree->addTopLevelItem(
new QgsProcessingHelpEditorTreeItem( ALGORITHM_VERSION, tr(
"Algorithm version" ) ) );
132 mElementTree->addTopLevelItem(
new QgsProcessingHelpEditorTreeItem( ALGORITHM_HELP_URL, tr(
"Documentation help URL (for help button)" ) ) );
137 QVariantMap QgsProcessingHelpEditorWidget::helpContent()
143 void QgsProcessingHelpEditorWidget::updateHtmlView()
145 mTextPreview->setHtml( formattedHelp() );
148 void QgsProcessingHelpEditorWidget::changeItem( QTreeWidgetItem *, QTreeWidgetItem * )
150 if ( QgsProcessingHelpEditorTreeItem *item =
dynamic_cast< QgsProcessingHelpEditorTreeItem *
>( mElementTree->currentItem() ) )
154 const QString name = item->name;
155 if ( !name.isEmpty() )
157 mTextEdit->setEnabled(
true );
158 mRichTextEdit->setEnabled(
true );
163 const bool useRichTextEdit = name == ALGORITHM_EXAMPLES || name == ALGORITHM_DESCRIPTION;
164 if ( useRichTextEdit )
166 mEditStackedWidget->setCurrentWidget( mPageRichEdit );
167 if ( mHelpContent.contains( name ) )
168 mRichTextEdit->setText( mHelpContent.value( name ).toString() );
170 mRichTextEdit->setText( QString() );
174 mEditStackedWidget->setCurrentWidget( mPagePlainText );
175 if ( mHelpContent.contains( name ) )
176 mTextEdit->setText( mHelpContent.value( name ).toString() );
183 mCurrentName.clear();
185 mRichTextEdit->setText( QString() );
186 mTextEdit->setEnabled(
false );
187 mRichTextEdit->setEnabled(
false );
188 mEditStackedWidget->setCurrentWidget( mPagePlainText );
194 QString QgsProcessingHelpEditorWidget::formattedHelp()
const
202 void QgsProcessingHelpEditorWidget::storeCurrentValue()
204 if ( !mCurrentName.isEmpty() )
206 if ( mEditStackedWidget->currentWidget() == mPagePlainText )
207 mHelpContent[ mCurrentName] = mTextEdit->toPlainText();
209 mHelpContent[ mCurrentName] = mRichTextEdit->toHtml();
213 QgsProcessingHelpEditorDialog::QgsProcessingHelpEditorDialog( QWidget *parent, Qt::WindowFlags flags )
214 : QDialog( parent, flags )
216 setObjectName( QStringLiteral(
"QgsProcessingHelpEditorDialog" ) );
218 QVBoxLayout *vLayout =
new QVBoxLayout();
219 mWidget =
new QgsProcessingHelpEditorWidget();
220 vLayout->addWidget( mWidget, 1 );
222 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
223 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
224 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
225 vLayout->addWidget( buttonBox );
226 setLayout( vLayout );
236 QVariantMap QgsProcessingHelpEditorDialog::helpContent()
238 return mWidget->helpContent();