summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/telescopeprop.cpp
blob: 93d158612e9c3ae286755cb7a8c347790d3acd97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/***************************************************************************
                          telescopeprop.cpp  -  description
                             -------------------
    begin                : Wed June 8th 2005
    copyright            : (C) 2005 by Jasem Mutlaq
    email                : mutlaqja@ikarustech.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include <kpushbutton.h>
#include <tdelistbox.h>
#include <kcombobox.h>
#include <klineedit.h>
#include <tdemessagebox.h>

#include <vector>

#include "telescopeprop.h"
#include "kstars.h"
#include "indimenu.h"
#include "indidriver.h"

telescopeProp::telescopeProp(TQWidget* parent, const char* name, bool modal, WFlags fl)
: scopeProp(parent,name, modal,fl)
{

  ksw = (KStars *) parent;
  
  ksw->establishINDI();
  indi_driver = ksw->getINDIDriver();
  newScopePending = false;

  connect (newB, TQT_SIGNAL(clicked()), this, TQT_SLOT(newScope()));
  connect (saveB, TQT_SIGNAL(clicked()), this, TQT_SLOT(saveScope()));
  connect (removeB, TQT_SIGNAL(clicked()), this, TQT_SLOT(removeScope()));
  connect (telescopeListBox, TQT_SIGNAL(highlighted(int)),this, TQT_SLOT(updateScopeDetails(int)));
  connect(closeB, TQT_SIGNAL(clicked()), this, TQT_SLOT(close()));

  // Fill the combo box with drivers
  driverCombo->insertStringList(indi_driver->driversList);

  // Fill the list box with telescopes
  for (unsigned int i=0; i < indi_driver->devices.size(); i++)
  {
    if (indi_driver->devices[i]->deviceType == KSTARS_TELESCOPE)
    	telescopeListBox->insertItem(indi_driver->devices[i]->label);
  }

  telescopeListBox->setCurrentItem(0);
  updateScopeDetails(0);
     

}

telescopeProp::~telescopeProp()
{
}

void telescopeProp::newScope()
{

  driverCombo->clearEdit();
  labelEdit->clear();
  focalEdit->clear();
  versionEdit->clear();
  apertureEdit->clear();

  driverCombo->setFocus();
  telescopeListBox->clearFocus();
  telescopeListBox->clearSelection();

  newScopePending = true;

}

void telescopeProp::saveScope()
{
  IDevice *dev (NULL);
  double focal_length(-1), aperture(-1);
  int finalIndex = -1;

  if (labelEdit->text().isEmpty())
    {
       KMessageBox::error(NULL, i18n("Telescope label is missing."));
       return;
    }

    if (driverCombo->currentText().isEmpty())
    {
      KMessageBox::error(NULL, i18n("Telescope driver is missing."));
      return;
    }

   if (versionEdit->text().isEmpty())
   {
     KMessageBox::error(NULL, i18n("Telescope driver version is missing."));
     return;
   }

   if (telescopeListBox->currentItem() != -1)
   	finalIndex = findDeviceIndex(telescopeListBox->currentItem());

  // Add new scope
  if (newScopePending)
  {

    dev = new IDevice(labelEdit->text(), driverCombo->currentText(), versionEdit->text());

    dev->deviceType = KSTARS_TELESCOPE;

    focal_length = focalEdit->text().toDouble();
    aperture = apertureEdit->text().toDouble();

    if (focal_length > 0)
     dev->focal_length = focal_length;
    if (aperture > 0)
     dev->aperture = aperture;

    indi_driver->devices.push_back(dev);

    telescopeListBox->insertItem(labelEdit->text());

    telescopeListBox->setCurrentItem(telescopeListBox->count() - 1);

  }
  else
  {
    if (finalIndex == -1) return;
    indi_driver->devices[finalIndex]->label  = labelEdit->text();
    indi_driver->devices[finalIndex]->version = versionEdit->text();
    indi_driver->devices[finalIndex]->driver = driverCombo->currentText();
    
    
    focal_length = focalEdit->text().toDouble();
    aperture = apertureEdit->text().toDouble();

    if (focal_length > 0)
     indi_driver->devices[finalIndex]->focal_length = focal_length;
    if (aperture > 0)
     indi_driver->devices[finalIndex]->aperture = aperture;
  }

  indi_driver->saveDevicesToDisk();

  newScopePending = false;

  driverCombo->clearFocus();
  labelEdit->clearFocus();
  focalEdit->clearFocus();
  apertureEdit->clearFocus();

  KMessageBox::information(NULL, i18n("You need to restart KStars for changes to take effect."));

}

int telescopeProp::findDeviceIndex(int listIndex)
{
  int finalIndex = -1;

  for (unsigned int i=0; i < indi_driver->devices.size(); i++)
  {
    if (indi_driver->devices[i]->label == telescopeListBox->text(listIndex))
    {
	finalIndex = i;
        break;
    }
   }

 return finalIndex;

}

void telescopeProp::updateScopeDetails(int index)
{

  int finalIndex = -1;
  newScopePending = false;
  bool foundFlag(false);
 
  focalEdit->clear();
  apertureEdit->clear();


   finalIndex = findDeviceIndex(index);
   if (finalIndex == -1)
   {
      kdDebug() << "final index is invalid. internal error." << endl;
      return;
   }

  for (int i=0; i < driverCombo->count(); i++)
    if (indi_driver->devices[finalIndex]->driver == driverCombo->text(i))
     {
       driverCombo->setCurrentItem(i);
       foundFlag = true;
       break;
     }

  if (foundFlag == false)
    driverCombo->setCurrentText(indi_driver->devices[finalIndex]->driver);

  labelEdit->setText(indi_driver->devices[finalIndex]->label);

  versionEdit->setText(indi_driver->devices[finalIndex]->version);

  if (indi_driver->devices[finalIndex]->focal_length != -1)
  	focalEdit->setText(TQString("%1").arg(indi_driver->devices[finalIndex]->focal_length));

  if (indi_driver->devices[finalIndex]->aperture != -1)
        apertureEdit->setText(TQString("%1").arg(indi_driver->devices[finalIndex]->aperture));

}

void telescopeProp::removeScope()
{

  int index, finalIndex;

  index = telescopeListBox->currentItem();
  finalIndex = findDeviceIndex(index);

  if (KMessageBox::warningContinueCancel( 0, i18n("Are you sure you want to remove %1?").arg(indi_driver->devices[finalIndex]->label), i18n("Delete Confirmation"),KStdGuiItem::del())!=KMessageBox::Continue)
           return;

  telescopeListBox->removeItem(index);

  delete (indi_driver->devices[finalIndex]);
  indi_driver->devices.erase(indi_driver->devices.begin() + finalIndex);
  
  indi_driver->saveDevicesToDisk();

}


#include "telescopeprop.moc"