16
16
BrowseWidget::BrowseWidget (QWidget *parent) :
17
17
QWidget(parent),
18
18
m_model(NULL ),
19
+ m_schemaWidget(NULL ),
19
20
ui(new Ui::BrowseWidget)
20
21
{
21
22
ui->setupUi (this );
@@ -30,13 +31,25 @@ BrowseWidget::~BrowseWidget()
30
31
delete ui;
31
32
}
32
33
33
- void BrowseWidget::setDatabase (QSqlDatabase *database)
34
+ void BrowseWidget::init (QSqlDatabase *database, Extension *extension )
34
35
{
35
- // Set the database
36
+ // Set the database and extension
36
37
m_database = database;
38
+ m_extension = extension;
37
39
38
40
// Setup the model
39
41
m_model = new QSqlTableModel (this , (*m_database));
42
+
43
+ // Make sure we save changes when moving to a new row (this is necessary so "Add" works)
44
+ connect (ui->tableView ->selectionModel (), SIGNAL (currentChanged (QModelIndex,QModelIndex)), this , SLOT (currentChanged (QModelIndex,QModelIndex)));
45
+
46
+ // Setup schema widget (if supported by extension)
47
+ if (m_extension && m_extension->hasCapability (VIEW_SCHEMA)) {
48
+ m_schemaWidget = new SchemaWidget (this , m_database, m_extension);
49
+ m_schemaWidget->init ();
50
+ ui->tabWidget ->addTab (m_schemaWidget, QIcon::fromTheme (" document-edit" ), " Schema" );
51
+ // connect(m_schemaWidget, SIGNAL(refreshNeeded()), this, SLOT(refresh())); // @TODO make this refresh
52
+ }
40
53
}
41
54
42
55
bool BrowseWidget::setTable (QString table)
@@ -74,6 +87,11 @@ bool BrowseWidget::setTable(QString table)
74
87
ui->tableView ->sortByColumn (0 , Qt::AscendingOrder);
75
88
ui->tableView ->setAlternatingRowColors (true );
76
89
90
+ // If there is a schema-editor, then set the table
91
+ if (m_schemaWidget) {
92
+ m_schemaWidget->setTable (table);
93
+ }
94
+
77
95
return true ;
78
96
}
79
97
@@ -113,9 +131,6 @@ void BrowseWidget::on_addButton_clicked()
113
131
114
132
// Select the newly inserted row
115
133
ui->tableView ->selectRow (m_model->rowCount ()-1 );
116
-
117
- // Make sure we save changes when moving to a new row (this is necessary so "Add" works)
118
- connect (ui->tableView ->selectionModel (), SIGNAL (currentChanged (QModelIndex,QModelIndex)), this , SLOT (currentChanged (QModelIndex,QModelIndex)));
119
134
}
120
135
121
136
void BrowseWidget::on_clearButton_clicked ()
@@ -140,6 +155,9 @@ void BrowseWidget::commitChanges()
140
155
// Check if there was an error and display it if there was
141
156
if (m_model->lastError ().type () != QSqlError::NoError) {
142
157
QMessageBox::critical (this , " Operation failed" , m_model->lastError ().text ());
158
+
159
+ // Revert changes and set back to onFieldChange
160
+ m_model->revertAll ();
143
161
}
144
162
}
145
163
}
@@ -152,9 +170,6 @@ void BrowseWidget::currentChanged(QModelIndex previous, QModelIndex current)
152
170
// Commit changes
153
171
commitChanges ();
154
172
155
- // There's no reason to call this anymore, we don't need to know if the row has changed
156
- disconnect (ui->tableView ->selectionModel (), SIGNAL (currentChanged (QModelIndex,QModelIndex)), this , SLOT (currentChanged (QModelIndex,QModelIndex)));
157
-
158
173
// Revert back to OnFieldChange
159
174
m_model->setEditStrategy (QSqlTableModel::OnFieldChange);
160
175
}
@@ -171,3 +186,12 @@ void BrowseWidget::on_saveAsButton_clicked()
171
186
// Execute the dialog
172
187
exportSelectionDialog.exec ();
173
188
}
189
+
190
+ void BrowseWidget::refresh ()
191
+ {
192
+ // If there is a schema widget (extension supports it), refresh it
193
+ /* if (m_schemaWidget) {
194
+ m_schemaWidget->setTable(m_table);
195
+ }*/
196
+ // @TODO this can cause errors if the table no longer exists
197
+ }
0 commit comments