widgetselection.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 737 行 · 第 1/2 页

CPP
737
字号
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer 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 "widgetselection.h"#include "formwindow.h"#include "formwindowmanager.h"// sdk#include <QtDesigner/QDesignerFormEditorInterface>#include <QtDesigner/QExtensionManager>#include <QtDesigner/QDesignerTaskMenuExtension>// shared#include <qdesigner_command_p.h>#include <qdesigner_propertycommand_p.h>#include <layout_p.h>#include <layoutinfo_p.h>#include <formwindowbase_p.h>#include <grid_p.h>#include <QtGui/QMenu>#include <QtGui/QWidget>#include <QtGui/QMouseEvent>#include <QtGui/QStylePainter>#include <QtGui/QGridLayout>#include <QtGui/QStyleOptionToolButton>#include <QtCore/QVariant>#include <QtCore/qdebug.h>#define NO_TOPWIDGETnamespace qdesigner_internal{class TopWidget: public InvisibleWidget{    Q_OBJECTpublic:    TopWidget(QWidget *parent)        : InvisibleWidget(parent)    {        setAttribute(Qt::WA_TransparentForMouseEvents);    }};WidgetHandle::WidgetHandle(FormWindow *parent, WidgetHandle::Type t, WidgetSelection *s)    : InvisibleWidget(parent->mainContainer()),    m_widget(0),    m_type(t),    m_formWindow( parent),    m_sel(s),    m_active(true){    setMouseTracking(false);    setAutoFillBackground(true);    if (m_type == TaskMenu) {        setBackgroundRole(QPalette::Button);        setFixedSize(12, 12);    } else {        setBackgroundRole(m_active ? QPalette::Text : QPalette::Dark);        setFixedSize(6, 6);    }    updateCursor();}void WidgetHandle::updateCursor(){    if (!m_active) {        setCursor(Qt::ArrowCursor);        return;    }    switch (m_type) {    case LeftTop:        setCursor(Qt::SizeFDiagCursor);        break;    case Top:        setCursor(Qt::SizeVerCursor);        break;    case RightTop:        setCursor(Qt::SizeBDiagCursor);        break;    case Right:        setCursor(Qt::SizeHorCursor);        break;    case RightBottom:        setCursor(Qt::SizeFDiagCursor);        break;    case Bottom:        setCursor(Qt::SizeVerCursor);        break;    case LeftBottom:        setCursor(Qt::SizeBDiagCursor);        break;    case Left:        setCursor(Qt::SizeHorCursor);        break;    case TaskMenu:        setCursor(Qt::ArrowCursor);        break;    default:        Q_ASSERT(0);    }}QDesignerFormEditorInterface *WidgetHandle::core() const{    if (m_formWindow)        return m_formWindow->core();    return 0;}void WidgetHandle::setActive(bool a){    m_active = a;    if (m_type != TaskMenu) {        setBackgroundRole(m_active ? QPalette::Text : QPalette::Dark);    }    updateCursor();}void WidgetHandle::setWidget(QWidget *w){    m_widget = w;}void WidgetHandle::paintEvent(QPaintEvent *){    QDesignerFormWindowManagerInterface *m = m_formWindow->core()->formWindowManager();    QStylePainter p(this);    if (m_type == TaskMenu) {        QStyleOptionToolButton option;        option.init(this);        option.state |= QStyle::State_Raised;        option.arrowType = Qt::RightArrow;        option.toolButtonStyle = Qt::ToolButtonIconOnly;        option.features = QStyleOptionToolButton::Arrow;        option.subControls = QStyle::SC_ToolButton;        p.drawComplexControl(QStyle::CC_ToolButton, option);    } else if (m_formWindow->currentWidget() == m_widget) {        p.setPen(m->activeFormWindow() == m_formWindow ? Qt::blue : Qt::red);        p.drawRect(0, 0, width() - 1, height() - 1);    }}void WidgetHandle::mousePressEvent(QMouseEvent *e){    e->accept();    if (!m_formWindow->hasFeature(FormWindow::EditFeature))        return;    if (!(m_widget && e->button() == Qt::LeftButton))        return;    if (!(m_active || m_type == TaskMenu))        return;    QWidget *container = m_widget->parentWidget();    m_origPressPos = container->mapFromGlobal(e->globalPos());    m_geom = m_origGeom = m_widget->geometry();    if (m_type == TaskMenu && e->button() == Qt::LeftButton) {        Q_ASSERT(m_sel->taskMenuExtension());        QMenu m(this);        foreach (QAction *a, m_sel->taskMenuExtension()->taskActions()) {            m.addAction(a);        }        m.exec(e->globalPos());    }}void WidgetHandle::mouseMoveEvent(QMouseEvent *e){    if (!(m_widget && m_active && e->buttons() & Qt::LeftButton))        return;    if (m_type == TaskMenu)        return;    e->accept();    QWidget *container = m_widget->parentWidget();    const QPoint rp = container->mapFromGlobal(e->globalPos());    const QPoint d = rp - m_origPressPos;    const QRect pr = container->rect();    qdesigner_internal::Grid grid;    if (const qdesigner_internal::FormWindowBase *fwb = qobject_cast<const qdesigner_internal::FormWindowBase*>(m_formWindow))        grid = fwb->designerGrid();    switch (m_type) {    case TaskMenu:        break;    case LeftTop: {        if (rp.x() > pr.width() - 2 * width() || rp.y() > pr.height() - 2 * height())            return;        int w = m_origGeom.width() - d.x();        m_geom.setWidth(w);        w = grid.widgetHandleAdjustX(w);        int h = m_origGeom.height() - d.y();        m_geom.setHeight(h);        h = grid.widgetHandleAdjustY(h);        const int dx = m_widget->width() - w;        const int dy = m_widget->height() - h;        trySetGeometry(m_widget, m_widget->x() + dx, m_widget->y() + dy, w, h);    } break;    case Top: {        if (rp.y() > pr.height() - 2 * height())            return;        int h = m_origGeom.height() - d.y();        m_geom.setHeight(h);        h = grid.widgetHandleAdjustY(h);        const int dy = m_widget->height() - h;        trySetGeometry(m_widget, m_widget->x(), m_widget->y() + dy, m_widget->width(), h);    } break;    case RightTop: {        if (rp.x() < 2 * width() || rp.y() > pr.height() - 2 * height())            return;        int h = m_origGeom.height() - d.y();        m_geom.setHeight(h);        h = grid.widgetHandleAdjustY(h);        const int dy = m_widget->height() - h;        int w = m_origGeom.width() + d.x();        m_geom.setWidth(w);        w = grid.widgetHandleAdjustX(w);        trySetGeometry(m_widget, m_widget->x(), m_widget->y() + dy, w, h);    } break;    case Right: {        if (rp.x() < 2 * width())            return;        int w = m_origGeom.width() + d.x();        m_geom.setWidth(w);        w = grid.widgetHandleAdjustX(w);        tryResize(m_widget, w, m_widget->height());    } break;    case RightBottom: {        if (rp.x() < 2 * width() || rp.y() < 2 * height())            return;        int w = m_origGeom.width() + d.x();        m_geom.setWidth(w);        w = grid.widgetHandleAdjustX(w);        int h = m_origGeom.height() + d.y();        m_geom.setHeight(h);        h = grid.widgetHandleAdjustY(h);        tryResize(m_widget, w, h);    } break;    case Bottom: {        if (rp.y() < 2 * height())            return;        int h = m_origGeom.height() + d.y();        m_geom.setHeight(h);        h = grid.widgetHandleAdjustY(h);        tryResize(m_widget, m_widget->width(), h);    } break;    case LeftBottom: {        if (rp.x() > pr.width() - 2 * width() || rp.y() < 2 * height())            return;        int w = m_origGeom.width() - d.x();        m_geom.setWidth(w);        w = grid.widgetHandleAdjustX(w);        int h = m_origGeom.height() + d.y();        m_geom.setHeight(h);        h = grid.widgetHandleAdjustY(h);        int dx = m_widget->width() - w;        trySetGeometry(m_widget, m_widget->x() + dx, m_widget->y(), w, h);    } break;    case Left: {        if (rp.x() > pr.width() - 2 * width())            return;        int w = m_origGeom.width() - d.x();        m_geom.setWidth(w);        w = grid.widgetHandleAdjustX(w);        const int dx = m_widget->width() - w;        trySetGeometry(m_widget, m_widget->x() + dx, m_widget->y(), w, m_widget->height());    } break;    default: break;    } // end switch    m_sel->updateGeometry();    if (LayoutInfo::layoutType(m_formWindow->core(), m_widget) != LayoutInfo::NoLayout)        m_formWindow->updateChildSelections(m_widget);}void WidgetHandle::mouseReleaseEvent(QMouseEvent *e){    if (e->button() != Qt::LeftButton || !m_active)        return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?