📄 formwindow.cpp
字号:
sel = widget_set.toList(); QDesignerResource builder(this); QList<QDesignerDnDItemInterface*> item_list; foreach (QWidget *widget, sel) { if (e->modifiers()#ifndef Q_WS_MAC & Qt::ControlModifier#else & Qt::AltModifier#endif ) { item_list.append(new FormWindowDnDItem(QDesignerDnDItemInterface::CopyDrop, this, widget, mapToGlobal(startPos))); } else { item_list.append(new FormWindowDnDItem(QDesignerDnDItemInterface::MoveDrop, this, widget, mapToGlobal(startPos))); widget->hide(); } } blockSelectionChanged(blocked); if (sel.count()) core()->formWindowManager()->dragItems(item_list); startPos = QPoint(); return true;}bool FormWindow::handleMouseReleaseEvent(QWidget *, QWidget *, QMouseEvent *e){ e->accept(); if (drawRubber) { // we were drawing a rubber selection endRectDraw(); // get rid of the rectangle bool blocked = blockSelectionChanged(true); selectWidgets(); // select widgets which intersect the rect blockSelectionChanged(blocked); drawRubber = false; } startPos = QPoint(); emitSelectionChanged(); // inform about selection changes return true;}void FormWindow::checkPreviewGeometry(QRect &r){ if (!rect().contains(r)) { if (r.left() < rect().left()) r.moveTopLeft(QPoint(0, r.top())); if (r.right() > rect().right()) r.moveBottomRight(QPoint(rect().right(), r.bottom())); if (r.top() < rect().top()) r.moveTopLeft(QPoint(r.left(), rect().top())); if (r.bottom() > rect().bottom()) r.moveBottomRight(QPoint(r.right(), rect().bottom())); }}void FormWindow::startRectDraw(const QPoint &pos, QWidget *, RectType t){ oldRectValid = false; rectAnchor = (t == Insert) ? gridPoint(pos) : pos; currRect = QRect(rectAnchor, QSize(0, 0)); if (!m_rubberBand) m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); m_rubberBand->setGeometry(currRect); m_rubberBand->show();}void FormWindow::continueRectDraw(const QPoint &pos, QWidget *, RectType t){ QPoint p2 = (t == Insert) ? gridPoint(pos) : pos; QRect r(rectAnchor, p2); r = r.normalized(); if (currRect == r) return; if (r.width() > 1 || r.height() > 1) { oldRectValid = true; currRect = r; if (m_rubberBand) m_rubberBand->setGeometry(currRect); } else { oldRectValid = false; }}void FormWindow::endRectDraw(){ if (m_rubberBand) { delete m_rubberBand; m_rubberBand = 0; }}QPoint FormWindow::gridPoint(const QPoint &p) const{ 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 (!mainContainer()) 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->setText(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(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -