⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionedit.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if (m_bg_widget == 0)        return 0;    QWidget *widget = m_bg_widget->childAt(pos);    if (widget == 0)        widget = m_bg_widget;    return widget;}QRect ConnectionEdit::widgetRect(QWidget *w) const{    if (w == 0)        return QRect();    QRect r = w->geometry();    QPoint pos = w->mapToGlobal(QPoint(0, 0));    pos = mapFromGlobal(pos);    r.moveTopLeft(pos);    return r;}ConnectionEdit::State ConnectionEdit::state() const{    if (m_tmp_con != 0)        return Connecting;    if (!m_drag_end_point.isNull())        return Dragging;    return Editing;}void ConnectionEdit::paintLabel(QPainter *p, EndPoint::Type type, Connection *con){    if (con->label(type).isEmpty())        return;    bool heavy = selected(con) || con == m_tmp_con;    p->setPen(heavy ? m_active_color : m_inactive_color);    p->setBrush(Qt::NoBrush);    QRect r = con->labelRect(type);    p->drawPixmap(r.topLeft(), con->labelPixmap(type));    p->drawRect(fixRect(r));}void ConnectionEdit::paintConnection(QPainter *p, Connection *con,                                        WidgetSet *heavy_highlight_set,                                        WidgetSet *light_highlight_set) const{    QWidget *source = con->widget(EndPoint::Source);    QWidget *target = con->widget(EndPoint::Target);    bool heavy = selected(con) || con == m_tmp_con;    WidgetSet *set = heavy ? heavy_highlight_set : light_highlight_set;    p->setPen(heavy ? m_active_color : m_inactive_color);    con->paint(p);    if (source != 0)        set->insert(source, source);    if (target != 0 && target != m_bg_widget)        set->insert(target, target);}void ConnectionEdit::paintEvent(QPaintEvent *e){    QPainter p(this);    p.setClipRegion(e->region());    WidgetSet heavy_highlight_set, light_highlight_set;    foreach (Connection *con, m_con_list) {        if (!con->isVisible())            continue;        paintConnection(&p, con, &heavy_highlight_set, &light_highlight_set);    }    if (m_tmp_con != 0)        paintConnection(&p, m_tmp_con, &heavy_highlight_set, &light_highlight_set);    if (!m_widget_under_mouse.isNull() && m_widget_under_mouse != m_bg_widget)        heavy_highlight_set.insert(m_widget_under_mouse, m_widget_under_mouse);    QColor c = m_active_color;    p.setPen(c);    c.setAlpha(BG_ALPHA);    p.setBrush(c);    foreach (QWidget *w, heavy_highlight_set) {        p.drawRect(fixRect(widgetRect(w)));        light_highlight_set.remove(w);    }    c = m_inactive_color;    p.setPen(c);    c.setAlpha(BG_ALPHA);    p.setBrush(c);    foreach (QWidget *w, light_highlight_set)        p.drawRect(fixRect(widgetRect(w)));    p.setBrush(palette().color(QPalette::Base));    p.setPen(palette().color(QPalette::Text));    foreach (Connection *con, m_con_list) {        if (!con->isVisible())            continue;        paintLabel(&p, EndPoint::Source, con);        paintLabel(&p, EndPoint::Target, con);    }    p.setPen(m_active_color);    p.setBrush(m_active_color);    foreach (Connection *con, m_con_list) {        if (!selected(con) || !con->isVisible())            continue;        paintEndPoint(&p, con->endPointPos(EndPoint::Source));        if (con->widget(EndPoint::Target) != 0)            paintEndPoint(&p, con->endPointPos(EndPoint::Target));    }}void ConnectionEdit::abortConnection(){    m_tmp_con->update();    delete m_tmp_con;    m_tmp_con = 0;    setCursor(QCursor());    if (m_widget_under_mouse == m_bg_widget)        m_widget_under_mouse = 0;}void ConnectionEdit::mousePressEvent(QMouseEvent *e){    e->accept();    Connection *con_under_mouse = connectionAt(e->pos());    m_start_connection_on_drag = false;    switch (state()) {        case Connecting:            if (e->button() == Qt::RightButton)                abortConnection();            break;        case Dragging:            break;        case Editing:            if (!m_end_point_under_mouse.isNull()) {                if (!(e->modifiers() & Qt::ShiftModifier)) {                    startDrag(m_end_point_under_mouse, e->pos());                }            } else if (con_under_mouse != 0) {                if (!(e->modifiers() & Qt::ShiftModifier)) {                    selectNone();                    setSelected(con_under_mouse, true);                } else {                    setSelected(con_under_mouse, !selected(con_under_mouse));                }            } else {                if (!(e->modifiers() & Qt::ShiftModifier)) {                    selectNone();                    if (!m_widget_under_mouse.isNull())                        m_start_connection_on_drag = true;                }            }            break;    }}void ConnectionEdit::mouseDoubleClickEvent(QMouseEvent *e){    e->accept();    switch (state()) {        case Connecting:            abortConnection();            break;        case Dragging:            break;        case Editing:            if (!m_widget_under_mouse.isNull()) {                emit widgetActivated(m_widget_under_mouse);            } else if (m_sel_con_set.size() == 1) {                Connection *con = m_sel_con_set.keys().first();                modifyConnection(con);            }            break;    }}void ConnectionEdit::mouseReleaseEvent(QMouseEvent *e){    e->accept();    switch (state()) {        case Connecting:            if (m_widget_under_mouse.isNull())                abortConnection();            else                endConnection(m_widget_under_mouse, e->pos());            setCursor(QCursor());            break;        case Editing:            break;        case Dragging:            endDrag(e->pos());            break;    }}void ConnectionEdit::findObjectsUnderMouse(const QPoint &pos){    Connection *con_under_mouse = connectionAt(pos);    QWidget *w = con_under_mouse != 0 ? 0 : widgetAt(pos);    if (state() != Connecting && w == m_bg_widget)        w = 0;    if (w != m_widget_under_mouse) {        if (!m_widget_under_mouse.isNull())            update(widgetRect(m_widget_under_mouse));        m_widget_under_mouse = w;        if (!m_widget_under_mouse.isNull())            update(widgetRect(m_widget_under_mouse));    }    EndPoint hs = endPointAt(pos);    if (hs != m_end_point_under_mouse) {        if (m_end_point_under_mouse.isNull())            setCursor(Qt::PointingHandCursor);        else            setCursor(QCursor());        m_end_point_under_mouse = hs;    }}void ConnectionEdit::mouseMoveEvent(QMouseEvent *e){    findObjectsUnderMouse(e->pos());    switch (state()) {        case Connecting:            continueConnection(m_widget_under_mouse, e->pos());            break;        case Editing:            if ((e->buttons() & Qt::LeftButton)                    && m_start_connection_on_drag                    && !m_widget_under_mouse.isNull()) {                m_start_connection_on_drag = false;                startConnection(m_widget_under_mouse, e->pos());                setCursor(Qt::CrossCursor);            }            break;        case Dragging:            continueDrag(e->pos());            break;    }    e->accept();}void ConnectionEdit::keyPressEvent(QKeyEvent *e){    switch (e->key()) {        case Qt::Key_Delete:            if (state() == Editing)                deleteSelected();            break;        case Qt::Key_Escape:            if (state() == Connecting)                abortConnection();            break;    }    e->accept();}void ConnectionEdit::startConnection(QWidget *source, const QPoint &pos){    Q_ASSERT(m_tmp_con == 0);    m_tmp_con = new Connection(this);    m_tmp_con->setEndPoint(EndPoint::Source, source, pos);}void ConnectionEdit::endConnection(QWidget *target, const QPoint &pos){    Q_ASSERT(m_tmp_con != 0);    m_tmp_con->setEndPoint(EndPoint::Target, target, pos);    QWidget *source = m_tmp_con->widget(EndPoint::Source);    Q_ASSERT(source != 0);    Q_ASSERT(target != 0);    setEnabled(false);    Connection *new_con = createConnection(source, target);    setEnabled(true);    if (new_con != 0) {        new_con->setEndPoint(EndPoint::Source, source, m_tmp_con->endPointPos(EndPoint::Source));        new_con->setEndPoint(EndPoint::Target, target, m_tmp_con->endPointPos(EndPoint::Target));        m_undo_stack->push(new AddConnectionCommand(this, new_con));    }    delete m_tmp_con;    m_tmp_con = 0;    findObjectsUnderMouse(mapFromGlobal(QCursor::pos()));}void ConnectionEdit::continueConnection(QWidget *target, const QPoint &pos){    Q_ASSERT(m_tmp_con != 0);    m_tmp_con->setEndPoint(EndPoint::Target, target, pos);}void ConnectionEdit::modifyConnection(Connection *){}Connection *ConnectionEdit::createConnection(QWidget *source, QWidget *target){    Connection *con = new Connection(this, source, target);    return con;}void ConnectionEdit::widgetRemoved(QWidget *widget){    QList<QWidget*> child_list = qFindChildren<QWidget*>(widget);    child_list.prepend(widget);    ConnectionSet remove_set;    foreach (QWidget *w, child_list) {        foreach (Connection *con, m_con_list) {            if (con->widget(EndPoint::Source) == w || con->widget(EndPoint::Target) == w)                remove_set.insert(con, con);        }    }    if (!remove_set.isEmpty())        m_undo_stack->push(new DeleteConnectionsCommand(this, remove_set.keys()));    updateBackground();}void ConnectionEdit::setSelected(Connection *con, bool sel){    if (!con || sel == m_sel_con_set.contains(con))        return;    if (sel) {        m_sel_con_set.insert(con, con);        emit connectionSelected(con);    } else {        m_sel_con_set.remove(con);    }    con->update();}bool ConnectionEdit::selected(const Connection *con) const{    return m_sel_con_set.contains(const_cast<Connection*>(con));}void ConnectionEdit::selectNone(){    foreach (Connection *con, m_sel_con_set)        con->update();    m_sel_con_set.clear();}Connection *ConnectionEdit::connectionAt(const QPoint &pos) const{    foreach (Connection *con, m_con_list) {        if (con->contains(pos))            return con;    }    return 0;}CETypes::EndPoint ConnectionEdit::endPointAt(const QPoint &pos) const{    foreach (Connection *con, m_con_list) {        if (!selected(con))            continue;        QRect sr = con->endPointRect(EndPoint::Source);        QRect tr = con->endPointRect(EndPoint::Target);        if (sr.contains(pos))            return EndPoint(con, EndPoint::Source);        if (tr.contains(pos))            return EndPoint(con, EndPoint::Target);    }    return EndPoint();}void ConnectionEdit::startDrag(const EndPoint &end_point, const QPoint &pos){    Q_ASSERT(m_drag_end_point.isNull());    m_drag_end_point = end_point;    m_old_source_pos = m_drag_end_point.con->endPointPos(EndPoint::Source);    m_old_target_pos = m_drag_end_point.con->endPointPos(EndPoint::Target);    adjustHotSopt(m_drag_end_point, pos);}void ConnectionEdit::continueDrag(const QPoint &pos){    Q_ASSERT(!m_drag_end_point.isNull());    adjustHotSopt(m_drag_end_point, pos);}void ConnectionEdit::endDrag(const QPoint &pos){    Q_ASSERT(!m_drag_end_point.isNull());    adjustHotSopt(m_drag_end_point, pos);    Connection *con = m_drag_end_point.con;    QPoint new_source_pos = con->endPointPos(EndPoint::Source);    QPoint new_target_pos = con->endPointPos(EndPoint::Target);    m_undo_stack->push(new AdjustConnectionCommand(this, con, m_old_source_pos, m_old_target_pos,                                                    new_source_pos, new_target_pos));    m_drag_end_point = EndPoint();}void ConnectionEdit::adjustHotSopt(const EndPoint &end_point, const QPoint &pos){    QWidget *w = end_point.con->widget(end_point.type);    end_point.con->setEndPoint(end_point.type, w, pointInsideRect(widgetRect(w), pos));}void ConnectionEdit::deleteSelected(){    if (m_sel_con_set.isEmpty())        return;    m_undo_stack->push(new DeleteConnectionsCommand(this, m_sel_con_set.keys()));}void ConnectionEdit::addConnection(Connection *con){    m_con_list.append(con);}void ConnectionEdit::updateLines(){    foreach (Connection *con, m_con_list)        con->checkWidgets();}void ConnectionEdit::resizeEvent(QResizeEvent *e){    updateBackground();    QWidget::resizeEvent(e);}void ConnectionEdit::setSource(Connection *con, const QString &obj_name){    QObject *object = qFindChild<QObject*>(m_bg_widget, obj_name);    if (object == 0 && m_bg_widget->objectName() == obj_name)        object = m_bg_widget;    if (object == con->object(EndPoint::Source))        return;    m_undo_stack->push(new SetEndPointCommand(this, con, EndPoint::Source, object));}void ConnectionEdit::setTarget(Connection *con, const QString &obj_name){    QObject *object = qFindChild<QObject*>(m_bg_widget, obj_name);    if (object == 0 && m_bg_widget->objectName() == obj_name)        object = m_bg_widget;    if (object == con->object(EndPoint::Target))        return;    m_undo_stack->push(new SetEndPointCommand(this, con, EndPoint::Target, object));}} // namespace qdesigner_internal#include "connectionedit.moc"

⌨️ 快捷键说明

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