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

« back to all changes in this revision

Viewing changes to kdetv/libkdetv/channeleditor.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 George Staikos <staikos@kde.org>
 
4
 * Copyright (C) 2002 Rich Moore <rich@kde.org>
 
5
 *               2004 Dirk Ziegelmeier <dziegel@gmx.de>
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public License
 
18
 * along with this library; see the file COPYING.LIB.  If not, write to
 
19
 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
 
20
 * Boston, MA 02110-1301, USA.
 
21
 */
 
22
 
 
23
#include <qobject.h>
 
24
#include <qcheckbox.h>
 
25
#include <qlcdnumber.h>
 
26
#include <qlabel.h>
 
27
#include <qlineedit.h>
 
28
#include <qcombobox.h>
 
29
 
 
30
#include <klocale.h>
 
31
#include <kdebug.h>
 
32
#include <kpushbutton.h>
 
33
#include <knuminput.h>
 
34
#include <kapplication.h>
 
35
#include <kiconloader.h>
 
36
#include <kpopupmenu.h>
 
37
 
 
38
#include "channel.h"
 
39
#include "channelstore.h"
 
40
#include "channelpropertiesdialogimpl.h"
 
41
#include "sourcemanager.h"
 
42
#include "channeleditor.h"
 
43
#include "channeleditor.moc"
 
44
 
 
45
 
 
46
#define CLI_RTTI 589425
 
47
 
 
48
int ChannelListItem::rtti() const
 
49
{
 
50
    return CLI_RTTI;
 
51
}
 
52
 
 
53
 
 
54
ChannelListItem::ChannelListItem(KListView* parent, Channel* ch, bool sso)
 
55
    : KListViewItem(parent, QString::number(ch->number()), ch->name()),
 
56
      c(ch), _showSelectedOnly(sso)
 
57
{
 
58
    if ( (!c->enabled()) && (_showSelectedOnly) )
 
59
        setVisible(false);
 
60
 
 
61
    connect(c, SIGNAL(changed()), this, SLOT(updateFields()));
 
62
}
 
63
 
 
64
 
 
65
ChannelListItem::~ChannelListItem()
 
66
{
 
67
}
 
68
 
 
69
 
 
70
void ChannelListItem::updateFields()
 
71
{
 
72
    if ( (!c->enabled()) && (_showSelectedOnly) )
 
73
        setVisible(false);
 
74
    else
 
75
        setVisible(true);
 
76
 
 
77
    if (c->name() != text(1))
 
78
        setText(1, c->name());
 
79
 
 
80
    if (QString::number(c->number()) != text(0))
 
81
        setText(0, QString::number(c->number()));
 
82
}
 
83
 
 
84
 
 
85
int ChannelListItem::compare(QListViewItem *i, int col, bool ascending) const
 
86
{
 
87
    int me, him;
 
88
 
 
89
    if (i->rtti() != CLI_RTTI)
 
90
        return KListViewItem::compare(i, col, ascending);
 
91
 
 
92
    switch (col) {
 
93
    case 0:
 
94
        me = c->number();
 
95
        him = static_cast<ChannelListItem*>(i)->c->number();
 
96
 
 
97
        if (me == him) return 0;
 
98
 
 
99
        return (ascending ? 1 : -1) * (me > him ? 1 : -1);
 
100
        break;
 
101
    default:
 
102
        break;
 
103
    }
 
104
 
 
105
    return KListViewItem::compare(i, col, ascending);
 
106
}
 
107
 
 
108
 
 
109
ChannelEditor::ChannelEditor( QWidget *parent, SourceManager* srcm, const char *name, bool showSelectedOnly )
 
110
    : KListView( parent, name ? name : "channel_editor" ),
 
111
      cs(0),
 
112
      _srcm(srcm),
 
113
      _showSelectedOnly(showSelectedOnly),
 
114
      prev(0)
 
115
{
 
116
    addColumn( "", -10 );
 
117
    addColumn( i18n("Name") );
 
118
 
 
119
    setResizeMode( LastColumn );
 
120
    setAllColumnsShowFocus( true );
 
121
    setColumnAlignment( 0, AlignCenter );
 
122
    setColumnAlignment( 1, AlignLeft );
 
123
 
 
124
    setItemsRenameable( true );
 
125
    setRenameable( 0, false );
 
126
    setRenameable( 1, true );
 
127
 
 
128
    _pop = new KPopupMenu(this, "ChannelEditorPopup");
 
129
    _titleId = _pop->insertTitle(i18n("Channel"));
 
130
    _hpId    = _pop->insertItem(KGlobal::iconLoader()->loadIcon("konqueror",
 
131
                                                                KIcon::NoGroup,
 
132
                                                                KIcon::SizeSmall),
 
133
                                i18n("Open Homepage"), this, SLOT( browseItem() ));
 
134
    _pop->insertItem(KGlobal::iconLoader()->loadIcon("edit",
 
135
                                                    KIcon::NoGroup,
 
136
                                                    KIcon::SizeSmall),
 
137
                     i18n("Edit..."), this, SLOT( editItem() ));
 
138
 
 
139
    connect( this, SIGNAL( itemRenamed(QListViewItem *, const QString &, int) ),
 
140
             this, SLOT( renameItem(QListViewItem *, const QString &, int) ) );
 
141
    connect( this, SIGNAL( selectionChanged(QListViewItem*) ),
 
142
             this, SLOT( requestChange(QListViewItem*) ) );
 
143
    connect( this, SIGNAL( doubleClicked(QListViewItem*, const QPoint&, int) ),
 
144
             this, SLOT( browseItem() ));
 
145
    connect( this, SIGNAL( contextMenu(KListView*, QListViewItem*, const QPoint&) ),
 
146
             this, SLOT( showContextMenu(KListView*, QListViewItem*, const QPoint&) ));
 
147
}
 
148
 
 
149
ChannelEditor::~ChannelEditor()
 
150
{
 
151
}
 
152
 
 
153
void ChannelEditor::setChannels( ChannelStore *chans )
 
154
{
 
155
    clear();
 
156
    cs = chans;
 
157
    createItems();
 
158
    connect(cs, SIGNAL(channelAdded(Channel*)), this, SLOT(reloadChannels()));
 
159
    connect(cs, SIGNAL(channelRemoved(Channel*)), this, SLOT(reloadChannels()));
 
160
    connect(cs, SIGNAL(aboutToReload()), this, SLOT(storeCurrentChannel()));
 
161
    connect(cs, SIGNAL(loaded()), this, SLOT(reloadChannels()));
 
162
}
 
163
 
 
164
 
 
165
void ChannelEditor::createItems()
 
166
{
 
167
    if ( !cs )
 
168
        return;
 
169
 
 
170
    for ( uint i = 0 ; i < cs->count(); i++ ) {
 
171
        Channel *c = cs->channelAt( i );
 
172
        if ( c )
 
173
            (void) new ChannelListItem( this, c, _showSelectedOnly );
 
174
    }
 
175
}
 
176
 
 
177
void ChannelEditor::showContextMenu(KListView*, QListViewItem*, const QPoint& p)
 
178
{
 
179
    QListViewItem *item = currentItem();
 
180
 
 
181
    if (!item || item->rtti() != CLI_RTTI)
 
182
        return;
 
183
 
 
184
    ChannelListItem *l = static_cast<ChannelListItem*>(item);
 
185
 
 
186
    _pop->changeTitle(_titleId, l->c->name());
 
187
    _pop->setItemEnabled(_hpId, !l->c->url().isEmpty());
 
188
    _pop->popup(p);
 
189
}
 
190
 
 
191
void ChannelEditor::browseItem()
 
192
{
 
193
    QListViewItem *item = currentItem();
 
194
 
 
195
    if (!item || item->rtti() != CLI_RTTI)
 
196
        return;
 
197
 
 
198
    ChannelListItem *l = static_cast<ChannelListItem*>(item);
 
199
 
 
200
    if(!l->c->url().isEmpty()) {
 
201
        kapp->invokeBrowser(l->c->url());
 
202
    }
 
203
}
 
204
 
 
205
void ChannelEditor::editItem()
 
