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., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include <unistd.h> 00020 00021 #include <tqapplication.h> 00022 #include <tqcheckbox.h> 00023 #include <tqdrawutil.h> 00024 #include <tqfontmetrics.h> 00025 #include <tqlabel.h> 00026 #include <tqgrid.h> 00027 #include <tqpainter.h> 00028 #include <tqpopupmenu.h> 00029 #include <tqstyle.h> 00030 #include <tqvbox.h> 00031 #include <tqwhatsthis.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 <kio/global.h> 00048 #include <kio/netaccess.h> 00049 00050 #include "kurlbar.h" 00051 00056 class KURLBarToolTip : public TQToolTip 00057 { 00058 public: 00059 KURLBarToolTip( TQListBox *view ) : TQToolTip( view ), m_view( view ) {} 00060 00061 protected: 00062 virtual void maybeTip( const TQPoint& point ) { 00063 TQListBoxItem *item = m_view->itemAt( point ); 00064 if ( item ) { 00065 TQString text = static_cast<KURLBarItem*>( item )->toolTip(); 00066 if ( !text.isEmpty() ) 00067 tip( m_view->itemRect( item ), text ); 00068 } 00069 } 00070 00071 private: 00072 TQListBox *m_view; 00073 }; 00074 00075 00078 00079 class KURLBarItem::KURLBarItemPrivate 00080 { 00081 public: 00082 KURLBarItemPrivate() 00083 { 00084 isPersistent = true; 00085 } 00086 00087 bool isPersistent; 00088 }; 00089 00090 KURLBarItem::KURLBarItem( KURLBar *parent, 00091 const KURL& url, bool persistent, const TQString& description, 00092 const TQString& icon, KIcon::Group group ) 00093 : TQListBoxPixmap( KIconLoader::unknown() /*, parent->listBox()*/ ), 00094 m_url( url ), 00095 m_pixmap( 0L ), 00096 m_parent( parent ), 00097 m_appLocal( false ) 00098 { 00099 init( icon, group, description, persistent ); 00100 } 00101 00102 KURLBarItem::KURLBarItem( KURLBar *parent, 00103 const KURL& url, const TQString& description, 00104 const TQString& icon, KIcon::Group group ) 00105 : TQListBoxPixmap( KIconLoader::unknown() /*, parent->listBox()*/ ), 00106 m_url( url ), 00107 m_pixmap( 0L ), 00108 m_parent( parent ), 00109 m_appLocal( false ) 00110 { 00111 init( icon, group, description, true /*persistent*/ ); 00112 } 00113 00114 void KURLBarItem::init( const TQString& icon, KIcon::Group group, 00115 const TQString& description, bool persistent ) 00116 { 00117 d = new KURLBarItemPrivate; 00118 d->isPersistent = persistent; 00119 00120 setCustomHighlighting( true ); 00121 setIcon( icon, group ); 00122 setDescription( description ); 00123 } 00124 00125 KURLBarItem::~KURLBarItem() 00126 { 00127 delete d; 00128 } 00129 00130 void KURLBarItem::setURL( const KURL& url ) 00131 { 00132 m_url = url; 00133 if ( m_description.isEmpty() ) 00134 setText( url.fileName() ); 00135 } 00136 00137 void KURLBarItem::setIcon( const TQString& icon, KIcon::Group group ) 00138 { 00139 m_icon = icon; 00140 m_group = group; 00141 00142 if ( icon.isEmpty() ) 00143 m_pixmap = KMimeType::pixmapForURL( m_url, 0, group, iconSize() ); 00144 else 00145 m_pixmap = KGlobal::iconLoader()->loadIcon( icon, group, iconSize(), 00146 KIcon::DefaultState ); 00147 } 00148 00149 void KURLBarItem::setDescription( const TQString& desc ) 00150 { 00151 m_description = desc; 00152 setText( desc.isEmpty() ? m_url.fileName() : desc ); 00153 } 00154 00155 void KURLBarItem::setApplicationLocal( bool local ) 00156 { 00157 if ( !local && !isPersistent() ) 00158 { 00159 kdWarning() << "KURLBar: dynamic (non-persistent) items can not be global." << endl; 00160 return; 00161 } 00162 00163 m_appLocal = local; 00164 } 00165 00166 void KURLBarItem::setToolTip( const TQString& tip ) 00167 { 00168 m_toolTip = tip; 00169 } 00170 00171 TQString KURLBarItem::toolTip() const 00172 { 00173 return m_toolTip.isEmpty() ? m_url.prettyURL() : m_toolTip; 00174 } 00175 00176 int KURLBarItem::iconSize() const 00177 { 00178 return m_parent->iconSize(); 00179 } 00180 00181 void KURLBarItem::paint( TQPainter *p ) 00182 { 00183 TQListBox *box = listBox(); 00184 int w = width( box ); 00185 static const int margin = KDialog::spacingHint(); 00186 00187 // draw sunken selection 00188 if ( isCurrent() || isSelected() ) { 00189 int h = height( box ); 00190 00191 TQBrush brush = box->colorGroup().brush( TQColorGroup::Highlight ); 00192 p->fillRect( 0, 0, w, h, brush ); 00193 TQPen pen = p->pen(); 00194 TQPen oldPen = pen; 00195 pen.setColor( box->colorGroup().mid() ); 00196 p->setPen( pen ); 00197 00198 p->drawPoint( 0, 0 ); 00199 p->drawPoint( 0, h - 1 ); 00200 p->drawPoint( w - 1, 0 ); 00201 p->drawPoint( w - 1, h - 1 ); 00202 00203 p->setPen( oldPen ); 00204 } 00205 00206 if ( m_parent->iconSize() < KIcon::SizeMedium ) { 00207 // small icon -> draw icon next to text 00208 00209 // ### mostly cut & paste of TQListBoxPixmap::paint() until Qt 3.1 00210 // (where it will properly use pixmap() instead of the internal pixmap) 00211 const TQPixmap *pm = pixmap(); 00212 int yPos = QMAX( 0, (height(box) - pm->height())/2 ); 00213 00214 p->drawPixmap( margin, yPos, *pm ); 00215 if ( !text().isEmpty() ) { 00216 TQFontMetrics fm = p->fontMetrics(); 00217 if ( pm->height() < fm.height() ) { 00218 yPos = fm.ascent() + fm.leading()/2; 00219 } 00220 else { 00221 yPos = height(box)/2 - fm.height()/2 + fm.ascent() - margin; 00222 } 00223 00224 yPos += margin; 00225 int stringWidth = box->width() - pm->width() - 2 - (margin * 2); 00226 TQString visibleText = KStringHandler::rPixelSqueeze( text(), fm, stringWidth ); 00227 int xPos = pm->width() + margin + 2; 00228 00229 if ( isCurrent() || isSelected() ) { 00230 p->setPen( box->colorGroup().highlight().dark(115) ); 00231 p->drawText( xPos + ( TQApplication::reverseLayout() ? -1 : 1), 00232 yPos + 1, visibleText ); 00233 p->setPen( box->colorGroup().highlightedText() ); 00234 } 00235 00236 p->drawText( xPos, yPos, visibleText ); 00237 } 00238 // end cut & paste (modulo pixmap centering) 00239 } 00240 00241 else { 00242 // big icons -> draw text below icon 00243 int y = margin; 00244 const TQPixmap *pm = pixmap(); 00245 00246 if ( !pm->isNull() ) { 00247 int x = (w - pm->width()) / 2; 00248 x = QMAX( x, margin ); 00249 p->drawPixmap( x, y, *pm ); 00250 } 00251 00252 if ( !text().isEmpty() ) { 00253 TQFontMetrics fm = p->fontMetrics(); 00254 y += pm->height() + fm.height() - fm.descent(); 00255 00256 int stringWidth = box->width() - (margin * 2); 00257 TQString visibleText = KStringHandler::rPixelSqueeze( text(), fm, stringWidth ); 00258 int x = (w - fm.width( visibleText )) / 2; 00259 x = QMAX( x, margin ); 00260 00261 if ( isCurrent() || isSelected() ) { 00262 p->setPen( box->colorGroup().highlight().dark(115) ); 00263 p->drawText( x + ( TQApplication::reverseLayout() ? -1 : 1), 00264 y + 1, visibleText ); 00265 p->setPen( box->colorGroup().highlightedText() ); 00266 } 00267 00268 p->drawText( x, y, visibleText ); 00269 } 00270 } 00271 } 00272 00273 TQSize KURLBarItem::sizeHint() const 00274 { 00275 int wmin = 0; 00276 int hmin = 0; 00277 const KURLBarListBox *lb =static_cast<const KURLBarListBox*>(listBox()); 00278 00279 if ( m_parent->iconSize() < KIcon::SizeMedium ) { 00280 wmin = TQListBoxPixmap::width( lb ) + KDialog::spacingHint() * 2; 00281 hmin = TQListBoxPixmap::height( lb ) + KDialog::spacingHint() * 2; 00282 } 00283 else { 00284 wmin = QMAX(lb->fontMetrics().width(text()), pixmap()->width()) + KDialog::spacingHint() * 2; 00285 hmin = lb->fontMetrics().lineSpacing() + pixmap()->height() + KDialog::spacingHint() * 2; 00286 } 00287 00288 if ( lb->isVertical() ) 00289 wmin = QMIN( wmin, lb->viewport()->sizeHint().width() ); 00290 else 00291 hmin = QMIN( hmin, lb->viewport()->sizeHint().height() ); 00292 00293 return TQSize( wmin, hmin ); 00294 } 00295 00296 int KURLBarItem::width( const TQListBox *lb ) const 00297 { 00298 if ( static_cast<const KURLBarListBox *>( lb )->isVertical() ) 00299 return QMAX( sizeHint().width(), lb->viewport()->width() ); 00300 else 00301 return sizeHint().width(); 00302 } 00303 00304 int KURLBarItem::height( const TQListBox *lb ) const 00305 { 00306 if ( static_cast<const KURLBarListBox *>( lb )->isVertical() ) 00307 return sizeHint().height(); 00308 else 00309 return QMAX( sizeHint().height(), lb->viewport()->height() ); 00310 } 00311 00312 bool KURLBarItem::isPersistent() const 00313 { 00314 return d->isPersistent; 00315 } 00316 00319 00320 class KURLBar::KURLBarPrivate 00321 { 00322 public: 00323 KURLBarPrivate() 00324 { 00325 currentURL.setPath( TQDir::homeDirPath() ); 00326 defaultIconSize = 0; 00327 } 00328 00329 int defaultIconSize; 00330 KURL currentURL; 00331 }; 00332 00333 00334 KURLBar::KURLBar( bool useGlobalItems, TQWidget *parent, const char *name, WFlags f ) 00335 : TQFrame( parent, name, f ), 00336 m_activeItem( 0L ), 00337 m_useGlobal( useGlobalItems ), 00338 m_isModified( false ), 00339 m_isImmutable( false ), 00340 m_listBox( 0L ), 00341 m_iconSize( KIcon::SizeMedium ) 00342 { 00343 d = new KURLBarPrivate(); 00344 00345 setListBox( 0L ); 00346 setSizePolicy( TQSizePolicy( isVertical() ? 00347 TQSizePolicy::Maximum : 00348 TQSizePolicy::Preferred, 00349 isVertical() ? 00350 TQSizePolicy::Preferred : 00351 TQSizePolicy::Maximum )); 00352 TQWhatsThis::add(this, i18n("<qt>The <b>Quick Access</b> panel provides easy access to commonly used file locations.<p>" 00353 "Clicking on one of the shortcut entries will take you to that location.<p>" 00354 "By right clicking on an entry you can add, edit and remove shortcuts.</qt>")); 00355 } 00356 00357 KURLBar::~KURLBar() 00358 { 00359 delete d; 00360 } 00361 00362 KURLBarItem * KURLBar::insertItem(const KURL& url, const TQString& description, 00363 bool applicationLocal, 00364 const TQString& icon, KIcon::Group group ) 00365 { 00366 KURLBarItem *item = new KURLBarItem(this, url, description, icon, group); 00367 item->setApplicationLocal( applicationLocal ); 00368 m_listBox->insertItem( item ); 00369 return item; 00370 } 00371 00372 KURLBarItem * KURLBar::insertDynamicItem(const KURL& url, const TQString& description, 00373 const TQString& icon, KIcon::Group group ) 00374 { 00375 KURLBarItem *item = new KURLBarItem(this, url, false, description, icon, group); 00376 m_listBox->insertItem( item ); 00377 return item; 00378 } 00379 00380 void KURLBar::setOrientation( Qt::Orientation orient ) 00381 { 00382 m_listBox->setOrientation( orient ); 00383 setSizePolicy( TQSizePolicy( isVertical() ? 00384 TQSizePolicy::Maximum : 00385 TQSizePolicy::Preferred, 00386 isVertical() ? 00387 TQSizePolicy::Preferred : 00388 TQSizePolicy::Maximum )); 00389 } 00390 00391 Qt::Orientation KURLBar::orientation() const 00392 { 00393 return m_listBox->orientation(); 00394 } 00395 00396 void KURLBar::setListBox( KURLBarListBox *view ) 00397 { 00398 delete m_listBox; 00399 00400 if ( !view ) { 00401 m_listBox = new KURLBarListBox( this, "urlbar listbox" ); 00402 setOrientation( Qt::Vertical ); 00403 } 00404 else { 00405 m_listBox = view; 00406 if ( m_listBox->parentWidget() != this ) 00407 m_listBox->reparent( this, TQPoint(0,0) ); 00408 m_listBox->resize( width(), height() ); 00409 } 00410 00411 m_listBox->setSelectionMode( KListBox::Single ); 00412 paletteChange( palette() ); 00413 m_listBox->setFocusPolicy( TQ_TabFocus ); 00414 00415 connect( m_listBox, TQT_SIGNAL( mouseButtonClicked( int, TQListBoxItem *, const TQPoint & ) ), 00416 TQT_SLOT( slotSelected( int, TQListBoxItem * ))); 00417 connect( m_listBox, TQT_SIGNAL( dropped( TQDropEvent * )), 00418 this, TQT_SLOT( slotDropped( TQDropEvent * ))); 00419 connect( m_listBox, TQT_SIGNAL( contextMenuRequested( TQListBoxItem *, 00420 const TQPoint& )), 00421 TQT_SLOT( slotContextMenuRequested( TQListBoxItem *, const TQPoint& ))); 00422 connect( m_listBox, TQT_SIGNAL( returnPressed( TQListBoxItem * ) ), 00423 TQT_SLOT( slotSelected( TQListBoxItem * ) )); 00424 } 00425 00426 void KURLBar::setIconSize( int size ) 00427 { 00428 if ( size == m_iconSize ) 00429 return; 00430 00431 m_iconSize = size; 00432 00433 // reload the icons with the new size 00434 KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() ); 00435 while ( item ) { 00436 item->setIcon( item->icon(), item->iconGroup() ); 00437 item = static_cast<KURLBarItem*>( item->next() ); 00438 } 00439 00440 resize( sizeHint() ); 00441 updateGeometry(); 00442 } 00443 00444 void KURLBar::clear() 00445 { 00446 m_listBox->clear(); 00447 } 00448 00449 void KURLBar::resizeEvent( TQResizeEvent *e ) 00450 { 00451 TQFrame::resizeEvent( e ); 00452 m_listBox->resize( width(), height() ); 00453 } 00454 00455 void KURLBar::paletteChange( const TQPalette & ) 00456 { 00457 TQPalette pal = palette(); 00458 TQColor gray = pal.color( TQPalette::Normal, TQColorGroup::Background ); 00459 TQColor selectedTextColor = pal.color( TQPalette::Normal, TQColorGroup::BrightText ); 00460 TQColor foreground = pal.color( TQPalette::Normal, TQColorGroup::Foreground ); 00461 pal.setColor( TQPalette::Normal, TQColorGroup::Base, gray ); 00462 pal.setColor( TQPalette::Normal, TQColorGroup::HighlightedText, selectedTextColor ); 00463 pal.setColor( TQPalette::Normal, TQColorGroup::Text, foreground ); 00464 pal.setColor( TQPalette::Inactive, TQColorGroup::Base, gray ); 00465 pal.setColor( TQPalette::Inactive, TQColorGroup::HighlightedText, selectedTextColor ); 00466 pal.setColor( TQPalette::Inactive, TQColorGroup::Text, foreground ); 00467 00468 setPalette( pal ); 00469 } 00470 00471 TQSize KURLBar::sizeHint() const 00472 { 00473 return m_listBox->sizeHint(); 00474 00475 #if 0 00476 // this code causes vertical and or horizontal scrollbars appearing 00477 // depending on the text, font, moonphase and earth rotation. Just using 00478 // m_listBox->sizeHint() fixes this (although the widget can then be 00479 // resized to a smaller size so that scrollbars appear). 00480 int w = 0; 00481 int h = 0; 00482 KURLBarItem *item; 00483 bool vertical = isVertical(); 00484 00485 for ( item = static_cast<KURLBarItem*>( m_listBox->firstItem() ); 00486 item; 00487 item = static_cast<KURLBarItem*>( item->next() ) ) { 00488 00489 TQSize sh = item->sizeHint(); 00490 00491 if ( vertical ) { 00492 w = QMAX( w, sh.width() ); 00493 h += sh.height(); 00494 } 00495 else { 00496 w += sh.width(); 00497 h = QMAX( h, sh.height() ); 00498 } 00499 } 00500 00501 // if ( vertical && m_listBox->verticalScrollBar()->isVisible() ) 00502 // w += m_listBox->verticalScrollBar()->width(); 00503 // else if ( !vertical && m_listBox->horizontalScrollBar()->isVisible() ) 00504 // h += m_listBox->horizontalScrollBar()->height(); 00505 00506 if ( w == 0 && h == 0 ) 00507 return TQSize( 100, 200 ); 00508 else 00509 return TQSize( 6 + w, h ); 00510 #endif 00511 } 00512 00513 TQSize KURLBar::minimumSizeHint() const 00514 { 00515 TQSize s = sizeHint(); // ### 00516 int w = s.width() + m_listBox->verticalScrollBar()->width(); 00517 int h = s.height() + m_listBox->horizontalScrollBar()->height(); 00518 return TQSize( w, h ); 00519 } 00520 00521 void KURLBar::slotSelected( int button, TQListBoxItem *item ) 00522 { 00523 if ( button != Qt::LeftButton ) 00524 return; 00525 00526 slotSelected( item ); 00527 } 00528 00529 void KURLBar::slotSelected( TQListBoxItem *item ) 00530 { 00531 if ( item && item != m_activeItem ) 00532 m_activeItem = static_cast<KURLBarItem*>( item ); 00533 00534 if ( m_activeItem ) { 00535 m_listBox->setCurrentItem( m_activeItem ); 00536 emit activated( m_activeItem->url() ); 00537 } 00538 } 00539 00540 void KURLBar::setCurrentItem( const KURL& url ) 00541 { 00542 d->currentURL = url; 00543 00544 TQString u = url.url(-1); 00545 00546 if ( m_activeItem && m_activeItem->url().url(-1) == u ) 00547 return; 00548 00549 bool hasURL = false; 00550 TQListBoxItem *item = m_listBox->firstItem(); 00551 while ( item ) { 00552 if ( static_cast<KURLBarItem*>( item )->url().url(-1) == u ) { 00553 m_activeItem = static_cast<KURLBarItem*>( item ); 00554 m_listBox->setCurrentItem( item ); 00555 m_listBox->setSelected( item, true ); 00556 hasURL = true; 00557 break; 00558 } 00559 item = item->next(); 00560 } 00561 00562 if ( !hasURL ) { 00563 m_activeItem = 0L; 00564 m_listBox->clearSelection(); 00565 } 00566 } 00567 00568 KURLBarItem * KURLBar::currentItem() const 00569 { 00570 TQListBoxItem *item = m_listBox->item( m_listBox->currentItem() ); 00571 if ( item ) 00572 return static_cast<KURLBarItem *>( item ); 00573 return 0L; 00574 } 00575 00576 KURL KURLBar::currentURL() const 00577 { 00578 KURLBarItem *item = currentItem(); 00579 return item ? item->url() : KURL(); 00580 } 00581 00582 void KURLBar::readConfig( KConfig *appConfig, const TQString& itemGroup ) 00583 { 00584 m_isImmutable = appConfig->groupIsImmutable( itemGroup ); 00585 KConfigGroupSaver cs( appConfig, itemGroup ); 00586 d->defaultIconSize = m_iconSize; 00587 m_iconSize = appConfig->readNumEntry( "Speedbar IconSize", m_iconSize ); 00588 00589 if ( m_useGlobal ) { // read global items 00590 KConfig *globalConfig = KGlobal::config(); 00591 KConfigGroupSaver cs( globalConfig, (TQString)(itemGroup +" (Global)")); 00592 int num = globalConfig->readNumEntry( "Number of Entries" ); 00593 for ( int i = 0; i < num; i++ ) { 00594 readItem( i, globalConfig, false ); 00595 } 00596 } 00597 00598 // read application local items 00599 int num = appConfig->readNumEntry( "Number of Entries" ); 00600 for ( int i = 0; i < num; i++ ) { 00601 readItem( i, appConfig, true ); 00602 } 00603 } 00604 00605 void KURLBar::readItem( int i, KConfig *config, bool applicationLocal ) 00606 { 00607 TQString number = TQString::number( i ); 00608 KURL url = KURL::fromPathOrURL( config->readPathEntry( TQString("URL_") + number )); 00609 if ( !url.isValid() || !KProtocolInfo::isKnownProtocol( url )) 00610 return; // nothing we could do. 00611 00612 TQString description = config->readEntry( TQString("Description_") + number ); 00613 00614 insertItem( url, 00615 description, 00616 applicationLocal, 00617 config->readEntry( TQString("Icon_") + number ), 00618 static_cast<KIcon::Group>( 00619 config->readNumEntry( TQString("IconGroup_") + number )) ); 00620 } 00621 00622 void KURLBar::writeConfig( KConfig *config, const TQString& itemGroup ) 00623 { 00624 KConfigGroupSaver cs1( config, itemGroup ); 00625 if(!config->hasDefault("Speedbar IconSize") && m_iconSize == d->defaultIconSize ) 00626 config->revertToDefault("Speedbar IconSize"); 00627 else 00628 config->writeEntry( "Speedbar IconSize", m_iconSize ); 00629 00630 if ( !m_isModified ) 00631 return; 00632 00633 int i = 0; 00634 int numLocal = 0; 00635 KURLBarItem *item = static_cast<KURLBarItem*>( m_listBox->firstItem() ); 00636 00637 while ( item ) 00638 { 00639 if ( item->isPersistent() ) // we only save persistent items 00640 { 00641 if ( item->applicationLocal() ) 00642 { 00643 writeItem( item, numLocal, config, false ); 00644 numLocal++; 00645 } 00646 00647 i++; 00648 } 00649 item = static_cast<KURLBarItem*>( item->next() ); 00650 } 00651 config->writeEntry("Number of Entries", numLocal); 00652 00653 00654 // write the global entries to kdeglobals, if any 00655 bool haveGlobalEntries = (i > numLocal); 00656 if ( m_useGlobal && haveGlobalEntries ) { 00657 config->setGroup( itemGroup + " (Global)" ); 00658 00659 int numGlobals = 0; 00660 item = static_cast<KURLBarItem*>( m_listBox->firstItem() ); 00661 00662 while ( item ) 00663 { 00664 if ( item->isPersistent() ) // we only save persistent items 00665 { 00666 if ( !item->applicationLocal() ) 00667 { 00668 writeItem( item, numGlobals, config, true ); 00669 numGlobals++; 00670 } 00671 } 00672 00673 item = static_cast<KURLBarItem*>( item->next() ); 00674 } 00675 config->writeEntry("Number of Entries", numGlobals, true, true); 00676 } 00677 00678 m_isModified = false; 00679 } 00680 00681 void KURLBar::writeItem( KURLBarItem *item, int i, KConfig *config, 00682 bool global ) 00683 { 00684 if ( !item->isPersistent() ) 00685 return; 00686 00687 TQString Description = "Description_"; 00688 TQString URL = "URL_"; 00689 TQString Icon = "Icon_"; 00690 TQString IconGroup = "IconGroup_"; 00691 00692 TQString number = TQString::number( i ); 00693 config->writePathEntry( URL + number, item->url().prettyURL(), true, global ); 00694 00695 config->writeEntry( Description + number, item->description(),true,global); 00696 config->writeEntry( Icon + number, item->icon(), true, global ); 00697 config->writeEntry( IconGroup + number, item->iconGroup(), true, global ); 00698 } 00699 00700 00701 void KURLBar::slotDropped( TQDropEvent *e ) 00702 { 00703 KURL::List urls; 00704 if ( KURLDrag::decode( e, urls ) ) { 00705 KURL url; 00706 TQString description; 00707 TQString icon; 00708 bool appLocal = false; 00709 00710 KURL::List::Iterator it = urls.begin(); 00711 for ( ; it != urls.end(); ++it ) { 00712 (void) insertItem( *it, description, appLocal, icon ); 00713 m_isModified = true; 00714 updateGeometry(); 00715 } 00716 } 00717 } 00718 00719 void KURLBar::slotContextMenuRequested( TQListBoxItem *_item, const TQPoint& 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 TQPopupMenu *popup = new TQPopupMenu(); 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 TQString description = item->description(); 00799 TQString 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( TQWidget *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( TQPaintEvent* ) 00838 { 00839 TQPainter p(this); 00840 p.setPen( colorGroup().mid() ); 00841 p.drawRect( 0, 0, width(), height() ); 00842 } 00843 00844 TQDragObject * 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( TQDragEnterEvent *e ) 00862 { 00863 e->accept( KURLDrag::canDecode( e )); 00864 } 00865 00866 void KURLBarListBox::contentsDropEvent( TQDropEvent *e ) 00867 { 00868 emit dropped( e ); 00869 } 00870 00871 void KURLBarListBox::contextMenuEvent( TQContextMenuEvent *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 == Qt::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 TQString& description, TQString& icon, 00900 bool& appLocal, int iconSize, 00901 TQWidget *parent ) 00902 { 00903 KURLBarItemDialog *dialog = new KURLBarItemDialog( allowGlobal, url, 00904 description, icon, 00905 appLocal, 00906 iconSize, parent ); 00907 if ( dialog->exec() == TQDialog::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 TQString& description, 00924 TQString icon, bool appLocal, 00925 int iconSize, 00926 TQWidget *parent, const char *name ) 00927 : KDialogBase( parent, name, true, 00928 i18n("Edit Quick Access Entry"), Ok | Cancel, Ok, true ) 00929 { 00930 TQVBox *box = new TQVBox( this ); 00931 TQString text = i18n("<qt><b>Please provide a description, URL and icon for this Quick Access entry.</b></br></qt>"); 00932 TQLabel *label = new TQLabel( text, box ); 00933 box->setSpacing( spacingHint() ); 00934 00935 TQGrid *grid = new TQGrid( 2, box ); 00936 grid->setSpacing( spacingHint() ); 00937 00938 TQString 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 TQLabel( 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 TQWhatsThis::add( label, whatsThisText ); 00946 TQWhatsThis::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.trinitydesktop.org<p>" 00950 "By clicking on the button next to the text edit box you can browse to an " 00951 "appropriate URL.</qt>").arg(TQDir::homeDirPath()); 00952 label = new TQLabel( i18n("&URL:"), grid ); 00953 m_urlEdit = new KURLRequester( url.prettyURL(), grid ); 00954 m_urlEdit->setMode( KFile::Directory ); 00955 label->setBuddy( m_urlEdit ); 00956 TQWhatsThis::add( label, whatsThisText ); 00957 TQWhatsThis::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 TQLabel( 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 TQWhatsThis::add( label, whatsThisText ); 00969 TQWhatsThis::add( m_iconButton, whatsThisText ); 00970 00971 if ( allowGlobal ) { 00972 TQString appName; 00973 if ( KGlobal::instance()->aboutData() ) 00974 appName = KGlobal::instance()->aboutData()->programName(); 00975 if ( appName.isEmpty() ) 00976 appName = TQString::fromLatin1( KGlobal::instance()->instanceName() ); 00977 m_appLocal = new TQCheckBox( i18n("&Only show when using this application (%1)").arg( appName ), box ); 00978 m_appLocal->setChecked( appLocal ); 00979 TQWhatsThis::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(),TQT_SIGNAL(textChanged ( const TQString & )),this,TQT_SLOT(urlChanged(const TQString & ))); 00989 m_edit->setFocus(); 00990 setMainWidget( box ); 00991 } 00992 00993 KURLBarItemDialog::~KURLBarItemDialog() 00994 { 00995 } 00996 00997 void KURLBarItemDialog::urlChanged(const TQString & text ) 00998 { 00999 enableButtonOK( !text.isEmpty() ); 01000 } 01001 01002 KURL KURLBarItemDialog::url() const 01003 { 01004 TQString 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 TQString KURLBarItemDialog::description() const 01015 { 01016 return m_edit->text(); 01017 } 01018 01019 TQString 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"