📄 q3titlebar.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** 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 "qplatformdefs.h"#ifndef QT_NO_TITLEBAR#include "qapplication.h"#include "qcursor.h"#include "qdatetime.h"#include "qevent.h"#include "qimage.h"#include "qpainter.h"#include "qiodevice.h"#include "qpixmap.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtimer.h"#include "qtooltip.h"#include "qdebug.h"#if defined(Q_WS_WIN)#include "qt_windows.h"#endif#include "private/qapplication_p.h"#include "private/q3titlebar_p.h"#include "private/qwidget_p.h"class Q3TitleBarPrivate : public QWidgetPrivate{ Q_DECLARE_PUBLIC(Q3TitleBar)public: Q3TitleBarPrivate() : toolTip(0), act(0), window(0), movable(1), pressed(0), autoraise(0), inevent(0) { } Qt::WindowFlags flags; QStyle::SubControl buttonDown; QPoint moveOffset; QToolTip *toolTip; bool act :1; QWidget* window; bool movable :1; bool pressed :1; bool autoraise :1; bool inevent : 1; int titleBarState() const; QStyleOptionTitleBar getStyleOption() const; void readColors();};inline int Q3TitleBarPrivate::titleBarState() const{ uint state = window ? window->windowState() : static_cast<Qt::WindowStates>(Qt::WindowNoState); state |= uint(act ? QStyle::State_Active : QStyle::State_None); return (int)state;}QStyleOptionTitleBar Q3TitleBarPrivate::getStyleOption() const{ Q_Q(const Q3TitleBar); QStyleOptionTitleBar opt; opt.init(q); opt.text = q->windowTitle(); //################ QIcon icon = q->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; return opt;}Q3TitleBar::Q3TitleBar(QWidget *w, QWidget *parent, Qt::WindowFlags f) : QWidget(*new Q3TitleBarPrivate, parent, Qt::WStyle_Customize | Qt::WStyle_NoBorder){ Q_D(Q3TitleBar); 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->minimumSize() == w->maximumSize()) 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));}void Q3TitleBar::setFakeWindowFlags(Qt::WindowFlags f){ Q_D(Q3TitleBar); d->flags = f;}Qt::WindowFlags Q3TitleBar::fakeWindowFlags() const{ Q_D(const Q3TitleBar); return d->flags;}Q3TitleBar::~Q3TitleBar(){}QStyleOptionTitleBar Q3TitleBar::getStyleOption() const{ return d_func()->getStyleOption();}#ifdef Q_WS_WINstatic inline QRgb colorref2qrgb(COLORREF col){ return qRgb(GetRValue(col),GetGValue(col),GetBValue(col));}#endifvoid Q3TitleBarPrivate::readColors(){ Q_Q(Q3TitleBar); 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::Window)); } q->setPalette(pal); q->setActive(act);}void Q3TitleBar::changeEvent(QEvent *ev){ if(ev->type() == QEvent::ModifiedChange) update(); QWidget::changeEvent(ev);}void Q3TitleBar::mousePressEvent(QMouseEvent *e){ Q_D(Q3TitleBar); if (!d->act) emit doActivate(); if (e->button() == Qt::LeftButton) { 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 Q3TitleBar *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: if (d->flags & Qt::WindowMinMaxButtonsHint) 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; } repaint(); } else { d->pressed = false; }}void Q3TitleBar::contextMenuEvent(QContextMenuEvent *e){ Q_D(Q3TitleBar); QStyleOptionTitleBar opt = d->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 Q3TitleBar::mouseReleaseEvent(QMouseEvent *e){ Q_D(Q3TitleBar); if (e->button() == Qt::LeftButton && d->pressed) { e->accept(); QStyleOptionTitleBar opt = d->getStyleOption(); QStyle::SubControl ctrl = style()->hitTestComplexControl(QStyle::CC_TitleBar, &opt, e->pos(), this); d->pressed = false; if (ctrl == d->buttonDown) { d->buttonDown = QStyle::SC_None;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -