📄 qaccessiblewidget.cpp
字号:
#include "qaccessiblewidget.h"#include <qapplication.h>#include <qstyle.h>#include <qobjectlist.h>#include <qpushbutton.h>#include <qslider.h>#include <qdial.h>#include <qspinbox.h>#include <qscrollbar.h>#include <qslider.h>#include <qlineedit.h>#include <qlabel.h>#include <qlcdnumber.h>#include <qprogressbar.h>#include <qgroupbox.h>#include <qtoolbutton.h>#include <qwhatsthis.h>#include <qtooltip.h>#include <qscrollview.h>#include <qheader.h>#include <qtabbar.h>#include <qcombobox.h>#include <qrangecontrol.h>#include <qlistbox.h>#include <qlistview.h>#include <qiconview.h>#include <qtextedit.h>#include <qwidgetstack.h>#include <private/qtitlebar_p.h>QString buddyString( QWidget *widget ) { QWidget *parent = widget->parentWidget(); QObjectList *ol = parent->queryList( "QLabel", 0, FALSE, FALSE ); if ( !ol || !ol->count() ) { delete ol; return QString::null; } QString str; QObjectListIt it(*ol); while ( it.current() ) { QLabel *label = (QLabel*)it.current(); ++it; if ( label->buddy() == widget ) { str = label->text(); break; } } delete ol; if ( !!str ) return str; if ( parent->inherits( "QGroupBox" ) ) return ((QGroupBox*)parent)->title(); return QString::null;}QString stripAmp( const QString &text ){ if ( text.isEmpty() ) return text; QString n = text; for ( uint i = 0; i < n.length(); i++ ) { if ( n[(int)i] == '&' ) n.remove( i, 1 ); } return n;}QString hotKey( const QString &text ){ if ( text.isEmpty() ) return text; QString n = text; int fa = 0; bool ac = FALSE; while ( ( fa = n.find( "&", fa ) ) != -1 ) { if ( n.at(fa+1) != '&' ) { ac = TRUE; break; } } if ( fa != -1 && ac ) return QString( n.at(fa + 1) ); return QString::null;}/*! \class QAccessibleWidget qaccessiblewidget.h \brief The QAccessibleWidget class implements the QAccessibleInterface for QWidgets.*/ulong QAccessibleWidget::objects = 0;/*! Creates a QAccessibleWidget object for \a o. \a role, \a name, \a description, \a value, \a help, \a defAction, \a accelerator and \a state are optional parameters for static values of the object's property.*/QAccessibleWidget::QAccessibleWidget( QObject *o, Role role, QString name, QString description, QString value, QString help, QString defAction, QString accelerator, State state ) : QAccessibleObject( o ), role_(role), name_(name), description_(description),value_(value),help_(help), defAction_(defAction), accelerator_(accelerator), state_(state){ objects++;}QAccessibleWidget::~QAccessibleWidget(){ objects--;}/*! Returns the widget. */QWidget *QAccessibleWidget::widget() const{ Q_ASSERT(object()->isWidgetType()); if ( !object()->isWidgetType() ) return 0; return (QWidget*)object();}/*! \reimp */int QAccessibleWidget::controlAt( int x, int y ) const{ QWidget *w = widget(); QPoint gp = w->mapToGlobal( QPoint( 0, 0 ) ); if ( !QRect( gp.x(), gp.y(), w->width(), w->height() ).contains( x, y ) ) return -1; QPoint rp = w->mapFromGlobal( QPoint( x, y ) ); QObjectList *list = w->queryList( "QWidget", 0, FALSE, FALSE ); if ( !list || list->isEmpty() ) return 0; QObjectListIt it( *list ); QWidget *child = 0; int index = 1; while ( ( child = (QWidget*)it.current() ) ) { if ( !child->isTopLevel() && !child->isHidden() && child->geometry().contains( rp ) ) { delete list; return index; } ++it; ++index; } delete list; return 0;}/*! \reimp */QRect QAccessibleWidget::rect( int control ) const{#if defined(QT_DEBUG) if ( control ) qWarning( "QAccessibleWidget::rect: This implementation does not support subelements! (ID %d unknown for %s)", control, widget()->className() );#else Q_UNUSED(control)#endif QWidget *w = widget(); QPoint wpos = w->mapToGlobal( QPoint( 0, 0 ) ); return QRect( wpos.x(), wpos.y(), w->width(), w->height() );}/*! \reimp */int QAccessibleWidget::navigate( NavDirection dir, int startControl ) const{#if defined(QT_DEBUG) if ( startControl ) qWarning( "QAccessibleWidget::navigate: This implementation does not support subelements! (ID %d unknown for %s)", startControl, widget()->className() );#else Q_UNUSED(startControl);#endif QWidget *w = widget(); QObject *o = 0; switch ( dir ) { case NavFirstChild: { QObjectList *list = widget()->queryList( "QWidget", 0, FALSE, FALSE ); bool has = !list->isEmpty(); delete list; return has ? 1 : -1; } case NavLastChild: { QObjectList *list = widget()->queryList( "QWidget", 0, FALSE, FALSE ); bool has = !list->isEmpty(); delete list; return has ? childCount() : -1; } case NavNext: case NavPrevious: { QWidget *parent = w->parentWidget(); QObjectList *sl = parent ? parent->queryList( "QWidget", 0, FALSE, FALSE ) : 0; if ( !sl ) return -1; QObject *sib; QObjectListIt it( *sl ); int index; if ( dir == NavNext ) { index = 1; while ( ( sib = it.current() ) ) { ++it; ++index; if ( sib == w ) break; } } else { it.toLast(); index = sl->count(); while ( ( sib = it.current() ) ) { --it; --index; if ( sib == w ) break; } } sib = it.current(); delete sl; if ( sib ) return index; return -1; } break; case NavFocusChild: { if ( w->hasFocus() ) return 0; QWidget *w2 = w->focusWidget(); if ( !w2 ) return -1; QObjectList *list = w->queryList( "QWidget", 0, FALSE, FALSE ); int index = list->findRef( w2 ); delete list; return ( index != -1 ) ? index+1 : -1; } default: qWarning( "QAccessibleWidget::navigate: unhandled request" ); break; }; return -1;}/*! \reimp */int QAccessibleWidget::childCount() const{ QObjectList *cl = widget()->queryList( "QWidget", 0, FALSE, FALSE ); if ( !cl ) return 0; int count = cl->count(); delete cl; return count;}/*! \reimp */QRESULT QAccessibleWidget::queryChild( int control, QAccessibleInterface **iface ) const{ *iface = 0; QObjectList *cl = widget()->queryList( "QWidget", 0, FALSE, FALSE ); if ( !cl ) return QS_FALSE; QObject *o = 0; if ( cl->count() >= (uint)control ) o = cl->at( control-1 ); delete cl; if ( !o ) return QS_FALSE; return QAccessible::queryAccessibleInterface( o, iface );}/*! \reimp */QRESULT QAccessibleWidget::queryParent( QAccessibleInterface **iface ) const{ return QAccessible::queryAccessibleInterface( widget()->parentWidget(), iface );}/*! \reimp */bool QAccessibleWidget::doDefaultAction( int control ){#if defined(QT_DEBUG) if ( control ) qWarning( "QAccessibleWidget::doDefaultAction: This implementation does not support subelements! (ID %d unknown for %s)", control, widget()->className() );#else Q_UNUSED(control)#endif return FALSE;}/*! \reimp */QString QAccessibleWidget::text( Text t, int control ) const{ switch ( t ) { case DefaultAction: return defAction_; case Description: if ( !control && description_.isNull() ) { QString desc = QToolTip::textFor( widget() ); return desc; } return description_; case Help: if ( !control && help_.isNull() ) { QString help = QWhatsThis::textFor( widget() ); return help; } return help_; case Accelerator: return accelerator_; case Name: { if ( !control && name_.isNull() && widget()->isTopLevel() ) return widget()->caption(); return name_; } case Value: return value_; default: break; } return QString::null;}/*! \reimp */void QAccessibleWidget::setText( Text t, int /*control*/, const QString &text ){ switch ( t ) { case DefaultAction: defAction_ = text; break; case Description: description_ = text; break; case Help: help_ = text; break; case Accelerator: accelerator_ = text; break; case Name: name_ = text; break; case Value: value_ = text; break; default: break; }}/*! \reimp */QAccessible::Role QAccessibleWidget::role( int control ) const{ if ( !control ) return role_; return NoRole;}/*! \reimp */QAccessible::State QAccessibleWidget::state( int control ) const{ if ( control ) return Normal; if ( state_ != Normal ) return state_; int state = Normal; QWidget *w = widget(); if ( w->isHidden() ) state |= Invisible; if ( w->focusPolicy() != QWidget::NoFocus && w->isActiveWindow() ) state |= Focusable; if ( w->hasFocus() ) state |= Focused; if ( !w->isEnabled() ) state |= Unavailable; if ( w->isTopLevel() ) { state |= Moveable; if ( w->minimumSize() != w->maximumSize() ) state |= Sizeable; } return (State)state;}/*! \reimp */bool QAccessibleWidget::setFocus( int control ){#if defined(QT_DEBUG) if ( control ) qWarning( "QAccessibleWidget::setFocus: This implementation does not support subelements! (ID %d unknown for %s)", control, widget()->className() );#else Q_UNUSED(control)#endif if ( widget()->focusPolicy() != QWidget::NoFocus ) { widget()->setFocus(); return TRUE; } return FALSE;}/*! \reimp */bool QAccessibleWidget::setSelected( int, bool, bool ){#if defined(QT_DEBUG) qWarning( "QAccessibleWidget::setSelected: This function not supported for simple widgets." );#endif return FALSE;}/*! \reimp */void QAccessibleWidget::clearSelection(){#if defined(QT_DEBUG) qWarning( "QAccessibleWidget::clearSelection: This function not supported for simple widgets." );#endif}/*! \reimp */QMemArray<int> QAccessibleWidget::selection() const{ return QMemArray<int>();}/*! \class QAccessibleWidgetStack qaccessible.h \brief The QAccessibleWidgetStack class implements the QAccessibleInterface for widget stacks.*//*! Creates a QAccessibleWidgetStack object for \a o.*/QAccessibleWidgetStack::QAccessibleWidgetStack( QObject *o ): QAccessibleWidget( o ){ Q_ASSERT( o->inherits("QWidgetStack") );}/*! Returns the widget stack. */QWidgetStack *QAccessibleWidgetStack::widgetStack() const{ return (QWidgetStack*)object();}/*! \reimp */int QAccessibleWidgetStack::controlAt( int, int ) const{ return widgetStack()->id( widgetStack()->visibleWidget() ) + 1;}/*! \reimp */QRESULT QAccessibleWidgetStack::queryChild( int control, QAccessibleInterface **iface ) const{ if ( !control ) { *iface = (QAccessibleInterface*)this; QS_OK; } QWidget *widget = widgetStack()->widget( control - 1 ); if ( !widget ) return QAccessibleWidget::queryChild( control, iface ); return QAccessible::queryAccessibleInterface( widgetStack()->widget( control - 1 ), iface );}/*! \class QAccessibleButton qaccessible.h \brief The QAccessibleButton class implements the QAccessibleInterface for button type widgets.*//*! Creates a QAccessibleButton object for \a o. \a role, \a description and \a help are propagated to the QAccessibleWidget constructor.*/QAccessibleButton::QAccessibleButton( QObject *o, Role role, QString description, QString help ): QAccessibleWidget( o, role, QString::null, description, QString::null, QString::null, QString::null, QString::null ){ Q_ASSERT(o->inherits("QButton"));}/*! \reimp */bool QAccessibleButton::doDefaultAction( int control ){ if ( !widget()->isEnabled() ) return FALSE; Role r = role(control); if ( r == PushButton || r == CheckBox || r == RadioButton ) { ((QButton*)object())->animateClick(); } else if ( object()->inherits("QToolButton") ) { QToolButton *tb = (QToolButton*)object(); tb->openPopup(); } return TRUE;}/*! \reimp */QString QAccessibleButton::text( Text t, int control ) const{ QString tx = QAccessibleWidget::text( t, control ); if ( !!tx ) return tx; switch ( t ) { case DefaultAction: switch( role(control) ) { case PushButton: return QButton::tr("Press"); case CheckBox: if ( state(control) & Checked ) return QButton::tr("UnCheck"); return QButton::tr("Check"); case RadioButton: return QButton::tr("Check"); default: return QButton::tr("Press"); } case Accelerator: tx = hotKey( ((QButton*)widget())->text() ); if ( !!tx ) { tx = "Alt + "+tx; } else { tx = hotKey( buddyString( widget() ) ); if ( !!tx ) tx = "Alt + "+tx; } return tx;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -