You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
364 lines
8.9 KiB
364 lines
8.9 KiB
15 years ago
|
/* This file is part of the KDE libraries
|
||
|
Copyright (C) 2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License version 2, as published by the Free Software Foundation.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqdir.h>
|
||
|
#include <tqlistbox.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <kglobal.h>
|
||
|
#include <kiconloader.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kmimetype.h>
|
||
|
|
||
|
#include <kurlcombobox.h>
|
||
|
|
||
|
class KURLComboBox::KURLComboBoxPrivate
|
||
|
{
|
||
|
public:
|
||
|
KURLComboBoxPrivate() {
|
||
13 years ago
|
dirpix = SmallIcon(TQString::fromLatin1("folder"));
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQPixmap dirpix;
|
||
15 years ago
|
};
|
||
|
|
||
|
|
||
15 years ago
|
KURLComboBox::KURLComboBox( Mode mode, TQWidget *parent, const char *name )
|
||
15 years ago
|
: KComboBox( parent, name )
|
||
|
{
|
||
|
init( mode );
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
KURLComboBox::KURLComboBox( Mode mode, bool rw, TQWidget *parent,
|
||
15 years ago
|
const char *name )
|
||
|
: KComboBox( rw, parent, name )
|
||
|
{
|
||
|
init( mode );
|
||
|
}
|
||
|
|
||
|
|
||
|
KURLComboBox::~KURLComboBox()
|
||
|
{
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
|
|
||
|
void KURLComboBox::init( Mode mode )
|
||
|
{
|
||
|
d = new KURLComboBoxPrivate();
|
||
|
|
||
|
myMode = mode;
|
||
|
urlAdded = false;
|
||
|
myMaximum = 10; // default
|
||
|
itemList.setAutoDelete( true );
|
||
|
defaultList.setAutoDelete( true );
|
||
|
setInsertionPolicy( NoInsertion );
|
||
|
setTrapReturnKey( true );
|
||
13 years ago
|
setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
|
||
15 years ago
|
|
||
13 years ago
|
opendirPix = SmallIcon(TQString::fromLatin1("folder_open"));
|
||
15 years ago
|
|
||
15 years ago
|
connect( this, TQT_SIGNAL( activated( int )), TQT_SLOT( slotActivated( int )));
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQStringList KURLComboBox::urls() const
|
||
15 years ago
|
{
|
||
|
kdDebug(250) << "::urls()" << endl;
|
||
12 years ago
|
//static const TQString &fileProt = TDEGlobal::staticQString("file:");
|
||
15 years ago
|
TQStringList list;
|
||
|
TQString url;
|
||
15 years ago
|
for ( int i = defaultList.count(); i < count(); i++ ) {
|
||
|
url = text( i );
|
||
|
if ( !url.isEmpty() ) {
|
||
13 years ago
|
//if ( url.at(0) == '/' )
|
||
15 years ago
|
// list.append( url.prepend( fileProt ) );
|
||
|
//else
|
||
|
list.append( url );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
void KURLComboBox::addDefaultURL( const KURL& url, const TQString& text )
|
||
15 years ago
|
{
|
||
|
addDefaultURL( url, getPixmap( url ), text );
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
void KURLComboBox::addDefaultURL( const KURL& url, const TQPixmap& pix,
|
||
|
const TQString& text )
|
||
15 years ago
|
{
|
||
|
KURLComboItem *item = new KURLComboItem;
|
||
|
item->url = url;
|
||
|
item->pixmap = pix;
|
||
|
if ( text.isEmpty() )
|
||
|
if ( url.isLocalFile() )
|
||
|
item->text = url.path( myMode );
|
||
|
else
|
||
|
item->text = url.prettyURL( myMode );
|
||
|
else
|
||
|
item->text = text;
|
||
|
|
||
|
defaultList.append( item );
|
||
|
}
|
||
|
|
||
|
|
||
|
void KURLComboBox::setDefaults()
|
||
|
{
|
||
|
clear();
|
||
|
itemMapper.clear();
|
||
|
|
||
|
KURLComboItem *item;
|
||
|
for ( unsigned int id = 0; id < defaultList.count(); id++ ) {
|
||
13 years ago
|
item = defaultList.at( id );
|
||
15 years ago
|
insertURLItem( item );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KURLComboBox::setURLs( TQStringList urls )
|
||
15 years ago
|
{
|
||
|
setURLs( urls, RemoveBottom );
|
||
|
}
|
||
|
|
||
15 years ago
|
void KURLComboBox::setURLs( TQStringList urls, OverLoadResolving remove )
|
||
15 years ago
|
{
|
||
|
setDefaults();
|
||
|
itemList.clear();
|
||
|
|
||
|
if ( urls.isEmpty() )
|
||
|
return;
|
||
|
|
||
15 years ago
|
TQStringList::Iterator it = urls.begin();
|
||
15 years ago
|
|
||
|
// kill duplicates
|
||
15 years ago
|
TQString text;
|
||
15 years ago
|
while ( it != urls.end() ) {
|
||
13 years ago
|
while ( urls.contains( *it ) > 1 ) {
|
||
15 years ago
|
it = urls.remove( it );
|
||
|
continue;
|
||
|
}
|
||
|
++it;
|
||
|
}
|
||
|
|
||
|
// limit to myMaximum items
|
||
|
/* Note: overload is an (old) C++ keyword, some compilers (KCC) choke
|
||
|
on that, so call it Overload (capital 'O'). (matz) */
|
||
|
int Overload = urls.count() - myMaximum + defaultList.count();
|
||
|
while ( Overload > 0 ) {
|
||
|
urls.remove((remove == RemoveBottom) ? urls.fromLast() : urls.begin());
|
||
|
Overload--;
|
||
|
}
|
||
|
|
||
|
it = urls.begin();
|
||
|
|
||
|
KURLComboItem *item = 0L;
|
||
|
KURL u;
|
||
|
|
||
|
while ( it != urls.end() ) {
|
||
|
if ( (*it).isEmpty() ) {
|
||
|
++it;
|
||
|
continue;
|
||
|
}
|
||
|
u = KURL::fromPathOrURL( *it );
|
||
|
|
||
|
// Don't restore if file doesn't exist anymore
|
||
15 years ago
|
if (u.isLocalFile() && !TQFile::exists(u.path())) {
|
||
15 years ago
|
++it;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
item = new KURLComboItem;
|
||
|
item->url = u;
|
||
|
item->pixmap = getPixmap( u );
|
||
|
|
||
|
if ( u.isLocalFile() )
|
||
|
item->text = u.path( myMode ); // don't show file:/
|
||
|
else
|
||
|
item->text = *it;
|
||
|
|
||
|
insertURLItem( item );
|
||
|
itemList.append( item );
|
||
|
++it;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void KURLComboBox::setURL( const KURL& url )
|
||
|
{
|
||
|
if ( url.isEmpty() )
|
||
|
return;
|
||
|
|
||
|
blockSignals( true );
|
||
|
|
||
|
// check for duplicates
|
||
15 years ago
|
TQMap<int,const KURLComboItem*>::ConstIterator mit = itemMapper.begin();
|
||
|
TQString urlToInsert = url.url(-1);
|
||
15 years ago
|
while ( mit != itemMapper.end() ) {
|
||
|
if ( urlToInsert == mit.data()->url.url(-1) ) {
|
||
|
setCurrentItem( mit.key() );
|
||
|
|
||
|
if ( myMode == Directories )
|
||
|
updateItem( mit.data(), mit.key(), opendirPix );
|
||
|
|
||
|
blockSignals( false );
|
||
|
return;
|
||
|
}
|
||
|
++mit;
|
||
|
}
|
||
|
|
||
|
// not in the combo yet -> create a new item and insert it
|
||
|
|
||
|
// first remove the old item
|
||
|
if ( urlAdded ) {
|
||
|
itemList.removeLast();
|
||
|
urlAdded = false;
|
||
|
}
|
||
|
|
||
|
setDefaults();
|
||
|
|
||
15 years ago
|
TQPtrListIterator<KURLComboItem> it( itemList );
|
||
15 years ago
|
for( ; it.current(); ++it )
|
||
|
insertURLItem( it.current() );
|
||
|
|
||
|
KURLComboItem *item = new KURLComboItem;
|
||
|
item->url = url;
|
||
|
item->pixmap = getPixmap( url );
|
||
|
if ( url.isLocalFile() )
|
||
|
item->text = url.path( myMode );
|
||
|
else
|
||
|
item->text = url.prettyURL( myMode );
|
||
|
kdDebug(250) << "setURL: text=" << item->text << endl;
|
||
|
|
||
|
int id = count();
|
||
15 years ago
|
TQString text = /*isEditable() ? item->url.prettyURL( myMode ) : */ item->text;
|
||
15 years ago
|
|
||
|
if ( myMode == Directories )
|
||
|
KComboBox::insertItem( opendirPix, text, id );
|
||
|
else
|
||
|
KComboBox::insertItem( item->pixmap, text, id );
|
||
|
itemMapper.insert( id, item );
|
||
|
itemList.append( item );
|
||
|
|
||
|
setCurrentItem( id );
|
||
|
urlAdded = true;
|
||
|
blockSignals( false );
|
||
|
}
|
||
|
|
||
|
|
||
|
void KURLComboBox::slotActivated( int index )
|
||
|
{
|
||
|
const KURLComboItem *item = itemMapper[ index ];
|
||
|
|
||
|
if ( item ) {
|
||
|
setURL( item->url );
|
||
|
emit urlActivated( item->url );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void KURLComboBox::insertURLItem( const KURLComboItem *item )
|
||
|
{
|
||
|
// kdDebug(250) << "insertURLItem " << item->text << endl;
|
||
|
int id = count();
|
||
|
KComboBox::insertItem( item->pixmap, item->text, id );
|
||
|
itemMapper.insert( id, item );
|
||
|
}
|
||
|
|
||
|
|
||
|
void KURLComboBox::setMaxItems( int max )
|
||
|
{
|
||
|
myMaximum = max;
|
||
|
|
||
|
if ( count() > myMaximum ) {
|
||
|
int oldCurrent = currentItem();
|
||
|
|
||
|
setDefaults();
|
||
|
|
||
15 years ago
|
TQPtrListIterator<KURLComboItem> it( itemList );
|
||
15 years ago
|
int Overload = itemList.count() - myMaximum + defaultList.count();
|
||
|
for ( int i = 0; i <= Overload; i++ )
|
||
|
++it;
|
||
|
|
||
|
for( ; it.current(); ++it )
|
||
|
insertURLItem( it.current() );
|
||
|
|
||
|
if ( count() > 0 ) { // restore the previous currentItem
|
||
|
if ( oldCurrent >= count() )
|
||
|
oldCurrent = count() -1;
|
||
|
setCurrentItem( oldCurrent );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void KURLComboBox::removeURL( const KURL& url, bool checkDefaultURLs )
|
||
|
{
|
||
15 years ago
|
TQMap<int,const KURLComboItem*>::ConstIterator mit = itemMapper.begin();
|
||
15 years ago
|
while ( mit != itemMapper.end() ) {
|
||
|
if ( url.url(-1) == mit.data()->url.url(-1) ) {
|
||
|
if ( !itemList.remove( mit.data() ) && checkDefaultURLs )
|
||
|
defaultList.remove( mit.data() );
|
||
|
}
|
||
|
++mit;
|
||
|
}
|
||
|
|
||
|
blockSignals( true );
|
||
|
setDefaults();
|
||
15 years ago
|
TQPtrListIterator<KURLComboItem> it( itemList );
|
||
15 years ago
|
while ( it.current() ) {
|
||
|
insertURLItem( *it );
|
||
|
++it;
|
||
|
}
|
||
|
blockSignals( false );
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQPixmap KURLComboBox::getPixmap( const KURL& url ) const
|
||
15 years ago
|
{
|
||
|
if ( myMode == Directories )
|
||
|
return d->dirpix;
|
||
|
else
|
||
|
return KMimeType::pixmapForURL( url, 0, KIcon::Small );
|
||
|
}
|
||
|
|
||
|
|
||
|
// updates "item" with pixmap "pixmap" and sets the URL instead of text
|
||
|
// works around a Qt bug.
|
||
|
void KURLComboBox::updateItem( const KURLComboItem *item,
|
||
15 years ago
|
int index, const TQPixmap& pixmap )
|
||
15 years ago
|
{
|
||
15 years ago
|
// TQComboBox::changeItem() doesn't honor the pixmap when
|
||
15 years ago
|
// using an editable combobox, so we just remove and insert
|
||
|
if ( editable() ) {
|
||
|
removeItem( index );
|
||
|
insertItem( pixmap,
|
||
|
item->url.isLocalFile() ? item->url.path( myMode ) :
|
||
|
item->url.prettyURL( myMode ),
|
||
|
index );
|
||
|
}
|
||
|
else
|
||
|
changeItem( pixmap, item->text, index );
|
||
|
}
|
||
|
|
||
|
|
||
|
#include "kurlcombobox.moc"
|