moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / indimenu.cpp
blob9ecd6e61dbcdd989c27341fdd7a40c65af71ba90
1 /* INDI frontend for KStars
2 Copyright (C) 2003 Jasem Mutlaq (mutlaqja@ikarustech.com)
3 Elwood C. Downey
5 This application is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 JM Changelog:
11 2003-04-28 Used indimenu.c as a template. C --> C++, Xm --> KDE/Qt
12 2003-05-01 Added tab for devices and a group feature
13 2003-05-02 Added scrolling area. Most things are rewritten
14 2003-05-05 Adding INDI Conf
15 2003-05-06 Drivers XML reader
16 2003-05-07 Device manager integration
17 2003-05-21 Full client support
21 #include "indimenu.h"
22 #include "indiproperty.h"
23 #include "indigroup.h"
24 #include "indidevice.h"
25 #include "devicemanager.h"
27 #include <stdlib.h>
29 #include <qlineedit.h>
30 #include <qtextedit.h>
31 #include <qframe.h>
32 #include <qtabwidget.h>
33 #include <qcheckbox.h>
34 #include <qlabel.h>
35 #include <qpushbutton.h>
36 #include <qlayout.h>
37 #include <qsocketnotifier.h>
38 #include <qdatetime.h>
39 #include <qvbox.h>
40 #include <qtimer.h>
42 #include <kled.h>
43 #include <klineedit.h>
44 #include <klistbox.h>
45 #include <kpushbutton.h>
46 #include <kapplication.h>
47 #include <klocale.h>
48 #include <kmessagebox.h>
49 #include <klistview.h>
50 #include <kdebug.h>
51 #include <kcombobox.h>
52 #include <knuminput.h>
53 #include <kdialogbase.h>
55 #include "kstars.h"
56 #include "indidriver.h"
58 /*******************************************************************
59 ** INDI Menu: Handles communication to server and fetching basic XML
60 ** data.
61 *******************************************************************/
62 INDIMenu::INDIMenu(QWidget *parent, const char *name ) : KDialogBase(KDialogBase::Tabbed, i18n("INDI Control Panel"), 0, KDialogBase::Default, parent, name, false)
65 ksw = (KStars *) parent;
67 mgrCounter = 0;
69 mgr.setAutoDelete(true);
71 currentLabel = "";
73 resize( 640, 480);
76 INDIMenu::~INDIMenu()
78 mgr.clear();
81 /*********************************************************************
82 ** Traverse the drivers list, check for updated drivers and take
83 ** appropriate action
84 *********************************************************************/
85 void INDIMenu::updateStatus()
87 INDIDriver *drivers = ksw->getINDIDriver();
89 // Local/Server
90 processServer();
92 if (drivers)
94 if (drivers->activeDriverCount() == 0)
96 KMessageBox::error(0, i18n("No INDI devices currently running. To run devices, please select devices from the Device Manager in the devices menu."));
97 return;
100 else if (mgr.count() == 0)
102 KMessageBox::error(0, i18n("No INDI devices currently running. To run devices, please select devices from the Device Manager in the devices menu."));
103 return;
106 show();
110 bool INDIMenu::processServer()
113 INDIDriver *drivers = ksw->getINDIDriver();
114 DeviceManager *dev;
116 if (drivers == NULL)
117 return false;
119 for (unsigned int i=0; i < drivers->devices.size(); i++)
121 // Devices ready to run but not yet managed
122 if (drivers->devices[i]->state && drivers->devices[i]->managed == false && drivers->devices[i]->mode == IDevice::M_LOCAL)
124 dev = new DeviceManager(this, mgrCounter);
125 if (dev->indiConnect("localhost", QString("%1").arg(drivers->devices[i]->indiPort)))
127 drivers->devices[i]->mgrID = mgrCounter;
128 drivers->devices[i]->managed = true;
129 mgr.append(dev);
130 connect(dev, SIGNAL(newDevice()), drivers, SLOT(updateMenuActions()));
131 connect(dev, SIGNAL(newDevice()), this, SLOT(discoverDevice()));
133 mgrCounter++;
136 else
138 delete (dev);
139 return false;
142 // Devices running and they need to be shutdown
143 else if (!drivers->devices[i]->state && drivers->devices[i]->managed == true && drivers->devices[i]->mode == IDevice::M_LOCAL)
145 drivers->devices[i]->managed = false;
146 removeDeviceMgr(drivers->devices[i]->mgrID);
147 return true;
152 return true;
156 int INDIMenu::processClient(QString hostname, QString portnumber)
159 DeviceManager *dev;
160 INDIDriver *drivers = ksw->getINDIDriver();
162 dev = new DeviceManager(this, mgrCounter);
163 if (dev->indiConnect(hostname, portnumber))
165 mgr.append(dev);
166 if (drivers)
168 connect(dev, SIGNAL(newDevice()), drivers, SLOT(updateMenuActions()));
169 connect(dev, SIGNAL(newDevice()), this, SLOT(discoverDevice()));
172 else
174 delete (dev);
175 return (-1);
178 mgrCounter++;
179 return (mgrCounter - 1);
182 void INDIMenu::removeDeviceMgr(int mgrID)
185 for (unsigned int i=0; i < mgr.count(); i++)
187 if (mgrID == mgr.at(i)->mgrID)
188 mgr.remove(i);
190 emit driverDisconnected(mgrID);
194 INDI_D * INDIMenu::findDevice(QString deviceName)
196 for (unsigned int i=0; i < mgr.count(); i++)
198 for (unsigned int j=0; j < mgr.at(i)->indi_dev.count(); j++)
199 if (mgr.at(i)->indi_dev.at(j)->name == deviceName)
200 return mgr.at(i)->indi_dev.at(j);
202 return NULL;
205 INDI_D * INDIMenu::findDeviceByLabel(QString label)
207 for (unsigned int i=0; i < mgr.count(); i++)
209 for (unsigned int j=0; j < mgr.at(i)->indi_dev.count(); j++)
210 if (mgr.at(i)->indi_dev.at(j)->label == label)
211 return mgr.at(i)->indi_dev.at(j);
214 return NULL;
218 void INDIMenu::setCustomLabel(QString deviceName)
221 int nset=0;
223 for (unsigned int i=0; i < mgr.count(); i++)
224 for (unsigned int j=0; j < mgr.at(i)->indi_dev.count(); j++)
225 if (mgr.at(i)->indi_dev.at(j)->label.find(deviceName) >= 0)
226 nset++;
228 if (nset)
229 currentLabel = deviceName + QString(" %1").arg(nset+1);
230 else
231 currentLabel = deviceName;
235 void INDIMenu::discoverDevice()
237 QTimer::singleShot( 1000, this, SLOT(announceDevice()) );
240 void INDIMenu::announceDevice()
242 newDevice();
247 #include "indimenu.moc"