QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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  const QgsLayoutTableColumn column = columns().value( index.row() );
95 
96  const 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  const 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  const 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  const int maxRow = std::min< int >( 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  const int swapWithRow = direction == ShiftUp ? row - 1 : row + 1;
326 
327  //remove row
328  beginRemoveRows( QModelIndex(), swapWithRow, swapWithRow );
329  const 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  const 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  const 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  const 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  const 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  const QString field = fieldExpression->expression();
466  model->setData( index, field, Qt::EditRole );
467 }
468 
469 void QgsLayoutColumnSourceDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
470 {
471  Q_UNUSED( index )
472  editor->setGeometry( option.rect );
473 }
474 
475 void QgsLayoutColumnSourceDelegate::commitAndCloseEditor()
476 {
477  QgsFieldExpressionWidget *fieldExpression = qobject_cast<QgsFieldExpressionWidget *>( sender() );
478  emit commitData( fieldExpression );
479 }
480 
481 
482 // QgsLayoutColumnSortOrderDelegate
483 
484 QgsLayoutColumnSortOrderDelegate::QgsLayoutColumnSortOrderDelegate( QObject *parent ) : QItemDelegate( parent )
485 {
486 
487 }
488 
489 QWidget *QgsLayoutColumnSortOrderDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
490 {
491  Q_UNUSED( option )
492  Q_UNUSED( index )
493 
494  QComboBox *comboBox = new QComboBox( parent );
495  QStringList sortOrders;
496  sortOrders << tr( "Ascending" ) << tr( "Descending" );
497  comboBox->addItems( sortOrders );
498  return comboBox;
499 }
500 
501 void QgsLayoutColumnSortOrderDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
502 {
503  const Qt::SortOrder order = ( Qt::SortOrder )index.model()->data( index, Qt::EditRole ).toInt();
504 
505  //set the value for the combobox
506  QComboBox *comboBox = static_cast<QComboBox *>( editor );
507  switch ( order )
508  {
509  case Qt::DescendingOrder:
510  comboBox->setCurrentIndex( 1 );
511  break;
512  case Qt::AscendingOrder:
513  default:
514  comboBox->setCurrentIndex( 0 );
515  break;
516  }
517 }
518 
519 void QgsLayoutColumnSortOrderDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
520 {
521  QComboBox *comboBox = static_cast<QComboBox *>( editor );
522  const int value = comboBox->currentIndex();
523  Qt::SortOrder order;
524  switch ( value )
525  {
526  case 1:
527  order = Qt::DescendingOrder;
528  break;
529  case 0:
530  default:
531  order = Qt::AscendingOrder;
532  break;
533  }
534 
535  model->setData( index, order, Qt::EditRole );
536 }
537 
538 void QgsLayoutColumnSortOrderDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
539 {
540  Q_UNUSED( index )
541  editor->setGeometry( option.rect );
542 }
543 
544 
545 //
546 // QgsLayoutColumnWidthDelegate
547 //
548 
550  : QItemDelegate( parent )
551 {
552 
553 }
554 
555 QWidget *QgsLayoutColumnWidthDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
556 {
557  Q_UNUSED( index )
558  Q_UNUSED( option )
559  QgsDoubleSpinBox *editor = new QgsDoubleSpinBox( parent );
560  editor->setMinimum( 0 );
561  editor->setMaximum( 1000 );
562  editor->setDecimals( 2 );
563  editor->setSuffix( tr( " mm" ) );
564  editor->setSpecialValueText( tr( "Automatic" ) );
565  editor->setShowClearButton( true );
566  return editor;
567 }
568 
569 void QgsLayoutColumnWidthDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
570 {
571  const int value = index.model()->data( index, Qt::EditRole ).toInt();
572 
573  QgsDoubleSpinBox *spinBox = static_cast<QgsDoubleSpinBox *>( editor );
574  spinBox->setValue( value );
575 }
576 
577 void QgsLayoutColumnWidthDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
578 {
579  QgsDoubleSpinBox *spinBox = static_cast<QgsDoubleSpinBox *>( editor );
580  spinBox->interpretText();
581  const int value = spinBox->value();
582 
583  model->setData( index, value, Qt::EditRole );
584 }
585 
586 void QgsLayoutColumnWidthDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
587 {
588  Q_UNUSED( index )
589  editor->setGeometry( option.rect );
590 }
591 
592 
593 // QgsLayoutAttributeSelectionDialog
594 
596  QWidget *parent, Qt::WindowFlags f )
597  : QDialog( parent, f )
598  , mTable( table )
599  , mVectorLayer( vLayer )
600 
601 {
602  setupUi( this );
604 
605  connect( mRemoveColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mRemoveColumnPushButton_clicked );
606  connect( mAddColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mAddColumnPushButton_clicked );
607  connect( mColumnUpPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mColumnUpPushButton_clicked );
608  connect( mColumnDownPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mColumnDownPushButton_clicked );
609  connect( mResetColumnsPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mResetColumnsPushButton_clicked );
610  connect( mClearColumnsPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mClearColumnsPushButton_clicked );
611  connect( mAddSortColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mAddSortColumnPushButton_clicked );
612  connect( mRemoveSortColumnPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mRemoveSortColumnPushButton_clicked );
613  connect( mSortColumnUpPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mSortColumnUpPushButton_clicked );
614  connect( mSortColumnDownPushButton, &QPushButton::clicked, this, &QgsLayoutAttributeSelectionDialog::mSortColumnDownPushButton_clicked );
615  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutAttributeSelectionDialog::showHelp );
616 
617  if ( mTable )
618  {
619  //set up models, views and delegates
620  mColumnModel = new QgsLayoutAttributeTableColumnModel( mTable, mColumnsTableView );
621  mColumnsTableView->setModel( mColumnModel );
622  mColumnsTableView->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
623  mColumnSourceDelegate = new QgsLayoutColumnSourceDelegate( vLayer, mColumnsTableView, mTable );
624  mColumnsTableView->setItemDelegateForColumn( 0, mColumnSourceDelegate );
625  mColumnAlignmentDelegate = new QgsLayoutColumnAlignmentDelegate( mColumnsTableView );
626  mColumnsTableView->setItemDelegateForColumn( 2, mColumnAlignmentDelegate );
627  mColumnWidthDelegate = new QgsLayoutColumnWidthDelegate( mColumnsTableView );
628  mColumnsTableView->setItemDelegateForColumn( 3, mColumnWidthDelegate );
629 
630  mSortColumnModel = new QgsLayoutTableSortModel( mTable, mSortColumnTableView );
631  mSortColumnTableView->setModel( mSortColumnModel );
632  mSortColumnTableView->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
633  mSortColumnSourceDelegate = new QgsLayoutColumnSourceDelegate( vLayer, mSortColumnTableView, mTable );
634  mSortColumnTableView->setItemDelegateForColumn( 0, mSortColumnSourceDelegate );
635  mSortColumnOrderDelegate = new QgsLayoutColumnSortOrderDelegate( mSortColumnTableView );
636  mSortColumnTableView->setItemDelegateForColumn( 1, mSortColumnOrderDelegate );
637  }
638 }
639 
640 void QgsLayoutAttributeSelectionDialog::mRemoveColumnPushButton_clicked()
641 {
642  //remove selected rows from model
643  const QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
644  const int count = indexes.count();
645 
646  for ( int i = count; i > 0; --i )
647  mColumnModel->removeRow( indexes.at( i - 1 ).row(), QModelIndex() );
648 }
649 
650 void QgsLayoutAttributeSelectionDialog::mAddColumnPushButton_clicked()
651 {
652  //add a new row to the model
653  mColumnModel->insertRow( mColumnModel->rowCount() );
654 }
655 
656 void QgsLayoutAttributeSelectionDialog::mColumnUpPushButton_clicked()
657 {
658  //move selected row up
659 
660  QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
661  const int count = indexes.count();
662 
663  std::reverse( indexes.begin(), indexes.end() );
664  for ( int i = count; i > 0; --i )
665  mColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutAttributeTableColumnModelBase::ShiftUp );
666 }
667 
668 void QgsLayoutAttributeSelectionDialog::mColumnDownPushButton_clicked()
669 {
670  //move selected row down
671  const QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
672  const int count = indexes.count();
673 
674  for ( int i = count; i > 0; --i )
675  mColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutAttributeTableColumnModelBase::ShiftDown );
676 }
677 
678 void QgsLayoutAttributeSelectionDialog::mResetColumnsPushButton_clicked()
679 {
680  //reset columns to match vector layer's fields
681  mColumnModel->resetToLayer();
682 }
683 
684 void QgsLayoutAttributeSelectionDialog::mClearColumnsPushButton_clicked()
685 {
686  //remove all columns
687  mColumnModel->removeRows( 0, mColumnModel->rowCount() );
688 }
689 
690 void QgsLayoutAttributeSelectionDialog::mAddSortColumnPushButton_clicked()
691 {
692  //add a new row to the model
693  mSortColumnModel->insertRow( mSortColumnModel->rowCount() );
694 }
695 
696 void QgsLayoutAttributeSelectionDialog::mRemoveSortColumnPushButton_clicked()
697 {
698  //remove selected rows from model
699  const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
700  const int count = indexes.count();
701 
702  for ( int i = count; i > 0; --i )
703  mSortColumnModel->removeRow( indexes.at( i - 1 ).row(), QModelIndex() );
704 }
705 
706 void QgsLayoutAttributeSelectionDialog::showHelp()
707 {
708  QgsHelp::openHelp( QStringLiteral( "print_composer/composer_items/composer_attribute_table.html" ) );
709 }
710 
711 void QgsLayoutAttributeSelectionDialog::mSortColumnDownPushButton_clicked()
712 {
713  //move selected row down
714  const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
715  const int count = indexes.count();
716 
717  for ( int i = count; i > 0; --i )
718  mSortColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutTableSortModel::ShiftDown );
719 }
720 
721 void QgsLayoutAttributeSelectionDialog::mSortColumnUpPushButton_clicked()
722 {
723  //move selected row up
724  const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
725  const int count = indexes.count();
726 
727  for ( int i = count; i > 0; --i )
728  mSortColumnModel->moveRow( indexes.at( i - 1 ).row(), QgsLayoutTableSortModel::ShiftUp );
729 }
730 
731 
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
QString expression() 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.
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:174
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:463
Single variable definition for use within a QgsExpressionContextScope.