void IconPalette::showEvent(QShowEvent * /*event*/) { resize(sizeHint()); setMaximumSize(sizeHint()); int hoffset = - parentWidget()->pos().x(); int voffset = - parentWidget()->pos().y(); int const parwidth = parentWidget()->geometry().width(); int const parheight = parentWidget()->geometry().height(); // vertical toolbar? QToolBar * toolbar = qobject_cast<QToolBar *>(parentWidget()->parentWidget()); if (toolbar && toolbar->orientation() == Qt::Vertical) { hoffset += parwidth; voffset -= parheight; } QRect const screen = qApp->desktop()->availableGeometry(this); QPoint const gpos = parentWidget()->mapToGlobal( parentWidget()->geometry().bottomLeft()); // space to the right? if (gpos.x() + hoffset + width() > screen.width()) { hoffset -= width(); if (toolbar && toolbar->orientation() == Qt::Vertical) hoffset -= parwidth; else hoffset += parwidth; } // space at the bottom? if (gpos.y() + voffset + height() > screen.height()) { voffset -= height(); if (toolbar && toolbar->orientation() == Qt::Horizontal) voffset -= parheight; else voffset += parheight; } QRect r = rect(); r.moveTo(gpos.x() + hoffset, gpos.y() + voffset); setGeometry(r); }
QSizePolicy PictButton::sizePolicy() const { QSizePolicy p = QToolButton::sizePolicy(); QToolBar *bar = static_cast<QToolBar*>(parent()); if (bar->orientation() == Vertical){ p.setVerData(QSizePolicy::Expanding); }else{ p.setHorData(QSizePolicy::Expanding); } return p; }
QSize PictButton::minimumSizeHint() const { int wChar = QFontMetrics(font()).width('0'); QSize p = QToolButton:: minimumSizeHint(); QToolBar *bar = static_cast<QToolBar*>(parent()); if (bar->orientation() == Vertical){ p.setHeight(p.height() + 2 * wChar + 16); }else{ p.setWidth(p.width() + 2 * wChar + 16); } return p; }
bool QSeparatorAction::addTo( QWidget *w ) { if ( ::qt_cast<QToolBar*>(w) ) { QToolBar *tb = (QToolBar*)w; wid = new QDesignerToolBarSeparator( tb->orientation(), tb ); return TRUE; } else if ( ::qt_cast<QPopupMenu*>(w) ) { idx = ( (QPopupMenu*)w )->count(); ( (QPopupMenu*)w )->insertSeparator( idx ); return TRUE; } return FALSE; }
void CToolButton::btnClicked() { if (popup == NULL) return; QPoint pos; QToolBar *bar = static_cast<QToolBar*>(parent()); if (bar->orientation() == Vertical){ pos = QPoint(width(), 0); }else{ pos = QPoint(0, height()); } pos = mapToGlobal(pos); popup->popup(pos); }
void QPushButtonPrivate::_q_popupPressed() { Q_Q(QPushButton); if (!down || !menu) return; menu->setNoReplayFor(q); bool horizontal = true; #if !defined(QT_NO_TOOLBAR) QToolBar *tb = qobject_cast<QToolBar*>(q->parentWidget()); if (tb && tb->orientation() == Qt::Vertical) horizontal = false; #endif QWidgetItem item(q); QRect rect = item.geometry(); rect.setRect(rect.x() - q->x(), rect.y() - q->y(), rect.width(), rect.height()); QSize menuSize = menu->sizeHint(); QPoint globalPos = q->mapToGlobal(rect.topLeft()); int x = globalPos.x(); int y = globalPos.y(); if (horizontal) { if (globalPos.y() + rect.height() + menuSize.height() <= qApp->desktop()->height()) { y += rect.height(); } else { y -= menuSize.height(); } if (q->layoutDirection() == Qt::RightToLeft) x += rect.width() - menuSize.width(); } else { if (globalPos.x() + rect.width() + menu->sizeHint().width() <= qApp->desktop()->width()) x += rect.width(); else x -= menuSize.width(); } QPointer<QPushButton> guard(q); //Because of a delay in menu effects, we must keep track of the //menu visibility to avoid flicker on button release menuOpen = true; menu->exec(QPoint(x, y)); if (guard) { menuOpen = false; q->setDown(false); } }
void PictButton::paintEvent(QPaintEvent *e) { if (icon) CToolButton::paintEvent(e); QPainter p(this); QRect rc(4, 4, width() - 4, height() - 4); if (icon){ const QPixmap &pict = Pict(icon); QToolBar *bar = static_cast<QToolBar*>(parent()); if (bar->orientation() == Vertical){ p.drawPixmap((width() - pict.width()) / 2, 4, pict); QWMatrix m; m.rotate(90); p.setWorldMatrix(m); rc = QRect(8 + pict.height(), -4, height() - 4, 4 - width()); }else{ p.drawPixmap(4, (height() - pict.height()) / 2, pict); rc = QRect(8 + pict.width(), 4, width() - 4, height() - 4); } } p.drawText(rc, AlignLeft | AlignVCenter, text); }
QPoint QPushButtonPrivate::adjustedMenuPosition() { Q_Q(QPushButton); bool horizontal = true; #if !defined(QT_NO_TOOLBAR) QToolBar *tb = qobject_cast<QToolBar*>(parent); if (tb && tb->orientation() == Qt::Vertical) horizontal = false; #endif QWidgetItem item(q); QRect rect = item.geometry(); rect.setRect(rect.x() - q->x(), rect.y() - q->y(), rect.width(), rect.height()); QSize menuSize = menu->sizeHint(); QPoint globalPos = q->mapToGlobal(rect.topLeft()); int x = globalPos.x(); int y = globalPos.y(); if (horizontal) { if (globalPos.y() + rect.height() + menuSize.height() <= QApplication::desktop()->availableGeometry(q).height()) { y += rect.height(); } else { y -= menuSize.height(); } if (q->layoutDirection() == Qt::RightToLeft) x += rect.width() - menuSize.width(); } else { if (globalPos.x() + rect.width() + menu->sizeHint().width() <= QApplication::desktop()->availableGeometry(q).width()) x += rect.width(); else x -= menuSize.width(); } return QPoint(x,y); }
void tst_QToolBar::orientation() { QToolBar tb; QCOMPARE(tb.orientation(), Qt::Horizontal); QSignalSpy spy(&tb, SIGNAL(orientationChanged(Qt::Orientation))); tb.setOrientation(Qt::Vertical); QCOMPARE(tb.orientation(), Qt::Vertical); QCOMPARE(spy.count(), 1); QCOMPARE(*static_cast<const Qt::Orientation *>(spy.at(0).value(0).constData()), tb.orientation()); spy.clear(); tb.setOrientation(tb.orientation()); QCOMPARE(spy.count(), 0); tb.setOrientation(Qt::Horizontal); QCOMPARE(tb.orientation(), Qt::Horizontal); QCOMPARE(spy.count(), 1); QCOMPARE(*static_cast<const Qt::Orientation *>(spy.at(0).value(0).constData()), tb.orientation()); spy.clear(); tb.setOrientation(tb.orientation()); QCOMPARE(spy.count(), 0); tb.setOrientation(Qt::Vertical); QCOMPARE(tb.orientation(), Qt::Vertical); QCOMPARE(spy.count(), 1); QCOMPARE(*static_cast<const Qt::Orientation *>(spy.at(0).value(0).constData()), tb.orientation()); spy.clear(); tb.setOrientation(tb.orientation()); QCOMPARE(spy.count(), 0); tb.setOrientation(Qt::Horizontal); QCOMPARE(tb.orientation(), Qt::Horizontal); QCOMPARE(spy.count(), 1); QCOMPARE(*static_cast<const Qt::Orientation *>(spy.at(0).value(0).constData()), tb.orientation()); spy.clear(); tb.setOrientation(tb.orientation()); QCOMPARE(spy.count(), 0); tb.setOrientation(Qt::Vertical); QCOMPARE(tb.orientation(), Qt::Vertical); QCOMPARE(spy.count(), 1); QCOMPARE(*static_cast<const Qt::Orientation *>(spy.at(0).value(0).constData()), tb.orientation()); spy.clear(); tb.setOrientation(tb.orientation()); QCOMPARE(spy.count(), 0); }
void WindowsModernStyle::drawComplexControl( ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget ) const { switch ( control ) { case CC_ToolButton: { QToolBar* toolBar; if ( widget && ( toolBar = qobject_cast<QToolBar*>( widget->parentWidget() ) ) ) { if ( const QStyleOptionToolButton* optionToolButton = qstyleoption_cast<const QStyleOptionToolButton*>( option ) ) { QRect buttonRect = subControlRect( control, option, SC_ToolButton, widget ); QStyle::State buttonState = option->state & ~State_Sunken; if ( option->state & State_Sunken ) { if ( optionToolButton->activeSubControls & SC_ToolButton ) buttonState |= State_Sunken; else if ( optionToolButton->activeSubControls & SC_ToolButtonMenu ) buttonState |= State_MouseOver; } bool selected = buttonState & State_MouseOver && option->state & State_Enabled; bool checked = buttonState & State_On; bool sunken = buttonState & State_Sunken; if ( selected || checked || sunken ) { QRect rect = buttonRect.adjusted( 0, 0, -1, -1 ); painter->setPen( m_colorItemBorder ); QLinearGradient gradient; if ( toolBar->orientation() == Qt::Vertical ) gradient = QLinearGradient( rect.topLeft(), rect.topRight() ); else gradient = QLinearGradient( rect.topLeft(), rect.bottomLeft() ); if ( sunken || (selected && checked) ) { gradient.setColorAt( 0.0, m_colorItemSunkenBegin ); gradient.setColorAt( 0.5, m_colorItemSunkenMiddle ); gradient.setColorAt( 1.0, m_colorItemSunkenEnd ); } else if ( checked ) { gradient.setColorAt( 0.0, m_colorItemCheckedBegin ); gradient.setColorAt( 0.5, m_colorItemCheckedMiddle ); gradient.setColorAt( 1.0, m_colorItemCheckedEnd ); } else { gradient.setColorAt( 0.0, m_colorItemBackgroundBegin ); gradient.setColorAt( 0.5, m_colorItemBackgroundMiddle ); gradient.setColorAt( 1.0, m_colorItemBackgroundEnd ); } painter->setBrush( gradient ); painter->drawRect( rect ); } QStyleOptionToolButton optionLabel = *optionToolButton; int fw = pixelMetric( PM_DefaultFrameWidth, option, widget ); optionLabel.rect = buttonRect.adjusted( fw, fw, -fw, -fw ); drawControl( CE_ToolButtonLabel, &optionLabel, painter, widget ); if ( optionToolButton->subControls & SC_ToolButtonMenu ) { QStyleOption optionMenu = *optionToolButton; optionMenu.rect = subControlRect( control, option, SC_ToolButtonMenu, widget ); optionMenu.state = optionToolButton->state & ~State_Sunken; if ( optionToolButton->state & State_Sunken ) { if ( optionToolButton->activeSubControls & SC_ToolButton ) optionMenu.state |= State_MouseOver | State_Sunken; else if ( optionToolButton->activeSubControls & SC_ToolButtonMenu ) optionMenu.state |= State_Sunken; } drawPrimitive( PE_IndicatorButtonDropDown, &optionMenu, painter, widget ); } else if ( optionToolButton->features & QStyleOptionToolButton::HasMenu ) { int size = pixelMetric( PM_MenuButtonIndicator, option, widget ); QRect rect = optionToolButton->rect; QStyleOptionToolButton optionArrow = *optionToolButton; optionArrow.rect = QRect( rect.right() + 4 - size, rect.height() - size + 4, size - 5, size - 5 ); drawPrimitive( PE_IndicatorArrowDown, &optionArrow, painter, widget ); } return; } } break; } default: break; } if ( useVista() ) QWindowsVistaStyle::drawComplexControl( control, option, painter, widget ); else QWindowsXPStyle::drawComplexControl( control, option, painter, widget ); }
void WindowsModernStyle::drawPrimitive( PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget ) const { switch ( element ) { case PE_Widget: if ( qobject_cast<const QMainWindow*>( widget ) ) { QRect rect = option->rect; if ( QStatusBar* statusBar = widget->findChild<QStatusBar*>() ) { rect.adjust( 0, 0, 0, -statusBar->height() ); painter->setPen( option->palette.light().color() ); painter->drawLine( rect.bottomLeft() + QPoint( 0, 1 ), rect.bottomRight() + QPoint( 0, 1 ) ); } QLinearGradient gradient( option->rect.topLeft(), option->rect.topRight() ); gradient.setColorAt( 0.0, m_colorBackgroundBegin ); gradient.setColorAt( 0.6, m_colorBackgroundEnd ); painter->fillRect( rect, gradient ); return; } if ( qobject_cast<const QToolBox*>( widget ) ) { QLinearGradient gradient( option->rect.topLeft(), option->rect.topRight() ); gradient.setColorAt( 0.4, m_colorBackgroundBegin ); gradient.setColorAt( 1.0, m_colorBackgroundEnd ); painter->fillRect( option->rect, gradient ); return; } if ( isToolBoxPanel( widget ) ) { QLinearGradient gradient( option->rect.topLeft(), option->rect.topRight() ); gradient.setColorAt( 0.4, m_colorBarMiddle ); gradient.setColorAt( 1.0, m_colorBarBegin ); painter->fillRect( option->rect, gradient ); return; } break; case PE_WindowGradient: { QLinearGradient gradient( option->rect.topLeft(), option->rect.topRight() ); gradient.setColorAt( 0.0, m_colorBackgroundBegin ); gradient.setColorAt( 0.6, m_colorBackgroundEnd ); painter->fillRect( option->rect, gradient ); return; } case PE_PanelMenuBar: return; case PE_FrameMenu: painter->setPen( m_colorMenuBorder ); painter->setBrush( Qt::NoBrush ); painter->drawRect( option->rect.adjusted( 0, 0, -1, -1 ) ); if ( const QMenu* menu = qobject_cast<const QMenu*>( widget ) ) { if ( const QMenuBar* menuBar = qobject_cast<const QMenuBar*>( menu->parent() ) ) { QRect rect = menuBar->actionGeometry( menu->menuAction() ); if ( !rect.isEmpty() ) { painter->setPen( m_colorMenuBackground ); painter->drawLine( 1, 0, rect.width() - 2, 0 ); } } } if ( const QToolBar* toolBar = qobject_cast<const QToolBar*>( widget ) ) { QRect rect = option->rect.adjusted( 1, 1, -1, -1 ); QLinearGradient gradient; if ( toolBar->orientation() == Qt::Vertical ) gradient = QLinearGradient( rect.topLeft(), rect.topRight() ); else gradient = QLinearGradient( rect.topLeft(), rect.bottomLeft() ); gradient.setColorAt( 0.0, m_colorBarBegin ); gradient.setColorAt( 0.4, m_colorBarMiddle ); gradient.setColorAt( 0.6, m_colorBarMiddle ); gradient.setColorAt( 1.0, m_colorBarEnd ); painter->fillRect( rect, gradient ); } return; case PE_IndicatorToolBarHandle: if ( option->state & State_Horizontal ) { for ( int i = option->rect.height() / 5; i <= 4 * ( option->rect.height() / 5 ); i += 5 ) { int x = option->rect.left() + 3; int y = option->rect.top() + i + 1; painter->fillRect( x + 1, y, 2, 2, m_colorHandleLight ); painter->fillRect( x, y - 1, 2, 2, m_colorHandle ); } } else { for ( int i = option->rect.width() / 5; i <= 4 * ( option->rect.width() / 5 ); i += 5 ) { int x = option->rect.left() + i + 1; int y = option->rect.top() + 3; painter->fillRect( x, y + 1, 2, 2, m_colorHandleLight ); painter->fillRect( x - 1, y, 2, 2, m_colorHandle ); } } return; case PE_IndicatorToolBarSeparator: painter->setPen( m_colorSeparator ); if ( option->state & State_Horizontal ) painter->drawLine( ( option->rect.left() + option->rect.right() - 1 ) / 2, option->rect.top() + 2, ( option->rect.left() + option->rect.right() - 1 ) / 2, option->rect.bottom() - 2 ); else painter->drawLine( option->rect.left() + 2, ( option->rect.top() + option->rect.bottom() - 1 ) / 2, option->rect.right() - 2, ( option->rect.top() + option->rect.bottom() - 1 ) / 2 ); painter->setPen( m_colorSeparatorLight ); if ( option->state & State_Horizontal ) painter->drawLine( ( option->rect.left() + option->rect.right() + 1 ) / 2, option->rect.top() + 2, ( option->rect.left() + option->rect.right() + 1 ) / 2, option->rect.bottom() - 2 ); else painter->drawLine( option->rect.left() + 2, ( option->rect.top() + option->rect.bottom() + 1 ) / 2, option->rect.right() - 2, ( option->rect.top() + option->rect.bottom() + 1 ) / 2 ); return; case PE_IndicatorButtonDropDown: { QToolBar* toolBar; if ( widget && ( toolBar = qobject_cast<QToolBar*>( widget->parentWidget() ) ) ) { QRect rect = option->rect.adjusted( -1, 0, -1, -1 ); bool selected = option->state & State_MouseOver && option->state & State_Enabled; bool sunken = option->state & State_Sunken; if ( selected || sunken ) { painter->setPen( m_colorItemBorder ); if ( toolBar->orientation() == Qt::Vertical ) { if ( sunken ) painter->setBrush( m_colorItemSunkenEnd ); else painter->setBrush( m_colorItemBackgroundEnd ); } else { QLinearGradient gradient( rect.topLeft(), rect.bottomLeft() ); if ( sunken ) { gradient.setColorAt( 0.0, m_colorItemSunkenBegin ); gradient.setColorAt( 0.5, m_colorItemSunkenMiddle ); gradient.setColorAt( 1.0, m_colorItemSunkenEnd ); } else { gradient.setColorAt( 0.0, m_colorItemBackgroundBegin ); gradient.setColorAt( 0.5, m_colorItemBackgroundMiddle ); gradient.setColorAt( 1.0, m_colorItemBackgroundEnd ); } painter->setBrush( gradient ); } painter->drawRect( rect ); } QStyleOption optionArrow = *option; optionArrow.rect.adjust( 2, 2, -2, -2 ); drawPrimitive( PE_IndicatorArrowDown, &optionArrow, painter, widget ); return; } } case PE_IndicatorDockWidgetResizeHandle: return; case PE_PanelButtonTool: if ( widget && widget->inherits( "QDockWidgetTitleButton" ) ) { if ( option->state & ( QStyle::State_MouseOver | QStyle::State_Sunken ) ) { painter->setPen( m_colorItemBorder ); painter->setBrush( ( option->state & QStyle::State_Sunken ) ? m_colorItemSunkenMiddle : m_colorItemBackgroundMiddle ); painter->drawRect( option->rect.adjusted( 0, 0, -1, -1 ) ); } return; } break; case PE_FrameTabWidget: if ( isStyledTabWidget( widget ) ) { painter->fillRect( option->rect, option->palette.window() ); return; } break; case PE_FrameTabBarBase: if ( isStyledTabBar( widget ) ) return; break; default: break; } if ( useVista() ) QWindowsVistaStyle::drawPrimitive( element, option, painter, widget ); else QWindowsXPStyle::drawPrimitive( element, option, painter, widget ); }
void QToolButtonPrivate::popupTimerDone() { Q_Q(QToolButton); popupTimer.stop(); if (!menuButtonDown && !down) return; menuButtonDown = true; QPointer<QMenu> actualMenu; bool mustDeleteActualMenu = false; if(menuAction) { actualMenu = menuAction->menu(); } else if (defaultAction && defaultAction->menu()) { actualMenu = defaultAction->menu(); } else { actualMenu = new QMenu(q); mustDeleteActualMenu = true; for(int i = 0; i < actions.size(); i++) actualMenu->addAction(actions.at(i)); } repeat = q->autoRepeat(); q->setAutoRepeat(false); bool horizontal = true; #if !defined(QT_NO_TOOLBAR) QToolBar *tb = qobject_cast<QToolBar*>(parent); if (tb && tb->orientation() == Qt::Vertical) horizontal = false; #endif QPoint p; QRect screen = QApplication::desktop()->availableGeometry(q); QSize sh = ((QToolButton*)(QMenu*)actualMenu)->receivers(SIGNAL(aboutToShow()))? QSize() : actualMenu->sizeHint(); QRect rect = q->rect(); if (horizontal) { if (q->isRightToLeft()) { if (q->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) { p = q->mapToGlobal(rect.bottomRight()); } else { p = q->mapToGlobal(rect.topRight() - QPoint(0, sh.height())); } p.rx() -= sh.width(); } else { if (q->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) { p = q->mapToGlobal(rect.bottomLeft()); } else { p = q->mapToGlobal(rect.topLeft() - QPoint(0, sh.height())); } } } else { if (q->isRightToLeft()) { if (q->mapToGlobal(QPoint(rect.left(), 0)).x() - sh.width() <= screen.x()) { p = q->mapToGlobal(rect.topRight()); } else { p = q->mapToGlobal(rect.topLeft()); p.rx() -= sh.width(); } } else { if (q->mapToGlobal(QPoint(rect.right(), 0)).x() + sh.width() <= screen.right()) { p = q->mapToGlobal(rect.topRight()); } else { p = q->mapToGlobal(rect.topLeft() - QPoint(sh.width(), 0)); } } } p.rx() = qMax(screen.left(), qMin(p.x(), screen.right() - sh.width())); p.ry() += 1; QPointer<QToolButton> that = q; actualMenu->setNoReplayFor(q); if (!mustDeleteActualMenu) //only if action are not in this widget QObject::connect(actualMenu, SIGNAL(triggered(QAction*)), q, SLOT(_q_menuTriggered(QAction*))); QObject::connect(actualMenu, SIGNAL(aboutToHide()), q, SLOT(_q_updateButtonDown())); actualMenu->d_func()->causedPopup.widget = q; actualMenu->d_func()->causedPopup.action = defaultAction; actionsCopy = q->actions(); //(the list of action may be modified in slots) actualMenu->exec(p); QObject::disconnect(actualMenu, SIGNAL(aboutToHide()), q, SLOT(_q_updateButtonDown())); if (mustDeleteActualMenu) delete actualMenu; else QObject::disconnect(actualMenu, SIGNAL(triggered(QAction*)), q, SLOT(_q_menuTriggered(QAction*))); if (!that) return; actionsCopy.clear(); if (repeat) q->setAutoRepeat(true); }