File: ChannelEditorDlg.cpp

package info (click to toggle)
camstream 0.27%2Bdfsg-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 12,368 kB
  • ctags: 5,393
  • sloc: cpp: 17,031; sh: 8,154; asm: 455; ansic: 440; makefile: 343
file content (618 lines) | stat: -rw-r--r-- 16,670 bytes parent folder | download | duplicates (2)
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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*  CamStream: a collection of GUI webcam tools
    Copyright (C) 2002-2005 Nemosoft Unv.

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    For questions, remarks, patches, etc. for this program, the author can be
    reached at camstream@smcc.demon.nl.
*/


#include <qcombobox.h>
#include <qlcdnumber.h>
#include <qlineedit.h>
#include <qlistbox.h>
#include <qlistview.h>
#include <qpushbutton.h>
#include <qslider.h>

#include "ChannelEditorDlg.h"



/**
  \class CChannelEditorDlg
  \brief Complete dialog that handles setting up the channels for a TV tuner card

*/



CChannelEditorDlg::CChannelEditorDlg(QWidget *parent, const char *name)
       : CChannelEditor(parent, name, true)
{
   m_pTVChannels = 0;
   m_CurrentFrequencySystem = TVChannel::FrequencyGrid_MAX;
   m_CurrentIsNew = false;
   m_UpdatingGUI = false;
   m_PresetList->setSorting(-1); // do not allow sorting
}

// private


/**
  Does several things:
  # sets m_TVChannels.current
  # calls SetDialogControls
  # sets PresetList
  # emits NewFrequency (if possible)
  # emits NewCurrentChannel
*/
void CChannelEditorDlg::SetNewCurrent(TVChannel *channel)
{
qDebug(">> CChannelEditorDlg::SetNewCurrent(%p)", channel);
   QListViewItem *lvi = 0;

   if (channel != 0) {
     if (m_pTVChannels->find(channel) < 0) {
       qWarning("CChannelEditorDlg::SetNewCurrent: channel not found.");
     }
   }

   SetDialogControls(channel);
   lvi = m_Channel2ListView[channel];
   if (lvi != 0)
     m_PresetList->setSelected(lvi, true);

   if (channel != 0) {
     emit NewFrequency(channel->Frequency() * 1e6);
   }
   emit NewCurrentChannel();
qDebug("<< CChannelEditorDlg::SetNewCurrent");
}

/**
  \brief Update various edit boxes to the current TVChannel struct

  This will update the name, channel, fine-tuning and color-system boxes
  to the current TVChannel struct. If that happens to be a null pointer, the
  elements in the dialog are blanked and disabled.

*/
void CChannelEditorDlg::SetDialogControls(const TVChannel *channel)
{
   QString s;

   m_UpdatingGUI = true;
   if (channel == 0) {
     m_NameLineEdit->setText("");
     SetFrequencySystem(TVChannel::FrequencyGrid_MAX);
     m_SystemChannels->setCurrentItem(-1);
     m_FinetuningSlider->setValue(0);
     m_FrequencyLCD->display("0.00");
   }
   else {
     m_NameLineEdit->setText(channel->Name);
     SetFrequencySystem(channel->FrequencySystem);
     m_SystemChannels->setCurrentItem(channel->Channel);
     m_FinetuningSlider->setValue(channel->Finetuning);
     switch(channel->ColorSystem)
     {
       case TVChannel::PAL_BG:     m_ColorSystemDropdown->setCurrentItem(0); break;
       case TVChannel::NTSC:       m_ColorSystemDropdown->setCurrentItem(1); break;
       case TVChannel::SECAM:      m_ColorSystemDropdown->setCurrentItem(2); break;
       case TVChannel::PAL_NC:     m_ColorSystemDropdown->setCurrentItem(3); break;
       case TVChannel::PAL_M:      m_ColorSystemDropdown->setCurrentItem(4); break;
       case TVChannel::PAL_N:      m_ColorSystemDropdown->setCurrentItem(5); break;
       case TVChannel::NTSC_JAPAN: m_ColorSystemDropdown->setCurrentItem(6); break;
     }
     m_FrequencyLCD->display(s.sprintf("%5.2f", channel->Frequency()));
   }

   m_NameLineEdit->setEnabled(channel != 0);
   m_FrequencySystemDropdown->setEnabled(channel != 0);
   m_SystemChannels->setEnabled(channel != 0);
   m_FinetuningSlider->setEnabled(channel != 0);
   m_ColorSystemDropdown->setEnabled(channel != 0);
   m_UpdatingGUI = false;
}


/**
  \brief Update columns in ListView
  \param channel TVChannel struct

  Sets the values in the colums in the Presets listview at the
  left hand side of the dialog for the given \b channel
*/

