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