📄 q3mainwindow.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3mainwindow.h"#ifndef QT_NO_MAINWINDOW#include "qapplication.h"#include "qbitmap.h"#include "qcursor.h"#include "qdatetime.h"#include "q3dockarea.h"#include "qevent.h"#include "qlayout.h"#include "qmap.h"#include "qmenubar.h"#include "qpainter.h"#include "q3popupmenu.h"#include "q3scrollview.h"#include "qstatusbar.h"#include "qstringlist.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtimer.h"#include "q3toolbar.h"#include "qtooltip.h"#include "qwhatsthis.h"#ifdef Q_WS_MAC# include <private/qt_mac_p.h>#endifclass QHideDock;#include <private/q3mainwindow_p.h>/* Q3MainWindowLayout, respects widthForHeight layouts (like the left and right docks are)*/class Q3MainWindowLayout : public QLayout{ Q_OBJECTpublic: Q3MainWindowLayout(Q3MainWindow *mw, QLayout* parent = 0); ~Q3MainWindowLayout() {} void addItem(QLayoutItem *); void setLeftDock(Q3DockArea *l); void setRightDock(Q3DockArea *r); void setCentralWidget(QWidget *w); bool hasHeightForWidth() const { return false; } QSize sizeHint() const; QSize minimumSize() const; QLayoutItem *itemAt(int) const { return 0; } //### QLayoutItem *takeAt(int) { return 0; } //### int count() const { return 0; } //###protected: void setGeometry(const QRect &r) { QLayout::setGeometry(r); layoutItems(r); }private: int layoutItems(const QRect&, bool testonly = false); int extraPixels() const; Q3DockArea *left, *right; QWidget *central; Q3MainWindow *mainWindow;};QSize Q3MainWindowLayout::sizeHint() const{ int w = 0; int h = 0; if (left) { w += left->sizeHint().width(); h = qMax(h, left->sizeHint().height()); } if (right) { w += right->sizeHint().width(); h = qMax(h, right->sizeHint().height()); } if (central) { w += central->sizeHint().width(); int diff = extraPixels(); h = qMax(h, central->sizeHint().height() + diff); } return QSize(w, h);}QSize Q3MainWindowLayout::minimumSize() const{ int w = 0; int h = 0; if (left) { QSize ms = left->minimumSizeHint().expandedTo(left->minimumSize()); w += ms.width(); h = qMax(h, ms.height()); } if (right) { QSize ms = right->minimumSizeHint().expandedTo(right->minimumSize()); w += ms.width(); h = qMax(h, ms.height()); } if (central) { QSize min = central->minimumSize().isNull() ? central->minimumSizeHint() : central->minimumSize(); w += min.width(); int diff = extraPixels(); h = qMax(h, min.height() + diff); } return QSize(w, h);}Q3MainWindowLayout::Q3MainWindowLayout(Q3MainWindow *mw, QLayout* parent) : QLayout(parent), left(0), right(0), central(0){ mainWindow = mw;}void Q3MainWindowLayout::setLeftDock(Q3DockArea *l){ left = l;}void Q3MainWindowLayout::setRightDock(Q3DockArea *r){ right = r;}void Q3MainWindowLayout::setCentralWidget(QWidget *w){ central = w;}int Q3MainWindowLayout::layoutItems(const QRect &r, bool testonly){ if (!left && !central && !right) return 0; int wl = 0, wr = 0; if (left) wl = ((Q3DockAreaLayout*)left->QWidget::layout())->widthForHeight(r.height()); if (right) wr = ((Q3DockAreaLayout*)right->QWidget::layout())->widthForHeight(r.height()); int w = r.width() - wr - wl; if (w < 0) w = 0; int diff = extraPixels(); if (!testonly) { QRect g(geometry()); if (left) left->setGeometry(QRect(g.x(), g.y() + diff, wl, r.height() - diff)); if (right) right->setGeometry(QRect(g.x() + g.width() - wr, g.y() + diff, wr, r.height() - diff)); if (central) central->setGeometry(g.x() + wl, g.y() + diff, w, r.height() - diff); } w = wl + wr; if (central) w += central->minimumSize().width(); return w;}int Q3MainWindowLayout::extraPixels() const{ if (mainWindow->d_func()->topDock->isEmpty() && !(mainWindow->d_func()->leftDock->isEmpty() && mainWindow->d_func()->rightDock->isEmpty())) { return 2; } else { return 0; }}void Q3MainWindowLayout::addItem(QLayoutItem * /* item */){}/* QHideToolTip and QHideDock - minimized dock*/#if 0class QHideToolTip : public QToolTip{public: QHideToolTip(QWidget *parent) : QToolTip(parent) {} void maybeTip(const QPoint &pos);};#endifclass QHideDock : public QWidget{ Q_OBJECTpublic: QHideDock(Q3MainWindow *parent) : QWidget(parent, "qt_hide_dock") { hide(); setFixedHeight(style()->pixelMetric(QStyle::PM_DockWidgetHandleExtent, 0, this) + 3); pressedHandle = -1; pressed = false; setMouseTracking(true); win = parent;#if 0 tip = new QHideToolTip(this);#endif } ~QHideDock() {#if 0 delete tip;#endif }protected: void paintEvent(QPaintEvent *e) { QObjectList childs = children(); if (childs.isEmpty()) return; QPainter p(this); p.setClipRegion(e->rect()); p.fillRect(e->rect(), palette().brush(QPalette::Background)); int x = 0; for (int i = 0; i < childs.size(); ++i) { QObject *o = childs.at(i); Q3DockWindow *dw = qobject_cast<Q3DockWindow*>(o); if (!dw || !dw->isVisible()) continue; QStyleOptionQ3DockWindow opt; opt.rect.setRect(x, 0, 30, 10); opt.palette = palette(); opt.docked = dw->area(); opt.closeEnabled = dw->isCloseEnabled(); opt.state = QStyle::State_None; if (i == pressedHandle) opt.state |= QStyle::State_On; style()->drawPrimitive(QStyle::PE_IndicatorToolBarHandle, &opt, &p, this); x += 30; } } void mousePressEvent(QMouseEvent *e) { pressed = true; QObjectList childs = children(); if (childs.isEmpty()) return; mouseMoveEvent(e); pressedHandle = -1; if (e->button() == Qt::RightButton && win->isDockMenuEnabled()) { // ### TODO: HideDock menu } else { mouseMoveEvent(e); } } void mouseMoveEvent(QMouseEvent *e) { QObjectList childs = children(); if (childs.isEmpty()) return; if (!pressed) return; int x = 0; if (e->y() >= 0 && e->y() <= height()) { for (int i = 0; i < childs.size(); ++i) { QObject *o = childs.at(i); Q3DockWindow *dw = qobject_cast<Q3DockWindow*>(o); if (!dw || !dw->isVisible()) continue; if (e->x() >= x && e->x() <= x + 30) { int old = pressedHandle; pressedHandle = i; if (pressedHandle != old) repaint(); return; } x += 30; } } int old = pressedHandle; pressedHandle = -1; if (old != -1) repaint(); } void mouseReleaseEvent(QMouseEvent *e) { pressed = false; if (pressedHandle == -1) return; QObjectList childs = children(); if (childs.isEmpty()) return; if (e->button() == Qt::LeftButton) { if (e->y() >= 0 && e->y() <= height()) { QObject *o = childs.at(pressedHandle); Q3DockWindow *dw = qobject_cast<Q3DockWindow*>(o); if (dw) { dw->show(); dw->dock(); } } } pressedHandle = -1; repaint(); } bool eventFilter(QObject *o, QEvent *e) { if (o == this || !o->isWidgetType()) return QWidget::eventFilter(o, e); if (e->type() == QEvent::HideToParent || e->type() == QEvent::ShowToParent) updateState(); return QWidget::eventFilter(o, e); } void updateState() { bool visible = true; QObjectList childs = children(); if (childs.isEmpty()) return; for (int i = 0; i < childs.size(); ++i) { QObject *o = childs.at(i); Q3DockWindow *dw = qobject_cast<Q3DockWindow*>(o); if (!dw) continue; if (dw->isHidden()) { visible = false; continue; } if (!dw->isVisible()) continue; visible = true; break; } if (visible) show(); else hide(); win->triggerLayout(false); update(); } void childEvent(QChildEvent *e) { QWidget::childEvent(e); if (e->type() == QEvent::ChildInserted) e->child()->installEventFilter(this); else e->child()->removeEventFilter(this); updateState(); }private: Q3MainWindow *win; int pressedHandle; bool pressed;#if 0 QHideToolTip *tip; friend class QHideToolTip;#endif};#if 0void QHideToolTip::maybeTip(const QPoint &pos){ if (!parentWidget()) return; QHideDock *dock = (QHideDock*)parentWidget(); QObjectList dchilds = dock->children(); if (dchilds.isEmpty()) return; int x = 0; for (int i = 0; i < dchilds.size(); ++i) { QObject *o = dchilds.at(i); Q3DockWindow *dw = qobject_cast<Q3DockWindow*>(o); if (!dw || !dw->isVisible()) continue; if (pos.x() >= x && pos.x() <= x + 30) { Q3DockWindow *dw = (Q3DockWindow*)o; if (!dw->windowTitle().isEmpty()) tip(QRect(x, 0, 30, dock->height()), dw->windowTitle()); return; } x += 30; }}#endif/*! \class Q3MainWindow \brief The Q3MainWindow class provides a main application window, with a menu bar, dock windows (e.g. for toolbars), and a status bar. \compat Main windows are most often used to provide menus, toolbars and a status bar around a large central widget, such as a text edit, drawing canvas or QWorkspace (for MDI applications). Q3MainWindow is usually subclassed since this makes it easier to encapsulate the central widget, menus and toolbars as well as the window's state. Subclassing makes it possible to create the slots that are called when the user clicks menu items or toolbar buttons. We'll briefly review adding menu items and toolbar buttons then describe the facilities of Q3MainWindow itself. \code
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -