25 #include <kbookmarkbar.h>
26 #include <kbookmarkdrag.h>
28 #include <kbookmarkmenu.h>
32 #include <ktoolbarbutton.h>
35 #include <kpopupmenu.h>
37 #include "kbookmarkdrag.h"
38 #include "kbookmarkmenu_p.h"
39 #include "kbookmarkdombuilder.h"
41 #include "dptrtemplate.h"
43 #include <tqapplication.h>
45 class KBookmarkBarPrivate :
public dPtrTemplate<KBookmarkBar, KBookmarkBarPrivate>
48 TQPtrList<KAction> m_actions;
51 KToolBar* m_sepToolBar;
54 TQString m_dropAddress;
55 TQString m_highlightedAddress;
57 KBookmarkBarPrivate() {
65 template<> TQPtrDict<KBookmarkBarPrivate>* dPtrTemplate<KBookmarkBar, KBookmarkBarPrivate>::d_ptr = 0;
67 KBookmarkBarPrivate* KBookmarkBar::dptr()
const
69 return KBookmarkBarPrivate::d(
this );
76 ToolbarFilter() : m_visible(false) { ; }
79 virtual void visit(
const KBookmark & );
89 KActionCollection *coll,
90 TQObject *parent,
const char *name )
91 : TQObject( parent, name ), m_pOwner(_owner), m_toolBar(_toolBar),
92 m_actionCollection( coll ), m_pManager(mgr)
94 m_lstSubMenus.setAutoDelete(
true );
96 m_toolBar->setAcceptDrops(
true );
97 m_toolBar->installEventFilter(
this );
99 dptr()->m_actions.setAutoDelete(
true );
101 connect( mgr, TQT_SIGNAL( changed(
const TQString &,
const TQString &) ),
102 TQT_SLOT( slotBookmarksChanged(
const TQString &) ) );
105 fillBookmarkBar( toolbar );
110 return dptr()->m_filteredMgr ? TQString::null : m_pManager->
toolbar().address();
113 #define CURRENT_TOOLBAR() ( \
114 dptr()->m_filteredMgr ? dptr()->m_filteredMgr->root() \
115 : m_pManager->toolbar() )
117 #define CURRENT_MANAGER() ( \
118 dptr()->m_filteredMgr ? dptr()->m_filteredMgr \
123 if ( KBookmarkSettings::self()->m_filteredtoolbar )
125 if ( !dptr()->m_filteredMgr ) {
126 dptr()->m_filteredMgr = KBookmarkManager::createTempManager();
129 TQValueList<KBookmark> bks;
130 for (KBookmark bm = bkRoot.
first(); !bm.isNull(); bm = bkRoot.
next(bm))
132 for ( TQValueListConstIterator<KBookmark> it = bks.begin(); it != bks.end(); ++it )
135 ToolbarFilter filter;
137 dptr()->m_filteredMgr );
138 builder.connectImporter( &filter );
139 filter.filter( m_pManager->
root() );
142 return CURRENT_TOOLBAR();
145 KBookmarkBar::~KBookmarkBar()
148 KBookmarkBarPrivate::delete_d(
this);
151 void KBookmarkBar::clear()
153 TQPtrListIterator<KAction> it( dptr()->m_actions );
155 for (; it.current(); ++it ) {
158 dptr()->m_actions.clear();
159 m_lstSubMenus.clear();
162 void KBookmarkBar::slotBookmarksChanged(
const TQString & group )
165 kdDebug(7043) <<
"slotBookmarksChanged( " << group <<
" )" << endl;
170 if ( KBookmark::commonParent(group, tb.address()) == group
171 || KBookmarkSettings::self()->m_filteredtoolbar )
174 fillBookmarkBar( tb );
179 TQPtrListIterator<KBookmarkMenu> it( m_lstSubMenus );
180 for (; it.current(); ++it )
182 it.current()->slotBookmarksChanged( group );
192 for (KBookmark bm = parent.
first(); !bm.isNull(); bm = parent.
next(bm))
194 TQString text = bm.text();
195 text.replace(
'&',
"&&" );
198 if ( bm.isSeparator() )
199 m_toolBar->insertLineSeparator();
202 KAction *action =
new KBookmarkAction( text, bm.icon(), 0, m_actionCollection, 0 );
203 connect(action, TQT_SIGNAL( activated ( KAction::ActivationReason, TQt::ButtonState )),
204 this, TQT_SLOT( slotBookmarkSelected( KAction::ActivationReason, TQt::ButtonState ) ));
206 action->setProperty(
"url", bm.url().url() );
207 action->setProperty(
"address", bm.address() );
209 action->setToolTip( bm.url().pathOrURL() );
211 action->plug(m_toolBar);
213 dptr()->m_actions.append( action );
218 KActionMenu *action =
new KBookmarkActionMenu( text, bm.icon(),
220 "bookmarkbar-actionmenu");
221 action->setProperty(
"address", bm.address() );
222 action->setProperty(
"readOnly", dptr()->m_readOnly );
223 action->setDelayed(
false );
226 KGlobal::config()->setGroup(
"Settings" );
227 bool addEntriesBookmarkBar = KGlobal::config()->readBoolEntry(
"AddEntriesBookmarkBar",
true);
230 m_actionCollection,
false, addEntriesBookmarkBar,
234 connect(menu, TQT_SIGNAL(
openBookmark(
const TQString &, TQt::ButtonState) ),
235 this, TQT_SIGNAL(
openBookmark(
const TQString &, TQt::ButtonState) ));
237 action->plug(m_toolBar);
238 m_lstSubMenus.append( menu );
240 dptr()->m_actions.append( action );
247 dptr()->m_readOnly = readOnly;
252 return dptr()->m_readOnly;
255 void KBookmarkBar::slotBookmarkSelected( KAction::ActivationReason , TQt::ButtonState state )
257 if (!m_pOwner)
return;
259 const KAction* action =
dynamic_cast<const KAction *
>(sender());
262 const TQString & url = sender()->property(
"url").toString();
268 void KBookmarkBar::slotBookmarkSelected()
270 slotBookmarkSelected(KAction::ToolBarActivation, Qt::NoButton);
273 static const int const_sepId = -9999;
277 static void removeTempSep(KBookmarkBarPrivate* p)
279 if (p->m_sepToolBar) {
280 p->m_sepToolBar->removeItem(const_sepId);
285 static KAction* findPluggedAction(TQPtrList<KAction> actions, KToolBar *tb,
int id)
287 TQPtrListIterator<KAction> it( actions );
289 if ((*it)->isPlugged(tb,
id))
304 static TQString handleToolbarDragMoveEvent(
305 KBookmarkBarPrivate *p, KToolBar *tb, TQPoint pos, TQPtrList<KAction> actions,
309 Q_ASSERT( actions.isEmpty() || (tb ==
dynamic_cast<KToolBar*
>(actions.first()->container(0))) );
310 p->m_sepToolBar = tb;
311 p->m_sepToolBar->removeItemDelayed(const_sepId);
316 b =
dynamic_cast<KToolBarButton*
>(tb->childAt(pos));
323 index = tb->itemIndex(b->id());
324 TQRect r = b->geometry();
325 if (pos.x() < ((r.left() + r.right())/2))
333 b = tb->getButton(tb->idAt(index));
337 else if (actions.isEmpty())
351 index = actions.count() - 1;
352 b = tb->getButton(tb->idAt(index));
354 if (pos.x() <= b->geometry().left())
359 return TQString::null;
361 a = findPluggedAction(actions, tb, b->id());
363 address = a->property(
"address").toString();
364 p->m_sepIndex = index + (atFirst ? 0 : 1);
371 kdDebug() <<
"kbookmarkbar:: popping up " << bk.text() << endl;
372 KBookmarkActionMenu *menu =
dynamic_cast<KBookmarkActionMenu*
>(a);
374 menu->popup(tb->mapToGlobal(b->geometry().center()));
380 tb->insertLineSeparator(p->m_sepIndex, const_sepId);
385 static KAction* handleToolbarMouseButton(TQPoint pos, TQPtrList<KAction> actions,
388 KAction *act = actions.first();
393 KToolBar *tb =
dynamic_cast<KToolBar*
>(act->container(0));
397 b =
dynamic_cast<KToolBarButton*
>(tb->childAt(pos));
402 a = findPluggedAction(actions, tb, b->id());
404 pt = tb->mapToGlobal(pos);
415 class KBookmarkBarRMBAssoc :
public dPtrTemplate<KBookmarkBar, RMB> { };
416 template<> TQPtrDict<RMB>* dPtrTemplate<KBookmarkBar, RMB>::d_ptr = 0;
418 static RMB* rmbSelf(
KBookmarkBar *m) {
return KBookmarkBarRMBAssoc::d(m); }
422 RMB *s = rmbSelf(
self);
424 s->m_parentAddress =
self->parentAddress();
425 s->s_highlightedAddress =
self->dptr()->m_highlightedAddress;
426 s->m_pManager =
self->m_pManager;
427 s->m_pOwner =
self->m_pOwner;
432 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionEditAt( val ); }
435 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionProperties( val ); }
438 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionInsert( val ); }
441 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionRemove( val ); }
444 { RMB::begin_rmb_action(
this); rmbSelf(
this)->slotRMBActionCopyLocation( val ); }
446 bool KBookmarkBar::eventFilter( TQObject *o, TQEvent *e )
448 if (dptr()->m_readOnly || dptr()->m_filteredMgr)
452 if ( (e->type() == TQEvent::MouseButtonRelease) || (e->type() == TQEvent::MouseButtonPress) )
454 TQMouseEvent *mev = (TQMouseEvent*)e;
460 _a = handleToolbarMouseButton( mev->pos(), dptr()->m_actions, m_pManager, pt );
461 if (_a && mev->button() == Qt::RightButton)
463 dptr()->m_highlightedAddress = _a->property(
"address").toString();
464 KBookmark bookmark = m_pManager->
findByAddress( dptr()->m_highlightedAddress );
465 RMB::begin_rmb_action(
this);
466 KPopupMenu *pm =
new KPopupMenu;
467 rmbSelf(
this)->fillContextMenu( pm, dptr()->m_highlightedAddress, 0 );
469 rmbSelf(
this)->fillContextMenu2( pm, dptr()->m_highlightedAddress, 0 );
476 else if ( e->type() == TQEvent::DragLeave )
478 removeTempSep(dptr());
479 dptr()->m_dropAddress = TQString::null;
481 else if ( e->type() == TQEvent::Drop )
483 removeTempSep(dptr());
484 TQDropEvent *dev = (TQDropEvent*)e;
485 if ( !KBookmarkDrag::canDecode( dev ) )
487 TQValueList<KBookmark> list = KBookmarkDrag::decode( dev );
488 if (list.count() > 1)
489 kdWarning(7043) <<
"Sorry, currently you can only drop one address "
490 "onto the bookmark bar!" << endl;
491 KBookmark toInsert = list.first();
492 KBookmark bookmark = m_pManager->
findByAddress( dptr()->m_dropAddress );
493 Q_ASSERT(!bookmark.isNull());
494 kdDebug(7043) <<
"inserting "
495 << TQString(dptr()->m_atFirst ?
"before" :
"after")
496 <<
" dptr()->m_dropAddress == " << dptr()->m_dropAddress << endl;
498 Q_ASSERT(!parentBookmark.isNull());
499 KBookmark newBookmark = parentBookmark.
addBookmark(
500 m_pManager, toInsert.fullText(),
502 parentBookmark.
moveItem( newBookmark, dptr()->m_atFirst ? KBookmark() : bookmark );
506 else if ( e->type() == TQEvent::DragMove )
508 TQDragMoveEvent *dme = (TQDragMoveEvent*)e;
509 if (!KBookmarkDrag::canDecode( dme ))
512 TQString dropAddress;
513 KToolBar *tb = (KToolBar*)o;
514 dropAddress = handleToolbarDragMoveEvent(dptr(), tb, dme->pos(), dptr()->m_actions, _atFirst, m_pManager);
515 if (!dropAddress.isNull())
517 dptr()->m_dropAddress = dropAddress;
518 dptr()->m_atFirst = _atFirst;
525 static bool showInToolbar(
const KBookmark &bk ) {
526 return (bk.internalElement().attributes().namedItem(
"showintoolbar").toAttr().value() ==
"yes");
529 void ToolbarFilter::visit(
const KBookmark &bk ) {
531 if ( m_visible || showInToolbar(bk) )
532 KXBELBookmarkImporterImpl::visit(bk);
537 if ( !m_visible && showInToolbar(grp) )
539 m_visibleStart = grp;
543 KXBELBookmarkImporterImpl::visitEnter(grp);
549 KXBELBookmarkImporterImpl::visitLeave(grp);
550 if ( m_visible && grp.address() == m_visibleStart.address() )
554 #include "kbookmarkbar.moc"
KBookmark addBookmark(KBookmarkManager *mgr, const KBookmark &bm, bool emitSignal=true)
Create a new bookmark, as the last child of this group Don't forget to use KBookmarkManager::self()->...
This class implements the reading/writing of bookmarks in XML.
void slotRMBActionProperties(int)
void emitChanged(KBookmarkGroup &group)
Saves the bookmark file and notifies everyone.
This class provides a bookmark toolbar.
virtual void openBookmarkURL(const TQString &_url)
This function is called if the user selects a bookmark.
KBookmarkGroup toolbar()
This returns the root of the toolbar menu.
The KBookmarkMenu and KBookmarkBar classes gives the user the ability to either edit bookmarks or add...
void slotRMBActionRemove(int)
KBookmarkBar(KBookmarkManager *manager, KBookmarkOwner *owner, KToolBar *toolBar, KActionCollection *, TQObject *parent=0L, const char *name=0L)
Fills a bookmark toolbar.
KBookmark findByAddress(const TQString &address, bool tolerate=false)
void slotRMBActionEditAt(int)
KBookmarkGroup root() const
This will return the root bookmark.
void openBookmark(const TQString &url, TQt::ButtonState state)
void slotRMBActionCopyLocation(int)
void deleteBookmark(KBookmark bk)
Delete a bookmark - it has to be one of our children ! Don't forget to use KBookmarkManager::self()->...
bool moveItem(const KBookmark &item, const KBookmark &after)
Moves item after after (which should be a child of ours).
void slotRMBActionInsert(int)
KBookmark next(const KBookmark ¤t) const
Return the next sibling of a child bookmark of this group.
void aboutToShowContextMenu(const KBookmark &, TQPopupMenu *)
KBookmark first() const
Return the first child bookmark of this group.
A class for importing XBEL files.