📄 formwindow.cpp
字号:
QWidget *FormWindow::containerAt(const QPoint &pos, QWidget *notParentOf){ QWidget *container = 0; int depth = -1; QList<QWidget*> 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;}QList<QWidget*> FormWindow::selectedWidgets() const{ return usedSelections.keys();}void FormWindow::raiseSelection(QWidget *w){ WidgetSelection *s = usedSelections.value(w); if (s) s->show();}void FormWindow::selectWidgets(){ QList<QWidget*> l = qFindChildren<QWidget*>(mainContainer()); QListIterator <QWidget*> it(l); const QRect selRect(mapToGlobal(currRect.topLeft()), currRect.size()); while (it.hasNext()) { QWidget *w = it.next(); if (w->isVisibleTo(this) && isManaged(w)) { QPoint p = w->mapToGlobal(QPoint(0,0)); QRect r(p, w->size()); if (r.intersects(selRect) && !r.contains(selRect)) selectWidget(w); } } emitSelectionChanged();}bool FormWindow::handleKeyPressEvent(QWidget *widget, QWidget *, QKeyEvent *e){ if (qobject_cast<FormWindow*>(widget) || qobject_cast<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: if (e->modifiers() & Qt::ControlModifier) handleArrowKeyEvent(e->key(), e->modifiers() == Qt::ControlModifier); break; } return true;}void FormWindow::handleArrowKeyEvent(int key, bool modifier){ bool startMacro = false; QDesignerFormWindowCursorInterface *c = cursor(); if (!c->hasSelection()) return; int selCount = c->selectedWidgetCount(); int x = grid().x(); int y = grid().y(); if (modifier) { x = 1; y = 1; } // check if a layed out widget is selected for (int index=0; index<c->selectedWidgetCount(); ++index) { QWidget *w = c->selectedWidget(index); if (LayoutInfo::isWidgetLaidout(m_core, w)) return; } // 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() != c->selectedWidget(index)) { m_moveSelection.clear(); startMacro = true; break; } } } if (startMacro) beginCommand(tr("Key Move")); for (int index=0; index<selCount; ++index) { QRect geom = c->selectedWidget(index)->geometry(); switch(key) { case Qt::Key_Left: geom.adjust(-x, 0,-x, 0); break; case Qt::Key_Right: geom.adjust( x, 0, x, 0); break; case Qt::Key_Up: geom.adjust( 0,-y, 0,-y); break; case Qt::Key_Down: geom.adjust( 0, y, 0, y); break; } if (!modifier) geom.moveTopLeft(gridPoint(geom.topLeft())); SetPropertyCommand *cmd = 0; if (m_moveSelection.count() > index) cmd = m_moveSelection[index]; if (!cmd) { cmd = new SetPropertyCommand(this); cmd->init(c->selectedWidget(index), 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 blocked = blockSignals(true); foreach (QWidget *widget, m_widgets) { if (widget->isVisibleTo(this)) selectWidget(widget); } blockSignals(blocked); 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(const QList<QWidget*> &widget_list){ if (widget_list.isEmpty()) return; beginCommand(tr("Delete")); foreach (QWidget *w, widget_list) { emit widgetRemoved(w); DeleteWidgetCommand *cmd = new DeleteWidgetCommand(this); cmd->init(w); m_commandHistory->push(cmd); } endCommand();}void FormWindow::deleteWidgets(){ QList<QWidget*> selection = selectedWidgets(); simplifySelection(&selection); deleteWidgets(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 (!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); QList<QWidget*> 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(); QList<QWidget*> 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); QList<QWidget*> 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(); } else { QMessageBox::information(this, tr("Paste error"), tr("Can't paste widgets. Designer couldn't find a container\n" "to paste into which does not contain a layout. Break the layout\n" "of the container you want to paste into and select this container\n" "and then paste again.")); }}void FormWindow::manageWidget(QWidget *w){ if (isManaged(w)) return; Q_ASSERT(qobject_cast<QMenu*>(w) == 0); if (w->hasFocus()) setFocus(); core()->metaDataBase()->add(w); m_insertedWidgets.insert(w); m_widgets.append(w); setCursorToAll(Qt::ArrowCursor, w); if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(w)) manageWidget(promoted->child()); emit changed(); emit widgetManaged(w);}void FormWindow::unmanageWidget(QWidget *w){ if (!isManaged(w)) return; if (usedSelections.contains(w)) usedSelections.value(w)->setWidget(0); emit aboutToUnmanageWidget(w); core()->metaDataBase()->remove(w); m_insertedWidgets.remove(w); m_widgets.removeAt(m_widgets.indexOf(w)); emit changed(); emit widgetUnmanaged(w);}bool FormWindow::isManaged(QWidget *w) const{ return m_insertedWidgets.contains(w);}void FormWindow::breakLayout(QWidget *w){ if (w == this) w = mainContainer(); w = core()->widgetFactory()->containerOfWidget(w); beginCommand(tr("Break layout")); for (;;) { if (!w || w == this) break; if (LayoutInfo::layoutType(m_core, core()->widgetFactory()->containerOfWidget(w)) != LayoutInfo::NoLayout && core()->widgetDataBase()->isContainer(w, false)) { if (BreakLayoutCommand *cmd = breakLayoutCommand(w)) { commandHistory()->push(cmd); } if (!qobject_cast<QLayoutWidget*>(w) && !qobject_cast<QSplitter*>(w)) break; } w = w->parentWidget(); } clearSelection(false); endCommand();}BreakLayoutCommand *FormWindow::breakLayoutCommand(QWidget *w){ QList<QWidget*> widgets; QListIterator<QObject*> it(w->children()); while (it.hasNext()) { QObject *obj = it.next(); if (!obj->isWidgetType() || !core()->metaDataBase()->item(obj)) continue; widgets.append(static_cast<QWidget*>(obj)); } BreakLayoutCommand *cmd = new BreakLayoutCommand(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -