📄 qwidgetresizehandler.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://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 "qwidgetresizehandler_p.h"#ifndef QT_NO_RESIZEHANDLER#include "qframe.h"#include "qapplication.h"#include "qdesktopwidget.h"#include "qcursor.h"#include "qsizegrip.h"#include "qevent.h"#if defined(Q_WS_WIN)#include "qt_windows.h"#endif#include "qdebug.h"#include "private/qlayoutengine_p.h"#define RANGE 4static bool resizeHorizontalDirectionFixed = false;static bool resizeVerticalDirectionFixed = false;QWidgetResizeHandler::QWidgetResizeHandler(QWidget *parent, QWidget *cw) : QObject(parent), widget(parent), childWidget(cw ? cw : parent), fw(0), extrahei(0), buttonDown(false), moveResizeMode(false), sizeprotect(true), movingEnabled(true){ mode = Nowhere; widget->setMouseTracking(true); QFrame *frame = qobject_cast<QFrame*>(widget); range = frame ? frame->frameWidth() : RANGE; range = qMax(RANGE, range); activeForMove = activeForResize = true; widget->installEventFilter(this);}void QWidgetResizeHandler::setActive(Action ac, bool b){ if (ac & Move) activeForMove = b; if (ac & Resize) activeForResize = b; if (!isActive()) setMouseCursor(Nowhere);}bool QWidgetResizeHandler::isActive(Action ac) const{ bool b = false; if (ac & Move) b = activeForMove; if (ac & Resize) b |= activeForResize; return b;}bool QWidgetResizeHandler::eventFilter(QObject *o, QEvent *ee){ if (!isActive() || (ee->type() != QEvent::MouseButtonPress && ee->type() != QEvent::MouseButtonRelease && ee->type() != QEvent::MouseMove && ee->type() != QEvent::KeyPress && ee->type() != QEvent::ShortcutOverride) ) return false; Q_ASSERT(o == widget); QWidget *w = widget; if (QApplication::activePopupWidget()) { if (buttonDown && ee->type() == QEvent::MouseButtonRelease) buttonDown = false; return false; } QMouseEvent *e = (QMouseEvent*)ee; switch (e->type()) { case QEvent::MouseButtonPress: { if (w->isMaximized()) break; if (!widget->rect().contains(widget->mapFromGlobal(e->globalPos()))) return false; if (e->button() == Qt::LeftButton) {#if defined(Q_WS_X11) /* Implicit grabs do not stop the X server from changing the cursor in children, which looks *really* bad when doing resizingk, so we grab the cursor. Note that we do not do this on Windows since double clicks are lost due to the grab (see change 198463). */ if (e->spontaneous())# if !defined(QT_NO_CURSOR) widget->grabMouse(widget->cursor());# else widget->grabMouse();# endif // QT_NO_CURSOR#endif // Q_WS_X11 buttonDown = false; emit activate(); bool me = movingEnabled; movingEnabled = (me && o == widget); mouseMoveEvent(e); movingEnabled = me; buttonDown = true; moveOffset = widget->mapFromGlobal(e->globalPos()); invertedMoveOffset = widget->rect().bottomRight() - moveOffset; if (mode == Center) { if (movingEnabled) return true; } else { return true; } } } break; case QEvent::MouseButtonRelease: if (w->isMaximized()) break; if (e->button() == Qt::LeftButton) { moveResizeMode = false; buttonDown = false; widget->releaseMouse(); widget->releaseKeyboard(); if (mode == Center) { if (movingEnabled) return true; } else { return true; } } break; case QEvent::MouseMove: { if (w->isMaximized()) break; buttonDown = buttonDown && (e->buttons() & Qt::LeftButton); // safety, state machine broken! bool me = movingEnabled; movingEnabled = (me && o == widget && (buttonDown || moveResizeMode)); mouseMoveEvent(e); movingEnabled = me; if (mode == Center) { if (movingEnabled) return true; } else { return true; } } break; case QEvent::KeyPress: keyPressEvent((QKeyEvent*)e); break; case QEvent::ShortcutOverride: if (buttonDown) { ((QKeyEvent*)ee)->accept(); return true; } break; default: break; } return false;}void QWidgetResizeHandler::mouseMoveEvent(QMouseEvent *e){ QPoint pos = widget->mapFromGlobal(e->globalPos()); if (!moveResizeMode && !buttonDown) { if (pos.y() <= range && pos.x() <= range) mode = TopLeft; else if (pos.y() >= widget->height()-range && pos.x() >= widget->width()-range) mode = BottomRight; else if (pos.y() >= widget->height()-range && pos.x() <= range) mode = BottomLeft; else if (pos.y() <= range && pos.x() >= widget->width()-range) mode = TopRight; else if (pos.y() <= range) mode = Top; else if (pos.y() >= widget->height()-range) mode = Bottom; else if (pos.x() <= range) mode = Left; else if ( pos.x() >= widget->width()-range) mode = Right; else if (widget->rect().contains(pos)) mode = Center; else mode = Nowhere; if (widget->isMinimized() || !isActive(Resize)) mode = Center;#ifndef QT_NO_CURSOR setMouseCursor(mode);#endif return; } if (mode == Center && !movingEnabled) return; if (widget->testAttribute(Qt::WA_WState_ConfigPending)) return; QPoint globalPos = (!widget->isWindow() && widget->parentWidget()) ? widget->parentWidget()->mapFromGlobal(e->globalPos()) : e->globalPos(); if (!widget->isWindow() && !widget->parentWidget()->rect().contains(globalPos)) { if (globalPos.x() < 0) globalPos.rx() = 0; if (globalPos.y() < 0) globalPos.ry() = 0; if (sizeprotect && globalPos.x() > widget->parentWidget()->width()) globalPos.rx() = widget->parentWidget()->width(); if (sizeprotect && globalPos.y() > widget->parentWidget()->height()) globalPos.ry() = widget->parentWidget()->height(); } QPoint p = globalPos + invertedMoveOffset; QPoint pp = globalPos - moveOffset;#ifdef Q_WS_X11 // Workaround for window managers which refuse to move a tool window partially offscreen. QRect desktop = qApp->desktop()->availableGeometry(widget); pp.rx() = qMax(pp.x(), desktop.left()); pp.ry() = qMax(pp.y(), desktop.top()); p.rx() = qMin(p.x(), desktop.right()); p.ry() = qMin(p.y(), desktop.bottom());#endif QSize ms = qSmartMinSize(childWidget); int mw = ms.width(); int mh = ms.height(); if (childWidget != widget) { mw += 2 * fw; mh += 2 * fw + extrahei; } QSize maxsize(childWidget->maximumSize()); if (childWidget != widget)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -