28 #include <QStandardItemModel> 29 #include <QStandardItem> 31 #include <QToolButton> 35 static const int EFFECT_ITEM_TYPE = QStandardItem::UserType + 1;
37 class EffectItem :
public QStandardItem
44 mWidget = propertiesWidget;
53 int type()
const override {
return EFFECT_ITEM_TYPE; }
60 QVariant data(
int role )
const override 62 if ( role == Qt::DisplayRole || role == Qt::EditRole )
66 if ( role == Qt::CheckStateRole )
68 return mEffect->enabled() ? Qt::Checked : Qt::Unchecked;
70 return QStandardItem::data( role );
73 void setData(
const QVariant &value,
int role )
override 75 if ( role == Qt::CheckStateRole )
77 mEffect->setEnabled( value.toBool() );
78 mWidget->updatePreview();
82 QStandardItem::setData( value, role );
110 this->layout()->setContentsMargins( 0, 0, 0, 0 );
112 mEffectsList->setMaximumHeight( static_cast< int >(
Qgis::UI_SCALE_FACTOR * fontMetrics().height() * 7 ) );
113 mEffectsList->setMinimumHeight( mEffectsList->maximumHeight() );
114 lblPreview->setMaximumWidth( mEffectsList->maximumHeight() );
121 mModel =
new QStandardItemModel();
123 mEffectsList->setModel(
mModel );
125 QItemSelectionModel *selModel = mEffectsList->selectionModel();
140 for (
int i = 0; i < stack->
count(); ++i )
143 if ( stack->
effect( stack->
count() - i - 1 )->enabled() )
149 QModelIndex newIndex = mEffectsList->model()->index( initialRow, 0 );
150 mEffectsList->setCurrentIndex( newIndex );
174 EffectItem *parent =
static_cast<EffectItem *
>(
mModel->invisibleRootItem() );
176 int count = stack->
count();
177 for (
int i = count - 1; i >= 0; i-- )
179 EffectItem *effectItem =
new EffectItem( stack->
effect( i ), this );
180 effectItem->setEditable(
false );
181 parent->appendRow( effectItem );
194 QModelIndex currentIdx = mEffectsList->currentIndex();
195 if ( !currentIdx.isValid() )
198 EffectItem *item =
static_cast<EffectItem *
>(
mModel->itemFromIndex( currentIdx ) );
200 QStandardItem *root =
mModel->invisibleRootItem();
201 int rowCount = root->rowCount();
202 int currentRow = item ? item->row() : 0;
204 mUpButton->setEnabled( currentRow > 0 );
205 mDownButton->setEnabled( currentRow < rowCount - 1 );
206 mRemoveButton->setEnabled( rowCount > 1 );
212 QImage previewImage( 100, 100, QImage::Format_ARGB32 );
213 previewImage.fill( Qt::transparent );
214 painter.begin( &previewImage );
215 painter.setRenderHint( QPainter::Antialiasing );
220 QPainter previewPicPainter;
221 previewPicPainter.begin( &previewPic );
222 previewPicPainter.setPen( Qt::red );
223 previewPicPainter.setBrush( QColor( 255, 100, 100, 255 ) );
224 previewPicPainter.drawEllipse( QPoint( 50, 50 ), 20, 20 );
225 previewPicPainter.end();
230 context.
painter()->translate( 20, 20 );
235 lblPreview->setPixmap( QPixmap::fromImage( previewImage ) );
241 QModelIndex idx = mEffectsList->currentIndex();
242 if ( !idx.isValid() )
245 EffectItem *item =
static_cast<EffectItem *
>(
mModel->itemFromIndex( idx ) );
266 int index = stackedWidget->addWidget( widget );
267 stackedWidget->setCurrentIndex( index );
282 EffectItem *newEffectItem =
new EffectItem( newEffect,
this );
285 mEffectsList->setCurrentIndex(
mModel->indexFromItem( newEffectItem ) );
293 int row = item->row();
294 QStandardItem *root =
mModel->invisibleRootItem();
296 int layerIdx = root->rowCount() - row - 1;
299 mModel->invisibleRootItem()->removeRow( row );
301 int newSelection = std::min( row, root->rowCount() - 1 );
302 QModelIndex newIdx = root->child( newSelection )->index();
303 mEffectsList->setCurrentIndex( newIdx );
327 int row = item->row();
329 QStandardItem *root =
mModel->invisibleRootItem();
331 int layerIdx = root->rowCount() - row - 1;
336 QList<QStandardItem *> toMove = root->takeRow( row );
337 root->insertRows( row + offset, toMove );
339 QModelIndex newIdx = toMove[ 0 ]->index();
340 mEffectsList->setCurrentIndex( newIdx );
349 item->setEffect( newEffect );
351 QStandardItem *root =
mModel->invisibleRootItem();
352 int effectIdx = root->rowCount() - item->row() - 1;
366 :
QgsDialog( parent, f, QDialogButtonBox::Ok | QDialogButtonBox::Cancel )
369 setWindowTitle( tr(
"Effect Properties" ) );
392 QHBoxLayout *layout =
new QHBoxLayout();
393 layout->setContentsMargins( 0, 0, 0, 0 );
394 layout->setSpacing( 6 );
397 mEnabledCheckBox =
new QCheckBox(
this );
398 mEnabledCheckBox->setText( tr(
"Draw effects" ) );
399 layout->addWidget( mEnabledCheckBox );
401 mButton =
new QToolButton(
this );
403 mButton->setToolTip( tr(
"Customize effects" ) );
404 layout->addWidget( mButton );
406 setFocusPolicy( Qt::StrongFocus );
407 setFocusProxy( mEnabledCheckBox );
409 connect( mButton, &QAbstractButton::clicked,
this, &QgsEffectStackCompactWidget::showDialog );
410 connect( mEnabledCheckBox, &QAbstractButton::toggled,
this, &QgsEffectStackCompactWidget::enableToggled );
417 delete mPreviewPicture;
424 mEnabledCheckBox->setChecked(
false );
425 mEnabledCheckBox->setEnabled(
false );
426 mButton->setEnabled(
false );
440 mEnabledCheckBox->setChecked( mStack->
enabled() );
441 mEnabledCheckBox->setEnabled(
true );
442 mButton->setEnabled( mStack->
enabled() );
452 delete mPreviewPicture;
453 mPreviewPicture =
new QPicture( picture );
456 void QgsEffectStackCompactWidget::showDialog()
463 if ( mPreviewPicture )
481 void QgsEffectStackCompactWidget::enableToggled(
bool checked )
489 mButton->setEnabled( checked );
493 void QgsEffectStackCompactWidget::updateAcceptWidget(
QgsPanelWidget *panel )
496 *mStack = *widget->
stack();
501 void QgsEffectStackCompactWidget::updateEffectLive()
504 *mStack = *widget->
stack();
static const double UI_SCALE_FACTOR
UI scaling factor.
bool insertEffect(int index, QgsPaintEffect *effect)
Inserts an effect at a specified index within the stack.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Base class for visual effects which can be applied to QPicture drawings.
A generic dialog with layout and button box.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects. ...
void setEnabled(bool enabled)
Sets whether the effect is enabled.
int count() const
Returns count of effects contained by the stack.
void setPreviewPicture(const QPicture &picture)
Sets the picture to use for effect previews for the dialog.
QgsPaintEffect * effect(int index) const
Returns a pointer to the effect at a specified index within the stack.
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
A paint effect which consists of a stack of other chained paint effects.
QgsEffectStack * clone() const override
Duplicates an effect by creating a deep copy of the effect.
bool enabled() const
Returns whether the effect is enabled.
QgsEffectStackPropertiesDialog(QgsEffectStack *stack, QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
QgsEffectStackPropertiesDialog constructor.
QgsEffectStack * stack()
Returns effect stack attached to the dialog.
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
QgsEffectStackPropertiesWidget * mPropertiesWidget
virtual void render(QPicture &picture, QgsRenderContext &context)
Renders a picture using the effect.
QgsPaintEffect * takeEffect(int index)
Removes an effect from the stack and returns a pointer to it.
A paint effect which draws the source picture with minor or no alterations.
bool changeEffect(int index, QgsPaintEffect *effect)
Replaces the effect at a specified position within the stack.