kio Library API Documentation

kurlbar.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2001,2002,2003 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 <qapplication.h>
00022 #include <qcheckbox.h>
00023 #include <qdrawutil.h>
00024 #include <qfontmetrics.h>
00025 #include <qlabel.h>
00026 #include <qgrid.h>
00027 #include <qpainter.h>
00028 #include <qpopupmenu.h>
00029 #include <qstyle.h>
00030 #include <qvbox.h>
00031 #include <qwhatsthis.h>
00032 
00033 #include <kaboutdata.h>
00034 #include <kconfig.h>
00035 #include <kdebug.h>
00036 #include <kglobal.h>
00037 #include <kicondialog.h>
00038 #include <kiconloader.h>
00039 #include <kinstance.h>
00040 #include <klineedit.h>
00041 #include <klocale.h>
00042 #include <kmimetype.h>
00043 #include <kprotocolinfo.h>
00044 #include <kstringhandler.h>
00045 #include <kurldrag.h>
00046 #include <kurlrequester.h>
00047 
00048 #include "kurlbar.h"
00049 
00054 class KURLBarToolTip : public QToolTip
00055 {
00056 public:
00057     KURLBarToolTip( QListBox *view ) : QToolTip( view ), m_view( view ) {}
00058 
00059 protected:
00060     virtual void maybeTip( const QPoint& point ) {
00061         QListBoxItem *item = m_view->itemAt( point );
00062         if ( item ) {
00063             QString text = static_cast<KURLBarItem*>( item )->toolTip();
00064             if ( !text.isEmpty() )
00065                 tip( m_view->itemRect( item ), text );
00066         }
00067     }
00068 
00069 private:
00070     QListBox *m_view;
00071 };
00072 
00073 
00076 
00077 class KURLBarItem::KURLBarItemPrivate
00078 {
00079 public:
00080     KURLBarItemPrivate()
00081     {
00082         isPersistent = true;
00083     }
00084 
00085     bool isPersistent;
00086 };
00087 
00088 KURLBarItem::KURLBarItem( KURLBar *parent,
00089                           const KURL& url, bool persistent, const QString& description,
00090                           const QString& icon, KIcon::Group group )
00091     : QListBoxPixmap( KIconLoader::unknown() /*, parent->listBox()*/ ),
00092       m_url( url ),
00093       m_pixmap( 0L ),
00094       m_parent( parent ),
00095       m_appLocal( true )
00096 {
00097     init( icon, group, description, persistent );
00098 }
00099 
00100 KURLBarItem::KURLBarItem( KURLBar *parent,
00101                           const KURL& url, const QString& description,
00102                           const QString& icon, KIcon::Group group )
00103     : QListBoxPixmap( KIconLoader::unknown() /*, parent->listBox()*/ ),
00104       m_url( url ),
00105       m_pixmap( 0L ),
00106       m_parent( parent ),
00107       m_appLocal( true )
00108 {
00109     init( icon, group, description, true /*persistent*/ );
00110 }
00111 
00112 void KURLBarItem::init( const QString& icon, KIcon::Group group,
00113                         const QString& description, bool persistent )
00114 {
00115     d = new KURLBarItemPrivate;
00116     d->isPersistent = persistent;
00117 
00118     setCustomHighlighting( true );
00119     setIcon( icon, group );
00120     setDescription( description );
00121 }
00122 
00123 KURLBarItem::~KURLBarItem()
00124 {
00125     delete d;
00126 }
00127 
00128 void KURLBarItem::setURL( const KURL& url )
00129 {
00130     m_url = url;
00131     if ( m_description.isEmpty() )
00132         setText( url.fileName() );
00133 }
00134 
00135 void KURLBarItem::setIcon( const QString& icon, KIcon::Group group )
00136 {
00137     m_icon  = icon;
00138     m_group = group;
00139 
00140     if ( icon.isEmpty() )
00141         m_pixmap = KMimeType::pixmapForURL( m_url, 0, group, iconSize() );
00142     else
00143         m_pixmap = KGlobal::iconLoader()->loadIcon( icon, group, iconSize(),
00144                                                     KIcon::DefaultState );
00145 }
00146 
00147 void KURLBarItem::setDescription( const QString& desc )
00148 {
00149     m_description = desc;
00150     setText( desc.isEmpty() ? m_url.fileName() : desc );
00151 }
00152 
00153 void KURLBarItem::setApplicationLocal( bool local )
00154 {
00155     if ( !local && !isPersistent() )
00156     {
00157         kdWarning() << "KURLBar: dynamic (non-persistent) items can not be global." << endl;
00158         return;
00159     }
00160 
00161     m_appLocal = local;
00162 }
00163 
00164 void KURLBarItem::setToolTip( const QString& tip )
00165 {
00166     m_toolTip = tip;
00167 }
00168 
00169 QString KURLBarItem::toolTip() const
00170 {
00171     return m_toolTip.isEmpty() ? m_url.prettyURL() : m_toolTip;
00172 }
00173 
00174 int KURLBarItem::iconSize() const
00175 {
00176     return m_parent->iconSize();
00177 }
00178 
00179 void KURLBarItem::paint( QPainter *p )
00180 {
00181     QListBox *box = listBox();
00182     int w = width( box );
00183     static const int margin = KDialog::spacingHint();
00184 
00185     // draw sunken selection
00186     if ( isCurrent() || isSelected() ) {
00187         int h = height( box );
00188 
00189         QBrush brush = box->colorGroup().brush( QColorGroup::Highlight );
00190         p->fillRect( 0, 0, w, h, brush );
00191         QPen pen = p->pen();
00192         QPen oldPen = pen;
00193         pen.setColor( box->colorGroup().mid() );
00194         p->setPen( pen );
00195 
00196         p->drawPoint( 0, 0 );
00197         p->drawPoint( 0, h - 1 );
00198         p->drawPoint( w - 1, 0 );
00199         p->drawPoint( w - 1, h - 1 );
00200 
00201         p->setPen( oldPen );
00202     }
00203 
00204     if ( m_parent->iconSize() < KIcon::SizeMedium ) {
00205         // small icon -> draw icon next to text
00206 
00207         // ### mostly cut & paste of QListBoxPixmap::paint() until Qt 3.1
00208         // (where it will properly use pixmap() instead of the internal pixmap)
00209         const QPixmap *pm = pixmap();
00210         int yPos = QMAX( 0, (height(box) - pm->height())/2 );
00211 
00212         p->drawPixmap( margin, yPos, *pm );
00213         if ( !text().isEmpty() ) {
00214             QFontMetrics fm = p->fontMetrics();
00215             if ( pm->height() < fm.height() )
00216                 yPos = fm.ascent() + fm.leading()/2;
00217             else
00218                 yPos = pm->height()/2 - fm.height()/2 + fm.ascent();
00219 
00220             yPos += margin;
00221             int stringWidth = box->width() - pm->width() - 2 - (margin * 2);
00222             QString visibleText = KStringHandler::rPixelSqueeze( text(), fm, stringWidth );
00223             int xPos = pm->width() + margin + 2;
00224 
00225             if ( isCurrent() || isSelected() ) {
00226                 p->setPen( box->colorGroup().highlight().dark(115) );
00227                 p->drawText( xPos + ( QApplication::reverseLayout() ? -1 : 1),
00228                              yPos + 1, visibleText );
00229                 p->setPen( box->colorGroup().highlightedText() );
00230             }
00231 
00232             p->drawText( xPos, yPos, visibleText );
00233         }
00234         // end cut & paste (modulo pixmap centering)
00235     }
00236 
00237     else {
00238         // big icons -> draw text below icon
00239         int y = margin;
00240         const QPixmap *pm = pixmap();
00241 
00242         if ( !pm->isNull() ) {
00243             int x = (w - pm->width()) / 2;
00244             x = QMAX( x, margin );
00245             p->drawPixmap( x, y, *pm );
00246         }
00247 
00248         if ( !text().isEmpty() ) {
00249             QFontMetrics fm = p->fontMetrics();
00250             y += pm->height() + fm.height() - fm.descent();
00251 
00252             int stringWidth = box->width() - (margin * 2);
00253             QString visibleText = KStringHandler::rPixelSqueeze( text(), fm, stringWidth );
00254             int x = (w - fm.width( visibleText )) / 2;
00255             x = QMAX( x, margin );
00256 
00257             if ( isCurrent() || isSelected() ) {
00258                 p->setPen( box->colorGroup().highlight().dark(115) );
00259                 p->drawText( x + ( QApplication::reverseLayout() ? -1 : 1),
00260                              y + 1, visibleText );
00261                 p->setPen( box->colorGroup().highlightedText() );
00262             }
00263 
00264             p->drawText( x, y, visibleText );
00265         }
00266     }
00267 }
00268 
00269 QSize KURLBarItem::sizeHint() const
00270 {
00271     int wmin = 0;
00272     int hmin = 0;
00273     const KURLBarListBox *lb =static_cast<const KURLBarListBox*>(listBox());
00274 
00275     if ( m_parent->iconSize() < KIcon::SizeMedium ) {
00276         wmin = QListBoxPixmap::width( lb ) + KDialog::spacingHint() * 2;
00277         hmin = QListBoxPixmap::height( lb ) + KDialog::spacingHint() * 2;
00278     }
00279     else {
00280         wmin = QMAX(lb->fontMetrics().width(text()), pixmap()->width()) + KDialog::spacingHint() * 2;
00281         hmin = lb->fontMetrics().lineSpacing() + pixmap()->height() + KDialog::spacingHint() * 2;
00282     }
00283 
00284     if ( lb->isVertical() )
00285         wmin = QMIN( wmin, lb->viewport()->sizeHint().width() );
00286     else
00287         hmin = QMIN( hmin, lb->viewport()->sizeHint().height() );
00288 
00289     return QSize( wmin, hmin );
00290 }
00291 
00292 int KURLBarItem::width( const QListBox *lb ) const
00293 {
00294     if ( static_cast<const KURLBarListBox *>( lb )->isVertical() )
00295         return QMAX( sizeHint().width(), lb->viewport()->width() );
00296     else
00297         return sizeHint().width();
00298 }
00299 
00300 int KURLBarItem::height( const QListBox *lb ) const
00301 {
00302     if ( static_cast<const KURLBarListBox *>( lb )->isVertical() )
00303         return sizeHint().height();
00304     else
00305         return QMAX( sizeHint().height(), lb->viewport()->height() );
00306 }
00307 
00308 bool KURLBarItem::isPersistent() const
00309 {
00310     return d->isPersistent;
00311 }
00312 
00315 
00316 class KURLBar::KURLBarPrivate
00317 {
00318 public:
00319     KURLBarPrivate()
00320     {
00321         currentURL.setPath( QDir::homeDirPath() );
00322         defaultIconSize = 0;
00323     }
00324 
00325     int defaultIconSize;
00326     KURL currentURL;
00327 };
00328 
00329 
00330 KURLBar::KURLBar( bool useGlobalItems, QWidget *parent, const char *name, WFlags f )
00331     : QFrame( parent, name, f ),
00332       m_activeItem( 0L ),
00333       m_useGlobal( useGlobalItems ),
00334       m_isModified( false ),
00335       m_isImmutable( false ),
00336       m_listBox( 0L ),
00337       m_iconSize( KIcon::SizeMedium )
00338 {
00339     d = new KURLBarPrivate();
00340 
00341     setListBox( 0L );
00342     setSizePolicy( QSizePolicy( isVertical() ?
00343                                 QSizePolicy::Maximum :
00344                                 QSizePolicy::Preferred,
00345                                 isVertical() ?
00346                                 QSizePolicy::Preferred :
00347                                 QSizePolicy::Maximum ));
00348     QWhatsThis::add(this, i18n("<qt>The <b>Quick Access</b> panel provides easy access to commonly used file locations.<p>"
00349                                "Clicking on one of the shortcut entries will take you to that location.<p>"
00350                                "By right clicking on an entry you can add, edit and remove shortcuts.</qt>"));
00351 }
00352 
00353 KURLBar::~KURLBar()
00354 {
00355     delete d;
00356 }
00357 
00358 KURLBarItem * KURLBar::insertItem(const KURL& url, const QString& description,
00359                                   bool applicationLocal,
00360                                   const QString& icon, KIcon::Group group )
00361 {
00362     KURLBarItem *item = new KURLBarItem(this, url, description, icon, group);
00363     item->setApplicationLocal( applicationLocal );
00364     m_listBox->insertItem( item );
00365     return item;
00366 }
00367 
00368 KURLBarItem * KURLBar::insertDynamicItem(const KURL& url, const QString& description,
00369                                          const QString& icon, KIcon::Group group )
00370 {
00371     KURLBarItem *item = new KURLBarItem(this, url, false, description, icon, group);
00372     m_listBox->insertItem( item );
00373     return item;
00374 }
00375 
00376 void KURLBar::setOrientation( Qt::Orientation orient )
00377 {
00378     m_listBox->setOrientation( orient );
00379     setSizePolicy( QSizePolicy( isVertical() ?
00380                                 QSizePolicy::Maximum :
00381                                 QSizePolicy::Preferred,
00382                                 isVertical() ?
00383                                 QSizePolicy::Preferred :
00384                                 QSizePolicy::Maximum ));
00385 }
00386 
00387 Qt::Orientation KURLBar::orientation() const
00388 {
00389     return m_listBox->orientation();
00390 }
00391 
00392 void KURLBar::setListBox( KURLBarListBox *view )
00393 {
00394     delete m_listBox;
00395 
00396     if ( !view ) {
00397         m_listBox = new KURLBarListBox( this, "urlbar listbox" );
00398         setOrientation( Vertical );
00399     }
00400     else {
00401         m_listBox = view;
00402         if ( m_listBox->parentWidget() != this )
00403             m_listBox->reparent( this, QPoint(0,0) );
00404         m_listBox->resize( width(), height() );
00405     }
00406 
00407     m_listBox->setSelectionMode( KListBox::Single );
00408     paletteChange( palette() );
00409     m_listBox->setFocusPolicy( TabFocus );
00410 
00411     connect( m_listBox, SIGNAL( mouseButtonClicked( int, QListBoxItem *, const QPoint & ) ),
00412              SLOT( slotSelected( int, QListBoxItem * )));
00413     connect( m_listBox, SIGNAL( dropped( QDropEvent * )),
00414              this, SLOT( slotDropped( QDropEvent * )));
00415     connect( m_listBox, SIGNAL( contextMenuRequested( QListBoxItem *,
00416                                                       const QPoint& )),
00417              SLOT( slotContextMenuRequested( QListBoxItem *, const QPoint& )));
00418     connect( m_listBox, SIGNAL( returnPressed( QListBoxItem * ) ),
00419              SLOT( slotSelected( QListBoxItem * ) ));
00420 }
00421 
00422 void KURLBar::setIconSize( int size )
00423 {
00424     if ( size == m_iconSize )
00425         return;
00426 
00427     m_iconSize = size;
00428 
00429     // reload the icons with the new size
00430     KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00431     while ( item ) {
00432         item->setIcon( item->icon(), item->iconGroup() );
00433         item = static_cast<KURLBarItem*>( item->next() );
00434     }
00435 
00436     resize( sizeHint() );
00437     updateGeometry();
00438 }
00439 
00440 void KURLBar::clear()
00441 {
00442     m_listBox->clear();
00443 }
00444 
00445 void KURLBar::resizeEvent( QResizeEvent *e )
00446 {
00447     QFrame::resizeEvent( e );
00448     m_listBox->resize( width(), height() );
00449 }
00450 
00451 void KURLBar::paletteChange( const QPalette & )
00452 {
00453     QPalette pal = palette();
00454     QColor gray = pal.color( QPalette::Normal, QColorGroup::Background );
00455     QColor selectedTextColor = pal.color( QPalette::Normal, QColorGroup::BrightText );
00456     QColor foreground = pal.color( QPalette::Normal, QColorGroup::Foreground );
00457     pal.setColor( QPalette::Normal,   QColorGroup::Base, gray );
00458     pal.setColor( QPalette::Normal,   QColorGroup::HighlightedText, selectedTextColor );
00459     pal.setColor( QPalette::Normal,   QColorGroup::Text, foreground );
00460     pal.setColor( QPalette::Inactive, QColorGroup::Base, gray );
00461     pal.setColor( QPalette::Inactive, QColorGroup::HighlightedText, selectedTextColor );
00462     pal.setColor( QPalette::Inactive, QColorGroup::Text, foreground );
00463 
00464     setPalette( pal );
00465 }
00466 
00467 QSize KURLBar::sizeHint() const
00468 {
00469     return m_listBox->sizeHint();
00470 
00471 #if 0
00472     // this code causes vertical and or horizontal scrollbars appearing
00473     // depending on the text, font, moonphase and earth rotation. Just using
00474     // m_listBox->sizeHint() fixes this (although the widget can then be
00475     // resized to a smaller size so that scrollbars appear).
00476     int w = 0;
00477     int h = 0;
00478     KURLBarItem *item;
00479     bool vertical = isVertical();
00480 
00481     for ( item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00482           item;
00483           item = static_cast<KURLBarItem*>( item->next() ) ) {
00484 
00485         QSize sh = item->sizeHint();
00486 
00487         if ( vertical ) {
00488             w = QMAX( w, sh.width() );
00489             h += sh.height();
00490         }
00491         else {
00492             w += sh.width();
00493             h = QMAX( h, sh.height() );
00494         }
00495     }
00496 
00497 //     if ( vertical && m_listBox->verticalScrollBar()->isVisible() )
00498 //         w += m_listBox->verticalScrollBar()->width();
00499 //     else if ( !vertical && m_listBox->horizontalScrollBar()->isVisible() )
00500 //         h += m_listBox->horizontalScrollBar()->height();
00501 
00502     if ( w == 0 && h == 0 )
00503         return QSize( 100, 200 );
00504     else
00505         return QSize( 6 + w, h );
00506 #endif
00507 }
00508 
00509 QSize KURLBar::minimumSizeHint() const
00510 {
00511     QSize s = sizeHint(); // ###
00512     int w = s.width()  + m_listBox->verticalScrollBar()->width();
00513     int h = s.height() + m_listBox->horizontalScrollBar()->height();
00514     return QSize( w, h );
00515 }
00516 
00517 void KURLBar::slotSelected( int button, QListBoxItem *item )
00518 {
00519     if ( button != Qt::LeftButton )
00520         return;
00521 
00522     slotSelected( item );
00523 }
00524 
00525 void KURLBar::slotSelected( QListBoxItem *item )
00526 {
00527     if ( item && item != m_activeItem )
00528         m_activeItem = static_cast<KURLBarItem*>( item );
00529 
00530     if ( m_activeItem ) {
00531         m_listBox->setCurrentItem( m_activeItem );
00532         emit activated( m_activeItem->url() );
00533     }
00534 }
00535 
00536 void KURLBar::setCurrentItem( const KURL& url )
00537 {
00538     d->currentURL = url;
00539 
00540     QString u = url.url(-1);
00541 
00542     if ( m_activeItem && m_activeItem->url().url(-1) == u )
00543         return;
00544 
00545     bool hasURL = false;
00546     QListBoxItem *item = m_listBox->firstItem();
00547     while ( item ) {
00548         if ( static_cast<KURLBarItem*>( item )->url().url(-1) == u ) {
00549             m_activeItem = static_cast<KURLBarItem*>( item );
00550             m_listBox->setCurrentItem( item );
00551             m_listBox->setSelected( item, true );
00552             hasURL = true;
00553             break;
00554         }
00555         item = item->next();
00556     }
00557 
00558     if ( !hasURL ) {
00559         m_activeItem = 0L;
00560         m_listBox->clearSelection();
00561     }
00562 }
00563 
00564 KURLBarItem * KURLBar::currentItem() const
00565 {
00566     QListBoxItem *item = m_listBox->item( m_listBox->currentItem() );
00567     if ( item )
00568         return static_cast<KURLBarItem *>( item );
00569     return 0L;
00570 }
00571 
00572 KURL KURLBar::currentURL() const
00573 {
00574     KURLBarItem *item = currentItem();
00575     return item ? item->url() : KURL();
00576 }
00577 
00578 void KURLBar::readConfig( KConfig *appConfig, const QString& itemGroup )
00579 {
00580     m_isImmutable = appConfig->groupIsImmutable( itemGroup );
00581     KConfigGroupSaver cs( appConfig, itemGroup );
00582     d->defaultIconSize = m_iconSize;
00583     m_iconSize = appConfig->readNumEntry( "Speedbar IconSize", m_iconSize );
00584 
00585     if ( m_useGlobal ) { // read global items
00586         KConfig *globalConfig = KGlobal::config();
00587         KConfigGroupSaver cs( globalConfig, (QString)(itemGroup +" (Global)"));
00588         int num = globalConfig->readNumEntry( "Number of Entries" );
00589         for ( int i = 0; i < num; i++ ) {
00590             readItem( i, globalConfig, false );
00591         }
00592     }
00593 
00594     // read application local items
00595     int num = appConfig->readNumEntry( "Number of Entries" );
00596     for ( int i = 0; i < num; i++ ) {
00597         readItem( i, appConfig, true );
00598     }
00599 }
00600 
00601 void KURLBar::readItem( int i, KConfig *config, bool applicationLocal )
00602 {
00603     QString number = QString::number( i );
00604     KURL url = KURL::fromPathOrURL( config->readPathEntry( QString("URL_") + number ));
00605     if ( !url.isValid() || !KProtocolInfo::isKnownProtocol( url ))
00606         return; // nothing we could do.
00607 
00608     insertItem( url,
00609                 config->readEntry( QString("Description_") + number ),
00610                 applicationLocal,
00611                 config->readEntry( QString("Icon_") + number ),
00612                 static_cast<KIcon::Group>(
00613                     config->readNumEntry( QString("IconGroup_") + number )) );
00614 }
00615 
00616 void KURLBar::writeConfig( KConfig *config, const QString& itemGroup )
00617 {
00618     KConfigGroupSaver cs1( config, itemGroup );
00619     if(!config->hasDefault("Speedbar IconSize") && m_iconSize == d->defaultIconSize )
00620         config->revertToDefault("Speedbar IconSize");
00621     else
00622         config->writeEntry( "Speedbar IconSize", m_iconSize );
00623 
00624     if ( !m_isModified )
00625         return;
00626 
00627     int i = 0;
00628     int numLocal = 0;
00629     KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00630 
00631     while ( item )
00632     {
00633         if ( item->isPersistent() ) // we only save persistent items
00634         {
00635             if ( item->applicationLocal() )
00636             {
00637                 writeItem( item, numLocal, config, false );
00638                 numLocal++;
00639             }
00640 
00641             i++;
00642         }
00643         item = static_cast<KURLBarItem*>( item->next() );
00644     }
00645     config->writeEntry("Number of Entries", numLocal);
00646 
00647 
00648     // write the global entries to kdeglobals, if any
00649     bool haveGlobalEntries = (i > numLocal);
00650     if ( m_useGlobal && haveGlobalEntries ) {
00651         config->setGroup( itemGroup + " (Global)" );
00652 
00653         int numGlobals = 0;
00654         item = static_cast<KURLBarItem*>( m_listBox->firstItem() );
00655 
00656         while ( item )
00657         {
00658             if ( item->isPersistent() ) // we only save persistent items
00659             {
00660                 if ( !item->applicationLocal() )
00661                 {
00662                     writeItem( item, numGlobals, config, true );
00663                     numGlobals++;
00664                 }
00665             }
00666 
00667             item = static_cast<KURLBarItem*>( item->next() );
00668         }
00669         config->writeEntry("Number of Entries", numGlobals, true, true);
00670     }
00671 
00672     m_isModified = false;
00673 }
00674 
00675 void KURLBar::writeItem( KURLBarItem *item, int i, KConfig *config,
00676                          bool global )
00677 {
00678     if ( !item->isPersistent() )
00679         return;
00680 
00681     QString Description = "Description_";
00682     QString URL = "URL_";
00683     QString Icon = "Icon_";
00684     QString IconGroup = "IconGroup_";
00685 
00686     QString number = QString::number( i );
00687     config->writePathEntry( URL + number, item->url().prettyURL(), true, global );
00688 
00689     config->writeEntry( Description + number, item->description(),true,global);
00690     config->writeEntry( Icon + number, item->icon(), true, global );
00691     config->writeEntry( IconGroup + number, item->iconGroup(), true, global );
00692 }
00693 
00694 
00695 void KURLBar::slotDropped( QDropEvent *e )
00696 {
00697     KURL::List urls;
00698     if ( KURLDrag::decode( e, urls ) ) {
00699         KURL url;
00700         QString description;
00701         QString icon;
00702         bool appLocal = false;
00703 
00704         KURL::List::Iterator it = urls.begin();
00705         for ( ; it != urls.end(); ++it ) {
00706             url = *it;
00707             if ( KURLBarItemDialog::getInformation( m_useGlobal,
00708                                                     url, description, icon,
00709                                                     appLocal, m_iconSize,
00710                                                     this ) ) {
00711                 (void) insertItem( url, description, appLocal, icon );
00712                 m_isModified = true;
00713                 updateGeometry();
00714             }
00715         }
00716     }
00717 }
00718 
00719 void KURLBar::slotContextMenuRequested( QListBoxItem *_item, const QPoint& pos )
00720 {
00721     if (m_isImmutable)
00722         return;
00723 
00724     KURLBarItem *item = dynamic_cast<KURLBarItem*>( _item );
00725 
00726     static const int IconSize   = 10;
00727     static const int AddItem    = 20;
00728     static const int EditItem   = 30;
00729     static const int RemoveItem = 40;
00730 
00731     KURL lastURL = m_activeItem ? m_activeItem->url() : KURL();
00732 
00733     bool smallIcons = m_iconSize < KIcon::SizeMedium;
00734     QPopupMenu *popup = new QPopupMenu();
00735     popup->insertItem( smallIcons ?
00736                        i18n("&Large Icons") : i18n("&Small Icons"),
00737                        IconSize );
00738     popup->insertSeparator();
00739 
00740     if (item != 0L && item->isPersistent())
00741     {
00742         popup->insertItem(SmallIconSet("edit"), i18n("&Edit Entry..."), EditItem);
00743         popup->insertSeparator();
00744     }
00745 
00746     popup->insertItem(SmallIconSet("filenew"), i18n("&Add Entry..."), AddItem);
00747 
00748     if (item != 0L && item->isPersistent())
00749     {
00750         popup->insertItem( SmallIconSet("editdelete"), i18n("&Remove Entry"),
00751                           RemoveItem );
00752     }
00753 
00754     int result = popup->exec( pos );
00755     switch ( result ) {
00756         case IconSize:
00757             setIconSize( smallIcons ? KIcon::SizeMedium : KIcon::SizeSmallMedium );
00758             m_listBox->triggerUpdate( true );
00759             break;
00760         case AddItem:
00761             addNewItem();
00762             break;
00763         case EditItem:
00764             editItem( static_cast<KURLBarItem *>( item ) );
00765             break;
00766         case RemoveItem:
00767             delete item;
00768             m_isModified = true;
00769             break;
00770         default: // abort
00771             break;
00772     }
00773 
00774     // reset current item
00775     m_activeItem = 0L;
00776     setCurrentItem( lastURL );
00777 }
00778 
00779 bool KURLBar::addNewItem()
00780 {
00781     KURLBarItem *item = new KURLBarItem( this, d->currentURL,
00782                                          i18n("Enter a description") );
00783     if ( editItem( item ) ) {
00784         m_listBox->insertItem( item );
00785         return true;
00786     }
00787 
00788     delete item;
00789     return false;
00790 }
00791 
00792 bool KURLBar::editItem( KURLBarItem *item )
00793 {
00794     if ( !item || !item->isPersistent() ) // should never happen tho
00795         return false;
00796 
00797     KURL url            = item->url();
00798     QString description = item->description();
00799     QString icon        = item->icon();
00800     bool appLocal       = item->applicationLocal();
00801 
00802     if ( KURLBarItemDialog::getInformation( m_useGlobal,
00803                                             url, description,
00804                                             icon, appLocal,
00805                                             m_iconSize, this ))
00806     {
00807         item->setURL( url );
00808         item->setDescription( description );
00809         item->setIcon( icon );
00810         item->setApplicationLocal( appLocal );
00811         m_listBox->triggerUpdate( true );
00812         m_isModified = true;
00813         updateGeometry();
00814         return true;
00815     }
00816 
00817     return false;
00818 }
00819 
00822 
00823 
00824 KURLBarListBox::KURLBarListBox( QWidget *parent, const char *name )
00825     : KListBox( parent, name )
00826 {
00827     m_toolTip = new KURLBarToolTip( this );
00828     setAcceptDrops( true );
00829     viewport()->setAcceptDrops( true );
00830 }
00831 
00832 KURLBarListBox::~KURLBarListBox()
00833 {
00834     delete m_toolTip;
00835 }
00836 
00837 void KURLBarListBox::paintEvent( QPaintEvent* )
00838 {
00839     QPainter p(this);
00840     p.setPen( colorGroup().mid() );
00841     p.drawRect( 0, 0, width(), height() );
00842 }
00843 
00844 QDragObject * KURLBarListBox::dragObject()
00845 {
00846     KURL::List urls;
00847     KURLBarItem *item = static_cast<KURLBarItem*>( firstItem() );
00848 
00849     while ( item ) {
00850         if ( item->isSelected() )
00851             urls.append( item->url() );
00852         item = static_cast<KURLBarItem*>( item->next() );
00853     }
00854 
00855     if ( !urls.isEmpty() ) // ### use custom drag-object with description etc.?
00856         return new KURLDrag( urls, this, "urlbar drag" );
00857 
00858     return 0L;
00859 }
00860 
00861 void KURLBarListBox::contentsDragEnterEvent( QDragEnterEvent *e )
00862 {
00863     e->accept( KURLDrag::canDecode( e ));
00864 }
00865 
00866 void KURLBarListBox::contentsDropEvent( QDropEvent *e )
00867 {
00868     emit dropped( e );
00869 }
00870 
00871 void KURLBarListBox::contextMenuEvent( QContextMenuEvent *e )
00872 {
00873     if (e)
00874     {
00875         emit contextMenuRequested( itemAt( e->globalPos() ), e->globalPos() );
00876         e->consume(); // Consume the event to avoid multiple contextMenuEvent calls...
00877     }
00878 }
00879 
00880 void KURLBarListBox::setOrientation( Qt::Orientation orient )
00881 {
00882     if ( orient == Vertical ) {
00883         setColumnMode( 1 );
00884         setRowMode( Variable );
00885     }
00886     else {
00887         setRowMode( 1 );
00888         setColumnMode( Variable );
00889     }
00890 
00891     m_orientation = orient;
00892 }
00893 
00896 
00897 
00898 bool KURLBarItemDialog::getInformation( bool allowGlobal, KURL& url,
00899                                         QString& description, QString& icon,
00900                                         bool& appLocal, int iconSize,
00901                                         QWidget *parent )
00902 {
00903     KURLBarItemDialog *dialog = new KURLBarItemDialog( allowGlobal, url,
00904                                                        description, icon,
00905                                                        appLocal,
00906                                                        iconSize, parent );
00907     if ( dialog->exec() == QDialog::Accepted ) {
00908         // set the return parameters
00909         url         = dialog->url();
00910         description = dialog->description();
00911         icon        = dialog->icon();
00912         appLocal    = dialog->applicationLocal();
00913 
00914         delete dialog;
00915         return true;
00916     }
00917 
00918     delete dialog;
00919     return false;
00920 }
00921 
00922 KURLBarItemDialog::KURLBarItemDialog( bool allowGlobal, const KURL& url,
00923                                       const QString& description,
00924                                       QString icon, bool appLocal,
00925                                       int iconSize,
00926                                       QWidget *parent, const char *name )
00927     : KDialogBase( parent, name, true,
00928                    i18n("Edit Quick Access Entry"), Ok | Cancel, Ok, true )
00929 {
00930     QVBox *box = new QVBox( this );
00931     QString text = i18n("<qt><b>Please provide a description, URL and icon for this Quick Access entry.</b></br></qt>");
00932     QLabel *label = new QLabel( text, box );
00933     box->setSpacing( spacingHint() );
00934 
00935     QGrid *grid = new QGrid( 2, box );
00936     grid->setSpacing( spacingHint() );
00937 
00938     QString whatsThisText = i18n("<qt>This is the text that will appear in the Quick Access panel.<p>"
00939                                  "The description should consist of one or two words "
00940                                  "that will help you remember what this entry refers to.</qt>");
00941     label = new QLabel( i18n("&Description:"), grid );
00942     m_edit = new KLineEdit( grid, "description edit" );
00943     m_edit->setText( description.isEmpty() ? url.fileName() : description );
00944     label->setBuddy( m_edit );
00945     QWhatsThis::add( label, whatsThisText );
00946     QWhatsThis::add( m_edit, whatsThisText );
00947 
00948     whatsThisText = i18n("<qt>This is the location associated with the entry. Any valid URL may be used. For example:<p>"
00949                          "%1<br>http://www.kde.org<br>ftp://ftp.kde.org/pub/kde/stable<p>"
00950                          "By clicking on the button next to the text edit box you can browse to an "
00951                          "appropriate URL.</qt>").arg(QDir::homeDirPath());
00952     label = new QLabel( i18n("&URL:"), grid );
00953     m_urlEdit = new KURLRequester( url.prettyURL(), grid );
00954     m_urlEdit->setMode( KFile::Directory );
00955     label->setBuddy( m_urlEdit );
00956     QWhatsThis::add( label, whatsThisText );
00957     QWhatsThis::add( m_urlEdit, whatsThisText );
00958 
00959     whatsThisText = i18n("<qt>This is the icon that will appear in the Quick Access panel.<p>"
00960                          "Click on the button to select a different icon.</qt>");
00961     label = new QLabel( i18n("Choose an &icon:"), grid );
00962     m_iconButton = new KIconButton( grid, "icon button" );
00963     m_iconButton->setIconSize( iconSize );
00964     if ( icon.isEmpty() )
00965         icon = KMimeType::iconForURL( url );
00966     m_iconButton->setIcon( icon );
00967     label->setBuddy( m_iconButton );
00968     QWhatsThis::add( label, whatsThisText );
00969     QWhatsThis::add( m_iconButton, whatsThisText );
00970 
00971     if ( allowGlobal ) {
00972         QString appName;
00973         if ( KGlobal::instance()->aboutData() )
00974             appName = KGlobal::instance()->aboutData()->programName();
00975         if ( appName.isEmpty() )
00976             appName = QString::fromLatin1( KGlobal::instance()->instanceName() );
00977         m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)").arg( appName ), box );
00978         m_appLocal->setChecked( appLocal );
00979         QWhatsThis::add( m_appLocal,
00980                          i18n("<qt>Select this setting if you want this "
00981                               "entry to show only when using the current application (%1).<p>"
00982                               "If this setting is not selected, the entry will be available in all "
00983                               "applications.</qt>")
00984                               .arg(appName));
00985     }
00986     else
00987         m_appLocal = 0L;
00988     connect(m_urlEdit->lineEdit(),SIGNAL(textChanged ( const QString & )),this,SLOT(urlChanged(const QString & )));
00989     m_edit->setFocus();
00990     setMainWidget( box );
00991 }
00992 
00993 KURLBarItemDialog::~KURLBarItemDialog()
00994 {
00995 }
00996 
00997 void KURLBarItemDialog::urlChanged(const QString & text )
00998 {
00999     enableButtonOK( !text.isEmpty() );
01000 }
01001 
01002 KURL KURLBarItemDialog::url() const
01003 {
01004     QString text = m_urlEdit->url();
01005     KURL u;
01006     if ( text.at(0) == '/' )
01007         u.setPath( text );
01008     else
01009         u = text;
01010 
01011     return u;
01012 }
01013 
01014 QString KURLBarItemDialog::description() const
01015 {
01016     return m_edit->text();
01017 }
01018 
01019 QString KURLBarItemDialog::icon() const
01020 {
01021     return m_iconButton->icon();
01022 }
01023 
01024 bool KURLBarItemDialog::applicationLocal() const
01025 {
01026     if ( !m_appLocal )
01027         return true;
01028 
01029     return m_appLocal->isChecked();
01030 }
01031 
01032 void KURLBarItem::virtual_hook( int, void* )
01033 { /*BASE::virtual_hook( id, data );*/ }
01034 
01035 void KURLBar::virtual_hook( int, void* )
01036 { /*BASE::virtual_hook( id, data );*/ }
01037 
01038 void KURLBarListBox::virtual_hook( int id, void* data )
01039 { KListBox::virtual_hook( id, data ); }
01040 
01041 
01042 #include "kurlbar.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 28 01:36:59 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003