22 #include "partmanager.h" 23 #include <kparts/event.h> 24 #include <kparts/part.h> 28 #include <tqapplication.h> 34 template class TQPtrList<Part>;
38 class PartManagerPrivate
47 m_bAllowNestedParts =
false;
48 m_bIgnoreScrollBars =
false;
49 m_activationButtonMask = Qt::LeftButton | Qt::MidButton | Qt::RightButton;
50 m_reason = PartManager::NoReason;
55 void setReason( TQEvent* ev ) {
56 switch( ev->type() ) {
57 case TQEvent::MouseButtonPress:
58 case TQEvent::MouseButtonDblClick: {
59 TQMouseEvent* mev = TQT_TQMOUSEEVENT( ev );
60 m_reason = mev->button() == Qt::LeftButton
61 ? PartManager::ReasonLeftClick
62 : ( mev->button() == Qt::MidButton
63 ? PartManager::ReasonMidClick
64 : PartManager::ReasonRightClick );
67 case TQEvent::FocusIn:
68 m_reason = TQT_TQFOCUSEVENT( ev )->reason();
71 kdWarning(1000) <<
"PartManagerPrivate::setReason got unexpected ev type " << ev->type() <<
endl;
77 TQWidget *m_activeWidget;
79 TQPtrList<Part> m_parts;
84 TQWidget *m_selectedWidget;
86 TQPtrList<TQWidget> m_managedTopLevelWidgets;
87 short int m_activationButtonMask;
88 bool m_bIgnoreScrollBars;
89 bool m_bAllowNestedParts;
96 : TQObject( parent, name )
98 d =
new PartManagerPrivate;
100 tqApp->installEventFilter(
this );
102 d->m_policy = Direct;
108 : TQObject( parent, name )
110 d =
new PartManagerPrivate;
112 tqApp->installEventFilter(
this );
114 d->m_policy = Direct;
119 PartManager::~PartManager()
121 for ( TQPtrListIterator<TQWidget> it( d->m_managedTopLevelWidgets );
123 disconnect( it.current(), TQT_SIGNAL( destroyed() ),
124 this, TQT_SLOT( slotManagedTopLevelWidgetDestroyed() ) );
126 for ( TQPtrListIterator<Part> it( d->m_parts ); it.current(); ++it )
128 it.current()->setManager( 0 );
132 tqApp->removeEventFilter(
this );
138 d->m_policy = policy;
148 d->m_bAllowNestedParts = allow;
153 return d->m_bAllowNestedParts;
158 d->m_bIgnoreScrollBars = ignore;
163 return d->m_bIgnoreScrollBars;
168 d->m_activationButtonMask = buttonMask;
173 return d->m_activationButtonMask;
176 bool PartManager::eventFilter( TQObject *obj, TQEvent *ev )
179 if ( ev->type() != TQEvent::MouseButtonPress &&
180 ev->type() != TQEvent::MouseButtonDblClick &&
181 ev->type() != TQEvent::FocusIn )
184 if ( !obj->isWidgetType() )
187 TQWidget *w = TQT_TQWIDGET( obj );
189 if ( ( w->testWFlags( WType_Dialog ) && w->isModal() ) ||
190 w->testWFlags( WType_Popup ) || w->testWFlags( WStyle_Tool ) )
193 TQMouseEvent* mev = 0L;
194 if ( ev->type() == TQEvent::MouseButtonPress || ev->type() == TQEvent::MouseButtonDblClick )
196 mev = TQT_TQMOUSEEVENT( ev );
197 #ifdef DEBUG_PARTMANAGER 198 kdDebug(1000) <<
"PartManager::eventFilter button: " << mev->button() <<
" " <<
"d->m_activationButtonMask=" << d->m_activationButtonMask <<
endl;
200 if ( ( mev->button() & d->m_activationButtonMask ) == 0 )
209 if ( !d->m_managedTopLevelWidgets.containsRef( w->topLevelWidget() ) )
212 if ( d->m_bIgnoreScrollBars && w->inherits( TQSCROLLBAR_OBJECT_NAME_STRING ) )
217 pos = mev->globalPos();
218 part = findPartFromWidget( w, pos );
220 part = findPartFromWidget( w );
222 #ifdef DEBUG_PARTMANAGER 223 TQCString evType = ( ev->type() == TQEvent::MouseButtonPress ) ?
"MouseButtonPress" 224 : ( ev->type() == TQEvent::MouseButtonDblClick ) ?
"MouseButtonDblClick" 225 : ( ev->type() == TQEvent::FocusIn ) ?
"FocusIn" :
"OTHER! ERROR!";
229 if ( d->m_policy == PartManager::TriState )
231 if ( ev->type() == TQEvent::MouseButtonDblClick )
233 if ( part == d->m_activePart && w == d->m_activeWidget )
236 #ifdef DEBUG_PARTMANAGER 237 kdDebug(1000) <<
"PartManager::eventFilter dblclick -> setActivePart" << part <<
endl;
241 d->m_reason = NoReason;
245 if ( ( d->m_selectedWidget != w || d->m_selectedPart != part ) &&
246 ( d->m_activeWidget != w || d->m_activePart != part ) )
251 #ifdef DEBUG_PARTMANAGER 252 kdDebug(1000) <<
"Part " << part <<
" (non-selectable) made active because " << w->className() <<
" got event" <<
" " << evType <<
endl;
256 d->m_reason = NoReason;
260 else if ( d->m_selectedWidget == w && d->m_selectedPart == part )
262 #ifdef DEBUG_PARTMANAGER 263 kdDebug(1000) <<
"Part " << part <<
" made active (from selected) because " << w->className() <<
" got event" <<
" " << evType <<
endl;
267 d->m_reason = NoReason;
270 else if ( d->m_activeWidget == w && d->m_activePart == part )
278 else if ( part != d->m_activePart )
280 #ifdef DEBUG_PARTMANAGER 281 kdDebug(1000) <<
"Part " << part <<
" made active because " << w->className() <<
" got event" <<
" " << evType <<
endl;
285 d->m_reason = NoReason;
291 w = w->parentWidget();
293 if ( w && ( ( w->testWFlags( WType_Dialog ) && w->isModal() ) ||
294 w->testWFlags( WType_Popup ) || w->testWFlags( WStyle_Tool ) ) )
296 #ifdef DEBUG_PARTMANAGER 297 kdDebug(1000) << TQString(
"No part made active although %1/%2 got event - loop aborted").arg(obj->name()).arg(obj->className()) <<
endl;
304 #ifdef DEBUG_PARTMANAGER 305 kdDebug(1000) << TQString(
"No part made active although %1/%2 got event").arg(obj->name()).arg(obj->className()) <<
endl;
310 Part * PartManager::findPartFromWidget( TQWidget * widget,
const TQPoint &pos )
312 TQPtrListIterator<Part> it ( d->m_parts );
313 for ( ; it.current() ; ++it )
316 if ( part && d->m_parts.findRef( part ) != -1 )
322 Part * PartManager::findPartFromWidget( TQWidget * widget )
324 TQPtrListIterator<Part> it ( d->m_parts );
325 for ( ; it.current() ; ++it )
327 if ( widget == it.current()->widget() )
335 if ( d->m_parts.findRef( part ) != -1 )
337 #ifdef DEBUG_PARTMANAGER 343 d->m_parts.append( part );
345 part->setManager(
this );
351 part->
widget()->setFocus();
355 if ( part->
widget() && part->
widget()->focusPolicy() == TQ_NoFocus )
357 kdWarning(1000) <<
"Part '" << part->name() <<
"' has a widget " << part->
widget()->name() <<
" with a focus policy of NoFocus. It should have at least a ClickFocus policy, for part activation to work well." <<
endl;
359 if ( part->
widget() && part->
widget()->focusPolicy() == TQ_TabFocus )
361 kdWarning(1000) <<
"Part '" << part->name() <<
"' has a widget " << part->
widget()->name() <<
" with a focus policy of TabFocus. It should have at least a ClickFocus policy, for part activation to work well." <<
endl;
364 if ( setActive && part->
widget() )
371 if ( d->m_parts.findRef( part ) == -1 )
373 kdFatal(1000) << TQString(TQString(
"Can't remove part %1, not in KPartManager's list.").arg(part->name())) <<
endl;
379 int nb = d->m_parts.count();
380 bool ok = d->m_parts.removeRef( part );
382 Q_ASSERT( (
int)d->m_parts.count() == nb-1 );
387 if ( part == d->m_activePart )
389 if ( part == d->m_selectedPart )
397 if ( d->m_parts.findRef( oldPart ) == -1 )
399 kdFatal(1000) << TQString(TQString(
"Can't remove part %1, not in KPartManager's list.").arg(oldPart->name())) <<
endl;
403 d->m_parts.removeRef( oldPart );
404 oldPart->setManager(0);
413 if ( part && d->m_parts.findRef( part ) == -1 )
415 kdWarning( 1000 ) <<
"PartManager::setActivePart : trying to activate a non-registered part! " << part->name() <<
endl;
421 if ( part && !d->m_bAllowNestedParts )
423 TQObject *parentPart = TQT_TQOBJECT(part->parent());
424 if ( parentPart && parentPart->inherits(
"KParts::Part" ) )
432 #ifdef DEBUG_PARTMANAGER 433 kdDebug(1000) <<
"PartManager::setActivePart d->m_activePart=" << d->m_activePart <<
"<->part=" << part
434 <<
" d->m_activeWidget=" << d->m_activeWidget <<
"<->widget=" << widget <<
endl;
438 if ( d->m_activePart && part && d->m_activePart == part &&
439 (!widget || d->m_activeWidget == widget) )
443 TQWidget *oldActiveWidget = d->m_activeWidget;
447 d->m_activePart = part;
448 d->m_activeWidget = widget;
453 TQWidget *savedActiveWidget = widget;
456 TQApplication::sendEvent( oldActivePart, &ev );
457 if ( oldActiveWidget )
459 disconnect( oldActiveWidget, TQT_SIGNAL( destroyed() ),
460 this, TQT_SLOT( slotWidgetDestroyed() ) );
461 TQApplication::sendEvent( oldActiveWidget, &ev );
464 d->m_activePart = savedActivePart;
465 d->m_activeWidget = savedActiveWidget;
468 if ( d->m_activePart )
471 d->m_activeWidget = part->
widget();
474 TQApplication::sendEvent( d->m_activePart, &ev );
475 if ( d->m_activeWidget )
477 connect( d->m_activeWidget, TQT_SIGNAL( destroyed() ),
478 this, TQT_SLOT( slotWidgetDestroyed() ) );
479 TQApplication::sendEvent( d->m_activeWidget, &ev );
485 kdDebug(1000) <<
this <<
" emitting activePartChanged " << d->m_activePart <<
endl;
492 KGlobal::_activeInstance = instance;
497 return d->m_activePart;
502 return d->m_activeWidget;
507 if ( part == d->m_selectedPart && widget == d->m_selectedWidget )
510 Part *oldPart = d->m_selectedPart;
511 TQWidget *oldWidget = d->m_selectedWidget;
513 d->m_selectedPart = part;
514 d->m_selectedWidget = widget;
516 if ( part && !widget )
517 d->m_selectedWidget = part->
widget();
522 TQApplication::sendEvent( oldPart, &ev );
523 TQApplication::sendEvent( oldWidget, &ev );
526 if ( d->m_selectedPart )
529 TQApplication::sendEvent( d->m_selectedPart, &ev );
530 TQApplication::sendEvent( d->m_selectedWidget, &ev );
536 return d->m_selectedPart;
541 return d->m_selectedWidget;
546 kdDebug(1000) <<
"KPartManager::slotObjectDestroyed()" <<
endl;
547 removePart( const_cast<Part *>( static_cast<const Part *>( sender() ) ) );
550 void PartManager::slotWidgetDestroyed()
552 kdDebug(1000) <<
"KPartsManager::slotWidgetDestroyed()" <<
endl;
553 if ( static_cast<const TQWidget *>( sender() ) == d->m_activeWidget )
565 if ( !topLevel->isTopLevel() )
568 if ( d->m_managedTopLevelWidgets.containsRef( topLevel ) )
571 d->m_managedTopLevelWidgets.append( topLevel );
572 connect( topLevel, TQT_SIGNAL( destroyed() ),
573 this, TQT_SLOT( slotManagedTopLevelWidgetDestroyed() ) );
578 if ( !topLevel->isTopLevel() )
581 if ( d->m_managedTopLevelWidgets.findRef( topLevel ) == -1 )
584 d->m_managedTopLevelWidgets.remove();
587 void PartManager::slotManagedTopLevelWidgetDestroyed()
589 const TQWidget *widget =
static_cast<const TQWidget *
>( sender() );
598 void PartManager::virtual_hook(
int,
void* )
601 #include "partmanager.moc" TQString kdBacktrace(int levels=-1)
void partRemoved(KParts::Part *part)
Emitted when a part has been removed.
const TQPtrList< Part > * parts() const
Returns the list of parts being managed by the partmanager.
This event is sent by the part manager when the active part changes.
void setSelectionPolicy(SelectionPolicy policy)
Sets the selection policy of the partmanager.
virtual void setActivePart(Part *part, TQWidget *widget=0L)
Sets the active part.
virtual void removePart(Part *part)
Removes a part from the manager (this does not delete the object) .
virtual Part * hitTest(TQWidget *widget, const TQPoint &globalPos)
Returns the part (this, or a child part) at the given global position.
virtual TQWidget * widget()
kdbgstream kdDebug(int area=0)
void slotObjectDestroyed()
Removes a part when it is destroyed.
void partAdded(KParts::Part *part)
Emitted when a new part has been added.
void addManagedTopLevelWidget(const TQWidget *topLevel)
Adds the topLevel widget to the list of managed toplevel widgets.
kdbgstream kdWarning(int area=0)
bool ignoreScrollBars() const
virtual Part * selectedPart() const
Returns the current selected part.
void removeManagedTopLevelWidget(const TQWidget *topLevel)
Removes the topLevel widget from the list of managed toplevel widgets.
virtual Part * activePart() const
Returns the active part.
virtual void addPart(Part *part, bool setActive=true)
Adds a part to the manager.
SelectionPolicy
Selection policy. The default policy of a PartManager is Direct.
void setIgnoreScrollBars(bool ignore)
Specifies whether the partmanager should ignore mouse click events for scrollbars or not...
bool isSelectable() const
Returns whether the part is selectable or not.
virtual void replacePart(Part *oldPart, Part *newPart, bool setActive=true)
Replaces oldPart with newPart, and sets newPart as active if setActive is true.
kdbgstream kdFatal(int area=0)
virtual TQWidget * selectedWidget() const
Returns the selected widget of the current selected part (see selectedPart()).
void setAllowNestedParts(bool allow)
Specifies whether the partmanager should handle/allow nested parts or not.
virtual void setActiveInstance(KInstance *instance)
Changes the active instance when the active part changes.
virtual void setSelectedPart(Part *part, TQWidget *widget=0L)
Sets the selected part.
SelectionPolicy selectionPolicy() const
Returns the current selection policy.
kndbgstream & endl(kndbgstream &s)
bool allowNestedParts() const
PartManager(TQWidget *parent, const char *name=0L)
Constructs a part manager.
virtual TQWidget * activeWidget() const
Returns the active widget of the current active part (see activePart()).
short int activationButtonMask() const
void activePartChanged(KParts::Part *newPart)
Emitted when the active part has changed.
void setActivationButtonMask(short int buttonMask)
Specifies which mouse buttons the partmanager should react upon.
This event is sent when a part is selected or deselected.