QCAD属性与Qt Event

QCAD属性与Qt Event

自定义Qt事件

class RPropertyEvent : public QEvent {
public:
    RPropertyEvent() :
        QEvent((QEvent::Type)(QEvent::User+500)),
        propertyTypeId(-1) {
    }

    /**
     * \\param propertyTypeId ID of the property that was changed.
     * \\param value New value of the property.
     */
    RPropertyEvent(RPropertyTypeId propertyTypeId, const QVariant& value,
                   RS::EntityType entityTypeFilter = RS::EntityAll)
        : QEvent((QEvent::Type)(QEvent::User+500)),
          propertyTypeId(propertyTypeId),
          value(value),
          entityTypeFilter(entityTypeFilter) {
    }

    /**
     * \\return The position of the event in real graphic measures.
     */
    RPropertyTypeId getPropertyTypeId() const {
        return propertyTypeId;
    }

    /**
     * \\return true If the position of the coordinate event is valid.
     */
    QVariant getValue() const {
        return value;
    }

    /**
     * \\return Entity type filter to use for this event.
     */
    RS::EntityType getEntityTypeFilter() const {
        return entityTypeFilter;
    }

private:
    RPropertyTypeId propertyTypeId;
    QVariant value;
    RS::EntityType entityTypeFilter;
};

事件处理

//
bool RMainWindowQt::event(QEvent* e){
    RPropertyEvent* pe = dynamic_cast<RPropertyEvent*>(e);
   if (pe!=NULL) {
       RDocumentInterface* documentInterface = getDocumentInterface();
       if (documentInterface!=NULL) {
           documentInterface->propertyChangeEvent(*pe);
       }
   }
}
//
void RDocumentInterface::propertyChangeEvent(RPropertyEvent& event) {
   if (hasCurrentAction()) {
       getCurrentAction()->propertyChangeEvent(event);
   } else if (defaultAction != NULL) {
       defaultAction->propertyChangeEvent(event);
   }
}
//
/**
* Allows all actions to handle property change events. This is neccessary to
* ensure that properties can be changed even if a tool is active (e.g. while
* drawing lines).
*/
EAction.prototype.propertyChangeEvent = function(event) {
   var di = this.getDocumentInterface();
   if (isNull(di)) {
       return;
   }

   var operation = new RChangePropertyOperation(event);
   di.applyOperation(operation);
   di.clearPreview();
   di.repaintViews();
};

发送事件

// initialize fixed general properties at the top:
   var layerCombo = this.widget.findChild("Layer");
   layerCombo['activated(QString)'].connect(
               new PropertyWatcher(this, layerCombo, REntity.PropertyLayer),
               'propertyChanged');
/**
* Called when a property has been changed by the user. Triggers
* a transaction to change the propery of all selected entities
* that match the current entity type filter.
*/
PropertyWatcher.prototype.propertyChanged = function(value) {
   // TODO:
   // only do something if the value has actually changed
   // remember edited status of widgets

   var attributes = this.propertyEditor.getPropertyAttributes(this.propertyType);
  
   var typeHint = 0;

   // value is a list property (e.g. x coordinate of a vertex of a polyline):
   if (attributes.isList()) {
       var indexControlObjectName =
           PropertyEditorImpl.getIndexControlObjectName(this.propertyType.getPropertyGroupTitle());
       var indexControl = this.propertyEditor.widget.findChild(indexControlObjectName);
       var index = indexControl.value;
       value = this.sender.getValue();
       if (!isNumber(value)) {
           return;
       }
       this.propertyEditor.listPropertyChanged(this.propertyType, index, value);
       return;
   }

   // value comes from a combo box:
   else if (isComboBox(this.sender)) {

       // value is index of combo box:
       if (isNumber(value)) {
           if (this.sender.itemData(value)===PropertyEditor.varies) {
               return;
           }
           this.propertyEditor.propertyChanged(this.propertyType,
                                               this.sender.itemData(value));
           return;
       }

       if (value===PropertyEditor.varies) {
           return;
       }
   }

   // value is string from a line edit (e.g. text contents):
   else if (this.sender.toString()==="QLineEdit") {
       value = this.sender.text;
       if (value===PropertyEditor.varies) {
           return;
       }
   }

   // value is number from a math line edit:
   else if (this.sender.toString().startsWith("RMathLineEdit")) {
       if (this.sender.text===this.sender.originalText) {
           return;
       }
       this.sender.setProperty("originalText", this.sender.text);
       value = this.sender.getValue();
       if (isNaN(value)) {
           this.propertyEditor.updateGui(true);
           return;
       }
       if (this.sender.isInteger()) {
           typeHint = 2;
       }
   }
   //update entity property value
   this.propertyEditor.propertyChanged(this.propertyType, value, typeHint);
};

void RPropertyEditor::propertyChanged(RPropertyTypeId propertyTypeId,
                                     QVariant propertyValue,
                                     QVariant::Type typeHint) {

   RMainWindow* appWin = RMainWindow::getMainWindow();
   if (appWin == NULL) {
       qWarning() << QString("RPropertyEditor::itemChanged: mainWindow is NULL");
       return;
   }

   if (typeHint!=QVariant::Invalid) {
       // broken for double to int conversion:
       //propertyValue = propertyValue.convert(typeHint);
       if (typeHint==QVariant::Int && propertyValue.type()==QVariant::Double) {
           propertyValue = QVariant(RMath::mround(propertyValue.toDouble()));
       }
   }

   appWin->postPropertyEvent(propertyTypeId, propertyValue, entityTypeFilter);
}
void RPropertyEditor::listPropertyChanged(RPropertyTypeId propertyTypeId,
                    int index, QVariant propertyValue) {

   QVariant v;
   QList<QPair<int, double> > list;
   list.append(QPair<int, double>(index, propertyValue.toDouble()));
   v.setValue(list);

   propertyChanged(propertyTypeId, v);
}

void RMainWindowQt::postPropertyEvent(RPropertyTypeId propertyTypeId, const QVariant& value, RS::EntityType entityTypeFilter) {
   RPropertyEvent* event = new RPropertyEvent(propertyTypeId, value, entityTypeFilter);
   QCoreApplication::postEvent(this, event);
}

注册事件

RMainWindow::installMessageHandler();

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片