📄 q3dockwindow.cpp
字号:
opt.rect = QStyle::visualRect(opt.direction, opt.rect, style()->subElementRect(QStyle::SE_Q3DockWindowHandleRect, &opt, this)); style()->drawPrimitive(QStyle::PE_IndicatorToolBarHandle, &opt, &p, this); QWidget::paintEvent(e);}void Q3DockWindowHandle::keyPressEvent(QKeyEvent *e){ if (!mousePressed) return; if (e->key() == Qt::Key_Control) { ctrlDown = true; dockWindow->handleMove(mapFromGlobal(QCursor::pos()) - offset, QCursor::pos(), !opaque); }}void Q3DockWindowHandle::keyReleaseEvent(QKeyEvent *e){ if (!mousePressed) return; if (e->key() == Qt::Key_Control) { ctrlDown = false; dockWindow->handleMove(mapFromGlobal(QCursor::pos()) - offset, QCursor::pos(), !opaque); }}void Q3DockWindowHandle::mousePressEvent(QMouseEvent *e){ if (!dockWindow->dockArea) return; ctrlDown = (e->state() & Qt::ControlButton) == Qt::ControlButton; oldFocus = qApp->focusWidget(); setFocus(); e->ignore(); if (e->button() != Qt::LeftButton) return; e->accept(); hadDblClick = false; mousePressed = true; offset = e->pos(); dockWindow->startRectDraw(mapToGlobal(e->pos()), !opaque); if (!opaque) qApp->installEventFilter(dockWindow);}void Q3DockWindowHandle::mouseMoveEvent(QMouseEvent *e){ if (!mousePressed || e->pos() == offset) return; ctrlDown = (e->state() & Qt::ControlButton) == Qt::ControlButton; dockWindow->handleMove(e->pos() - offset, e->globalPos(), !opaque); if (opaque) dockWindow->updatePosition(e->globalPos());}void Q3DockWindowHandle::mouseReleaseEvent(QMouseEvent *e){ ctrlDown = false; qApp->removeEventFilter(dockWindow); if (oldFocus) oldFocus->setFocus(); if (!mousePressed) return; dockWindow->endRectDraw(!opaque); mousePressed = false;#ifdef Q_WS_MAC releaseMouse();#endif if (!hadDblClick && offset == e->pos()) { timer->start(QApplication::doubleClickInterval(), true); } else if (!hadDblClick) { dockWindow->updatePosition(e->globalPos()); } if (opaque) dockWindow->titleBar->mousePressed = false; if (dockWindow->parentWidget()) QApplication::postEvent(dockWindow->parentWidget(), new QEvent(QEvent::LayoutHint));}void Q3DockWindowHandle::minimize(){ if (!dockWindow->area()) return; Q3MainWindow *mw = qobject_cast<Q3MainWindow*>(dockWindow->area()->parentWidget()); if (mw && mw->isDockEnabled(dockWindow, Qt::DockMinimized)) mw->moveDockWindow(dockWindow, Qt::DockMinimized);}void Q3DockWindowHandle::resizeEvent(QResizeEvent *){ updateGui();}void Q3DockWindowHandle::updateGui(){ if (!closeButton) { closeButton = new QToolButton(this, "qt_close_button1");#ifndef QT_NO_CURSOR closeButton->setCursor(Qt::ArrowCursor);#endif QStyleOption opt(0); opt.init(closeButton); closeButton->setIcon(style()->standardIcon(QStyle::SP_DockWidgetCloseButton, &opt, closeButton)); closeButton->setFixedSize(12, 12); connect(closeButton, SIGNAL(clicked()), dockWindow, SLOT(hide())); } if (dockWindow->isCloseEnabled() && dockWindow->area()) closeButton->show(); else closeButton->hide(); if (!dockWindow->area()) return; if (dockWindow->area()->orientation() == Qt::Horizontal) { int off = (width() - closeButton->width() - 1) / 2; closeButton->move(off, 2); } else { int off = (height() - closeButton->height() - 1) / 2; int x = QApplication::reverseLayout() ? 2 : width() - closeButton->width() - 2; closeButton->move(x, off); }}void Q3DockWindowHandle::changeEvent(QEvent *ev){ if(ev->type() == QEvent::StyleChange) { if (closeButton) { QStyleOption opt(0); opt.init(closeButton); closeButton->setIcon(style()->standardIcon(QStyle::SP_DockWidgetCloseButton, &opt, closeButton)); } } QWidget::changeEvent(ev);}QSize Q3DockWindowHandle::minimumSizeHint() const{ if (!dockWindow->dockArea) return QSize(0, 0); int wh = dockWindow->isCloseEnabled() ? 17 : style()->pixelMetric(QStyle::PM_ToolBarHandleExtent, 0, this); if (dockWindow->orientation() == Qt::Horizontal) return QSize(wh, 0); return QSize(0, wh);}void Q3DockWindowHandle::mouseDoubleClickEvent(QMouseEvent *e){ e->ignore(); if (e->button() != Qt::LeftButton) return; e->accept(); timer->stop(); emit doubleClicked(); hadDblClick = true; if (dockWindow->parentWidget()) QApplication::postEvent(dockWindow->parentWidget(), new QEvent(QEvent::LayoutHint));}Q3DockWindowTitleBar::Q3DockWindowTitleBar(Q3DockWindow *dw) : Q3TitleBar(0, dw), dockWindow(dw), mousePressed(false), hadDblClick(false), opaque(default_opaque){ setObjectName(QLatin1String("qt_dockwidget_internal")); ctrlDown = false; setMouseTracking(true); QStyleOptionTitleBar opt = getStyleOption(); setFixedHeight(style()->pixelMetric(QStyle::PM_TitleBarHeight, &opt, this)); connect(this, SIGNAL(doClose()), dockWindow, SLOT(hide()));}void Q3DockWindowTitleBar::keyPressEvent(QKeyEvent *e){ if (!mousePressed) return; if (e->key() == Qt::Key_Control) { ctrlDown = true; dockWindow->handleMove(mapFromGlobal(QCursor::pos()) - offset, QCursor::pos(), !opaque); }}void Q3DockWindowTitleBar::keyReleaseEvent(QKeyEvent *e){ if (!mousePressed) return; if (e->key() == Qt::Key_Control) { ctrlDown = false; dockWindow->handleMove(mapFromGlobal(QCursor::pos()) - offset, QCursor::pos(), !opaque); }}void Q3DockWindowTitleBar::mousePressEvent(QMouseEvent *e){ QStyleOptionTitleBar opt; opt.init(this); opt.subControls = QStyle::SC_All; opt.activeSubControls = QStyle::SC_None; opt.text = windowTitle(); //################ QIcon icon = windowIcon(); QSize s = icon.actualSize(QSize(64, 64)); opt.icon = icon.pixmap(s); opt.titleBarState = window() ? window()->windowState() : static_cast<Qt::WindowStates>(Qt::WindowNoState); opt.titleBarFlags = fakeWindowFlags(); QStyle::SubControl tbctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(), this); if (tbctrl < QStyle::SC_TitleBarLabel && tbctrl != QStyle::SC_None) { Q3TitleBar::mousePressEvent(e); return; } ctrlDown = (e->state() & Qt::ControlButton) == Qt::ControlButton; oldFocus = qApp->focusWidget();// setFocus activates the window, which deactivates the main window// not what we want, and not required anyway on Windows#ifndef Q_WS_WIN setFocus();#endif e->ignore(); if (e->button() != Qt::LeftButton) return; if (e->y() < 3 && dockWindow->isResizeEnabled()) return; e->accept(); bool oldPressed = mousePressed; mousePressed = true; hadDblClick = false; offset = e->pos(); dockWindow->startRectDraw(mapToGlobal(e->pos()), !opaque);// grabMouse resets the Windows mouse press count, so we never receive a double click on Windows// not required on Windows, and did work on X11, too, but no problem there in the first place#ifndef Q_WS_WIN if(!oldPressed && dockWindow->opaqueMoving()) grabMouse();#else Q_UNUSED(oldPressed);#endif}void Q3DockWindowTitleBar::mouseMoveEvent(QMouseEvent *e){ if (!mousePressed) { Q3TitleBar::mouseMoveEvent(e); return; } ctrlDown = (e->state() & Qt::ControlButton) == Qt::ControlButton; e->accept(); dockWindow->handleMove(e->pos() - offset, e->globalPos(), !opaque);}void Q3DockWindowTitleBar::mouseReleaseEvent(QMouseEvent *e){ if (!mousePressed) { Q3TitleBar::mouseReleaseEvent(e); return; } ctrlDown = false; qApp->removeEventFilter(dockWindow); if (oldFocus) oldFocus->setFocus(); if (dockWindow->place() == Q3DockWindow::OutsideDock) dockWindow->raise(); if(dockWindow->opaqueMoving()) releaseMouse(); if (!mousePressed) return; dockWindow->endRectDraw(!opaque); mousePressed = false; if (!hadDblClick) dockWindow->updatePosition(e->globalPos()); if (opaque) { dockWindow->horHandle->mousePressed = false; dockWindow->verHandle->mousePressed = false; } if (dockWindow->parentWidget()) QApplication::postEvent(dockWindow->parentWidget(), new QEvent(QEvent::LayoutHint));}void Q3DockWindowTitleBar::resizeEvent(QResizeEvent *e){ updateGui(); Q3TitleBar::resizeEvent(e);}void Q3DockWindowTitleBar::updateGui(){ if (dockWindow->isCloseEnabled()) { setFakeWindowFlags(fakeWindowFlags() | Qt::WStyle_SysMenu); } else { setFakeWindowFlags(fakeWindowFlags() & ~Qt::WStyle_SysMenu); }}void Q3DockWindowTitleBar::mouseDoubleClickEvent(QMouseEvent *){ emit doubleClicked(); hadDblClick = true; if (dockWindow->parentWidget()) QApplication::postEvent(dockWindow->parentWidget(), new QEvent(QEvent::LayoutHint));}/*! \class Q3DockWindow q3dockwindow.h \brief The Q3DockWindow class provides a widget which can be docked inside a Q3DockArea or floated as a top level window on the desktop. \compat This class handles moving, resizing, docking and undocking dock windows. Q3ToolBar is a subclass of Q3DockWindow so the functionality provided for dock windows is available with the same API for toolbars. \img qmainwindow-qdockareas.png Q3DockWindows in a Q3DockArea \caption Two Q3DockWindows (\l{Q3ToolBar}s) in a \l Q3DockArea \img qdockwindow.png A Q3DockWindow \caption A Floating Q3DockWindow If the user drags the dock window into the dock area the dock window will be docked. If the user drags the dock area outside any dock areas the dock window will be undocked (floated) and will become a top level window. Double clicking a floating dock window's title bar will dock the dock window to the last dock area it was docked in. Double clicking a docked dock window's handle will undock (float) the dock window. \omit Single clicking a docked dock window's handle will minimize the dock window (only its handle will appear, below the menu bar). Single clicking the minimized handle will restore the dock window to the last dock area that it was docked in. \endomit If the user clicks the close button (which appears on floating dock windows by default) the dock window will disappear. You can control whether or not a dock window has a close button with setCloseMode(). Q3MainWindow provides four dock areas (top, left, right and bottom) which can be used by dock windows. For many applications using the dock areas provided by Q3MainWindow is sufficient. (See the \l Q3DockArea documentation if you want to create your own dock areas.) In Q3MainWindow a right-click popup menu (the dock window menu) is available which lists dock windows and can be used to show or hide them. (The popup menu only lists dock windows that have a \link QWidget::setWindowTitle() caption\endlink.) When you construct a dock window you \e must pass it a Q3DockArea or a Q3MainWindow as its parent if you want it docked. Pass 0 for the parent if you want it floated. \code Q3ToolBar *fileTools = new Q3ToolBar(this, "File Actions"); moveDockWindow(fileTools, Left); \endcode In the example above we create a new Q3ToolBar in the constructor of a Q3MainWindow subclass (so that the \e this pointer points to the Q3MainWindow). By default the toolbar will be added to the \c Top dock area, but we've moved it to the \c Left dock area. A dock window is often used to contain a single widget. In these cases the widget can be set by calling setWidget(). If you're constructing a dock window that contains multiple widgets, e.g. a toolbar, arrange the widgets within a box layout inside the dock window. To do this use the boxLayout() function to get a pointer to the dock window's box layout, then add widgets to the layout using the box layout's QBoxLayout::addWidget() function. The dock window will dynamically set the orientation of the layout to be vertical or horizontal as necessary, although you can control this yourself with setOrientation(). Although a common use of dock windows is for toolbars, they can be used with any widgets. When using larger widgets it may make sense for the dock window to be resizable by calling setResizeEnabled(). Resizable dock windows are given splitter-like handles to allow the user to resize them within their dock area. When resizable dock windows are undocked they become top level windows and can be resized like any other top level windows, e.g. by dragging a corner or edge. Qt::Dock windows can be docked and undocked using dock() and undock(). A dock window's orientation can be set with setOrientation(). You can also use Q3DockArea::moveDockWindow(). If you're using a Q3MainWindow, Q3MainWindow::moveDockWindow() and Q3MainWindow::removeDockWindow() are available. A dock window can have some preferred settings, for example, you can set a preferred offset from the left edge (or top edge for vertical dock areas) of the dock area using setOffset(). If you'd prefer a dock window to start on a new line when it is docked use setNewLine(). The setFixedExtentWidth() and setFixedExtentHeight() functions can be used to define the dock window's preferred size, and the setHorizontallyStretchable() and setVerticallyStretchable() functions set whether the dock window can be stretched or not. Dock windows can be moved by default, but this can be changed with setMovingEnabled(). When a dock window is moved it is shown as a rectangular outline, but it can be shown normally using setOpaqueMoving(). When a dock window's visibility changes, i.e. it is shown or hidden, the visibilityChanged() signal is emitted. When a dock window is docked, undocked or moved inside the dock area the placeChanged() signal is emitted.*//*! \enum Q3DockWindow::Place This enum specifies the possible locations for a Q3DockWindow:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -