QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgslayoutattributeselectiondialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutattributeselectiondialog.cpp
3 -------------------------------------
4 begin : November 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
19#include "moc_qgslayoutattributeselectiondialog.cpp"
21#include "qgsvectorlayer.h"
23#include "qgsdoublespinbox.h"
24#include "qgssettings.h"
25#include "qgsgui.h"
27#include "qgshelp.h"
28
29#include <QCheckBox>
30#include <QDialogButtonBox>
31#include <QGridLayout>
32#include <QLabel>
33#include <QLineEdit>
34#include <QPushButton>
35#include <QSpinBox>
36#include <QSortFilterProxyModel>
37
38
39
40// QgsLayoutAttributeTableColumnModelBase
41
43 : QAbstractTableModel( parent )
44 , mTable( table )
45{
46}
47
48QModelIndex QgsLayoutAttributeTableColumnModelBase::index( int row, int column, const QModelIndex &parent ) const
49{
50 if ( !hasIndex( row, column, parent ) )
51 return QModelIndex();
52
53 if ( !parent.isValid() )
54 {
55 return createIndex( row, column );
56 }
57
58 return QModelIndex();
59}
60
61QModelIndex QgsLayoutAttributeTableColumnModelBase::parent( const QModelIndex &child ) const
62{
63 Q_UNUSED( child )
64 return QModelIndex();
65}
66
67int QgsLayoutAttributeTableColumnModelBase::rowCount( const QModelIndex &parent ) const
68{
69 if ( parent.isValid() )
70 return 0;
71
72 return columns().length();
73}
74
75int QgsLayoutAttributeTableColumnModelBase::columnCount( const QModelIndex &parent ) const
76{
77 Q_UNUSED( parent )
78 return displayedColumns().count();
79}
80
81QVariant QgsLayoutAttributeTableColumnModelBase::data( const QModelIndex &index, int role ) const
82{
83 if ( !index.isValid() ||
84 ( role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::UserRole ) )
85 {
86 return QVariant();
87 }
88
89 if ( index.row() >= columns().length() )
90 {
91 return QVariant();
92 }
93
94 // get layout column for index
95 const QgsLayoutTableColumn column = columns().value( index.row() );
96
97 const Column col = displayedColumns()[index.column()];
98 switch ( col )
99 {
100 case Attribute:
101 return column.attribute();
102 case Heading:
103 return column.heading();
104 case Alignment:
105 {
106 if ( role == Qt::DisplayRole )
107 {
108 switch ( column.hAlignment() )
109 {
110 case Qt::AlignHCenter:
111 switch ( column.vAlignment() )
112 {
113 case Qt::AlignTop:
114 return tr( "Top center" );
115 case Qt::AlignBottom:
116 return tr( "Bottom center" );
117 default:
118 return tr( "Middle center" );
119 }
120 case Qt::AlignRight:
121 switch ( column.vAlignment() )
122 {
123 case Qt::AlignTop:
124 return tr( "Top right" );
125 case Qt::AlignBottom:
126 return tr( "Bottom right" );
127 default:
128 return tr( "Middle right" );
129 }
130 case Qt::AlignLeft:
131 default:
132 switch ( column.vAlignment() )
133 {
134 case Qt::AlignTop:
135 return tr( "Top left" );
136 case Qt::AlignBottom:
137 return tr( "Bottom left" );
138 default:
139 return tr( "Middle left" );
140 }
141 }
142 }
143 else
144 {
145 //edit role
146 return int( column.hAlignment() | column.vAlignment() );
147 }
148 }
149 case Width:
150 {
151 if ( role == Qt::DisplayRole )
152 {
153 return column.width() <= 0 ? tr( "Automatic" ) : tr( "%L1 mm" ).arg( column.width(), 0, 'f', 2 );
154 }
155 else
156 {
157 //edit role
158 return column.width();
159 }
160 }
161 case SortOrder:
162 if ( role == Qt::DisplayRole )
163 {
164 switch ( column.sortOrder() )
165 {
166 case Qt::DescendingOrder:
167 return tr( "Descending" );
168 case Qt::AscendingOrder:
169 default:
170 return tr( "Ascending" );
171 }
172 }
173 else
174 {
175 //edit role
176 return column.sortOrder();
177 }
178 }
179
180 return QVariant();
181}
182
183QVariant QgsLayoutAttributeTableColumnModelBase::headerData( int section, Qt::Orientation orientation, int role ) const
184{
185 if ( !mTable )
186 {
187 return QVariant();
188 }
189
190 if ( role == Qt::DisplayRole )
191 {
192 if ( orientation == Qt::Vertical ) //row
193 {
194 return QVariant( section );
195 }
196 else
197 {
198 const Column col = displayedColumns()[section];
199 switch ( col )
200 {
201 case Attribute:
202 return QVariant( tr( "Attribute" ) );
203
204 case Heading:
205 return QVariant( tr( "Heading" ) );
206
207 case Alignment:
208 return QVariant( tr( "Alignment" ) );
209
210 case Width:
211 return QVariant( tr( "Width" ) );
212
213 case SortOrder:
214 return QVariant( tr( "Sort Order" ) );
215 }
216 }
217 }
218
219 return QVariant();
220}
221
222bool QgsLayoutAttributeTableColumnModelBase::setData( const QModelIndex &index, const QVariant &value, int role )
223{
224 if ( !index.isValid() || role != Qt::EditRole || !mTable )
225 return false;
226
227 if ( index.row() >= columns().length() )
228 return false;
229
230 if ( index.column() > displayedColumns().count() )
231 return false;
232
233 QgsLayoutTableColumn &colToUpdate = columns()[index.row()];
234
235 const Column col = displayedColumns()[index.column()];
236 switch ( col )
237 {
238 case Attribute:
239 // also update column's heading, if it hasn't been customized
240 if ( colToUpdate.heading().isEmpty() || ( colToUpdate.heading() == colToUpdate.attribute() ) )
241 {
242 colToUpdate.setHeading( value.toString() );
243 emit dataChanged( createIndex( index.row(), 1 ), createIndex( index.row(), 1 ) );
244 }
245 colToUpdate.setAttribute( value.toString() );
246 emit dataChanged( index, index );
247 return true;
248
249 case Heading:
250 colToUpdate.setHeading( value.toString() );
251 emit dataChanged( index, index );
252 return true;
253
254 case Alignment:
255 colToUpdate.setHAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignHorizontal_Mask ) );
256 colToUpdate.setVAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignVertical_Mask ) );
257 emit dataChanged( index, index );
258 return true;
259
260 case Width:
261 colToUpdate.setWidth( value.toDouble() );
262 emit dataChanged( index, index );
263 return true;
264
265 case SortOrder:
266 colToUpdate.setSortOrder( static_cast< Qt::SortOrder >( value.toInt() ) );
267 emit dataChanged( index, index );
268 return true;
269 }
270
271 return false;
272}
273
274Qt::ItemFlags QgsLayoutAttributeTableColumnModelBase::flags( const QModelIndex &index ) const
275{
276 Qt::ItemFlags flags = QAbstractTableModel::flags( index );
277
278 if ( index.isValid() )
279 {
280 return flags | Qt::ItemIsEditable;
281 }
282 else
283 {
284 return flags;
285 }
286}
287
288bool QgsLayoutAttributeTableColumnModelBase::removeRows( int row, int count, const QModelIndex &parent )
289{
290 Q_UNUSED( parent )
291
292 const int maxRow = std::min< int >( row + count - 1, columns().length() - 1 );
293 beginRemoveRows( QModelIndex(), row, maxRow );
294 //move backwards through rows, removing each corresponding QgsComposerTableColumn
295 for ( int i = maxRow; i >= row; --i )
296 {
297 columns().removeAt( i );
298 }
299 endRemoveRows();
300 return true;
301}
302
303bool QgsLayoutAttributeTableColumnModelBase::insertRows( int row, int count, const QModelIndex &parent )
304{
305 Q_UNUSED( parent )
306 beginInsertRows( QModelIndex(), row, row + count - 1 );
307 //create new QgsComposerTableColumns for each inserted row
308 for ( int i = row; i < row + count; ++i )
309 {
310 columns().insert( i, QgsLayoutTableColumn() );
311 }
312 endInsertRows();
313 return true;
314}
315
317{
318 if ( ( direction == ShiftUp && row <= 0 ) ||
319 ( direction == ShiftDown && row >= rowCount() - 1 ) )
320 {
321 //row is already at top/bottom
322 return false;
323 }
324
325 //we shift a row by removing the next row up/down, then reinserting it before/after the target row
326 const int swapWithRow = direction == ShiftUp ? row - 1 : row + 1;
327
328 //remove row
329 beginRemoveRows( QModelIndex(), swapWithRow, swapWithRow );
330 const QgsLayoutTableColumn temp = columns().takeAt( swapWithRow );
331 endRemoveRows();
332
333 //insert row
334 beginInsertRows( QModelIndex(), row, row );
335 columns().insert( row, temp );
336 endInsertRows();
337
338 return true;
339}
340
341// QgsLayoutAttributeTableColumnModel
342
343QVector<QgsLayoutTableColumn> &QgsLayoutAttributeTableColumnModel::columns() const
344{
345 return mTable->columns();
346}
347
349{
350 beginResetModel();
352 endResetModel();
353}
354
355
356// QgsLayoutTableSortModel
357
358QVector<QgsLayoutTableColumn> &QgsLayoutTableSortModel::columns() const
359{
360 return mTable->sortColumns();
361}
362
363// QgsLayoutColumnAlignmentDelegate
364
366{
367
368}
369
370QWidget *QgsLayoutColumnAlignmentDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
371{
372 Q_UNUSED( option )
373 Q_UNUSED( index )
374
375 //create a combo box showing alignment options
376 QComboBox *comboBox = new QComboBox( parent );
377
378 comboBox->addItem( tr( "Top Left" ), int( Qt::AlignTop | Qt::AlignLeft ) );
379 comboBox->addItem( tr( "Top Center" ), int( Qt::AlignTop | Qt::AlignHCenter ) );
380 comboBox->addItem( tr( "Top Right" ), int( Qt::AlignTop | Qt::AlignRight ) );
381 comboBox->addItem( tr( "Middle Left" ), int( Qt::AlignVCenter | Qt::AlignLeft ) );
382 comboBox->addItem( tr( "Middle Center" ), int( Qt::AlignVCenter | Qt::AlignHCenter ) );
383 comboBox->addItem( tr( "Middle Right" ), int( Qt::AlignVCenter | Qt::AlignRight ) );
384 comboBox->addItem( tr( "Bottom Left" ), int( Qt::AlignBottom | Qt::AlignLeft ) );
385 comboBox->addItem( tr( "Bottom Center" ), int( Qt::AlignBottom | Qt::AlignHCenter ) );
386 comboBox->addItem( tr( "Bottom Right" ), int( Qt::AlignBottom | Qt::AlignRight ) );
387
388 const Qt::AlignmentFlag alignment = ( Qt::AlignmentFlag )index.model()->data( index, Qt::EditRole ).toInt();
389 comboBox->setCurrentIndex( comboBox->findData( alignment ) );
390
391 return comboBox;
392}
393
394void QgsLayoutColumnAlignmentDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
395{
396 const Qt::AlignmentFlag alignment = ( Qt::AlignmentFlag )index.model()->data( index, Qt::EditRole ).toInt();
397
398 //set the value for the combobox
399 QComboBox *comboBox = static_cast<QComboBox *>( editor );
400 comboBox->setCurrentIndex( comboBox->findData( alignment ) );
401}
402
403void QgsLayoutColumnAlignmentDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
404{
405 QComboBox *comboBox = static_cast<QComboBox *>( editor );
406 const Qt::AlignmentFlag alignment = ( Qt::AlignmentFlag ) comboBox->currentData().toInt();
407 model->setData( index, alignment, Qt::EditRole );
408}
409
410void QgsLayoutColumnAlignmentDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
411{
412 Q_UNUSED( index )
413 editor->setGeometry( option.rect );
414}
415
416
417// QgsLayoutColumnSourceDelegate
418
419QgsLayoutColumnSourceDelegate::QgsLayoutColumnSourceDelegate( QgsVectorLayer *vlayer, QObject *parent, const QgsLayoutObject *layoutObject, bool forceExpressions )
420 : QItemDelegate( parent )
421 , mVectorLayer( vlayer )
422 , mLayoutObject( layoutObject )
423 , mForceExpressions( forceExpressions )
424{
425
426}
427
428QgsExpressionContext QgsLayoutColumnSourceDelegate::createExpressionContext() const
429{
430 if ( !mLayoutObject )
431 {
432 return QgsExpressionContext();
433 }
434
435 QgsExpressionContext expContext = mLayoutObject->createExpressionContext();
436 expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "row_number" ), 1, true ) );
437 expContext.setHighlightedVariables( QStringList() << QStringLiteral( "row_number" ) );
438 return expContext;
439}
440
441QWidget *QgsLayoutColumnSourceDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
442{
443 Q_UNUSED( option )
444 Q_UNUSED( index )
445
446 QgsFieldExpressionWidget *fieldExpression = new QgsFieldExpressionWidget( parent );
447 fieldExpression->setLayer( mVectorLayer );
448 fieldExpression->registerExpressionContextGenerator( this );
449
450 //listen out for field changes
451 connect( fieldExpression, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, [ = ] { const_cast< QgsLayoutColumnSourceDelegate * >( this )->commitAndCloseEditor(); } );
452 return fieldExpression;
453}
454
455void QgsLayoutColumnSourceDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
456{
457 const QString field = index.model()->data( index, Qt::EditRole ).toString();
458
459 //set the value for the field combobox
460 QgsFieldExpressionWidget *fieldExpression = static_cast<QgsFieldExpressionWidget *>( editor );
461 fieldExpression->setField( field );
462}
463
464void QgsLayoutColumnSourceDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
465{
466 QgsFieldExpressionWidget *fieldExpression = static_cast<QgsFieldExpressionWidget *>( editor );
467 const QString field = mForceExpressions ? fieldExpression->asExpression() : fieldExpression->currentField();
468
469 model->setData( index, field, Qt::EditRole );
470}
471
472void QgsLayoutColumnSourceDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
473{
474 Q_UNUSED( index )
475 editor->setGeometry( option.rect );
476}
477
478void QgsLayoutColumnSourceDelegate::commitAndCloseEditor()
479{
480 QgsFieldExpressionWidget *fieldExpression = qobject_cast<QgsFieldExpressionWidget *>( sender() );
481 emit commitData( fieldExpression );
482}
483
484
485// QgsLayoutColumnSortOrderDelegate
486
488{
489
490}
491
492QWidget *QgsLayoutColumnSortOrderDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
493{
494 Q_UNUSED( option )
495 Q_UNUSED( index )
496
497 QComboBox *comboBox = new QComboBox( parent );
498 QStringList sortOrders;
499 sortOrders << tr( "Ascending" ) << tr( "Descending" );
500 comboBox->addItems( sortOrders );
501 return comboBox;
502}
503
504void QgsLayoutColumnSortOrderDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
505{
506 const Qt::SortOrder order = ( Qt::SortOrder )index.model()->data( index, Qt::EditRole ).toInt();
507
508 //set the value for the combobox
509 QComboBox *comboBox = static_cast<QComboBox *>( editor );
510 switch ( order )
511 {
512 case Qt::DescendingOrder:
513 comboBox->setCurrentIndex( 1 );
514 break;
515 case Qt::AscendingOrder:
516 default:
517 comboBox->setCurrentIndex( 0 );
518 break;
519 }
520}
521
522void QgsLayoutColumnSortOrderDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
523{
524 QComboBox *comboBox = static_cast<QComboBox *>( editor );
525 const int value = comboBox->currentIndex();
526 Qt::SortOrder order;
527 switch ( value )
528 {
529 case 1:
530 order = Qt::DescendingOrder;
531 break;
532 case 0:
533 default:
534 order = Qt::AscendingOrder;
535 break;
536 }
537
538 model->setData( index, order, Qt::EditRole );
539}
540
541void QgsLayoutColumnSortOrderDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
542{
543 Q_UNUSED( index )
544 editor->setGeometry( option.rect );
545}
546
547
548//
549// QgsLayoutColumnWidthDelegate
550//
551
553 : QItemDelegate( parent )
554{
555
556}
557
558QWidget *QgsLayoutColumnWidthDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
559{
560 Q_UNUSED( index )
561 Q_UNUSED( option )
562 QgsDoubleSpinBox *editor = new QgsDoubleSpinBox( parent );
563 editor->setMinimum( 0 );
564 editor->setMaximum( 1000 );
565 editor->setDecimals( 2 );
566 editor->setSuffix( tr( " mm" ) );
567 editor->setSpecialValueText( tr( "Automatic" ) );
568 editor->setShowClearButton( true );
569 return editor;
570}
571
572void QgsLayoutColumnWidthDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
573{
574 const int value = index.model()->data( index, Qt::EditRole ).toInt();
575
576 QgsDoubleSpinBox *spinBox = static_cast<QgsDoubleSpinBox *>( editor );
577 spinBox->setValue( value );
578}
579
580void QgsLayoutColumnWidthDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
581{
582 QgsDoubleSpinBox *spinBox = static_cast<QgsDoubleSpinBox *>( editor );
583 spinBox->interpretText();
584 const int value = spinBox->value();
585
586 model->setData( index, value, Qt::EditRole );
587}
588
589void QgsLayoutColumnWidthDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
590{
591 Q_UNUSED( index )
592 editor->setGeometry( option.rect );
593}
594
595
596// QgsLayoutAttributeSelectionDialog
597
599 QWidget *parent, Qt::WindowFlags f )
600 : QDialog( parent, f )
601 , mTable( table )
602
603{
604 setupUi( this );
606
607 connect( mRemoveColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mRemoveColumnPushButton_clicked );
608 connect( mAddColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mAddColumnPushButton_clicked );
609 connect( mColumnUpPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mColumnUpPushButton_clicked );
610 connect( mColumnDownPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mColumnDownPushButton_clicked );
611 connect( mResetColumnsPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mResetColumnsPushButton_clicked );
612 connect( mClearColumnsPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mClearColumnsPushButton_clicked );
613 connect( mAddSortColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mAddSortColumnPushButton_clicked );
614 connect( mRemoveSortColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mRemoveSortColumnPushButton_clicked );
615 connect( mSortColumnUpPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mSortColumnUpPushButton_clicked );
616 connect( mSortColumnDownPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mSortColumnDownPushButton_clicked );
617 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutAttributeSelectionDialog::showHelp );
618
619 if ( mTable )
620 {
621 //set up models, views and delegates
622 mColumnModel = new QgsLayoutAttributeTableColumnModel( mTable, mColumnsTableView );
623 mColumnsTableView->setModel( mColumnModel );
624 mColumnsTableView->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
625 mColumnSourceDelegate = new QgsLayoutColumnSourceDelegate( vLayer, mColumnsTableView, mTable );
626 mColumnsTableView->setItemDelegateForColumn( 0, mColumnSourceDelegate );
627 mColumnAlignmentDelegate = new QgsLayoutColumnAlignmentDelegate( mColumnsTableView );
628 mColumnsTableView->setItemDelegateForColumn( 2, mColumnAlignmentDelegate );
629 mColumnWidthDelegate = new QgsLayoutColumnWidthDelegate( mColumnsTableView );
630 mColumnsTableView->setItemDelegateForColumn( 3, mColumnWidthDelegate );
631
632 mSortColumnModel = new QgsLayoutTableSortModel( mTable, mSortColumnTableView );
633 mSortColumnTableView->setModel( mSortColumnModel );
634 mSortColumnTableView->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
635 mSortColumnSourceDelegate = new QgsLayoutColumnSourceDelegate( vLayer, mSortColumnTableView, mTable, true );
636 mSortColumnTableView->setItemDelegateForColumn( 0, mSortColumnSourceDelegate );
637 mSortColumnOrderDelegate = new QgsLayoutColumnSortOrderDelegate( mSortColumnTableView );
638 mSortColumnTableView->setItemDelegateForColumn( 1, mSortColumnOrderDelegate );
639 }
640}
641
642void QgsLayoutAttributeSelectionDialog::mRemoveColumnPushButton_clicked()
643{
644 //remove selected rows from model
645 const QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
646 const int count = indexes.count();
647
648 for ( int i = count; i > 0; --i )
649 mColumnModel->removeRow( indexes.at( i - 1 ).row(), QModelIndex() );
650}
651
652void QgsLayoutAttributeSelectionDialog::mAddColumnPushButton_clicked()
653{
654 //add a new row to the model
655 mColumnModel->insertRow( mColumnModel->rowCount() );
656}
657
658void QgsLayoutAttributeSelectionDialog::mColumnUpPushButton_clicked()
659{
660 //move selected row up
661
662 QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
663 const int count = indexes.count();
664
665 std::reverse( indexes.begin(), indexes.end() );
666 for ( int i = count; i > 0; --i )
667 mColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutAttributeTableColumnModelBase::ShiftUp );
668}
669
670void QgsLayoutAttributeSelectionDialog::mColumnDownPushButton_clicked()
671{
672 //move selected row down
673 const QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
674 const int count = indexes.count();
675
676 for ( int i = count; i > 0; --i )
677 mColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutAttributeTableColumnModelBase::ShiftDown );
678}
679
680void QgsLayoutAttributeSelectionDialog::mResetColumnsPushButton_clicked()
681{
682 //reset columns to match vector layer's fields
683 mColumnModel->resetToLayer();
684}
685
686void QgsLayoutAttributeSelectionDialog::mClearColumnsPushButton_clicked()
687{
688 //remove all columns
689 mColumnModel->removeRows( 0, mColumnModel->rowCount() );
690}
691
692void QgsLayoutAttributeSelectionDialog::mAddSortColumnPushButton_clicked()
693{
694 //add a new row to the model
695 mSortColumnModel->insertRow( mSortColumnModel->rowCount() );
696}
697
698void QgsLayoutAttributeSelectionDialog::mRemoveSortColumnPushButton_clicked()
699{
700 //remove selected rows from model
701 const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
702 const int count = indexes.count();
703
704 for ( int i = count; i > 0; --i )
705 mSortColumnModel->removeRow( indexes.at( i - 1 ).row(), QModelIndex() );
706}
707
708void QgsLayoutAttributeSelectionDialog::showHelp()
709{
710 QgsHelp::openHelp( QStringLiteral( "print_composer/composer_items/composer_attribute_table.html" ) );
711}
712
713void QgsLayoutAttributeSelectionDialog::mSortColumnDownPushButton_clicked()
714{
715 //move selected row down
716 const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
717 const int count = indexes.count();
718
719 for ( int i = count; i > 0; --i )
720 mSortColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutTableSortModel::ShiftDown );
721}
722
723void QgsLayoutAttributeSelectionDialog::mSortColumnUpPushButton_clicked()
724{
725 //move selected row up
726 const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
727 const int count = indexes.count();
728
729 for ( int i = count; i > 0; --i )
730 mSortColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutTableSortModel::ShiftUp );
731}
732
733
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
void setSpecialValueText(const QString &txt)
Set the special-value text to be txt If set, the spin box will display this text instead of a numeric...
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains...
void setField(const QString &fieldName)
sets the current field or expression in the widget
QString asExpression() const
Returns the currently selected field or expression.
void setLayer(QgsMapLayer *layer)
Sets the layer used to display the fields and expression.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
QString currentField(bool *isExpression=nullptr, bool *isValid=nullptr) const
currentField returns the currently selected field or expression if allowed
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...
Definition qgsgui.cpp:209
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
QgsLayoutAttributeSelectionDialog(QgsLayoutItemAttributeTable *table, QgsVectorLayer *vLayer, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
constructor
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Column
Available columns for the configuration table to be used by the model.
@ Attribute
Attribute for a field or an expression.
@ Alignment
Defines the alignment of the column.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool moveRow(int row, ShiftDirection direction)
Moves the specified row up or down in the model.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
virtual QVector< QgsLayoutTableColumn > & columns() const =0
To be reimplemented to provide the display or the sort columns.
QVariant data(const QModelIndex &index, int role) const override
QgsLayoutAttributeTableColumnModelBase(QgsLayoutItemAttributeTable *table, QObject *parent=nullptr)
Constructor for QgsLayoutAttributeTableColumnModel.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
virtual QList< Column > displayedColumns() const =0
To be reimplemented to choose which column should be used by the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QModelIndex parent(const QModelIndex &child) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
ShiftDirection
Controls whether a row/column is shifted up or down.
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
A model for displaying columns shown in a QgsLayoutAttributeTable.
QVector< QgsLayoutTableColumn > & columns() const override
To be reimplemented to provide the display or the sort columns.
void resetToLayer()
Resets the attribute table's columns to match the source layer's fields.
A delegate for showing column alignment as a combo box.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QgsLayoutColumnAlignmentDelegate(QObject *parent=nullptr)
constructor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
A delegate for showing column sort order as a combo box.
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QgsLayoutColumnSortOrderDelegate(QObject *parent=nullptr)
constructor
A delegate for showing column attribute source as a QgsFieldExpressionWidget.
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
QgsLayoutColumnSourceDelegate(QgsVectorLayer *vlayer, QObject *parent=nullptr, const QgsLayoutObject *layoutObject=nullptr, bool forceExpressions=false)
Constructor for QgsLayoutColumnSourceDelegate.
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
A delegate for showing column width as a spin box.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
QgsLayoutColumnWidthDelegate(QObject *parent=nullptr)
constructor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
A layout table subclass that displays attributes from a vector layer.
void resetColumns()
Resets the attribute table's columns to match the vector layer's fields.
A base class for objects which belong to a layout.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the objects' current state.
Stores properties of a column for a QgsLayoutTable.
QString attribute() const
Returns the attribute name or expression used for the column's values.
void setVAlignment(Qt::AlignmentFlag alignment)
Sets the vertical alignment for a column, which controls the alignment used for drawing column values...
double width() const
Returns the width for the column in mm, or 0 if column width is automatically calculated.
Qt::AlignmentFlag vAlignment() const
Returns the vertical alignment for a column, which controls the alignment used for drawing column val...
QString heading() const
Returns the heading for a column, which is the value displayed in the column's header cell.
void setSortOrder(Qt::SortOrder order)
Sets the sort order for the column.
void setAttribute(const QString &attribute)
Sets the attribute name or expression used for the column's values.
Qt::SortOrder sortOrder() const
Returns the sort order for the column.
void setHAlignment(Qt::AlignmentFlag alignment)
Sets the horizontal alignment for a column, which controls the alignment used for drawing column valu...
void setHeading(const QString &heading)
Sets the heading for a column, which is the value displayed in the column's header cell.
Qt::AlignmentFlag hAlignment() const
Returns the horizontal alignment for a column, which controls the alignment used for drawing column v...
void setWidth(const double width)
Sets the width for a column in mm.
Allows for filtering QgsComposerAttributeTable columns by columns which are sorted or unsorted.
QVector< QgsLayoutTableColumn > & columns() const override
To be reimplemented to provide the display or the sort columns.
QgsLayoutTableColumns & columns()
Returns a reference to the list of QgsLayoutTableColumns shown in the table.
QgsLayoutTableSortColumns & sortColumns()
Returns a reference to the list of QgsLayoutTableSortColumns shown in the table.
Represents a vector layer which manages a vector based data sets.
Single variable definition for use within a QgsExpressionContextScope.