29 #include <QDialogButtonBox>
30 #include <QGridLayout>
33 #include <QPushButton>
35 #include <QSortFilterProxyModel>
42 : QAbstractTableModel( parent )
49 if ( !hasIndex( row, column,
parent ) )
54 return createIndex( row, column );
82 if ( !
index.isValid() ||
83 ( role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::UserRole ) )
105 if ( role == Qt::DisplayRole )
109 case Qt::AlignHCenter:
113 return tr(
"Top center" );
114 case Qt::AlignBottom:
115 return tr(
"Bottom center" );
117 return tr(
"Middle center" );
123 return tr(
"Top right" );
124 case Qt::AlignBottom:
125 return tr(
"Bottom right" );
127 return tr(
"Middle right" );
134 return tr(
"Top left" );
135 case Qt::AlignBottom:
136 return tr(
"Bottom left" );
138 return tr(
"Middle left" );
150 if ( role == Qt::DisplayRole )
152 return column.
width() <= 0 ? tr(
"Automatic" ) : tr(
"%1 mm" ).arg( column.
width(), 0,
'f', 2 );
157 return column.
width();
161 if ( role == Qt::DisplayRole )
165 case Qt::DescendingOrder:
166 return tr(
"Descending" );
167 case Qt::AscendingOrder:
169 return tr(
"Ascending" );
189 if ( role == Qt::DisplayRole )
191 if ( orientation == Qt::Vertical )
193 return QVariant( section );
201 return QVariant( tr(
"Attribute" ) );
204 return QVariant( tr(
"Heading" ) );
207 return QVariant( tr(
"Alignment" ) );
210 return QVariant( tr(
"Width" ) );
213 return QVariant( tr(
"Sort Order" ) );
223 if ( !
index.isValid() || role != Qt::EditRole || !
mTable )
242 emit dataChanged( createIndex(
index.row(), 1 ), createIndex(
index.row(), 1 ) );
254 colToUpdate.
setHAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignHorizontal_Mask ) );
255 colToUpdate.
setVAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignVertical_Mask ) );
260 colToUpdate.
setWidth( value.toDouble() );
265 colToUpdate.
setSortOrder(
static_cast< Qt::SortOrder
>( value.toInt() ) );
275 Qt::ItemFlags
flags = QAbstractTableModel::flags(
index );
277 if (
index.isValid() )
279 return flags | Qt::ItemIsEditable;
291 const int maxRow = std::min< int >( row + count - 1,
columns().length() - 1 );
292 beginRemoveRows( QModelIndex(), row, maxRow );
294 for (
int i = maxRow; i >= row; --i )
305 beginInsertRows( QModelIndex(), row, row + count - 1 );
307 for (
int i = row; i < row + count; ++i )
317 if ( ( direction ==
ShiftUp && row <= 0 ) ||
325 const int swapWithRow = direction ==
ShiftUp ? row - 1 : row + 1;
328 beginRemoveRows( QModelIndex(), swapWithRow, swapWithRow );
333 beginInsertRows( QModelIndex(), row, row );
375 QComboBox *comboBox =
new QComboBox( parent );
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 ) );
387 const Qt::AlignmentFlag alignment = ( Qt::AlignmentFlag )index.model()->data( index, Qt::EditRole ).toInt();
388 comboBox->setCurrentIndex( comboBox->findData( alignment ) );
395 const Qt::AlignmentFlag alignment = ( Qt::AlignmentFlag )index.model()->data( index, Qt::EditRole ).toInt();
398 QComboBox *comboBox =
static_cast<QComboBox *
>( editor );
399 comboBox->setCurrentIndex( comboBox->findData( alignment ) );
404 QComboBox *comboBox =
static_cast<QComboBox *
>( editor );
405 const Qt::AlignmentFlag alignment = ( Qt::AlignmentFlag ) comboBox->currentData().toInt();
406 model->setData( index, alignment, Qt::EditRole );
412 editor->setGeometry( option.rect );
419 : QItemDelegate( parent )
420 , mVectorLayer( vlayer )
421 , mLayoutObject( layoutObject )
422 , mForceExpressions( forceExpressions )
429 if ( !mLayoutObject )
446 fieldExpression->
setLayer( mVectorLayer );
451 return fieldExpression;
456 const QString
field = index.model()->data( index, Qt::EditRole ).toString();
468 model->setData( index,
field, Qt::EditRole );
474 editor->setGeometry( option.rect );
477 void QgsLayoutColumnSourceDelegate::commitAndCloseEditor()
480 emit commitData( fieldExpression );
496 QComboBox *comboBox =
new QComboBox( parent );
497 QStringList sortOrders;
498 sortOrders << tr(
"Ascending" ) << tr(
"Descending" );
499 comboBox->addItems( sortOrders );
505 const Qt::SortOrder order = ( Qt::SortOrder )index.model()->data( index, Qt::EditRole ).toInt();
508 QComboBox *comboBox =
static_cast<QComboBox *
>( editor );
511 case Qt::DescendingOrder:
512 comboBox->setCurrentIndex( 1 );
514 case Qt::AscendingOrder:
516 comboBox->setCurrentIndex( 0 );
523 QComboBox *comboBox =
static_cast<QComboBox *
>( editor );
524 const int value = comboBox->currentIndex();
529 order = Qt::DescendingOrder;
533 order = Qt::AscendingOrder;
537 model->setData( index, order, Qt::EditRole );
543 editor->setGeometry( option.rect );
552 : QItemDelegate( parent )
562 editor->setMinimum( 0 );
563 editor->setMaximum( 1000 );
564 editor->setDecimals( 2 );
565 editor->setSuffix( tr(
" mm" ) );
573 const int value = index.model()->data( index, Qt::EditRole ).toInt();
576 spinBox->setValue( value );
582 spinBox->interpretText();
583 const int value = spinBox->value();
585 model->setData( index, value, Qt::EditRole );
591 editor->setGeometry( option.rect );
598 QWidget *parent, Qt::WindowFlags f )
599 : QDialog( parent, f )
601 , mVectorLayer( vLayer )
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 );
623 mColumnsTableView->setModel( mColumnModel );
624 mColumnsTableView->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
626 mColumnsTableView->setItemDelegateForColumn( 0, mColumnSourceDelegate );
628 mColumnsTableView->setItemDelegateForColumn( 2, mColumnAlignmentDelegate );
630 mColumnsTableView->setItemDelegateForColumn( 3, mColumnWidthDelegate );
633 mSortColumnTableView->setModel( mSortColumnModel );
634 mSortColumnTableView->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
636 mSortColumnTableView->setItemDelegateForColumn( 0, mSortColumnSourceDelegate );
638 mSortColumnTableView->setItemDelegateForColumn( 1, mSortColumnOrderDelegate );
642 void QgsLayoutAttributeSelectionDialog::mRemoveColumnPushButton_clicked()
645 const QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
646 const int count = indexes.count();
648 for (
int i = count; i > 0; --i )
649 mColumnModel->removeRow( indexes.at( i - 1 ).row(), QModelIndex() );
652 void QgsLayoutAttributeSelectionDialog::mAddColumnPushButton_clicked()
655 mColumnModel->insertRow( mColumnModel->
rowCount() );
658 void QgsLayoutAttributeSelectionDialog::mColumnUpPushButton_clicked()
662 QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
663 const int count = indexes.count();
665 std::reverse( indexes.begin(), indexes.end() );
666 for (
int i = count; i > 0; --i )
670 void QgsLayoutAttributeSelectionDialog::mColumnDownPushButton_clicked()
673 const QModelIndexList indexes = mColumnsTableView->selectionModel()->selectedRows();
674 const int count = indexes.count();
676 for (
int i = count; i > 0; --i )
680 void QgsLayoutAttributeSelectionDialog::mResetColumnsPushButton_clicked()
686 void QgsLayoutAttributeSelectionDialog::mClearColumnsPushButton_clicked()
692 void QgsLayoutAttributeSelectionDialog::mAddSortColumnPushButton_clicked()
695 mSortColumnModel->insertRow( mSortColumnModel->
rowCount() );
698 void QgsLayoutAttributeSelectionDialog::mRemoveSortColumnPushButton_clicked()
701 const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
702 const int count = indexes.count();
704 for (
int i = count; i > 0; --i )
705 mSortColumnModel->removeRow( indexes.at( i - 1 ).row(), QModelIndex() );
708 void QgsLayoutAttributeSelectionDialog::showHelp()
710 QgsHelp::openHelp( QStringLiteral(
"print_composer/composer_items/composer_attribute_table.html" ) );
713 void QgsLayoutAttributeSelectionDialog::mSortColumnDownPushButton_clicked()
716 const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
717 const int count = indexes.count();
719 for (
int i = count; i > 0; --i )
723 void QgsLayoutAttributeSelectionDialog::mSortColumnUpPushButton_clicked()
726 const QModelIndexList indexes = mSortColumnTableView->selectionModel()->selectedRows();
727 const int count = indexes.count();
729 for (
int i = count; i > 0; --i )