profileeditor.cpp 13.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*  Profile Editor
    Copyright (C) 2016 Jasem Mutlaq <mutlaqja@ikarustech.com>

    This application 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 <QDialogButtonBox>

#include <KMessageBox>

#include "kstarsdata.h"
15
#include "geolocation.h"
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
#include "auxiliary/ksuserdb.h"

#include "indi/drivermanager.h"
#include "indi/driverinfo.h"

#
#include "profileeditor.h"
#include "profileinfo.h"

ProfileEditorUI::ProfileEditorUI( QWidget *p ) : QFrame( p )
{
    setupUi( this );
}


ProfileEditor::ProfileEditor(QWidget *w )  : QDialog( w )
{
    ui = new ProfileEditorUI( this );

    pi = NULL;

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(ui);
    setLayout(mainLayout);

    setWindowTitle(i18n("Profile Editor"));

    // Create button box and link it to save and reject functions
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Save|QDialogButtonBox::Close);
    mainLayout->addWidget(buttonBox);
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(saveProfile()));

    connect(ui->remoteMode, SIGNAL(toggled(bool)), this, SLOT(setRemoteMode(bool)));

51
    #ifdef Q_OS_WIN
Jasem Mutlaq's avatar
Jasem Mutlaq committed
52
    ui->localMode->setEnabled(false);
53
54
55
    setRemoteMode(true);
    #endif

56
57
58
59
60
61
62
63
64
65
66
    // Load all drivers
    loadDrivers();
}

ProfileEditor::~ProfileEditor()
{

}

void ProfileEditor::saveProfile()
{
Jasem Mutlaq's avatar
Jasem Mutlaq committed
67
    bool newProfile = (pi == NULL);
68
69
70
71
72
73
74

    if (ui->profileIN->text().isEmpty())
    {
        KMessageBox::error(this, i18n("Cannot save an empty profile!"));
        return;
    }

Jasem Mutlaq's avatar
Jasem Mutlaq committed
75
   if (newProfile)
76
77
78
79
   {
       int id = KStarsData::Instance()->userdb()->AddProfile(ui->profileIN->text());
       pi = new ProfileInfo(id, ui->profileIN->text());
   }
Jasem Mutlaq's avatar
Jasem Mutlaq committed
80
81
   else
       pi->name = ui->profileIN->text();
82
83
84
85
86
87

   // Local Mode
   if (ui->localMode->isChecked())
   {
       pi->host.clear();
       pi->port=-1;
88
       pi->INDIWebManagerPort=-1;
89
90
91
92
93
94
95
       //pi->customDrivers = ui->customDriversIN->text();
   }
   // Remote Mode
   else
   {
       pi->host = ui->remoteHost->text();
       pi->port = ui->remotePort->text().toInt();
96
97
98
99
       if (ui->INDIWebManagerCheck->isChecked())
           pi->INDIWebManagerPort = ui->INDIWebManagerPort->text().toInt();
       else
           pi->INDIWebManagerPort=-1;
100
       //pi->customDrivers.clear();
101
   }      
102

103
104
105
106
107
108
109
110
111
112
113
114
115
116
   // City Info
   if (ui->loadSiteCheck->isEnabled() && ui->loadSiteCheck->isChecked())
   {
       pi->city     = KStarsData::Instance()->geo()->name();
       pi->province = KStarsData::Instance()->geo()->province();
       pi->country  = KStarsData::Instance()->geo()->country();
   }
   else
   {
      pi->city.clear();
      pi->province.clear();
      pi->country.clear();
   }

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
   if (ui->mountCombo->currentText() != "--")
       pi->drivers["Mount"] = ui->mountCombo->currentText();
   else
       pi->drivers.remove("Mount");

   if (ui->ccdCombo->currentText() != "--")
       pi->drivers["CCD"] = ui->ccdCombo->currentText();
   else
       pi->drivers.remove("CCD");

   if (ui->guiderCombo->currentText() != "--")
       pi->drivers["Guider"] = ui->guiderCombo->currentText();
   else
       pi->drivers.remove("Guider");

   if (ui->focuserCombo->currentText() != "--")
       pi->drivers["Focuser"] = ui->focuserCombo->currentText();
   else
       pi->drivers.remove("Focuser");

   if (ui->filterCombo->currentText() != "--")
       pi->drivers["Filter"] = ui->filterCombo->currentText();
   else
       pi->drivers.remove("Filter");

   if (ui->AOCombo->currentText() != "--")
       pi->drivers["AO"] = ui->AOCombo->currentText();
   else
       pi->drivers.remove("AO");

   if (ui->domeCombo->currentText() != "--")
       pi->drivers["Dome"] = ui->domeCombo->currentText();
   else
       pi->drivers.remove("Dome");

   if (ui->weatherCombo->currentText() != "--")
       pi->drivers["Weather"] = ui->weatherCombo->currentText();
   else
       pi->drivers.remove("Weather");

   if (ui->aux1Combo->currentText() != "--")
       pi->drivers["Aux1"] = ui->aux1Combo->currentText();
   else
       pi->drivers.remove("Aux1");

   if (ui->aux2Combo->currentText() != "--")
       pi->drivers["Aux2"] = ui->aux2Combo->currentText();
   else
       pi->drivers.remove("Aux2");

   if (ui->aux3Combo->currentText() != "--")
       pi->drivers["Aux3"] = ui->aux3Combo->currentText();
   else
       pi->drivers.remove("Aux3");

   if (ui->aux4Combo->currentText() != "--")
       pi->drivers["Aux4"] = ui->aux4Combo->currentText();
   else
       pi->drivers.remove("Aux4");

   KStarsData::Instance()->userdb()->SaveProfile(pi);

   // Ekos manager will reload and new profiles will be created
Jasem Mutlaq's avatar
Jasem Mutlaq committed
180
   if (newProfile)
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
    delete (pi);

   accept();
}

