⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 popupmenueditor.cpp

📁 Trolltech公司发布的基于C++图形开发环境
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		return;	    } else if ( currentField == 2 ) {		setAccelerator( e->key(), e->state() );		showSubMenu();	    }	    break;	}    } else { // In edit mode	switch ( e->key() ) {	case Qt::Key_Enter:	case Qt::Key_Return:	case Qt::Key_Escape:	    leaveEditMode( e );	    e->accept();	    return;	}    }    update();}void PopupMenuEditor::focusInEvent( QFocusEvent * ){    showSubMenu();    update();    parentMenu->update();}void PopupMenuEditor::focusOutEvent( QFocusEvent * ){    QWidget * fw = qApp->focusWidget();    if ( !fw || ( !::qt_cast<PopupMenuEditor*>(fw) && fw != lineEdit ) ) {	hideSubMenu();	if ( fw && ::qt_cast<MenuBarEditor*>(fw) )	    return;	QWidget * w = this;	while ( w && w != fw && ::qt_cast<PopupMenuEditor*>(w) ) { // hide all popups	    w->hide();	    w = ((PopupMenuEditor *)w)->parentEditor();	}    }}void PopupMenuEditor::drawItem( QPainter * p, PopupMenuEditorItem * i,				const QRect & r, int f ) const{    int x = r.x();    int y = r.y();    int h = r.height();    p->fillRect( r, colorGroup().brush( QColorGroup::Background ) );    if ( i->isSeparator() ) {	style().drawPrimitive( QStyle::PE_Separator, p,			       QRect( r.x(), r.y() + 2, r.width(), 1 ),			       colorGroup(), QStyle::Style_Sunken | f );	return;    }    const QAction * a = i->action();    if ( a->isToggleAction() && a->isOn() ) {	style().drawPrimitive( QStyle::PE_CheckMark, p,			       QRect( x , y, iconWidth, h ),			       colorGroup(), f );    } else {	QPixmap icon = a->iconSet().pixmap( QIconSet::Automatic, QIconSet::Normal );	p->drawPixmap( x + ( iconWidth - icon.width() ) / 2,		       y + ( h - icon.height() ) / 2,		       icon );    }    x += iconWidth;    p->drawText( x, y, textWidth, h,		 QPainter::AlignLeft |		 QPainter::AlignVCenter |		 Qt::ShowPrefix |		 Qt::SingleLine,		 a->menuText() );    x += textWidth + borderSize * 3;    p->drawText( x, y, accelWidth, h,		 QPainter::AlignLeft | QPainter::AlignVCenter,		 a->accel() );    if ( i->count() ) // Item has submenu	style().drawPrimitive( QStyle::PE_ArrowRight, p,			       QRect( r.width() - arrowWidth, r.y(), arrowWidth, r.height() ),			       colorGroup(), f );}void PopupMenuEditor::drawWinFocusRect( QPainter * p, const QRect & r ) const{    if ( currentIndex < (int)itemList.count() &&	 ((PopupMenuEditor*)this)->itemList.at( currentIndex )->isSeparator() ) {	p->drawWinFocusRect( borderSize, r.y(), width() - borderSize * 2, r.height() );	return;    }    int y = r.y();    int h = r.height();    if ( currentField == 0 )	p->drawWinFocusRect( borderSize + 1, y, iconWidth - 2, h );    else if ( currentField == 1 )	p->drawWinFocusRect( borderSize + iconWidth, y, textWidth, h );    else if ( currentField == 2 )	p->drawWinFocusRect( borderSize + iconWidth + textWidth +			     borderSize * 3, y, accelWidth, h );}void PopupMenuEditor::drawItems( QPainter * p ){    int flags = 0;    int idx = 0;    QColorGroup enabled = colorGroup();    QColorGroup disabled = palette().disabled();    QRect focus;    QRect rect( borderSize, borderSize, width() - borderSize * 2, 0 );    PopupMenuEditorItem * i = itemList.first();    while ( i ) {	if ( i->isVisible() ) {	    rect.setHeight( itemHeight( i ) );	    if ( idx == currentIndex )		focus = rect;	    if ( i->action()->isEnabled() ) {		flags = QStyle::Style_Enabled;		p->setPen( enabled.buttonText() );	    } else {		flags = QStyle::Style_Default;		p->setPen( disabled.buttonText() );	    }	    drawItem( p, i, rect, flags );	    rect.moveBy( 0, rect.height() );	}	i = itemList.next();	idx++;    }    // Draw the "add item" and "add separator" items    p->setPen( darkBlue );    rect.setHeight( itemHeight( &addItem ) );    if ( idx == currentIndex )	focus = rect;    drawItem( p, &addItem, rect, QStyle::Style_Default );    rect.moveBy( 0, rect.height() );    idx++;    rect.setHeight( itemHeight( &addSeparator ) );    if ( idx == currentIndex )	focus = rect;    drawItem( p, &addSeparator, rect, QStyle::Style_Default );    idx++;    if ( hasFocus() && !draggedItem )	drawWinFocusRect( p, focus );}QSize PopupMenuEditor::contentsSize(){    QRect textRect = fontMetrics().boundingRect( addSeparator.action()->menuText() );    textWidth = textRect.width();    accelWidth = textRect.height(); // default size    iconWidth = textRect.height();    int w = 0;    int h = itemHeight( &addItem ) + itemHeight( &addSeparator );    PopupMenuEditorItem * i = itemList.first();    QAction * a = 0;    while ( i ) {	if ( i->isVisible() ) {	    if ( !i->isSeparator() ) {		a = i->action();		w = a->iconSet().pixmap( QIconSet::Automatic, QIconSet::Normal ).rect().width() +		    borderSize; // padding		iconWidth = QMAX( iconWidth, w );		w = fontMetrics().boundingRect( a->menuText() ).width();		textWidth = QMAX( textWidth, w );		w = fontMetrics().boundingRect( a->accel() ).width() + 2; // added padding?		accelWidth = QMAX( accelWidth, w );	    }	    h += itemHeight( i );	}	i = itemList.next();    }    int width = iconWidth + textWidth + borderSize * 3 + accelWidth + arrowWidth;    return QSize( width, h );}int PopupMenuEditor::itemHeight( const PopupMenuEditorItem * item ) const{    if ( !item || ( item && !item->isVisible() ) )	return 0;    if ( item->isSeparator() )	return 4; // FIXME: hardcoded ( get from styles )r    int padding =  + borderSize * 6;    QAction * a = item->action();    int h = a->iconSet().pixmap( QIconSet::Automatic, QIconSet::Normal ).rect().height();    h = QMAX( h, fontMetrics().boundingRect( a->menuText() ).height() + padding );    h = QMAX( h, fontMetrics().boundingRect( a->accel() ).height() + padding );    return h;}int PopupMenuEditor::itemPos( const PopupMenuEditorItem * item ) const{    PopupMenuEditor * that = ( PopupMenuEditor * ) this;    int y = 0;    PopupMenuEditorItem * i = that->itemList.first();    while ( i ) {	if ( i == item )	    return y;	y += itemHeight( i );	i = that->itemList.next();    }    return y;}int PopupMenuEditor::snapToItem( int y ){    int iy = 0;    int dy = 0;    PopupMenuEditorItem * i = itemList.first();    while ( i ) {	    dy = itemHeight( i );	    if ( iy + dy / 2 > y )		return iy;	    iy += dy;	i = itemList.next();    }    return iy;}void PopupMenuEditor::dropInPlace( PopupMenuEditorItem * i, int y ){    int iy = 0;    int dy = 0;    int idx = 0;    PopupMenuEditorItem * n = itemList.first();    while ( n ) {	dy = itemHeight( n );	if ( iy + dy / 2 > y )	    break;	iy += dy;	idx++;	n = itemList.next();    }    int same = itemList.findRef( i );    AddActionToPopupCommand * cmd = new AddActionToPopupCommand( "Drop Item", formWnd, this, i, idx );    formWnd->commandHistory()->addCommand( cmd );    cmd->execute();    currentIndex = ( same >= 0 && same < idx ) ? idx - 1 : idx;    currentField = 1;}void PopupMenuEditor::dropInPlace( QActionGroup * g, int y ){    if (!g->children())	return;    QObjectList l = *g->children();    for ( int i = 0; i < (int)l.count(); ++i ) {	QAction *a = ::qt_cast<QAction*>(l.at(i));	QActionGroup *g = ::qt_cast<QActionGroup*>(l.at(i));	if ( g )	    dropInPlace( g, y );	else if ( a )	    dropInPlace( new PopupMenuEditorItem( a, this ), y );    }}void PopupMenuEditor::safeDec(){    do  {	currentIndex--;    } while ( currentIndex > 0 && !currentItem()->isVisible() );    if ( currentIndex == 0 &&	 !currentItem()->isVisible() &&	 parentMenu ) {	parentMenu->setFocus();    }}void PopupMenuEditor::safeInc(){    int max = itemList.count() + 1;    if ( currentIndex < max ) {	do  {	    currentIndex++;	} while ( currentIndex < max && !currentItem()->isVisible() ); // skip invisible items    }}void PopupMenuEditor::clearCurrentField(){    if ( currentIndex >= (int)itemList.count() )	return; // currentIndex is addItem or addSeparator    PopupMenuEditorItem * i = currentItem();    hideSubMenu();    if ( i->isSeparator() )	return;    if ( currentField == 0 ) {	QIconSet icons( 0 );	SetActionIconsCommand * cmd = new SetActionIconsCommand( "Remove icon",								 formWnd,								 i->action(),								 this,								 icons );	formWnd->commandHistory()->addCommand( cmd );	cmd->execute();    } else if ( currentField == 2 ) {	i->action()->setAccel( 0 );    }    resizeToContents();    showSubMenu();    return;}void PopupMenuEditor::navigateUp( bool ctrl ){    if ( currentIndex > 0 ) {	hideSubMenu();	if ( ctrl ) {	    ExchangeActionInPopupCommand * cmd =		new ExchangeActionInPopupCommand( "Move Item Up",						  formWnd,						  this,						  currentIndex,						  currentIndex - 1 );	    formWnd->commandHistory()->addCommand( cmd );	    cmd->execute();	    safeDec();	} else {	    safeDec();	}	showSubMenu();    } else if ( parentMenu ) {	parentMenu->setFocus();	parentMenu->update();    }}void PopupMenuEditor::navigateDown( bool ctrl ){    hideSubMenu();    if ( ctrl ) {	if ( currentIndex < ( (int)itemList.count() - 1 ) ) { // safe index	    ExchangeActionInPopupCommand * cmd =		new ExchangeActionInPopupCommand( "Move Item Down",						  formWnd,						  this,						  currentIndex,						  currentIndex + 1 );	    formWnd->commandHistory()->addCommand( cmd );	    cmd->execute();	    safeInc();	}    } else { // ! Ctrl	safeInc();    }    if ( currentIndex >= (int)itemList.count() )	currentField = 1;    showSubMenu();}void PopupMenuEditor::navigateLeft(){    if ( currentItem()->isSeparator() ||	 currentIndex >= (int)itemList.count() ||	 currentField == 0 ) {	if ( parentMenu ) {	    hideSubMenu();	    parentMenu->setFocus();	} else if ( !currentItem()->isSeparator() ) {	    currentField = 2;	}    } else {	currentField--;    }}void PopupMenuEditor::navigateRight(){    if ( !currentItem()->isSeparator() &&	 currentIndex < (int)itemList.count() ) {	if ( currentField == 2 ) {	    focusOnSubMenu();	} else {	    currentField++;	    currentField %= 3;	}    }}void PopupMenuEditor::enterEditMode( QKeyEvent * e ){    PopupMenuEditorItem * i = currentItem();    if ( i == &addSeparator ) {	i = createItem( new QSeparatorAction( 0 ) );    } else if ( i->isSeparator() ) {	return;    } else if ( currentField == 0 ) {	choosePixmap();    } else if ( currentField == 1 ) {	showLineEdit();	return;    } else {// currentField == 2	setAccelerator( e->key(), e->state() );    }    showSubMenu();    return;}void PopupMenuEditor::leaveEditMode( QKeyEvent * e ){    setFocus();    lineEdit->hide();    PopupMenuEditorItem * i = 0;    if ( e && e->key() == Qt::Key_Escape ) { 	update();	return;    }    if ( currentIndex >= (int)itemList.count() ) {	// new item was created	QAction * a = formWnd->mainWindow()->actioneditor()->newActionEx();	QString actionText = lineEdit->text();	actionText.replace("&&", "&");	QString menuText = lineEdit->text();	a->setText( actionText );	a->setMenuText( menuText );	i = createItem( a );	QString n = constructName( i );	formWindow()->unify( a, n, TRUE );	a->setName( n );	MetaDataBase::addEntry( a );	MetaDataBase::setPropertyChanged( a, "menuText", TRUE );	ActionEditor *ae = (ActionEditor*)formWindow()->mainWindow()->child( 0, "ActionEditor" );	if ( ae )	    ae->updateActionName( a );    } else {	i = itemList.at( currentIndex );	RenameActionCommand * cmd = new RenameActionCommand( "Rename Item",							     formWnd,							     i->action(),							     this,							     lineEdit->text() );	formWnd->commandHistory()->addCommand( cmd );	cmd->execute();    }    resizeToContents();    if ( !i )	return;    if ( i->isSeparator() )	hideSubMenu();    else	showSubMenu();}QString PopupMenuEditor::constructName( PopupMenuEditorItem *item ){    QString s;    QString name = item->action()->menuText();    QWidget *e = parentEditor();    PopupMenuEditor *p = ::qt_cast<PopupMenuEditor*>(e);    if ( p ) {	int idx = p->find( item->m );	PopupMenuEditorItem * i = ( idx > -1 ? p->at( idx ) : 0 );	s = ( i ? QString( i->action()->name() ).remove( "Action" ) : QString( "" ) );    } else {	MenuBarEditor *b = ::qt_cast<MenuBarEditor*>(e);	if ( b ) {	    int idx = b->findItem( item->m );	    MenuBarEditorItem * i = ( idx > -1 ? b->item( idx ) : 0 );	    s = ( i ? i->menuText().lower() : QString( "" ) );	}    }    // replace illegal characters    return ( RenameMenuCommand::makeLegal( s ) +	     RenameMenuCommand::makeLegal( name ) + "Action" );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -