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 #include <kstandarddirs.h> 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( false ) 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( false ) 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 (void) insertItem( *it, description, appLocal, icon ); 00707 if( !appLocal) 00708 createItemOnMDKMerge( url, description, icon ); 00709 m_isModified = true; 00710 updateGeometry(); 00711 } 00712 } 00713 } 00714 00715 void KURLBar::slotContextMenuRequested( QListBoxItem *_item, const QPoint& pos ) 00716 { 00717 if (m_isImmutable) 00718 return; 00719 00720 KURLBarItem *item = dynamic_cast<KURLBarItem*>( _item ); 00721 00722 static const int IconSize = 10; 00723 static const int AddItem = 20; 00724 static const int EditItem = 30; 00725 static const int RemoveItem = 40; 00726 00727 KURL lastURL = m_activeItem ? m_activeItem->url() : KURL(); 00728 00729 bool smallIcons = m_iconSize < KIcon::SizeMedium; 00730 QPopupMenu *popup = new QPopupMenu(); 00731 popup->insertItem( smallIcons ? 00732 i18n("&Large Icons") : i18n("&Small Icons"), 00733 IconSize ); 00734 popup->insertSeparator(); 00735 00736 if (item != 0L && item->isPersistent()) 00737 { 00738 popup->insertItem(SmallIconSet("edit"), i18n("&Edit Entry..."), EditItem); 00739 popup->insertSeparator(); 00740 } 00741 00742 popup->insertItem(SmallIconSet("filenew"), i18n("&Add Entry..."), AddItem); 00743 00744 if (item != 0L && item->isPersistent()) 00745 { 00746 popup->insertItem( SmallIconSet("editdelete"), i18n("&Remove Entry"), 00747 RemoveItem ); 00748 } 00749 00750 int result = popup->exec( pos ); 00751 switch ( result ) { 00752 case IconSize: 00753 setIconSize( smallIcons ? KIcon::SizeMedium : KIcon::SizeSmallMedium ); 00754 m_listBox->triggerUpdate( true ); 00755 break; 00756 case AddItem: 00757 addNewItem(); 00758 break; 00759 case EditItem: 00760 editItem( static_cast<KURLBarItem *>( item ) ); 00761 break; 00762 case RemoveItem: 00763 delete item; 00764 m_isModified = true; 00765 break; 00766 default: // abort 00767 break; 00768 } 00769 00770 // reset current item 00771 m_activeItem = 0L; 00772 setCurrentItem( lastURL ); 00773 } 00774 00775 bool KURLBar::addNewItem() 00776 { 00777 KURLBarItem *item = new KURLBarItem( this, d->currentURL, 00778 i18n("Enter a description") ); 00779 if ( editItem( item ) ) { 00780 m_listBox->insertItem( item ); 00781 return true; 00782 } 00783 00784 delete item; 00785 return false; 00786 } 00787 00788 bool KURLBar::editItem( KURLBarItem *item ) 00789 { 00790 if ( !item || !item->isPersistent() ) // should never happen tho 00791 return false; 00792 00793 KURL url = item->url(); 00794 QString description = item->description(); 00795 QString icon = item->icon(); 00796 bool appLocal = item->applicationLocal(); 00797 00798 QString oldDescription = description; 00799 00800 if ( KURLBarItemDialog::getInformation( m_useGlobal, 00801 url, description, 00802 icon, appLocal, 00803 m_iconSize, this )) 00804 { 00805 if( !appLocal) 00806 createItemOnMDKMerge( url, description, icon ); 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 00820 void KURLBar::createItemOnMDKMerge( const KURL &url, const QString & description, const QString &icon ) 00821 { 00822 if ( !QFile::exists( KStandardDirs::mandrake_merge_directory() ) ) 00823 KStandardDirs::makeDir( QFile::encodeName( KStandardDirs::mandrake_merge_directory() ) ); 00824 KDesktopFile df(KStandardDirs::mandrake_merge_directory() + description); 00825 df.writeEntry( "Name", description ); 00826 df.writeEntry( "Icon", icon ); 00827 df.writeEntry( "Type", "Link" ); 00828 df.writeEntry( "URL", url.url() ); 00829 df.sync(); 00830 } 00831 00834 00835 00836 KURLBarListBox::KURLBarListBox( QWidget *parent, const char *name ) 00837 : KListBox( parent, name ) 00838 { 00839 m_toolTip = new KURLBarToolTip( this ); 00840 setAcceptDrops( true ); 00841 viewport()->setAcceptDrops( true ); 00842 } 00843 00844 KURLBarListBox::~KURLBarListBox() 00845 { 00846 delete m_toolTip; 00847 } 00848 00849 void KURLBarListBox::paintEvent( QPaintEvent* ) 00850 { 00851 QPainter p(this); 00852 p.setPen( colorGroup().mid() ); 00853 p.drawRect( 0, 0, width(), height() ); 00854 } 00855 00856 QDragObject * KURLBarListBox::dragObject() 00857 { 00858 KURL::List urls; 00859 KURLBarItem *item = static_cast<KURLBarItem*>( firstItem() ); 00860 00861 while ( item ) { 00862 if ( item->isSelected() ) 00863 urls.append( item->url() ); 00864 item = static_cast<KURLBarItem*>( item->next() ); 00865 } 00866 00867 if ( !urls.isEmpty() ) // ### use custom drag-object with description etc.? 00868 return new KURLDrag( urls, this, "urlbar drag" ); 00869 00870 return 0L; 00871 } 00872 00873 void KURLBarListBox::contentsDragEnterEvent( QDragEnterEvent *e ) 00874 { 00875 e->accept( KURLDrag::canDecode( e )); 00876 } 00877 00878 void KURLBarListBox::contentsDropEvent( QDropEvent *e ) 00879 { 00880 emit dropped( e ); 00881 } 00882 00883 void KURLBarListBox::contextMenuEvent( QContextMenuEvent *e ) 00884 { 00885 if (e) 00886 { 00887 emit contextMenuRequested( itemAt( e->globalPos() ), e->globalPos() ); 00888 e->consume(); // Consume the event to avoid multiple contextMenuEvent calls... 00889 } 00890 } 00891 00892 void KURLBarListBox::setOrientation( Qt::Orientation orient ) 00893 { 00894 if ( orient == Vertical ) { 00895 setColumnMode( 1 ); 00896 setRowMode( Variable ); 00897 } 00898 else { 00899 setRowMode( 1 ); 00900 setColumnMode( Variable ); 00901 } 00902 00903 m_orientation = orient; 00904 } 00905 00908 00909 00910 bool KURLBarItemDialog::getInformation( bool allowGlobal, KURL& url, 00911 QString& description, QString& icon, 00912 bool& appLocal, int iconSize, 00913 QWidget *parent ) 00914 { 00915 KURLBarItemDialog *dialog = new KURLBarItemDialog( allowGlobal, url, 00916 description, icon, 00917 appLocal, 00918 iconSize, parent ); 00919 if ( dialog->exec() == QDialog::Accepted ) { 00920 // set the return parameters 00921 url = dialog->url(); 00922 description = dialog->description(); 00923 icon = dialog->icon(); 00924 appLocal = dialog->applicationLocal(); 00925 00926 delete dialog; 00927 return true; 00928 } 00929 00930 delete dialog; 00931 return false; 00932 } 00933 00934 KURLBarItemDialog::KURLBarItemDialog( bool allowGlobal, const KURL& url, 00935 const QString& description, 00936 QString icon, bool appLocal, 00937 int iconSize, 00938 QWidget *parent, const char *name ) 00939 : KDialogBase( parent, name, true, 00940 i18n("Edit Quick Access Entry"), Ok | Cancel, Ok, true ) 00941 { 00942 QVBox *box = new QVBox( this ); 00943 QString text = i18n("<qt><b>Please provide a description, URL and icon for this Quick Access entry.</b></br></qt>"); 00944 QLabel *label = new QLabel( text, box ); 00945 box->setSpacing( spacingHint() ); 00946 00947 QGrid *grid = new QGrid( 2, box ); 00948 grid->setSpacing( spacingHint() ); 00949 00950 QString whatsThisText = i18n("<qt>This is the text that will appear in the Quick Access panel.<p>" 00951 "The description should consist of one or two words " 00952 "that will help you remember what this entry refers to.</qt>"); 00953 label = new QLabel( i18n("&Description:"), grid ); 00954 m_edit = new KLineEdit( grid, "description edit" ); 00955 m_edit->setText( description.isEmpty() ? url.fileName() : description ); 00956 label->setBuddy( m_edit ); 00957 QWhatsThis::add( label, whatsThisText ); 00958 QWhatsThis::add( m_edit, whatsThisText ); 00959 00960 whatsThisText = i18n("<qt>This is the location associated with the entry. Any valid URL may be used. For example:<p>" 00961 "%1<br>http://www.kde.org<br>ftp://ftp.kde.org/pub/kde/stable<p>" 00962 "By clicking on the button next to the text edit box you can browse to an " 00963 "appropriate URL.</qt>").arg(QDir::homeDirPath()); 00964 label = new QLabel( i18n("&URL:"), grid ); 00965 m_urlEdit = new KURLRequester( url.prettyURL(), grid ); 00966 m_urlEdit->setMode( KFile::Directory ); 00967 label->setBuddy( m_urlEdit ); 00968 QWhatsThis::add( label, whatsThisText ); 00969 QWhatsThis::add( m_urlEdit, whatsThisText ); 00970 00971 whatsThisText = i18n("<qt>This is the icon that will appear in the Quick Access panel.<p>" 00972 "Click on the button to select a different icon.</qt>"); 00973 label = new QLabel( i18n("Choose an &icon:"), grid ); 00974 m_iconButton = new KIconButton( grid, "icon button" ); 00975 m_iconButton->setIconSize( iconSize ); 00976 if ( icon.isEmpty() ) 00977 icon = KMimeType::iconForURL( url ); 00978 m_iconButton->setIcon( icon ); 00979 label->setBuddy( m_iconButton ); 00980 QWhatsThis::add( label, whatsThisText ); 00981 QWhatsThis::add( m_iconButton, whatsThisText ); 00982 00983 if ( allowGlobal ) { 00984 QString appName; 00985 if ( KGlobal::instance()->aboutData() ) 00986 appName = KGlobal::instance()->aboutData()->programName(); 00987 if ( appName.isEmpty() ) 00988 appName = QString::fromLatin1( KGlobal::instance()->instanceName() ); 00989 m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)").arg( appName ), box ); 00990 m_appLocal->setChecked( appLocal ); 00991 QWhatsThis::add( m_appLocal, 00992 i18n("<qt>Select this setting if you want this " 00993 "entry to show only when using the current application (%1).<p>" 00994 "If this setting is not selected, the entry will be available in all " 00995 "applications.</qt>") 00996 .arg(appName)); 00997 } 00998 else 00999 m_appLocal = 0L; 01000 connect(m_urlEdit->lineEdit(),SIGNAL(textChanged ( const QString & )),this,SLOT(urlChanged(const QString & ))); 01001 m_edit->setFocus(); 01002 setMainWidget( box ); 01003 } 01004 01005 KURLBarItemDialog::~KURLBarItemDialog() 01006 { 01007 } 01008 01009 void KURLBarItemDialog::urlChanged(const QString & text ) 01010 { 01011 enableButtonOK( !text.isEmpty() ); 01012 } 01013 01014 KURL KURLBarItemDialog::url() const 01015 { 01016 QString text = m_urlEdit->url(); 01017 KURL u; 01018 if ( text.at(0) == '/' ) 01019 u.setPath( text ); 01020 else 01021 u = text; 01022 01023 return u; 01024 } 01025 01026 QString KURLBarItemDialog::description() const 01027 { 01028 return m_edit->text(); 01029 } 01030 01031 QString KURLBarItemDialog::icon() const 01032 { 01033 return m_iconButton->icon(); 01034 } 01035 01036 bool KURLBarItemDialog::applicationLocal() const 01037 { 01038 if ( !m_appLocal ) 01039 return true; 01040 01041 return m_appLocal->isChecked(); 01042 } 01043 01044 void KURLBarItem::virtual_hook( int, void* ) 01045 { /*BASE::virtual_hook( id, data );*/ } 01046 01047 void KURLBar::virtual_hook( int, void* ) 01048 { /*BASE::virtual_hook( id, data );*/ } 01049 01050 void KURLBarListBox::virtual_hook( int id, void* data ) 01051 { KListBox::virtual_hook( id, data ); } 01052 01053 01054 #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 Tue Apr 12 23:09:29 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003