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