summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/indielement.cpp
blob: 248007af4007e928ff0b7774581b7ccd5deaecb0 (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
246
247
248
249
250
251
252
253
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
/*  INDI Element
    Copyright (C) 2003 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.
    
    2004-01-15	INDI element is the most basic unit of the INDI KStars client.
 */
 
#include "indielement.h"
#include "indiproperty.h"
#include "indigroup.h"
#include "indidevice.h"

#include "indi/indicom.h"

#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqstring.h>
#include <tqptrlist.h>
#include <tqslider.h>
#include <tqdir.h>

#include <kurl.h>
#include <kfiledialog.h>
#include <kled.h>
#include <ksqueezedtextlabel.h> 
#include <klineedit.h>
#include <kpushbutton.h>
#include <klocale.h>
#include <kdebug.h>
#include <kcombobox.h>
#include <knuminput.h>
#include <kdialogbase.h>

/* search element for attribute.
 * return XMLAtt if tqfind, else NULL with helpful info in errmsg.
 */
XMLAtt * findAtt (XMLEle *ep, const char *name, char errmsg[])
{
	XMLAtt *ap = findXMLAtt (ep, name);
	if (ap)
	    return (ap);
	if (errmsg)
	    snprintf (errmsg, ERRMSG_SIZE, "INDI: <%.64s> missing attribute '%.64s'", tagXMLEle(ep),name);
	return NULL;
}

/* search element for given child. pp is just to build a better errmsg.
 * return XMLEle if tqfind, else NULL with helpful info in errmsg.
 */
XMLEle * findEle (XMLEle *ep, INDI_P *pp, const char *child, char errmsg[])
{
	XMLEle *cp = findXMLEle (ep, child);
	if (cp)
	    return (cp);
	if (errmsg)
	    snprintf (errmsg, ERRMSG_SIZE, "INDI: <%.64s %.64s %.64s> missing child '%.64s'", tagXMLEle(ep),
						pp->pg->dp->name.ascii(), pp->name.ascii(), child);
	return (NULL);
}

/*******************************************************************
** INDI Element
*******************************************************************/
INDI_E::INDI_E(INDI_P *parentProperty, TQString inName, TQString inLabel)
{
  name = inName;
  label = inLabel;

  pp = parentProperty;

  EHBox     = new TQHBoxLayout(0, 0, KDialog::spacingHint());
  label_w   = NULL;
  read_w    = NULL;
  write_w   = NULL;
  spin_w    = NULL;
  slider_w  = NULL;
  push_w    = NULL;
  browse_w  = NULL;
  check_w   = NULL;
  led_w     = NULL;
  hSpacer   = NULL;

}

INDI_E::~INDI_E()
{
    delete (EHBox);
    delete (label_w);
    delete (read_w);
    delete (write_w);
    delete (spin_w);
    delete (slider_w);
    delete (push_w);
    delete (browse_w);
    delete (check_w);
    delete (led_w);
    delete (hSpacer);
}

void INDI_E::setupElementLabel()
{
label_w = new KSqueezedTextLabel(pp->pg->propertyContainer);
label_w->setMinimumWidth(ELEMENT_LABEL_WIDTH);
label_w->setMaximumWidth(ELEMENT_LABEL_WIDTH);
label_w->setFrameShape( KSqueezedTextLabel::Box );
label_w->setPaletteBackgroundColor( TQColor( 224, 232, 238 ) );
label_w->setTextFormat( TQLabel::RichText );
label_w->tqsetAlignment( int( TQLabel::WordBreak | TQLabel::AlignCenter ) );

if (label.length() > MAX_LABEL_LENGTH)
{
  TQFont tempFont(  label_w->font() );
  tempFont.setPointSize( tempFont.pointSize() - MED_INDI_FONT );
  label_w->setFont( tempFont );
}

label_w->setText(label);

EHBox->addWidget(label_w);
}

int INDI_E::buildTextGUI(TQString initText)
{

  setupElementLabel();  
  
  text = initText;
  
  switch (pp->perm)
  {
    case PP_RW:
    setupElementRead(ELEMENT_READ_WIDTH);
    setupElementWrite(ELEMENT_WRITE_WIDTH);
    
    break;
    
    case PP_RO:
    setupElementRead(ELEMENT_FULL_WIDTH);
    break;
    
    case PP_WO:
    setupElementWrite(ELEMENT_FULL_WIDTH);
    break;
  }
  
  pp->PVBox->addLayout(EHBox);
  return (0);
    
}

int INDI_E::buildBLOBGUI()
{

  setupElementLabel();  
  
  text = "INDI DATA STREAM";
  
  switch (pp->perm)
  {
    case PP_RW:
      setupElementRead(ELEMENT_READ_WIDTH);
      setupElementWrite(ELEMENT_WRITE_WIDTH);
      setupBrowseButton();
    
      break;
    
    case PP_RO:
      setupElementRead(ELEMENT_FULL_WIDTH);
      break;
    
    case PP_WO:
      setupElementWrite(ELEMENT_FULL_WIDTH);
      setupBrowseButton();
      break;
  }
  
  pp->PVBox->addLayout(EHBox);
  return (0);
    
}

int INDI_E::buildNumberGUI  (double initValue)
{
  bool scale = false;
  
  updateValue(initValue);
  setupElementLabel();
  
  if (step != 0 && (max - min)/step <= MAXSCSTEPS)
    scale = true;
  
  switch (pp->perm)
  {
    case PP_RW:
     setupElementRead(ELEMENT_READ_WIDTH);
     if (scale)
       setupElementScale(ELEMENT_WRITE_WIDTH);
     else
       setupElementWrite(ELEMENT_WRITE_WIDTH);
       
       pp->PVBox->addLayout(EHBox);
     break;
     
    case PP_RO:
    setupElementRead(ELEMENT_READ_WIDTH);
    pp->PVBox->addLayout(EHBox);
    break;
    
    case PP_WO:
    if (scale)
     setupElementScale(ELEMENT_FULL_WIDTH);
    else
     setupElementWrite(ELEMENT_FULL_WIDTH);
     
     pp->PVBox->addLayout(EHBox);
    
    break;
  }
  
  return (0);
    
}

int INDI_E::buildLightGUI()
{

        led_w = new KLed (pp->pg->propertyContainer);
	led_w->setMaximumSize(16,16);
	led_w->setLook( KLed::Sunken );
	drawLt();
	
	EHBox->addWidget(led_w);
	
	setupElementLabel();
	
	pp->PVBox->addLayout(EHBox);
	
	return (0);
}

void INDI_E::drawLt()
{
        /* set state light */
	switch (state)
	{
	  case PS_IDLE:
	  led_w->setColor(TQt::gray);
	  break;

	  case PS_OK:
	  led_w->setColor(TQt::green);
	  break;

	  case PS_BUSY:
	  led_w->setColor(TQt::yellow);
	  break;

	  case PS_ALERT:
	  led_w->setColor(TQt::red);
	  break;

	  default:
	  break;

	}
}


void INDI_E::updateValue(double newValue)
{
  char iNumber[32];
  
  value = newValue; 

  numberFormat(iNumber, format.ascii(), value);
  text = iNumber;

}

void INDI_E::setupElementScale(int length)
{

int steps = (int) ((max - min) / step);
spin_w    = new KDoubleSpinBox(min, max, step, value, 2, pp->pg->propertyContainer );
slider_w  = new TQSlider(0, steps, 1, (int) ((value - min) / step),  Qt::Horizontal, pp->pg->propertyContainer );

connect(spin_w, TQT_SIGNAL(valueChanged(double)), this, TQT_SLOT(spinChanged(double )));
connect(slider_w, TQT_SIGNAL(sliderMoved(int)), this, TQT_SLOT(sliderChanged(int )));

//kdDebug() << "For element " << label << " we have step of " << step << endl;

  if (length == ELEMENT_FULL_WIDTH)
	spin_w->tqsetSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)7, (TQSizePolicy::SizeType)0, 0, 0, spin_w->sizePolicy().hasHeightForWidth() ) );
  else
	spin_w->tqsetSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)0, (TQSizePolicy::SizeType)0, 0, 0, spin_w->sizePolicy().hasHeightForWidth() ) );
	
