kio Library API Documentation

kurlbar.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2001,2002 Carsten Pfeiffer <pfeiffer@kde.org>
00003 
00004     library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation, version 2.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016     Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <unistd.h>
00020 
00021 #include <qcheckbox.h>
00022 #include <qdrawutil.h>
00023 #include <qfontmetrics.h>
00024 #include <qlabel.h>
00025 #include <qgrid.h>
00026 #include <qpainter.h>
00027 #include <qpopupmenu.h>
00028 #include <qvbox.h>
00029 #include <qwhatsthis.h>
00030 
00031 #include <kaboutdata.h>
00032 #include <kconfig.h>
00033 #include <kglobal.h>
00034 #include <kicondialog.h>
00035 #include <kiconloader.h>
00036 #include <kinstance.h>
00037 #include <klineedit.h>
00038 #include <klocale.h>
00039 #include <kmimetype.h>
00040 #include <kprotocolinfo.h>
00041 #include <kurldrag.h>
00042 #include <kurlrequester.h>
00043 
00044 #include "kurlbar.h"
00045 
00050 class KURLBarToolTip : public QToolTip
00051 {
00052 public:
00053     KURLBarToolTip( QListBox *view ) : QToolTip( view ), m_view( view ) {}
00054 
00055 protected:
00056     virtual void maybeTip( const QPoint& point ) {
00057         QListBoxItem *item = m_view->itemAt( point );
00058         if ( item ) {
00059             QString text = static_cast<KURLBarItem*>( item )->toolTip();
00060             if ( !text.isEmpty() )
00061                 tip( m_view->itemRect( item ), text );
00062         }
00063     }
00064 
00065 private:
00066     QListBox *m_view;
00067 };
00068 
00069 
00072 
00073 
00074 KURLBarItem::KURLBarItem( KURLBar *parent,
00075                           const KURL& url, const QString& description,
00076                           const QString& icon, KIcon::Group group )
00077     : QListBoxPixmap( KIconLoader::unknown() /*, parent->listBox()*/ ),
00078       m_url( url ),
00079       m_pixmap( 0L ),
00080       m_parent( parent ),
00081       m_appLocal( true )
00082 {
00083     setCustomHighlighting( true );
00084     setDescription( description );
00085     setIcon( icon, group );
00086 }
00087 
00088 KURLBarItem::~KURLBarItem()
00089 {
00090 }
00091 
00092 void KURLBarItem::setURL( const KURL& url )
00093 {
00094     m_url = url;
00095     if ( m_description.isEmpty() )
00096         setText( url.fileName() );
00097 }
00098 
00099 void KURLBarItem::setIcon( const QString& icon, KIcon::Group group )
00100 {
00101     m_icon  = icon;
00102     m_group = group;
00103 
00104     if ( icon.isEmpty() )
00105         m_pixmap = KMimeType::pixmapForURL( m_url, 0, group, iconSize() );
00106     else
00107         m_pixmap = KGlobal::iconLoader()->loadIcon( icon, group, iconSize(),
00108                                                     KIcon::DefaultState );
00109 }
00110 
00111 void KURLBarItem::setDescription( const QString& desc )
00112 {
00113     m_description = desc;
00114     setText( desc.isEmpty() ? m_url.fileName() : desc );
00115 }
00116 
00117 void KURLBarItem::setToolTip( const QString& tip )
00118 {
00119     m_toolTip = tip;
00120 }
00121 
00122 QString KURLBarItem::toolTip() const
00123 {
00124     return m_toolTip.isEmpty() ? m_url.prettyURL() : m_toolTip;
00125 }
00126 
00127 int KURLBarItem::iconSize() const
00128 {
00129     return m_parent->iconSize();
00130 }
00131 
00132 void KURLBarItem::paint( QPainter *p )
00133 {
00134     QListBox *box = listBox();
00135     int w = width( box );
00136 
00137     if ( m_parent->iconSize() < KIcon::SizeMedium ) {
00138         // small icon -> draw icon next to text
00139 
00140         // ### mostly cut & paste of QListBoxPixmap::paint() until Qt 3.1
00141         // (where it will properly use pixmap() instead of the internal pixmap)
00142         const QPixmap *pm = pixmap();
00143         int yPos = QMAX( 0, (height(box) - pm->height())/2 );
00144 
00145         p->drawPixmap( 3, yPos, *pm );
00146         if ( !text().isEmpty() ) {
00147             QFontMetrics fm = p->fontMetrics();
00148             if ( pm->height() < fm.height() )
00149                 yPos = fm.ascent() + fm.leading()/2;
00150             else
00151                 yPos = pm->height()/2 - fm.height()/2 + fm.ascent();
00152             p->drawText( pm->width() + 5, yPos, text() );
00153         }
00154         // end cut & paste (modulo pixmap centering)
00155     }
00156 
00157     else {
00158         // big icons -> draw text below icon
00159         static const int margin = 3;
00160         int y = margin;
00161         const QPixmap *pm = pixmap();
00162 
00163         if ( !pm->isNull() ) {
00164             int x = (w - pm->width()) / 2;
00165             x = QMAX( x, margin );
00166             p->drawPixmap( x, y, *pm );
00167         }
00168 
00169         if ( !text().isEmpty() ) {
00170             QFontMetrics fm = p->fontMetrics();
00171             y += pm->height() + fm.height() - fm.descent();
00172             int x = (w - fm.width( text() )) / 2;
00173             x = QMAX( x, margin );
00174             p->drawText( x, y, text() );
00175         }
00176     }
00177 
00178     // draw sunken selection
00179     if ( isCurrent() || isSelected() ) {
00180         qDrawShadePanel( p, 1, 0, w -2, height(box),
00181                          box->colorGroup(), true, 1, 0L );
00182     }
00183 }
00184 
00185 QSize KURLBarItem::sizeHint() const
00186 {
00187     int wmin = 0;
00188     int hmin = 0;
00189     const KURLBarListBox *lb =static_cast<const KURLBarListBox*>(listBox());
00190 
00191     if ( m_parent->iconSize() < KIcon::SizeMedium ) {
00192         wmin = QListBoxPixmap::width( lb );
00193         hmin = QListBoxPixmap::height( lb );
00194     }
00195     else {
00196         wmin = QMAX(lb->fontMetrics().width(text()), pixmap()->width()) + 6;
00197         hmin = lb->fontMetrics().lineSpacing() + pixmap()->height() + 6;
00198     }
00199 
00200     if ( lb->isVertical() )
00201         wmin = QMAX( wmin, lb->viewport()->sizeHint().width() );
00202     else
00203         hmin = QMAX( hmin, lb->viewport()->sizeHint().height() );
00204 
00205     return QSize( wmin, hmin );
00206 }
00207 
00208 int KURLBarItem::width( const QListBox *lb ) const
00209 {
00210     if ( static_cast<const KURLBarListBox *>( lb )->isVertical() )
00211         return QMAX( sizeHint().width(), lb->viewport()->width() );
00212     else
00213         return sizeHint().width();
00214 }
00215 
00216 int KURLBarItem::height( const QListBox *lb ) const
00217 {
00218     if ( static_cast<const KURLBarListBox *>( lb )->isVertical() )
00219         return sizeHint().height();
00220     else
00221         return QMAX( sizeHint().height(), lb->viewport()->height() );
00222 }
00223 
00226 
00227 class KURLBar::KURLBarPrivate
00228 {
00229 public:
00230     KURLBarPrivate()
00231     {
00232         currentURL.setPath( QDir::homeDirPath() );
00233     }
00234 
00235     KURL currentURL;
00236 };
00237 
00238 
00239 KURLBar::KURLBar( bool useGlobalItems, QWidget *parent, const char *name, WFlags f )
00240     : QFrame( parent, name, f ),
00241       m_activeItem( 0L ),
00242       m_useGlobal( useGlobalItems ),
00243       m_isModified( false ),
00244       m_isImmutable( false ),
00245       m_listBox( 0L ),
00246       m_iconSize( KIcon::SizeMedium )
00247 {
00248     d = new KURLBarPrivate();
00249 
00250     setListBox( 0L );
00251     setSizePolicy( QSizePolicy( isVertical() ?
00252                                 QSizePolicy::Maximum :
00253                                 QSizePolicy::Preferred,
00254                                 isVertical() ?
00255                                 QSizePolicy::Preferred :
00256                                 QSizePolicy::Maximum ));
00257     QWhatsThis::add(this, i18n("<qt>The <b>Quick Access</b> panel provides easy access to commonly used file locations.<p>"
00258                                "Clicking on one of the shortcut entries will take you to that location.<p>"
00259                                "By right clicking on an entry you can add, edit and remove shortcuts.</qt>"));
00260 }
00261 
00262 KURLBar::~KURLBar()
00263 {
00264     delete d;
00265 }
00266 
00267 KURLBarItem * KURLBar::insertItem(const KURL& url, const QString& description,
00268                                   bool applicationLocal,
00269                                   const QString& icon, KIcon::Group group )
00270 {
00271     KURLBarItem *item = new KURLBarItem(this, url, description, icon, group);
00272     item->setApplicationLocal( applicationLocal );
00273     m_listBox->insertItem( item );
00274     return item;
00275 }
00276 
00277 void KURLBar::setOrientation( Qt::Orientation orient )
00278 {
00279     m_listBox->setOrientation( orient );
00280     setSizePolicy( QSizePolicy( isVertical() ?
00281                                 QSizePolicy::Maximum :
00282                                 QSizePolicy::Preferred,
00283                                 isVertical() ?
00284                                 QSizePolicy::Preferred :
00285                                 QSizePolicy::Maximum ));
00286 }
00287 
00288 Qt::Orientation KURLBar::orientation() const
00289 {
00290     return m_listBox->orientation();
00291 }
00292 
00293 void KURLBar::setListBox( KURLBarListBox *view )
00294 {
00295     delete m_listBox;
00296 
00297     if ( !view ) {
00298         m_listBox = new KURLBarListBox( this, "urlbar listbox" );
00299         setOrientation( Vertical );
00300     }
00301     else {
00302         m_listBox = view;
00303         if ( m_listBox->parentWidget() != this )
00304             m_listBox->reparent( this, QPoint(0,0) );
00305         m_listBox->resize( width(), height() );
00306     }
00307 
00308     m_listBox->setSelectionMode( KListBox::Single );
00309     QPalette pal = palette();
00310     QColor gray = pal.color( QPalette::Normal, QColorGroup::Mid );
00311     QColor selectedTextColor = pal.color( QPalette::Normal, QColorGroup::BrightText );
00312     pal.setColor( QPalette::Normal,   QColorGroup::Base, gray );
00313     pal.setColor( QPalette::Normal,   QColorGroup::HighlightedText, selectedTextColor );
00314     pal.setColor( QPalette::Inactive, QColorGroup::Base, gray );
00315     pal.setColor( QPalette::Inactive, QColorGroup::HighlightedText, selectedTextColor );
00316 
00317     setPalette( pal );
00318     m_listBox->viewport()->setBackgroundMode( PaletteMid );
00319 
00320     connect( m_listBox, SIGNAL( mouseButtonClicked( int, QListBoxItem *, const QPoint & ) ),
00321              SLOT( slotSelected( int, QListBoxItem * )));
00322     connect( m_listBox, SIGNAL( dropped( QDropEvent * )),
00323              this, SLOT( slotDropped( QDropEvent * )));
00324     connect( m_listBox, SIGNAL( contextMenuRequested( QListBoxItem *,
00325                                                       const QPoint& )),
00326              SLOT( slotContextMenuRequested( QListBoxItem *, const QPoint& )));
00327 }
00328 
00329 void KURLBar::setIconSize( int size )
00330 {
00331     if ( size == m_iconSize )
00332         return;
00333 
00334     m_iconSize = size;
00335 
00336     // reload the icons with the new size
00337     KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00338     while ( item ) {
00339         item->setIcon( item->icon(), item->iconGroup() );
00340         item = static_cast<KURLBarItem*>( item->next() );
00341     }
00342 
00343     resize( sizeHint() );
00344     updateGeometry();
00345 }
00346 
00347 void KURLBar::clear()
00348 {
00349     m_listBox->clear();
00350 }
00351 
00352 void KURLBar::resizeEvent( QResizeEvent *e )
00353 {
00354     QFrame::resizeEvent( e );
00355     m_listBox->resize( width(), height() );
00356 }
00357 
00358 QSize KURLBar::sizeHint() const
00359 {
00360     return m_listBox->sizeHint();
00361 
00362 #if 0
00363     // this code causes vertical and or horizontal scrollbars appearing
00364     // depending on the text, font, moonphase and earth rotation. Just using
00365     // m_listBox->sizeHint() fixes this (although the widget can then be 
00366     // resized to a smaller size so that scrollbars appear).
00367     int w = 0;
00368     int h = 0;
00369     KURLBarItem *item;
00370     bool vertical = isVertical();
00371 
00372     for ( item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00373           item;
00374           item = static_cast<KURLBarItem*>( item->next() ) ) {
00375 
00376         QSize sh = item->sizeHint();
00377 
00378         if ( vertical ) {
00379             w = QMAX( w, sh.width() );
00380             h += sh.height();
00381         }
00382         else {
00383             w += sh.width();
00384             h = QMAX( h, sh.height() );
00385         }
00386     }
00387 
00388 //     if ( vertical && m_listBox->verticalScrollBar()->isVisible() )
00389 //         w += m_listBox->verticalScrollBar()->width();
00390 //     else if ( !vertical && m_listBox->horizontalScrollBar()->isVisible() )
00391 //         h += m_listBox->horizontalScrollBar()->height();
00392 
00393     if ( w == 0 && h == 0 )
00394         return QSize( 100, 200 );
00395     else
00396         return QSize( 6 + w, h );
00397 #endif
00398 }
00399 
00400 QSize KURLBar::minimumSizeHint() const
00401 {
00402     QSize s = sizeHint(); // ###
00403     int w = s.width()  + m_listBox->verticalScrollBar()->width();
00404     int h = s.height() + m_listBox->horizontalScrollBar()->height();
00405     return QSize( w, h );
00406 }
00407 
00408 void KURLBar::slotSelected( int button, QListBoxItem *item )
00409 {
00410     if ( button != Qt::LeftButton )
00411         return;
00412 
00413     slotSelected( item );
00414 }
00415 
00416 void KURLBar::slotSelected( QListBoxItem *item )
00417 {
00418     if ( item && item != m_activeItem )
00419         m_activeItem = static_cast<KURLBarItem*>( item );
00420 
00421     if ( m_activeItem ) {
00422         m_listBox->setCurrentItem( m_activeItem );
00423         emit activated( m_activeItem->url() );
00424     }
00425 }
00426 
00427 void KURLBar::setCurrentItem( const KURL& url )
00428 {
00429     d->currentURL = url;
00430 
00431     QString u = url.url(-1);
00432 
00433     if ( m_activeItem && m_activeItem->url().url(-1) == u )
00434         return;
00435 
00436     bool hasURL = false;
00437     QListBoxItem *item = m_listBox->firstItem();
00438     while ( item ) {
00439         if ( static_cast<KURLBarItem*>( item )->url().url(-1) == u ) {
00440             m_activeItem = static_cast<KURLBarItem*>( item );
00441             m_listBox->setCurrentItem( item );
00442             m_listBox->setSelected( item, true );
00443             hasURL = true;
00444             break;
00445         }
00446         item = item->next();
00447     }
00448 
00449     if ( !hasURL ) {
00450         m_activeItem = 0L;
00451         m_listBox->clearSelection();
00452     }
00453 }
00454 
00455 KURLBarItem * KURLBar::currentItem() const
00456 {
00457     QListBoxItem *item = m_listBox->item( m_listBox->currentItem() );
00458     if ( item )
00459         return static_cast<KURLBarItem *>( item );
00460     return 0L;
00461 }
00462 
00463 KURL KURLBar::currentURL() const
00464 {
00465     KURLBarItem *item = currentItem();
00466     return item ? item->url() : KURL();
00467 }
00468 
00469 void KURLBar::readConfig( KConfig *appConfig, const QString& itemGroup )
00470 {
00471     m_isImmutable = appConfig->groupIsImmutable( itemGroup );
00472     KConfigGroupSaver cs( appConfig, itemGroup );
00473     m_iconSize = appConfig->readNumEntry( "Speedbar IconSize", m_iconSize );
00474 
00475     if ( m_useGlobal ) { // read global items
00476         KConfig *globalConfig = KGlobal::config();
00477         KConfigGroupSaver cs( globalConfig, (QString)(itemGroup +" (Global)"));
00478         int num = globalConfig->readNumEntry( "Number of Entries" );
00479         for ( int i = 0; i < num; i++ ) {
00480             readItem( i, globalConfig, false );
00481         }
00482     }
00483 
00484     // read application local items
00485     int num = appConfig->readNumEntry( "Number of Entries" );
00486     for ( int i = 0; i < num; i++ ) {
00487         readItem( i, appConfig, true );
00488     }
00489 }
00490 
00491 static KURL kurlbar_readURLEntry( KConfig *config, const QString& key )
00492 {
00493     // This looks a bit complex to just read a URL, but we have our reasons:
00494     // Until KDE 3.1.1, the url was written as prettyURL(), so it included the file:/
00495     // prefix. Now we use KConfig::writePathEntry( url.path() ) in case it's a local
00496     // file, so readEntry() may return e.g. either file:/foo, /foo or $HOME
00497 
00498     QString urlEntry = config->readEntry( key );
00499     bool isPath = !urlEntry.isEmpty() && (urlEntry[0] == '/' || urlEntry[0] == '$');
00500     // if it's local, try dollar expansion
00501     if ( !urlEntry.isEmpty() && (isPath || urlEntry.startsWith("file:/") ))
00502         urlEntry = config->readPathEntry( key );
00503     KURL url;
00504     if ( isPath )
00505         url.setPath( urlEntry );
00506     else
00507         url = urlEntry;
00508 
00509     return url;
00510 }
00511 
00512 void KURLBar::readItem( int i, KConfig *config, bool applicationLocal )
00513 {
00514     QString number = QString::number( i );
00515     KURL url = kurlbar_readURLEntry( config, QString("URL_") + number );
00516     if ( url.isMalformed() || !KProtocolInfo::isKnownProtocol( url ))
00517         return; // nothing we could do.
00518 
00519     insertItem( url,
00520                 config->readEntry( QString("Description_") + number ),
00521                 applicationLocal,
00522                 config->readEntry( QString("Icon_") + number ),
00523                 static_cast<KIcon::Group>(
00524                     config->readNumEntry( QString("IconGroup_") + number )) );
00525 }
00526 
00527 void KURLBar::writeConfig( KConfig *config, const QString& itemGroup )
00528 {
00529     KConfigGroupSaver cs1( config, itemGroup );
00530     config->writeEntry( "Speedbar IconSize", m_iconSize );
00531 
00532     if ( !m_isModified )
00533         return;
00534 
00535     int i = 0;
00536     int numLocal = 0;
00537     KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00538 
00539     while ( item ) {
00540         if ( item->applicationLocal() ) {
00541             writeItem( item, numLocal, config, false );
00542             numLocal++;
00543         }
00544 
00545         i++;
00546         item = static_cast<KURLBarItem*>( item->next() );
00547     }
00548     config->writeEntry("Number of Entries", numLocal);
00549 
00550 
00551     // write the global entries to kdeglobals, if any
00552     bool haveGlobalEntries = (i > numLocal);
00553     if ( m_useGlobal && haveGlobalEntries ) {
00554         config->setGroup( itemGroup + " (Global)" );
00555 
00556         int numGlobals = 0;
00557         item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00558 
00559         while ( item ) {
00560             if ( !item->applicationLocal() ) {
00561                 writeItem( item, numGlobals, config, true );
00562                 numGlobals++;
00563             }
00564 
00565             item = static_cast<KURLBarItem*>( item->next() );
00566         }
00567         config->writeEntry("Number of Entries", numGlobals, true, true);
00568     }
00569 
00570     m_isModified = false;
00571 }
00572 
00573 void KURLBar::writeItem( KURLBarItem *item, int i, KConfig *config,
00574                          bool global )
00575 {
00576     QString Description = "Description_";
00577     QString URL = "URL_";
00578     QString Icon = "Icon_";
00579     QString IconGroup = "IconGroup_";
00580 
00581     QString number = QString::number( i );
00582     if ( item->url().isLocalFile() )
00583         config->writePathEntry( URL + number, item->url().path(), true, global );
00584     else
00585     config->writeEntry( URL + number, item->url().prettyURL(), true, global );
00586 
00587     config->writeEntry( Description + number, item->description(),true,global);
00588     config->writeEntry( Icon + number, item->icon(), true, global );
00589     config->writeEntry( IconGroup + number, item->iconGroup(), true, global );
00590 }
00591 
00592 
00593 void KURLBar::slotDropped( QDropEvent *e )
00594 {
00595     KURL::List urls;
00596     if ( KURLDrag::decode( e, urls ) ) {
00597         KURL url;
00598         QString description;
00599         QString icon;
00600         bool appLocal = false;
00601 
00602         KURL::List::Iterator it = urls.begin();
00603         for ( ; it != urls.end(); ++it ) {
00604             url = *it;
00605             if ( KURLBarItemDialog::getInformation( m_useGlobal,
00606                                                     url, description, icon,
00607                                                     appLocal, m_iconSize,
00608                                                     this ) ) {
00609                 (void) insertItem( url, description, appLocal, icon );
00610                 m_isModified = true;
00611                 updateGeometry();
00612             }
00613         }
00614     }
00615 }
00616 
00617 void KURLBar::slotContextMenuRequested( QListBoxItem *item, const QPoint& pos )
00618 {
00619     if (m_isImmutable) return;
00620 
00621     static const int IconSize   = 10;
00622     static const int AddItem    = 20;
00623     static const int EditItem   = 30;
00624     static const int RemoveItem = 40;
00625 
00626     KURL lastURL = m_activeItem ? m_activeItem->url() : KURL();
00627 
00628     bool smallIcons = m_iconSize < KIcon::SizeMedium;
00629     QPopupMenu *popup = new QPopupMenu();
00630     popup->insertItem( smallIcons ?
00631                        i18n("&Large Icons") : i18n("&Small Icons"),
00632                        IconSize );
00633     popup->insertSeparator();
00634     popup->insertItem(SmallIcon("filenew"), i18n("&Add Entry..."), AddItem);
00635     popup->insertItem(SmallIcon("edit"), i18n("&Edit Entry..."), EditItem);
00636     popup->insertSeparator();
00637     popup->insertItem( SmallIcon("editdelete"), i18n("&Remove Entry"),
00638                        RemoveItem );
00639 
00640     popup->setItemEnabled( EditItem, item != 0L );
00641     popup->setItemEnabled( RemoveItem, item != 0L );
00642 
00643     int result = popup->exec( pos );
00644     switch ( result ) {
00645         case IconSize:
00646             setIconSize( smallIcons ? KIcon::SizeMedium : KIcon::SizeSmall );
00647             m_listBox->triggerUpdate( true );
00648             break;
00649         case AddItem:
00650             addNewItem();
00651             break;
00652         case EditItem:
00653             editItem( static_cast<KURLBarItem *>( item ) );
00654             break;
00655         case RemoveItem:
00656             delete item;
00657             m_isModified = true;
00658             break;
00659         default: // abort
00660             break;
00661     }
00662 
00663     // reset current item
00664     m_activeItem = 0L;
00665     setCurrentItem( lastURL );
00666 }
00667 
00668 bool KURLBar::addNewItem()
00669 {
00670     KURLBarItem *item = new KURLBarItem( this, d->currentURL,
00671                                          i18n("Enter a description") );
00672     if ( editItem( item ) ) {
00673         m_listBox->insertItem( item );
00674         return true;
00675     }
00676 
00677     delete item;
00678     return false;
00679 }
00680 
00681 bool KURLBar::editItem( KURLBarItem *item )
00682 {
00683     KURL url            = item->url();
00684     QString description = item->description();
00685     QString icon        = item->icon();
00686     bool appLocal       = item->applicationLocal();
00687 
00688     if ( KURLBarItemDialog::getInformation( m_useGlobal,
00689                                             url, description,
00690                                             icon, appLocal,
00691                                             m_iconSize, this ))
00692     {
00693         item->setURL( url );
00694         item->setDescription( description );
00695         item->setIcon( icon );
00696         item->setApplicationLocal( appLocal );
00697         m_listBox->triggerUpdate( true );
00698         m_isModified = true;
00699         updateGeometry();
00700         return true;
00701     }
00702 
00703     return false;
00704 }
00705 
00708 
00709 
00710 KURLBarListBox::KURLBarListBox( QWidget *parent, const char *name )
00711     : KListBox( parent, name )
00712 {
00713     m_toolTip = new KURLBarToolTip( this );
00714     setAcceptDrops( true );
00715     viewport()->setAcceptDrops( true );
00716 }
00717 
00718 KURLBarListBox::~KURLBarListBox()
00719 {
00720     delete m_toolTip;
00721 }
00722 
00723 QDragObject * KURLBarListBox::dragObject()
00724 {
00725     KURL::List urls;
00726     KURLBarItem *item = static_cast<KURLBarItem*>( firstItem() );
00727 
00728     while ( item ) {
00729         if ( item->isSelected() )
00730             urls.append( item->url() );
00731         item = static_cast<KURLBarItem*>( item->next() );
00732     }
00733 
00734     if ( !urls.isEmpty() ) // ### use custom drag-object with description etc.?
00735         return KURLDrag::newDrag( urls, this, "urlbar drag" );
00736 
00737     return 0L;
00738 }
00739 
00740 void KURLBarListBox::contentsDragEnterEvent( QDragEnterEvent *e )
00741 {
00742     e->accept( KURLDrag::canDecode( e ));
00743 }
00744 
00745 void KURLBarListBox::contentsDropEvent( QDropEvent *e )
00746 {
00747     emit dropped( e );
00748 }
00749 
00750 void KURLBarListBox::setOrientation( Qt::Orientation orient )
00751 {
00752     if ( orient == Vertical ) {
00753         setColumnMode( 1 );
00754         setRowMode( Variable );
00755     }
00756     else {
00757         setRowMode( 1 );
00758         setColumnMode( Variable );
00759     }
00760 
00761     m_orientation = orient;
00762 }
00763 
00766 
00767 
00768 bool KURLBarItemDialog::getInformation( bool allowGlobal, KURL& url,
00769                                         QString& description, QString& icon,
00770                                         bool& appLocal, int iconSize,
00771                                         QWidget *parent )
00772 {
00773     KURLBarItemDialog *dialog = new KURLBarItemDialog( allowGlobal, url,
00774                                                        description, icon,
00775                                                        appLocal,
00776                                                        iconSize, parent );
00777     if ( dialog->exec() == QDialog::Accepted ) {
00778         // set the return parameters
00779         url         = dialog->url();
00780         description = dialog->description();
00781         icon        = dialog->icon();
00782         appLocal    = dialog->applicationLocal();
00783 
00784         delete dialog;
00785         return true;
00786     }
00787 
00788     delete dialog;
00789     return false;
00790 }
00791 
00792 KURLBarItemDialog::KURLBarItemDialog( bool allowGlobal, const KURL& url,
00793                                       const QString& description,
00794                                       QString icon, bool appLocal,
00795                                       int iconSize,
00796                                       QWidget *parent, const char *name )
00797     : KDialogBase( parent, name, true,
00798                    i18n("Edit Quick Access Entry"), Ok | Cancel, Ok, true )
00799 {
00800     QVBox *box = new QVBox( this );
00801     QString text = i18n("<qt><b>Please provide a description, URL and icon for this Quick Access entry.</b></br></qt>");
00802     QLabel *label = new QLabel( text, box );
00803     box->setSpacing( spacingHint() );
00804 
00805     QGrid *grid = new QGrid( 2, box );
00806     grid->setSpacing( spacingHint() );
00807 
00808     QString whatsThisText = i18n("<qt>This is the text that will appear in the Quick Access panel.<p>"
00809                                  "The description should consist of one or two words "
00810                                  "that will help you remember what this entry refers to.</qt>");
00811     label = new QLabel( i18n("&Description:"), grid );
00812     m_edit = new KLineEdit( grid, "description edit" );
00813     m_edit->setText( description.isEmpty() ? url.fileName() : description );
00814     label->setBuddy( m_edit );
00815     QWhatsThis::add( label, whatsThisText );
00816     QWhatsThis::add( m_edit, whatsThisText );
00817 
00818     whatsThisText = i18n("<qt>This is the location associated with the entry. Any valid URL may be used. For example:<p>"
00819                          "%1<br>http://www.kde.org<br>ftp://ftp.kde.org/pub/kde/stable<p>"
00820                          "By clicking on the button next to the text edit box you can browse to an "
00821                          "appropriate URL.</qt>").arg(QDir::homeDirPath());
00822     label = new QLabel( i18n("&URL:"), grid );
00823     m_urlEdit = new KURLRequester( url.prettyURL(), grid );
00824     m_urlEdit->setMode( KFile::Directory );
00825     label->setBuddy( m_urlEdit );
00826     QWhatsThis::add( label, whatsThisText );
00827     QWhatsThis::add( m_urlEdit, whatsThisText );
00828 
00829     whatsThisText = i18n("<qt>This is the icon that will appear in the Quick Access panel.<p>"
00830                          "Click on the button to select a different icon.</qt>");
00831     label = new QLabel( i18n("Choose an &icon:"), grid );
00832     m_iconButton = new KIconButton( grid, "icon button" );
00833     m_iconButton->setIconSize( iconSize );
00834     m_iconButton->setStrictIconSize( true );
00835     if ( icon.isEmpty() )
00836         icon = KMimeType::iconForURL( url );
00837     m_iconButton->setIcon( icon );
00838     label->setBuddy( m_iconButton );
00839     QWhatsThis::add( label, whatsThisText );
00840     QWhatsThis::add( m_iconButton, whatsThisText );
00841 
00842     if ( allowGlobal ) {
00843         QString appName;
00844         if ( KGlobal::instance()->aboutData() )
00845             appName = KGlobal::instance()->aboutData()->programName();
00846         if ( appName.isEmpty() )
00847             appName = QString::fromLatin1( KGlobal::instance()->instanceName() );
00848         m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)").arg( appName ), box );
00849         m_appLocal->setChecked( appLocal );
00850         QWhatsThis::add( m_appLocal,
00851                          i18n("<qt>Select this setting if you want this "
00852                               "entry to show only when using the current application (%1).<p>"
00853                               "If this setting is not selected, the entry will be available in all "
00854                               "applications.</qt>")
00855                               .arg(appName));
00856     }
00857     else
00858         m_appLocal = 0L;
00859     connect(m_urlEdit->lineEdit(),SIGNAL(textChanged ( const QString & )),this,SLOT(urlChanged(const QString & )));
00860     m_edit->setFocus();
00861     setMainWidget( box );
00862 }
00863 
00864 KURLBarItemDialog::~KURLBarItemDialog()
00865 {
00866 }
00867 
00868 void KURLBarItemDialog::urlChanged(const QString & text )
00869 {
00870     enableButtonOK( !text.isEmpty() );
00871 }
00872 
00873 KURL KURLBarItemDialog::url() const
00874 {
00875     QString text = m_urlEdit->url();
00876     KURL u;
00877     if ( text.at(0) == '/' )
00878         u.setPath( text );
00879     else
00880         u = text;
00881 
00882     return u;
00883 }
00884 
00885 QString KURLBarItemDialog::description() const
00886 {
00887     return m_edit->text();
00888 }
00889 
00890 QString KURLBarItemDialog::icon() const
00891 {
00892     return m_iconButton->icon();
00893 }
00894 
00895 bool KURLBarItemDialog::applicationLocal() const
00896 {
00897     if ( !m_appLocal )
00898         return true;
00899 
00900     return m_appLocal->isChecked();
00901 }
00902 
00903 void KURLBarItem::virtual_hook( int, void* )
00904 { /*BASE::virtual_hook( id, data );*/ }
00905 
00906 void KURLBar::virtual_hook( int, void* )
00907 { /*BASE::virtual_hook( id, data );*/ }
00908 
00909 void KURLBarListBox::virtual_hook( int id, void* data )
00910 { KListBox::virtual_hook( id, data ); }
00911 
00912 
00913 #include "kurlbar.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:32 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001