widgetbox.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,171 行 · 第 1/3 页
CPP
1,171 行
switch (topLevelRole(cat_item)) { case SCRATCHPAD_ITEM: result.setType(Category::Scratchpad); break; default: result.setType(Category::Default); break; } return result;}void WidgetBoxTreeView::addCategory(const Category &cat){ if (cat.widgetCount() == 0) return; const bool isScratchPad = cat.type() == Category::Scratchpad; QTreeWidgetItem *cat_item = 0; if (isScratchPad) { cat_item = topLevelItem(ensureScratchpad()); } else { const int existingIndex = indexOfCategory(cat.name()); if (existingIndex == -1) { cat_item = new QTreeWidgetItem(); cat_item->setText(0, cat.name()); setTopLevelRole(NORMAL_ITEM, cat_item); // insert before scratchpad const int scratchPadIndex = indexOfScratchpad(); if (scratchPadIndex == -1) { addTopLevelItem(cat_item); } else { insertTopLevelItem(scratchPadIndex, cat_item); } setItemExpanded(cat_item, true); } else { cat_item = topLevelItem(existingIndex); } } for (int i = 0; i < cat.widgetCount(); ++i) widgetToItem(cat.widget(i), cat_item, isScratchPad);}void WidgetBoxTreeView::removeCategory(int cat_idx){ if (cat_idx >= topLevelItemCount()) return; delete takeTopLevelItem(cat_idx);}int WidgetBoxTreeView::widgetCount(int cat_idx) const{ if (cat_idx >= topLevelItemCount()) return 0; return topLevelItem(cat_idx)->childCount();}WidgetBoxTreeView::Widget WidgetBoxTreeView::widget(int cat_idx, int wgt_idx) const{ if (cat_idx >= topLevelItemCount()) return Widget(); QTreeWidgetItem *cat_item = topLevelItem(cat_idx); if (wgt_idx >= cat_item->childCount()) return Widget(); return itemToWidget(cat_item->child(wgt_idx));}void WidgetBoxTreeView::addWidget(int cat_idx, const Widget &wgt){ if (cat_idx >= topLevelItemCount()) return; QTreeWidgetItem *cat_item = topLevelItem(cat_idx); const bool scratch = topLevelRole(cat_item) == SCRATCHPAD_ITEM; widgetToItem(wgt, cat_item, scratch);}void WidgetBoxTreeView::removeWidget(int cat_idx, int wgt_idx){ if (cat_idx >= topLevelItemCount()) return; QTreeWidgetItem *cat_item = topLevelItem(cat_idx); if (wgt_idx >= cat_item->childCount()) return; delete cat_item->takeChild(wgt_idx);}void WidgetBoxTreeView::removeCurrentItem(){ QTreeWidgetItem *item = currentItem(); if (item == 0) return; QTreeWidgetItem *parent = item->parent(); if (parent == 0) { takeTopLevelItem(indexOfTopLevelItem(item)); } else { parent->takeChild(parent->indexOfChild(item)); setItemExpanded(parent, true); if (topLevelRole(parent) == SCRATCHPAD_ITEM && parent->childCount() == 0) { QMetaObject::invokeMethod(this, "deleteScratchpad", Qt::QueuedConnection); } } delete item; save();}void WidgetBoxTreeView::deleteScratchpad(){ const int idx = indexOfScratchpad(); if (idx == -1) return; delete takeTopLevelItem(idx);}void WidgetBoxTreeView::updateItemData(QTreeWidgetItem *item){ if (item->parent() == 0) return; Widget widget = qvariant_cast<Widget>(item->data(0, Qt::UserRole)); if (item->text(0).isEmpty()) { QString widgetName = widget.name(); if (!widgetName.isEmpty()) item->setText(0, widgetName); return; } widget.setName(item->text(0)); const QDomDocument doc = stringToDom(widgetDomXml(widget)); QDomElement widget_elt = doc.firstChildElement(QLatin1String("widget")); if (!widget_elt.isNull()) { widget_elt.setAttribute(QLatin1String("name"), item->text(0)); widget.setDomXml(domToString(widget_elt)); } const bool block = blockSignals(true); item->setData(0, Qt::UserRole, qVariantFromValue(widget)); blockSignals(block); save();}void WidgetBoxTreeView::editCurrentItem(){ QModelIndex index = currentIndex(); if (!index.isValid()) return; edit(index);}void WidgetBoxTreeView::contextMenuEvent(QContextMenuEvent *e){ QTreeWidgetItem *item = itemAt(e->pos()); const bool scratchpad_menu = item != 0 && item->parent() != 0 && topLevelRole(item->parent()) == SCRATCHPAD_ITEM; QMenu menu; menu.addAction(tr("Expand all"), this, SLOT(expandAll())); menu.addAction(tr("Collapse all"), this, SLOT(collapseAll())); menu.addSeparator(); if (scratchpad_menu) { setCurrentItem(item); menu.addAction(tr("Remove"), this, SLOT(removeCurrentItem())); menu.addAction(tr("Edit name"), this, SLOT(editCurrentItem())); } e->accept(); menu.exec(mapToGlobal(e->pos()));}void WidgetBoxTreeView::dropWidgets(const QList<QDesignerDnDItemInterface*> &item_list){ QTreeWidgetItem *last_item = 0; foreach (QDesignerDnDItemInterface *item, item_list) { QWidget *w = item->widget(); if (w == 0) continue; DomUI *dom_ui = item->domUi(); if (dom_ui == 0) continue; const int scratch_idx = ensureScratchpad(); QTreeWidgetItem *scratch_item = topLevelItem(scratch_idx); QDomDocument dom; QDomElement elt = dom_ui->write(dom); QString xml = domToString(elt .firstChildElement(QLatin1String("widget")) .firstChildElement(QLatin1String("widget"))); last_item = widgetToItem(Widget(w->objectName(), xml), scratch_item, true); setItemExpanded(scratch_item, true); } if (last_item != 0) { save(); QApplication::setActiveWindow(this); setCurrentItem(last_item); }}/********************************************************************************* WidgetBox*/WidgetBox::WidgetBox(QDesignerFormEditorInterface *core, QWidget *parent, Qt::WindowFlags flags) : QDesignerWidgetBox(parent, flags), m_core(core), m_view(new WidgetBoxTreeView(m_core, this)){ QVBoxLayout *l = new QVBoxLayout(this); l->setMargin(0); l->addWidget(m_view); connect(m_view, SIGNAL(pressed(QString,QString,bool,QPoint)), this, SLOT(handleMousePress(QString,QString,bool,QPoint))); setAcceptDrops (true);}WidgetBox::~WidgetBox(){}QDesignerFormEditorInterface *WidgetBox::core() const{ return m_core;}void WidgetBox::handleMousePress(const QString &name, const QString &xml, bool custom, const QPoint &global_mouse_pos){ DomWidget *dom_widget = xmlToUi(name, xml); if (dom_widget == 0) return; // Sanity check: Do the names match? if (custom) { if (dom_widget->hasAttributeClass()) { const QString domClassName = dom_widget->attributeClass(); if (domClassName != name) qdesigner_internal::designerWarning(QObject::tr("The class attribute for the class %1 does not match the class name %2.").arg(domClassName).arg(name)); } else { qdesigner_internal::designerWarning(QObject::tr("The class attribute for the class %1 is missing.").arg(name)); } } if (QApplication::mouseButtons() == Qt::LeftButton) { QList<QDesignerDnDItemInterface*> item_list; item_list.append(new WidgetBoxDnDItem(core(), dom_widget, global_mouse_pos)); m_core->formWindowManager()->dragItems(item_list); }}int WidgetBox::categoryCount() const{ return m_view->categoryCount();}QDesignerWidgetBoxInterface::Category WidgetBox::category(int cat_idx) const{ return m_view->category(cat_idx);}void WidgetBox::addCategory(const Category &cat){ m_view->addCategory(cat);}void WidgetBox::removeCategory(int cat_idx){ m_view->removeCategory(cat_idx);}int WidgetBox::widgetCount(int cat_idx) const{ return m_view->widgetCount(cat_idx);}QDesignerWidgetBoxInterface::Widget WidgetBox::widget(int cat_idx, int wgt_idx) const{ return m_view->widget(cat_idx, wgt_idx);}void WidgetBox::addWidget(int cat_idx, const Widget &wgt){ m_view->addWidget(cat_idx, wgt);}void WidgetBox::removeWidget(int cat_idx, int wgt_idx){ m_view->removeWidget(cat_idx, wgt_idx);}void WidgetBox::dropWidgets(const QList<QDesignerDnDItemInterface*> &item_list, const QPoint&){ m_view->dropWidgets(item_list);}void WidgetBox::setFileName(const QString &file_name){ m_view->setFileName(file_name);}QString WidgetBox::fileName() const{ return m_view->fileName();}bool WidgetBox::load(){ return m_view->load(loadMode());}bool WidgetBox::loadContents(const QString &contents){ return m_view->loadContents(contents, QString());}bool WidgetBox::save(){ return m_view->save();}static const QDesignerMimeData *checkDragEvent(QDropEvent * event, bool acceptEventsFromWidgetBox){ const QDesignerMimeData *mimeData = qobject_cast<const QDesignerMimeData *>(event->mimeData()); if (!mimeData) { event->ignore(); return 0; } // If desired, ignore a widget box drag and drop, where widget==0. if (!acceptEventsFromWidgetBox) { const bool fromWidgetBox = !mimeData->items().first()->widget(); if (fromWidgetBox) { event->ignore(); return 0; } } mimeData->acceptEvent(event); return mimeData;}void WidgetBox::dragEnterEvent (QDragEnterEvent * event){ // We accept event originating from the widget box also here, // because otherwise Windows will not show the DnD pixmap. checkDragEvent(event, true);}void WidgetBox::dragMoveEvent(QDragMoveEvent * event){ checkDragEvent(event, true);}void WidgetBox::dropEvent(QDropEvent * event){ const QDesignerMimeData *mimeData = checkDragEvent(event, false); if (!mimeData) return; dropWidgets(mimeData->items(), event->pos()); QDesignerMimeData::removeMovedWidgetsFromSourceForm(mimeData->items());}} // namespace qdesigner_internal#include "widgetbox.moc"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?