kurlbar.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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     m_listBox->setFocusPolicy( TabFocus );
    00320
    00321     connect( m_listBox, SIGNAL( mouseButtonClicked( int, QListBoxItem *, const QPoint & ) ),
    00322              SLOT( slotSelected( int, QListBoxItem * )));
    00323     connect( m_listBox, SIGNAL( dropped( QDropEvent * )),
    00324              this, SLOT( slotDropped( QDropEvent * )));
    00325     connect( m_listBox, SIGNAL( contextMenuRequested( QListBoxItem *,
    00326                                                       const QPoint& )),
    00327              SLOT( slotContextMenuRequested( QListBoxItem *, const QPoint& )));
    00328 }
    00329
    00330 void KURLBar::setIconSize( int size )
    00331 {
    00332     if ( size == m_iconSize )
    00333         return;
    00334
    00335     m_iconSize = size;
    00336
    00337     // reload the icons with the new size
    00338     KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
    00339     while ( item ) {
    00340         item->setIcon( item->icon(), item->iconGroup() );
    00341         item = static_cast<KURLBarItem*>( item->next() );
    00342     }
    00343
    00344     resize( sizeHint() );
    00345     updateGeometry();
    00346 }
    00347
    00348 void KURLBar::clear()
    00349 {
    00350     m_listBox->clear();
    00351 }
    00352
    00353 void KURLBar::resizeEvent( QResizeEvent *e )
    00354 {
    00355     QFrame::resizeEvent( e );
    00356     m_listBox->resize( width(), height() );
    00357 }
    00358
    00359 QSize KURLBar::sizeHint() const
    00360 {
    00361     return m_listBox->sizeHint();
    00362
    00363 #if 0
    00364     // this code causes vertical and or horizontal scrollbars appearing
    00365     // depending on the text, font, moonphase and earth rotation. Just using
    00366     // m_listBox->sizeHint() fixes this (although the widget can then be
    00367     // resized to a smaller size so that scrollbars appear).
    00368     int w = 0;
    00369     int h = 0;
    00370     KURLBarItem *item;
    00371     bool vertical = isVertical();
    00372
    00373     for ( item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
    00374           item;
    00375           item = static_cast<KURLBarItem*>( item->next() ) ) {
    00376
    00377         QSize sh = item->sizeHint();
    00378
    00379         if ( vertical ) {
    00380             w = QMAX( w, sh.width() );
    00381             h += sh.height();
    00382         }
    00383         else {
    00384             w += sh.width();
    00385             h = QMAX( h, sh.height() );
    00386         }
    00387     }
    00388
    00389 //     if ( vertical && m_listBox->verticalScrollBar()->isVisible() )
    00390 //         w += m_listBox->verticalScrollBar()->width();
    00391 //     else if ( !vertical && m_listBox->horizontalScrollBar()->isVisible() )
    00392 //         h += m_listBox->horizontalScrollBar()->height();
    00393
    00394     if ( w == 0 && h == 0 )
    00395         return QSize( 100, 200 );
    00396     else
    00397         return QSize( 6 + w, h );
    00398 #endif
    00399 }
    00400
    00401 QSize KURLBar::minimumSizeHint() const
    00402 {
    00403     QSize s = sizeHint(); // ###
    00404     int w = s.width()  + m_listBox->verticalScrollBar()->width();
    00405     int h = s.height() + m_listBox->horizontalScrollBar()->height();
    00406     return QSize( w, h );
    00407 }
    00408
    00409 void KURLBar::slotSelected( int button, QListBoxItem *item )
    00410 {
    00411     if ( button != Qt::LeftButton )
    00412         return;
    00413
    00414     slotSelected( item );
    00415 }
    00416
    00417 void KURLBar::slotSelected( QListBoxItem *item )
    00418 {
    00419     if ( item && item != m_activeItem )
    00420         m_activeItem = static_cast<KURLBarItem*>( item );
    00421
    00422     if ( m_activeItem ) {
    00423         m_listBox->setCurrentItem( m_activeItem );
    00424         emit activated( m_activeItem->url() );
    00425     }
    00426 }
    00427
    00428 void KURLBar::setCurrentItem( const KURL& url )
    00429 {
    00430     d->currentURL = url;
    00431
    00432     QString u = url.url(-1);
    00433
    00434     if ( m_activeItem && m_activeItem->url().url(-1) == u )
    00435         return;
    00436
    00437     bool hasURL = false;
    00438     QListBoxItem *item = m_listBox->firstItem();
    00439     while ( item ) {
    00440         if ( static_cast<KURLBarItem*>( item )->url().url(-1) == u ) {
    00441             m_activeItem = static_cast<KURLBarItem*>( item );
    00442             m_listBox->setCurrentItem( item );
    00443             m_listBox->setSelected( item, true );
    00444             hasURL = true;
    00445             break;
    00446         }
    00447         item = item->next();
    00448     }
    00449
    00450     if ( !hasURL ) {
    00451         m_activeItem = 0L;
    00452         m_listBox->clearSelection();
    00453     }
    00454 }
    00455
    00456 KURLBarItem * KURLBar::currentItem() const
    00457 {
    00458     QListBoxItem *item = m_listBox->item( m_listBox->currentItem() );
    00459     if ( item )
    00460         return static_cast<KURLBarItem *>( item );
    00461     return 0L;
    00462 }
    00463
    00464 KURL KURLBar::currentURL() const
    00465 {
    00466     KURLBarItem *item = currentItem();
    00467     return item ? item->url() : KURL();
    00468 }
    00469
    00470 void KURLBar::readConfig( KConfig *appConfig, const QString& itemGroup )
    00471 {
    00472     m_isImmutable = appConfig->groupIsImmutable( itemGroup );
    00473     KConfigGroupSaver cs( appConfig, itemGroup );
    00474     m_iconSize = appConfig->readNumEntry( "Speedbar IconSize", m_iconSize );
    00475
    00476     if ( m_useGlobal ) { // read global items
    00477         KConfig *globalConfig = KGlobal::config();
    00478         KConfigGroupSaver cs( globalConfig, (QString)(itemGroup +" (Global)"));
    00479         int num = globalConfig->readNumEntry( "Number of Entries" );
    00480         for ( int i = 0; i < num; i++ ) {
    00481             readItem( i, globalConfig, false );
    00482         }
    00483     }
    00484
    00485     // read application local items
    00486     int num = appConfig->readNumEntry( "Number of Entries" );
    00487     for ( int i = 0; i < num; i++ ) {
    00488         readItem( i, appConfig, true );
    00489     }
    00490 }
    00491
    00492 static KURL kurlbar_readURLEntry( KConfig *config, const QString& key )
    00493 {
    00494     // This looks a bit complex to just read a URL, but we have our reasons:
    00495     // Until KDE 3.1.1, the url was written as prettyURL(), so it included the file:/
    00496     // prefix. Now we use KConfig::writePathEntry( url.path() ) in case it's a local
    00497     // file, so readEntry() may return e.g. either file:/foo, /foo or $HOME
    00498
    00499     QString urlEntry = config->readEntry( key );
    00500     bool isPath = !urlEntry.isEmpty() && (urlEntry[0] == '/' || urlEntry[0] == '$');
    00501     // if it's local, try dollar expansion
    00502     if ( !urlEntry.isEmpty() && (isPath || urlEntry.startsWith("file:/") ))
    00503         urlEntry = config->readPathEntry( key );
    00504     KURL url;
    00505     if ( isPath )
    00506         url.setPath( urlEntry );
    00507     else
    00508         url = urlEntry;
    00509
    00510     return url;
    00511 }
    00512
    00513 void KURLBar::readItem( int i, KConfig *config, bool applicationLocal )
    00514 {
    00515     QString number = QString::number( i );
    00516     KURL url = kurlbar_readURLEntry( config, QString("URL_") + number );
    00517     if ( url.isMalformed() || !KProtocolInfo::isKnownProtocol( url ))
    00518         return; // nothing we could do.
    00519
    00520     insertItem( url,
    00521                 config->readEntry( QString("Description_") + number ),
    00522                 applicationLocal,
    00523                 config->readEntry( QString("Icon_") + number ),
    00524                 static_cast<KIcon::Group>(
    00525                     config->readNumEntry( QString("IconGroup_") + number )) );
    00526 }
    00527
    00528 void KURLBar::writeConfig( KConfig *config, const QString& itemGroup )
    00529 {
    00530     KConfigGroupSaver cs1( config, itemGroup );
    00531     config->writeEntry( "Speedbar IconSize", m_iconSize );
    00532
    00533     if ( !m_isModified )
    00534         return;
    00535
    00536     int i = 0;
    00537     int numLocal = 0;
    00538     KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
    00539
    00540     while ( item ) {
    00541         if ( item->applicationLocal() ) {
    00542             writeItem( item, numLocal, config, false );
    00543             numLocal++;
    00544         }
    00545
    00546         i++;
    00547         item = static_cast<KURLBarItem*>( item->next() );
    00548     }
    00549     config->writeEntry("Number of Entries", numLocal);
    00550
    00551
    00552     // write the global entries to kdeglobals, if any
    00553     bool haveGlobalEntries = (i > numLocal);
    00554     if ( m_useGlobal && haveGlobalEntries ) {
    00555         config->setGroup( itemGroup + " (Global)" );
    00556
    00557         int numGlobals = 0;
    00558         item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
    00559
    00560         while ( item ) {
    00561             if ( !item->applicationLocal() ) {
    00562                 writeItem( item, numGlobals, config, true );
    00563                 numGlobals++;
    00564             }
    00565
    00566             item = static_cast<KURLBarItem*>( item->next() );
    00567         }
    00568         config->writeEntry("Number of Entries", numGlobals, true, true);
    00569     }
    00570
    00571     m_isModified = false;
    00572 }
    00573
    00574 void KURLBar::writeItem( KURLBarItem *item, int i, KConfig *config,
    00575                          bool global )
    00576 {
    00577     QString Description = "Description_";
    00578     QString URL = "URL_";
    00579     QString Icon = "Icon_";
    00580     QString IconGroup = "IconGroup_";
    00581
    00582     QString number = QString::number( i );
    00583     if ( item->url().isLocalFile() )
    00584         config->writePathEntry( URL + number, item->url().path(), true, global );
    00585     else
    00586     config->writeEntry( URL + number, item->url().prettyURL(), true, global );
    00587
    00588     config->writeEntry( Description + number, item->description(),true,global);
    00589     config->writeEntry( Icon + number, item->icon(), true, global );
    00590     config->writeEntry( IconGroup + number, item->iconGroup(), true, global );
    00591 }
    00592
    00593
    00594 void KURLBar::slotDropped( QDropEvent *e )
    00595 {
    00596     KURL::List urls;
    00597     if ( KURLDrag::decode( e, urls ) ) {
    00598         KURL url;
    00599         QString description;
    00600         QString icon;
    00601         bool appLocal = false;
    00602
    00603         KURL::List::Iterator it = urls.begin();
    00604         for ( ; it != urls.end(); ++it ) {
    00605             url = *it;
    00606             if ( KURLBarItemDialog::getInformation( m_useGlobal,
    00607                                                     url, description, icon,
    00608                                                     appLocal, m_iconSize,
    00609                                                     this ) ) {
    00610                 (void) insertItem( url, description, appLocal, icon );
    00611                 m_isModified = true;
    00612                 updateGeometry();
    00613             }
    00614         }
    00615     }
    00616 }
    00617
    00618 void KURLBar::slotContextMenuRequested( QListBoxItem *item, const QPoint& pos )
    00619 {
    00620     if (m_isImmutable) return;
    00621
    00622     static const int IconSize   = 10;
    00623     static const int AddItem    = 20;
    00624     static const int EditItem   = 30;
    00625     static const int RemoveItem = 40;
    00626
    00627     KURL lastURL = m_activeItem ? m_activeItem->url() : KURL();
    00628
    00629     bool smallIcons = m_iconSize < KIcon::SizeMedium;
    00630     QPopupMenu *popup = new QPopupMenu();
    00631     popup->insertItem( smallIcons ?
    00632                        i18n("&Large Icons") : i18n("&Small Icons"),
    00633                        IconSize );
    00634     popup->insertSeparator();
    00635     popup->insertItem(SmallIcon("filenew"), i18n("&Add Entry..."), AddItem);
    00636     popup->insertItem(SmallIcon("edit"), i18n("&Edit Entry..."), EditItem);
    00637     popup->insertSeparator();
    00638     popup->insertItem( SmallIcon("editdelete"), i18n("&Remove Entry"),
    00639                        RemoveItem );
    00640
    00641     popup->setItemEnabled( EditItem, item != 0L );
    00642     popup->setItemEnabled( RemoveItem, item != 0L );
    00643
    00644     int result = popup->exec( pos );
    00645     switch ( result ) {
    00646         case IconSize:
    00647             setIconSize( smallIcons ? KIcon::SizeMedium : KIcon::SizeSmall );
    00648             m_listBox->triggerUpdate( true );
    00649             break;
    00650         case AddItem:
    00651             addNewItem();
    00652             break;
    00653         case EditItem:
    00654             editItem( static_cast<KURLBarItem *>( item ) );
    00655             break;
    00656         case RemoveItem:
    00657             delete item;
    00658             m_isModified = true;
    00659             break;
    00660         default: // abort
    00661             break;
    00662     }
    00663
    00664     // reset current item
    00665     m_activeItem = 0L;
    00666     setCurrentItem( lastURL );
    00667 }
    00668
    00669 bool KURLBar::addNewItem()
    00670 {
    00671     KURLBarItem *item = new KURLBarItem( this, d->currentURL,
    00672                                          i18n("Enter a description") );
    00673     if ( editItem( item ) ) {
    00674         m_listBox->insertItem( item );
    00675         return true;
    00676     }
    00677
    00678     delete item;
    00679     return false;
    00680 }
    00681
    00682 bool KURLBar::editItem( KURLBarItem *item )
    00683 {
    00684     KURL url            = item->url();
    00685     QString description = item->description();
    00686     QString icon        = item->icon();
    00687     bool appLocal       = item->applicationLocal();
    00688
    00689     if ( KURLBarItemDialog::getInformation( m_useGlobal,
    00690                                             url, description,
    00691                                             icon, appLocal,
    00692                                             m_iconSize, this ))
    00693     {
    00694         item->setURL( url );
    00695         item->setDescription( description );
    00696         item->setIcon( icon );
    00697         item->setApplicationLocal( appLocal );
    00698         m_listBox->triggerUpdate( true );
    00699         m_isModified = true;
    00700         updateGeometry();
    00701         return true;
    00702     }
    00703
    00704     return false;
    00705 }
    00706
    00709
    00710
    00711 KURLBarListBox::KURLBarListBox( QWidget *parent, const char *name )
    00712     : KListBox( parent, name )
    00713 {
    00714     m_toolTip = new KURLBarToolTip( this );
    00715     setAcceptDrops( true );
    00716     viewport()->setAcceptDrops( true );
    00717 }
    00718
    00719 KURLBarListBox::~KURLBarListBox()
    00720 {
    00721     delete m_toolTip;
    00722 }
    00723
    00724 QDragObject * KURLBarListBox::dragObject()
    00725 {
    00726     KURL::List urls;
    00727     KURLBarItem *item = static_cast<KURLBarItem*>( firstItem() );
    00728
    00729     while ( item ) {
    00730         if ( item->isSelected() )
    00731             urls.append( item->url() );
    00732         item = static_cast<KURLBarItem*>( item->next() );
    00733     }
    00734
    00735     if ( !urls.isEmpty() ) // ### use custom drag-object with description etc.?
    00736         return KURLDrag::newDrag( urls, this, "urlbar drag" );
    00737
    00738     return 0L;
    00739 }
    00740
    00741 void KURLBarListBox::contentsDragEnterEvent( QDragEnterEvent *e )
    00742 {
    00743     e->accept( KURLDrag::canDecode( e ));
    00744 }
    00745
    00746 void KURLBarListBox::contentsDropEvent( QDropEvent *e )
    00747 {
    00748     emit dropped( e );
    00749 }
    00750
    00751 void KURLBarListBox::setOrientation( Qt::Orientation orient )
    00752 {
    00753     if ( orient == Vertical ) {
    00754         setColumnMode( 1 );
    00755         setRowMode( Variable );
    00756     }
    00757     else {
    00758         setRowMode( 1 );
    00759         setColumnMode( Variable );
    00760     }
    00761
    00762     m_orientation = orient;
    00763 }
    00764
    00767
    00768
    00769 bool KURLBarItemDialog::getInformation( bool allowGlobal, KURL& url,
    00770                                         QString& description, QString& icon,
    00771                                         bool& appLocal, int iconSize,
    00772                                         QWidget *parent )
    00773 {
    00774     KURLBarItemDialog *dialog = new KURLBarItemDialog( allowGlobal, url,
    00775                                                        description, icon,
    00776                                                        appLocal,
    00777                                                        iconSize, parent );
    00778     if ( dialog->exec() == QDialog::Accepted ) {
    00779         // set the return parameters
    00780         url         = dialog->url();
    00781         description = dialog->description();
    00782         icon        = dialog->icon();
    00783         appLocal    = dialog->applicationLocal();
    00784
    00785         delete dialog;
    00786         return true;
    00787     }
    00788
    00789     delete dialog;
    00790     return false;
    00791 }
    00792
    00793 KURLBarItemDialog::KURLBarItemDialog( bool allowGlobal, const KURL& url,
    00794                                       const QString& description,
    00795                                       QString icon, bool appLocal,
    00796                                       int iconSize,
    00797                                       QWidget *parent, const char *name )
    00798     : KDialogBase( parent, name, true,
    00799                    i18n("Edit Quick Access Entry"), Ok | Cancel, Ok, true )
    00800 {
    00801     QVBox *box = new QVBox( this );
    00802     QString text = i18n("<qt><b>Please provide a description, URL and icon for this Quick Access entry.</b></br></qt>");
    00803     QLabel *label = new QLabel( text, box );
    00804     box->setSpacing( spacingHint() );
    00805
    00806     QGrid *grid = new QGrid( 2, box );
    00807     grid->setSpacing( spacingHint() );
    00808
    00809     QString whatsThisText = i18n("<qt>This is the text that will appear in the Quick Access panel.<p>"
    00810                                  "The description should consist of one or two words "
    00811                                  "that will help you remember what this entry refers to.</qt>");
    00812     label = new QLabel( i18n("&Description:"), grid );
    00813     m_edit = new KLineEdit( grid, "description edit" );
    00814     m_edit->setText( description.isEmpty() ? url.fileName() : description );
    00815     label->setBuddy( m_edit );
    00816     QWhatsThis::add( label, whatsThisText );
    00817     QWhatsThis::add( m_edit, whatsThisText );
    00818
    00819     whatsThisText = i18n("<qt>This is the location associated with the entry. Any valid URL may be used. For example:<p>"
    00820                          "%1<br>http://www.kde.org<br>ftp://ftp.kde.org/pub/kde/stable<p>"
    00821                          "By clicking on the button next to the text edit box you can browse to an "
    00822                          "appropriate URL.</qt>").arg(QDir::homeDirPath());
    00823     label = new QLabel( i18n("&URL:"), grid );
    00824     m_urlEdit = new KURLRequester( url.prettyURL(), grid );
    00825     m_urlEdit->setMode( KFile::Directory );
    00826     label->setBuddy( m_urlEdit );
    00827     QWhatsThis::add( label, whatsThisText );
    00828     QWhatsThis::add( m_urlEdit, whatsThisText );
    00829
    00830     whatsThisText = i18n("<qt>This is the icon that will appear in the Quick Access panel.<p>"
    00831                          "Click on the button to select a different icon.</qt>");
    00832     label = new QLabel( i18n("Choose an &icon:"), grid );
    00833     m_iconButton = new KIconButton( grid, "icon button" );
    00834     m_iconButton->setIconSize( iconSize );
    00835     m_iconButton->setStrictIconSize( true );
    00836     if ( icon.isEmpty() )
    00837         icon = KMimeType::iconForURL( url );
    00838     m_iconButton->setIcon( icon );
    00839     label->setBuddy( m_iconButton );
    00840     QWhatsThis::add( label, whatsThisText );
    00841     QWhatsThis::add( m_iconButton, whatsThisText );
    00842
    00843     if ( allowGlobal ) {
    00844         QString appName;
    00845         if ( KGlobal::instance()->aboutData() )
    00846             appName = KGlobal::instance()->aboutData()->programName();
    00847         if ( appName.isEmpty() )
    00848             appName = QString::fromLatin1( KGlobal::instance()->instanceName() );
    00849         m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)").arg( appName ), box );
    00850         m_appLocal->setChecked( appLocal );
    00851         QWhatsThis::add( m_appLocal,
    00852                          i18n("<qt>Select this setting if you want this "
    00853                               "entry to show only when using the current application (%1).<p>"
    00854                               "If this setting is not selected, the entry will be available in all "
    00855                               "applications.</qt>")
    00856                               .arg(appName));
    00857     }
    00858     else
    00859         m_appLocal = 0L;
    00860     connect(m_urlEdit->lineEdit(),SIGNAL(textChanged ( const QString & )),this,SLOT(urlChanged(const QString & )));
    00861     m_edit->setFocus();
    00862     setMainWidget( box );
    00863 }
    00864
    00865 KURLBarItemDialog::~KURLBarItemDialog()
    00866 {
    00867 }
    00868
    00869 void KURLBarItemDialog::urlChanged(const QString & text )
    00870 {
    00871     enableButtonOK( !text.isEmpty() );
    00872 }
    00873
    00874 KURL KURLBarItemDialog::url() const
    00875 {
    00876     QString text = m_urlEdit->url();
    00877     KURL u;
    00878     if ( text.at(0) == '/' )
    00879         u.setPath( text );
    00880     else
    00881         u = text;
    00882
    00883     return u;
    00884 }
    00885
    00886 QString KURLBarItemDialog::description() const
    00887 {
    00888     return m_edit->text();
    00889 }
    00890
    00891 QString KURLBarItemDialog::icon() const
    00892 {
    00893     return m_iconButton->icon();
    00894 }
    00895
    00896 bool KURLBarItemDialog::applicationLocal() const
    00897 {
    00898     if ( !m_appLocal )
    00899         return true;
    00900
    00901     return m_appLocal->isChecked();
    00902 }
    00903
    00904 void KURLBarItem::virtual_hook( int, void* )
    00905 { /*BASE::virtual_hook( id, data );*/ }
    00906
    00907 void KURLBar::virtual_hook( int, void* )
    00908 { /*BASE::virtual_hook( id, data );*/ }
    00909
    00910 void KURLBarListBox::virtual_hook( int id, void* data )
    00911 { KListBox::virtual_hook( id, data ); }
    00912
    00913
    00914 #include "kurlbar.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:32 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001