spin_w->setMinimumWidth( (int) (length * 0.45) );
slider_w->setMinimumWidth( (int) (length * 0.55) );

EHBox->addWidget(slider_w);
EHBox->addWidget(spin_w);
}

void INDI_E::spinChanged(double value)
{
  int slider_value = (int) ((value - min) / step);
  slider_w->setValue(slider_value);
}

void INDI_E::sliderChanged(int value)
{

 double spin_value = (value * step) + min;
 spin_w->setValue(spin_value);  

}
   
void INDI_E::setMin (double inMin)
{
  min = inMin;
  if (spin_w)
  {
    spin_w->setMinValue(min);
    spin_w->setValue(value);
  }
  if (slider_w)
  {
    slider_w->setMaxValue((int) ((max - min) / step));
    slider_w->setMinValue(0);
    slider_w->setPageStep(1);
    slider_w->setValue( (int) ((value - min) / step ));
  }
  
}
   
void INDI_E::setMax (double inMax)
{
 max = inMax;
 if (spin_w)
 {
   spin_w->setMaxValue(max);
   spin_w->setValue(value);
 }
 if (slider_w)
 {
    slider_w->setMaxValue((int) ((max - min) / step));
    slider_w->setMinValue(0);
    slider_w->setPageStep(1);
    slider_w->setValue( (int) ((value - min) / step ));
 }
 
}
   
