actioneditor.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 593 行 · 第 1/2 页

CPP
593
字号
        return 0;    QListWidgetItem *item = new QListWidgetItem(m_actionRepository);    const QSize s = m_actionRepository->iconSize();    item->setSizeHint(QSize(s.width() * 3, s.height() * 2));    setListWidgetItem(action, item);    QVariant itemData;    qVariantSetValue(itemData, action);    item->setData(ActionRepository::ActionRole, itemData);    item->setFlags(item->flags() | Qt::ItemIsDropEnabled);    return item;}void ActionEditor::slotItemChanged(QListWidgetItem *item){    QDesignerFormWindowInterface *fw = formWindow();    if (!fw)        return;    QAction *action = 0;    if (item)        action = itemToAction(item);    m_actionDelete->setEnabled(action != 0);    if (!action) {        fw->clearSelection();        return;    }    QDesignerObjectInspector *oi = qobject_cast<QDesignerObjectInspector *>(core()->objectInspector());    if (action->associatedWidgets().empty()) {        // Special case: action not in object tree. Unselect all and set in property editor        fw->clearSelection(false);        if (oi)            oi->clearSelection();        core()->propertyEditor()->setObject(action);    } else {        if (oi)            oi->selectObject(action);    }}QListWidgetItem *ActionEditor::actionToItem(QAction *action) const{    const int cnt = m_actionRepository->count();    for (int i = 0; i < cnt; ++i) {        QListWidgetItem *item = m_actionRepository->item(i);        if (itemToAction(item) == action)            return item;    }    return 0;}QAction *ActionEditor::itemToAction(QListWidgetItem *item) const{    return qvariant_cast<QAction*>(item->data(ActionRepository::ActionRole));}void ActionEditor::slotActionChanged(){    QAction *action = qobject_cast<QAction*>(sender());    Q_ASSERT(action != 0);    QListWidgetItem *item = actionToItem(action);    if (item == 0) {        if (action->menu() == 0) // action got its menu deleted, create item            createListWidgetItem(action);    } else if (action->menu() != 0) { // action got its menu created, remove item        delete item;    } else {        // action text or icon changed, update item        setListWidgetItem(action, item);    }}QDesignerFormEditorInterface *ActionEditor::core() const{    return m_core;}QString ActionEditor::filter() const{    return m_filter;}void ActionEditor::setFilter(const QString &f){    m_filter = f;    m_actionRepository->filter(m_filter);}// Set changed state of icon property,  reset when icon is clearedstatic void refreshIconPropertyChanged(const QAction *action, QDesignerPropertySheetExtension *sheet){    sheet->setChanged(sheet->indexOf(QLatin1String("icon")), !action->icon().isNull());}void ActionEditor::manageAction(QAction *action){    action->setParent(formWindow()->mainContainer());    core()->metaDataBase()->add(action);    if (action->isSeparator() || action->menu() != 0)        return;    QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), action);    sheet->setChanged(sheet->indexOf(QLatin1String("objectName")), true);    sheet->setChanged(sheet->indexOf(QLatin1String("text")), true);    refreshIconPropertyChanged(action, sheet);    QListWidgetItem *item = createListWidgetItem(action);    m_actionRepository->setCurrentItem(item);    connect(action, SIGNAL(changed()), this, SLOT(slotActionChanged()));}void ActionEditor::unmanageAction(QAction *action){    core()->metaDataBase()->remove(action);    action->setParent(0);    disconnect(action, SIGNAL(changed()), this, SLOT(slotActionChanged()));    QListWidgetItem *item = actionToItem(action);    if (item == 0)        return;    delete item;}void ActionEditor::slotNewAction(){    NewActionDialog dlg(this);    dlg.setWindowTitle(tr("New action"));    if (dlg.exec() == QDialog::Accepted) {        QAction *action = new QAction(formWindow());        action->setObjectName(dlg.actionName());        formWindow()->ensureUniqueObjectName(action);        action->setText(dlg.actionText());        action->setIcon(dlg.actionIcon());        AddActionCommand *cmd = new AddActionCommand(formWindow());        cmd->init(action);        formWindow()->commandHistory()->push(cmd);    }}static inline bool isSameIcon(const QIcon &i1, const QIcon &i2){    return i1.serialNumber() == i2.serialNumber();}// return a FormWindow command to apply an iconstatic QDesignerFormWindowCommand *setIconPropertyCommand(const QIcon &newIcon, QAction *action, QDesignerFormWindowInterface *fw){    const QString iconProperty = QLatin1String("icon");    if (newIcon.isNull()) {        ResetPropertyCommand *cmd = new ResetPropertyCommand(fw);        cmd->init(action, iconProperty);        return cmd;    }    SetPropertyCommand *cmd = new SetPropertyCommand(fw);    cmd->init(action, iconProperty, newIcon);    return cmd;}static QDesignerFormWindowCommand *setTextPropertyCommand(const QString &propertyName, const QString &text, QAction *action, QDesignerFormWindowInterface *fw){    if (text.isEmpty()) {        ResetPropertyCommand *cmd = new ResetPropertyCommand(fw);        cmd->init(action, propertyName);        return cmd;    }    SetPropertyCommand *cmd = new SetPropertyCommand(fw);    cmd->init(action, propertyName, text);    return cmd;}void ActionEditor::editAction(QListWidgetItem *item){    if (!item)        return;    QAction *action = itemToAction(item);    if (action == 0)        return;    NewActionDialog dlg(this);    dlg.setWindowTitle(tr("Edit action"));    dlg.setActionData(action->text(), action->objectName(), action->icon());    if (!dlg.exec())        return;    // figure out changes and whether to start a macro    enum ChangedMask { NameChanged = 1, TextChanged = 2 , IconChanged = 4 };    const QString newName = dlg.actionName();    const QString newText = dlg.actionText();    const QIcon newIcon = dlg.actionIcon();    int changedMask = 0;    if (newName != action->objectName())        changedMask |= NameChanged;    if (newText != action->text())        changedMask |= TextChanged;    if (!isSameIcon(newIcon, action->icon()))        changedMask |= IconChanged;    if (!changedMask)        return;    const bool severalChanges = (changedMask != NameChanged) && (changedMask != TextChanged) && (changedMask != IconChanged);    if (severalChanges)        formWindow()->beginCommand(QLatin1String("Edit action"));    if (changedMask & NameChanged)        formWindow()->commandHistory()->push(setTextPropertyCommand(QLatin1String("objectName"), newName, action, formWindow()));    if (changedMask & TextChanged)        formWindow()->commandHistory()->push(setTextPropertyCommand(QLatin1String("text"), newText, action, formWindow()));    if (changedMask & IconChanged)        formWindow()->commandHistory()->push(setIconPropertyCommand(newIcon, action, formWindow()));    if (severalChanges)        formWindow()->endCommand();}void ActionEditor::slotDeleteAction(){    QListWidgetItem *item = m_actionRepository->currentItem();    if (item == 0)        return;    QAction *action = itemToAction(item);    if (action == 0)        return;    RemoveActionCommand *cmd = new RemoveActionCommand(formWindow());    cmd->init(action);    formWindow()->commandHistory()->push(cmd);}void ActionEditor::slotNotImplemented(){    QMessageBox::information(this, tr("Designer"), tr("Feature not implemented!"));}QString ActionEditor::actionTextToName(const QString &text, const QString &prefix){    QString name = text;    if (name.isEmpty())        return QString();    name[0] = name.at(0).toUpper();    name.prepend(prefix);    const QString underscore = QString(QLatin1Char('_'));    name.replace(QRegExp(QString(QLatin1String("[^a-zA-Z_0-9]"))), underscore);    name.replace(QRegExp(QLatin1String("__*")), underscore);    if (name.endsWith(underscore.at(0)))        name.truncate(name.size() - 1);    return name;}void  ActionEditor::resourceImageDropped(const ResourceMimeData &data, QAction *action){    QDesignerFormWindowInterface *fw =  formWindow();    if (!fw)        return;    const QIcon icon = resourceMimeDataToIcon(data, fw);    if (icon.isNull() || isSameIcon(icon, action->icon()))        return;    fw->commandHistory()->push(setIconPropertyCommand(icon , action, fw));}void ActionEditor::mainContainerChanged(){    // Invalidate references to objects kept in model    if (sender() == formWindow())        setFormWindow(0);}} // namespace qdesigner_internal#include "actioneditor.moc"

⌨️ 快捷键说明

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