QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "moc_qgstablewidgetbase.cpp"
19
21 : QWidget( parent )
22{
23 setupUi( this );
24 connect( addButton, &QToolButton::clicked, this, &QgsTableWidgetBase::addButton_clicked );
25 connect( removeButton, &QToolButton::clicked, this, &QgsTableWidgetBase::removeButton_clicked );
26}
27
28void QgsTableWidgetBase::init( QAbstractTableModel *model )
29{
30 tableView->setModel( model );
31 connect( tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsTableWidgetBase::onSelectionChanged );
32 connect( model, &QAbstractItemModel::dataChanged, this, &QgsTableWidgetBase::valueChanged );
33 connect( model, &QAbstractItemModel::rowsRemoved, this, &QgsTableWidgetBase::valueChanged );
34 connect( model, &QAbstractItemModel::rowsInserted, this, &QgsTableWidgetBase::valueChanged );
35}
36
37void QgsTableWidgetBase::addButton_clicked()
38{
39 if ( mReadOnly )
40 return;
41
42 const QItemSelectionModel *select = tableView->selectionModel();
43 const int pos = select->hasSelection() ? select->selectedRows()[0].row() : 0;
44 QAbstractItemModel *model = tableView->model();
45 model->insertRows( pos, 1 );
46 const QModelIndex index = model->index( pos, 0 );
47 tableView->scrollTo( index );
48 tableView->edit( index );
49 tableView->selectRow( pos );
50}
51
52void QgsTableWidgetBase::removeButton_clicked()
53{
54 if ( mReadOnly )
55 return;
56
57 const QItemSelectionModel *select = tableView->selectionModel();
58 // The UI is configured to have single row selection.
59 if ( select->hasSelection() )
60 {
61 tableView->model()->removeRows( select->selectedRows()[0].row(), 1 );
62 }
63}
64
65void QgsTableWidgetBase::onSelectionChanged()
66{
67 removeButton->setEnabled( tableView->selectionModel()->hasSelection() );
68}
69
71{
72 mReadOnly = readOnly;
73
74 addButton->setEnabled( !mReadOnly );
75 removeButton->setEnabled( !mReadOnly && tableView->selectionModel()->hasSelection() );
76
77 if ( mReadOnly )
78 {
79 mWidgetActions->hide();
80 layout()->setSpacing( 0 );
81 }
82 else
83 {
84 mWidgetActions->show();
85 layout()->setSpacing( 6 );
86 }
87}
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.