void CChannelEditorDlg::SetListEntry(const TVChannel *channel)
{
  QString s;
  QListViewItem *lvi;

  if (channel == 0)
    return;
  lvi = m_Channel2ListView[channel];
  if (lvi == 0)
    return;

  lvi->setText(0, channel->Name);
  switch(channel->FrequencySystem)
  {
    case TVChannel::American: s = tr("American"); break;
    case TVChannel::European: s = tr("European"); break;
    case TVChannel::Japanese: s = tr("Japanese"); break;
  }
  lvi->setText(1, s);
  lvi->setText(2, s.sprintf("%5.2f", channel->Frequency()));
  switch(channel->ColorSystem)
  {  // Not translated on purpose
    case TVChannel::PAL_BG  : s = "PAL-BG"; break;
    case TVChannel::NTSC    : s = "NTSC";   break;
    case TVChannel::SECAM   : s = "SECAM";  break;
    case TVChannel::PAL_NC  : s = "PAL-NC"; break;
    case TVChannel::PAL_M   : s = "PAL-M";  break;
    case TVChannel::PAL_N   : s = "PAL-N";  break;
    case TVChannel::NTSC_JAPAN: s = "NTSC-Japan"; break;
  }
  lvi->setText(3, s);
}


// protected

/**
  \brief New frequency table
  \param table The new system (see \ref FrequencySystem)

  Different countries (continents) have different frequency ranges
  available for their terrestial TV broadcasts. Selecting a new
  table will fill the m_SystemChannels listbox with the channel names.

  If the system is already selected, nothing happens.

*/
void CChannelEditorDlg::SetFrequencySystem(TVChannel::FrequencyGrid system)
{
   int i, j;
   QString fname;

   if (m_CurrentFrequencySystem == system)
     return;
   if (system < 0 || system >= TVChannel::FrequencyGrid_MAX)
     return;

   // Remember...
   m_CurrentFrequencySystem = system;
   // Prevent loops
   m_FrequencySystemDropdown->blockSignals(true);

   // Set GUI
   switch (system) {
     case TVChannel::American:
       m_FrequencySystemDropdown->setCurrentItem(0);
       break;

     case TVChannel::European:  // Europe, including cable
       m_FrequencySystemDropdown->setCurrentItem(1);
       m_FinetuningSlider->setMinValue(-400); //  -4 MHz
       m_FinetuningSlider->setMaxValue(400);
       m_FinetuningSlider->setPageStep(40);
       break;

     case TVChannel::Japanese:
       m_FrequencySystemDropdown->setCurrentItem(2);
       break;

     case TVChannel::FrequencyGrid_MAX:
       m_FrequencySystemDropdown->setCurrentItem(-1);
       m_FrequencySystemDropdown->setEditText(QString::null);
       break;
   } // ..switch

   // Copy names to m_SystemChannels listview box
   m_SystemChannels->clear();
   j = TVChannel::GetNumberOfChannels(system);
   for (i = 0; i < j; i++) {
      m_SystemChannels->insertItem(TVChannel::ChannelName(system, i), i);
   }

   m_FrequencySystemDropdown->blockSignals(false);
}

/**
  \brief Remove channel

  This function removes the channel both from the channel list and the
  display. May update the current pointer.
*/
void CChannelEditorDlg::RemoveChannel(TVChannel *channel)
{
   QListViewItem *lvi = 0;

   if (m_pTVChannels == 0)
     return;
   if (channel == 0)
     return;

   lvi = m_Channel2ListView[channel];
   // Remember, m_pTVChannels is auto-delete
   if (!m_pTVChannels->remove(channel)) {
      qWarning("CChannelEditorDlg::RemoveChannel() Could not remove channel from list!");
   }
   else {
     if (lvi != 0) {
       delete lvi;
     }
     SetNewCurrent(m_pTVChannels->current());
   }
}


/**
   \brief Add channel at specified position

   \param channel The new TV Channel
   \param index Location in array; the default -1 means at end of list (append)

   This function adds the channel to the list

*/
void CChannelEditorDlg::AddChannel(TVChannel *channel, int index)
{
   QListViewItem *lvi = 0;

   if (m_pTVChannels == 0)
     return;

qDebug("AddChannel: index = %d", index);
   if (index == 0) {
qDebug("Added at front");
     lvi = 0; // added at front
   }
   else if (index < 0 || index >= m_pTVChannels->count()) { // added at end
qDebug("Added at end");
     lvi = m_Channel2ListView[m_pTVChannels->getLast()];
   }
   else {
qDebug("Added in between");
     lvi = m_Channel2ListView[m_pTVChannels->at(index - 1)]; // changes current ptr!
   }

qDebug("lvi = %p", lvi);
   if (lvi == 0) {
     lvi = new QListViewItem(m_PresetList);
   }
   else {
     lvi = new QListViewItem(m_PresetList, lvi);
   }

   if (index < 0 || index >= m_pTVChannels->count()) {
     m_pTVChannels->append(channel);
   }
   else {
     m_pTVChannels->insert(index, channel);
   }

   if (lvi == 0) {
     qWarning("CChannelEditorDlg::AddChannel() lvi = 0");
   }
   else {
     m_ListView2Channel[lvi] = channel;
     m_Channel2ListView[channel] = lvi;
   }
}


