formwindow.cpp

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

CPP
2,326
字号
                if ((idx >= 0) && (QLatin1Char('_') == s.at(idx)))                    s = s.left(idx+1) + QString::number(num+1);                else                    s = s + QLatin1String("_2");                it.toFront();            }        }    }    return !found;}/* already_in_form is true when we are moving a widget from one parent to another inside the same * form. All this means is that InsertWidgetCommand::undo() must not unmanage it. */void FormWindow::insertWidget(QWidget *w, const QRect &rect, QWidget *container, bool already_in_form){    clearSelection(false);    beginCommand(tr("Insert widget '%1").arg(QString::fromUtf8(w->metaObject()->className()))); // ### use the WidgetDatabaseItem    /* Reparenting into a QSplitter automatically adjusts child's geometry. We create the geometry     * command before we push the reparent command, so that the geometry command has the original     * geometry of the widget. */    QRect r = rect;    Q_ASSERT(r.isValid());    SetPropertyCommand *geom_cmd = new SetPropertyCommand(this);    geom_cmd->init(w, QLatin1String("geometry"), r); // ### use rc.size()    if (w->parentWidget() != container) {        ReparentWidgetCommand *cmd = new ReparentWidgetCommand(this);        cmd->init(w, container);        m_commandHistory->push(cmd);    }    m_commandHistory->push(geom_cmd);    InsertWidgetCommand *cmd = new InsertWidgetCommand(this);    cmd->init(w, already_in_form);    m_commandHistory->push(cmd);    endCommand();    w->show();}QWidget *FormWindow::createWidget(DomUI *ui, const QRect &rc, QWidget *target){    QWidget *container = findContainer(target, false);    if (!container)        return 0;    if (isMainContainer(container)) {        if (QMainWindow *mw = qobject_cast<QMainWindow*>(container)) {            Q_ASSERT(mw->centralWidget() != 0);            container = mw->centralWidget();        }    }    QDesignerResource resource(this);    const QWidgetList widgets = resource.paste(ui, container);    Q_ASSERT(widgets.size() == 1); // multiple-paste from DomUI not supported yet    insertWidget(widgets.first(), rc, container);    return widgets.first();}#ifndef QT_NO_DEBUGstatic bool isDescendant(const QWidget *parent, const QWidget *child){    for (; child != 0; child = child->parentWidget()) {        if (child == parent)            return true;    }    return false;}#endifvoid FormWindow::resizeWidget(QWidget *widget, const QRect &geometry){    Q_ASSERT(isDescendant(this, widget));    QRect r = geometry;    SetPropertyCommand *cmd = new SetPropertyCommand(this);    cmd->init(widget, QLatin1String("geometry"), r);    cmd->setText(tr("Resize"));    m_commandHistory->push(cmd);}void FormWindow::raiseChildSelections(QWidget *w){    const QWidgetList l = qFindChildren<QWidget*>(w);    if (l.isEmpty())        return;    m_selection->raiseList(l);}QWidget *FormWindow::containerAt(const QPoint &pos, QWidget *notParentOf){    QWidget *container = 0;    int depth = -1;    const QWidgetList selected = selectedWidgets();    if (rect().contains(mapFromGlobal(pos))) {        container = mainContainer();        depth = widgetDepth(container);    }    QListIterator<QWidget*> it(m_widgets);    while (it.hasNext()) {        QWidget *wit = it.next();        if (qobject_cast<QLayoutWidget*>(wit) || qobject_cast<QSplitter*>(wit))            continue;        if (!wit->isVisibleTo(this))            continue;        if (selected.indexOf(wit) != -1)            continue;        if (!core()->widgetDataBase()->isContainer(wit) &&             wit != mainContainer())            continue;        // the rectangles of all ancestors of the container must contain the insert position        QWidget *w = wit;        while (w && !w->isWindow()) {            if (!w->rect().contains((w->mapFromGlobal(pos))))                break;            w = w->parentWidget();        }        if (!(w == 0 || w->isWindow()))            continue; // we did not get through the full while loop        int wd = widgetDepth(wit);        if (wd == depth && container) {            if (wit->parentWidget()->children().indexOf(wit) >                 container->parentWidget()->children().indexOf(container))                wd++;        }        if (wd > depth && !isChildOf(wit, notParentOf)) {            depth = wd;            container = wit;        }    }    return container;}QWidgetList FormWindow::selectedWidgets() const{    return m_selection->selectedWidgets();}void FormWindow::selectWidgets(){    bool selectionChanged = false;    const QWidgetList l = qFindChildren<QWidget*>(mainContainer());    QListIterator <QWidget*> it(l);    const QRect selRect(mapToGlobal(m_currRect.topLeft()), m_currRect.size());    while (it.hasNext()) {        QWidget *w = it.next();        if (w->isVisibleTo(this) && isManaged(w)) {            const QPoint p = w->mapToGlobal(QPoint(0,0));            const QRect r(p, w->size());            if (r.intersects(selRect) && !r.contains(selRect) && trySelectWidget(w, true))                selectionChanged = true;        }    }    if (selectionChanged)        emitSelectionChanged();}bool FormWindow::handleKeyPressEvent(QWidget *widget, QWidget *, QKeyEvent *e){    if (qobject_cast<const FormWindow*>(widget) || qobject_cast<const QMenu*>(widget))        return false;    e->accept(); // we always accept!    switch (e->key()) {        default: break; // we don't care about the other keys        case Qt::Key_Delete:        case Qt::Key_Backspace:            deleteWidgets();            break;        case Qt::Key_Tab:            cursor()->movePosition(QDesignerFormWindowCursorInterface::Next);            break;        case Qt::Key_Backtab:            cursor()->movePosition(QDesignerFormWindowCursorInterface::Prev);            break;        case Qt::Key_Left:        case Qt::Key_Right:        case Qt::Key_Up:        case Qt::Key_Down:            handleArrowKeyEvent(e->key(), e->modifiers());            break;    }    return true;}int FormWindow::getValue(const QRect &rect, int key, bool size) const{    if (size) {        if (key == Qt::Key_Left || key == Qt::Key_Right)            return rect.width();        return rect.height();    }    if (key == Qt::Key_Left || key == Qt::Key_Right)        return rect.x();    return rect.y();}int FormWindow::calcValue(int val, bool forward, bool snap, int snapOffset) const{    if (snap) {        const int rest = val % snapOffset;        if (rest) {            const int offset = forward ? snapOffset : 0;            const int newOffset = rest < 0 ? offset - snapOffset : offset;            return val + newOffset - rest;        }        return (forward ? val + snapOffset : val - snapOffset);    }    return (forward ? val + 1 : val - 1);}QRect FormWindow::applyValue(const QRect &rect, int val, int key, bool size) const{    QRect r = rect;    if (size) {        if (key == Qt::Key_Left || key == Qt::Key_Right)            r.setWidth(val);        else            r.setHeight(val);    } else {        if (key == Qt::Key_Left || key == Qt::Key_Right)            r.moveLeft(val);        else            r.moveTop(val);    }    return r;}void FormWindow::handleArrowKeyEvent(int key, Qt::KeyboardModifiers modifiers){    bool startMacro = false;    QDesignerFormWindowCursorInterface *c = cursor();    if (!c->hasSelection())        return;    QList<QWidget *> selection;    // check if a laid out widget is selected    for (int index = 0; index < c->selectedWidgetCount(); ++index) {        QWidget *w = c->selectedWidget(index);        if (!LayoutInfo::isWidgetLaidout(m_core, w))            selection.append(w);    }    if (selection.isEmpty())        return;    QWidget *current = c->current();    if (!current || LayoutInfo::isWidgetLaidout(m_core, current)) {        current = selection.first();    }    const bool size = modifiers & Qt::ShiftModifier;    const bool snap = !(modifiers & Qt::ControlModifier);    const bool forward = (key == Qt::Key_Right || key == Qt::Key_Down);    const int snapPoint = (key == Qt::Key_Left || key == Qt::Key_Right) ? grid().x() : grid().y();    const int oldValue = getValue(current->geometry(), key, size);    const int newValue = calcValue(oldValue, forward, snap, snapPoint);    const int offset = newValue - oldValue;    const int selCount = selection.count();    // check if selection is the same as last time    if (selCount != m_moveSelection.count() ||        m_lastUndoIndex != m_commandHistory->index()) {        m_moveSelection.clear();        startMacro = true;    } else {        for (int index = 0; index < selCount; ++index) {            if (m_moveSelection[index]->object() != selection.at(index)) {                m_moveSelection.clear();                startMacro = true;                break;            }        }    }    if (startMacro)        beginCommand(tr("Key Move"));    for (int index = 0; index < selCount; ++index) {        QWidget *w = selection.at(index);        const QRect oldGeom = w->geometry();        const QRect geom = applyValue(oldGeom, getValue(oldGeom, key, size) + offset, key, size);        SetPropertyCommand *cmd = 0;        if (m_moveSelection.count() > index)            cmd = m_moveSelection[index];        if (!cmd) {            cmd = new SetPropertyCommand(this);            cmd->init(w, QLatin1String("geometry"), geom);            cmd->setText(tr("Key Move"));            m_commandHistory->push(cmd);            if (m_moveSelection.count() > index)                m_moveSelection.replace(index, cmd);            else                m_moveSelection.append(cmd);        } else {            cmd->setNewValue(geom);            cmd->redo();        }    }    if (startMacro) {        endCommand();        m_lastUndoIndex = m_commandHistory->index();    }}bool FormWindow::handleKeyReleaseEvent(QWidget *, QWidget *, QKeyEvent *e){    e->accept();    return true;}void FormWindow::selectAll(){    bool selectionChanged = false;    foreach (QWidget *widget, m_widgets) {        if (widget->isVisibleTo(this) && trySelectWidget(widget, true))            selectionChanged = true;    }    if (selectionChanged)        emitSelectionChanged();}void FormWindow::layoutHorizontal(){    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), selectedWidgets(), LayoutInfo::HBox);    clearSelection(false);    commandHistory()->push(cmd);}void FormWindow::layoutVertical(){    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), selectedWidgets(), LayoutInfo::VBox);    clearSelection(false);    commandHistory()->push(cmd);}void FormWindow::layoutGrid(){    LayoutCommand *cmd = new LayoutCommand(this);    cmd->init(mainContainer(), selectedWidgets(), LayoutInfo::Grid);    clearSelection(false);    commandHistory()->push(cmd);}void FormWindow::deleteWidgets(){    QWidgetList selection = selectedWidgets();    simplifySelection(&selection);    deleteWidgetList(selection);}QString FormWindow::fileName() const{    return m_fileName;}void FormWindow::setFileName(const QString &fileName){    if (m_fileName == fileName)        return;    m_fileName = fileName;    emit fileNameChanged(fileName);}QString FormWindow::contents() const{    QBuffer b;    if (!mainContainer() || !b.open(QIODevice::WriteOnly))        return QString();    QDesignerResource resource(const_cast<FormWindow*>(this));    resource.save(&b, mainContainer());    return QString::fromUtf8(b.buffer());}void FormWindow::copy(){    QBuffer b;    if (!b.open(QIODevice::WriteOnly))        return;    QDesignerResource resource(this);    QWidgetList sel = selectedWidgets();    simplifySelection(&sel);    resource.copy(&b, sel);    qApp->clipboard()->setText(QString::fromUtf8(b.buffer()), QClipboard::Clipboard);}void FormWindow::cut(){    copy();    deleteWidgets();}void FormWindow::paste(){    QWidget *w = mainContainer();    const QWidgetList l(selectedWidgets());    if (l.count() == 1) {        w = l.first();        w = m_core->widgetFactory()->containerOfWidget(w);        if (LayoutInfo::layoutType(m_core, w) != LayoutInfo::NoLayout ||             (!core()->widgetDataBase()->isContainer(w) &&               w != mainContainer()))            w = mainContainer();    }    if (w && LayoutInfo::layoutType(m_core, w) == LayoutInfo::NoLayout) {        clearSelection(true);        QByteArray code = qApp->clipboard()->text().toUtf8();        QBuffer b(&code);        b.open(QIODevice::ReadOnly);        QDesignerResource resource(this);        QWidget *widget = core()->widgetFactory()->containerOfWidget(w);       const  QWidgetList widgets = resource.paste(&b, widget);        beginCommand(tr("Paste"));        foreach (QWidget *w, widgets) {            InsertWidgetCommand *cmd = new InsertWidgetCommand(this);            cmd->init(w);            m_commandHistory->push(cmd);            selectWidget(w);        }        endCommand();        /* This will put the freshly pasted widgets into the clipboard, replacing the original.         *  The point here is that the copied widgets are shifted a little with respect to the original.         *  If the user presses paste again, the pasted widgets will be shifted again, rather than         *  appearing on top of the previously pasted widgets. */        copy();

⌨️ 快捷键说明

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