206
{
 
207
    QListViewItem *item = currentItem();
 
208
 
 
209
    if (!item || item->rtti() != CLI_RTTI)
 
210
        return;
 
211
 
 
212
    ChannelListItem *l = static_cast<ChannelListItem*>(item);
 
213
 
 
214
    QDialog* d = _srcm->channelPropertiesDialog(l->c, this);
 
215
    connect(d, SIGNAL(accepted()),
 
216
            this, SLOT(slotPropertiesDlgFinished()));
 
217
    connect(d, SIGNAL(rejected()),
 
218
            this, SLOT(slotPropertiesDlgFinished()));
 
219
 
 
220
    d->show();
 
221
}
 
222
 
 
223
 
 
224
void ChannelEditor::slotPropertiesDlgFinished()
 
225
{
 
226
    QListViewItem *item = currentItem();
 
227
 
 
228
    if (!item || item->rtti() != CLI_RTTI)
 
229
        return;
 
230
 
 
231
    ChannelListItem *l = static_cast<ChannelListItem*>(item);
 
232
 
 
233
    emit channelSelected(l->c);
 
234
}
 
235
 
 
236
 
 
237
void ChannelEditor::renameItem( QListViewItem *item, const QString &str, int /*col*/ )
 
238
{
 
239
    if (!item || item->rtti() != CLI_RTTI)
 
240
        return;
 
241
 
 
242
    ChannelListItem *l = static_cast<ChannelListItem*>(item);
 
243
    l->c->setName( str );
 
244
    emit channelModified( l->c );
 
245
}
 
246
 
 
247
 
 
248
void ChannelEditor::requestChange(QListViewItem *item)
 
249
{
 
250
    if (!item || item->rtti() != CLI_RTTI)
 
251
        return;
 
252
 
 
253
    ChannelListItem *l = static_cast<ChannelListItem*>(item);
 
254
    emit channelSelected(l->c);
 
255
}
 
256
 
 
257
 
 
258
void ChannelEditor::ensureSelected(Channel *ch)
 
259
{
 
260
    ChannelListItem *x = static_cast<ChannelListItem*>(firstChild());
 
261
 
 
262
    while (x) {
 
263
        if (x->rtti() == CLI_RTTI && x->c == ch) {
 
264
            // Block signals here before selecting the current
 
265
            // channel inorder to avoid ::setChannel from being
 
266
            // invoked again...
 
267
            bool block = signalsBlocked();
 
268
            blockSignals (true);
 
269
            setCurrentItem(x);
 
270
            ensureItemVisible(x);
 
271
            setSelected(x, true);
 
272
            repaintItem(x);
 
273
            x->setSelected(true);
 
274
            blockSignals (block);
 
275
            //kdDebug() << "Ensuring item is selected: " << x->c->number() << endl;
 
276
            return;
 
277
        }
 
278
        x = static_cast<ChannelListItem *>(x->nextSibling());
 
279
    }
 
280
}
 
281
 
 
282
 
 
283
void ChannelEditor::reloadChannels()
 
284
{
 
285
    clear();
 
286
    createItems();
 
287
    //resize(columnWidth(0)+columnWidth(1)+8, height());
 
288
}
 
289
 
 
290
 
 
291
void ChannelEditor::slotSetShowSelectedOnly(bool sso)
 
292
{
 
293
    _showSelectedOnly = sso;
 
294
    storeCurrentChannel();
 
295
 
 
296
    // re-create items & restore selection
 
297
    restoreCurrentChannel();
 
298
    return;
 
299
} // showSelectedOnly
 
300
 
 
301
 
 
302
void ChannelEditor::storeCurrentChannel()
 
303
{
 
304
    ChannelListItem* l = static_cast<ChannelListItem*>(currentItem());
 
305
    prev = l ? l->c->number() : 0;
 
306
}
 
307
 
 
308
 
 
309
void ChannelEditor::restoreCurrentChannel()
 
310
{
 
311
    reloadChannels();
 
312
    if (prev != 0)
 
313
        ensureSelected(cs->channelNumber(prev));
 
314
}