📄 partmanager.cpp
字号:
// -*- mode: c++; c-basic-offset: 2 -*-/* This file is part of the KDE project Copyright (C) 1999 Simon Hausmann <hausmann@kde.org> (C) 1999 David Faure <faure@kde.org> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/#include "partmanager.h"#include <kparts/event.h>#include <kparts/part.h>#include <kglobal.h>#include <kdebug.h>#include <qapplication.h>//#define DEBUG_PARTMANAGERusing namespace KParts;template class QPtrList<Part>;namespace KParts {class PartManagerPrivate{public: PartManagerPrivate() { m_activeWidget = 0; m_activePart = 0; m_selectedPart = 0; m_selectedWidget = 0; m_bAllowNestedParts = false; m_bIgnoreScrollBars = false; m_activationButtonMask = Qt::LeftButton | Qt::MidButton | Qt::RightButton; m_reason = PartManager::NoReason; } ~PartManagerPrivate() { } void setReason( QEvent* ev ) { switch( ev->type() ) { case QEvent::MouseButtonPress: case QEvent::MouseButtonDblClick: { QMouseEvent* mev = static_cast<QMouseEvent *>( ev ); m_reason = mev->button() == Qt::LeftButton ? PartManager::ReasonLeftClick : ( mev->button() == Qt::MidButton ? PartManager::ReasonMidClick : PartManager::ReasonRightClick ); break; } case QEvent::FocusIn: m_reason = static_cast<QFocusEvent *>( ev )->reason(); break; default: kdWarning(1000) << "PartManagerPrivate::setReason got unexpected ev type " << ev->type() << endl; break; } } Part * m_activePart; QWidget *m_activeWidget; QPtrList<Part> m_parts; PartManager::SelectionPolicy m_policy; Part *m_selectedPart; QWidget *m_selectedWidget; QPtrList<QWidget> m_managedTopLevelWidgets; short int m_activationButtonMask; bool m_bIgnoreScrollBars; bool m_bAllowNestedParts; int m_reason;};}PartManager::PartManager( QWidget * parent, const char * name ) : QObject( parent, name ){ d = new PartManagerPrivate; qApp->installEventFilter( this ); d->m_policy = Direct; addManagedTopLevelWidget( parent );}PartManager::PartManager( QWidget *topLevel, QObject *parent, const char *name ) : QObject( parent, name ){ d = new PartManagerPrivate; qApp->installEventFilter( this ); d->m_policy = Direct; addManagedTopLevelWidget( topLevel );}PartManager::~PartManager(){ for ( QPtrListIterator<QWidget> it( d->m_managedTopLevelWidgets ); it.current(); ++it ) disconnect( it.current(), SIGNAL( destroyed() ), this, SLOT( slotManagedTopLevelWidgetDestroyed() ) ); for ( QPtrListIterator<Part> it( d->m_parts ); it.current(); ++it ) { it.current()->setManager( 0 ); } // core dumps ... setActivePart( 0L ); qApp->removeEventFilter( this ); delete d;}void PartManager::setSelectionPolicy( SelectionPolicy policy ){ d->m_policy = policy;}PartManager::SelectionPolicy PartManager::selectionPolicy() const{ return d->m_policy;}void PartManager::setAllowNestedParts( bool allow ){ d->m_bAllowNestedParts = allow;}bool PartManager::allowNestedParts() const{ return d->m_bAllowNestedParts;}void PartManager::setIgnoreScrollBars( bool ignore ){ d->m_bIgnoreScrollBars = ignore;}bool PartManager::ignoreScrollBars() const{ return d->m_bIgnoreScrollBars;}void PartManager::setActivationButtonMask( short int buttonMask ){ d->m_activationButtonMask = buttonMask;}short int PartManager::activationButtonMask() const{ return d->m_activationButtonMask;}bool PartManager::eventFilter( QObject *obj, QEvent *ev ){ if ( ev->type() != QEvent::MouseButtonPress && ev->type() != QEvent::MouseButtonDblClick && ev->type() != QEvent::FocusIn ) return false; if ( !obj->isWidgetType() ) return false; QWidget *w = static_cast<QWidget *>( obj ); if ( ( w->testWFlags( WType_Dialog ) && w->isModal() ) || w->testWFlags( WType_Popup ) || w->testWFlags( WStyle_Tool ) ) return false; QMouseEvent* mev = 0L; if ( ev->type() == QEvent::MouseButtonPress || ev->type() == QEvent::MouseButtonDblClick ) { mev = static_cast<QMouseEvent *>( ev );#ifdef DEBUG_PARTMANAGER kdDebug(1000) << "PartManager::eventFilter button: " << mev->button() << " " << "d->m_activationButtonMask=" << d->m_activationButtonMask << endl;#endif if ( ( mev->button() & d->m_activationButtonMask ) == 0 ) return false; // ignore this button } Part * part; while ( w ) { QPoint pos; if ( !d->m_managedTopLevelWidgets.containsRef( w->topLevelWidget() ) ) return false; if ( d->m_bIgnoreScrollBars && w->inherits( "QScrollBar" ) ) return false; if ( mev ) // mouse press or mouse double-click event { pos = mev->globalPos(); part = findPartFromWidget( w, pos ); } else part = findPartFromWidget( w );#ifdef DEBUG_PARTMANAGER QCString evType = ( ev->type() == QEvent::MouseButtonPress ) ? "MouseButtonPress" : ( ev->type() == QEvent::MouseButtonDblClick ) ? "MouseButtonDblClick" : ( ev->type() == QEvent::FocusIn ) ? "FocusIn" : "OTHER! ERROR!";#endif if ( part ) // We found a part whose widget is w { if ( d->m_policy == PartManager::TriState ) { if ( ev->type() == QEvent::MouseButtonDblClick ) { if ( part == d->m_activePart && w == d->m_activeWidget ) return false;#ifdef DEBUG_PARTMANAGER kdDebug(1000) << "PartManager::eventFilter dblclick -> setActivePart" << part << endl;#endif d->setReason( ev ); setActivePart( part, w ); d->m_reason = NoReason; return true; } if ( ( d->m_selectedWidget != w || d->m_selectedPart != part ) && ( d->m_activeWidget != w || d->m_activePart != part ) ) { if ( part->isSelectable() ) setSelectedPart( part, w ); else {#ifdef DEBUG_PARTMANAGER kdDebug(1000) << "Part " << part << " (non-selectable) made active because " << w->className() << " got event" << " " << evType << endl;#endif d->setReason( ev ); setActivePart( part, w ); d->m_reason = NoReason; } return true; } else if ( d->m_selectedWidget == w && d->m_selectedPart == part ) {#ifdef DEBUG_PARTMANAGER kdDebug(1000) << "Part " << part << " made active (from selected) because " << w->className() << " got event" << " " << evType << endl;#endif d->setReason( ev ); setActivePart( part, w ); d->m_reason = NoReason; return true; } else if ( d->m_activeWidget == w && d->m_activePart == part ) { setSelectedPart( 0L ); return false; } return false; } else if ( part != d->m_activePart ) {#ifdef DEBUG_PARTMANAGER kdDebug(1000) << "Part " << part << " made active because " << w->className() << " got event" << " " << evType << endl;#endif d->setReason( ev ); setActivePart( part, w ); d->m_reason = NoReason; } return false; } w = w->parentWidget(); if ( w && ( ( w->testWFlags( WType_Dialog ) && w->isModal() ) || w->testWFlags( WType_Popup ) || w->testWFlags( WStyle_Tool ) ) ) {#ifdef DEBUG_PARTMANAGER kdDebug(1000) << QString("No part made active although %1/%2 got event - loop aborted").arg(obj->name()).arg(obj->className()) << endl;#endif return false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -