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>
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.
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.
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.
24
#include <qcheckbox.h>
25
#include <qlcdnumber.h>
27
#include <qlineedit.h>
28
#include <qcombobox.h>
32
#include <kpushbutton.h>
33
#include <knuminput.h>
34
#include <kapplication.h>
35
#include <kiconloader.h>
36
#include <kpopupmenu.h>
39
#include "channelstore.h"
40
#include "channelpropertiesdialogimpl.h"
41
#include "sourcemanager.h"
42
#include "channeleditor.h"
43
#include "channeleditor.moc"
46
#define CLI_RTTI 589425
48
int ChannelListItem::rtti() const
54
ChannelListItem::ChannelListItem(KListView* parent, Channel* ch, bool sso)
55
: KListViewItem(parent, QString::number(ch->number()), ch->name()),
56
c(ch), _showSelectedOnly(sso)
58
if ( (!c->enabled()) && (_showSelectedOnly) )
61
connect(c, SIGNAL(changed()), this, SLOT(updateFields()));
65
ChannelListItem::~ChannelListItem()
70
void ChannelListItem::updateFields()
72
if ( (!c->enabled()) && (_showSelectedOnly) )
77
if (c->name() != text(1))
78
setText(1, c->name());
80
if (QString::number(c->number()) != text(0))
81
setText(0, QString::number(c->number()));
85
int ChannelListItem::compare(QListViewItem *i, int col, bool ascending) const
89
if (i->rtti() != CLI_RTTI)
90
return KListViewItem::compare(i, col, ascending);
95
him = static_cast<ChannelListItem*>(i)->c->number();
97
if (me == him) return 0;
99
return (ascending ? 1 : -1) * (me > him ? 1 : -1);
105
return KListViewItem::compare(i, col, ascending);
109
ChannelEditor::ChannelEditor( QWidget *parent, SourceManager* srcm, const char *name, bool showSelectedOnly )
110
: KListView( parent, name ? name : "channel_editor" ),
113
_showSelectedOnly(showSelectedOnly),
116
addColumn( "", -10 );
117
addColumn( i18n("Name") );
119
setResizeMode( LastColumn );
120
setAllColumnsShowFocus( true );
121
setColumnAlignment( 0, AlignCenter );
122
setColumnAlignment( 1, AlignLeft );
124
setItemsRenameable( true );
125
setRenameable( 0, false );
126
setRenameable( 1, true );
128
_pop = new KPopupMenu(this, "ChannelEditorPopup");
129
_titleId = _pop->insertTitle(i18n("Channel"));
130
_hpId = _pop->insertItem(KGlobal::iconLoader()->loadIcon("konqueror",
133
i18n("Open Homepage"), this, SLOT( browseItem() ));
134
_pop->insertItem(KGlobal::iconLoader()->loadIcon("edit",
137
i18n("Edit..."), this, SLOT( editItem() ));
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&) ));
149
ChannelEditor::~ChannelEditor()
153
void ChannelEditor::setChannels( ChannelStore *chans )
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()));
165
void ChannelEditor::createItems()
170
for ( uint i = 0 ; i < cs->count(); i++ ) {
171
Channel *c = cs->channelAt( i );
173
(void) new ChannelListItem( this, c, _showSelectedOnly );
177
void ChannelEditor::showContextMenu(KListView*, QListViewItem*, const QPoint& p)
179
QListViewItem *item = currentItem();
181
if (!item || item->rtti() != CLI_RTTI)
184
ChannelListItem *l = static_cast<ChannelListItem*>(item);
186
_pop->changeTitle(_titleId, l->c->name());
187
_pop->setItemEnabled(_hpId, !l->c->url().isEmpty());
191
void ChannelEditor::browseItem()
193
QListViewItem *item = currentItem();
195
if (!item || item->rtti() != CLI_RTTI)
198
ChannelListItem *l = static_cast<ChannelListItem*>(item);
200
if(!l->c->url().isEmpty()) {
201
kapp->invokeBrowser(l->c->url());
205
void ChannelEditor::editItem()
207
QListViewItem *item = currentItem();
209
if (!item || item->rtti() != CLI_RTTI)
212
ChannelListItem *l = static_cast<ChannelListItem*>(item);
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()));
224
void ChannelEditor::slotPropertiesDlgFinished()
226
QListViewItem *item = currentItem();
228
if (!item || item->rtti() != CLI_RTTI)
231
ChannelListItem *l = static_cast<ChannelListItem*>(item);
233
emit channelSelected(l->c);
237
void ChannelEditor::renameItem( QListViewItem *item, const QString &str, int /*col*/ )
239
if (!item || item->rtti() != CLI_RTTI)
242
ChannelListItem *l = static_cast<ChannelListItem*>(item);
243
l->c->setName( str );
244
emit channelModified( l->c );
248
void ChannelEditor::requestChange(QListViewItem *item)
250
if (!item || item->rtti() != CLI_RTTI)
253
ChannelListItem *l = static_cast<ChannelListItem*>(item);
254
emit channelSelected(l->c);
258
void ChannelEditor::ensureSelected(Channel *ch)
260
ChannelListItem *x = static_cast<ChannelListItem*>(firstChild());
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
267
bool block = signalsBlocked();
270
ensureItemVisible(x);
271
setSelected(x, true);
273
x->setSelected(true);
274
blockSignals (block);
275
//kdDebug() << "Ensuring item is selected: " << x->c->number() << endl;
278
x = static_cast<ChannelListItem *>(x->nextSibling());
283
void ChannelEditor::reloadChannels()
287
//resize(columnWidth(0)+columnWidth(1)+8, height());
291
void ChannelEditor::slotSetShowSelectedOnly(bool sso)
293
_showSelectedOnly = sso;
294
storeCurrentChannel();
296
// re-create items & restore selection
297
restoreCurrentChannel();
299
} // showSelectedOnly
302
void ChannelEditor::storeCurrentChannel()
304
ChannelListItem* l = static_cast<ChannelListItem*>(currentItem());
305
prev = l ? l->c->number() : 0;
309
void ChannelEditor::restoreCurrentChannel()
313
ensureSelected(cs->channelNumber(prev));