Qwt为拾取操作提供了状态机设置,用于控制拾取操作的不同行为。
void setStateMachine( QwtPickerMachine * );
其实现如下:
/*!
Set a state machine and delete the previous one
\param stateMachine State machine
\sa stateMachine()
*/
void QwtPicker::setStateMachine( QwtPickerMachine *stateMachine )
{
if ( d_data->stateMachine != stateMachine )
{
reset();
delete d_data->stateMachine;
d_data->stateMachine = stateMachine;
if ( d_data->stateMachine )
d_data->stateMachine->reset();
}
}
这里其实就是State模式[GOF《设计模式》]的应用。State模式的UML图如下:
角色分配:
QwtPicker 相当于UML图中的Context类;
QwtPickerMachine相当于状态基类State; 而QwtPickerTrackerMachine, QwtPickerClickPointMachine, QwtPickerDragPointMachine, QwtPickerClickRectMachine, QwtPickerDragRectMachine, QwtPickerPolygonMachine则是具体的状态子类。
QwtPicker::transition() 相当于Context::Request();
virtual void transition( const QEvent * );
QwtPickerMachine::transition( )相当于Handle();
//! Transition
virtual QList<Command> transition(
const QwtEventPattern &, const QEvent * ) = 0;
最后给上状态机类的继承关系图: