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

« back to all changes in this revision

Viewing changes to kdetv/plugins/osd/elegant/elegant.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 library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library 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.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public License
 
16
 * along with this library; see the file COPYING.LIB.  If not, write to
 
17
 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
 
18
 * Boston, MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
 
 
22
#include <qcheckbox.h>
 
23
#include <qfont.h>
 
24
#include <qpalette.h>
 
25
#include <qpoint.h>
 
26
#include <qslider.h>
 
27
#include <qspinbox.h>
 
28
#include <qtimer.h>
 
29
#include <qwidget.h>
 
30
 
 
31
#include <kconfig.h>
 
32
#include <kdebug.h>
 
33
#include <kglobal.h>
 
34
#include <kiconloader.h>
 
35
#include <klocale.h>
 
36
#include <kpopupmenu.h>
 
37
 
 
38
#include "elegant.h"
 
39
#include "elegant.moc"
 
40
 
 
41
KdetvElegant::KdetvElegant(Kdetv *ktv, QWidget *p, const char *name)
 
42
    : KdetvOSDPlugin( ktv, "elegant-osd", p, name ),
 
43
      parent(p),
 
44
      align(AlignBottom|AlignCenter), indent(20),
 
45
      iconSize(KIcon::SizeLarge), fontSize(48),
 
46
      fontBold(true), fontItalic(false), fontUnderline(false), autoFontSize(false)
 
47
{
 
48
    timer = new QTimer( this );
 
49
    popup = new KPopupTitle( p, "display" );
 
50
    popup->hide();
 
51
 
 
52
    // FIXME: Don't use system colors, they may not work or look bad as OSD
 
53
    popup->setPaletteBackgroundColor( QColor( 200, 200, 200 ) );
 
54
 
 
55
    readConfig();
 
56
    applySettings();
 
57
 
 
58
    connect( timer, SIGNAL( timeout() ), popup, SLOT( hide() ) );
 
59
    connect( parent, SIGNAL( resized(int,int) ), this, SLOT( viewResized(int,int) ) );
 
60
 
 
61
    viewResized(p->width(), p->height());
 
62
}
 
63
 
 
64
KdetvElegant::~KdetvElegant()
 
65
{
 
66
    kdDebug() << "Elegant: destroying plugin." << endl;
 
67
}
 
68
 
 
69
void KdetvElegant::applySettings()
 
70
{
 
71
    QFont font = popup->font();
 
72
    font.setPointSize( fontSize );
 
73
    font.setBold( fontBold );
 
74
    font.setItalic( fontItalic );
 
75
    font.setUnderline( fontUnderline );
 
76
    popup->setFont( font );
 
77
 
 
78
    updateGeometry();
 
79
}
 
80
 
 
81
void KdetvElegant::readConfig()
 
82
{
 
83
    kdDebug() << "Elegant: reading config" << endl;
 
84
    
 
85
    _cfg->setGroup( "Elegant OSD");
 
86
    timeout = _cfg->readNumEntry( "Timeout", 2000 );
 
87
    indent = _cfg->readNumEntry( "Indent", 20 );
 
88
    align = _cfg->readNumEntry( "Alignment", AlignBottom|AlignCenter );
 
89
    iconSize = _cfg->readNumEntry( "IconSize", KIcon::SizeLarge );
 
90
    fontSize = _cfg->readNumEntry( "FontSize", 48 );
 
91
    fontBold = _cfg->readBoolEntry( "FontBold", true );
 
92
    fontItalic = _cfg->readBoolEntry( "FontItalic", false );
 
93
    fontUnderline = _cfg->readBoolEntry( "FontUnderline", false );
 
94
    autoFontSize = _cfg->readBoolEntry( "AutoFontSize", true );
 
95
}
 
96
 
 
97
void KdetvElegant::saveConfig()
 
98
{
 
99
    applyConfigWidget();
 
100
 
 
101
    kdDebug() << "Elegant: writing config" << endl;
 
102
    
 
103
    _cfg->setGroup( "Elegant OSD");
 
104
    _cfg->writeEntry( "Timeout", timeout );
 
105
    _cfg->writeEntry( "Indent", indent );
 
106
    _cfg->writeEntry( "Alignment", align );
 
107
    _cfg->writeEntry( "IconSize", iconSize );
 
108
    _cfg->writeEntry( "FontSize", fontSize );
 
109
    _cfg->writeEntry( "FontBold", fontBold );
 
110
    _cfg->writeEntry( "FontItalic", fontItalic );
 
111
    _cfg->writeEntry( "FontUnderline", fontUnderline );
 
112
    _cfg->writeEntry( "AutoFontSize", autoFontSize );
 
113
    _cfg->sync();
 
114
}
 
115
 
 
116
void KdetvElegant::updateGeometry()
 
117
{
 
118
    popup->resize( popup->minimumSizeHint() );
 
119
 
 
120
    int x;
 
121
    if ( align & AlignLeft )
 
122
        x = indent;
 
123
    else if ( align & AlignRight )
 
124
        x = parent->width() - indent - popup->width();
 
125
    else
 
126
        x = parent->width()/2 - popup->width()/2;
 
127
 
 
128
    int y;
 
129
    if ( align & AlignTop )
 
130
        y = indent;
 
131
    else if ( align & AlignBottom )
 
132
        y = parent->height() - indent - popup->height();
 
133
    else
 
134
        y = parent->height()/2 - popup->height()/2;
 
135
 
 
136
    popup->move( QPoint(x,y) );
 
137
}
 
138
 
 
139
void KdetvElegant::clear()
 
140
{
 
141
    static QPixmap kwicon = KGlobal::iconLoader()->loadIcon("kdetv", KIcon::NoGroup, iconSize);
 
142
    popup->setTitle(i18n("Elegant OSD"), &kwicon);
 
143
}
 
144
 
 
145
void KdetvElegant::showPopup()
 
146
{
 
147
    if ( popup->isHidden() )
 
148
        popup->show();
 
149
    else {
 
150
        popup->update();
 
151
        popup->raise();
 
152
    }
 
153
 
 
154
    updateGeometry();
 
155
    timer->start(timeout, true);
 
156
}
 
157
 
 
158
void KdetvElegant::display( const QString &text, const QPixmap &icon )
 
159
{
 
160
    popup->setTitle(text, &icon);
 
161
    showPopup();
 
162
}
 
163
 
 
164
void KdetvElegant::display( const QString &text, const QString &icon )
 
165
{
 
166
    QString i = icon.isNull() ? QString("kdetv") : icon;
 
167
    display( text, KGlobal::iconLoader()->loadIcon( i, KIcon::NoGroup, iconSize ) );
 
168
}
 
169
 
 
170
void KdetvElegant::displayMisc( const QString &text )
 
171
{
 
172
    display( text, "kdetv" );
 
173
}
 
174
 
 
175
void KdetvElegant::displayChannel( int channel, const QString &name )
 
176
{
 
177
    display( i18n("%1 - %2").arg(channel).arg(name), "kdetv" );
 
178
}
 
179
 
 
180
void KdetvElegant::displayMuted( bool muted )
 
181
{
 
182
    if ( muted )
 
183
        display( i18n("Mute"), "player_mute" );
 
184
    else
 
185
        display( i18n("Unmute"), "player_volume_up" );
 
186
}
 
187
 
 
188
void KdetvElegant::displayVolume( int vol )
 
189
{
 
190
    display( i18n("Volume: %1%").arg(vol), "player_volume_up" );
 
191
}
 
192
 
 
193
void KdetvElegant::displayCC( const QString &text )
 
194
{
 
195
    if ( text.isEmpty() )
 
196
        popup->hide();
 
197
    else
 
198
        display( text );
 
199
}
 
200
 
 
201
void KdetvElegant::viewResized( int w, int /*h*/ )
 
202
{
 
203
    if (!autoFontSize)
 
204
        return;
 
205
    QFont font = popup->font();
 
206
    font.setPixelSize(w / 15);
 
207
    popup->setFont( font );
 
208
    kdDebug() << "elegant font resized to " << w / 15 << endl;
 
209
    if ( !popup->isHidden() ) {
 
210
        updateGeometry();
 
211
    }
 
212
}
 
213
 
 
214
ElegantConfigWidget *KdetvElegant::configWidget( QWidget *parent, const char *name )
 
215
{
 
216
    form = new ElegantConfigWidget( parent, name );
 
217
 
 
218
    if ( align & AlignLeft )
 
219
        form->HSlider->setValue(0);
 
220
    else if ( align & AlignRight )
 
221
        form->HSlider->setValue(2);
 
222
    else
 
223
        form->HSlider->setValue(1);
 
224
 
 
225
    if ( align & AlignTop )
 
226
        form->VSlider->setValue(0);
 
227
    else if ( align & AlignBottom )
 
228
        form->VSlider->setValue(2);
 
229
    else
 
230
        form->VSlider->setValue(1);
 
231
 
 
232
    form->TimeSpin->setValue( timeout );
 
233
    form->MarginSpin->setValue( indent );
 
234
    form->SizeSpin->setValue( fontSize );
 
235
    form->BoldCheck->setChecked( fontBold );
 
236
    form->ItalicCheck->setChecked( fontItalic );
 
237
    form->UnderlineCheck->setChecked( fontUnderline );
 
238
    form->AutoFontSizeCheck->setChecked( autoFontSize );
 
239
 
 
240
    return form;
 
241
}
 
242
 
 
243
void KdetvElegant::applyConfigWidget()
 
244
{
 
245
    timeout = form->TimeSpin->value();
 
246
    indent = form->MarginSpin->value();
 
247
    fontSize = form->SizeSpin->value();
 
248
    iconSize = form->SizeSpin->value();
 
249
    fontBold = form->BoldCheck->isChecked();
 
250
    fontItalic = form->ItalicCheck->isChecked();
 
251
    fontUnderline = form->UnderlineCheck->isChecked();
 
252
    autoFontSize = form->AutoFontSizeCheck->isChecked();
 
253
 
 
254
    switch ( form->HSlider->value() ) {
 
255
        case 0:
 
256
            align = AlignLeft;
 
257
            break;
 
258
        case 1:
 
259
            align = AlignHCenter;
 
260
            break;
 
261
        case 2:
 
262
        default:
 
263
            align = AlignRight;
 
264
            break;
 
265
    }
 
266
    switch ( form->VSlider->value() ) {
 
267
        case 0:
 
268
            align = align | AlignTop;
 
269
            break;
 
270
        case 1:
 
271
            align = align | AlignVCenter;
 
272
            break;
 
273
        case 2:
 
274
        default:
 
275
            align = align | AlignBottom;
 
276
            break;
 
277
    }
 
278
 
 
279
    applySettings();
 
280
}
 
281
 
 
282
 
 
283
void KdetvElegant::colourKeyChanged(QColor)
 
284
{
 
285
    // nothing in here, but this suppresses a kdetv complaint
 
286
    // about the colourKeyChanged slot missing in this plugin
 
287
}
 
288
 
 
289
extern "C" {
 
290
    KdetvElegant* create_elegant( Kdetv *ktv, QWidget *parent )
 
291
    {
 
292
        if ( !parent )
 
293
            return 0;
 
294
 
 
295
        return new KdetvElegant( ktv, parent );
 
296
    }
 
297
}
 
298
 
 
299
 
 
300
#ifdef INCLUDE_TESTS
 
301
 
 
302
static int test_chan_num = 0;
 
303
static int test_muted = false;
 
304
static int test_vol = 0;
 
305
 
 
306
void KdetvElegant::test_channel()
 
307
{
 
308
    displayChannel( test_chan_num, QString("Channel %1").arg(test_chan_num) );
 
309
    test_chan_num++;
 
310
}
 
311
 
 
312
void KdetvElegant::test_mute()
 
313
{
 
314
    displayMuted( test_muted );
 
315
    test_muted = !test_muted;
 
316
}
 
317
 
 
318
void KdetvElegant::test_volume()
 
319
{
 
320
    displayVolume( test_vol );
 
321
    test_vol = test_vol+10;
 
322
}
 
323
 
 
324
void KdetvElegant::test_config()
 
325
{
 
326
    QWidget *w = configWidget( 0, "conf" );
 
327
    w->show();
 
328
}
 
329
#endif // INCLUDE_TESTS