📄 qworkspace.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui 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 "qworkspace.h"#ifndef QT_NO_WORKSPACE#include "qapplication.h"#include "qbitmap.h"#include "qcursor.h"#include "qdatetime.h"#include "qdesktopwidget.h"#include "qevent.h"#include "qhash.h"#include "qicon.h"#include "qimage.h"#include "qlabel.h"#include "qlayout.h"#include "qmenubar.h"#include "qmenu.h"#include "qpainter.h"#include "qpointer.h"#include "qscrollbar.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtoolbutton.h"#include "qtooltip.h"#include "qdebug.h"#include <private/qwidget_p.h>#include <private/qwidgetresizehandler_p.h>class QWorkspaceTitleBarPrivate;class QWorkspaceTitleBar : public QWidget{ Q_OBJECT Q_DECLARE_PRIVATE(QWorkspaceTitleBar) Q_PROPERTY(bool autoRaise READ autoRaise WRITE setAutoRaise) Q_PROPERTY(bool movable READ isMovable WRITE setMovable)public: QWorkspaceTitleBar (QWidget *w, QWidget *parent, Qt::WFlags f = 0); ~QWorkspaceTitleBar(); bool isActive() const; bool usesActiveColor() const; bool isMovable() const; void setMovable(bool); bool autoRaise() const; void setAutoRaise(bool); QWidget *window() const; bool isTool() const; QSize sizeHint() const; QStyleOptionTitleBar getStyleOption() const;public slots: void setActive(bool);signals: void doActivate(); void doNormal(); void doClose(); void doMaximize(); void doMinimize(); void doShade(); void showOperationMenu(); void popupOperationMenu(const QPoint&); void doubleClicked();protected: bool event(QEvent *); void contextMenuEvent(QContextMenuEvent *); void mousePressEvent(QMouseEvent *); void mouseDoubleClickEvent(QMouseEvent *); void mouseReleaseEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *); void enterEvent(QEvent *e); void leaveEvent(QEvent *e); void paintEvent(QPaintEvent *p);private: Q_DISABLE_COPY(QWorkspaceTitleBar)};class QWorkspaceTitleBarPrivate : public QWidgetPrivate{ Q_DECLARE_PUBLIC(QWorkspaceTitleBar)public: QWorkspaceTitleBarPrivate() : lastControl(QStyle::SC_None),#ifndef QT_NO_TOOLTIP toolTip(0),#endif act(0), window(0), movable(1), pressed(0), autoraise(0), inevent(0) { } Qt::WFlags flags; QStyle::SubControl buttonDown; QStyle::SubControl lastControl; QPoint moveOffset;#ifndef QT_NO_TOOLTIP QToolTip *toolTip;#endif bool act :1; QPointer<QWidget> window; bool movable :1; bool pressed :1; bool autoraise :1; bool inevent : 1; int titleBarState() const; QStyleOptionTitleBar getStyleOption() const; void readColors();};inline int QWorkspaceTitleBarPrivate::titleBarState() const{ Q_Q(const QWorkspaceTitleBar); uint state = window ? window->windowState() : static_cast<Qt::WindowStates>(Qt::WindowNoState); state |= uint((act && q->isActiveWindow()) ? QStyle::State_Active : QStyle::State_None); return (int)state;}QStyleOptionTitleBar QWorkspaceTitleBarPrivate::getStyleOption() const{ Q_Q(const QWorkspaceTitleBar); QStyleOptionTitleBar opt; opt.init(q); //################ if (window) { opt.text = window->windowTitle(); QIcon icon = window->windowIcon(); QSize s = icon.actualSize(QSize(64, 64)); opt.icon = icon.pixmap(s); } opt.subControls = QStyle::SC_All; opt.activeSubControls = QStyle::SC_None; opt.titleBarState = titleBarState(); opt.titleBarFlags = flags; opt.state &= ~QStyle::State_MouseOver; return opt;}QWorkspaceTitleBar::QWorkspaceTitleBar(QWidget *w, QWidget *parent, Qt::WFlags f) : QWidget(*new QWorkspaceTitleBarPrivate, parent, Qt::FramelessWindowHint){ Q_D(QWorkspaceTitleBar); if (f == 0 && w) f = w->windowFlags(); d->flags = f; d->window = w; d->buttonDown = QStyle::SC_None; d->act = 0; if (w) { if (w->maximumSize() != QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)) d->flags &= ~Qt::WindowMaximizeButtonHint; setWindowTitle(w->windowTitle()); } d->readColors(); setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); setMouseTracking(true); setAutoRaise(style()->styleHint(QStyle::SH_TitleBar_AutoRaise, 0, this));}QWorkspaceTitleBar::~QWorkspaceTitleBar(){}QStyleOptionTitleBar QWorkspaceTitleBar::getStyleOption() const{ return d_func()->getStyleOption();}#ifdef Q_WS_WINstatic inline QRgb colorref2qrgb(COLORREF col){ return qRgb(GetRValue(col),GetGValue(col),GetBValue(col));}#endifvoid QWorkspaceTitleBarPrivate::readColors(){ Q_Q(QWorkspaceTitleBar); QPalette pal = q->palette(); bool colorsInitialized = false;#ifdef Q_WS_WIN // ask system properties on windows#ifndef SPI_GETGRADIENTCAPTIONS#define SPI_GETGRADIENTCAPTIONS 0x1008#endif#ifndef COLOR_GRADIENTACTIVECAPTION#define COLOR_GRADIENTACTIVECAPTION 27#endif#ifndef COLOR_GRADIENTINACTIVECAPTION#define COLOR_GRADIENTINACTIVECAPTION 28#endif if (QApplication::desktopSettingsAware()) { pal.setColor(QPalette::Active, QPalette::Highlight, colorref2qrgb(GetSysColor(COLOR_ACTIVECAPTION))); pal.setColor(QPalette::Inactive, QPalette::Highlight, colorref2qrgb(GetSysColor(COLOR_INACTIVECAPTION))); pal.setColor(QPalette::Active, QPalette::HighlightedText, colorref2qrgb(GetSysColor(COLOR_CAPTIONTEXT))); pal.setColor(QPalette::Inactive, QPalette::HighlightedText, colorref2qrgb(GetSysColor(COLOR_INACTIVECAPTIONTEXT))); if (QSysInfo::WindowsVersion != QSysInfo::WV_95 && QSysInfo::WindowsVersion != QSysInfo::WV_NT) { colorsInitialized = true; BOOL gradient; QT_WA({ SystemParametersInfo(SPI_GETGRADIENTCAPTIONS, 0, &gradient, 0); } , { SystemParametersInfoA(SPI_GETGRADIENTCAPTIONS, 0, &gradient, 0); }); if (gradient) { pal.setColor(QPalette::Active, QPalette::Base, colorref2qrgb(GetSysColor(COLOR_GRADIENTACTIVECAPTION))); pal.setColor(QPalette::Inactive, QPalette::Base, colorref2qrgb(GetSysColor(COLOR_GRADIENTINACTIVECAPTION))); } else { pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Active, QPalette::Highlight)); pal.setColor(QPalette::Inactive, QPalette::Base, pal.color(QPalette::Inactive, QPalette::Highlight)); } } }#endif // Q_WS_WIN if (!colorsInitialized) { pal.setColor(QPalette::Active, QPalette::Highlight, pal.color(QPalette::Active, QPalette::Highlight)); pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Active, QPalette::Highlight)); pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.color(QPalette::Inactive, QPalette::Dark)); pal.setColor(QPalette::Inactive, QPalette::Base, pal.color(QPalette::Inactive, QPalette::Dark)); pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.color(QPalette::Inactive, QPalette::Background)); } q->setPalette(pal); q->setActive(act);}void QWorkspaceTitleBar::mousePressEvent(QMouseEvent *e){ Q_D(QWorkspaceTitleBar); if (!d->act) emit doActivate(); if (e->button() == Qt::LeftButton) { if (style()->styleHint(QStyle::SH_TitleBar_NoBorder, 0, 0) && !rect().adjusted(5, 5, -5, 0).contains(e->pos())) { // propagate border events to the QWidgetResizeHandler e->ignore(); return; } d->pressed = true; QStyleOptionTitleBar opt = d->getStyleOption(); QStyle::SubControl ctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(), this); switch (ctrl) { case QStyle::SC_TitleBarSysMenu: if (d->flags & Qt::WindowSystemMenuHint) { d->buttonDown = QStyle::SC_None; static QTime *t = 0; static QWorkspaceTitleBar *tc = 0; if (!t) t = new QTime; if (tc != this || t->elapsed() > QApplication::doubleClickInterval()) { emit showOperationMenu(); t->start(); tc = this; } else { tc = 0; emit doClose(); return; } } break; case QStyle::SC_TitleBarShadeButton: case QStyle::SC_TitleBarUnshadeButton: if (d->flags & Qt::WindowShadeButtonHint) d->buttonDown = ctrl; break; case QStyle::SC_TitleBarNormalButton: d->buttonDown = ctrl; break; case QStyle::SC_TitleBarMinButton: if (d->flags & Qt::WindowMinimizeButtonHint) d->buttonDown = ctrl; break; case QStyle::SC_TitleBarMaxButton: if (d->flags & Qt::WindowMaximizeButtonHint) d->buttonDown = ctrl; break; case QStyle::SC_TitleBarCloseButton: if (d->flags & Qt::WindowSystemMenuHint) d->buttonDown = ctrl; break; case QStyle::SC_TitleBarLabel: d->buttonDown = ctrl; d->moveOffset = mapToParent(e->pos()); break; default: break; } update(); } else { d->pressed = false; }}void QWorkspaceTitleBar::contextMenuEvent(QContextMenuEvent *e){ QStyleOptionTitleBar opt = d_func()->getStyleOption(); QStyle::SubControl ctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(), this); if(ctrl == QStyle::SC_TitleBarLabel || ctrl == QStyle::SC_TitleBarSysMenu) { e->accept(); emit popupOperationMenu(e->globalPos()); } else { e->ignore(); }}void QWorkspaceTitleBar::mouseReleaseEvent(QMouseEvent *e){ Q_D(QWorkspaceTitleBar); if (!d->window) { // could have been deleted as part of a double click event on the sysmenu return; } if (e->button() == Qt::LeftButton && d->pressed) { if (style()->styleHint(QStyle::SH_TitleBar_NoBorder, 0, 0) && !rect().adjusted(5, 5, -5, 0).contains(e->pos())) { // propagate border events to the QWidgetResizeHandler e->ignore(); d->buttonDown = QStyle::SC_None; d->pressed = false; return; } e->accept(); QStyleOptionTitleBar opt = d->getStyleOption(); QStyle::SubControl ctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(), this); if (d->pressed) { update(); d->pressed = false; } if (ctrl == d->buttonDown) { d->buttonDown = QStyle::SC_None; switch(ctrl) { case QStyle::SC_TitleBarShadeButton: case QStyle::SC_TitleBarUnshadeButton: if(d->flags & Qt::WindowShadeButtonHint) emit doShade(); break; case QStyle::SC_TitleBarNormalButton: if(d->flags & Qt::WindowMinMaxButtonsHint) emit doNormal(); break; case QStyle::SC_TitleBarMinButton: if(d->flags & Qt::WindowMinimizeButtonHint) { if (d->window && d->window->isMinimized()) emit doNormal(); else emit doMinimize(); } break; case QStyle::SC_TitleBarMaxButton: if(d->flags & Qt::WindowMaximizeButtonHint) { if(d->window && d->window->isMaximized()) emit doNormal(); else emit doMaximize(); } break; case QStyle::SC_TitleBarCloseButton: if(d->flags & Qt::WindowSystemMenuHint) { d->buttonDown = QStyle::SC_None; emit doClose(); return; } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -