QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgstablewidgetbase.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstablewidgetbase.cpp
3 --------------------------------------
4 Date : 08.2016
5 Copyright : (C) 2016 Patrick Valsecchi
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgstablewidgetbase.h"
17#include "moc_qgstablewidgetbase.cpp"
18
20 : QWidget( parent )
21{
22 setupUi( this );
23 connect( addButton, &QToolButton::clicked, this, &QgsTableWidgetBase::addButton_clicked );
24 connect( removeButton, &QToolButton::clicked, this, &QgsTableWidgetBase::removeButton_clicked );
25}
26
27void QgsTableWidgetBase::init( QAbstractTableModel *model )
28{
29 tableView->setModel( model );
30 connect( tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsTableWidgetBase::onSelectionChanged );
31 connect( model, &QAbstractItemModel::dataChanged, this, &QgsTableWidgetBase::valueChanged );
32 connect( model, &QAbstractItemModel::rowsRemoved, this, &QgsTableWidgetBase::valueChanged );
33 connect( model, &QAbstractItemModel::rowsInserted, this, &QgsTableWidgetBase::valueChanged );
34}
35
36void QgsTableWidgetBase::addButton_clicked()
37{
38 if ( mReadOnly )
39 return;
40
41 const QItemSelectionModel *select = tableView->selectionModel();
42 const int pos = select->hasSelection() ? select->selectedRows()[0].row() : 0;
43 QAbstractItemModel *model = tableView->model();
44 model->insertRows( pos, 1 );
45 const QModelIndex index = model->index( pos, 0 );
46 tableView->scrollTo( index );
47 tableView->edit( index );
48 tableView->selectRow( pos );
49}
50
51void QgsTableWidgetBase::removeButton_clicked()
52{
53 if ( mReadOnly )
54 return;
55
56 const QItemSelectionModel *select = tableView->selectionModel();
57 // The UI is configured to have single row selection.
58 if ( select->hasSelection() )
59 {
60 tableView->model()->removeRows( select->selectedRows()[0].row(), 1 );
61 }
62}
63
64void QgsTableWidgetBase::onSelectionChanged()
65{
66 removeButton->setEnabled( tableView->selectionModel()->hasSelection() );
67}
68
70{
71 mReadOnly = readOnly;
72
73 addButton->setEnabled( !mReadOnly );
74 removeButton->setEnabled( !mReadOnly && tableView->selectionModel()->hasSelection() );
75
76 if ( mReadOnly )
77 {
78 mWidgetActions->hide();
79 layout()->setSpacing( 0 );
80 }
81 else
82 {
83 mWidgetActions->show();
84 layout()->setSpacing( 6 );
85 }
86}
void init(QAbstractTableModel *model)
Initialize the table with the given model.
void valueChanged()
Emitted each time a key or a value is changed.
QgsTableWidgetBase(QWidget *parent)
Constructor.
virtual void setReadOnly(bool readOnly)
Sets whether the widget should be shown in a read-only state.