void INDI_E::setupElementWrite(int length)
{
    write_w = new KLineEdit( pp->pg->propertyContainer);
    write_w->tqsetSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)0, (TQSizePolicy::SizeType)0, 0, 0, write_w->sizePolicy().hasHeightForWidth() ));
    write_w->setMinimumWidth( length );
    write_w->setMaximumWidth( length);
    
    TQObject::connect(write_w, TQT_SIGNAL(returnPressed()), pp, TQT_SLOT(newText()));
    EHBox->addWidget(write_w);
}


void INDI_E::setupElementRead(int length)
{

  read_w = new KLineEdit( pp->pg->propertyContainer );
  read_w->setMinimumWidth( length );
  read_w->setFocusPolicy( TQ_NoFocus );
  read_w->setFrameShape( KLineEdit::GroupBoxPanel );
  read_w->setFrameShadow( KLineEdit::Plain );
  read_w->setCursorPosition( 0 );
  read_w->tqsetAlignment( int( KLineEdit::AlignHCenter ) );
  read_w->setReadOnly( TRUE );
  read_w->setText(text);
  
  EHBox->addWidget(read_w);

}

void INDI_E::setupBrowseButton()
{
   browse_w = new KPushButton("...", pp->pg->propertyContainer);
   browse_w->tqsetSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)5, (TQSizePolicy::SizeType)0, 0, 0, browse_w->sizePolicy().hasHeightForWidth() ) );
   browse_w->setMinimumWidth( MIN_SET_WIDTH );
   browse_w->setMaximumWidth( MAX_SET_WIDTH );

   EHBox->addWidget(browse_w);
   TQObject::connect(browse_w, TQT_SIGNAL(clicked()), this, TQT_SLOT(browseBlob()));
}


void INDI_E::initNumberValues(double newMin, double newMax, double newStep, char * newFormat)
{
  min = newMin;
  max = newMax;
  step = newStep;
  format = newFormat;
}

void INDI_E::browseBlob()
{

  KURL currentURL;

  currentURL = KFileDialog::getOpenURL( TQDir::homeDirPath(), "*");

  // if user presses cancel
  if (currentURL.isEmpty())
		  return;

  if ( currentURL.isValid() )
    write_w->setText(currentURL.path());

}


#include "indielement.moc"