📄 qworkspace.cpp
字号:
... } \endcode Child windows (MDI windows) are standard Qt widgets that are inserted into the workspace with addWindow(). As with top-level widgets, you can call functions such as show(), hide(), showMaximized(), and setWindowTitle() on a child window to change its appearance within the workspace. You can also provide widget flags to determine the layout of the decoration or the behavior of the widget itself. To change or retrieve the geometry of a child window, you must operate on its parentWidget(). The parentWidget() provides access to the decorated frame that contains the child window widget. When a child window is maximised, its decorated frame is hidden. If the top-level widget contains a menu bar, it will display the maximised window's operations menu to the left of the menu entries, and the window's controls to the right. A child window becomes active when it gets the keyboard focus, or when setFocus() is called. The user can activate a window by moving focus in the usual ways, for example by clicking a window or by pressing Tab. The workspace emits a signal windowActivated() when the active window changes, and the function activeWindow() returns a pointer to the active child window, or 0 if no window is active. The convenience function windowList() returns a list of all child windows. This information could be used in a popup menu containing a list of windows, for example. This feature is also available as part of the \l{Window Menu} Solution. QWorkspace provides two built-in layout strategies for child windows: cascade() and tile(). Both are slots so you can easily connect menu entries to them. \img qworkspace-arrange.png If you want your users to be able to work with child windows larger than the visible workspace area, set the scrollBarsEnabled property to true. \sa QDockWidget, {MDI Example}*/class QWorkspaceChild : public QWidget{ Q_OBJECT friend class QWorkspacePrivate; friend class QWorkspace; friend class QWorkspaceTitleBar;public: QWorkspaceChild(QWidget* window, QWorkspace* parent=0, Qt::WindowFlags flags = 0); ~QWorkspaceChild(); void setActive(bool); bool isActive() const; void adjustToFullscreen(); QWidget* windowWidget() const; QWidget* iconWidget() const; void doResize(); void doMove(); QSize sizeHint() const; QSize minimumSizeHint() const; QSize baseSize() const; int frameWidth() const; void show(); bool isWindowOrIconVisible() const;signals: void showOperationMenu(); void popupOperationMenu(const QPoint&);public slots: void activate(); void showMinimized(); void showMaximized(); void showNormal(); void showShaded(); void internalRaise(); void titleBarDoubleClicked();protected: void enterEvent(QEvent *); void leaveEvent(QEvent *); void childEvent(QChildEvent*); void resizeEvent(QResizeEvent *); void moveEvent(QMoveEvent *); bool eventFilter(QObject *, QEvent *); void paintEvent(QPaintEvent *); void changeEvent(QEvent *);private: void updateMask(); Q_DISABLE_COPY(QWorkspaceChild) QWidget *childWidget; QWidgetResizeHandler *widgetResizeHandler; QWorkspaceTitleBar *titlebar; QPointer<QWorkspaceTitleBar> iconw; QSize windowSize; QSize shadeRestore; QSize shadeRestoreMin; bool act :1; bool shademode :1;};int QWorkspaceChild::frameWidth() const{ return contentsRect().left();}class QWorkspacePrivate : public QWidgetPrivate { Q_DECLARE_PUBLIC(QWorkspace)public: QWorkspaceChild* active; QList<QWorkspaceChild *> windows; QList<QWorkspaceChild *> focus; QList<QWidget *> icons; QWorkspaceChild* maxWindow; QRect maxRestore; QPointer<QMDIControl> maxcontrols; QPointer<QMenuBar> maxmenubar; QHash<int, const char*> shortcutMap; int px; int py; QWidget *becomeActive; QPointer<QLabel> maxtools; QString topTitle; QMenu *popup, *toolPopup; enum WSActs { RestoreAct, MoveAct, ResizeAct, MinimizeAct, MaximizeAct, CloseAct, StaysOnTopAct, ShadeAct, NCountAct }; QAction *actions[NCountAct]; QScrollBar *vbar, *hbar; QWidget *corner; int yoffset, xoffset; QBrush background; void init(); void insertIcon(QWidget* w); void removeIcon(QWidget* w); void place(QWidget*); QWorkspaceChild* findChild(QWidget* w); void showMaximizeControls(); void hideMaximizeControls(); void activateWindow(QWidget* w, bool change_focus = true); void hideChild(QWorkspaceChild *c); void showWindow(QWidget* w); void maximizeWindow(QWidget* w); void minimizeWindow(QWidget* w); void normalizeWindow(QWidget* w); QRect updateWorkspace();private: void _q_normalizeActiveWindow(); void _q_minimizeActiveWindow(); void _q_showOperationMenu(); void _q_popupOperationMenu(const QPoint&); void _q_operationMenuActivated(QAction *); void _q_scrollBarChanged(); void _q_updateActions(); bool inTitleChange;};static bool isChildOf(QWidget * child, QWidget * parent){ if (!parent || !child) return false; QWidget * w = child; while(w && w != parent) w = w->parentWidget(); return w != 0;}/*! Constructs a workspace with the given \a parent.*/QWorkspace::QWorkspace(QWidget *parent) : QWidget(*new QWorkspacePrivate, parent, 0){ Q_D(QWorkspace); d->init();}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QWorkspace::QWorkspace(QWidget *parent, const char *name) : QWidget(*new QWorkspacePrivate, parent, 0){ Q_D(QWorkspace); setObjectName(QString::fromAscii(name)); d->init();}#endif // QT3_SUPPORT/*! \internal*/voidQWorkspacePrivate::init(){ Q_Q(QWorkspace); maxcontrols = 0; active = 0; maxWindow = 0; maxtools = 0; px = 0; py = 0; becomeActive = 0; popup = new QMenu(q); toolPopup = new QMenu(q); popup->setObjectName(QLatin1String("qt_internal_mdi_popup")); toolPopup->setObjectName(QLatin1String("qt_internal_mdi_tool_popup")); actions[QWorkspacePrivate::RestoreAct] = new QAction(QIcon(q->style()->standardPixmap(QStyle::SP_TitleBarNormalButton)), QWorkspace::tr("&Restore"), q); actions[QWorkspacePrivate::MoveAct] = new QAction(QWorkspace::tr("&Move"), q); actions[QWorkspacePrivate::ResizeAct] = new QAction(QWorkspace::tr("&Size"), q); actions[QWorkspacePrivate::MinimizeAct] = new QAction(QIcon(q->style()->standardPixmap(QStyle::SP_TitleBarMinButton)), QWorkspace::tr("Mi&nimize"), q); actions[QWorkspacePrivate::MaximizeAct] = new QAction(QIcon(q->style()->standardPixmap(QStyle::SP_TitleBarMaxButton)), QWorkspace::tr("Ma&ximize"), q); actions[QWorkspacePrivate::CloseAct] = new QAction(QIcon(q->style()->standardPixmap(QStyle::SP_TitleBarCloseButton)), QWorkspace::tr("&Close")#ifndef QT_NO_SHORTCUT +QLatin1Char('\t')+(QString)QKeySequence(Qt::CTRL+Qt::Key_F4)#endif ,q); QObject::connect(actions[QWorkspacePrivate::CloseAct], SIGNAL(triggered()), q, SLOT(closeActiveWindow())); actions[QWorkspacePrivate::StaysOnTopAct] = new QAction(QWorkspace::tr("Stay on &Top"), q); actions[QWorkspacePrivate::StaysOnTopAct]->setChecked(true); actions[QWorkspacePrivate::ShadeAct] = new QAction(QIcon(q->style()->standardPixmap(QStyle::SP_TitleBarShadeButton)), QWorkspace::tr("Sh&ade"), q); QObject::connect(popup, SIGNAL(aboutToShow()), q, SLOT(_q_updateActions())); QObject::connect(popup, SIGNAL(triggered(QAction*)), q, SLOT(_q_operationMenuActivated(QAction*))); popup->addAction(actions[QWorkspacePrivate::RestoreAct]); popup->addAction(actions[QWorkspacePrivate::MoveAct]); popup->addAction(actions[QWorkspacePrivate::ResizeAct]); popup->addAction(actions[QWorkspacePrivate::MinimizeAct]); popup->addAction(actions[QWorkspacePrivate::MaximizeAct]); popup->addSeparator(); popup->addAction(actions[QWorkspacePrivate::CloseAct]); QObject::connect(toolPopup, SIGNAL(aboutToShow()), q, SLOT(_q_updateActions())); QObject::connect(toolPopup, SIGNAL(triggered(QAction*)), q, SLOT(_q_operationMenuActivated(QAction*))); toolPopup->addAction(actions[QWorkspacePrivate::MoveAct]); toolPopup->addAction(actions[QWorkspacePrivate::ResizeAct]); toolPopup->addAction(actions[QWorkspacePrivate::StaysOnTopAct]); toolPopup->addSeparator(); toolPopup->addAction(actions[QWorkspacePrivate::ShadeAct]); toolPopup->addAction(actions[QWorkspacePrivate::CloseAct]);#ifndef QT_NO_SHORTCUT // Set up shortcut bindings (id -> slot), most used first QList <QKeySequence> shortcuts = QKeySequence::keyBindings(QKeySequence::NextChild); foreach (QKeySequence seq, shortcuts) shortcutMap.insert(q->grabShortcut(seq), "activateNextWindow"); shortcuts = QKeySequence::keyBindings(QKeySequence::PreviousChild); foreach (QKeySequence seq, shortcuts) shortcutMap.insert(q->grabShortcut(seq), "activatePreviousWindow"); shortcuts = QKeySequence::keyBindings(QKeySequence::Close); foreach (QKeySequence seq, shortcuts) shortcutMap.insert(q->grabShortcut(seq), "closeActiveWindow"); shortcutMap.insert(q->grabShortcut(QKeySequence(QLatin1String("ALT+-"))), "_q_showOperationMenu");#endif // QT_NO_SHORTCUT q->setBackgroundRole(QPalette::Dark); q->setAutoFillBackground(true); q->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); hbar = vbar = 0; corner = 0; xoffset = yoffset = 0; q->window()->installEventFilter(q); inTitleChange = false; updateWorkspace();}/*! Destroys the workspace and frees any allocated resources.*/QWorkspace::~QWorkspace(){}/*! \reimp */QSize QWorkspace::sizeHint() const{ QSize s(QApplication::desktop()->size()); return QSize(s.width()*2/3, s.height()*2/3);}#ifdef QT3_SUPPORT/*! Sets the background color to \a c. Use setBackground() instead.*/void QWorkspace::setPaletteBackgroundColor(const QColor & c){ setBackground(c);}/*! Sets the background pixmap to \a pm. Use setBackground() instead.*/void QWorkspace::setPaletteBackgroundPixmap(const QPixmap & pm){ setBackground(pm);}#endif // QT3_SUPPORT/*! \property QWorkspace::background \brief the workspace's background*/QBrush QWorkspace::background() const{ Q_D(const QWorkspace); if (d->background.style() == Qt::NoBrush) return palette().dark(); return d->background;}void QWorkspace::setBackground(const QBrush &background){ Q_D(QWorkspace); d->background = background; setAttribute(Qt::WA_OpaquePaintEvent, background.style() == Qt::NoBrush); update();}/*! Adds widget \a w as new sub window to the workspace. If \a flags are non-zero, they will override the flags set on the widget. Returns the widget used for the window frame. To remove the widget \a w from the workspace, simply call setParent() with the new parent (or 0 to make it a stand-alone window).*/QWidget * QWorkspace::addWindow(QWidget *w, Qt::WindowFlags flags){ Q_D(QWorkspace); if (!w) return 0; w->setAutoFillBackground(true); QWidgetPrivate::adjustFlags(flags);#if 0 bool wasMaximized = w->isMaximized(); bool wasMinimized = w->isMinimized();#endif bool hasSize = w->testAttribute(Qt::WA_Resized); int x = w->x(); int y = w->y(); bool hasPos = w->testAttribute(Qt::WA_Moved); QSize s = w->size().expandedTo(qSmartMinSize(w)); if (!hasSize && w->sizeHint().isValid()) w->adjustSize(); QWorkspaceChild* child = new QWorkspaceChild(w, this, flags); child->setObjectName(QLatin1String("qt_workspacechild")); child->installEventFilter(this); connect(child, SIGNAL(popupOperationMenu(QPoint)), this, SLOT(_q_popupOperationMenu(QPoint))); connect(child, SIGNAL(showOperationMenu()), this, SLOT(_q_showOperationMenu())); d->windows.append(child); if (child->isVisibleTo(this)) d->focus.append(child); child->internalRaise(); if (!hasPos) d->place(child); if (!hasSize) child->adjustSize(); if (hasPos) child->move(x, y); return child;#if 0 if (wasMaximized) w->showMaximized(); else if (wasMinimized) w->showMinimized(); else if (!hasBeenHidden) d->activateWindow(w);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -