2
* Copyright (C) 2009 Timothy Reaves
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
#include "OcularsDataMapper.hpp"
20
#include "LogBookCommon.hpp"
21
#include "ui_OcularsWidget.h"
24
#include <QModelIndex>
28
#include <QSqlTableModel>
29
#include <QSqlRelationalTableModel>
32
OcularsDataMapper::OcularsDataMapper(Ui_OcularsWidget *aWidget, QMap<QString, QSqlTableModel *> tableModels, QObject *parent) : QObject(parent)
36
QRegExp nonEmptyStringRegEx("\\S+.*");
37
QRegExpValidator *nonEmptyStringValidator = new QRegExpValidator(nonEmptyStringRegEx, this);
38
QDoubleValidator *ocularEFL = new QDoubleValidator(1.0, 60.0, 1, this);
39
QIntValidator *ocularAFOV = new QIntValidator(35, 100, this);
40
widget->modelLineEdit->setValidator(nonEmptyStringValidator);
41
widget->focalLengthLineEdit->setValidator(ocularEFL);
42
widget->apparentFOVLineEdit->setValidator(ocularAFOV);
44
tableModel = tableModels[OCULARS];
47
widget->ocularsListView->setCurrentIndex(tableModel->index(0, 1));
50
OcularsDataMapper::~OcularsDataMapper()
54
/* ********************************************************************* */
57
#pragma mark protected slots
59
/* ********************************************************************* */
60
void OcularsDataMapper::apparentFOVChanged()
62
QSqlRecord record = currentRecord();
63
if (!record.isEmpty() && record.value("apparent_fov") != widget->apparentFOVLineEdit->text()) {
64
record.setValue("apparent_fov", widget->apparentFOVLineEdit->text());
65
tableModel->setRecord(lastRowNumberSelected, record);
66
if (!tableModel->submit()) {
67
qWarning() << "LogBook: could not update Ocular. Error is: " << tableModel->lastError();
71
widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
74
void OcularsDataMapper::deleteSelectedOcular()
76
QModelIndex selection = widget->ocularsListView->currentIndex();
77
if (selection.row() != -1 && tableModel->rowCount() > 1) {
78
tableModel->removeRows(selection.row(), 1);
79
widget->ocularsListView->setCurrentIndex(tableModel->index(0, 1));
83
void OcularsDataMapper::focalLengthChanged()
85
QSqlRecord record = currentRecord();
86
if (!record.isEmpty() && record.value("focal_length") != widget->focalLengthLineEdit->text()) {
87
record.setValue("focal_length", widget->focalLengthLineEdit->text());
88
tableModel->setRecord(lastRowNumberSelected, record);
89
if (!tableModel->submit()) {
90
qWarning() << "LogBook: could not update Ocular. Error is: " << tableModel->lastError();
94
widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
97
void OcularsDataMapper::insertNewOcular()
99
QSqlField field1("model", QVariant::String);
100
QSqlField field2("vendor", QVariant::String);
101
QSqlField field3("focal_length", QVariant::Double);
102
QSqlField field4("apparent_fov", QVariant::Int);
103
field1.setValue(QVariant("Model"));
104
field2.setValue(QVariant("Vendor"));
105
field3.setValue(QVariant(31.0));
106
field4.setValue(QVariant(55));
107
QSqlRecord newRecord = QSqlRecord();
108
newRecord.append(field1);
109
newRecord.append(field2);
110
newRecord.append(field3);
111
newRecord.append(field4);
113
if (tableModel->insertRecord(-1, newRecord)) {
114
widget->ocularsListView->setCurrentIndex(tableModel->index(tableModel->rowCount() - 1, 1));
116
qWarning() << "LogBook: could not insert new Ocular. The error is: " << tableModel->lastError();
120
void OcularsDataMapper::modelChanged()
122
QSqlRecord record = currentRecord();
123
if (!record.isEmpty() && record.value("model") != widget->modelLineEdit->text()) {
124
record.setValue("model", widget->modelLineEdit->text());
125
tableModel->setRecord(lastRowNumberSelected, record);
126
if (!tableModel->submit()) {
127
qWarning() << "LogBook: could not update Ocular. Error is: " << tableModel->lastError();
130
widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
133
void OcularsDataMapper::ocularSelected(const QModelIndex &index)
135
if (index.row() != -1) {
136
lastRowNumberSelected = index.row();
137
populateFormWithIndex(index);
141
void OcularsDataMapper::vendorChanged()
143
QSqlRecord record = currentRecord();
144
if (!record.isEmpty() && record.value("vendor") != widget->vendorLineEdit->text()) {
145
record.setValue("vendor", widget->vendorLineEdit->text());
146
tableModel->setRecord(lastRowNumberSelected, record);
147
if (!tableModel->submit()) {
148
qWarning() << "LogBook: could not update Ocular. Error is: " << tableModel->lastError();
152
widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
155
/* ********************************************************************* */
158
#pragma mark protected methods
160
/* ********************************************************************* */
161
QSqlRecord OcularsDataMapper::currentRecord()
163
if (lastRowNumberSelected == -1) {
166
return tableModel->record(lastRowNumberSelected);
170
void OcularsDataMapper::populateFormWithIndex(const QModelIndex &index)
172
if (index.row() != -1) {
173
teardownConnections();
174
QSqlRecord record = tableModel->record(index.row());
175
widget->idLabel->setText(record.value("Ocular_id").toString());
176
widget->modelLineEdit->setText(record.value("model").toString());
177
widget->vendorLineEdit->setText(record.value("vendor").toString());
178
widget->focalLengthLineEdit->setText(record.value("focal_length").toString());
179
widget->apparentFOVLineEdit->setText(record.value("apparent_fov").toString());
184
void OcularsDataMapper::setupConnections()
186
connect(widget->addOcularButton, SIGNAL(clicked()), this, SLOT(insertNewOcular()));
187
connect(widget->deleteOcularButton, SIGNAL(clicked()), this, SLOT(deleteSelectedOcular()));
188
connect(widget->ocularsListView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(ocularSelected(const QModelIndex &)));
189
connect(widget->ocularsListView->selectionModel() , SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
190
this, SLOT(ocularSelected(QModelIndex)));
191
connect(widget->modelLineEdit, SIGNAL(editingFinished()), this, SLOT(modelChanged()));
192
connect(widget->vendorLineEdit, SIGNAL(editingFinished()), this, SLOT(vendorChanged()));
193
connect(widget->focalLengthLineEdit, SIGNAL(editingFinished()), this, SLOT(focalLengthChanged()));
194
connect(widget->apparentFOVLineEdit, SIGNAL(editingFinished()), this, SLOT(apparentFOVChanged()));
197
void OcularsDataMapper::teardownConnections()
199
disconnect(widget->addOcularButton, SIGNAL(clicked()), this, SLOT(insertNewOcular()));
200
disconnect(widget->deleteOcularButton, SIGNAL(clicked()), this, SLOT(deleteSelectedOcular()));
201
disconnect(widget->ocularsListView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(ocularSelected(const QModelIndex &)));
202
disconnect(widget->ocularsListView->selectionModel() , SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
203
this, SLOT(ocularSelected(QModelIndex)));
204
disconnect(widget->modelLineEdit, SIGNAL(editingFinished()), this, SLOT(modelChanged()));
205
disconnect(widget->vendorLineEdit, SIGNAL(editingFinished()), this, SLOT(vendorChanged()));
206
disconnect(widget->focalLengthLineEdit, SIGNAL(editingFinished()), this, SLOT(focalLengthChanged()));
207
disconnect(widget->apparentFOVLineEdit, SIGNAL(editingFinished()), this, SLOT(apparentFOVChanged()));