📄 qmdisubwindow.cpp
字号:
void paintEvent(QPaintEvent *event); void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void leaveEvent(QEvent *event); bool event(QEvent *event);private: QStyle::SubControl activeControl; QStyle::SubControl hoverControl; QStyle::SubControls visibleControls; void initStyleOption(QStyleOptionComplex *option) const; inline QStyle::SubControl getSubControl(const QPoint &pos) const { QStyleOptionComplex opt; initStyleOption(&opt); return style()->hitTestComplexControl(QStyle::CC_MdiControls, &opt, pos, this); }};/* \internal*/ControllerWidget::ControllerWidget(QWidget *parent) : QWidget(parent), activeControl(QStyle::SC_None), hoverControl(QStyle::SC_None), visibleControls(QStyle::SC_None){ setFocusPolicy(Qt::NoFocus); setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); setMouseTracking(true);}/* \internal*/QSize ControllerWidget::sizeHint() const{ ensurePolished(); QStyleOptionComplex opt; initStyleOption(&opt); QSize size(48, 16); return style()->sizeFromContents(QStyle::CT_MdiControls, &opt, size, this);}void ControllerWidget::setControlVisible(QMdiSubWindowPrivate::WindowStateAction action, bool visible){ QStyle::SubControl subControl = QStyle::SC_None; // Map action from QMdiSubWindowPrivate::WindowStateAction to QStyle::SubControl. if (action == QMdiSubWindowPrivate::MaximizeAction) subControl = QStyle::SC_MdiNormalButton; else if (action == QMdiSubWindowPrivate::CloseAction) subControl = QStyle::SC_MdiCloseButton; else if (action == QMdiSubWindowPrivate::MinimizeAction) subControl = QStyle::SC_MdiMinButton; if (subControl == QStyle::SC_None) return; if (visible && !(visibleControls & subControl)) visibleControls |= subControl; else if (!visible && (visibleControls & subControl)) visibleControls &= ~subControl;}/* \internal*/void ControllerWidget::paintEvent(QPaintEvent * /*paintEvent*/){ QStyleOptionComplex opt; initStyleOption(&opt); if (activeControl == hoverControl) { opt.activeSubControls = activeControl; opt.state |= QStyle::State_Sunken; } else if (hoverControl != QStyle::SC_None && (activeControl == QStyle::SC_None)) { opt.activeSubControls = hoverControl; opt.state |= QStyle::State_MouseOver; } QPainter painter(this); style()->drawComplexControl(QStyle::CC_MdiControls, &opt, &painter, this);}/* \internal*/void ControllerWidget::mousePressEvent(QMouseEvent *event){ if (event->button() != Qt::LeftButton) { event->ignore(); return; } activeControl = getSubControl(event->pos()); update();}/* \internal*/void ControllerWidget::mouseReleaseEvent(QMouseEvent *event){ if (event->button() != Qt::LeftButton) { event->ignore(); return; } QStyle::SubControl under_mouse = getSubControl(event->pos()); if (under_mouse == activeControl) { switch (activeControl) { case QStyle::SC_MdiCloseButton: emit _q_close(); break; case QStyle::SC_MdiNormalButton: emit _q_restore(); break; case QStyle::SC_MdiMinButton: emit _q_minimize(); break; default: break; } } activeControl = QStyle::SC_None; update();}/* \internal*/void ControllerWidget::mouseMoveEvent(QMouseEvent *event){ QStyle::SubControl under_mouse = getSubControl(event->pos()); //test if hover state changes if (hoverControl != under_mouse) { hoverControl = under_mouse; update(); }}/* \internal*/void ControllerWidget::leaveEvent(QEvent * /*event*/){ hoverControl = QStyle::SC_None; update();}/* \internal*/bool ControllerWidget::event(QEvent *event){#ifndef QT_NO_TOOLTIP if (event->type() != QEvent::ToolTip) return QWidget::event(event); QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event); QStyle::SubControl subControl = getSubControl(helpEvent->pos()); switch (subControl) { case QStyle::SC_MdiCloseButton: QToolTip::showText(helpEvent->globalPos(), QMdiSubWindow::tr("Close")); break; case QStyle::SC_MdiMinButton: QToolTip::showText(helpEvent->globalPos(), QMdiSubWindow::tr("Minimize")); break; case QStyle::SC_MdiNormalButton: QToolTip::showText(helpEvent->globalPos(), QMdiSubWindow::tr("Restore Down")); break; default: QToolTip::hideText(); break; }#endif // QT_NO_TOOLTIP return QWidget::event(event);}/* \internal*/void ControllerWidget::initStyleOption(QStyleOptionComplex *option) const{ option->initFrom(this); option->subControls = visibleControls; option->activeSubControls = QStyle::SC_None;}/* \internal*/ControlContainer::ControlContainer(QMdiSubWindow *mdiChild) : QObject(mdiChild), previousLeft(0), previousRight(0),#ifndef QT_NO_MENUBAR m_menuBar(0),#endif mdiChild(mdiChild){ Q_ASSERT(mdiChild); m_controllerWidget = new ControlElement<ControllerWidget>(mdiChild); connect(m_controllerWidget, SIGNAL(_q_close()), mdiChild, SLOT(close())); connect(m_controllerWidget, SIGNAL(_q_restore()), mdiChild, SLOT(showNormal())); connect(m_controllerWidget, SIGNAL(_q_minimize()), mdiChild, SLOT(showMinimized())); m_menuLabel = new ControlElement<ControlLabel>(mdiChild); m_menuLabel->setWindowIcon(mdiChild->windowIcon());#ifndef QT_NO_MENU connect(m_menuLabel, SIGNAL(_q_clicked()), mdiChild, SLOT(showSystemMenu()));#endif connect(m_menuLabel, SIGNAL(_q_doubleClicked()), mdiChild, SLOT(close()));}ControlContainer::~ControlContainer(){#ifndef QT_NO_MENUBAR removeButtonsFromMenuBar();#endif delete m_menuLabel; m_menuLabel = 0; delete m_controllerWidget; m_controllerWidget = 0;}#ifndef QT_NO_MENUBAR/* \internal*/void ControlContainer::showButtonsInMenuBar(QMenuBar *menuBar){ if (!menuBar || !mdiChild || mdiChild->windowFlags() & Qt::FramelessWindowHint) return; m_menuBar = menuBar; if (m_menuLabel && mdiChild->windowFlags() & Qt::WindowSystemMenuHint) { QWidget *currentLeft = menuBar->cornerWidget(Qt::TopLeftCorner); if (currentLeft) currentLeft->hide(); if (currentLeft != m_menuLabel) { menuBar->setCornerWidget(m_menuLabel, Qt::TopLeftCorner); previousLeft = currentLeft; } m_menuLabel->show(); } ControllerWidget *controllerWidget = qobject_cast<ControllerWidget *>(m_controllerWidget); if (controllerWidget && controllerWidget->hasVisibleControls()) { QWidget *currentRight = menuBar->cornerWidget(Qt::TopRightCorner); if (currentRight) currentRight->hide(); if (currentRight != m_controllerWidget) { menuBar->setCornerWidget(m_controllerWidget, Qt::TopRightCorner); previousRight = currentRight; } m_controllerWidget->show(); } setNewWindowTitle(mdiChild);}/* \internal*/void ControlContainer::removeButtonsFromMenuBar(QMenuBar *menuBar){ if (menuBar && menuBar != m_menuBar) { // m_menubar was deleted while sub-window was maximized previousRight = 0; previousLeft = 0; m_menuBar = menuBar; } if (!m_menuBar) return; QMdiSubWindow *child = 0; if (m_controllerWidget) { QWidget *currentRight = m_menuBar->cornerWidget(Qt::TopRightCorner); if (currentRight == m_controllerWidget) { if (ControlElement<ControllerWidget> *ce = ptr<ControllerWidget>(previousRight)) { if (!ce->mdiChild || !ce->mdiChild->isMaximized()) previousRight = 0; else child = ce->mdiChild; } m_menuBar->setCornerWidget(previousRight, Qt::TopRightCorner); if (previousRight) { previousRight->show(); previousRight = 0; } } m_controllerWidget->hide(); m_controllerWidget->setParent(0); } if (m_menuLabel) { QWidget *currentLeft = m_menuBar->cornerWidget(Qt::TopLeftCorner); if (currentLeft == m_menuLabel) { if (ControlElement<ControlLabel> *ce = ptr<ControlLabel>(previousLeft)) { if (!ce->mdiChild || !ce->mdiChild->isMaximized()) previousLeft = 0; else if (!child) child = mdiChild; } m_menuBar->setCornerWidget(previousLeft, Qt::TopLeftCorner); if (previousLeft) { previousLeft->show(); previousLeft = 0; } } m_menuLabel->hide(); m_menuLabel->setParent(0); } m_menuBar->update(); if (child) setNewWindowTitle(child); else if (mdiChild) mdiChild->window()->setWindowTitle(originalWindowTitle(mdiChild));}#endif // QT_NO_MENUBARvoid ControlContainer::updateWindowIcon(const QIcon &windowIcon){ if (m_menuLabel) m_menuLabel->setWindowIcon(windowIcon);}/*! \internal*/QMdiSubWindowPrivate::QMdiSubWindowPrivate() : baseWidget(0), restoreFocusWidget(0), controlContainer(0),#ifndef QT_NO_SIZEGRIP sizeGrip(0),#endif#ifndef QT_NO_RUBBERBAND rubberBand(0),#endif resizeEnabled(true), moveEnabled(true), isInInteractiveMode(false),#ifndef QT_NO_RUBBERBAND isInRubberBandMode(false),#endif isShadeMode(false), ignoreWindowTitleChange(false), ignoreNextActivationEvent(false), activationEnabled(true), isShadeRequestFromMinimizeMode(false), isMaximizeMode(false), isWidgetHiddenByUs(false), isActive(false), isExplicitlyDeactivated(false), keyboardSingleStep(5), keyboardPageStep(20), resizeTimerId(-1), currentOperation(None), hoveredSubControl(QStyle::SC_None), activeSubControl(QStyle::SC_None), focusInReason(Qt::ActiveWindowFocusReason){ initOperationMap();}/*! \internal*/void QMdiSubWindowPrivate::_q_updateStaysOnTopHint(){#ifndef QT_NO_ACTION Q_Q(QMdiSubWindow); if (QAction *senderAction = qobject_cast<QAction *>(q->sender())) { if (senderAction->isChecked()) { q->setWindowFlags(q->windowFlags() | Qt::WindowStaysOnTopHint); q->raise(); } else { q->setWindowFlags(q->windowFlags() & ~Qt::WindowStaysOnTopHint); q->lower(); } }#endif // QT_NO_ACTION}/*! \internal*/void QMdiSubWindowPrivate::_q_enterInteractiveMode(){#ifndef QT_NO_ACTION Q_Q(QMdiSubWindow); QAction *action = qobject_cast<QAction *>(q->sender()); if (!action) return; QPoint pressPos; if (actions[MoveAction] && actions[MoveAction] == action) { currentOperation = Move; pressPos = QPoint(q->width() / 2, titleBarHeight() - 1); } else if (actions[ResizeAction] && actions[ResizeAction] == action) { currentOperation = q->isLeftToRight() ? BottomRightResize : BottomLeftResize; int offset = q->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth) / 2; int x = q->isLeftToRight() ? q->width() - offset : offset; pressPos = QPoint(x, q->height() - offset); } else { return; } updateCursor();#ifndef QT_NO_CURSOR q->cursor().setPos(q->mapToGlobal(pressPos));#endif mousePressPosition = q->mapToParent(pressPos); oldGeometry = q->geometry();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -