~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to plugins/LogBook/src/gui/dataMappers/OcularsDataMapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-02-15 20:48:39 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20100215204839-u3qgbv60rho997yk
Tags: 0.10.3-0ubuntu1
* New upstream release.
  - fixes intel rendering bug (LP: #480553)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Timothy Reaves
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "OcularsDataMapper.hpp"
 
20
#include "LogBookCommon.hpp"
 
21
#include "ui_OcularsWidget.h"
 
22
 
 
23
#include <QDebug>
 
24
#include <QModelIndex>
 
25
#include <QSqlError>
 
26
#include <QSqlField>
 
27
#include <QSqlRecord>
 
28
#include <QSqlTableModel>
 
29
#include <QSqlRelationalTableModel>
 
30
#include <QVariant>
 
31
 
 
32
OcularsDataMapper::OcularsDataMapper(Ui_OcularsWidget *aWidget, QMap<QString, QSqlTableModel *> tableModels, QObject *parent) : QObject(parent)
 
33
{
 
34
        widget = aWidget;
 
35
        
 
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);
 
43
        
 
44
        tableModel = tableModels[OCULARS];
 
45
        
 
46
        setupConnections();
 
47
        widget->ocularsListView->setCurrentIndex(tableModel->index(0, 1));      
 
48
}
 
49
 
 
50
OcularsDataMapper::~OcularsDataMapper()
 
51
{
 
52
}
 
53
 
 
54
/* ********************************************************************* */
 
55
#if 0
 
56
#pragma mark -
 
57
#pragma mark protected slots
 
58
#endif
 
59
/* ********************************************************************* */
 
60
void OcularsDataMapper::apparentFOVChanged()
 
61
{
 
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();
 
68
                }
 
69
        }
 
70
        
 
71
        widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
 
72
}
 
73
 
 
74
void OcularsDataMapper::deleteSelectedOcular()
 
75
{
 
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));
 
80
        }
 
81
}
 
82
 
 
83
void OcularsDataMapper::focalLengthChanged()
 
84
{
 
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();
 
91
                }
 
92
        }
 
93
        
 
94
        widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
 
95
}
 
96
 
 
97
void OcularsDataMapper::insertNewOcular()
 
98
{
 
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);
 
112
        
 
113
        if (tableModel->insertRecord(-1, newRecord)) {
 
114
                widget->ocularsListView->setCurrentIndex(tableModel->index(tableModel->rowCount() - 1, 1));
 
115
        } else {
 
116
                qWarning() << "LogBook: could not insert new Ocular.  The error is: " << tableModel->lastError();
 
117
        }
 
118
}
 
119
 
 
120
void OcularsDataMapper::modelChanged()
 
121
{
 
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();
 
128
                }
 
129
        }
 
130
        widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
 
131
}
 
132
 
 
133
void OcularsDataMapper::ocularSelected(const QModelIndex &index)
 
134
{       
 
135
        if (index.row() != -1) {
 
136
                lastRowNumberSelected = index.row();
 
137
                populateFormWithIndex(index);
 
138
        }
 
139
}
 
140
 
 
141
void OcularsDataMapper::vendorChanged()
 
142
{
 
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();
 
149
                }
 
150
        }
 
151
        
 
152
        widget->ocularsListView->setCurrentIndex(tableModel->index(lastRowNumberSelected, 1));
 
153
}
 
154
 
 
155
/* ********************************************************************* */
 
156
#if 0
 
157
#pragma mark -
 
158
#pragma mark protected methods
 
159
#endif
 
160
/* ********************************************************************* */
 
161
QSqlRecord OcularsDataMapper::currentRecord()
 
162
 
163
        if (lastRowNumberSelected == -1) {
 
164
                return QSqlRecord();
 
165
        } else {
 
166
                return tableModel->record(lastRowNumberSelected);
 
167
        }
 
168
}
 
169
 
 
170
void OcularsDataMapper::populateFormWithIndex(const QModelIndex &index)
 
171
{
 
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());
 
180
                setupConnections();
 
181
        }
 
182
}
 
183
 
 
184
void OcularsDataMapper::setupConnections()
 
185
{
 
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()));
 
195
}
 
196
 
 
197
void OcularsDataMapper::teardownConnections()
 
198
{
 
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()));
 
208
}
 
209