/src/app/decorations/qgsdecorationtitledialog.cpp

https://github.com/ricardogsilva/Quantum-GIS · C++ · 144 lines · 103 code · 22 blank · 19 comment · 10 complexity · 8eea00867c4dcbeb2c3090bd6af46a0c MD5 · raw file

  1. /***************************************************************************
  2. qgsdecorationtitledialog.cpp
  3. --------------------------------------
  4. Date : November 2018
  5. Copyright : (C) 2018 by Mathieu Pellerin
  6. Email : nirvn dot asia at gmail dot com
  7. ***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include "qgsdecorationtitledialog.h"
  16. #include "qgsdecorationtitle.h"
  17. #include "qgisapp.h"
  18. #include "qgsexpression.h"
  19. #include "qgsexpressionbuilderdialog.h"
  20. #include "qgsexpressioncontext.h"
  21. #include "qgshelp.h"
  22. #include "qgsmapcanvas.h"
  23. #include "qgsgui.h"
  24. #include <QColorDialog>
  25. #include <QColor>
  26. #include <QFont>
  27. #include <QDialogButtonBox>
  28. #include <QPushButton>
  29. QgsDecorationTitleDialog::QgsDecorationTitleDialog( QgsDecorationTitle &deco, QWidget *parent )
  30. : QDialog( parent )
  31. , mDeco( deco )
  32. {
  33. setupUi( this );
  34. QgsGui::enableAutoGeometryRestore( this );
  35. connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsDecorationTitleDialog::buttonBox_accepted );
  36. connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsDecorationTitleDialog::buttonBox_rejected );
  37. connect( mInsertExpressionButton, &QPushButton::clicked, this, &QgsDecorationTitleDialog::mInsertExpressionButton_clicked );
  38. connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsDecorationTitleDialog::showHelp );
  39. QPushButton *applyButton = buttonBox->button( QDialogButtonBox::Apply );
  40. connect( applyButton, &QAbstractButton::clicked, this, &QgsDecorationTitleDialog::apply );
  41. grpEnable->setChecked( mDeco.enabled() );
  42. // label text
  43. txtTitleText->setAcceptRichText( false );
  44. if ( !mDeco.enabled() && mDeco.mLabelText.isEmpty() )
  45. {
  46. const QString defaultString = QgsProject::instance()->metadata().title();
  47. txtTitleText->setPlainText( defaultString );
  48. }
  49. else
  50. {
  51. txtTitleText->setPlainText( mDeco.mLabelText );
  52. }
  53. // background bar color
  54. pbnBackgroundColor->setAllowOpacity( true );
  55. pbnBackgroundColor->setColor( mDeco.mBackgroundColor );
  56. pbnBackgroundColor->setContext( QStringLiteral( "gui" ) );
  57. pbnBackgroundColor->setColorDialogTitle( tr( "Select Background Bar Color" ) );
  58. // placement
  59. cboPlacement->addItem( tr( "Top Left" ), QgsDecorationItem::TopLeft );
  60. cboPlacement->addItem( tr( "Top Center" ), QgsDecorationItem::TopCenter );
  61. cboPlacement->addItem( tr( "Top Right" ), QgsDecorationItem::TopRight );
  62. cboPlacement->addItem( tr( "Bottom Left" ), QgsDecorationItem::BottomLeft );
  63. cboPlacement->addItem( tr( "Bottom Center" ), QgsDecorationItem::BottomCenter );
  64. cboPlacement->addItem( tr( "Bottom Right" ), QgsDecorationItem::BottomRight );
  65. connect( cboPlacement, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
  66. {
  67. spnHorizontal->setMinimum( cboPlacement->currentData() == QgsDecorationItem::TopCenter || cboPlacement->currentData() == QgsDecorationItem::BottomCenter ? -100 : 0 );
  68. } );
  69. cboPlacement->setCurrentIndex( cboPlacement->findData( mDeco.placement() ) );
  70. spnHorizontal->setClearValue( 0 );
  71. spnHorizontal->setValue( mDeco.mMarginHorizontal );
  72. spnVertical->setValue( mDeco.mMarginVertical );
  73. wgtUnitSelection->setUnits( QgsUnitTypes::RenderUnitList() << QgsUnitTypes::RenderMillimeters << QgsUnitTypes::RenderPercentage << QgsUnitTypes::RenderPixels );
  74. wgtUnitSelection->setUnit( mDeco.mMarginUnit );
  75. // font settings
  76. mButtonFontStyle->setDialogTitle( tr( "Title Label Text Format" ) );
  77. mButtonFontStyle->setMapCanvas( QgisApp::instance()->mapCanvas() );
  78. mButtonFontStyle->setTextFormat( mDeco.textFormat() );
  79. }
  80. void QgsDecorationTitleDialog::buttonBox_accepted()
  81. {
  82. apply();
  83. accept();
  84. }
  85. void QgsDecorationTitleDialog::buttonBox_rejected()
  86. {
  87. reject();
  88. }
  89. void QgsDecorationTitleDialog::mInsertExpressionButton_clicked()
  90. {
  91. QString selText = txtTitleText->textCursor().selectedText();
  92. // edit the selected expression if there's one
  93. if ( selText.startsWith( QLatin1String( "[%" ) ) && selText.endsWith( QLatin1String( "%]" ) ) )
  94. selText = selText.mid( 2, selText.size() - 4 );
  95. selText = selText.replace( QChar( 0x2029 ), QChar( '\n' ) );
  96. QgsExpressionBuilderDialog exprDlg( nullptr, selText, this, QStringLiteral( "generic" ), QgisApp::instance()->mapCanvas()->mapSettings().expressionContext() );
  97. exprDlg.setWindowTitle( QObject::tr( "Insert Expression" ) );
  98. if ( exprDlg.exec() == QDialog::Accepted )
  99. {
  100. const QString expression = exprDlg.expressionText();
  101. if ( !expression.isEmpty() )
  102. {
  103. txtTitleText->insertPlainText( "[%" + expression + "%]" );
  104. }
  105. }
  106. }
  107. void QgsDecorationTitleDialog::apply()
  108. {
  109. mDeco.setTextFormat( mButtonFontStyle->textFormat() );
  110. mDeco.mLabelText = txtTitleText->toPlainText();
  111. mDeco.mBackgroundColor = pbnBackgroundColor->color();
  112. mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->currentData().toInt() ) );
  113. mDeco.mMarginUnit = wgtUnitSelection->unit();
  114. mDeco.mMarginHorizontal = spnHorizontal->value();
  115. mDeco.mMarginVertical = spnVertical->value();
  116. mDeco.setEnabled( grpEnable->isChecked() );
  117. mDeco.update();
  118. }
  119. void QgsDecorationTitleDialog::showHelp()
  120. {
  121. QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#title_label_decoration" ) );
  122. }