// protected slots

/**
  \brief Helper slot for the Name linedit box

  Called whenever the user types something in the Name box (which
  contains the name of the channel)
*/
void CChannelEditorDlg::NameChanged(const QString &new_name)
{
   if (m_UpdatingGUI) // prevent loops
     return;
   if (m_pTVChannels->current() != 0)
   {
     m_pTVChannels->current()->Name = new_name;
   }
   SetDialogControls(m_pTVChannels->current());
   SetListEntry(m_pTVChannels->current());
}

/**
  \brief Helper slot for the Frequency system dropdown box

*/
void CChannelEditorDlg::FrequencySystemChanged(int table)
{
   if (m_UpdatingGUI) // prevent loops
     return;
   if (m_pTVChannels->current() != 0) {
     switch (table) {
       case 0: m_pTVChannels->current()->FrequencySystem = TVChannel::American; break;
       case 1: m_pTVChannels->current()->FrequencySystem = TVChannel::European; break;
       case 2: m_pTVChannels->current()->FrequencySystem = TVChannel::Japanese; break;
     }
     m_pTVChannels->current()->Channel = 0;
     m_pTVChannels->current()->Finetuning = 0;
   }
   SetDialogControls(m_pTVChannels->current());
   SetListEntry(m_pTVChannels->current());
   if (m_pTVChannels->current() != 0) {
     emit NewFrequency(m_pTVChannels->current()->Frequency() * 1e6);
   }
}

/**
  \brief Helper slot for the system channel listview box

  Called when the user selects one of the entries in the list of system
  channels systembox; this are the pre-programmed channels in the current
  frequency grid (not to be confused with the Preset list, which is
  the list of TV channels that the user programs).

  Also emits new frequency.
*/
void CChannelEditorDlg::SystemChannelClicked(int channel)
{
   if (m_UpdatingGUI) // prevent loops
     return;
   if (m_pTVChannels->current() != 0) {
     m_pTVChannels->current()->Channel = channel;
   }
   SetListEntry(m_pTVChannels->current());
   SetNewCurrent(m_pTVChannels->current());
}

/**
  \brief Helper slot for the finetuning slider

  For finetuning the frequency of the current TV channel (preset)

  Also emits new frequency.
*/
void CChannelEditorDlg::FinetuningMoved(int offset)
{
   if (m_UpdatingGUI) // prevent loops
     return;
   if (m_pTVChannels->current() != 0)
   {
     m_pTVChannels->current()->Finetuning = offset;
   }
   SetDialogControls(m_pTVChannels->current());
   SetListEntry(m_pTVChannels->current());
   if (m_pTVChannels->current() != 0) {
     emit NewFrequency(m_pTVChannels->current()->Frequency() * 1e6);
   }
}

/**
  \brief Helper slot for Color System dropdown box

  When the user select a new color system (a.k.a. 'norm') from the drop down
  box, this will set that norm in the current channel (if any). It also
  emits a NewColorSystem signal.

*/
void CChannelEditorDlg::ColorSystemChanged(int color)
{
   if (m_UpdatingGUI) // prevent loops
     return;
  if (m_pTVChannels->current() != 0)
  {
    switch (color) {
      case 0: m_pTVChannels->current()->ColorSystem = TVChannel::PAL_BG; break;
      case 1: m_pTVChannels->current()->ColorSystem = TVChannel::NTSC; break;
      case 2: m_pTVChannels->current()->ColorSystem = TVChannel::SECAM; break;
      case 3: m_pTVChannels->current()->ColorSystem = TVChannel::PAL_NC; break;
      case 4: m_pTVChannels->current()->ColorSystem = TVChannel::PAL_M; break;
      case 5: m_pTVChannels->current()->ColorSystem = TVChannel::PAL_N; break;
      case 6: m_pTVChannels->current()->ColorSystem = TVChannel::NTSC_JAPAN; break;
    } // ..switch
  }
  SetDialogControls(m_pTVChannels->current());
  SetListEntry(m_pTVChannels->current());
  if (m_pTVChannels->current() != 0) {
    emit NewColorSystem(m_pTVChannels->current()->ColorSystem);
  }
}


