~ubuntu-branches/ubuntu/feisty/kdetv/feisty

« back to all changes in this revision

Viewing changes to kdetv/clients/shared/actions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-09-17 23:25:16 UTC
  • Revision ID: james.westby@ubuntu.com-20050917232516-9wdsn3ckagbqieh8
Tags: upstream-0.8.8
ImportĀ upstreamĀ versionĀ 0.8.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Copyright (C) 2002 Richard Moore <rich@kde.org>
 
4
 *
 
5
 *   This program is free software; you can redistribute it and/or modify 
 
6
 *   it under the terms of the GNU General Public License as published by 
 
7
 *   the Free Software Foundation; either version 2 of the License, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 */
 
11
 
 
12
#include <qaccel.h>
 
13
#include <qlcdnumber.h>
 
14
#include <qpoint.h>
 
15
#include <qslider.h>
 
16
#include <qstylesheet.h>
 
17
#include <qtooltip.h>
 
18
#include <qvbox.h>
 
19
 
 
20
#include <kdebug.h>
 
21
#include <klocale.h>
 
22
#include <kmainwindow.h>
 
23
#include <kmenubar.h>
 
24
#include <kstatusbar.h>
 
25
#include <ktoolbar.h>
 
26
#include <kdockwidget.h>
 
27
 
 
28
#include "kdetv.h"
 
29
#include "actions.h"
 
30
#include "actions.moc"
 
31
 
 
32
//
 
33
// LCD Number Action
 
34
//
 
35
 
 
36
LCDNumberAction::LCDNumberAction( const QString &text, int accel, 
 
37
                                  QObject* receiver, const char* slot, 
 
38
                                  QObject *parent, const char *name )
 
39
    :KAction( text, accel, receiver, slot, parent, name ),
 
40
     numDig(3), val("  0")
 
41
{
 
42
}
 
43
 
 
44
int LCDNumberAction::plug( QWidget *widget, int index )
 
