diff --git a/block.cpp b/block.cpp index 1d2484a..162c079 100644 --- a/block.cpp +++ b/block.cpp @@ -70,15 +70,15 @@ QPicture Block::drawBlock(QColor color, bool plain) { if (pins[i].type == true) { double rad = abs(dir.x()) / 4.0; qpainter.drawEllipse( - (QPointF(pins[i].point * Settings::final()->gridSize())) + + (QPointF(pins[i].m_point * Settings::final()->gridSize())) + ((dir / 4.0) * 3.0), rad, rad); dir /= 2.0; } if(!plain) { - qpainter.drawLine(pins[i].point * Settings::final()->gridSize(), - (pins[i].point * Settings::final()->gridSize()) + dir); + qpainter.drawLine(pins[i].m_point * Settings::final()->gridSize(), + (pins[i].m_point * Settings::final()->gridSize()) + dir); pen.setColor(Qt::black); pen.setStyle(Qt::DotLine); pen.setWidth(Settings::final()->penWidth()*Settings::final()->gridSize()); @@ -97,8 +97,8 @@ QPicture Block::drawBlock(QColor color, bool plain) { } } qpainter.setPen(pen); - qpainter.drawLine(pins[i].point * Settings::final()->gridSize(), - (pins[i].point * Settings::final()->gridSize()) + dir); + qpainter.drawLine(pins[i].m_point * Settings::final()->gridSize(), + (pins[i].m_point * Settings::final()->gridSize()) + dir); // qDebug()<<(pins[i].point*10)<pins.append(pin); diff --git a/block.h b/block.h index eca0e3e..9e3a053 100644 --- a/block.h +++ b/block.h @@ -4,6 +4,7 @@ #include #include #include +#include extern "C" { #include "lua.h" #include "lauxlib.h" @@ -11,18 +12,9 @@ extern "C" { } #include #include -struct pin_t { - QPoint point; - int direction; - int state; - bool type; - QColor color; - pin_t() - { - state=false; - color=Qt::black; - } -}; +class Block; +class block_t; +class pin_t; class Block : public QObject { @@ -63,5 +55,47 @@ QFileSystemWatcher watcher; public slots: void fileChanged(const QString &path); }; +class block_t { +public: + Block* block; + QColor color; + block_t() + { + color=Qt::black; + } + QPoint m_pos; + QRectF rect() + { + QRectF rect(m_pos.x()+0.5,m_pos.y(),block->width,block->height+1.0); + return rect; + } + QRectF pinsRect() + { + QRectF rect(m_pos.x(),m_pos.y(),block->width+1,block->height+1.0); + return rect; + } + QPointF unmap(QPointF p) + { + return p-m_pos; + } +}; +class pin_t { +public: + QPoint pos(){ + return parent->m_pos+m_point; + } + + int direction; + int state; + bool type; + QColor color; + block_t* parent; + pin_t() + { + state=false; + color=Qt::black; + } + QPoint m_point; +}; #endif // BLOCK_H diff --git a/digiview.cpp b/digiview.cpp index 9251433..4e022f9 100644 --- a/digiview.cpp +++ b/digiview.cpp @@ -129,9 +129,11 @@ void DigiView::paintEvent(QPaintEvent* event) painter.setBrush(brush); for(int i=0;igridSize()).toPoint(),QSize(1,1)); - rect.setWidth((blocks[selectedBlocks[i]].block->width+1)*Settings::final()->gridSize()); - rect.setHeight((blocks[selectedBlocks[i]].block->height)*Settings::final()->gridSize()); + QRectF rect=blocks[selectedBlocks[i]]->rect(); + rect.setTop(rect.top()*(Settings::final()->gridSize())); + rect.setLeft(rect.left()*(Settings::final()->gridSize())); + rect.setBottom(rect.bottom()*Settings::final()->gridSize()); + rect.setRight(rect.right()*Settings::final()->gridSize()); painter.drawRect(rect); } for(int i=0;igridSize(),blocks[i].block->drawBlock(blocks[i].color)); - for(int j=0;jpins.length();j++) - if(blocks[i].block->pins[j].state==2) + painter.drawPicture(blocks[i]->m_pos*Settings::final()->gridSize(),blocks[i]->block->drawBlock(blocks[i]->color)); + for(int j=0;jblock->pins.length();j++) + if(blocks[i]->block->pins[j].state==2) { QPen pen(QColor::fromRgbF(1,0.5,0)); pen.setWidth(0); painter.setPen(pen); for(int r=5;r<(Settings::final()->gridSize());r+=5) - painter.drawEllipse(((blocks[i].pos+blocks[i].block->pins[j].point) * Settings::final()->gridSize()),r,r); + painter.drawEllipse((blocks[i]->block->pins[j].pos() * Settings::final()->gridSize()),r,r); } } QPen line(Qt::black); @@ -275,13 +277,15 @@ void DigiView::dropEvent(QDropEvent *event) { clearSelection(); dragPos=toGrid(event->pos())-QPoint(1,1); - block_t blk; - blk.block=BlockList::newBlock(dragGate); - blk.pos=dragPos.toPoint(); + block_t* blk=new block_t; + blk->block=BlockList::newBlock(dragGate); + for(int i=0;iblock->pins.length();i++) + blk->block->pins[i].parent=blk; + blk->m_pos=dragPos.toPoint(); if(dragGate=="Large-IN") { for(int i=0;iname=="Large-IN") + if(blocks[i]->block->name=="Large-IN") { dragGate=""; return; @@ -289,7 +293,7 @@ void DigiView::dropEvent(QDropEvent *event) } dragGate=""; for(int i=0;iwidth,blocks[i].block->height).intersects(QRect(blk.pos.x(),blk.pos.y(),blk.block->width,blk.block->height))) + if(QRectF(blocks[i]->rect()).intersects(blk->rect())) return; blocks.append(blk); emit changed(); @@ -308,17 +312,14 @@ void DigiView::mousePressEvent(QMouseEvent *event) curPoint=startPoint; int idx=-1; for(int i=0;iheight)>=curPoint.y()) - if((blocks[i].pos.x()+blocks[i].block->width)>=curPoint.x()) - { - idx=i; - } + if(blocks[i]->rect().contains(curPoint)) + { + idx=i; + } int pin=-1; if(idx>=0) - for(int i=0;ipins.length();i++) - if(startPoint==(blocks[idx].pos+blocks[idx].block->pins[i].point)) + for(int i=0;iblock->pins.length();i++) + if(startPoint==blocks[idx]->block->pins[i].pos()) pin=i; drag=false; blkIdx=idx; @@ -326,7 +327,7 @@ void DigiView::mousePressEvent(QMouseEvent *event) if((pin==-1)&&(idx>=0)) { clear=false; - startBlock=blocks[idx].pos; + startBlock=blocks[idx]->m_pos; drag=true; } if(idx>=0) @@ -334,7 +335,7 @@ void DigiView::mousePressEvent(QMouseEvent *event) double x=event->pos().x()/Settings::final()->gridSize(); double y=event->pos().y()/Settings::final()->gridSize(); QPointF p(x,y); - blocks[idx].block->onpress(p-QPointF(blocks[idx].pos)); + blocks[idx]->block->onpress(blocks[idx]->unmap(p)); } if(clear) clearSelection(); @@ -368,10 +369,10 @@ void DigiView::mouseMoveEvent(QMouseEvent *event) dragged=true; if(blkIdx>=0) { - blocks[blkIdx].pos+=curPoint-startPoint; + blocks[blkIdx]->m_pos+=curPoint-startPoint; for(int i=0;im_pos+=curPoint-startPoint; for(int i=0;iwidth+1,blocks[i].block->height+1)); - if(sel.intersects(blk)) + if(sel.intersects(blocks[i]->rect())) selectedBlocks.append(i); } @@ -441,13 +441,10 @@ void DigiView::mouseReleaseEvent(QMouseEvent *event) { int idx=-1; for(int i=0;iheight)>=curPoint.y()) - if((blocks[i].pos.x()+blocks[i].block->width)>=curPoint.x()) - { - idx=i; - } + if(blocks[i]->rect().contains(curPoint)) + { + idx=i; + } if(idx>=0) { double x=event->pos().x()/Settings::final()->gridSize(); @@ -455,8 +452,8 @@ void DigiView::mouseReleaseEvent(QMouseEvent *event) QPointF p(x,y); if(!dragged) { - blocks[idx].block->onclick(p-QPointF(blocks[idx].pos)); - blocks[idx].block->onrelease(p-QPointF(blocks[idx].pos)); + blocks[idx]->block->onclick(blocks[idx]->unmap(p)); + blocks[idx]->block->onrelease(blocks[idx]->unmap(p)); } } } @@ -510,7 +507,7 @@ void DigiView::load(QString where) { lines.clear(); for(int i=0;ideleteLater(); + blocks[i]->block->deleteLater(); blocks.clear(); vias.clear(); QFile file(where); @@ -584,9 +581,9 @@ void DigiView::timeout() for(int i=0;i()); - for(int j=0;jpins.length();j++) + for(int j=0;jblock->pins.length();j++) { - if(blocks[i].block->pins[j].direction==2) + if(blocks[i]->block->pins[j].direction==2) done[i].append(true); else done[i].append(false); @@ -594,14 +591,14 @@ void DigiView::timeout() } QMap,QPair > states; for(int i=0;ipins.length();j++) - if(blocks[i].block->pins[j].direction==2) + for(int j=0;jblock->pins.length();j++) + if(blocks[i]->block->pins[j].direction==2) { QList > points; - points.append(QPair (blocks[i].block->pins[j].point+blocks[i].pos,Qt::black)); - QPoint sp=blocks[i].block->pins[j].point+blocks[i].pos; - bool state=blocks[i].block->getState(j); - if(blocks[i].block->pins[j].type) + points.append(QPair (blocks[i]->block->pins[j].pos(),Qt::black)); + QPoint sp=blocks[i]->block->pins[j].pos(); + bool state=blocks[i]->block->getState(j); + if(blocks[i]->block->pins[j].type) state=!state; QList linesVisited; while(points.length()>0) @@ -618,7 +615,7 @@ void DigiView::timeout() points.append(QPair(lines[k].line.p1(),lines[k].color)); points.append(QPair(lines[k].line.p2(),lines[k].color)); if((lines[k].line.p1()==sp)||(lines[k].line.p2()==sp)) - blocks[i].block->pins[j].color=lines[k].color; + blocks[i]->block->pins[j].color=lines[k].color; QPoint dp=lines[k].line.p2(); if(lines[k].line.p1()==p) dp=lines[k].line.p1(); @@ -631,15 +628,15 @@ void DigiView::timeout() } } for(int k=0;kpins.length();l++) - if((blocks[k].block->pins[l].point+blocks[k].pos+QPoint(0,0))==p) + for(int l=0;lblock->pins.length();l++) + if((blocks[k]->block->pins[l].pos()+QPoint(0,0))==p) { - if(blocks[k].block->pins[l].direction==0) + if(blocks[k]->block->pins[l].direction==0) { done[k][l]=true; states.insert(QPair(k,l),QPair(state,pp.second)); } - if(blocks[k].block->pins[l].direction==2) + if(blocks[k]->block->pins[l].direction==2) { if(!((i==k)&&(j==l))) { @@ -671,20 +668,20 @@ void DigiView::timeout() int l=keys[i].second; bool state=states[keys[i]].first; QColor col=states[keys[i]].second; - blocks[k].block->pins[l].state=state; - blocks[k].block->pins[l].color=col; + blocks[k]->block->pins[l].state=state; + blocks[k]->block->pins[l].color=col; } if(!ok) { error=true; /*for(int i=0;ipins.length();j++) - if(blocks[i].block->pins[j].direction==0) - blocks[i].block->pins[j].state=false; + for(int j=0;jblock->pins.length();j++) + if(blocks[i]->block->pins[j].direction==0) + blocks[i]->block->pins[j].state=false; for(int i=0;ipins[offen[i].second].state=2; + blocks[offen[i].first]->block->pins[offen[i].second].state=2; } update(); } @@ -693,30 +690,33 @@ void DigiView::contextMenuEvent(QContextMenuEvent *event) { event->accept(); int block=-1; + int pblock=-1; bool ok=false; QMenu menu; QPoint p=toGrid(event->pos()); QPointF pf=QPointF(event->pos())/Settings::final()->gridSize(); for(int i=0;iwidth)>=(pf.x()-1))) - if((blocks[i].pos.y()<=(pf.y()-0.5))&&((blocks[i].pos.y()+blocks[i].block->height)>=(pf.y()-0.5))) - block=i; + if(QRectF(blocks[i]->rect()).contains(pf)) + block=i; + for(int i=0;ipinsRect()).contains(pf)) + pblock=i; int pin=-1; QStringList alt; - if(block>=0) + if(pblock>=0) { - for(int i=0;ipins.length();i++) + for(int i=0;iblock->pins.length();i++) { - if(blocks[block].block->pins[i].point+blocks[block].pos==p) + if(blocks[pblock]->block->pins[i].pos()==p) pin=i; - if(blocks[block].block->pins[i].direction==0) - if(blocks[block].block->pins[i].point+blocks[block].pos+QPoint(1,0)==p) + if(blocks[pblock]->block->pins[i].direction==0) + if(blocks[pblock]->block->pins[i].pos()+QPoint(1,0)==p) pin=i; - if(blocks[block].block->pins[i].direction==2) - if(blocks[block].block->pins[i].point+blocks[block].pos+QPoint(-1,0)==p) + if(blocks[pblock]->block->pins[i].direction==2) + if(blocks[pblock]->block->pins[i].pos()+QPoint(-1,0)==p) pin=i; } - alt=blocks[block].block->alt; + alt=blocks[pblock]->block->alt; } int line=-1; double dist=1.1; @@ -810,10 +810,10 @@ void DigiView::contextMenuEvent(QContextMenuEvent *event) if(block>=0) { int cnt=0; - QList keys=blocks[block].block->contextMenu.keys(); + QList keys=blocks[block]->block->contextMenu.keys(); for(auto key:keys) { - int val=blocks[block].block->contextMenu[key]; + int val=blocks[block]->block->contextMenu[key]; QAction* act=menu.addAction(key); blockDefAct.insert(act,val); cnt++; @@ -919,11 +919,11 @@ void DigiView::contextMenuEvent(QContextMenuEvent *event) if(blockDefAct.contains(act)) { int func=blockDefAct[act]; - blocks[block].block->execContext(func); + blocks[block]->block->execContext(func); } if(act==delBlockAct) { - blocks[block].block->deleteLater(); + blocks[block]->block->deleteLater(); blocks.removeAt(block); emit changed(); clearSelection(); @@ -931,8 +931,11 @@ void DigiView::contextMenuEvent(QContextMenuEvent *event) for(int i=0;ideleteLater(); - blocks[block].block=BlockList::newBlock(alt[i]); + blocks[block]->block->deleteLater(); + blocks[block]->block=BlockList::newBlock(alt[i]); + block_t* blk=blocks[block]; + for(int i=0;iblock->pins.length();i++) + blk->block->pins[i].parent=blk; emit changed(); clearSelection(); } @@ -968,7 +971,7 @@ void DigiView::contextMenuEvent(QContextMenuEvent *event) if(pin>=0) if(act==changePinAct) { - blocks[block].block->pins[pin].type=!blocks[block].block->pins[pin].type; + blocks[pblock]->block->pins[pin].type=!blocks[pblock]->block->pins[pin].type; emit changed(); clearSelection(); } @@ -1018,7 +1021,7 @@ void DigiView::contextMenuEvent(QContextMenuEvent *event) if(setBlockColorAction.contains(act)) { QColor c=setBlockColorAction[act]; - blocks[block].color=c; + blocks[block]->color=c; emit changed(); clearSelection(); } @@ -1257,8 +1260,8 @@ void DigiView::cleanUp() testVias.append(lines[i].line.p2()); } for(int j=0;jpins.length();k++) - testVias.append(blocks[j].block->pins[k].point+blocks[j].pos); + for(int k=0;kblock->pins.length();k++) + testVias.append(blocks[j]->block->pins[k].pos()); testVias.append(vias); vias.clear(); for(int i=0;ipins.length();k++) - if((blocks[j].block->pins[k].point+blocks[j].pos)==point) + for(int k=0;kblock->pins.length();k++) + if((blocks[j]->block->pins[k].pos())==point) { num++; cnt++; @@ -1335,9 +1338,9 @@ QList DigiView::allIntersect(QLine line) ret.append(lines[i].line.p2()); } for(int i=0;ipins.length();j++) - if(onLine(line,blocks[i].block->pins[j].point+blocks[i].pos)) - ret.append(blocks[i].block->pins[j].point+blocks[i].pos); + for(int j=0;jblock->pins.length();j++) + if(onLine(line,blocks[i]->block->pins[j].pos())) + ret.append(blocks[i]->block->pins[j].pos()); return ret; } @@ -1398,7 +1401,7 @@ QPicture DigiView::exportPicture() QPicture picture; QPainter painter(&picture); for(int i=0;igridSize(),blocks[i].block->drawBlock(blocks[i].color,true)); + painter.drawPicture(blocks[i]->m_pos*Settings::final()->gridSize(),blocks[i]->block->drawBlock(blocks[i]->color,true)); QPen line(Qt::black); line.setWidth(Settings::final()->penWidth()*Settings::final()->gridSize()); for(int i=0;im_pos=QPoint(g[i].toObject()["x"].toInt(),g[i].toObject()["y"].toInt()); + c->block=0; BlockList list; for(int j=0;jname==g[i].toObject()["name"].toString()) { - c.block=list.blocks[j]->clone(); + c->block=list.blocks[j]->clone(); } QJsonArray pins=g[i].toObject()["pins"].toArray(); for(int j=0;jpins.length()>j) - c.block->pins[j].type=pin["type"].toBool(); + if(c->block->pins.length()>j) + c->block->pins[j].type=pin["type"].toBool(); } - c.color=QColor(g[i].toObject()["color"].toString()); - if(c.block!=0) + c->color=QColor(g[i].toObject()["color"].toString()); + if(c->block!=0) + { + for(int i=0;iblock->pins.length();i++) + c->block->pins[i].parent=c; blocks.append(c); + } } for(int i=0;iname); + c.insert("x",blocks[i]->m_pos.x()); + c.insert("y",blocks[i]->m_pos.y()); + c.insert("name",blocks[i]->block->name); g.append(c); } cleanUp(); @@ -1488,7 +1495,7 @@ void DigiView::clearSelection() void DigiView::deleteSelection() { for(int i=0;ideleteLater(); + blocks[selectedBlocks[i]]->block->deleteLater(); for(int i=0;iname=="Large-IN") + if(blocks[i]->block->name=="Large-IN") { - pos=blocks[i].pos; - lin=blocks[i].block; + pos=blocks[i]->m_pos; + lin=blocks[i]->block; i=blocks.length(); } } @@ -1603,7 +1610,7 @@ void DigiView::createTable() QMap fakeGates; QList outputs; for(int i=0;iname=="OUT") + if(blocks[i]->block->name=="OUT") outputs.append(i); while(cnt<16) { @@ -1639,7 +1646,7 @@ void DigiView::createTable() timeout(); QList res; for(int j=0;jpins[0].state); + res.append(blocks[outputs[j]]->block->pins[0].state); row.second=res; table.append(row); } @@ -1755,9 +1762,9 @@ void DigiView::keyPressEvent(QKeyEvent *event) void DigiView::largeIn(int o) { for(int i=0;iname=="Large-IN") + if(blocks[i]->block->name=="Large-IN") { - blocks[i].block->keyPress(o); + blocks[i]->block->keyPress(o); } } @@ -1791,15 +1798,15 @@ QJsonObject DigiView::exportJSON() for(int i=0;iname); - c.insert("color",blocks[i].color.name()); + c.insert("x",blocks[i]->m_pos.x()); + c.insert("y",blocks[i]->m_pos.y()); + c.insert("name",blocks[i]->block->name); + c.insert("color",blocks[i]->color.name()); QJsonArray pins; - for(int j=0;jpins.length();j++) + for(int j=0;jblock->pins.length();j++) { QJsonObject obj; - obj.insert("type",blocks[i].block->pins[j].type); + obj.insert("type",blocks[i]->block->pins[j].type); pins.append(obj); } c.insert("pins",pins); diff --git a/digiview.h b/digiview.h index edbe393..6dab4af 100644 --- a/digiview.h +++ b/digiview.h @@ -7,15 +7,7 @@ #include #include -struct block_t { - QPoint pos; - Block* block; - QColor color; - block_t() - { - color=Qt::black; - } -}; + struct text_t { QPoint pos; @@ -63,7 +55,7 @@ class DigiView : public QWidget QList vias; QPointF dragPos; QString dragGate; - QList blocks; + QList blocks; void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);