Exemplo n.º 1
0
void WorkPlaceWidget::mousePressEvent(QMouseEvent* pe){
    if(this->core->getState()==0){ // Добавление сущности
        //=====================================================
        string name;
        int num = this->core->getCounter();
        char str[255];
        sprintf(str, "Entitie № %d", num);
        name = (const char*)str;
        Entitie* e0 = EntitieFactory::entitieFactory()->createEntitie(this->core->getEntitieType());
        ((IntField*)e0->fieldByID("X"))->setValue(pe->x());
        ((IntField*)e0->fieldByID("Y"))->setValue(pe->y());
        calculateEntitie(e0);
        //=====================================================


        bool add = true;
        for(int i=0; i<this->core->getEntitieCount(); i++){
           Entitie* e = this->core->getEntitieAt(i);
           int x = ((IntField*)e->fieldByID("X"))->getValue();
           int y = ((IntField*)e->fieldByID("Y"))->getValue();
           int w = ((IntField*)e->fieldByID("W"))->getValue();
           int h = ((IntField*)e->fieldByID("H"))->getValue();
           int w0 =((IntField*)e0->fieldByID("W"))->getValue();
           int h0 =((IntField*)e0->fieldByID("H"))->getValue();
           if((x-w0-15<pe->x())&&(y-h0-15<pe->y())&&(w+x+15>pe->x())&&(h+y+15>pe->y())){
               add = false;
           }
        }
        if(add){
            this->core->Changed(true);
            core->addEntitieTo(e0);
            core->setFocus(core->getIndexEntitieByID(e0->getID()));
            this->repaint();
        }else{
            delete(e0);
        }
    }
    if(this->core->getState()==1){ // Добавление связи
        //if(core->getFocusObj())
        int focus = -1;
        for(int i=0; i<this->core->getEntitieCount(); i++){
           Entitie* e = this->core->getEntitieAt(i);
           int x =((IntField*)e->fieldByID("X"))->getValue();
           int y = ((IntField*)e->fieldByID("Y"))->getValue();
           int w = ((IntField*)e->fieldByID("W"))->getValue();
           int h = ((IntField*)e->fieldByID("H"))->getValue();
           if((x<pe->x())&&(y<pe->y())&&(w+x>pe->x())&&(h+y>pe->y())){
               focus = i;
           }
        }
        if(core->getFocus()==-1 || !core->getFocusObj()){
            //core->e= this->core->getEntitieCount(focus);
            core->setFocus(focus);
            core->setFocusObj(true);
        }else{
            if(focus!=this->core->getFocus() && focus!=-1){
                string name;
                int num = this->core->getCounter0();
                char str[255];
                sprintf(str, "Relation %d", num);
                name = (const char*)str;

                Entitie* e1 = this->core->getEntitieAt(this->core->getFocus());
                Entitie* e2 = this->core->getEntitieAt(focus);
                core->addRelation(e1,e2,name,"W");
                this->core->Changed(true);
            }
            this->core->setFocus(core->getRelationCount()-1);
            //core->setFocus(-1);
            core->setFocusObj(false);
        }
        core->spotFocus();
        this->repaint();
    }
    if(this->core->getState()==2 || this->core->getState()==10){ // Выделение объектов
        this->selected = true;
        core->setFocus(-1);
        for(int i=0; i<this->core->getEntitieCount(); i++){
           Entitie* e = this->core->getEntitieAt(i);
           int x =((IntField*)e->fieldByID("X"))->getValue();
           int y = ((IntField*)e->fieldByID("Y"))->getValue();
           int w = ((IntField*)e->fieldByID("W"))->getValue();
           int h = ((IntField*)e->fieldByID("H"))->getValue();
           if((x<pe->x())&&(y<pe->y())&&(w+x>pe->x())&&(h+y>pe->y())){
               if(this->currentMoved==-1){this->currentMoved=i;}
               core->setFocus(i);
               core->setFocusObj(true);
           }
        }
        for(int i=0; i<this->core->getRelationCount(); i++){
           Relation* r = this->core->getRelationAt(i);
           Entitie* eR = r->getEntR();
           Entitie* eL = r->getEntL();

           int indexR = 0;
           int indexL = 0;
           for(int j=0; j<eR->fieldCount() && indexR==0; j++){
               if(eR->fieldAt(j)->getID()==r->getKey()){
                   indexR = j;
               }
           }
           for(int j=0; j<eL->fieldCount() && indexL==0; j++){
               if(eL->fieldAt(j)->getID()==r->getKey()){
                   indexL = j;
               }
           }
           if(indexR<work_count-1){
               indexR=work_count-1;
           }
           if(indexL<work_count-1){
               indexL=work_count-1;
           }
           //this->calculateEntitie(eR);
           //this->calculateEntitie(eL);
           int x1 = ((IntField*)eR->fieldByID("X"))->getValue();
           int y1 = ((IntField*)eR->fieldByID("Y"))->getValue();
           int w1 = ((IntField*)eR->fieldByID("W"))->getValue();
           int h1 = ((IntField*)eR->fieldByID("H"))->getValue();
           int x2 = ((IntField*)eL->fieldByID("X"))->getValue();
           int y2 = ((IntField*)eL->fieldByID("Y"))->getValue();
           int w2 = ((IntField*)eL->fieldByID("W"))->getValue();
           int h2 = ((IntField*)eL->fieldByID("H"))->getValue();

           int x01=0;
           int x02=0;
           int y01=0;
           int y02=0;

           if(qAbs(x1-x2)>w2 && qAbs(x1-x2)>w1){
               if(x1>x2){
                   x01=x1;
                   x02=x2+w2;
               }else{
                   x02=x2-5;
                   x01=x1+w1;
               }
               y01 = y1+(indexR+1-work_count)*24+((indexR+1-(work_count-1))*24-(indexR+1-work_count)*24)/2;
               y02 = y2+(indexL+1-work_count)*24+((indexL+1-(work_count-1))*24-(indexL+1-work_count)*24)/2;
           }else{
               if(y1>y2){
                   y01=y1;
                   y02=y2+h2;
               }else{
                   y02=y2;
                   y01=y1+h1;
               }
               x01=x1+w1/2;
               x02=x2+w2/2;
           }

           int x = pe->x();
           int y = pe->y();

           int x0 = 0;
           int y0 = 0;
           if(x01>x02){
               x0 = x02 + (x01 - x02)/2;
           }else{
               x0 = x01 + (x02 - x01)/2;
           }
           if(y01>y02){
               y0 = y02 + (y01 - y02)/2;
           }else{
               y0 = y01 + (y02 - y01)/2;
           }
           if(x<x0+10 && x>x0-10 && y>y0-10 && y<y0+10){
                core->setFocus(i);
                core->setFocusObj(false);
           }

        }
        core->spotFocus();
        this->repaint();
    }
}
void EntitieCustomeWidget::timerEvent(){
    if(core->getFocus()!=-1){
        if(this->curFocus != core->getFocus() || (core->getFocusObj()!=this->curObj)){
            this->curFocus = core->getFocus();
            this->curObj = core->getFocusObj();
            if(core->getFocusObj()){
                if(core->getFocus()<core->getEntitieCount()){
                    Entitie* e = core->getEntitieAt(core->getFocus());
                    this->ClearWidget();

                    this->entitieName = new QLabel("Название сущности:");
                    this->tb = new QLineEdit();

                    // Label
                    this->tb->setMaxLength(50);
                    this->tb->setText(QString::fromStdString(e->getID()));

                    // Layout
                    this->setLayout(qbl);

                    // Добавление widget'ов
                    this->qbl->addWidget(this->entitieName);
                    this->qbl->addWidget(this->tb);
                    for(int i=0; i<e->fieldCount(); i++){
                        LineOfField* lf = new LineOfField(this, e, e->fieldAt(i),this);
                        this->fildlist->push_back(lf);
                        this->qbl->addWidget(lf);
                    }
                    this->addButton = new QPushButton("Добавить поле");
                    this->qbl->addWidget(this->addButton);
                    this->qbl->addStretch();
                    QObject::connect(this->addButton,SIGNAL(clicked()),this,SLOT(buttonAdd()));
                }
            }else{
                if(core->getFocus()<core->getRelationCount()){
                    Relation* r = core->getRelationAt(core->getFocus());
                    this->ClearWidget();

                    this->entitieName = new QLabel("Название связи:");
                    this->tb = new QLineEdit();
                    // Label
                    this->tb->setMaxLength(50);
                    this->tb->setText(QString::fromStdString(r->getID()));

                    this->qbl->addWidget(this->entitieName);
                    this->qbl->addWidget(this->tb);
                    this->qbl->addWidget(new QLabel("Ключевое поле"));
                    this->key = new QLineEdit("Ключ");
                    this->key->setText(QString::fromStdString(r->getKey()));
                    this->qbl->addWidget(this->key);
                    //==========================================
                    this->ml = new QCheckBox("Множественная связь слева");
                    this->ml->setChecked(r->getMulL());
                    this->qbl->addWidget(ml);
                    //==========================================
                    this->mr = new QCheckBox("Множественная связь справа");
                    this->mr->setChecked(r->getMulR());
                    this->qbl->addWidget(mr);
                    //==========================================
                    this->al = new QCheckBox("Абстрактная связь слева");
                    this->al->setChecked(r->getAbsL());
                    this->qbl->addWidget(al);
                    //==========================================
                    this->ar = new QCheckBox("Абстрактная связь справа");
                    this->ar->setChecked(r->getAbsR());
                    this->qbl->addWidget(ar);
                    //==========================================
                    this->qbl->addStretch();
                    this->setLayout(qbl);
                }
            }
        }
    }else{
        this->curFocus = -1;
        this->curObj = true;
        while(this->layout()->count()>0){
            QLayoutItem* item = this->layout()->itemAt(0);
            this->layout()->removeItem( item );
            this->layout()->removeWidget(item->widget());
            delete item->widget();
            delete item;
            this->layout()->update();
        }
    }
}