/**
  \brief Helper slot for "Delete" button

  Removes the current entry and selects the next (or previous in case of
  end-of-list) entry, or 0 if it was the last one.
*/
void CChannelEditorDlg::DeleteClicked()
{
   TVChannel *pOldChannel = 0;

   // Check pointers
   if (m_pTVChannels == 0)
     return;

   // Already empty? Nothing to do...
   if (m_pTVChannels->count() == 0)
     return;

   RemoveChannel(m_pTVChannels->current());
}


/**
  \brief Move channel 'up' (earlier in list)

*/
void CChannelEditorDlg::UpClicked()
{
   int At;
   TVChannel *Copy, *Old;
   QListViewItem *lvi = 0, *OldLV = 0, *PrevLV;

   if (m_pTVChannels == 0)
     return;
   At = m_pTVChannels->at(); // current position in list
   if (At <= 0) // already at top/empty
     return;

   // okay... now create copy, remove current, insert copy before previous. Can't do swaps :(
   Old = m_pTVChannels->current();
   OldLV = m_Channel2ListView[Old];
   Copy = new TVChannel(*Old);

   AddChannel(Copy, At - 1);
   RemoveChannel(Old);
   SetListEntry(Copy);
   SetNewCurrent(Copy);

#ifdef DEBUG
   QListIterator<TVChannel> it(*m_pTVChannels);
   while (it.current() != 0)
   {
      qDebug("channel: " + it.current()->Name);
      ++it;
   }
#endif
}

/**
  \brief Move channel 'down' (later in list)

*/
void CChannelEditorDlg::DownClicked()
{
   int At;
   TVChannel *Copy, *Old;
   QListViewItem *lvi = 0, *OldLV = 0, *PrevLV;

   if (m_pTVChannels == 0)
     return;
   At = m_pTVChannels->at(); // current position in list
   if ((At + 1) >= m_pTVChannels->count()) // already at bottom/empty
     return;

   // okay... now create copy, remove current, insert copy after new current.
   Old = m_pTVChannels->current();
   OldLV = m_Channel2ListView[Old];
   Copy = new TVChannel(*Old);

   RemoveChannel(Old);
   AddChannel(Copy, At + 1);
   SetListEntry(Copy);
   SetNewCurrent(Copy);
#ifdef DEBUG
   QListIterator<TVChannel> it(*m_pTVChannels);
   while (it.current() != 0)
   {
      qDebug("channel: " + it.current()->Name);
      ++it;
   }
#endif
}


/**
   \brief Slot for when user clicked on "New..."

   This will create a new entry and append it to the list of channel presets.
   A new entry will be based on the current channel (if any)
*/
void CChannelEditorDlg::NewClicked()
{
   TVChannel *v = 0;

   if (m_pTVChannels == 0)
     return;

   if (m_pTVChannels->current() != 0) {
     v = new TVChannel(*(m_pTVChannels->current()));
     v->Name = "New";
   }
   else {
     v = new TVChannel();
   }

   if (v != 0) {
     AddChannel(v);
     SetListEntry(v);
     SetNewCurrent(v);

     m_NameLineEdit->setFocus();
     buttonApply->setEnabled(true);
     buttonCancel->setEnabled(true);
     m_CurrentIsNew = true;
   }
   else {
     qWarning("CChannelEditorDlg::NewClicked(): could not allocated TVChannel struct.");
   }
}


/**
  \brief Helper slot for Preset list view

  Called when an item in the Preset listview is clicked. It will
  set the current TVChannel pointer belonging to the entry.
*/
void CChannelEditorDlg::PresetClicked(QListViewItem *lvi)
{
qDebug(">> CChannelEditorDlg::PresetClicked(%p)", lvi);
   TVChannel *Search = 0;

   Search = m_ListView2Channel[lvi];
   SetNewCurrent(Search);
qDebug("<< CChannelEditorDlg::PresetClicked()");
}



// public

void CChannelEditorDlg::SetTVChannels(QList<TVChannel> *tv_channels)
{
   QListViewItem *after = 0;

   // clear existing arrays/widgets
   m_Channel2ListView.clear();
   m_ListView2Channel.clear();
   m_PresetList->clear();

   m_pTVChannels = tv_channels;
   if (m_pTVChannels == 0)
     return;

   // fill listview with channels
   QListIterator<TVChannel> it(*m_pTVChannels);
   while (it.current() != 0)
   {
     QListViewItem *lvi = new QListViewItem(m_PresetList, after);
     m_Channel2ListView[it.current()] = lvi;
     m_ListView2Channel[lvi] = it.current();
     SetListEntry(it.current());
     ++it;
     after = lvi;
   }

   SetDialogControls(m_pTVChannels->current());
}