45
{
 
46
    //do not call the previous implementation here
 
47
 
 
48
    if ( widget->inherits( "KToolBar" ) ) {
 
49
        
 
50
        KToolBar *tb = static_cast<KToolBar *>(widget);
 
51
        int id = KAction::getToolButtonID();
 
52
        
 
53
        QLCDNumber *lcd = createWidget( tb );
 
54
        tb->insertWidget( id, lcd->width(), lcd, index );
 
55
        addContainer( tb, id );
 
56
        
 
57
        connect( tb, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
 
58
        return containerCount() - 1;
 
59
    }
 
60
 
 
61
    if ( widget->inherits( "QPopupMenu" ) ) {
 
62
        QPopupMenu *pop = static_cast<QPopupMenu *>(widget);
 
63
        
 
64
        // Create item
 
65
        QHBox *box = new QHBox( pop );
 
66
        box->setMargin( 4 );
 
67
        box->setSpacing( 6 );
 
68
        
 
69
        // Icon
 
70
        QLCDNumber *num = createWidget( box );
 
71
        num->setFixedSize( QSize(18,18) );
 
72
        
 
73
        // Label
 
74
        QLabel *l = new QLabel( box );
 
75
        l->setText( text() );
 
76
 
 
77
        // Insert item
 
78
        int id = pop->insertItem( box, -1, index );
 
79
        addContainer( box, id );
 
80
        connect( box, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
 
81
        
 
82
        return containerCount() - 1;
 
83
    }
 
84
 
 
85
    return -1;
 
86
}
 
87
 
 
88
void LCDNumberAction::unplug( QWidget *widget )
 
89
{
 
90
    if ( widget->inherits( "KToolBar" ) ) {
 
91
        KToolBar *bar = static_cast<KToolBar *>(widget);
 
92
        int idx = findContainer( bar );
 
93
 
 
94
        if ( idx != -1 ) {
 
95
            bar->removeItem( menuId( idx ) );
 
96
            removeContainer( idx );
 
97
        }
 
98
    }
 
99
    else if ( widget->inherits( "QPopupMenu" ) ) {
 
100
        QPopupMenu *pop = static_cast<QPopupMenu *>(widget);
 
101
        
 
102
        int idx = findContainer( pop );
 
103
        if ( idx != -1 ) {
 
104
            pop->removeItem( menuId( idx ) );
 
105
            removeContainer( idx );
 
106
        }
 
107
    }
 
108
}
 
109
 
 
110
QLCDNumber *LCDNumberAction::createWidget( QWidget *parent, const char *name )
 
111
{
 
112
    QLCDNumber *lcd = new QLCDNumber( parent, name );
 
113
    lcd->setFrameStyle( QFrame::Panel | QFrame::Sunken );
 
114
    lcd->setLineWidth(2);
 
115
    lcd->setMidLineWidth(1);
 
116
 
 
117
    lcd->setSegmentStyle( QLCDNumber::Flat );
 
118
    lcd->setBackgroundColor( Qt::black );
 
119
    lcd->setPaletteForegroundColor( Qt::green );
 
120
 
 
121
    lcd->setNumDigits( numDig );
 
122
    lcd->display( val );
 
123
 
 
124
    if ( !text().isEmpty() )
 
125
        QToolTip::add( lcd, text() );
 
126
 
 
127
    connect( this, SIGNAL( valueChanged(const QString &) ),
 
128
             lcd, SLOT( display(const QString &) ) );
 
129
    return lcd;
 
130
}
 
131
 
 
132
void LCDNumberAction::setNumDigits( int nDigits )
 
133
{
 
134
    numDig = nDigits;
 
135
}
 
136
 
 
137
void LCDNumberAction::display( int num )
 
138
{
 
139
    display( QString::number(num) );
 
140
}
 
141
 
 
142
void LCDNumberAction::display( const QString &s )
 
143
{
 
144
    val = s;
 
145
    emit valueChanged( val );
 
146
}
 
147
 
 
148
//
 
149
// Slider Action
 
150
//
 
151
 
 
152
KdetvSlider::KdetvSlider( int minValue, int maxValue, int pageStep, int value, Orientation o,
 
153
                          QWidget *parent, const char* name )
 
154
    : QSlider( minValue, maxValue, pageStep, value, o, parent, name)
 
155
{
 
156
}
 
157
 
 
158
KdetvSlider::~KdetvSlider()
 
159
{
 
160
}
 
161
 
 
162
void KdetvSlider::setOrientation( Orientation o )
 
163
{
 
164
    QSlider::setOrientation(o);
 
165
    emit orientationChanged(o);
 
166
}
 
167
 
 
168
void KdetvSlider::updateOrientation()
 
169
{
 
170
    if (!parent()->inherits("QDockWindow"))
 
171
        return;
 
172
 
 
173
    QDockWindow* d = static_cast<QDockWindow*>(parent());
 
174
    setOrientation( d->orientation() );
 
175
}
 
176
 
 
177
 
 
178
SliderAction::SliderAction( int min_, int max_, int step_, int val_,
 
179
                            const QString &text, QObject *parent, 
 
180
                            const char *name )
 
181
    : KAction( parent, name ), inhibitRecursion(false), min(min_),
 
182
      max(max_), step(step_), val(val_), tickStep(-1)
 
183
{
 
184
    setText( text );
 
185
}
 
186
 
 
187
int SliderAction::plug( QWidget *widget, int index )
 
188
{
 
189
    //do not call the previous implementation here
 
190
    kdDebug() << "Plugging Slider into class '" << widget->className() << "'" << endl;
 
191
 
 
192
    if ( widget->inherits( "KToolBar" ) || widget->isA("KToolBar")) {
 
193
        KToolBar *bar = static_cast<KToolBar *>(widget);
 
194
 
 
195
        int id = KAction::getToolButtonID();
 
196
 
 
197
        KdetvSlider* s = createWidget( bar, "ToolbarSlider" );
 
198
        s->setOrientation( bar->orientation() );
 
199
 
 
200
        int sz = (s->orientation() == Horizontal) ? s->width() : s->height();
 
201
        bar->insertWidget( id, sz, s, index );
 
202
        addContainer( bar, id );
 
203
 
 
204
        connect( bar, SIGNAL( orientationChanged(Orientation) ),
 
205
                 s, SLOT( setOrientation(Orientation) ) );
 
206
 
 
207
        /*
 
208
         * FIXME: This is a really bad hack to get events
 
209
         *        _after_ the GUI has been created.
 
210
         *        If I don't do this, the slider orientation
 
211
         *        is not updated at startup.
 
212
         *        It seems QToolBar does not emit orientationChanged()
 
213
         *        during startup phase.
 
214
         *        This connection creates loads of unnesscessary
 
215
         *        updateOrientation() calls...
 
216
         *        I tried lots of other signals (QToolBar::visibilityChanged(),
 
217
         *        QMainWindow::dockWindowPositionChanged() and many others)
 
218
         *        but none of them works.
 
219
         */
 
220
        if (bar->mainWindow()->inherits("KDockMainWindow")) {
 
221
            KDockMainWindow* kdmw = static_cast<KDockMainWindow*>(bar->mainWindow());
 
222
            connect( kdmw->manager(), SIGNAL( change() ), 
 
223
                     s, SLOT( updateOrientation() ) );
 
224
        }
 
225
 
 
226
        connect( bar, SIGNAL( destroyed() ),
 
227
                 this, SLOT( slotDestroyed() ) );
 
228
 
 
229
        return containerCount() - 1;
 
230
    }
 
231
 
 
232
    if ( widget->inherits( "QPopupMenu" ) ) {
 
233
        QPopupMenu *pop = static_cast<QPopupMenu *>(widget);
 
234
 
 
235
        // Create item
 
236
        QHBox *box = new QHBox( pop );
 
237
        box->setMargin( 4 );
 
238
        box->setSpacing( 6 );
 
239
 
 
240
        // Icon
 
241
        QLabel *p = new QLabel( box );
 
242
        if ( hasIconSet() ) {
 
243
            QPixmap pix = iconSet().pixmap( QIconSet::Small, true );
 
244
            p->setPixmap( pix );
 
245
        }
 
246
        p->setFixedSize( QSize(18,18) );
 
247
 
 
248
        // Label
 
249
        QLabel *l = new QLabel( box );
 
250
        l->setText( text() );
 
251
 
 
252
        // Slider
 
253
        KdetvSlider* s = createWidget( box, "PopupSlider" );
 
254
 
 
255
        // Focus handling
 
256
        s->setFocusPolicy( QWidget::TabFocus );
 
257
        l->setBuddy( s );
 
258
        box->setFocusProxy( s );
 
259
 
 
260
        // Insert item
 
261
        int id = pop->insertItem( box, -1, index );
 
262
        addContainer( pop, id );
 
263
        connect( box, SIGNAL( destroyed() ),
 
264
                 this, SLOT( slotDestroyed() ) );
 
265
 
 
266
        return containerCount() - 1;
 
267
    }
 
268
 
 
269
    return -1;
 
270
 
 
271
}
 
272
 
 
273
void SliderAction::unplug( QWidget *widget )
 
274
{
 
275
    if ( widget->inherits( "KToolBar" ) )
 
276
        {
 
277
            KToolBar *bar = static_cast<KToolBar *>(widget);
 
278
 
 
279
            int idx = findContainer( bar );
 
280
 
 
281
            if ( idx != -1 ) {
 
282
                bar->removeItem( menuId( idx ) );
 
283
                removeContainer( idx );
 
284
            }
 
285
 
 
286
            return;
 
287
        }
 
288
 
 
289
    if ( widget->inherits( "QPopupMenu" ) ) {
 
290
        QPopupMenu *pop = static_cast<QPopupMenu *>(widget);
 
291
 
 
292
        int idx = findContainer( pop );
 
293
 
 
294
        if ( idx != -1 ) {
 
295
            pop->removeItem( menuId( idx ) );
 
296
            removeContainer( idx );
 
297
        }
 
298
 
 
299
        return;
 
300
    }
 
301
}
 
302
 
 
303
void SliderAction::setOrientation( Orientation o )
 
304
{
 
305
    const QObject* cobj = sender();
 
306
    QObject* obj = const_cast<QObject*>(cobj);
 
307
    
 
308
    if (!obj->inherits("KdetvSlider"))
 
309
        return;
 
310
 
 
311
    KdetvSlider* s = static_cast<KdetvSlider*>(obj);
 
312
 
 
313
    switch( o ) {
 
314
    case Horizontal:
 
315
        s->setTickmarks( QSlider::Below );
 
316
        disconnect( s, SIGNAL(valueChanged(int)), this, SLOT(setInverseInternal(int)) );
 
317
        connect( s, SIGNAL(valueChanged(int)), this, SLOT(setValueInternal(int)));
 
318
        s->setValue( val );
 
319
        break;
 
320
    case Vertical:
 
321
        s->setTickmarks( QSlider::Right );
 
322
        disconnect( s, SIGNAL(valueChanged(int)), this, SLOT(setValueInternal(int)));
 
323
        connect( s, SIGNAL(valueChanged(int)), this, SLOT(setInverseInternal(int)) );
 
324
        s->setValue( max-val );
 
325
        break;
 
326
    default:
 
327
        break;
 
328
    }
 
329
}
 
330
 
 
331
void SliderAction::setValueInternal( int num )
 
332
{
 
333
    if (inhibitRecursion)
 
334
        return;
 
335
 
 
336
    inhibitRecursion = true;
 
337
    setValue(num);
 
338
    inhibitRecursion = false;
 
339
    emit valueChanged(num);
 
340
}
 
341
 
 
342
void SliderAction::setInverseInternal( int num )
 
343
{
 
344
    setValueInternal (max - num);
 
345
 
346
 
 
347
void SliderAction::setValue( int num )
 
348
{
 
349
    if (val == num)
 
350
        return;
 
351
    val = num;
 
352
 
 
353
    for (QPtrListIterator<KdetvSlider> it(_sliders);
 
354
         it.current() != 0;
 
355
         ++it) {
 
356
        if (it.current()->orientation() == Qt::Horizontal) {
 
357
            it.current()->setValue(val);
 
358
        } else {
 
359
            it.current()->setValue(max - val);
 
360
        }
 
361
    }
 
362
}
 
363
 
 
364
void SliderAction::setTickInterval( int ticks )
 
365
{
 
366
    tickStep = ticks;
 
367
    for (QPtrListIterator<KdetvSlider> it(_sliders);
 
368
         it.current() != 0;
 
369
         ++it) {
 
370
        it.current()->setTickInterval( ticks );
 
371
    }
 
372
}
 
373
 
 
374
KdetvSlider *SliderAction::createWidget( QWidget *parent, const char *name )
 
375
{
 
376
    KdetvSlider *s = new KdetvSlider( min, max, step, val, Horizontal, parent, name );
 
377
 
 
378
    _sliders.append(s);
 
379
    connect( s, SIGNAL( destroyed(QObject*) ),
 
380
             this, SLOT( removeSlider(QObject*) ) );
 
381
 
 
382
    connect( s, SIGNAL( valueChanged(int) ),
 
383
             this, SLOT( setValueInternal(int) ) );
 
384
    connect( s, SIGNAL( orientationChanged(Orientation) ),
 
385
             this, SLOT( setOrientation(Orientation) ) );
 
386
 
 
387
    if ( tickStep > 0 )
 
388
        s->setTickInterval( tickStep );
 
389
        
 
390
    s->setTickmarks( QSlider::Below );
 
391
 
 
392
    if ( !text().isEmpty() )
 
393
        QToolTip::add( s, text() );
 
394
 
 
395
    return s;
 
396
}
 
397
 
 
398
void SliderAction::removeSlider( QObject* o )
 
399
{
 
400
    KdetvSlider* s = static_cast<KdetvSlider*>(o);
 
401
    _sliders.remove(s);
 
402
}
 
403
 
 
404
int SliderAction::value() const
 
405
 
406
    return val;
 
407
}
 
408
 
 
409
//
 
410
// Channel import/export action
 
411
//
 
412
 
 
413
ImpExChannelsAction::ImpExChannelsAction( const QString &text, int accel, const QString& fmt, Kdetv* ktv,
 
414
                                          int direction, QObject *parent, const char *name)
 
415
    : KAction(text, accel, parent, name),
 
416
      _fmt(fmt),
 
417
      _ktv(ktv),
 
418
      _direction(direction)
 
419
{
 
420
    connect(this, SIGNAL( activated() ),
 
421
            this, SLOT( slotActivated() ));
 
422
}
 
423
 
 
424
ImpExChannelsAction::~ImpExChannelsAction()
 
425
{
 
426
}
 
427
 
 
428
void ImpExChannelsAction::slotActivated()
 
429
{
 
430
    if (_direction) {
 
431
        _ktv->importChannelFile(_fmt);
 
432
    } else {
 
433
        _ktv->exportChannelFile(_fmt);
 
434
    }
 
435
}
 
436
 
 
437
//
 
438
// Device action
 
439
//
 
440
 
 
441
DeviceAction::DeviceAction( const QString &text, int accel, const QString& dev, 
 
442
                            Kdetv* ktv, QObject *parent, const char *name)
 
443
    : KToggleAction(text, accel, parent, name),
 
444
      _dev(dev),
 
445
      _ktv(ktv)
 
446
{
 
447
    connect(this, SIGNAL( activated() ),
 
448
            this, SLOT( slotActivated() ));
 
449
}
 
450
 
 
451
DeviceAction::~DeviceAction()
 
452
{
 
453
}
 
454
 
 
455
void DeviceAction::slotActivated()
 
456
{
 
457
    _ktv->playDevice(_dev);
 
458
}
 
459
 
 
460
//
 
461
// AudioMode action
 
462
//
 
463
 
 
464
AudioModeAction::AudioModeAction( const QString &text, int accel, const QString& mode, 
 
465
                                  Kdetv* ktv, QObject *parent, const char *name)
 
466
    : KToggleAction(text, accel, parent, name),
 
467
      _mode(mode),
 
468
      _ktv(ktv)
 
469
{
 
470
    connect(this, SIGNAL( activated() ),
 
471
            this, SLOT( slotActivated() ));
 
472
}
 
473
 
 
474
AudioModeAction::~AudioModeAction()
 
475
{
 
476
}
 
477
 
 
478
void AudioModeAction::slotActivated()
 
479
{
 
480
    _ktv->setAudioMode(_mode);
 
481
}