📄 formwindow.cpp
字号:
{ return QPoint(((p.x() + grid().x()/2) / grid().x()) * grid().x(), ((p.y() + grid().y()/2) / grid().y()) * grid().y());}QWidget *FormWindow::currentWidget() const{ return m_currentWidget;}void FormWindow::setCurrentWidget(QWidget *currentWidget){ m_currentWidget = currentWidget;}QSize FormWindow::sizeHint() const{ QMainWindow *mw = qobject_cast<QMainWindow*>(mainContainer()); if (!mw) { return QSize(400, 300); } QSize sh = mw->sizeHint(); if (sh.width() < 400) sh.setWidth(400); if (sh.height() < 300) sh.setHeight(300); return sh;}void FormWindow::selectWidget(QWidget* w, bool select){ if (!isManaged(w) && !isCentralWidget(w)) return; if (!select && !isWidgetSelected(w)) return; if (isMainContainer(w)) { QWidget *opw = m_currentWidget; setCurrentWidget(mainContainer()); repaintSelection(opw); emitSelectionChanged(); return; } if (isCentralWidget(w)) { QWidget *opw = m_currentWidget; setCurrentWidget(mainContainer()); repaintSelection(opw); emitSelectionChanged(); return; } if (select) { QWidget *opw = m_currentWidget; setCurrentWidget(w); repaintSelection(opw); WidgetSelection *s = usedSelections.value(w); if (s != 0) { s->show(); return; } QListIterator<WidgetSelection*> it(selections); while (it.hasNext()) { WidgetSelection *sel = it.next(); if (!sel->isUsed()) { s = sel; break; } } if (s == 0) { s = new WidgetSelection(this, &usedSelections); selections.append(s); } s->setWidget(w); } else { if (WidgetSelection *s = usedSelections.value(w)) s->setWidget(0); if (!usedSelections.isEmpty()) setCurrentWidget((*usedSelections.begin())->widget()); else setCurrentWidget(mainContainer()); repaintSelection(currentWidget()); } emitSelectionChanged();}void FormWindow::hideSelection(QWidget *w){ selectWidget(w, false);}void FormWindow::clearSelection(bool changePropertyDisplay){ for (QHash<QWidget *, WidgetSelection *>::Iterator it = usedSelections.begin(); it != usedSelections.end(); ++it) { it.value()->setWidget(0, false); } usedSelections.clear(); if (changePropertyDisplay == false) return; setCurrentWidget(mainContainer()); if (m_currentWidget) repaintSelection(m_currentWidget); emitSelectionChanged();}void FormWindow::emitSelectionChanged(){ if (m_blockSelectionChanged == true) { // nothing to do return; } m_selectionChangedTimer->start(0);}void FormWindow::selectionChangedTimerDone(){ emit selectionChanged();}void FormWindow::repaintSelection(QWidget *w){ if (WidgetSelection *s = usedSelections.value(w)) s->update();}bool FormWindow::isWidgetSelected(QWidget *w) const{ return usedSelections.contains(w);}bool FormWindow::isMainContainer(const QWidget *w) const{ return w && (w == this || w == mainContainer());}void FormWindow::updateChildSelections(QWidget *w){ QList<QWidget*> l = qFindChildren<QWidget*>(w); QListIterator<QWidget*> it(l); while (it.hasNext()) { QWidget *w = it.next(); if (isManaged(w)) { updateSelection(w); } }}void FormWindow::updateSelection(QWidget *w){ WidgetSelection *s = usedSelections.value(w); if (!w->isVisibleTo(this)) { selectWidget(w, false); } else if (s) s->updateGeometry();}QWidget *FormWindow::designerWidget(QWidget *w) const{ while (w && !isMainContainer(w) && !isManaged(w) || isCentralWidget(w)) w = w->parentWidget(); return w;}bool FormWindow::isCentralWidget(QWidget *w) const{ if (QMainWindow *mainWindow = qobject_cast<QMainWindow*>(mainContainer())) return w == mainWindow->centralWidget(); return false;}void FormWindow::ensureUniqueObjectName(QObject *object){ QString name = object->objectName(); if (!unify(object, name, true)) { object->setObjectName(name); }}template <class T, class P>void merge(QList<T> *target, const QList<P> &source){ foreach (P item, source) { target->append(item); }}bool FormWindow::unify(QObject *w, QString &s, bool changeIt){ bool found = !isMainContainer(static_cast<QWidget*>(w)) && objectName() == s; if (!found) { QList<QObject*> objects; if (mainContainer()) { objects.append(mainContainer()); merge(&objects, qFindChildren<QWidget*>(mainContainer())); merge(&objects, qFindChildren<QAction*>(mainContainer())); } QMutableListIterator<QObject*> mit(objects); while (mit.hasNext()) { if (!core()->metaDataBase()->item(mit.next())) { mit.remove(); } } QListIterator<QObject*> it(objects); while (it.hasNext()) { QObject *child = it.next(); if (child != w && child->objectName() == s) { found = true; if (!changeIt) break; qlonglong num = 0; qlonglong factor = 1; int idx = s.length()-1; for ( ; (idx > 0) && s.at(idx).isDigit(); --idx) { num += (s.at(idx).unicode() - QLatin1Char('0').unicode()) * factor; factor *= 10; } 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()); r.moveTopLeft(gridPoint(container->mapFromGlobal(r.topLeft()))); 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); QList<QWidget*> 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();}static bool isDescendant(const QWidget *parent, const QWidget *child){ for (; child != 0; child = child->parentWidget()) { if (child == parent) return true; } return false;}void FormWindow::resizeWidget(QWidget *widget, const QRect &geometry){ Q_ASSERT(isDescendant(this, widget)); QRect r = geometry; r.moveTopLeft(gridPoint(geometry.topLeft())); SetPropertyCommand *cmd = new SetPropertyCommand(this); cmd->init(widget, QLatin1String("geometry"), r); cmd->setDescription(tr("Resize")); m_commandHistory->push(cmd);}void FormWindow::raiseChildSelections(QWidget *w){ QList<QWidget*> l = qFindChildren<QWidget*>(w); if (l.isEmpty()) return; QHashIterator<QWidget *, WidgetSelection *> it(usedSelections); while (it.hasNext()) { it.next(); WidgetSelection *w = it.value(); if (l.contains(w->widget())) w->show(); }}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->currentIndex()) { 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->setDescription(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->currentIndex(); }}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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -