⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 widgetbox.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	    if (!iconName.startsWith("__qt_icon__"))	      wgt_elt.setAttribute(QLatin1String("icon"), wgt.iconName());            wgt_elt.setAttribute(QLatin1String("type"), QLatin1String("default"));            cat_elt.appendChild(wgt_elt);        }    }    return doc;}WidgetBoxTreeView::CategoryList    WidgetBoxTreeView::domToCateogryList(const QDomDocument &doc) const{    CategoryList result;    QDomElement root = doc.firstChildElement();    if (root.nodeName() != QLatin1String("widgetbox")) {        qWarning("WidgetCollectionModel::xmlToModel(): not a widgetbox file");        return result;    }    QDomElement cat_elt = root.firstChildElement();    for (; !cat_elt.isNull(); cat_elt = cat_elt.nextSiblingElement()) {        if (cat_elt.nodeName() != QLatin1String("category")) {            qWarning("WidgetCollectionModel::xmlToModel(): bad child of widgetbox: \"%s\"", cat_elt.nodeName().toUtf8().constData());            return result;        }        Category cat = domToCategory(cat_elt);        if (!cat.isNull())            result.append(cat);    }    return result;}WidgetBoxTreeView::Category WidgetBoxTreeView::domToCategory(const QDomElement &cat_elt) const{    Category result(cat_elt.attribute(QLatin1String("name")));    if (cat_elt.attribute(QLatin1String("type"))                                        == QLatin1String("scratchpad"))        result.setType(Category::Scratchpad);    QDomElement widget_elt = cat_elt.firstChildElement();    for (; !widget_elt.isNull(); widget_elt = widget_elt.nextSiblingElement()) {        QString type_attr = widget_elt.attribute("type");        Widget::Type type = type_attr == QLatin1String("custom")                                ? Widget::Custom                                : Widget::Default;        Widget w(widget_elt.attribute(QLatin1String("name")),                    domToString(widget_elt),                    widget_elt.attribute(QLatin1String("icon")),                    type);        result.addWidget(w);    }    return result;}static int findCategory(const QString &name, const WidgetBoxTreeView::CategoryList &list){    int idx = 0;    foreach (const WidgetBoxTreeView::Category &cat, list) {        if (cat.name() == name)            return idx;        ++idx;    }    return -1;}WidgetBoxTreeView::CategoryList WidgetBoxTreeView::loadCustomCategoryList() const{    CategoryList result;    QDesignerPluginManager *pm = m_core->pluginManager();    QList<QDesignerCustomWidgetInterface*> customWidgets = pm->registeredCustomWidgets();    foreach (QDesignerCustomWidgetInterface *c, customWidgets) {        QString dom_xml = c->domXml();        if (dom_xml.isEmpty())            continue;        QString cat_name = c->group();        if (cat_name.isEmpty())            cat_name = tr("Custom Widgets");        int idx = findCategory(cat_name, result);        if (idx == -1) {            result.append(Category(cat_name));            idx = result.size() - 1;        }        Category &cat = result[idx];        QIcon icon = c->icon();        QString icon_name;        if (icon.isNull())	    icon_name = QLatin1String("qtlogo.png");	else {	    icon_name = QLatin1String("__qt_icon__") + c->name();	    m_pluginIcons.insert(icon_name, icon);	}        cat.addWidget(Widget(c->name(), dom_xml, icon_name, Widget::Custom));    }    return result;}QTreeWidgetItem *WidgetBoxTreeView::widgetToItem(const Widget &wgt,                                                    QTreeWidgetItem *parent,                                                    bool editable){    QTreeWidgetItem *item = new QTreeWidgetItem(parent);    item->setFlags(item->flags() & ~Qt::ItemIsSelectable);    QString icon_name = wgt.iconName();    if (icon_name.isEmpty())        icon_name = QLatin1String("qtlogo.png");    bool block = blockSignals(true);    item->setText(0, wgt.name());    QIcon icon;    if (icon_name.startsWith("__qt_icon__"))      icon = m_pluginIcons.value(icon_name);    if (icon.isNull())      icon = createIconSet(icon_name);    item->setIcon(0, icon);    item->setData(0, Qt::UserRole, qVariantFromValue(wgt));    blockSignals(block);    if (editable) {        item->setFlags(Qt::ItemIsSelectable                        | Qt::ItemIsEditable                        | Qt::ItemIsEnabled);    }    return item;}WidgetBoxTreeView::Widget WidgetBoxTreeView::itemToWidget(const QTreeWidgetItem *item) const{    return qvariant_cast<Widget>(item->data(0, Qt::UserRole));}int WidgetBoxTreeView::categoryCount() const{    return topLevelItemCount();}WidgetBoxTreeView::Category WidgetBoxTreeView::category(int cat_idx) const{    Category result;    if (cat_idx >= topLevelItemCount())        return result;    QTreeWidgetItem *cat_item = topLevelItem(cat_idx);    result.setName(cat_item->text(0));    for (int i = 0; i < cat_item->childCount(); ++i) {        QTreeWidgetItem *child = cat_item->child(i);        result.addWidget(itemToWidget(child));    }    int j = cat_item->data(0, Qt::UserRole).toInt();    if (j == SCRATCHPAD_ITEM)        result.setType(Category::Scratchpad);    else        result.setType(Category::Default);    return result;}void WidgetBoxTreeView::addCategory(const Category &cat){    if (cat.widgetCount() == 0)        return;    int idx = indexOfCategory(cat.name());    QTreeWidgetItem *cat_item = 0;    if (idx == -1) {        cat_item = new QTreeWidgetItem(this);        cat_item->setText(0, cat.name());        setItemExpanded(cat_item, true);        if (cat.type() == Category::Scratchpad)            cat_item->setData(0, Qt::UserRole, SCRATCHPAD_ITEM);    } else {        cat_item = topLevelItem(idx);    }    for (int i = 0; i < cat.widgetCount(); ++i)        widgetToItem(cat.widget(i), cat_item, cat.type() == Category::Scratchpad);}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);    bool scratch = cat_item->data(0, Qt::UserRole).toInt() == 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 (parent->data(0, Qt::UserRole).toInt() == SCRATCHPAD_ITEM                && parent->childCount() == 0) {            QMetaObject::invokeMethod(this, "deleteScratchpad",                                        Qt::QueuedConnection);        }    }    delete item;    save();}void WidgetBoxTreeView::deleteScratchpad(){    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()) {        item->setText(0, widget.name());        return;    }    widget.setName(item->text(0));    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));    }    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){    QPoint global_pos = mapToGlobal(e->pos());    QTreeWidgetItem *item = itemAt(e->pos());    bool scratchpad_menu = item != 0                            && item->parent() != 0                            && item->parent()->data(0, Qt::UserRole).toInt()                                ==  SCRATCHPAD_ITEM;    if (scratchpad_menu) {        e->accept();        setCurrentItem(item);        QMenu *menu = new QMenu(this);        menu->addAction(tr("Remove"), this, SLOT(removeCurrentItem()));        menu->addAction(tr("Edit name"), this, SLOT(editCurrentItem()));        menu->exec(global_pos);    } else {        e->ignore();    }}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;        int scratch_idx = indexOfScratchpad();        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::WFlags flags)    : QDesignerWidgetBoxInterface(parent, flags), m_core(core){    m_core = core;    QVBoxLayout *l = new QVBoxLayout(this);    l->setMargin(0);    m_view = new WidgetBoxTreeView(m_core, this);    l->addWidget(m_view);    connect(m_view, SIGNAL(pressed(QString,QPoint)),            this, SLOT(handleMousePress(QString,QPoint)));}WidgetBox::~WidgetBox(){}QDesignerFormEditorInterface *WidgetBox::core() const{    return m_core;}void WidgetBox::handleMousePress(const QString &xml, const QPoint &global_mouse_pos){    DomWidget *dom_widget = xmlToUi(xml);    if (dom_widget == 0)        return;    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();}bool WidgetBox::save(){    return m_view->save();}#include "widgetbox.moc"

⌨️ 快捷键说明

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