ProfileInfo *ProfileEditor::getPi() const
{
    return pi;
}

void ProfileEditor::setRemoteMode(bool enable)
{
    ui->remoteHost->setEnabled(enable);
    ui->remoteHostLabel->setEnabled(enable);
    ui->remotePort->setEnabled(enable);
    ui->remotePortLabel->setEnabled(enable);

    //ui->customLabel->setEnabled(!enable);
    //ui->customDriversIN->setEnabled(!enable);

    ui->mountCombo->setEditable(enable);
    ui->ccdCombo->setEditable(enable);
    ui->guiderCombo->setEditable(enable);
    ui->focuserCombo->setEditable(enable);
    ui->filterCombo->setEditable(enable);
    ui->AOCombo->setEditable(enable);
    ui->domeCombo->setEditable(enable);
    ui->weatherCombo->setEditable(enable);
    ui->aux1Combo->setEditable(enable);
    ui->aux2Combo->setEditable(enable);
    ui->aux3Combo->setEditable(enable);
    ui->aux4Combo->setEditable(enable);

214
215
    ui->loadSiteCheck->setEnabled(enable);

216
217
218
    ui->INDIWebManagerCheck->setEnabled(enable);
    ui->INDIWebManagerPort->setEnabled(enable);

219
220
221
222
223
224
225
226
}

void ProfileEditor::setPi(ProfileInfo *value)
{
    pi = value;

    ui->profileIN->setText(pi->name);

227
228
229
230
231
232
233
234
235
236
    ui->loadSiteCheck->setChecked(!pi->city.isEmpty());

    if (pi->city.isEmpty() == false)
    {
        if (pi->province.isEmpty())
            ui->loadSiteCheck->setText(ui->loadSiteCheck->text() + QString(" (%1, %2)").arg(pi->country).arg(pi->city));
        else
            ui->loadSiteCheck->setText(ui->loadSiteCheck->text() + QString(" (%1, %2, %3)").arg(pi->country).arg(pi->province).arg(pi->city));
    }

237
238
239
240
241
242
    if (pi->host.isEmpty() == false)
    {
        ui->remoteHost->setText(pi->host);
        ui->remotePort->setText(QString::number(pi->port));

        ui->remoteMode->setChecked(true);
243

244
        if (pi->INDIWebManagerPort > 0)
245
246
247
248
        {
            ui->INDIWebManagerCheck->setChecked(true);
            ui->INDIWebManagerPort->setText(QString::number(pi->INDIWebManagerPort));
        }
249
250
251
252
253
        else
        {
            ui->INDIWebManagerCheck->setChecked(false);
            ui->INDIWebManagerPort->setText("8624");
        }
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
    }

    QMapIterator<QString, QString> i(pi->drivers);
    int row=0;

    while (i.hasNext())
    {
        i.next();

        QString key   = i.key();
        QString value = i.value();

        if (key == "Mount")
        {
            // If driver doesn't exist, let's add it to the list
            if ( (row = ui->mountCombo->findText(value)) == -1)
            {
                ui->mountCombo->addItem(value);
                row = ui->mountCombo->count() - 1;
            }

            // Set index to our driver
            ui->mountCombo->setCurrentIndex(row);
        }
        else if (key == "CCD")
        {
            if ( (row = ui->ccdCombo->findText(value)) == -1)
            {
                ui->ccdCombo->addItem(value);
                row = ui->ccdCombo->count() - 1;
            }

            ui->ccdCombo->setCurrentIndex(row);
        }
        else if (key == "Guider")
        {
            if ( (row = ui->guiderCombo->findText(value)) == -1)
            {
                ui->guiderCombo->addItem(value);
                row = ui->guiderCombo->count() - 1;
            }
            else
                ui->guiderCombo->setCurrentIndex(row);
        }
        else if (key == "Focuser")
        {
            if ( (row = ui->focuserCombo->findText(value)) == -1)
            {
                ui->focuserCombo->addItem(value);
                row = ui->focuserCombo->count() - 1;
            }

            ui->focuserCombo->setCurrentIndex(row);
        }
        else if (key == "Filter")
        {
            if ( (row = ui->filterCombo->findText(value)) == -1)
            {
                ui->filterCombo->addItem(value);
                row = ui->filterCombo->count() - 1;
            }

            ui->filterCombo->setCurrentIndex(row);
        }
        else if (key == "AO")
        {
            if ( (row = ui->AOCombo->findText(value)) == -1)
            {
                ui->AOCombo->addItem(value);
                row = ui->AOCombo->count() - 1;
            }

            ui->AOCombo->setCurrentIndex(row);
        }
        else if (key == "Dome")
        {
            if ( (row = ui->domeCombo->findText(value)) == -1)
            {
                ui->domeCombo->addItem(value);
                row = ui->domeCombo->count() - 1;
            }

            ui->domeCombo->setCurrentIndex(row);
        }
        else if (key == "Weather")
        {
            if ( (row = ui->weatherCombo->findText(value)) == -1)
            {
                ui->weatherCombo->addItem(value);
                row = ui->weatherCombo->count() - 1;
            }

            ui->weatherCombo->setCurrentIndex(row);
        }
        else if (key == "Aux1")
        {
            if ( (row = ui->aux1Combo->findText(value)) == -1)
            {
                ui->aux1Combo->addItem(value);
                row = ui->aux1Combo->count() - 1;
            }

            ui->aux1Combo->setCurrentIndex(row);
        }
        else if (key == "Aux2")
        {
            if ( (row = ui->aux2Combo->findText(value)) == -1)
            {
                ui->aux2Combo->addItem(value);
                row = ui->aux2Combo->count() - 1;
            }

            ui->aux2Combo->setCurrentIndex(row);
        }
        else if (key == "Aux3")
        {
            if ( (row = ui->aux3Combo->findText(value)) == -1)
            {
                ui->aux3Combo->addItem(value);
                row = ui->aux3Combo->count() - 1;
            }

            ui->aux3Combo->setCurrentIndex(row);
        }
        else if (key == "Aux4")
        {
            if ( (row = ui->aux4Combo->findText(value)) == -1)
            {
                ui->aux4Combo->addItem(value);
                row = ui->aux4Combo->count() - 1;
            }

            ui->aux4Combo->setCurrentIndex(row);
        }
    }
}

void ProfileEditor::loadDrivers()
{
    ui->mountCombo->addItem("--");
    ui->ccdCombo->addItem("--");
    ui->guiderCombo->addItem("--");
    ui->AOCombo->addItem("--");
    ui->focuserCombo->addItem("--");
    ui->filterCombo->addItem("--");
    ui->domeCombo->addItem("--");
    ui->weatherCombo->addItem("--");
    ui->aux1Combo->addItem("--");
    ui->aux2Combo->addItem("--");
    ui->aux3Combo->addItem("--");
    ui->aux4Combo->addItem("--");

    foreach(DriverInfo *dv, DriverManager::Instance()->getDrivers())
    {
        switch (dv->getType())
        {
        case KSTARS_TELESCOPE:
        {
            ui->mountCombo->addItem(dv->getTreeLabel());
        }
        break;

        case KSTARS_CCD:
        {
            ui->ccdCombo->addItem(dv->getTreeLabel());
            ui->guiderCombo->addItem(dv->getTreeLabel());

            ui->aux1Combo->addItem(dv->getTreeLabel());
            ui->aux2Combo->addItem(dv->getTreeLabel());
            ui->aux3Combo->addItem(dv->getTreeLabel());
            ui->aux4Combo->addItem(dv->getTreeLabel());
        }
        break;

        case KSTARS_ADAPTIVE_OPTICS:
        {
            ui->AOCombo->addItem(dv->getTreeLabel());
        }
        break;

        case KSTARS_FOCUSER:
        {
            ui->focuserCombo->addItem(dv->getTreeLabel());
        }
        break;

        case KSTARS_FILTER:
        {
            ui->filterCombo->addItem(dv->getTreeLabel());
        }
        break;

        case KSTARS_DOME:
        {
            ui->domeCombo->addItem(dv->getTreeLabel());
        }
        break;

        case KSTARS_WEATHER:
        {
            ui->weatherCombo->addItem(dv->getTreeLabel());
        }
        break;

        case KSTARS_AUXILIARY:
        {
            ui->aux1Combo->addItem(dv->getTreeLabel());
            ui->aux2Combo->addItem(dv->getTreeLabel());
            ui->aux3Combo->addItem(dv->getTreeLabel());
            ui->aux4Combo->addItem(dv->getTreeLabel());
        }
        break;

        default:
            continue;
            break;
        }
    }

    //ui->mountCombo->setCurrentIndex(-1);
    ui->mountCombo->model()->sort(0);
    ui->ccdCombo->model()->sort(0);
    ui->guiderCombo->model()->sort(0);
    ui->AOCombo->model()->sort(0);
    ui->focuserCombo->model()->sort(0);
    ui->filterCombo->model()->sort(0);
    ui->domeCombo->model()->sort(0);
    ui->weatherCombo->model()->sort(0);
    ui->aux1Combo->model()->sort(0);
    ui->aux2Combo->model()->sort(0);
    ui->aux3Combo->model()->sort(0);
    ui->aux4Combo->model()->sort(0);
}