2
channellist.cpp - IRC Channel Search Widget
4
Copyright (c) 2004 by Jason Keirstead <jason@keirstead.org>
5
Copyright (c) 2004-2007 by Michel Hermier <michel.hermier@gmail.com>
7
Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
9
*************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
*************************************************************************
19
#include "channellist.h"
21
#include "kircclient.h"
24
#include <kmessagebox.h>
29
#include <qapplication.h>
31
#include <qlineedit.h>
32
#include <qpushbutton.h>
34
#include <Q3HBoxLayout>
36
#include <Q3VBoxLayout>
37
#include <k3listview.h>
47
class ChannelListItem : public K3ListViewItem
50
ChannelListItem( K3ListView *parent, QString arg1, QString arg2, QString arg3 );
51
virtual int compare( Q3ListViewItem *i, int col, bool ascending ) const;
52
//virtual void paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int align );
55
K3ListView *parentList;
58
ChannelListItem::ChannelListItem( K3ListView *parent, QString arg1, QString arg2, QString arg3 ) :
59
K3ListViewItem( parent, parent->lastItem() ), parentList( parent )
66
int ChannelListItem::compare( Q3ListViewItem *i, int col, bool ascending ) const
70
if( text(1).toUInt() < i->text(1).toUInt() )
72
else if ( text(1).toUInt() == i->text(1).toUInt() )
78
return Q3ListViewItem::compare( i, col, ascending );
82
void ChannelListItem::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int align )
84
QPixmap back( width, height() );
85
QPainter paint( &back );
86
//K3ListViewItem::paintCell( &paint, cg, column, width, align );
87
// PASTED FROM KLISTVIEWITEM:
88
// set the alternate cell background colour if necessary
91
if (listView()->viewport()->backgroundMode()==Qt::FixedColor)
92
_cg.setColor(QColorGroup::Background, static_cast< K3ListView* >(listView())->alternateBackground());
94
_cg.setColor(QColorGroup::Base, static_cast< K3ListView* >(listView())->alternateBackground());
95
// PASTED FROM QLISTVIEWITEM
99
Q3ListView *lv = listView();
102
QFontMetrics fm( p->fontMetrics() );
104
// any text we render is done by the Components, not by this class, so make sure we've nothing to write
107
// removed text truncating code from Qt - we do that differently, further on
109
int marg = lv->itemMargin();
111
// const QPixmap * icon = pixmap( column );
113
const BackgroundMode bgmode = lv->viewport()->backgroundMode();
114
const QColorGroup::ColorRole crole = QPalette::backgroundRoleFromMode( bgmode );
116
if ( _cg.brush( crole ) != lv->colorGroup().brush( crole ) )
117
p->fillRect( 0, 0, width, height(), _cg.brush( crole ) );
120
// all copied from QListView::paintEmptyArea
122
//lv->paintEmptyArea( p, QRect( 0, 0, width, height() ) );
123
QStyleOption opt( lv->sortColumn(), 0 ); // ### hack; in 3.1, add a property in QListView and QHeader
124
QStyle::SFlags how = QStyle::Style_Default;
125
if ( lv->isEnabled() )
126
how |= QStyle::Style_Enabled;
128
lv->style().drawComplexControl( QStyle::CC_ListView,
129
p, lv, QRect( 0, 0, width, height() ), lv->colorGroup(),
130
how, QStyle::SC_ListView, QStyle::SC_None,
137
(column == 0 || lv->allColumnsShowFocus()) ) {
138
p->fillRect( r - marg, 0, width - r + marg, height(),
139
_cg.brush( QColorGroup::Highlight ) );
140
// removed text pen setting code from Qt
143
// removed icon drawing code from Qt
145
// draw the tree gubbins
146
if ( multiLinesEnabled() && column == 0 && isOpen() && childCount() ) {
147
int textheight = fm.size( align, t ).height() + 2 * lv->itemMargin();
148
textheight = qMax( textheight, QApplication::globalStrut().height() );
149
if ( textheight % 2 > 0 )
151
if ( textheight < height() ) {
152
int w = lv->treeStepSize() / 2;
153
lv->style().drawComplexControl( QStyle::CC_ListView, p, lv,
154
QRect( 0, textheight, w + 1, height() - textheight + 1 ), _cg,
155
lv->isEnabled() ? QStyle::Style_Enabled : QStyle::Style_Default,
156
QStyle::SC_ListViewExpand,
157
(uint)QStyle::SC_All, QStyleOption( this ) );
164
//do you see a better way to tell the TextComponent we are selected ? - Olivier 2004-09-02
166
_cg.setColor(QColorGroup::Text , _cg.highlightedText() );
168
Q3SimpleRichText myrichtext( text(column), paint.font() );
169
myrichtext.draw( &paint, 0, 0, paint.window(), _cg );
172
p->drawPixmap( 0, 0, back );
176
ChannelList::ChannelList( QWidget* parent, KIRC::Client *client )
177
: QWidget( parent ), m_client( client )
179
ChannelListLayout = new Q3VBoxLayout( this, 11, 6, "ChannelListLayout");
181
layout72_2 = new Q3HBoxLayout( 0, 0, 6, "layout72_2");
183
textLabel1_2 = new QLabel( this, "textLabel1_2" );
184
layout72_2->addWidget( textLabel1_2 );
186
channelSearch = new QLineEdit( this, "channelSearch" );
187
layout72_2->addWidget( channelSearch );
189
numUsers = new QSpinBox( 0, 32767, 1, this, "num_users" );
190
numUsers->setSuffix( i18n(" members") );
191
layout72_2->addWidget( numUsers );
193
mSearchButton = new QPushButton( this, "mSearchButton" );
194
layout72_2->addWidget( mSearchButton );
195
ChannelListLayout->addLayout( layout72_2 );
197
mChannelList = new K3ListView( this );
198
mChannelList->addColumn( i18n( "Channel" ) );
199
mChannelList->addColumn( i18n( "Users" ) );
200
//mChannelList->header()->setResizeEnabled( false, mChannelList->header()->count() - 1 );
201
mChannelList->addColumn( i18n( "Topic" ) );
202
mChannelList->setAllColumnsShowFocus( true );
203
mChannelList->setShowSortIndicator( true );
205
ChannelListLayout->addWidget( mChannelList );
207
//clearWState( WState_Polished );
209
textLabel1_2->setText( i18n( "Search for:" ) );
210
textLabel1_2->setToolTip( i18n( "You may search for channels on the IRC server, using a text string entered here." ) );
211
numUsers->setToolTip( i18n( "Channels returned must have at least this many members." ) );
212
numUsers->setWhatsThis( i18n( "Channels returned must have at least this many members." ) );
213
textLabel1_2->setWhatsThis( i18n( "You may search for channels on the IRC server, using a text string entered here. For instance, you may type 'linux' to find channels that have something to do with Linux." ) );
214
channelSearch->setToolTip( i18n( "You may search for channels on the IRC server for a text string entered here." ) );
215
channelSearch->setWhatsThis( i18n( "You may search for channels on the IRC server, using a text string entered here. For instance, you may type 'linux' to find channels that have something to do with Linux." ) );
216
mSearchButton->setText( i18n( "S&earch" ) );
217
mSearchButton->setToolTip( i18n( "Perform a channel search." ) );
218
mSearchButton->setWhatsThis( i18n( "Perform a channel search. Please be patient, as this can be slow, depending on the total number of channels on the server." ) );
220
mChannelList->setToolTip( i18n( "Double click on a channel to select it." ) );
221
mChannelList->header()->setLabel( 0, i18n( "Channel" ) );
222
mChannelList->header()->setLabel( 1, i18n( "Users" ) );
223
mChannelList->header()->setLabel( 2, i18n( "Topic" ) );
225
// signals and slots connections
226
connect( mChannelList, SIGNAL(doubleClicked(Q3ListViewItem*)),
227
this, SLOT(slotItemDoubleClicked(Q3ListViewItem*)) );
229
connect( mSearchButton, SIGNAL(clicked()), this, SLOT(search()) );
231
connect( mChannelList, SIGNAL(selectionChanged(Q3ListViewItem*)), this,
232
SLOT(slotItemSelected(Q3ListViewItem*)) );
235
connect( m_engine, SIGNAL(incomingListedChan(QString,uint,QString)),
236
this, SLOT(slotChannelListed(QString,uint,QString)) );
238
connect( m_engine, SIGNAL(incomingEndOfList()), this, SLOT(slotListEnd()) );
240
connect( m_engine, SIGNAL(connectedToServer()), this, SLOT(reset()) );
241
connect( m_engine, SIGNAL(disconnected()), this, SLOT(slotDisconnected()) );
247
void ChannelList::slotItemDoubleClicked( Q3ListViewItem *i )
249
emit channelDoubleClicked( i->text(0) );
252
void ChannelList::slotItemSelected( Q3ListViewItem *i )
254
emit channelSelected( i->text(0) );
257
void ChannelList::reset()
259
channelCache.clear();
263
void ChannelList::clear()
265
mChannelList->clear();
266
channelSearch->clear();
267
channelSearch->setFocus();
270
void ChannelList::search()
274
if( m_client->connectionState() == KIRC::Socket::Open || !channelCache.isEmpty() )
276
mChannelList->clear();
277
mChannelList->setSorting( -1 );
278
mSearchButton->setEnabled(false);
279
mSearch = channelSearch->text();
281
mUsers = numUsers->value();
283
if( channelCache.isEmpty() ) {
289
cacheIterator = channelCache.begin();
295
KMessageBox::queuedMessageBox(
296
this, KMessageBox::Error,
297
i18n("You must be connected to the IRC server to perform a channel listing."),
298
i18n("Not Connected"), 0
303
void ChannelList::slotChannelListed( const QString &channel, uint users, const QString &topic )
305
checkSearchResult( channel, users, topic );
306
channelCache.insert( channel, QPair< uint, QString >( users, topic ) );
309
void ChannelList::checkSearchResult( const QString &channel, uint users, const QString &topic )
311
if( ( mUsers == 0 || mUsers <= users ) &&
312
( mSearch.isEmpty() || channel.contains( mSearch, false ) || topic.contains( mSearch, false ) )
315
//new ChannelListItem( mChannelList, channel, QString::number(users), topic );
319
void ChannelList::slotSearchCache()
321
if( cacheIterator != channelCache.end() )
323
checkSearchResult( cacheIterator.key(), cacheIterator.data().first, cacheIterator.data().second );
325
QTimer::singleShot( 0, this, SLOT(slotSearchCache()) );
333
void ChannelList::slotDisconnected()
335
KMessageBox::queuedMessageBox(
336
this, KMessageBox::Error, i18n("You have been disconnected from the IRC server."),
337
i18n("Disconnected"), 0
343
void ChannelList::slotListEnd()
345
mChannelList->setSorting(0, true);
346
mSearchButton->setEnabled(true);
350
#include "channellist.moc"