code format

This commit is contained in:
2022-12-14 14:14:33 +03:00
parent 09e5342956
commit cdb02fc9be
278 changed files with 15371 additions and 12176 deletions

View File

@@ -1,8 +1,10 @@
#include "alignedtextitem.h"
#include "blockbase.h"
#include <QApplication>
#include <QPainter>
#include <QDebug>
#include <QPainter>
AlignedTextItem::AlignedTextItem(QGraphicsItem * parent): QGraphicsItem(parent), text_(this) {
@@ -28,7 +30,7 @@ void AlignedTextItem::setFont(const QFont & f) {
QFont AlignedTextItem::sceneFont(const QFont & f) {
QFont ret = f;
QFont ret = f;
double scl = 16. / fontHeight();
ret.setPointSizeF(ret.pointSizeF() * scl);
return ret;

View File

@@ -1,67 +1,81 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ALIGNEDTEXTITEM_H
#define ALIGNEDTEXTITEM_H
#include <QGraphicsSimpleTextItem>
#include <QPen>
#include "qad_blockview_export.h"
#include <QBrush>
#include <QFont>
#include "qad_blockview_export.h"
#include <QGraphicsSimpleTextItem>
#include <QPen>
class QAD_BLOCKVIEW_EXPORT AlignedTextItem: public QGraphicsItem {
public:
AlignedTextItem(QGraphicsItem * parent = 0);
AlignedTextItem(const QString & text, QGraphicsItem * parent = 0);
void setText(const QString & t) {text_.setText(t); _move();}
void setText(const QString & t) {
text_.setText(t);
_move();
}
void setFont(const QFont & f);
void setPen(const QPen & p) {text_.setPen(p); _move();}
void setBrush(const QBrush & b) {text_.setBrush(b); _move();}
void setAlignment(Qt::Alignment align) {align_ = align; _move();}
QString text() const {return text_.text();}
QFont font() const {return font_;}
QPen pen() const {return text_.pen();}
QBrush brush() const {return text_.brush();}
Qt::Alignment alignment() const {return align_;}
void clear() {setText(QString());}
enum {Type = UserType + 0x100};
void setPen(const QPen & p) {
text_.setPen(p);
_move();
}
void setBrush(const QBrush & b) {
text_.setBrush(b);
_move();
}
void setAlignment(Qt::Alignment align) {
align_ = align;
_move();
}
QString text() const { return text_.text(); }
QFont font() const { return font_; }
QPen pen() const { return text_.pen(); }
QBrush brush() const { return text_.brush(); }
Qt::Alignment alignment() const { return align_; }
void clear() { setText(QString()); }
enum {
Type = UserType + 0x100
};
static QFont sceneFont(const QFont & f);
protected:
virtual QRectF boundingRect() const {return text_.boundingRect().translated(text_.pos());}
virtual QRectF boundingRect() const { return text_.boundingRect().translated(text_.pos()); }
virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
virtual int type() const {return Type;}
void _move() {text_.setPos(-_point(align_));}
virtual int type() const { return Type; }
void _move() { text_.setPos(-_point(align_)); }
QPointF _point(Qt::Alignment a) const;
QGraphicsSimpleTextItem text_;
Qt::Alignment align_;
QFont font_;
};

View File

@@ -1,20 +1,21 @@
#include "blockbase.h"
#include "alignedtextitem.h"
#include "qvariantedit.h"
QDataStream & operator <<(QDataStream & s, const QGraphicsItem * item) {
QDataStream & operator<<(QDataStream & s, const QGraphicsItem * item) {
if (!item) {
s << int(-1);
return s;
}
const QGraphicsRectItem * irect = qgraphicsitem_cast<const QGraphicsRectItem*>(item);
const QGraphicsEllipseItem * iell = qgraphicsitem_cast<const QGraphicsEllipseItem*>(item);
const QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<const QGraphicsSimpleTextItem*>(item);
const AlignedTextItem * iatext = qgraphicsitem_cast<const AlignedTextItem*>(item);
const QGraphicsLineItem * iline = qgraphicsitem_cast<const QGraphicsLineItem*>(item);
const QGraphicsPathItem * ipath = qgraphicsitem_cast<const QGraphicsPathItem*>(item);
const QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<const QGraphicsPixmapItem*>(item);
const QGraphicsRectItem * irect = qgraphicsitem_cast<const QGraphicsRectItem *>(item);
const QGraphicsEllipseItem * iell = qgraphicsitem_cast<const QGraphicsEllipseItem *>(item);
const QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<const QGraphicsSimpleTextItem *>(item);
const AlignedTextItem * iatext = qgraphicsitem_cast<const AlignedTextItem *>(item);
const QGraphicsLineItem * iline = qgraphicsitem_cast<const QGraphicsLineItem *>(item);
const QGraphicsPathItem * ipath = qgraphicsitem_cast<const QGraphicsPathItem *>(item);
const QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<const QGraphicsPixmapItem *>(item);
if (irect) {
s << int(0) << (irect->pen()) << (irect->brush()) << (irect->rect());
} else if (iell) {
@@ -38,72 +39,181 @@ QDataStream & operator <<(QDataStream & s, const QGraphicsItem * item) {
}
QDataStream & operator >>(QDataStream & s, QGraphicsItem *& item) {
int type_; s >> type_;
QDataStream & operator>>(QDataStream & s, QGraphicsItem *& item) {
int type_;
s >> type_;
if (type_ < 0) {
item = 0;
return s;
}
QGraphicsRectItem * nrect = 0;
QGraphicsEllipseItem * nell = 0;
QGraphicsRectItem * nrect = 0;
QGraphicsEllipseItem * nell = 0;
QGraphicsSimpleTextItem * ntext = 0;
AlignedTextItem * natext = 0;
QGraphicsLineItem * nline = 0;
QGraphicsPathItem * npath = 0;
QGraphicsPixmapItem * npixmap = 0;
item = 0;
AlignedTextItem * natext = 0;
QGraphicsLineItem * nline = 0;
QGraphicsPathItem * npath = 0;
QGraphicsPixmapItem * npixmap = 0;
item = 0;
switch (type_) {
case 0:
nrect = new QGraphicsRectItem(); item = nrect;
{QPen _v; s >> _v; nrect->setPen(_v);}
{QBrush _v; s >> _v; nrect->setBrush(_v);}
{QRectF _v; s >> _v; nrect->setRect(_v);}
nrect = new QGraphicsRectItem();
item = nrect;
{
QPen _v;
s >> _v;
nrect->setPen(_v);
}
{
QBrush _v;
s >> _v;
nrect->setBrush(_v);
}
{
QRectF _v;
s >> _v;
nrect->setRect(_v);
}
break;
case 1:
nell = new QGraphicsEllipseItem(); item = nell;
{QPen _v; s >> _v; nell->setPen(_v);}
{QBrush _v; s >> _v; nell->setBrush(_v);}
{QRectF _v; s >> _v; nell->setRect(_v);}
nell = new QGraphicsEllipseItem();
item = nell;
{
QPen _v;
s >> _v;
nell->setPen(_v);
}
{
QBrush _v;
s >> _v;
nell->setBrush(_v);
}
{
QRectF _v;
s >> _v;
nell->setRect(_v);
}
break;
case 2:
ntext = new QGraphicsSimpleTextItem(); item = ntext;
{QPen _v; s >> _v; ntext->setPen(_v);}
{QBrush _v; s >> _v; ntext->setBrush(_v);}
{QFont _v; s >> _v; ntext->setFont(_v);}
{QString _v; s >> _v; ntext->setText(_v);}
ntext = new QGraphicsSimpleTextItem();
item = ntext;
{
QPen _v;
s >> _v;
ntext->setPen(_v);
}
{
QBrush _v;
s >> _v;
ntext->setBrush(_v);
}
{
QFont _v;
s >> _v;
ntext->setFont(_v);
}
{
QString _v;
s >> _v;
ntext->setText(_v);
}
break;
case 6:
natext = new AlignedTextItem(); item = natext;
{QPen _v; s >> _v; natext->setPen(_v);}
{QBrush _v; s >> _v; natext->setBrush(_v);}
{QFont _v; s >> _v; natext->setFont(_v);}
{QString _v; s >> _v; natext->setText(_v);}
{int _v; s >> _v; natext->setAlignment((Qt::AlignmentFlag)_v);}
natext = new AlignedTextItem();
item = natext;
{
QPen _v;
s >> _v;
natext->setPen(_v);
}
{
QBrush _v;
s >> _v;
natext->setBrush(_v);
}
{
QFont _v;
s >> _v;
natext->setFont(_v);
}
{
QString _v;
s >> _v;
natext->setText(_v);
}
{
int _v;
s >> _v;
natext->setAlignment((Qt::AlignmentFlag)_v);
}
break;
case 3:
nline = new QGraphicsLineItem(); item = nline;
{QPen _v; s >> _v; nline->setPen(_v);}
{QLineF _v; s >> _v; nline->setLine(_v);}
nline = new QGraphicsLineItem();
item = nline;
{
QPen _v;
s >> _v;
nline->setPen(_v);
}
{
QLineF _v;
s >> _v;
nline->setLine(_v);
}
break;
case 4:
npath = new QGraphicsPathItem(); item = npath;
{QPen _v; s >> _v; npath->setPen(_v);}
{QPainterPath _v; s >> _v; npath->setPath(_v);}
npath = new QGraphicsPathItem();
item = npath;
{
QPen _v;
s >> _v;
npath->setPen(_v);
}
{
QPainterPath _v;
s >> _v;
npath->setPath(_v);
}
break;
case 5:
npixmap = new QGraphicsPixmapItem(); item = npixmap;
{QPixmap _v; s >> _v; npixmap->setPixmap(_v);}
npixmap = new QGraphicsPixmapItem();
item = npixmap;
{
QPixmap _v;
s >> _v;
npixmap->setPixmap(_v);
}
break;
case 7:
npixmap = new QGraphicsPixmapItem(); item = npixmap;
{QPixmap _v; s >> _v; npixmap->setPixmap(_v);}
{QTransform _t; s >> _t; npixmap->setTransform(_t);}
npixmap = new QGraphicsPixmapItem();
item = npixmap;
{
QPixmap _v;
s >> _v;
npixmap->setPixmap(_v);
}
{
QTransform _t;
s >> _t;
npixmap->setTransform(_t);
}
break;
}
if (item) {
{QPointF _v; s >> _v; item->setPos(_v);}
{qreal _v; s >> _v; item->setRotation(_v);}
{int _v; s >> _v; item->setFlags((QGraphicsItem::GraphicsItemFlags)_v);}
{
QPointF _v;
s >> _v;
item->setPos(_v);
}
{
qreal _v;
s >> _v;
item->setRotation(_v);
}
{
int _v;
s >> _v;
item->setFlags((QGraphicsItem::GraphicsItemFlags)_v);
}
}
return s;
}

View File

@@ -1,52 +1,53 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLOCKBASE_H
#define BLOCKBASE_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include "qad_blockview_export.h"
#include "qad_types.h"
#include <QGraphicsEllipseItem>
#include <QGraphicsItem>
#include <QGraphicsObject>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QStack>
#include "qad_types.h"
#include "qad_blockview_export.h"
static const int _blockitem_current_version_ = 1;
enum BlockviewItemData {
bvidSelected = 1000, // bool
bvidType , // BlockviewItemType
bvidItemPos , // QPointF
bvidDecorText , // QString src text for QGraphicsSimpleTextItem
bvidMoveParent , // bool flag for move parent
bvidVisualizeSelection , // bool flag for visualize selection
bvidItemSelection , // bool BlockItem selection
bvidTmpItem , // bool item is temporary, ignore
bvidBlockDecor , // bool item is BlockItem decor
bvidDTHandle , // bool
bvidCorrectMove , // bool
bvidSelected = 1000, // bool
bvidType, // BlockviewItemType
bvidItemPos, // QPointF
bvidDecorText, // QString src text for QGraphicsSimpleTextItem
bvidMoveParent, // bool flag for move parent
bvidVisualizeSelection, // bool flag for visualize selection
bvidItemSelection, // bool BlockItem selection
bvidTmpItem, // bool item is temporary, ignore
bvidBlockDecor, // bool item is BlockItem decor
bvidDTHandle, // bool
bvidCorrectMove, // bool
};
enum BlockviewItemType {
@@ -57,16 +58,15 @@ enum BlockviewItemType {
bvitDecor,
};
QAD_BLOCKVIEW_EXPORT QDataStream & operator <<(QDataStream & s, const QGraphicsItem * item);
QAD_BLOCKVIEW_EXPORT QDataStream & operator >>(QDataStream & s, QGraphicsItem *& item);
QAD_BLOCKVIEW_EXPORT QDataStream & operator<<(QDataStream & s, const QGraphicsItem * item);
QAD_BLOCKVIEW_EXPORT QDataStream & operator>>(QDataStream & s, QGraphicsItem *& item);
class QAD_BLOCKVIEW_EXPORT BlockItemBase: public QObject
{
class QAD_BLOCKVIEW_EXPORT BlockItemBase: public QObject {
Q_OBJECT
Q_ENUMS(Action)
public:
enum Action {
BlockAdd = 1,
BlockMove,
@@ -82,7 +82,6 @@ public:
BusSegmentRemove,
Paste
};
};
#endif // BLOCKBASE_H

View File

@@ -1,12 +1,15 @@
#include "blockview.h"
#include <QApplication>
BlockBusItem::BlockBusItem(bool temp): QGraphicsObject(), PropertyStorage() {
temp_ = temp;
_init();
if (!temp) setData(bvidType, bvitBus);
else hide();
if (!temp)
setData(bvidType, bvitBus);
else
hide();
}
@@ -17,12 +20,12 @@ BlockBusItem::BlockBusItem(const BlockBusItem & other): QGraphicsObject(), Prope
setPen(other.pen());
setBrush(other.brush());
setBusType(other.busType());
square_node = other.square_node;
max_ep = other.max_ep;
pol = other.pol;
segments = other.segments;
im_bus = other.im_bus;
im_end = other.im_end;
square_node = other.square_node;
max_ep = other.max_ep;
pol = other.pol;
segments = other.segments;
im_bus = other.im_bus;
im_end = other.im_end;
im_bus_scale = other.im_bus_scale;
im_end_scale = other.im_end_scale;
updateGeometry();
@@ -33,26 +36,34 @@ void BlockBusItem::_init() {
setZValue(1.);
setBusType(-1);
setAcceptHoverEvents(true);
ph.setColor(Qt::blue); ph.setJoinStyle(Qt::MiterJoin);
bh.setColor(Qt::blue); bh.setStyle(Qt::SolidPattern);
pu = pa = pr = ph; bu = ba = br = bh;
grid_step = 10.;
ph.setColor(Qt::blue);
ph.setJoinStyle(Qt::MiterJoin);
bh.setColor(Qt::blue);
bh.setStyle(Qt::SolidPattern);
pu = pa = pr = ph;
bu = ba = br = bh;
grid_step = 10.;
pu.setWidth(1);
pu.setColor(Qt::black); bu.setColor(Qt::black);
pa.setColor(Qt::darkGreen); ba.setColor(Qt::darkGreen);
pr.setColor(Qt::darkRed); br.setColor(Qt::darkRed);
pn.setColor(Qt::gray); pn.setStyle(Qt::DashLine);
pu.setColor(Qt::black);
bu.setColor(Qt::black);
pa.setColor(Qt::darkGreen);
ba.setColor(Qt::darkGreen);
pr.setColor(Qt::darkRed);
br.setColor(Qt::darkRed);
pn.setColor(Qt::gray);
pn.setStyle(Qt::DashLine);
if (temp_) {
pu.setStyle(Qt::DashLine);
pu.setColor(Qt::darkGray);
bu.setColor(Qt::darkGray);
}
setPen(pu); setBrush(bu);
setPen(pu);
setBrush(bu);
square_node = false;
max_ep = 0;
max_ep = 0;
selPoint = selSegment = state_ = -1;
pen_width = 2.;
point_size = 3.;
pen_width = 2.;
point_size = 3.;
im_bus_scale = im_end_scale = 1.;
moved = deleted = mark_in = mark_out = new_segment = mm_cancel = lm_point = false;
anim_point_size.setTargetObject(this);
@@ -68,19 +79,19 @@ void BlockBusItem::reconnect() {
if (temp_) return;
if (!scene()) return;
if (scene()->views().isEmpty()) return;
qobject_cast<BlockView*>(scene()->views().back())->reconnectAll();
qobject_cast<BlockView *>(scene()->views().back())->reconnectAll();
}
bool BlockBusItem::sceneEvent(QEvent * e) {
if (temp_) return QGraphicsObject::sceneEvent(e);
switch (e->type()) {
case QEvent::GraphicsSceneHoverEnter: hoverEnterEvent((QGraphicsSceneHoverEvent * )e); break;
case QEvent::GraphicsSceneHoverMove: hoverMoveEvent((QGraphicsSceneHoverEvent * )e); break;
case QEvent::GraphicsSceneHoverLeave: hoverLeaveEvent((QGraphicsSceneHoverEvent * )e); break;
case QEvent::GraphicsSceneMousePress: mousePressEvent((QGraphicsSceneMouseEvent * )e); break;
case QEvent::GraphicsSceneMouseMove: mouseMoveEvent((QGraphicsSceneMouseEvent * )e); break;
case QEvent::GraphicsSceneMouseRelease: mouseReleaseEvent((QGraphicsSceneMouseEvent * )e); break;
case QEvent::GraphicsSceneHoverEnter: hoverEnterEvent((QGraphicsSceneHoverEvent *)e); break;
case QEvent::GraphicsSceneHoverMove: hoverMoveEvent((QGraphicsSceneHoverEvent *)e); break;
case QEvent::GraphicsSceneHoverLeave: hoverLeaveEvent((QGraphicsSceneHoverEvent *)e); break;
case QEvent::GraphicsSceneMousePress: mousePressEvent((QGraphicsSceneMouseEvent *)e); break;
case QEvent::GraphicsSceneMouseMove: mouseMoveEvent((QGraphicsSceneMouseEvent *)e); break;
case QEvent::GraphicsSceneMouseRelease: mouseReleaseEvent((QGraphicsSceneMouseEvent *)e); break;
default: break;
}
return QGraphicsObject::sceneEvent(e);
@@ -95,7 +106,7 @@ int BlockBusItem::addPoint(const QPointF & point, bool update) {
selPoint = pol.size() - 1;
segments << QPair<int, int>(selPoint, segments[selSegment].second);
segments[selSegment].second = selPoint;
selSegment = -1;
selSegment = -1;
updateGeometry();
if (scene() != 0 && update) scene()->update();
return pol.size() - 1;
@@ -122,32 +133,33 @@ void BlockBusItem::removePoint(int index) {
if (index < 0 || index > pol.size() - 1) return;
int sc = 0, fs = -1, ss = -1;
for (int i = 0; i < segments.size(); ++i)
if (segments[i].first == index ||
segments[i].second == index) {
sc++;
if (fs < 0) fs = i;
else ss = i;
if (segments[i].first == index || segments[i].second == index) {
sc++;
if (fs < 0)
fs = i;
else
ss = i;
}
int ei(0);
switch (sc) {
case 1:
segments.removeAt(fs);
break;
case 1: segments.removeAt(fs); break;
case 2:
if (segments[ss].first == index) ei = segments[ss].second;
else ei = segments[ss].first;
if (segments[fs].first == index) segments[fs].first = ei;
else segments[fs].second = ei;
if (segments[ss].first == index)
ei = segments[ss].second;
else
ei = segments[ss].first;
if (segments[fs].first == index)
segments[fs].first = ei;
else
segments[fs].second = ei;
segments.removeAt(ss);
break;
default: return;
}
pol.remove(index);
for (int i = 0; i < segments.size(); ++i) {
if (segments[i].first >= index)
segments[i].first--;
if (segments[i].second >= index)
segments[i].second--;
if (segments[i].first >= index) segments[i].first--;
if (segments[i].second >= index) segments[i].second--;
}
selPoint = -1;
checkDelete();
@@ -162,25 +174,19 @@ void BlockBusItem::removeSegment(int index) {
if (pif > pis) qSwap<int>(pif, pis);
int scf = 0, scs = 0;
for (int i = 0; i < segments.size(); ++i) {
if (segments[i].first == pif ||
segments[i].second == pif)
scf++;
if (segments[i].first == pis ||
segments[i].second == pis)
scs++;
if (segments[i].first == pif || segments[i].second == pif) scf++;
if (segments[i].first == pis || segments[i].second == pis) scs++;
}
if (scs <= 2) removePoint(pis);
if (scf <= 2) removePoint(pif);
if (scs <= 2 || scf <= 2) selSegment = -1;
if (scene() != 0) scene()->update();
}
void BlockBusItem::appendPoint(const QPointF & p) {
pol << p;
if (pol.size() > 1)
segments << QPair<int, int>(pol.size() - 2, pol.size() - 1);
if (pol.size() > 1) segments << QPair<int, int>(pol.size() - 2, pol.size() - 1);
updateGeometry();
}
@@ -229,13 +235,13 @@ void BlockBusItem::setSquareNodes(bool yes) {
void BlockBusItem::markAsInput() {
mark_in = true;
mark_in = true;
mark_out = false;
}
void BlockBusItem::markAsOutput() {
mark_in = false;
mark_in = false;
mark_out = true;
}
@@ -266,17 +272,23 @@ void BlockBusItem::simplify(bool full) {
int s0 = segs[0], s1 = segs[1];
QPointF cp = pol[p], sp[2];
for (int i = 0; i < 2; ++i) {
if (segments[segs[i]].first == p) sp[i] = pol[segments[segs[i]].second];
else sp[i] = pol[segments[segs[i]].first];
if (segments[segs[i]].first == p)
sp[i] = pol[segments[segs[i]].second];
else
sp[i] = pol[segments[segs[i]].first];
}
QLineF l0(sp[0], cp), l1(cp, sp[1]);
if (qAbs(l0.angle() - l1.angle()) > 0.1) continue;
if (segments[s0].first == p) {
if (segments[s1].first == p) segments[s0].first = segments[s1].second;
else segments[s0].first = segments[s1].first;
if (segments[s1].first == p)
segments[s0].first = segments[s1].second;
else
segments[s0].first = segments[s1].first;
} else {
if (segments[s1].first == p) segments[s0].second = segments[s1].second;
else segments[s0].second = segments[s1].first;
if (segments[s1].first == p)
segments[s0].second = segments[s1].second;
else
segments[s0].second = segments[s1].first;
}
segments.removeAt(s1);
pol.remove(p);
@@ -292,8 +304,7 @@ void BlockBusItem::simplify(bool full) {
}
void BlockBusItem::adjustLine() {
}
void BlockBusItem::adjustLine() {}
int BlockBusItem::endpointCount() const {
@@ -301,16 +312,16 @@ int BlockBusItem::endpointCount() const {
}
QList<BlockItem * > BlockBusItem::connectedBlocks() const {
QList<BlockItemPin * > pins = connections_.values();
QSet<BlockItem * > ret;
foreach (BlockItemPin * p, pins)
QList<BlockItem *> BlockBusItem::connectedBlocks() const {
QList<BlockItemPin *> pins = connections_.values();
QSet<BlockItem *> ret;
foreach(BlockItemPin * p, pins)
ret << p->parent();
return ret.values();
}
QList<BlockItemPin * > BlockBusItem::connectedPins() const {
QList<BlockItemPin *> BlockBusItem::connectedPins() const {
return connections_.values();
}
@@ -318,14 +329,14 @@ QList<BlockItemPin * > BlockBusItem::connectedPins() const {
BlockBusItem::PointInfo BlockBusItem::pointInfo(QPointF pos) const {
PointInfo ret;
int pi = -1, si = -1;
testPoint(pos, &pi, & si);
testPoint(pos, &pi, &si);
if (pi < 0 && si < 0) return ret;
if (si >= 0) {
ret.type = PointInfo::Type::Segment;
} else {
if (endpoints().contains(pi)) {
ret.type = PointInfo::Type::Endpoint;
ret.pin = connections_.value(pi, nullptr);
ret.pin = connections_.value(pi, nullptr);
} else
ret.type = PointInfo::Type::Node;
}
@@ -350,9 +361,8 @@ void BlockBusItem::clearBusState() {
QByteArray BlockBusItem::save() const {
ChunkStream cs;
cs << cs.chunk(1, busType()) << cs.chunk(2, busName()) << cs.chunk(3, width()) << cs.chunk(4, pen())
<< cs.chunk(5, brush()) << cs.chunk(6, pol) << cs.chunk(7, segments) << cs.chunk(8, props)
<< cs.chunk(9, im_bus_scale) << cs.chunk(10, im_end_scale);
cs << cs.chunk(1, busType()) << cs.chunk(2, busName()) << cs.chunk(3, width()) << cs.chunk(4, pen()) << cs.chunk(5, brush())
<< cs.chunk(6, pol) << cs.chunk(7, segments) << cs.chunk(8, props) << cs.chunk(9, im_bus_scale) << cs.chunk(10, im_end_scale);
return cs.data();
}
@@ -369,9 +379,9 @@ void BlockBusItem::load(const QByteArray & data) {
case 4: setPen(cs.getData<QPen>()); break;
case 5: setBrush(cs.getData<QBrush>()); break;
case 6: pol = cs.getData<QPolygonF>(); break;
case 7: segments = cs.getData<QList<QPair<int, int> > >(); break;
case 8: props = cs.getData<QList<BlockItem::Property> >(); break;
case 9: im_bus_scale = cs.getData<double>(); break;
case 7: segments = cs.getData<QList<QPair<int, int>>>(); break;
case 8: props = cs.getData<QList<BlockItem::Property>>(); break;
case 9: im_bus_scale = cs.getData<double>(); break;
case 10: im_end_scale = cs.getData<double>(); break;
}
}
@@ -387,16 +397,16 @@ BlockBusItem * BlockBusItem::copy() const {
void BlockBusItem::saveState() {
segments_s = segments;
ends_ind_s = ends_ind;
ends_s = ends;
pol_s = pol;
ends_s = ends;
pol_s = pol;
}
void BlockBusItem::restoreState() {
segments = segments_s;
ends_ind = ends_ind_s;
ends = ends_s;
pol = pol_s;
ends = ends_s;
pol = pol_s;
}
@@ -429,8 +439,8 @@ bool BlockBusItem::checkDelete() {
void BlockBusItem::emitAction(BlockItemBase::Action a) {
qobject_cast<BlockView*>(scene()->views().back())->schemeAction(a, QList<QGraphicsItem*>() << this);
qobject_cast<BlockView*>(scene()->views().back())->connectionsChanged();
qobject_cast<BlockView *>(scene()->views().back())->schemeAction(a, QList<QGraphicsItem *>() << this);
qobject_cast<BlockView *>(scene()->views().back())->connectionsChanged();
}
@@ -441,8 +451,7 @@ QVector<int> BlockBusItem::endpoints() const {
counts[segments[i].second]++;
}
for (int i = 0; i < counts.size(); ++i) {
if (counts[i] == 1)
ret << i;
if (counts[i] == 1) ret << i;
}
return ret;
}
@@ -455,16 +464,16 @@ QVector<int> BlockBusItem::endpointLine(int ep, double angle) const {
if (ep < 0 || np < 0) return ret;
if (pol[np] == pol[ep]) return ret;
QLineF l(pol[ep], pol[np]);
//qDebug() << "first" << l.angle() << angle << (l.angle() != angle);
// qDebug() << "first" << l.angle() << angle << (l.angle() != angle);
if (qAbs(l.angle() - angle) > 0.1) return ret;
//qDebug() << "check next" << segments.size();
// qDebug() << "check next" << segments.size();
for (int i = 0; i < segments.size(); ++i) {
//qDebug() << i << np << pointSegmentsCount(np);
// qDebug() << i << np << pointSegmentsCount(np);
if (np < 0) break;
if (pointSegmentsCount(np) != 2) break;
if (i > 0) {
QLineF l(pol[pp], pol[np]);
//qDebug() << i << l.angle() << angle;
// qDebug() << i << l.angle() << angle;
if (qAbs(l.angle() - angle) > 0.1) break;
}
ret << np;
@@ -472,7 +481,6 @@ QVector<int> BlockBusItem::endpointLine(int ep, double angle) const {
np = neighborSegmentPoint(np, &seg);
}
return ret;
}
@@ -492,8 +500,14 @@ int BlockBusItem::neighborSegmentPoint(int point, int * seg) const {
if (point < 0 || !seg) return -1;
for (int i = 0; i < segments.size(); ++i) {
if (i == *seg) continue;
if (segments[i].first == point) {*seg = i; return segments[i].second;}
if (segments[i].second == point) {*seg = i; return segments[i].first ;}
if (segments[i].first == point) {
*seg = i;
return segments[i].second;
}
if (segments[i].second == point) {
*seg = i;
return segments[i].first;
}
}
return -1;
}
@@ -502,57 +516,58 @@ int BlockBusItem::neighborSegmentPoint(int point, int * seg) const {
void BlockBusItem::testPoint(QPointF pos, int * sel_point, int * sel_segment, bool for_trace) const {
for (int i = 0; i < pol.size(); ++i) {
if ((pol[i] - pos).manhattanLength() <= (for_trace ? 5. : 10.)) { // Point
*sel_point = i;
*sel_point = i;
*sel_segment = -1;
return;
}
}
for (int i = 0; i < segments.size(); ++i) {
if (distPointToLine(pol[segments[i].first], pol[segments[i].second], pos) <= (for_trace ? 5. : 7.)) { // Segment
*sel_point = -1;
*sel_point = -1;
*sel_segment = i;
return;
}
}
*sel_point = -1;
*sel_point = -1;
*sel_segment = -1;
}
void BlockBusItem::hoverEnterEvent(QGraphicsSceneHoverEvent * e) {
tt = bus_name + (bus_name.isEmpty() ? "" : "\n\n")
+ tr("Add point: Ctrl + LeftClick\n"
"Remove point\\segment: Ctrl + RightClick\n"
"Remove connection: Shift + RightClick\n"
"Move point\\segment: Shift + LeftPress\n"
"Change trace mode: press Shift, when mouse move");
tt = bus_name + (bus_name.isEmpty() ? "" : "\n\n") +
tr("Add point: Ctrl + LeftClick\n"
"Remove point\\segment: Ctrl + RightClick\n"
"Remove connection: Shift + RightClick\n"
"Move point\\segment: Shift + LeftPress\n"
"Change trace mode: press Shift, when mouse move");
}
void BlockBusItem::hoverMoveEvent(QGraphicsSceneHoverEvent * e) {
if (temp_) return;
QPointF sp = e->scenePos();
int pp = selPoint;
int ps = selSegment;
bool empt = !(selPoint >= 0 || selSegment >= 0);
int pp = selPoint;
int ps = selSegment;
bool empt = !(selPoint >= 0 || selSegment >= 0);
testPoint(sp, &selPoint, &selSegment);
BlockView * bv = 0;
if (!scene()->views().isEmpty()) {
bv = qobject_cast<BlockView*>(scene()->views().back());
bv = qobject_cast<BlockView *>(scene()->views().back());
}
if ((selPoint >= 0 && pp != selPoint) || (selSegment >= 0 && ps != selSegment)) {
if ((selPoint >= 0 && pp != selPoint) || (selSegment >= 0 && ps != selSegment)) {
if (bv) {
if (bv->isBlockAnimationEnabled()) {
setPointSize(0);
anim_point_size.start();
}
} else setPointSize(anim_point_size.endValue().toDouble());
} else
setPointSize(anim_point_size.endValue().toDouble());
}
if (selPoint >= 0 || selSegment >= 0) {
if (empt) {
QList<BlockItemPin * > pins = connectedPins();
foreach (BlockItemPin * p, pins) {
QList<BlockItemPin *> pins = connectedPins();
foreach(BlockItemPin * p, pins) {
p->animAccept();
}
}
@@ -563,15 +578,15 @@ void BlockBusItem::hoverMoveEvent(QGraphicsSceneHoverEvent * e) {
}
if (bv) bv->cur_bus = 0;
setToolTip(QString());
QList<QGraphicsItem * > il = scene()->items(sp, Qt::ContainsItemBoundingRect, Qt::DescendingOrder), bil;
QList<QGraphicsItem *> il = scene()->items(sp, Qt::ContainsItemBoundingRect, Qt::DescendingOrder), bil;
bil << this;
for (int i = 0; i < il.size(); ++i) {
QGraphicsItem * b = il[i];
if ((b->data(bvidType).toInt() == bvitBus) && b != this) {
int tp = -1, ts = -1;
((BlockBusItem*)b)->testPoint(sp, &tp, &ts);
((BlockBusItem *)b)->testPoint(sp, &tp, &ts);
if (tp >= 0 || ts >= 0) {
foreach (QGraphicsItem * b2, bil)
foreach(QGraphicsItem * b2, bil)
b2->stackBefore(b);
break;
}
@@ -585,14 +600,13 @@ void BlockBusItem::hoverMoveEvent(QGraphicsSceneHoverEvent * e) {
void BlockBusItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) {
if (temp_) return;
selPoint = selSegment = -1;
setPen(pu); setBrush(bu);
setPen(pu);
setBrush(bu);
setToolTip(QString());
anim_point_size.stop();
BlockView * bv = 0;
if (!scene()->views().isEmpty())
bv = qobject_cast<BlockView*>(scene()->views().back());
if (bv && (QApplication::mouseButtons() == 0))
bv->cur_bus = 0;
if (!scene()->views().isEmpty()) bv = qobject_cast<BlockView *>(scene()->views().back());
if (bv && (QApplication::mouseButtons() == 0)) bv->cur_bus = 0;
update();
QGraphicsObject::hoverLeaveEvent(e);
}
@@ -601,18 +615,16 @@ void BlockBusItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) {
void BlockBusItem::mousePressEvent(QGraphicsSceneMouseEvent * e) {
if (temp_) return;
lp = quantize(e->scenePos(), grid_step);
if (e->button() != Qt::RightButton)
bpol = pol;
if (e->button() != Qt::RightButton) bpol = pol;
BlockView * bv = 0;
if (!scene()->views().isEmpty()) {
bv = qobject_cast<BlockView*>(scene()->views().back());
bv = qobject_cast<BlockView *>(scene()->views().back());
}
if (bv) {
if (selPoint >= 0 || selSegment >= 0)
bv->cur_bus = this;
if (selPoint >= 0 || selSegment >= 0) bv->cur_bus = this;
}
if (new_segment) {
qobject_cast<BlockView*>(scene()->views().back())->newBranchCancel();
qobject_cast<BlockView *>(scene()->views().back())->newBranchCancel();
}
new_segment = false;
if ((selPoint < 0 || selPoint > pol.size() - 1) && (selSegment < 0) && e->modifiers().testFlag(Qt::ShiftModifier)) {
@@ -621,23 +633,24 @@ void BlockBusItem::mousePressEvent(QGraphicsSceneMouseEvent * e) {
}
int btncnt = 0;
if ((e->button() == Qt::LeftButton) && e->modifiers().testFlag(Qt::ShiftModifier)) {
if (endpoints().contains(selPoint)) qobject_cast<BlockView*>(scene()->views().back())->startBusPointMove(bus_type);
if (endpoints().contains(selPoint)) qobject_cast<BlockView *>(scene()->views().back())->startBusPointMove(bus_type);
}
if (e->buttons().testFlag(Qt::LeftButton )) btncnt++;
if (e->buttons().testFlag(Qt::LeftButton)) btncnt++;
if (e->buttons().testFlag(Qt::RightButton)) btncnt++;
if (e->buttons().testFlag(QT_MID_BUTTON )) btncnt++;
if (e->buttons().testFlag(QT_MID_BUTTON)) btncnt++;
if (btncnt > 0) mm_mods = e->modifiers();
if (btncnt >= 2 && e->button() == Qt::RightButton) {
//qDebug() << "bus revert";
// qDebug() << "bus revert";
mm_cancel = true;
moved = false;
pol = bpol;
moved = false;
pol = bpol;
prepareGeometryChange();
return;
}
if (e->buttons().testFlag(Qt::LeftButton) && e->modifiers().testFlag(Qt::NoModifier)) {
if (selSegment >= 0)
press_pos = quantize(nearestPointOnLine(pol[segments[selSegment].first], pol[segments[selSegment].second], e->scenePos()), grid_step);
press_pos =
quantize(nearestPointOnLine(pol[segments[selSegment].first], pol[segments[selSegment].second], e->scenePos()), grid_step);
else {
if (selPoint >= 0)
press_pos = pol[selPoint];
@@ -646,10 +659,9 @@ void BlockBusItem::mousePressEvent(QGraphicsSceneMouseEvent * e) {
}
if (max_ep >= 2) {
if (endpointCount() >= max_ep)
if (pointSegmentsCount(selPoint) >= 2 || selSegment >= 0)
return;
if (pointSegmentsCount(selPoint) >= 2 || selSegment >= 0) return;
}
qobject_cast<BlockView*>(scene()->views().back())->newBranch(this);
qobject_cast<BlockView *>(scene()->views().back())->newBranch(this);
new_segment = true;
return;
}
@@ -657,7 +669,7 @@ void BlockBusItem::mousePressEvent(QGraphicsSceneMouseEvent * e) {
deleteLater();
}
if (e->modifiers().testFlag(Qt::ControlModifier)) {
//qDebug() << "remove" << selPoint << selSegment;
// qDebug() << "remove" << selPoint << selSegment;
if (e->buttons().testFlag(Qt::RightButton)) {
if (selPoint >= 0 && selPoint <= pol.size() - 1) {
removePoint(selPoint);
@@ -673,8 +685,7 @@ void BlockBusItem::mousePressEvent(QGraphicsSceneMouseEvent * e) {
}
}
if (e->buttons().testFlag(Qt::LeftButton) && selSegment >= 0) {
if (addPoint(e->scenePos()) >= 0)
emitAction(BlockItemBase::BusPointAdd);
if (addPoint(e->scenePos()) >= 0) emitAction(BlockItemBase::BusPointAdd);
return;
}
}
@@ -693,12 +704,12 @@ void BlockBusItem::mouseMoveEvent(QGraphicsSceneMouseEvent * e) {
}
BlockView * bv = 0;
if (!scene()->views().isEmpty()) {
bv = qobject_cast<BlockView*>(scene()->views().back());
bv = qobject_cast<BlockView *>(scene()->views().back());
}
qp = quantize(e->scenePos(), grid_step);
lp = qp - lp;
if (e->buttons().testFlag(Qt::LeftButton) && mm_mods.testFlag(Qt::NoModifier) && new_segment) {
if (bv) qobject_cast<BlockView*>(scene()->views().back())->newBranchTrace(this, e->scenePos());
if (bv) qobject_cast<BlockView *>(scene()->views().back())->newBranchTrace(this, e->scenePos());
return;
}
if (new_segment) {
@@ -707,8 +718,7 @@ void BlockBusItem::mouseMoveEvent(QGraphicsSceneMouseEvent * e) {
} else {
if (e->buttons().testFlag(Qt::LeftButton)) {
lm_point = selPoint >= 0;
if (selPoint >= 0 && selPoint <= pol.size() - 1)
pol[selPoint] += lp;
if (selPoint >= 0 && selPoint <= pol.size() - 1) pol[selPoint] += lp;
if (selSegment >= 0 && selSegment <= segments.size() - 1) {
pol[segments[selSegment].first] += lp;
pol[segments[selSegment].second] += lp;
@@ -722,14 +732,14 @@ void BlockBusItem::mouseMoveEvent(QGraphicsSceneMouseEvent * e) {
void BlockBusItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * e) {
mm_mods = Qt::KeyboardModifiers();
mm_mods = Qt::KeyboardModifiers();
int btncnt = 0;
if (e->buttons().testFlag(Qt::LeftButton )) btncnt++;
if (e->buttons().testFlag(Qt::LeftButton)) btncnt++;
if (e->buttons().testFlag(Qt::RightButton)) btncnt++;
if (e->buttons().testFlag(QT_MID_BUTTON )) btncnt++;
if (e->buttons().testFlag(QT_MID_BUTTON)) btncnt++;
if (btncnt == 0) mm_cancel = false;
if (new_segment) {
qobject_cast<BlockView*>(scene()->views().back())->newBranchAccept(this);
qobject_cast<BlockView *>(scene()->views().back())->newBranchAccept(this);
updateGeometry();
selPoint = selSegment = -1;
}
@@ -739,7 +749,7 @@ void BlockBusItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * e) {
emitAction(BlockItemBase::BusPointMove);
} else {
reconnect();
emitAction( BlockItemBase::BusSegmentMove);
emitAction(BlockItemBase::BusSegmentMove);
}
}
moved = new_segment = false;

View File

@@ -1,20 +1,20 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLOCKBUSITEM_H
@@ -24,15 +24,18 @@
#include "qad_blockview_export.h"
class QAD_BLOCKVIEW_EXPORT BlockBusItem: public QGraphicsObject, public PropertyStorage {
class QAD_BLOCKVIEW_EXPORT BlockBusItem
: public QGraphicsObject
, public PropertyStorage {
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
Q_PROPERTY(double pointSize READ pointSize WRITE setPointSize DESIGNABLE false SCRIPTABLE false)
friend class BlockView;
public:
BlockBusItem(bool temp = false);
BlockBusItem(const BlockBusItem & other);
~BlockBusItem() {;}
~BlockBusItem() { ; }
struct QAD_BLOCKVIEW_EXPORT PointInfo {
enum class Type {
@@ -41,30 +44,46 @@ public:
Endpoint,
Segment
};
Type type = Type::None;
Type type = Type::None;
BlockItemPin * pin = nullptr; // if tEndpoint and pin connected
};
void setGridStep(double gs) {grid_step = gs;}
void setEndpointsNumber(int num) {max_ep = num;}
void setImages(const QImage & bus, const QImage & end = QImage()) {im_bus = bus; im_end = end; update();}
void setBusImageScale(double s) {im_bus_scale = s; update();}
void setEndpointImageScale(double s) {im_end_scale = s; update();}
void setBusType(int type_) {bus_type = type_;}
void setBusName(const QString & name) {bus_name = name;}
int busType() const {return bus_type;}
QString busName() const {return bus_name;}
double busImageScale() const {return im_bus_scale;}
double endpointImageScale() const {return im_end_scale;}
void setGridStep(double gs) { grid_step = gs; }
void setEndpointsNumber(int num) { max_ep = num; }
void setImages(const QImage & bus, const QImage & end = QImage()) {
im_bus = bus;
im_end = end;
update();
}
void setBusImageScale(double s) {
im_bus_scale = s;
update();
}
void setEndpointImageScale(double s) {
im_end_scale = s;
update();
}
void setBusType(int type_) { bus_type = type_; }
void setBusName(const QString & name) { bus_name = name; }
int busType() const { return bus_type; }
QString busName() const { return bus_name; }
double busImageScale() const { return im_bus_scale; }
double endpointImageScale() const { return im_end_scale; }
void appendPoint(const QPointF & p);
void appendPoint(qreal x, qreal y);
void movePolyline(const QPointF & dp);
void clear();
void setPen(const QPen & p) {p_ = p; update();}
QPen pen() const {return p_;}
void setBrush(const QBrush & b) {b_ = b; update();}
QBrush brush() const {return b_;}
double width() const {return pen_width;}
void setPen(const QPen & p) {
p_ = p;
update();
}
QPen pen() const { return p_; }
void setBrush(const QBrush & b) {
b_ = b;
update();
}
QBrush brush() const { return b_; }
double width() const { return pen_width; }
void setWidth(const double & w);
void setColor(const QColor & c);
void setSquareNodes(bool yes);
@@ -74,24 +93,26 @@ public:
void simplify(bool full = true);
void adjustLine();
int endpointCount() const;
bool isBusSelected() const {return selSegment >= 0 || selPoint >= 0;}
QList<BlockItem * > connectedBlocks() const;
QList<BlockItemPin * > connectedPins() const;
bool isBusSelected() const { return selSegment >= 0 || selPoint >= 0; }
QList<BlockItem *> connectedBlocks() const;
QList<BlockItemPin *> connectedPins() const;
PointInfo pointInfo(QPointF pos) const;
void setBusState(bool state);
bool busState() const {return state_ > 0;}
bool busState() const { return state_ > 0; }
void clearBusState();
QByteArray save() const;
void load(const QByteArray & data);
BlockBusItem * copy() const;
void saveState();
void restoreState();
enum {Type = UserType + 2};
enum {
Type = UserType + 2
};
protected:
void _init();
void reconnect();
@@ -108,7 +129,7 @@ protected:
QVector<int> endpoints() const;
QVector<int> endpointLine(int ep, double angle) const;
int neighborSegmentPoint(int point, int * seg) const;
int type() const {return Type;}
int type() const { return Type; }
QRectF boundingRect() const;
bool sceneEvent(QEvent * e);
void hoverEnterEvent(QGraphicsSceneHoverEvent * e);
@@ -118,13 +139,13 @@ protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent * e);
void mouseReleaseEvent(QGraphicsSceneMouseEvent * e);
void paint(QPainter * p, const QStyleOptionGraphicsItem * o, QWidget * w = 0);
QPointF lp, new_start, new_end, press_pos, qp;
QPen p_, ph, pu, pa, pr, pn;
QBrush b_, bh, bu, ba, br;
QString tt, bus_name;
QList<QPair<int, int> > segments, ends_ind, segments_s, ends_ind_s;
QMap<int, BlockItemPin * > connections_;
QList<QPair<int, int>> segments, ends_ind, segments_s, ends_ind_s;
QMap<int, BlockItemPin *> connections_;
QVector<int> ends, ends_s;
QImage im_bus, im_end;
QPolygonF pol, bpol, pol_s;
@@ -133,9 +154,9 @@ protected:
double pen_width, grid_step, im_bus_scale, im_end_scale;
int selPoint, selSegment, max_ep, bus_type, state_;
bool moved, deleted, mark_in, mark_out, new_segment, mm_cancel, lm_point;
private:
double pointSize() const {return point_size;}
double pointSize() const { return point_size; }
void setPointSize(double s);
double point_size;
@@ -143,9 +164,13 @@ private:
};
inline QDataStream & operator <<(QDataStream & s, const BlockBusItem * b) {s << b->save(); return s;}
inline QDataStream & operator >>(QDataStream & s, BlockBusItem *& b) {
QByteArray ba; s >> ba;
inline QDataStream & operator<<(QDataStream & s, const BlockBusItem * b) {
s << b->save();
return s;
}
inline QDataStream & operator>>(QDataStream & s, BlockBusItem *& b) {
QByteArray ba;
s >> ba;
b = new BlockBusItem();
b->load(ba);
return s;

View File

@@ -1,17 +1,19 @@
#include "blockeditor.h"
#include "ui_blockeditor.h"
#include "drawtools.h"
#include "blockview.h"
#include <QToolBar>
#include "drawtools.h"
#include "ui_blockeditor.h"
#include <QComboBox>
#include <QFileDialog>
#include <QFile>
#include <QFileDialog>
#include <QTimer>
#include <QToolBar>
BlockEditor::BlockEditor(QWidget *parent) : QWidget(parent), ui(new Ui::BlockEditor) {
init = false;
m_editorMode = false;
BlockEditor::BlockEditor(QWidget * parent): QWidget(parent), ui(new Ui::BlockEditor) {
init = false;
m_editorMode = false;
m_pinsEditable = true;
ui->setupUi(this);
src_title = windowTitle();
@@ -20,13 +22,15 @@ BlockEditor::BlockEditor(QWidget *parent) : QWidget(parent), ui(new Ui::BlockEdi
ui->blockView->addItem(&block);
ui->blockView->viewport()->installEventFilter(this);
DrawTools * drawtools = new DrawTools(ui->blockView);
connect(drawtools, SIGNAL(itemCreated(QGraphicsItem*)), this, SLOT(addItem(QGraphicsItem*)));
drawtools->textEditCombo()->addItems(QStringList() << "%name" << "%value" << "%id");
connect(drawtools, SIGNAL(itemCreated(QGraphicsItem *)), this, SLOT(addItem(QGraphicsItem *)));
drawtools->textEditCombo()->addItems(QStringList() << "%name"
<< "%value"
<< "%id");
ui->layoutProperties->addWidget(drawtools->propertyWidget());
ui->actionRemove_items->setEnabled(false);
ui->button_color->setColor(Qt::lightGray);
ui->treePins->setItemDelegateForColumn(1, new PinBusDelegate());
connect(ui->treePins, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(updateBlock()));
connect(ui->treePins, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(updateBlock()));
ui->treePins->viewport()->installEventFilter(this);
QToolBar * bar;
@@ -39,7 +43,7 @@ BlockEditor::BlockEditor(QWidget *parent) : QWidget(parent), ui(new Ui::BlockEdi
bar->setOrientation(Qt::Vertical);
bar->addActions(drawtools->actionsForZ());
bar->addSeparator();
bar->addActions(QList<QAction*>() << ui->actionRemove_items);
bar->addActions(QList<QAction *>() << ui->actionRemove_items);
ui->widgetBarZ->setMinimumSize(bar->sizeHint());
init = true;
on_buttonClear_clicked();
@@ -64,7 +68,7 @@ void BlockEditor::loadFile(QString path) {
}
void BlockEditor::loadModel(const QByteArray &model) {
void BlockEditor::loadModel(const QByteArray & model) {
BlockItem b;
b.loadModel(model);
ui->spin_w->setValue(b.width());
@@ -74,15 +78,15 @@ void BlockEditor::loadModel(const QByteArray &model) {
block.loadModel(model);
treePinsClear();
ui->treePins->blockSignals(true);
QVector<BlockItemPin * > pins = block.pins();
foreach (BlockItemPin * p, pins) {
QVector<BlockItemPin *> pins = block.pins();
foreach(BlockItemPin * p, pins) {
QTreeWidgetItem * ti = new QTreeWidgetItem(QStringList() << p->text() << QString::number(p->busType()));
ti->setData(0, Qt::UserRole, qulonglong(p));
ti->setData(0, Qt::UserRole + 1, (int)p->alignment());
ti->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled);
pin_tli[p->alignment()]->addChild(ti);
}
foreach (QGraphicsItem * i, block.decors()) {
foreach(QGraphicsItem * i, block.decors()) {
i->setData(bvidMoveParent, false);
i->setData(bvidCorrectMove, true);
}
@@ -121,7 +125,7 @@ void BlockEditor::selectionChanged() {
}
void BlockEditor::addItem(QGraphicsItem *item) {
void BlockEditor::addItem(QGraphicsItem * item) {
block.addDecor(item);
item->setData(bvidMoveParent, false);
item->setData(bvidCorrectMove, true);
@@ -138,9 +142,13 @@ void BlockEditor::updateBlock() {
void BlockEditor::treePinsClear() {
ui->treePins->blockSignals(true);
ui->treePins->clear();
QFont bf(font()); bf.setBold(true);
QList<int> al = QList<int>() << Qt::AlignLeft << Qt::AlignRight << Qt::AlignTop << Qt::AlignBottom;
QStringList an = QStringList() << "Left" << "Right" << "Top" << "Bottom";
QFont bf(font());
bf.setBold(true);
QList<int> al = QList<int>() << Qt::AlignLeft << Qt::AlignRight << Qt::AlignTop << Qt::AlignBottom;
QStringList an = QStringList() << "Left"
<< "Right"
<< "Top"
<< "Bottom";
pin_tli.clear();
for (int i = 0; i < al.size(); ++i) {
QTreeWidgetItem * ti = new QTreeWidgetItem();
@@ -157,7 +165,7 @@ void BlockEditor::treePinsClear() {
}
bool BlockEditor::eventFilter(QObject *o, QEvent *e) {
bool BlockEditor::eventFilter(QObject * o, QEvent * e) {
if (!init) QWidget::eventFilter(o, e);
if (o == ui->treePins->viewport()) {
if (e->type() == QEvent::Drop) {
@@ -176,17 +184,15 @@ bool BlockEditor::eventFilter(QObject *o, QEvent *e) {
void BlockEditor::changeEvent(QEvent * e) {
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
case QEvent::LanguageChange: ui->retranslateUi(this); break;
default: break;
}
}
void BlockEditor::on_actionRemove_items_triggered() {
QList<QGraphicsItem*> si = ui->blockView->scene()->selectedItems();
foreach (QGraphicsItem * i, si)
QList<QGraphicsItem *> si = ui->blockView->scene()->selectedItems();
foreach(QGraphicsItem * i, si)
block.removeDecor(i);
}
@@ -233,10 +239,10 @@ void BlockEditor::on_buttonClear_clicked() {
}
void BlockEditor::on_buttonPinAdd_clicked() {
ui->treePins->blockSignals(true);
QTreeWidgetItem * ti = new QTreeWidgetItem(QStringList() << "" << "0");
QTreeWidgetItem * ti = new QTreeWidgetItem(QStringList() << ""
<< "0");
ti->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled);
ti->setData(0, Qt::UserRole, qulonglong(block.addPin(Qt::AlignLeft, ti->text(1).toInt(), ti->text(0))));
ti->setData(0, Qt::UserRole + 1, (int)Qt::AlignLeft);
@@ -265,10 +271,10 @@ void BlockEditor::on_buttonPinDup_clicked() {
void BlockEditor::on_buttonPinDelete_clicked() {
ui->treePins->blockSignals(true);
QList<QTreeWidgetItem*> si = ui->treePins->selectedItems();
foreach (QTreeWidgetItem * i, si) {
QList<QTreeWidgetItem *> si = ui->treePins->selectedItems();
foreach(QTreeWidgetItem * i, si) {
if (!i->parent()) continue;
block.removePin((BlockItemPin*)(i->data(0, Qt::UserRole).toLongLong()));
block.removePin((BlockItemPin *)(i->data(0, Qt::UserRole).toLongLong()));
delete i;
}
ui->treePins->blockSignals(false);
@@ -283,7 +289,7 @@ void BlockEditor::on_buttonPinClear_clicked() {
void BlockEditor::on_treePins_itemChanged(QTreeWidgetItem * item, int column) {
if (!item) return;
BlockItemPin * pin = (BlockItemPin*)item->data(0, Qt::UserRole).toULongLong();
BlockItemPin * pin = (BlockItemPin *)item->data(0, Qt::UserRole).toULongLong();
if (!pin) return;
switch (column) {
case 0:
@@ -297,16 +303,15 @@ void BlockEditor::on_treePins_itemChanged(QTreeWidgetItem * item, int column) {
}
void BlockEditor::arrangePins() {
QVector<BlockItemPin * > pins = block.pins();
QList<QTreeWidgetItem*> tli = pin_tli.values();
foreach (QTreeWidgetItem * ti, tli) {
QVector<BlockItemPin *> pins = block.pins();
QList<QTreeWidgetItem *> tli = pin_tli.values();
foreach(QTreeWidgetItem * ti, tli) {
for (int i = 0; i < ti->childCount(); ++i) {
foreach (BlockItemPin * p, pins)
if (p == (BlockItemPin*)(ti->child(i)->data(0, Qt::UserRole).toULongLong())) {
foreach(BlockItemPin * p, pins)
if (p == (BlockItemPin *)(ti->child(i)->data(0, Qt::UserRole).toULongLong())) {
p->setAlignment((Qt::Alignment)ti->data(0, Qt::UserRole).toInt());
BlockItemPin * np = block.addPin(p, false);
BlockItemPin * np = block.addPin(p, false);
ti->child(i)->setData(0, Qt::UserRole, qulonglong(np));
ti->child(i)->setData(0, Qt::UserRole + 1, ti->data(0, Qt::UserRole).toInt());
break;
@@ -325,11 +330,15 @@ void BlockEditor::arrangePins() {
QWidget * PinAlignDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {
QComboBox * combo = new QComboBox(parent);
int cv = index.data().toInt();
combo->addItem("Left", int(Qt::AlignLeft)); if (cv == Qt::AlignLeft) combo->setCurrentIndex(0);
combo->addItem("Right", int(Qt::AlignRight)); if (cv == Qt::AlignRight) combo->setCurrentIndex(1);
combo->addItem("Top", int(Qt::AlignTop)); if (cv == Qt::AlignTop) combo->setCurrentIndex(2);
combo->addItem("Bottom", int(Qt::AlignBottom)); if (cv == Qt::AlignBottom) combo->setCurrentIndex(3);
int cv = index.data().toInt();
combo->addItem("Left", int(Qt::AlignLeft));
if (cv == Qt::AlignLeft) combo->setCurrentIndex(0);
combo->addItem("Right", int(Qt::AlignRight));
if (cv == Qt::AlignRight) combo->setCurrentIndex(1);
combo->addItem("Top", int(Qt::AlignTop));
if (cv == Qt::AlignTop) combo->setCurrentIndex(2);
combo->addItem("Bottom", int(Qt::AlignBottom));
if (cv == Qt::AlignBottom) combo->setCurrentIndex(3);
combo->setGeometry(option.rect);
return combo;
}
@@ -348,7 +357,7 @@ QString PinAlignDelegate::displayText(const QVariant & value, const QLocale & lo
void PinAlignDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const {
model->setData(index, ((QComboBox*)editor)->itemData(((QComboBox*)editor)->currentIndex()).toInt());
model->setData(index, ((QComboBox *)editor)->itemData(((QComboBox *)editor)->currentIndex()).toInt());
}
@@ -374,6 +383,5 @@ QString PinBusDelegate::displayText(const QVariant & value, const QLocale & loca
void PinBusDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const {
model->setData(index, ((QSpinBox*)editor)->value());
model->setData(index, ((QSpinBox *)editor)->value());
}

View File

@@ -1,49 +1,50 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLOCKEDITOR_H
#define BLOCKEDITOR_H
#include <QWidget>
#include <QTreeWidgetItem>
#include <QStyledItemDelegate>
#include "blockitem.h"
#include "qad_blockview_export.h"
#include <QStyledItemDelegate>
#include <QTreeWidgetItem>
#include <QWidget>
namespace Ui {
class BlockEditor;
class BlockEditor;
}
class QAD_BLOCKVIEW_EXPORT BlockEditor : public QWidget
{
class QAD_BLOCKVIEW_EXPORT BlockEditor: public QWidget {
Q_OBJECT
Q_PROPERTY(bool editorMode READ editorMode WRITE setEditorMode)
Q_PROPERTY(bool pinsEditable READ pinsEditable WRITE setPinsEditable)
public:
explicit BlockEditor(QWidget *parent = 0);
explicit BlockEditor(QWidget * parent = 0);
~BlockEditor();
bool editorMode() const {return m_editorMode;}
bool pinsEditable() const {return m_pinsEditable;}
bool editorMode() const { return m_editorMode; }
bool pinsEditable() const { return m_pinsEditable; }
public slots:
void loadFile(QString path);
@@ -67,14 +68,14 @@ private slots:
void on_buttonPinDup_clicked();
void on_buttonPinDelete_clicked();
void on_buttonPinClear_clicked();
void on_treePins_itemChanged(QTreeWidgetItem *item, int column);
void on_treePins_itemChanged(QTreeWidgetItem * item, int column);
private:
bool eventFilter(QObject * o, QEvent * e);
void changeEvent(QEvent * e);
Ui::BlockEditor *ui;
QMap<int, QTreeWidgetItem*> pin_tli;
Ui::BlockEditor * ui;
QMap<int, QTreeWidgetItem *> pin_tli;
BlockItem block;
QString src_title, cur_file;
bool init;
@@ -83,26 +84,28 @@ private:
};
class QAD_BLOCKVIEW_EXPORT PinAlignDelegate: public QStyledItemDelegate {
Q_OBJECT
public:
PinAlignDelegate(QObject * parent = 0): QStyledItemDelegate(parent) {}
QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
QString displayText(const QVariant & value, const QLocale & locale) const;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const {return QSize(60, 26);}
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const { return QSize(60, 26); }
};
class QAD_BLOCKVIEW_EXPORT PinBusDelegate: public QStyledItemDelegate {
Q_OBJECT
public:
PinBusDelegate(QObject * parent = 0): QStyledItemDelegate(parent) {}
QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
QString displayText(const QVariant & value, const QLocale & locale) const;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const {return QSize(60, 26);}
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const { return QSize(60, 26); }
private:
typedef QPair<int, QString> ISPair;
QVector<ISPair> buses;

View File

@@ -1,11 +1,11 @@
#include "blockview.h"
#include <QApplication>
#define BLOCKITEM_DEFAULT_PIN_MARGIN 20
BlockItem::BlockItem(QGraphicsItem * parent): QGraphicsObject(parent), PropertyStorage(),
g_main(this), g_selection(this) {
BlockItem::BlockItem(QGraphicsItem * parent): QGraphicsObject(parent), PropertyStorage(), g_main(this), g_selection(this) {
setData(bvidType, bvitBlock);
setZValue(2.);
setAcceptHoverEvents(true);
@@ -58,11 +58,9 @@ void BlockItem::_resize(QSizeF s) {
void BlockItem::_moveToTop(bool only_decors) {
qreal dy = -g_main.rect().center().y() + 10;
if (!only_decors)
moveBy(0., dy);
foreach (QGraphicsItem * d, decors_)
if (!only_decors) moveBy(0., dy);
foreach(QGraphicsItem * d, decors_)
d->moveBy(0., -dy);
}
@@ -70,30 +68,27 @@ BlockItemPin * BlockItem::addPin(BlockItemPin * pin, bool update_) {
pin->setParentItem(this);
if (!pins_[pin->alignment()].contains(pin)) pins_[pin->alignment()] << pin;
pin->parent_ = this;
if (update_)
arrangePins();
if (update_) arrangePins();
return pin;
}
BlockItemPin * BlockItem::addPin(Qt::Alignment align, int bus_type, const QString & text, bool update_) {
BlockItemPin * pin = new BlockItemPin(align, bus_type, text, this);
pin->parent_ = this;
pin->parent_ = this;
pins_[pin->alignment()] << pin;
if (update_)
arrangePins();
if (update_) arrangePins();
return pin;
}
void BlockItem::removePin(BlockItemPin * pin) {
if (!pin) return;
QMutableMapIterator<Qt::Alignment, QVector<BlockItemPin * > > it(pins_);
QMutableMapIterator<Qt::Alignment, QVector<BlockItemPin *>> it(pins_);
while (it.hasNext()) {
it.next();
QVector<BlockItemPin * > & pv(it.value());
if (pv.contains(pin))
pv.remove(it.value().indexOf(pin));
QVector<BlockItemPin *> & pv(it.value());
if (pv.contains(pin)) pv.remove(it.value().indexOf(pin));
}
delete pin;
arrangePins();
@@ -102,12 +97,13 @@ void BlockItem::removePin(BlockItemPin * pin) {
void BlockItem::addDecor(QGraphicsItem * item) {
if (decors_.contains(item)) return;
if (qgraphicsitem_cast<QGraphicsPixmapItem*>(item))
qgraphicsitem_cast<QGraphicsPixmapItem*>(item)->setTransformationMode(Qt::SmoothTransformation);
if (qgraphicsitem_cast<QGraphicsSimpleTextItem*>(item))
qgraphicsitem_cast<QGraphicsSimpleTextItem*>(item)->setData(bvidDecorText, qgraphicsitem_cast<QGraphicsSimpleTextItem*>(item)->text());
if (qgraphicsitem_cast<AlignedTextItem*>(item))
qgraphicsitem_cast<AlignedTextItem*>(item)->setData(bvidDecorText, qgraphicsitem_cast<AlignedTextItem*>(item)->text());
if (qgraphicsitem_cast<QGraphicsPixmapItem *>(item))
qgraphicsitem_cast<QGraphicsPixmapItem *>(item)->setTransformationMode(Qt::SmoothTransformation);
if (qgraphicsitem_cast<QGraphicsSimpleTextItem *>(item))
qgraphicsitem_cast<QGraphicsSimpleTextItem *>(item)->setData(bvidDecorText,
qgraphicsitem_cast<QGraphicsSimpleTextItem *>(item)->text());
if (qgraphicsitem_cast<AlignedTextItem *>(item))
qgraphicsitem_cast<AlignedTextItem *>(item)->setData(bvidDecorText, qgraphicsitem_cast<AlignedTextItem *>(item)->text());
item->setData(bvidMoveParent, true);
item->setData(bvidBlockDecor, true);
decors_ << item;
@@ -117,12 +113,13 @@ void BlockItem::addDecor(QGraphicsItem * item) {
void BlockItem::addDecor(QGraphicsItem & item) {
if (decors_.contains(&item)) return;
if (qgraphicsitem_cast<QGraphicsPixmapItem*>(&item))
qgraphicsitem_cast<QGraphicsPixmapItem*>(&item)->setTransformationMode(Qt::SmoothTransformation);
if (qgraphicsitem_cast<QGraphicsSimpleTextItem*>(&item))
qgraphicsitem_cast<QGraphicsSimpleTextItem*>(&item)->setData(bvidDecorText, qgraphicsitem_cast<QGraphicsSimpleTextItem*>(&item)->text());
if (qgraphicsitem_cast<AlignedTextItem*>(&item))
qgraphicsitem_cast<AlignedTextItem*>(&item)->setData(bvidDecorText, qgraphicsitem_cast<AlignedTextItem*>(&item)->text());
if (qgraphicsitem_cast<QGraphicsPixmapItem *>(&item))
qgraphicsitem_cast<QGraphicsPixmapItem *>(&item)->setTransformationMode(Qt::SmoothTransformation);
if (qgraphicsitem_cast<QGraphicsSimpleTextItem *>(&item))
qgraphicsitem_cast<QGraphicsSimpleTextItem *>(&item)->setData(bvidDecorText,
qgraphicsitem_cast<QGraphicsSimpleTextItem *>(&item)->text());
if (qgraphicsitem_cast<AlignedTextItem *>(&item))
qgraphicsitem_cast<AlignedTextItem *>(&item)->setData(bvidDecorText, qgraphicsitem_cast<AlignedTextItem *>(&item)->text());
item.setData(bvidMoveParent, true);
item.setData(bvidBlockDecor, true);
item.setParentItem(this);
@@ -130,22 +127,21 @@ void BlockItem::addDecor(QGraphicsItem & item) {
void BlockItem::removeDecor(QGraphicsItem * item) {
if (scene() && item)
scene()->sendEvent(item, new QGraphicsSceneEvent(QEvent::Close));
if (scene() && item) scene()->sendEvent(item, new QGraphicsSceneEvent(QEvent::Close));
decors_.removeAll(item);
delete item;
}
QVector<BlockItemPin * > BlockItem::takePins() {
QVector<BlockItemPin * > ret = pins();
QVector<BlockItemPin *> BlockItem::takePins() {
QVector<BlockItemPin *> ret = pins();
pins_.clear();
return ret;
}
void BlockItem::clearPins() {
QList<QVector<BlockItemPin * > > mp = pins_.values();
QList<QVector<BlockItemPin *>> mp = pins_.values();
for (int i = 0; i < mp.size(); ++i)
qDeleteAll(mp[i]);
pins_.clear();
@@ -156,7 +152,7 @@ void BlockItem::clearDecors() {
bool pbs = false;
if (scene()) pbs = scene()->blockSignals(true);
if (scene()) {
foreach (QGraphicsItem * i, decors_)
foreach(QGraphicsItem * i, decors_)
scene()->sendEvent(i, new QGraphicsSceneEvent(QEvent::Close));
}
qDeleteAll(decors_);
@@ -168,9 +164,9 @@ void BlockItem::clearDecors() {
}
QVector<BlockItemPin * > BlockItem::pins() const {
QList<QVector<BlockItemPin * > > mp = pins_.values();
QVector<BlockItemPin * > ret;
QVector<BlockItemPin *> BlockItem::pins() const {
QList<QVector<BlockItemPin *>> mp = pins_.values();
QVector<BlockItemPin *> ret;
for (int i = 0; i < mp.size(); ++i)
ret << mp[i];
return ret;
@@ -186,15 +182,15 @@ QByteArray BlockItem::saveModel() {
void BlockItem::loadModel(const QByteArray & data) {
//qDebug() << "load from" << data.size() << "bytes";
// qDebug() << "load from" << data.size() << "bytes";
clearPins();
clearDecors();
col = Qt::lightGray;
_resize(QSizeF(100., 60.));
if (data.isEmpty()) return;
ChunkStream cs(data);
QVector<BlockItemPin * > tp;
QList<QGraphicsItem * > dl;
QVector<BlockItemPin *> tp;
QList<QGraphicsItem *> dl;
int version = -1;
while (!cs.atEnd()) {
switch (cs.read()) {
@@ -204,30 +200,32 @@ void BlockItem::loadModel(const QByteArray & data) {
case 4: setColor(cs.getData<QColor>()); break;
case 5:
cs.get(tp);
foreach (BlockItemPin * p, tp) addPin(p);
foreach(BlockItemPin * p, tp)
addPin(p);
break;
case 6:
cs.get(dl);
foreach (QGraphicsItem * d, dl) addDecor(d);
foreach(QGraphicsItem * d, dl)
addDecor(d);
break;
case 7: setPinsMargin(cs.getData<int>()); break;
case 0xFF: cs.get(version); break;
}
}
if (version <= 0)
_moveToTop(true);
if (version <= 0) _moveToTop(true);
}
QByteArray BlockItem::save() const {
ChunkStream cs;
QMap<QString, QList<BlockItem::Property> > pp;
foreach (BlockItemPin * p, pins()) {
//qDebug() << "save pin" << p->text() << "->" << p->properties().size();
QMap<QString, QList<BlockItem::Property>> pp;
foreach(BlockItemPin * p, pins()) {
// qDebug() << "save pin" << p->text() << "->" << p->properties().size();
pp[p->text()] = p->properties();
}
cs << cs.chunk(1, pos()) << cs.chunk(2, rotation()) << cs.chunk(3, props) << cs.chunk(5, pp) << cs.chunk(6, size());
cs << cs.chunk(10, data(2000)) << cs.chunk(11, data(2001)) << cs.chunk(12, prop_bindings) << cs.chunk(0xFF, _blockitem_current_version_);
cs << cs.chunk(10, data(2000)) << cs.chunk(11, data(2001)) << cs.chunk(12, prop_bindings)
<< cs.chunk(0xFF, _blockitem_current_version_);
return cs.data();
}
@@ -235,7 +233,7 @@ QByteArray BlockItem::save() const {
void BlockItem::load(const QByteArray & data) {
if (data.isEmpty()) return;
ChunkStream cs(data);
QMap<QString, QList<BlockItem::Property> > _p;
QMap<QString, QList<BlockItem::Property>> _p;
int version = -1;
while (!cs.atEnd()) {
switch (cs.read()) {
@@ -244,17 +242,16 @@ void BlockItem::load(const QByteArray & data) {
case 3: cs.get(props); break;
case 5:
cs.get(_p);
//qDebug() << "load pins" << _p.size();
foreach (BlockItemPin * p, pins()) {
//qDebug() << "load pin" << p->text() << "->" << _p.contains(p->text());
if (_p.contains(p->text()))
p->properties() = _p[p->text()];
// qDebug() << "load pins" << _p.size();
foreach(BlockItemPin * p, pins()) {
// qDebug() << "load pin" << p->text() << "->" << _p.contains(p->text());
if (_p.contains(p->text())) p->properties() = _p[p->text()];
}
break;
case 6: setSize(cs.getData<QSizeF>()); break;
case 10: setData(2000, cs.getData<QVariant>()); break;
case 11: setData(2001, cs.getData<QVariant>()); break;
case 12: prop_bindings = cs.getData<QList<QPair<QString, QString> > >(); break;
case 12: prop_bindings = cs.getData<QList<QPair<QString, QString>>>(); break;
case 0xFF: cs.get(version); break;
}
}
@@ -272,8 +269,8 @@ BlockItem * BlockItem::copy() const {
ret->setSelected(false);
ret->props = props;
ret->setPinsMargin(pinsMargin());
QVector<BlockItemPin * > mp = pins();
foreach (BlockItemPin * p, mp) {
QVector<BlockItemPin *> mp = pins();
foreach(BlockItemPin * p, mp) {
BlockItemPin * np = new BlockItemPin();
np->setBusType(p->busType());
np->setAlignment(p->alignment());
@@ -283,39 +280,38 @@ BlockItem * BlockItem::copy() const {
ret->addPin(np);
}
QByteArray ba;
foreach (QGraphicsItem * i, decors_) {
foreach(QGraphicsItem * i, decors_) {
ba.clear();
QGraphicsItem * ni = 0;
QDataStream s(&ba, QIODevice::ReadWrite); s << i;
QDataStream s2(ba); s2 >> ni;
if (ni)
ret->addDecor(ni);
QDataStream s(&ba, QIODevice::ReadWrite);
s << i;
QDataStream s2(ba);
s2 >> ni;
if (ni) ret->addDecor(ni);
}
return ret;
}
QList<BlockBusItem * > BlockItem::connectedBuses() const {
QList<BlockBusItem * > ret;
foreach (BlockItemPin * p, pins())
QList<BlockBusItem *> BlockItem::connectedBuses() const {
QList<BlockBusItem *> ret;
foreach(BlockItemPin * p, pins())
ret << p->connectedBuses();
return ret;
}
BlockItemPin * BlockItem::pinByText(const QString & t) const {
foreach (BlockItemPin * p, pins())
if (p->text() == t)
return p;
foreach(BlockItemPin * p, pins())
if (p->text() == t) return p;
return 0;
}
BlockItemPin * BlockItem::pinAtBus(BlockBusItem * bus) const {
if (bus == 0) return 0;
foreach (BlockItemPin * p, pins())
if (p->connectedBuses().contains(bus))
return p;
foreach(BlockItemPin * p, pins())
if (p->connectedBuses().contains(bus)) return p;
return 0;
}
@@ -337,7 +333,8 @@ void BlockItem::hoverEnterEvent(QGraphicsSceneHoverEvent * e) {
anim_thick.setStartValue(thickness());
anim_thick.setEndValue(2.5);
anim_thick.start();
} else setThickness(2.5);
} else
setThickness(2.5);
emit blockHoverEnter(this);
}
@@ -349,7 +346,8 @@ void BlockItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) {
anim_thick.setStartValue(thickness());
anim_thick.setEndValue(1);
anim_thick.start();
} else setThickness(1);
} else
setThickness(1);
emit blockHoverLeave(this);
}
@@ -357,25 +355,29 @@ void BlockItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) {
#define _POS(m) (i - ((cp.size() - 1) / 2)) * m
void BlockItem::arrangePins() {
QVector<BlockItemPin * > pl = pins();
QVector<BlockItemPin *> pl = pins();
pins_.clear();
foreach (BlockItemPin * p, pl)
foreach(BlockItemPin * p, pl)
pins_[p->alignment()] << p;
QVector<BlockItemPin * > cp = pins_.value(Qt::AlignBottom);
for (int i = 0; i < cp.size(); ++i) cp[i]->setPos(_POS(pins_margin), bottom());
QVector<BlockItemPin *> cp = pins_.value(Qt::AlignBottom);
for (int i = 0; i < cp.size(); ++i)
cp[i]->setPos(_POS(pins_margin), bottom());
cp = pins_.value(Qt::AlignTop);
for (int i = 0; i < cp.size(); ++i) cp[i]->setPos(_POS(pins_margin), top());
for (int i = 0; i < cp.size(); ++i)
cp[i]->setPos(_POS(pins_margin), top());
cp = pins_.value(Qt::AlignLeft);
for (int i = 0; i < cp.size(); ++i) cp[i]->setPos(left(), i * pins_margin);
for (int i = 0; i < cp.size(); ++i)
cp[i]->setPos(left(), i * pins_margin);
cp = pins_.value(Qt::AlignRight);
for (int i = 0; i < cp.size(); ++i) cp[i]->setPos(right(), i * pins_margin);
for (int i = 0; i < cp.size(); ++i)
cp[i]->setPos(right(), i * pins_margin);
}
#undef _POS
void BlockItem::removeBindings(const QString & bind_name) {
for(int i=0; i<prop_bindings.size(); ++i) {
for (int i = 0; i < prop_bindings.size(); ++i) {
if (prop_bindings[i].second == bind_name) {
prop_bindings.removeAt(i);
i--;
@@ -385,7 +387,7 @@ void BlockItem::removeBindings(const QString & bind_name) {
void BlockItem::removeBindingByProperty(const QString & prop_name) {
for(int i=0; i<prop_bindings.size(); ++i) {
for (int i = 0; i < prop_bindings.size(); ++i) {
if (prop_bindings[i].first == prop_name) {
prop_bindings.removeAt(i);
i--;
@@ -402,7 +404,7 @@ void BlockItem::addBinding(const QString & prop_name, const QString & bind_name)
void BlockItem::applyBinding(const QString & bind_name, const QVariant & bind_value) {
for(int i=0; i<prop_bindings.size(); ++i) {
for (int i = 0; i < prop_bindings.size(); ++i) {
if (prop_bindings[i].second == bind_name) {
setPropertyValue(prop_bindings[i].first, bind_value);
}
@@ -411,7 +413,7 @@ void BlockItem::applyBinding(const QString & bind_name, const QVariant & bind_va
void BlockItem::applyBindings(const PropertyStorage & bindings) {
for(int i=0; i<prop_bindings.size(); ++i) {
for (int i = 0; i < prop_bindings.size(); ++i) {
if (bindings.isPropertyExists(prop_bindings[i].second)) {
setPropertyValue(prop_bindings[i].first, bindings.propertyValueByName(prop_bindings[i].second));
}
@@ -419,18 +421,18 @@ void BlockItem::applyBindings(const PropertyStorage & bindings) {
}
void BlockItem::setBindings(const QList<QPair<QString, QString> > & bindings) {
void BlockItem::setBindings(const QList<QPair<QString, QString>> & bindings) {
prop_bindings = bindings;
}
QList<QPair<QString, QString> > BlockItem::getBindings() {
QList<QPair<QString, QString>> BlockItem::getBindings() {
return prop_bindings;
}
QString BlockItem::getBindName(const QString & prop_name) const {
for(int i=0; i<prop_bindings.size(); ++i) {
for (int i = 0; i < prop_bindings.size(); ++i) {
if (prop_bindings[i].first == prop_name) {
return prop_bindings[i].second;
}
@@ -441,14 +443,14 @@ QString BlockItem::getBindName(const QString & prop_name) const {
QStringList BlockItem::getBindNames() const {
QStringList ret;
for(int i=0; i<prop_bindings.size(); ++i)
for (int i = 0; i < prop_bindings.size(); ++i)
if (!ret.contains(prop_bindings[i].second)) ret << prop_bindings[i].second;
return ret;
}
QStringList BlockItem::getBindProps() const {
QStringList ret;
for(int i=0; i<prop_bindings.size(); ++i)
for (int i = 0; i < prop_bindings.size(); ++i)
ret << prop_bindings[i].first;
return ret;
}
@@ -456,7 +458,7 @@ QStringList BlockItem::getBindProps() const {
QVariant BlockItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant & value) {
if (change == QGraphicsItem::ItemSelectedChange) {
// qDebug() << "select" << value.toBool();
// qDebug() << "select" << value.toBool();
if (value.toBool() && !isSelected() && ((BlockView *)scene()->views().back())->isBlockAnimationEnabled() && t_sel.elapsed() > 50) {
g_selection.setRect(enlargedRect(g_main.rect(), 0, 0, 16));
anim_sel.setStartValue(selectionRect());
@@ -490,4 +492,3 @@ QRectF BlockItem::selectionRect() const {
void BlockItem::setSelectionRect(const QRectF & r) {
g_selection.setRect(r);
}

View File

@@ -1,38 +1,41 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLOCKITEM_H
#define BLOCKITEM_H
#include <QElapsedTimer>
#include "blockitempin.h"
#include "qad_blockview_export.h"
#include <QElapsedTimer>
class QAD_BLOCKVIEW_EXPORT BlockItem: public QGraphicsObject, public PropertyStorage
{
class QAD_BLOCKVIEW_EXPORT BlockItem
: public QGraphicsObject
, public PropertyStorage {
friend class BlockView;
friend class BlockItemPin;
friend class DrawTools;
Q_OBJECT
Q_PROPERTY(double _thickness READ thickness WRITE setThickness DESIGNABLE false SCRIPTABLE false)
Q_PROPERTY(QRectF _selRect READ selectionRect WRITE setSelectionRect DESIGNABLE false SCRIPTABLE false)
public:
BlockItem(QGraphicsItem * parent = 0);
~BlockItem();
@@ -44,26 +47,32 @@ public:
void addDecor(QGraphicsItem * item);
void addDecor(QGraphicsItem & item);
void removeDecor(QGraphicsItem * item);
QVector<BlockItemPin * > takePins();
QVector<BlockItemPin *> takePins();
void clearPins();
void clearDecors();
QVector<BlockItemPin * > pins() const;
QList<QGraphicsItem * > decors() const {return decors_;}
QList<BlockBusItem * > connectedBuses() const;
QVector<BlockItemPin *> pins() const;
QList<QGraphicsItem *> decors() const { return decors_; }
QList<BlockBusItem *> connectedBuses() const;
BlockItemPin * pinByText(const QString & t) const;
BlockItemPin * pinAtBus(BlockBusItem * bus) const;
QColor color() const {return col;}
void setColor(QColor c) {col = c; _resize(size());}
QSizeF size() const {return g_main.rect().size();}
QColor color() const { return col; }
void setColor(QColor c) {
col = c;
_resize(size());
}
QSizeF size() const { return g_main.rect().size(); }
QRectF sceneRect() const;
qreal width() const {return size().width();}
qreal height() const {return size().height();}
int pinsMargin() const {return pins_margin;}
void setSize(QSizeF s) {_resize(s);}
void setSize(qreal w, qreal h) {setSize(QSizeF(w, h));}
void setWidth(qreal w) {setSize(QSizeF(w, size().height()));}
void setHeight(qreal h) {setSize(QSizeF(size().width(), h));}
void setPinsMargin(int marg) {if (marg > 1 && marg < 256) pins_margin = marg; arrangePins();}
qreal width() const { return size().width(); }
qreal height() const { return size().height(); }
int pinsMargin() const { return pins_margin; }
void setSize(QSizeF s) { _resize(s); }
void setSize(qreal w, qreal h) { setSize(QSizeF(w, h)); }
void setWidth(qreal w) { setSize(QSizeF(w, size().height())); }
void setHeight(qreal h) { setSize(QSizeF(size().width(), h)); }
void setPinsMargin(int marg) {
if (marg > 1 && marg < 256) pins_margin = marg;
arrangePins();
}
QByteArray saveModel();
void loadModel(const QByteArray & data);
@@ -76,34 +85,36 @@ public:
void addBinding(const QString & prop_name, const QString & bind_name);
void applyBinding(const QString & bind_name, const QVariant & bind_value);
void applyBindings(const PropertyStorage & bindings);
void setBindings(const QList<QPair<QString, QString> > & bindings);
QList<QPair<QString, QString> > getBindings();
void setBindings(const QList<QPair<QString, QString>> & bindings);
QList<QPair<QString, QString>> getBindings();
QString getBindName(const QString & prop_name) const;
QStringList getBindNames() const;
QStringList getBindProps() const;
enum {Type = UserType + 1};
enum {
Type = UserType + 1
};
protected:
void _resize(QSizeF s);
void _moveToTop(bool only_decors = false);
int type() const {return Type;}
int type() const { return Type; }
QRectF boundingRect() const;
void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent * event);
double left() const {return boundingRect().left();}
double right() const {return boundingRect().right();}
double top() const {return boundingRect().top();}
double bottom() const {return boundingRect().bottom();}
double left() const { return boundingRect().left(); }
double right() const { return boundingRect().right(); }
double top() const { return boundingRect().top(); }
double bottom() const { return boundingRect().bottom(); }
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = nullptr) {}
QVariant itemChange(GraphicsItemChange change, const QVariant & value);
QGraphicsRectItem g_main, g_selection;
int pins_margin;
QColor col;
QMap<Qt::Alignment, QVector<BlockItemPin * > > pins_;
QList<QGraphicsItem * > decors_;
QList<QPair<QString, QString> > prop_bindings; // <property_name, binding_name>
QMap<Qt::Alignment, QVector<BlockItemPin *>> pins_;
QList<QGraphicsItem *> decors_;
QList<QPair<QString, QString>> prop_bindings; // <property_name, binding_name>
private:
double thickness() const;
@@ -120,28 +131,34 @@ signals:
};
inline QDataStream & operator <<(QDataStream & s, const BlockItemPin * p) {
inline QDataStream & operator<<(QDataStream & s, const BlockItemPin * p) {
ChunkStream cs;
cs << cs.chunk(1, int(p->alignment())) << cs.chunk(2, p->busType()) << cs.chunk(3, p->text()) << cs.chunk(4, p->toolTip());
s << cs.data(); return s;}
inline QDataStream & operator >>(QDataStream & s, BlockItemPin *& p) {
s << cs.data();
return s;
}
inline QDataStream & operator>>(QDataStream & s, BlockItemPin *& p) {
ChunkStream cs(s);
p = new BlockItemPin();
while (!cs.atEnd()) {
switch (cs.read()) {
case 1: p->setAlignment((Qt::Alignment)cs.getData<int>()); break;
case 2: p->setBusType(cs.getData<int>()); break;
case 3: p->setText(cs.getData<QString>()); break;
case 4: p->setToolTip(cs.getData<QString>()); break;
case 1: p->setAlignment((Qt::Alignment)cs.getData<int>()); break;
case 2: p->setBusType(cs.getData<int>()); break;
case 3: p->setText(cs.getData<QString>()); break;
case 4: p->setToolTip(cs.getData<QString>()); break;
}
}
return s;
}
inline QDataStream & operator <<(QDataStream & s, const BlockItem * b) {s << b->save(); return s;}
inline QDataStream & operator >>(QDataStream & s, BlockItem *& b) {
QByteArray ba; s >> ba;
inline QDataStream & operator<<(QDataStream & s, const BlockItem * b) {
s << b->save();
return s;
}
inline QDataStream & operator>>(QDataStream & s, BlockItem *& b) {
QByteArray ba;
s >> ba;
b = new BlockItem();
b->load(ba);
return s;

View File

@@ -1,19 +1,23 @@
#include "blockview.h"
#include <QApplication>
BlockItemPin::BlockItemPin(Qt::Alignment a, int bus_type_, const QString & text_, QGraphicsObject * _parent): QGraphicsObject(_parent), ell_item(this), text_item(this) {
BlockItemPin::BlockItemPin(Qt::Alignment a, int bus_type_, const QString & text_, QGraphicsObject * _parent)
: QGraphicsObject(_parent)
, ell_item(this)
, text_item(this) {
parent_ = 0;
setData(bvidType, bvitPin);
setAcceptHoverEvents(true);
text_item.setData(bvidMoveParent, true);
ell_item.setData(bvidVisualizeSelection, true);
br[Disconnected] = QBrush(Qt::lightGray);
br[Connected] = QBrush(Qt::darkGreen);
br[Hover] = QBrush(Qt::blue);
br[Drop] = QBrush(Qt::green);
br[Accept] = QBrush(Qt::green);
br[Reject] = QBrush(Qt::red);
br[Connected] = QBrush(Qt::darkGreen);
br[Hover] = QBrush(Qt::blue);
br[Drop] = QBrush(Qt::green);
br[Accept] = QBrush(Qt::green);
br[Reject] = QBrush(Qt::red);
anim_pin_size.setTargetObject(this);
anim_pin_size.setPropertyName("pinSize");
anim_pin_size.setEasingCurve(QEasingCurve::OutElastic);
@@ -42,7 +46,7 @@ void BlockItemPin::animAccept() {
void BlockItemPin::setState(State s) {
State os = state_;
state_ = s;
state_ = s;
setBrush(br[int(state_)]);
if (s == Accept && os != Accept) {
animAccept();
@@ -61,8 +65,7 @@ void BlockItemPin::enlargePin(bool enlarge) {
resizePin(sz);
return;
}
if (sz == anim_pin_size.endValue())
return;
if (sz == anim_pin_size.endValue()) return;
anim_pin_size.stop();
anim_pin_size.setStartValue(pinSize());
anim_pin_size.setEndValue(sz);
@@ -71,7 +74,7 @@ void BlockItemPin::enlargePin(bool enlarge) {
void BlockItemPin::resizePin(double r) {
ell_item.setRect(-r, -r, r+r, r+r);
ell_item.setRect(-r, -r, r + r, r + r);
}
@@ -90,29 +93,44 @@ void BlockItemPin::animationAccept() {
void BlockItemPin::_init(bool affect_parent) {
text_item.setFont(AlignedTextItem::sceneFont(QApplication::font()));
QRectF tbr = text_item.boundingRect();
QRectF tbr = text_item.boundingRect();
const double r = 7.;
ell_item.setRect(-r, -r, r+r, r+r);
ell_item.setSpanAngle(16*180);
ell_item.setRect(-r, -r, r + r, r + r);
ell_item.setSpanAngle(16 * 180);
text_item.resetTransform();
text_item.setPos(0, -tbr.height() / 2.);
text_item.setTransformOriginPoint(0, tbr.height() / 2.);
switch (align) {
case Qt::AlignBottom: ell_item.setStartAngle(16*0); text_item.setRotation(-90.); text_item.moveBy(0, -r * 1.5); break;
case Qt::AlignRight: ell_item.setStartAngle(16*90); text_item.setRotation(0.); text_item.moveBy(-tbr.width() - r * 1.5, 0); break;
case Qt::AlignTop: ell_item.setStartAngle(16*180); text_item.setRotation(-90.); text_item.moveBy(0, tbr.width() + r * 1.5); break;
case Qt::AlignLeft: ell_item.setStartAngle(16*270); text_item.setRotation(0.); text_item.moveBy(r * 1.5, 0); break;
case Qt::AlignBottom:
ell_item.setStartAngle(16 * 0);
text_item.setRotation(-90.);
text_item.moveBy(0, -r * 1.5);
break;
case Qt::AlignRight:
ell_item.setStartAngle(16 * 90);
text_item.setRotation(0.);
text_item.moveBy(-tbr.width() - r * 1.5, 0);
break;
case Qt::AlignTop:
ell_item.setStartAngle(16 * 180);
text_item.setRotation(-90.);
text_item.moveBy(0, tbr.width() + r * 1.5);
break;
case Qt::AlignLeft:
ell_item.setStartAngle(16 * 270);
text_item.setRotation(0.);
text_item.moveBy(r * 1.5, 0);
break;
default: break;
}
if (affect_parent && parent_)
parent_->arrangePins();
if (affect_parent && parent_) parent_->arrangePins();
}
void BlockItemPin::_reparent() {
if (parentItem() == 0) return;
if (qgraphicsitem_cast<BlockItem*>(parentItem()) == 0) return;
QPen p = qgraphicsitem_cast<BlockItem*>(parentItem())->g_main.pen();
if (qgraphicsitem_cast<BlockItem *>(parentItem()) == 0) return;
QPen p = qgraphicsitem_cast<BlockItem *>(parentItem())->g_main.pen();
ell_item.setPen(p);
if (scene()) {
text_item.setFont(AlignedTextItem::sceneFont(scene()->font()));
@@ -132,18 +150,20 @@ QGraphicsView * BlockItemPin::_view() const {
QVariant BlockItemPin::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant & value) {
if (change == QGraphicsItem::ItemParentChange)
_reparent();
if (change == QGraphicsItem::ItemParentChange) _reparent();
return QGraphicsItem::itemChange(change, value);
}
void BlockItemPin::hoverEnterEvent(QGraphicsSceneHoverEvent * e) {
QGraphicsView * v = _view();
bool m_pin_mc = false;
bool m_pin_mc = false;
if (v) {
qobject_cast<BlockView*>(v)->getPinMC(&m_pin_mc);
QMetaObject::invokeMethod(v, [this, v](){qobject_cast<BlockView*>(v)->pinHoverInOut(this);}, Qt::QueuedConnection);
qobject_cast<BlockView *>(v)->getPinMC(&m_pin_mc);
QMetaObject::invokeMethod(
v,
[this, v]() { qobject_cast<BlockView *>(v)->pinHoverInOut(this); },
Qt::QueuedConnection);
}
if ((state() != Disconnected) && !m_pin_mc) return;
saveState();
@@ -159,6 +179,6 @@ void BlockItemPin::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) {
enlargePin(false);
update();
if (v) {
qobject_cast<BlockView*>(v)->pinHoverInOut(nullptr);
qobject_cast<BlockView *>(v)->pinHoverInOut(nullptr);
}
}

View File

@@ -1,50 +1,53 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLOCKITEMPIN_H
#define BLOCKITEMPIN_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsObject>
#include "alignedtextitem.h"
#include "blockbase.h"
#include "qad_blockview_export.h"
#include <QDebug>
#include <QGraphicsEllipseItem>
#include <QGraphicsObject>
#include <QGraphicsScene>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QStack>
#include <QDebug>
#include <QGraphicsView>
#include <QPropertyAnimation>
#include <QStack>
#include <qmath.h>
#include "blockbase.h"
#include "alignedtextitem.h"
#include "qad_blockview_export.h"
class BlockItem;
class BlockBusItem;
class QAD_BLOCKVIEW_EXPORT BlockItemPin: public QGraphicsObject, public PropertyStorage
{
class QAD_BLOCKVIEW_EXPORT BlockItemPin
: public QGraphicsObject
, public PropertyStorage {
friend class BlockView;
friend class BlockItem;
Q_OBJECT
Q_PROPERTY(double pinSize READ pinSize WRITE resizePin DESIGNABLE false SCRIPTABLE false)
public:
BlockItemPin(Qt::Alignment a = Qt::AlignLeft, int bus_type = 0, const QString & text_ = QString(), QGraphicsObject * parent_ = 0);
@@ -58,45 +61,59 @@ public:
};
enum Direction {
None = 0x0,
Input = 0x1,
Output = 0x2,
None = 0x0,
Input = 0x1,
Output = 0x2,
InputOutput = 0x3
};
void setPen(const QPen & p) {ell_item.setPen(p);}
QPen pen() const {return ell_item.pen();}
void setBrush(const QBrush & b) {ell_item.setBrush(b);}
QBrush brush() const {return ell_item.brush();}
void setPen(const QPen & p) { ell_item.setPen(p); }
QPen pen() const { return ell_item.pen(); }
void setBrush(const QBrush & b) { ell_item.setBrush(b); }
QBrush brush() const { return ell_item.brush(); }
int busType() const {return bus_type;}
Qt::Alignment alignment() const {return align;}
QString text() const {return text_item.text();}
State state() const {return state_;}
int busType() const { return bus_type; }
Qt::Alignment alignment() const { return align; }
QString text() const { return text_item.text(); }
State state() const { return state_; }
void setBusType(int type_) {bus_type = type_;}
void setAlignment(Qt::Alignment a) {align = a; _init(true);}
void setText(const QString & t) {text_item.setText(t); _init(true);}
void setBusType(int type_) { bus_type = type_; }
void setAlignment(Qt::Alignment a) {
align = a;
_init(true);
}
void setText(const QString & t) {
text_item.setText(t);
_init(true);
}
void setState(State s);
void saveState() {sstate_.push(state_);}
bool restoreState() {if (sstate_.isEmpty()) return false; setState(sstate_.pop()); return true;}
void clearStateStack() {sstate_.clear();}
void saveState() { sstate_.push(state_); }
bool restoreState() {
if (sstate_.isEmpty()) return false;
setState(sstate_.pop());
return true;
}
void clearStateStack() { sstate_.clear(); }
void enlargePin(bool enlarge);
BlockItem * parent() const {return parent_;}
QList<BlockBusItem * > connectedBuses() const {return buses_;}
BlockItem * parent() const { return parent_; }
QList<BlockBusItem *> connectedBuses() const { return buses_; }
enum {Type = UserType + 3};
enum {
Type = UserType + 3
};
public slots:
void animAccept();
protected:
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0) {}
QRectF boundingRect() const {return ell_item.boundingRect().translated(ell_item.pos()) | text_item.boundingRect().translated(text_item.pos());}
int type() const {return Type;}
QRectF boundingRect() const {
return ell_item.boundingRect().translated(ell_item.pos()) | text_item.boundingRect().translated(text_item.pos());
}
int type() const { return Type; }
QVariant itemChange(GraphicsItemChange change, const QVariant & value);
void hoverEnterEvent(QGraphicsSceneHoverEvent * e);
void hoverLeaveEvent(QGraphicsSceneHoverEvent * e);
@@ -108,7 +125,7 @@ protected:
QGraphicsEllipseItem ell_item;
QGraphicsSimpleTextItem text_item;
QStack<State> sstate_;
QList<BlockBusItem * > buses_;
QList<BlockBusItem *> buses_;
BlockItem * parent_;
Qt::Alignment align;
QBrush br[6];
@@ -122,7 +139,6 @@ private:
QPropertyAnimation anim_pin_size;
QPropertyAnimation anim_accept;
};

View File

@@ -1,15 +1,17 @@
#include "blockview.h"
#include "qad_types.h"
#include <qmath.h>
#include <QScrollBar>
#include <QGraphicsSceneMouseEvent>
#include <QApplication>
#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QElapsedTimer>
#include <QGraphicsSceneMouseEvent>
#include <QMimeData>
#include <QScrollBar>
#include <QShortcut>
#include <QVector2D>
#include <QClipboard>
#include <QMimeData>
#include <QElapsedTimer>
#include <qmath.h>
const QString _BlockView_Mime_ = "_BlockView_copypaste_";
@@ -24,33 +26,32 @@ BlockView::BlockView(QGraphicsScene * scene, QWidget * parent): QGraphicsView(sc
}
BlockView::~BlockView() {
}
BlockView::~BlockView() {}
void BlockView::_init() {
qRegisterMetaType<BlockItem*>();
qRegisterMetaType<BlockItemPin*>();
qRegisterMetaType<BlockBusItem*>();
qRegisterMetaType<BlockItem *>();
qRegisterMetaType<BlockItemPin *>();
qRegisterMetaType<BlockBusItem *>();
grid_visible = grid_snap = pm_connect = navigation = m_connect = m_trace_with_buses = prev_tcb = minimap = true;
mm_drag = moved = new_branch = new_bus = mm_cancel = iconnect = mm_copy = m_pin_mc = mm_thumb = move_bus_point = wheel_zoom = false;
match_bus = bus_from = cur_bus = 0;
mm_ci = 0;
hpin = 0;
ghost_ = 0;
grid_step = 10.;
grid_points = 1;
grid_pen = QPen(palette().color(QPalette::Disabled, QPalette::WindowText), 1, Qt::NoPen);
thick = 1;
thumb_hide_delay = 500;
timer_thumb = 0;
smode = BlockView::MultiSelection;
mm_ci = 0;
hpin = 0;
ghost_ = 0;
grid_step = 10.;
grid_points = 1;
grid_pen = QPen(palette().color(QPalette::Disabled, QPalette::WindowText), 1, Qt::NoPen);
thick = 1;
thumb_hide_delay = 500;
timer_thumb = 0;
smode = BlockView::MultiSelection;
cur_scl = thumb_scl = prev_app_scale = 1.;
_talpha = 0.;
_talpha = 0.;
ae_enabled = is_block_anim = is_nav_anim = true;
nav_prev_aa = nav_prev_imaa = nav_prev_grid = true;
square_node = block_emit_selection = false;
thumb_size = QSizeF(200, 200);
thumb_size = QSizeF(200, 200);
if (scene() == 0) {
scene_ = new QGraphicsScene;
setScene(scene_);
@@ -120,18 +121,12 @@ bool BlockView::event(QEvent * e) {
bool BlockView::eventFilter(QObject * o, QEvent * e) {
if (o == &widget_thumb) {
QMouseEvent * me = (QMouseEvent*)e;
if(!me) return true;
QMouseEvent * me = (QMouseEvent *)e;
if (!me) return true;
switch (e->type()) {
case QEvent::Paint:
drawThumb();
return true;
case QEvent::Enter:
thumbShow();
break;
case QEvent::Leave:
restartTimer(timer_thumb, thumb_hide_delay);
break;
case QEvent::Paint: drawThumb(); return true;
case QEvent::Enter: thumbShow(); break;
case QEvent::Leave: restartTimer(timer_thumb, thumb_hide_delay); break;
case QEvent::MouseButtonPress:
thumb_press = me->pos();
widget_thumb.setCursor(Qt::ClosedHandCursor);
@@ -140,12 +135,9 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
scrollFromThumb();
}
break;
case QEvent::MouseButtonRelease:
widget_thumb.setCursor(Qt::OpenHandCursor);
break;
case QEvent::MouseButtonRelease: widget_thumb.setCursor(Qt::OpenHandCursor); break;
case QEvent::MouseMove:
if (me->buttons() == 0)
widget_thumb.setCursor(thumb_vr.contains(me->pos()) ? Qt::OpenHandCursor : Qt::CrossCursor);
if (me->buttons() == 0) widget_thumb.setCursor(thumb_vr.contains(me->pos()) ? Qt::OpenHandCursor : Qt::CrossCursor);
if (me->buttons().testFlag(Qt::LeftButton)) {
thumb_vr.translate(me->pos() - thumb_press);
scrollFromThumb();
@@ -157,24 +149,30 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
return QGraphicsView::eventFilter(o, e);
}
if (o == scene_) {
QGraphicsSceneMouseEvent * me = (QGraphicsSceneMouseEvent*)e;
QList<QGraphicsItem * > mil;
QGraphicsSceneMouseEvent * me = (QGraphicsSceneMouseEvent *)e;
QList<QGraphicsItem *> mil;
QPointF mdp;
bool fmm_drag = false;
int btncnt = 0;
int btncnt = 0;
switch (e->type()) {
case QEvent::GraphicsSceneMouseDoubleClick:
scene_point = me->scenePos();
mil = scene_->items(scene_point);
foreach (QGraphicsItem * i, mil) {
mil = scene_->items(scene_point);
foreach(QGraphicsItem * i, mil) {
if (i->data(bvidTmpItem).toBool()) continue;
if (i->data(bvidType).toInt() == bvitBlock) {
QMetaObject::invokeMethod(this, [this, i](){blockDoubleClicked(qgraphicsitem_cast<BlockItem*>(i));}, Qt::QueuedConnection);
QMetaObject::invokeMethod(
this,
[this, i]() { blockDoubleClicked(qgraphicsitem_cast<BlockItem *>(i)); },
Qt::QueuedConnection);
return true;
}
if (i->data(bvidType).toInt() == bvitBus) {
if (qgraphicsitem_cast<BlockBusItem*>(i)->isBusSelected()) {
QMetaObject::invokeMethod(this, [this, i](){busDoubleClicked(qgraphicsitem_cast<BlockBusItem*>(i));}, Qt::QueuedConnection);
if (qgraphicsitem_cast<BlockBusItem *>(i)->isBusSelected()) {
QMetaObject::invokeMethod(
this,
[this, i]() { busDoubleClicked(qgraphicsitem_cast<BlockBusItem *>(i)); },
Qt::QueuedConnection);
return true;
}
}
@@ -187,23 +185,23 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
break;
}
}
if (me->buttons().testFlag(Qt::LeftButton )) btncnt++;
if (me->buttons().testFlag(Qt::LeftButton)) btncnt++;
if (me->buttons().testFlag(Qt::RightButton)) btncnt++;
if (me->buttons().testFlag(QT_MID_BUTTON )) btncnt++;
if (me->buttons().testFlag(QT_MID_BUTTON)) btncnt++;
mm_cancel = btncnt >= 2;
match_bus = bus_from = 0;
hpin = 0;
copy_dp = QPointF();
//qDebug() << mm_cancel << moved << sel_rect.isVisible();
hpin = 0;
copy_dp = QPointF();
// qDebug() << mm_cancel << moved << sel_rect.isVisible();
if (sel_rect.isVisible()) {
QList<QGraphicsItem*> gi = scene_->items();
block_emit_selection = true;
foreach (QGraphicsItem * i, gi)
QList<QGraphicsItem *> gi = scene_->items();
block_emit_selection = true;
foreach(QGraphicsItem * i, gi)
i->setSelected(i->data(bvidSelected).toBool());
block_emit_selection = false;
emit selectionChanged();
}
//qDebug() << "cur_bus" << cur_bus;
// qDebug() << "cur_bus" << cur_bus;
if (cur_bus) {
return false;
}
@@ -235,30 +233,30 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
}
mm_mods = me->modifiers();
mm_drag = moved = false;
screen_point = me->screenPos();
scene_point = me->scenePos();
screen_point = me->screenPos();
scene_point = me->scenePos();
if ((me->button() == QT_MID_BUTTON) || (me->button() == Qt::RightButton)) {
thumbShow();
restartTimer(timer_thumb, thumb_hide_delay);
return true;
}
mil = scene_->items(scene_point);
//qDebug() << "mil" << mil;
// qDebug() << "mil" << mil;
while (!mil.isEmpty()) {
mm_ci = mil.front();
if (mm_ci->data(bvidDTHandle).toBool()) return QGraphicsView::eventFilter(o, e);
if (mm_ci->data(bvidTmpItem).toBool() || mm_ci->data(bvidItemSelection).toBool()) {
mil.pop_front();
} else break;
} else
break;
}
if (mil.isEmpty()) {
mm_ci = 0;
return true;
}
while (mm_ci->data(bvidType).toInt() == bvitBus) {
if (qgraphicsitem_cast<BlockBusItem*>(mm_ci))
if (qgraphicsitem_cast<BlockBusItem*>(mm_ci)->isBusSelected())
break;
if (qgraphicsitem_cast<BlockBusItem *>(mm_ci))
if (qgraphicsitem_cast<BlockBusItem *>(mm_ci)->isBusSelected()) break;
if (mil.size() > 1) {
mm_ci = mil[1];
mil.pop_front();
@@ -272,8 +270,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
mm_ci = mil[1];
mil.pop_front();
if (mm_ci->data(bvidVisualizeSelection).toBool())
if (mil.size() > 1)
mm_ci = mil[1];
if (mil.size() > 1) mm_ci = mil[1];
} else
mm_ci = 0;
}
@@ -282,10 +279,10 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
while (mm_ci->parentItem() != 0)
mm_ci = mm_ci->parentItem();
if (!ti->data(bvidDecorText).toString().isEmpty()) { // text item, check for rect
BlockItem * bi = qgraphicsitem_cast<BlockItem*>(mm_ci);
BlockItem * bi = qgraphicsitem_cast<BlockItem *>(mm_ci);
if (bi) {
if (!bi->sceneRect().contains(scene_point)) {
//qDebug() << "return";
// qDebug() << "return";
mm_ci = 0;
}
}
@@ -294,27 +291,27 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
}
if (mm_ci) {
if ((mm_ci->data(bvidType).toInt() == bvitPin) && m_connect) {
if (qgraphicsitem_cast<BlockItemPin*>(mm_ci)->state() == BlockItemPin::Hover) {
if (qgraphicsitem_cast<BlockItemPin *>(mm_ci)->state() == BlockItemPin::Hover) {
trace_from = mm_ci->scenePos();
qgraphicsitem_cast<BlockItemPin*>(mm_ci)->clearStateStack();
qgraphicsitem_cast<BlockItemPin *>(mm_ci)->clearStateStack();
hideTmpBuses();
tmp_bus.setBusType(qgraphicsitem_cast<BlockItemPin*>(mm_ci)->busType());
tmp_bus.setBusType(qgraphicsitem_cast<BlockItemPin *>(mm_ci)->busType());
tmp_bus.setEndpointsNumber(3);
tmp_bus.show();
new_bus = true;
qDeleteAll(tmp_buses);
tmp_buses.clear();
foreach (BlockItemPin * p, last_multiconnect_pl) {
foreach(BlockItemPin * p, last_multiconnect_pl) {
tmp_buses << new BlockBusItem(true);
tmp_buses.back()->setBusType(p->busType());
addItem(tmp_buses.back());
}
//qDebug() << "new" << ;
// qDebug() << "new" << ;
prev_tcb = m_trace_with_buses;
newBusStarted(tmp_bus.busType());
markPins(tmp_bus.busType());
if (qgraphicsitem_cast<BlockItemPin*>(mm_ci)->alignment() == Qt::AlignLeft ||
qgraphicsitem_cast<BlockItemPin*>(mm_ci)->alignment() == Qt::AlignRight)
if (qgraphicsitem_cast<BlockItemPin *>(mm_ci)->alignment() == Qt::AlignLeft ||
qgraphicsitem_cast<BlockItemPin *>(mm_ci)->alignment() == Qt::AlignRight)
wavetrace.setPreferredDirection(BlockViewWavetrace::Horizontal);
else
wavetrace.setPreferredDirection(BlockViewWavetrace::Vertical);
@@ -325,11 +322,11 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
cur_scl = qSqrt(transform().determinant());
break;
case QEvent::GraphicsSceneMouseMove:
//qDebug() << "move" << (mm_ci != 0 ? mm_ci : 0) << mm_mods << mm_cancel << mm_drag;
// qDebug() << "move" << (mm_ci != 0 ? mm_ci : 0) << mm_mods << mm_cancel << mm_drag;
if (cur_bus) {
return false;
}
//qDebug() << "move mm_ci" << mm_ci << mm_cancel;
// qDebug() << "move mm_ci" << mm_ci << mm_cancel;
if (mm_ci)
if (mm_ci->data(bvidTmpItem).toBool()) {
mm_ci = 0;
@@ -346,19 +343,21 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
}
} else {
if (tmp_bus.isVisible()) {
mil = scene_->items(me->scenePos());
mil = scene_->items(me->scenePos());
hpin = 0;
foreach (QGraphicsItem * i, mil)
foreach(QGraphicsItem * i, mil)
if (i->data(bvidType).toInt() == bvitPin) {
hpin = qgraphicsitem_cast<BlockItemPin*>(i);
hpin = qgraphicsitem_cast<BlockItemPin *>(i);
break;
}
if (hpin) {
if (hpin->state() == BlockItemPin::Accept) {
unhoverPins(hpin);
hoverAcceptedPin(hpin, true);
} else hpin = 0;
} else unhoverPins();
} else
hpin = 0;
} else
unhoverPins();
if (new_branch) {
matchBus();
break;
@@ -366,7 +365,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
trace(trace_from, me->scenePos(), &tmp_bus);
for (int i = 0; i < qMin(tmp_buses.size(), last_multiconnect_pl.size()); ++i) {
QPointF dp = last_multiconnect_pl[i]->scenePos() - trace_from;
//qDebug() << "trace" << i << dp;
// qDebug() << "trace" << i << dp;
trace(trace_from + dp, me->scenePos() + dp, tmp_buses[i], false);
tmp_buses[i]->show();
}
@@ -378,7 +377,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
if (fmm_drag) {
fmm_drag = false;
if (mm_ci) {
if ((mm_ci->data(bvidType).toInt() == bvitBlock) && !mm_ci->data(bvidTmpItem).toBool()) {
if ((mm_ci->data(bvidType).toInt() == bvitBlock) && !mm_ci->data(bvidTmpItem).toBool()) {
if (!mm_ci->isSelected() && sel_items.isEmpty()) {
clearSelection();
mm_ci->setSelected(true);
@@ -392,21 +391,22 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
}
sel_items = scene_->selectedItems();
deleteCopyTemp();
QList<BlockItem * > bi;
foreach (QGraphicsItem * i, sel_items) {
QList<BlockItem *> bi;
foreach(QGraphicsItem * i, sel_items) {
if ((i->data(bvidType).toInt() == bvitBlock) && !i->data(bvidTmpItem).toBool()) {
//qDebug() << "copy";
bi << qgraphicsitem_cast<BlockItem*>(i);
// qDebug() << "copy";
bi << qgraphicsitem_cast<BlockItem *>(i);
BlockItem * ti = bi.back()->copy();
ti->g_main.setPen(QPen(ti->g_main.pen().color(), ti->g_main.pen().widthF(), Qt::DashLine));
QColor bc = ti->g_main.brush().color(); bc.setAlphaF(bc.alphaF() * 0.5);
QColor bc = ti->g_main.brush().color();
bc.setAlphaF(bc.alphaF() * 0.5);
ti->g_main.setBrush(bc);
copy_items << ti;
scene_->addItem(ti);
}
}
QList<BlockBusItem * > ibi = internalBuses(bi);
foreach (BlockBusItem * i, ibi) {
QList<BlockBusItem *> ibi = internalBuses(bi);
foreach(BlockBusItem * i, ibi) {
i = i->copy();
i->setOpacity(i->opacity() * 0.5);
copy_buses << i;
@@ -416,7 +416,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
setCursor(Qt::DragCopyCursor);
}
} else {
//qDebug() << "move smode" << smode;
// qDebug() << "move smode" << smode;
if (smode == BlockView::SingleSelection) {
if (fmm_drag) {
clearSelection();
@@ -432,8 +432,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
if (mm_ci == 0) {
scene_->addItem(&sel_rect);
sel_rect.show();
if (!mm_mods.testFlag(Qt::ControlModifier))
clearSelection();
if (!mm_mods.testFlag(Qt::ControlModifier)) clearSelection();
} else {
if (!mm_mods.testFlag(Qt::ControlModifier) && !mm_ci->isSelected()) {
clearSelection();
@@ -442,8 +441,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
}
saveSelState();
if (mm_ci != 0)
if (!sel_items.contains(mm_ci))
mm_ci = 0;
if (!sel_items.contains(mm_ci)) mm_ci = 0;
}
}
}
@@ -452,46 +450,39 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
else {
if (mm_drag && mm_ci != 0) {
mdp = (me->scenePos() - scene_point);
if (grid_snap)
mdp = quantize(mdp, grid_step);
if (grid_snap) mdp = quantize(mdp, grid_step);
if (!mdp.isNull()) {
scene_point += mdp;
copy_dp += mdp;
}
if (mm_copy) {
if (!mdp.isNull())
moved = true;
foreach (QGraphicsItem * i, copy_items)
if (!mdp.isNull()) moved = true;
foreach(QGraphicsItem * i, copy_items)
i->setPos(i->pos() + mdp);
foreach (BlockBusItem * i, copy_buses)
foreach(BlockBusItem * i, copy_buses)
i->movePolyline(mdp);
}
if (!mm_mods.testFlag(Qt::ControlModifier) && !mm_mods.testFlag(Qt::ShiftModifier)) {
if (!mdp.isNull())
moved = true;
foreach (QGraphicsItem * i, sel_items)
if (i->flags().testFlag(QGraphicsItem::ItemIsMovable))
i->setPos(i->pos() + mdp);
if (!me->modifiers().testFlag(Qt::AltModifier))
moveBuses(sel_items, mdp);
if (!mdp.isNull()) moved = true;
foreach(QGraphicsItem * i, sel_items)
if (i->flags().testFlag(QGraphicsItem::ItemIsMovable)) i->setPos(i->pos() + mdp);
if (!me->modifiers().testFlag(Qt::AltModifier)) moveBuses(sel_items, mdp);
setCursor(Qt::ClosedHandCursor);
}
return true;
}
}
if (mm_ci)
if (mm_ci->data(bvidBlockDecor).toBool())
return true;
if (mm_ci->data(bvidBlockDecor).toBool()) return true;
}
if (me->modifiers().testFlag(Qt::ControlModifier) && me->buttons() != 0 && mm_ci == 0)
return true;
//qDebug() << "scene mouse";
if (me->modifiers().testFlag(Qt::ControlModifier) && me->buttons() != 0 && mm_ci == 0) return true;
// qDebug() << "scene mouse";
break;
case QEvent::GraphicsSceneMouseRelease:
if (me->buttons().testFlag(Qt::LeftButton)) btncnt++;
if (me->buttons().testFlag(Qt::RightButton)) btncnt++;
if (me->buttons().testFlag(QT_MID_BUTTON)) btncnt++;
cur_bus = 0;
cur_bus = 0;
mm_cancel = btncnt > 0;
if (mm_cancel || (me->button() == QT_MID_BUTTON) || (me->button() == Qt::RightButton)) {
mm_ci = 0;
@@ -504,15 +495,15 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
}
if (mm_ci->data(bvidItemSelection).toBool()) break;
if (mm_copy) {
QList<QGraphicsItem*> ai;
QList<QGraphicsItem *> ai;
blockSignals(true);
if (moved) {
QList<BlockItem*> ci;
QList<BlockBusItem*> bi;
foreach (QGraphicsItem * b, sel_items)
QList<BlockItem *> ci;
QList<BlockBusItem *> bi;
foreach(QGraphicsItem * b, sel_items)
if ((b->data(bvidType).toInt() == bvitBlock) && !b->data(bvidTmpItem).toBool()) {
ci << qgraphicsitem_cast<BlockItem*>(b);
ai << qgraphicsitem_cast<QGraphicsItem*>(b);
ci << qgraphicsitem_cast<BlockItem *>(b);
ai << qgraphicsitem_cast<QGraphicsItem *>(b);
}
bi = internalBuses(ci);
if (!ci.isEmpty()) copyBlocks(ci, copy_dp);
@@ -530,10 +521,9 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
hideTmpBuses(false);
}
if (moved && pm_connect) {
QList<QGraphicsItem*> ci;
foreach (QGraphicsItem * b, sel_items)
if (b->data(bvidType).toInt() == bvitBlock)
ci << b;
QList<QGraphicsItem *> ci;
foreach(QGraphicsItem * b, sel_items)
if (b->data(bvidType).toInt() == bvitBlock) ci << b;
simplifyBuses();
emitActionEvent(BlockItemBase::BlockMove, ci);
reconnectAll();
@@ -545,7 +535,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
BlockBusItem * nb = new BlockBusItem(tmp_bus);
addItem(nb, tmp_buses.isEmpty());
newBusCreated(nb);
foreach (BlockBusItem * b, tmp_buses) {
foreach(BlockBusItem * b, tmp_buses) {
nb = new BlockBusItem(*b);
addItem(nb, b == tmp_buses.back());
newBusCreated(nb);
@@ -553,7 +543,7 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
} else {
if (connectTmpToBus(match_bus)) {
newBusCreated(match_bus);
emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem*>() << match_bus);
emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem *>() << match_bus);
emit connectionsChanged();
}
}
@@ -570,34 +560,28 @@ bool BlockView::eventFilter(QObject * o, QEvent * e) {
switch (smode) {
case SingleSelection:
clearSelection();
if (mm_ci)
mm_ci->setSelected(true);
if (mm_ci) mm_ci->setSelected(true);
break;
case MultiSelection:
if (mm_ci == 0 || !me->modifiers().testFlag(Qt::ControlModifier)) {
clearSelection();
if (mm_ci)
mm_ci->setSelected(true);
if (mm_ci) mm_ci->setSelected(true);
} else {
if (mm_ci != 0) {
if (me->modifiers().testFlag(Qt::ControlModifier)) {
if (mm_ci->data(bvidType).toInt() == bvitBlock)
mm_ci->setSelected(!mm_ci->isSelected());
if (mm_ci->data(bvidType).toInt() == bvitBlock) mm_ci->setSelected(!mm_ci->isSelected());
} else
mm_ci->setSelected(true);
}
}
break;
default:
clearSelection();
break;
default: clearSelection(); break;
}
}
sel_rect.hide();
if (sel_rect.scene())
scene_->removeItem(&sel_rect);
if (sel_rect.scene()) scene_->removeItem(&sel_rect);
mm_drag = false;
mm_ci = 0;
mm_ci = 0;
unsetCursor();
break;
default: break;
@@ -637,9 +621,8 @@ void BlockView::wheelEvent(QWheelEvent * e) {
#else
double scl = 1. - e->angleDelta().y() / 500.;
#endif
if (!is_nav_anim || (nav_anim.state() != QPropertyAnimation::Running))
nav_target = _nav();
QRectF r = nav_target;
if (!is_nav_anim || (nav_anim.state() != QPropertyAnimation::Running)) nav_target = _nav();
QRectF r = nav_target;
double vw = viewport()->width(), vh = viewport()->height();
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
double cx = double(e->pos().x()) / vw, cy = double(e->pos().y()) / vh;
@@ -661,8 +644,7 @@ void BlockView::mousePressEvent(QMouseEvent * event) {
press_point = event->pos();
if (event->buttons().testFlag(QT_MID_BUTTON) || event->buttons().testFlag(Qt::RightButton)) {
setCursor(Qt::ClosedHandCursor);
if (sel_rect.scene())
scene_->removeItem(&sel_rect);
if (sel_rect.scene()) scene_->removeItem(&sel_rect);
}
QGraphicsView::mousePressEvent(event);
}
@@ -677,7 +659,7 @@ void BlockView::mouseReleaseEvent(QMouseEvent * event) {
void BlockView::updateNavRect() {
QPointF t = mapToScene(viewport()->rect().topLeft());
QPointF b = mapToScene(viewport()->rect().bottomRight());
nav_rect = QRectF(t, b);
nav_rect = QRectF(t, b);
}
@@ -717,7 +699,7 @@ void BlockView::mouseDoubleClickEvent(QMouseEvent * event) {
void BlockView::keyPressEvent(QKeyEvent * e) {
BlockItemPin * pin = getPin(items(mapFromGlobal(QCursor::pos())));
if (pin) {
//qDebug() << "pin" << pin->state();
// qDebug() << "pin" << pin->state();
if (pin->state() == BlockItemPin::Hover) {
highlightNearPins(pin, e->modifiers());
}
@@ -727,20 +709,20 @@ void BlockView::keyPressEvent(QKeyEvent * e) {
if (e->key() == Qt::Key_Shift) {
retrace = true;
switch (wavetrace.preferredDirection()) {
case BlockViewWavetrace::NoTrace: wavetrace.setPreferredDirection(BlockViewWavetrace::Horizontal); break;
case BlockViewWavetrace::Horizontal: wavetrace.setPreferredDirection(BlockViewWavetrace::Vertical); break;
case BlockViewWavetrace::Vertical: wavetrace.setPreferredDirection(BlockViewWavetrace::NoTrace); break;
case BlockViewWavetrace::NoTrace: wavetrace.setPreferredDirection(BlockViewWavetrace::Horizontal); break;
case BlockViewWavetrace::Horizontal: wavetrace.setPreferredDirection(BlockViewWavetrace::Vertical); break;
case BlockViewWavetrace::Vertical: wavetrace.setPreferredDirection(BlockViewWavetrace::NoTrace); break;
}
}
if (e->key() == Qt::Key_Alt) {
retrace = true;
retrace = true;
m_trace_with_buses = !m_trace_with_buses;
}
if (retrace) {
trace(last_trace_from, trace_to, &tmp_bus);
for (int i = 0; i < qMin(tmp_buses.size(), last_multiconnect_pl.size()); ++i) {
QPointF dp = last_multiconnect_pl[i]->scenePos() - last_trace_from;
//qDebug() << "trace" << i << dp;
// qDebug() << "trace" << i << dp;
trace(last_trace_from + dp, trace_to + dp, tmp_buses[i], false);
tmp_buses[i]->show();
}
@@ -754,7 +736,7 @@ void BlockView::keyPressEvent(QKeyEvent * e) {
void BlockView::keyReleaseEvent(QKeyEvent * e) {
BlockItemPin * pin = getPin(items(mapFromGlobal(QCursor::pos())));
if (pin) {
//qDebug() << "pin" << pin->state();
// qDebug() << "pin" << pin->state();
if (pin->state() == BlockItemPin::Hover) {
highlightNearPins(pin, e->modifiers());
}
@@ -781,7 +763,10 @@ void BlockView::scrollContentsBy(int dx, int dy) {
thumbShow();
restartTimer(timer_thumb, thumb_hide_delay);
QWidget * w = &widget_thumb;
QMetaObject::invokeMethod(this, [w](){w->update();}, Qt::QueuedConnection);
QMetaObject::invokeMethod(
this,
[w]() { w->update(); },
Qt::QueuedConnection);
}
@@ -796,26 +781,26 @@ void BlockView::drawBackground(QPainter * painter, const QRectF & rect) {
sy *= scl;
}
if (!grid_visible) return;
rx = quantize(rect.left(), sx);
ry = quantize(rect.top(), sy);
rx = quantize(rect.left(), sx);
ry = quantize(rect.top(), sy);
bool gp = grid_points > 0.5;
if (gp) {
QPen pp(grid_pen.color(), qMax<int>(grid_points, thick), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
pp.setCosmetic(true);
painter->setPen(pp);
for(int i = 0; i < qCeil(rect.width() / sx) + 1; ++i)
for(int j = 0; j < qCeil(rect.height() / sy) + 1; ++j)
for (int i = 0; i < qCeil(rect.width() / sx) + 1; ++i)
for (int j = 0; j < qCeil(rect.height() / sy) + 1; ++j)
painter->drawPoint(rx + i * sx, ry + j * sy);
}
if (grid_pen.style() == Qt::NoPen) return;
QPen pen = grid_pen;
pen.setWidth(qMax<int>(pen.width(), thick));
painter->setPen(grid_pen);
for(int i = 0; i < qCeil(rect.width() / sx) + 1; ++i) {
for (int i = 0; i < qCeil(rect.width() / sx) + 1; ++i) {
double cx = rx + i * sx;
painter->drawLine(cx, ry, cx, ry + rect.height());
}
for(int j = 0; j < qCeil(rect.height() / sy) + 1; ++j) {
for (int j = 0; j < qCeil(rect.height() / sy) + 1; ++j) {
double cy = ry + j * sy;
painter->drawLine(rx, cy, rx + rect.width(), cy);
}
@@ -838,7 +823,7 @@ void BlockView::deleteCopyTemp() {
}
void BlockView::emitActionEvent(BlockItemBase::Action action, QList<QGraphicsItem * > items) {
void BlockView::emitActionEvent(BlockItemBase::Action action, QList<QGraphicsItem *> items) {
if (!ae_enabled) return;
emit schemeAction(action, items);
}
@@ -868,18 +853,20 @@ void BlockView::getPinMC(bool * v) {
void BlockView::drawThumb() {
if (!minimap) return;
QPainter p(&widget_thumb);
QRect wr = widget_thumb.rect().adjusted(0, 0, -1, -1);
QRect wr = widget_thumb.rect().adjusted(0, 0, -1, -1);
QSizeF sr = sceneRect().size(), tr;
if (sr.width() >= sr.height())
thumb_scl = thumb_size.width() / sr.width();
else
thumb_scl = thumb_size.height() / sr.height();
tr = sr * thumb_scl;
tr = sr * thumb_scl;
cur_scl = qSqrt(transform().determinant());
thumb_scl /= cur_scl;
QSizeF vs(size().width() - verticalScrollBar()->width(), size().height() - horizontalScrollBar()->height());
QRectF vr(QPointF(horizontalScrollBar()->value() - horizontalScrollBar()->minimum(),
verticalScrollBar()->value() - verticalScrollBar()->minimum()) * thumb_scl, vs * thumb_scl);
verticalScrollBar()->value() - verticalScrollBar()->minimum()) *
thumb_scl,
vs * thumb_scl);
vr.adjust(0, 0, -1, -1);
p.setBrush(Qt::lightGray);
@@ -920,7 +907,7 @@ void BlockView::thumbHide() {
thumb_anim.setStartValue(_thumb());
thumb_anim.setEndValue(0.);
thumb_anim.start();
//qDebug() << "hide" << thumb_anim.startValue() << thumb_anim.endValue();
// qDebug() << "hide" << thumb_anim.startValue() << thumb_anim.endValue();
}
@@ -929,20 +916,19 @@ void BlockView::thumbShow() {
if (widget_thumb.isHidden() || (_talpha < 0.1)) drawSceneThumb();
thumb_anim.stop();
_setThumb(1.);
//qDebug() << "show" << thumb_anim.startValue() << thumb_anim.endValue();
// qDebug() << "show" << thumb_anim.startValue() << thumb_anim.endValue();
}
void BlockView::clearSelection() {
bool pb = block_emit_selection;
bool pb = block_emit_selection;
block_emit_selection = true;
sel_items.clear();
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if (i->flags().testFlag(QGraphicsItem::ItemIsSelectable)) i->setSelected(false);
block_emit_selection = pb;
if (!block_emit_selection)
emit selectionChanged();
if (!block_emit_selection) emit selectionChanged();
}
@@ -950,84 +936,80 @@ void BlockView::addItem(QGraphicsItem * item, bool emit_action) {
scene_->addItem(item);
applyGridStep();
if (item->data(bvidType).toInt() == bvitBus) {
loadBus(qgraphicsitem_cast<BlockBusItem*>(item));
((BlockBusItem*)item)->setSquareNodes(square_node);
connect((BlockBusItem*)item, SIGNAL(destroyed(QObject*)), this, SLOT(removedBus(QObject*)), Qt::UniqueConnection);
if (emit_action) emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem*>() << item);
loadBus(qgraphicsitem_cast<BlockBusItem *>(item));
((BlockBusItem *)item)->setSquareNodes(square_node);
connect((BlockBusItem *)item, SIGNAL(destroyed(QObject *)), this, SLOT(removedBus(QObject *)), Qt::UniqueConnection);
if (emit_action) emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem *>() << item);
emit connectionsChanged();
return;
}
if (item->data(bvidType).toInt() == bvitBlock) {
connect((BlockItem*)item, SIGNAL(destroyed(QObject*)), this, SLOT(removedBlock(QObject*)), Qt::UniqueConnection);
connect((BlockItem*)item, SIGNAL(blockHoverEnter(BlockItem*)), this, SIGNAL(blockHoverEnter(BlockItem*)), Qt::UniqueConnection);
connect((BlockItem*)item, SIGNAL(blockHoverLeave(BlockItem*)), this, SIGNAL(blockHoverLeave(BlockItem*)), Qt::UniqueConnection);
if (emit_action) emitActionEvent(BlockItemBase::BlockAdd, QList<QGraphicsItem*>() << item);
connect((BlockItem *)item, SIGNAL(destroyed(QObject *)), this, SLOT(removedBlock(QObject *)), Qt::UniqueConnection);
connect((BlockItem *)item, SIGNAL(blockHoverEnter(BlockItem *)), this, SIGNAL(blockHoverEnter(BlockItem *)), Qt::UniqueConnection);
connect((BlockItem *)item, SIGNAL(blockHoverLeave(BlockItem *)), this, SIGNAL(blockHoverLeave(BlockItem *)), Qt::UniqueConnection);
if (emit_action) emitActionEvent(BlockItemBase::BlockAdd, QList<QGraphicsItem *>() << item);
return;
}
item->setData(bvidType, bvitDecor);
}
QList<BlockBusItem * > BlockView::buses() const {
QList<BlockBusItem * > ret;
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
QList<BlockBusItem *> BlockView::buses() const {
QList<BlockBusItem *> ret;
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if ((i->data(bvidType).toInt() == bvitBus) && !i->data(bvidTmpItem).toBool())
if (!copy_buses.contains((BlockBusItem*)i))
ret << qgraphicsitem_cast<BlockBusItem*>(i);
if (!copy_buses.contains((BlockBusItem *)i)) ret << qgraphicsitem_cast<BlockBusItem *>(i);
return ret;
}
QList<BlockBusItem * > BlockView::wrongConnectedBuses() const {
QList<BlockBusItem * > sl = buses(), ret;
QList<BlockItem * > bl = blocks();
foreach (BlockItem * b, bl) {
QVector<BlockItemPin * > pins = b->pins();
foreach (BlockItemPin * p, pins)
QList<BlockBusItem *> BlockView::wrongConnectedBuses() const {
QList<BlockBusItem *> sl = buses(), ret;
QList<BlockItem *> bl = blocks();
foreach(BlockItem * b, bl) {
QVector<BlockItemPin *> pins = b->pins();
foreach(BlockItemPin * p, pins)
if (p->state() == BlockItemPin::Reject) {
QPointF pp = p->scenePos();
foreach (BlockBusItem * s, sl)
foreach(BlockBusItem * s, sl)
if (s->pol.contains(pp))
if (!ret.contains(s))
ret << s;
if (!ret.contains(s)) ret << s;
}
}
return ret;
}
QList<BlockItem * > BlockView::blocks() const {
QList<BlockItem * > ret;
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
if ((i->data(bvidType).toInt() == bvitBlock) && !i->data(bvidTmpItem).toBool())
ret << qgraphicsitem_cast<BlockItem*>(i);
QList<BlockItem *> BlockView::blocks() const {
QList<BlockItem *> ret;
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if ((i->data(bvidType).toInt() == bvitBlock) && !i->data(bvidTmpItem).toBool()) ret << qgraphicsitem_cast<BlockItem *>(i);
return ret;
}
QList<QGraphicsItem * > BlockView::decors() const {
QList<QGraphicsItem*> ret, gi = scene_->items();
foreach (QGraphicsItem * i, gi)
if ((i->data(bvidType).toInt() == bvitDecor) && !i->data(bvidTmpItem).toBool() && !i->data(bvidItemSelection).toBool() && (i->parentItem() == 0)
&& (i != &sel_rect) && (i != &tmp_bus) && !tmp_buses.contains((BlockBusItem*)i))
QList<QGraphicsItem *> BlockView::decors() const {
QList<QGraphicsItem *> ret, gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if ((i->data(bvidType).toInt() == bvitDecor) && !i->data(bvidTmpItem).toBool() && !i->data(bvidItemSelection).toBool() &&
(i->parentItem() == 0) && (i != &sel_rect) && (i != &tmp_bus) && !tmp_buses.contains((BlockBusItem *)i))
ret << i;
return ret;
}
BlockBusItem * BlockView::connectionBus(BlockItem * b0, BlockItem * b1) const {
QList<BlockBusItem * > cbl = connectionBuses(b0, b1);
QList<BlockBusItem *> cbl = connectionBuses(b0, b1);
if (cbl.isEmpty()) return 0;
return cbl.front();
}
QList<BlockBusItem * > BlockView::connectionBuses(BlockItem * b0, BlockItem * b1) const {
if (!b0 || !b1) return QList<BlockBusItem * >();
QSet<BlockBusItem * > bs0 = QList2QSet(b0->connectedBuses()),
bs1 = QList2QSet(b1->connectedBuses());
QList<BlockBusItem *> BlockView::connectionBuses(BlockItem * b0, BlockItem * b1) const {
if (!b0 || !b1) return QList<BlockBusItem *>();
QSet<BlockBusItem *> bs0 = QList2QSet(b0->connectedBuses()), bs1 = QList2QSet(b1->connectedBuses());
return (bs0 & bs1).values();
}
@@ -1036,25 +1018,31 @@ bool BlockView::connectPins(BlockItemPin * p0, BlockItemPin * p1) {
if (!p0 || !p1) return false;
if (p0 == p1) return false;
if (p0->busType() != p1->busType()) return false;
QList<BlockBusItem * > bl0 = p0->connectedBuses(), bl1 = p1->connectedBuses();
QList<BlockBusItem *> bl0 = p0->connectedBuses(), bl1 = p1->connectedBuses();
if (!(QList2QSet(bl0) & QList2QSet(bl1)).isEmpty()) return true;
BlockBusItem * nb = new BlockBusItem();
nb->setBusType(p0->busType());
loadBus(nb);
if (!bl0.isEmpty() && !bl1.isEmpty()) { // connect two existing buses
if (!bl0.isEmpty() && !bl1.isEmpty()) { // connect two existing buses
} else {
if ((bl0.isEmpty() && !bl1.isEmpty()) || (bl1.isEmpty() && !bl0.isEmpty())) { // connect empty pin to existing bus
BlockItemPin * ep = 0;
BlockBusItem * eb = 0;
if (bl0.isEmpty()) {ep = p0; eb = bl1[0];}
else {ep = p1; eb = bl0[0];}
double md = -1; int mi = -1;
if (bl0.isEmpty()) {
ep = p0;
eb = bl1[0];
} else {
ep = p1;
eb = bl0[0];
}
double md = -1;
int mi = -1;
QPointF sp = ep->scenePos();
if (eb->pol.size() == 2) {
eb->selSegment = 0;
eb->addPoint((eb->pol[0] + eb->pol[1]) / 2.);
eb->selPoint = -1;
mi = 2;
mi = 2;
} else {
for (int i = 0; i < eb->pol.size(); ++i) {
if (eb->ends.contains(i)) continue;
@@ -1092,7 +1080,7 @@ bool BlockView::connectPins(BlockItemPin * p0, BlockItemPin * p1) {
}
reconnectAll();
newBusCreated(nb);
emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem*>() << nb);
emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem *>() << nb);
emit connectionsChanged();
return true;
}
@@ -1159,17 +1147,19 @@ void BlockView::fitInView() {
QRectF BlockView::itemsBoundingRect() const {
QList<QGraphicsItem*> gi = scene_->items();
QList<QGraphicsItem *> gi = scene_->items();
if (gi.isEmpty()) return QRectF();
bool f = true;
QRectF ret;
foreach (QGraphicsItem * i, gi)
if (i->isVisible() && (i != &tmp_bus) && !tmp_buses.contains((BlockBusItem*)i)) {
foreach(QGraphicsItem * i, gi)
if (i->isVisible() && (i != &tmp_bus) && !tmp_buses.contains((BlockBusItem *)i)) {
if (!(i->data(bvidItemSelection).toBool()) && !i->data(bvidTmpItem).toBool()) {
QRectF br = i->mapRectToScene(i->boundingRect());
if (br.width() <= 1 || br.height() <= 1) continue;
if (f) ret = br;
else ret |= br;
if (f)
ret = br;
else
ret |= br;
f = false;
}
}
@@ -1178,96 +1168,93 @@ QRectF BlockView::itemsBoundingRect() const {
void BlockView::restoreSelState() {
//qDebug() << "restoreSelState";
foreach (QGraphicsItem * i, sel_items) {
// qDebug() << "restoreSelState";
foreach(QGraphicsItem * i, sel_items) {
i->setPos(i->data(bvidItemPos).toPointF());
}
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if (i->data(bvidType).toInt() == bvitBus) {
BlockBusItem * bi = qgraphicsitem_cast<BlockBusItem*>(i);
bi->pol = bi->bpol;
BlockBusItem * bi = qgraphicsitem_cast<BlockBusItem *>(i);
bi->pol = bi->bpol;
bi->prepareGeometryChange();
}
}
void BlockView::saveSelState() {
//qDebug() << "saveSelState";
QList<QGraphicsItem*> gi = scene_->items();
sel_items = scene_->selectedItems();
foreach (QGraphicsItem * i, gi) {
// qDebug() << "saveSelState";
QList<QGraphicsItem *> gi = scene_->items();
sel_items = scene_->selectedItems();
foreach(QGraphicsItem * i, gi) {
i->setData(bvidSelected, i->isSelected());
i->setData(bvidItemPos, i->pos());
if (i->data(bvidType).toInt() == bvitBus)
qgraphicsitem_cast<BlockBusItem*>(i)->bpol = qgraphicsitem_cast<BlockBusItem*>(i)->pol;
if (i->data(bvidType).toInt() == bvitBus) qgraphicsitem_cast<BlockBusItem *>(i)->bpol = qgraphicsitem_cast<BlockBusItem *>(i)->pol;
}
}
void BlockView::saveBusesState() {
QList<BlockBusItem*> bl = buses();
foreach (BlockBusItem * b, bl)
QList<BlockBusItem *> bl = buses();
foreach(BlockBusItem * b, bl)
b->saveState();
}
void BlockView::restoreBusesState() {
QList<BlockBusItem*> bl = buses();
foreach (BlockBusItem * b, bl)
QList<BlockBusItem *> bl = buses();
foreach(BlockBusItem * b, bl)
b->restoreState();
}
void BlockView::applySelRect(QGraphicsSceneMouseEvent * me) {
QList<QGraphicsItem*> ci = sel_rect.collidingItems(Qt::IntersectsItemBoundingRect);
QList<QGraphicsItem*> gi = scene_->items();
bool add = me->modifiers().testFlag(Qt::ControlModifier);
QList<QGraphicsItem*> sil = scene_->selectedItems();
block_emit_selection = true;
if (!add) clearSelection();
QList<QGraphicsItem *> ci = sel_rect.collidingItems(Qt::IntersectsItemBoundingRect);
QList<QGraphicsItem *> gi = scene_->items();
bool add = me->modifiers().testFlag(Qt::ControlModifier);
QList<QGraphicsItem *> sil = scene_->selectedItems();
block_emit_selection = true;
if (!add)
clearSelection();
else {
foreach (QGraphicsItem * i, gi)
foreach(QGraphicsItem * i, gi)
i->setSelected(i->data(bvidSelected).toBool());
}
foreach (QGraphicsItem * i, ci) {
foreach(QGraphicsItem * i, ci) {
i->setSelected(!i->isSelected());
}
block_emit_selection = false;
if (sil != scene_->selectedItems())
emit selectionChanged();
if (sil != scene_->selectedItems()) emit selectionChanged();
}
void BlockView::applyGridStep() {
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
if (i->type() == QGraphicsItem::UserType + 2)
qgraphicsitem_cast<BlockBusItem*>(i)->setGridStep(grid_step);
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if (i->type() == QGraphicsItem::UserType + 2) qgraphicsitem_cast<BlockBusItem *>(i)->setGridStep(grid_step);
}
void BlockView::trace(QPointF scene_pos_from, QPointF scene_pos_to, BlockBusItem * bus, bool primary) {
if (primary) {
if (hpin)
scene_pos_to = hpin->scenePos();
if (hpin) scene_pos_to = hpin->scenePos();
last_trace_from = scene_pos_from;
trace_to = scene_pos_to;
trace_to = scene_pos_to;
}
QRect sr = scene_->sceneRect().toRect();
int dx = sr.left() / grid_step, dy = sr.top() / grid_step;
QPoint dp(-dx, -dy), qpt = quantize(scene_pos_to, grid_step).toPoint() / grid_step + dp;
QPoint dp(-dx, -dy), qpt = quantize(scene_pos_to, grid_step).toPoint() / grid_step + dp;
QElapsedTimer tm;
tm.restart();
wavetrace.resize(sr.size() / grid_step);
wavetrace.fill(BlockViewWavetrace::Empty);
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if (i->data(bvidType).toInt() == bvitBlock) {
QRect ir = i->mapRectToScene(i->boundingRect()).toRect().normalized();
wavetrace.fill(QRect(ir.topLeft() / grid_step + dp, ir.bottomRight() / grid_step + dp), BlockViewWavetrace::Blocked);
QVector<BlockItemPin * > pins = qgraphicsitem_cast<BlockItem * >(i)->pins();
foreach (BlockItemPin * p, pins) {
QVector<BlockItemPin *> pins = qgraphicsitem_cast<BlockItem *>(i)->pins();
foreach(BlockItemPin * p, pins) {
if (p->busType() == bus->busType())
wavetrace.fill(quantize(p->scenePos(), grid_step).toPoint() / grid_step + dp, BlockViewWavetrace::Empty);
if (!m_pin_mc && !p->connectedBuses().isEmpty())
@@ -1275,9 +1262,9 @@ void BlockView::trace(QPointF scene_pos_from, QPointF scene_pos_to, BlockBusItem
}
}
if (m_trace_with_buses) {
foreach (QGraphicsItem * i, gi)
foreach(QGraphicsItem * i, gi)
if (i->data(bvidType).toInt() == bvitBus) {
BlockBusItem * b = qgraphicsitem_cast<BlockBusItem * >(i);
BlockBusItem * b = qgraphicsitem_cast<BlockBusItem *>(i);
if (!b) continue;
for (int s = 0; s < b->segments.size(); ++s) {
QPointF p0 = b->pol[b->segments[s].first], p1 = b->pol[b->segments[s].second], cp = p0;
@@ -1287,24 +1274,23 @@ void BlockView::trace(QPointF scene_pos_from, QPointF scene_pos_to, BlockBusItem
if ((dx + dy) < grid_step) continue;
BlockViewWavetrace::CellState cs = BlockViewWavetrace::Blocked;
if (dx >= dy) { // by x
sx = grid_step;
sy = sx * dy / dx;
sx = grid_step;
sy = sx * dy / dx;
steps = qRound(dx / grid_step);
cs = BlockViewWavetrace::HorizontalBus;
cs = BlockViewWavetrace::HorizontalBus;
} else {
sy = grid_step;
sx = sy * dx / dy;
sy = grid_step;
sx = sy * dx / dy;
steps = qRound(dy / grid_step);
cs = BlockViewWavetrace::VerticalBus;
cs = BlockViewWavetrace::VerticalBus;
}
sx *= signx;
sy *= signy;
//qDebug() << "fill" << p0 << "->" << p1 << "in" << steps << sx << sy;
// qDebug() << "fill" << p0 << "->" << p1 << "in" << steps << sx << sy;
for (int j = 0; j <= steps; ++j) {
QPoint tp = quantize(cp, grid_step).toPoint() / grid_step + dp;
if (tp != qpt)
wavetrace.fill(tp, (j > 0 && j < steps) ? cs : BlockViewWavetrace::Blocked);
//qDebug() << " set" << cp << ((j > 0 && j < steps) ? cs : BlockViewWavetrace::Blocked);
if (tp != qpt) wavetrace.fill(tp, (j > 0 && j < steps) ? cs : BlockViewWavetrace::Blocked);
// qDebug() << " set" << cp << ((j > 0 && j < steps) ? cs : BlockViewWavetrace::Blocked);
cp += QPointF(sx, sy);
}
}
@@ -1313,67 +1299,63 @@ void BlockView::trace(QPointF scene_pos_from, QPointF scene_pos_to, BlockBusItem
bus->clear();
if (wavetrace.trace(quantize(scene_pos_from, grid_step).toPoint() / grid_step + dp, qpt)) {
wavetrace.gatherPath();
foreach (const QPoint & p, wavetrace.path()) {
foreach(const QPoint & p, wavetrace.path()) {
bus->appendPoint((p - dp) * grid_step);
}
}
//qDebug() << quantize(scene_pos_from, grid_step).toPoint() / grid_step + dp << qpt;
// qDebug() << quantize(scene_pos_from, grid_step).toPoint() / grid_step + dp << qpt;
scene_->update();
}
void BlockView::clearBusStates() {
QList<QGraphicsItem * > gi = scene_->items();
foreach (QGraphicsItem * i, gi)
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if (i->data(bvidType).toInt() == bvitBus) {
BlockBusItem * b = qgraphicsitem_cast<BlockBusItem*>(i);
BlockBusItem * b = qgraphicsitem_cast<BlockBusItem *>(i);
b->clearBusState();
}
}
void BlockView::matchBus() {
match_bus = 0;
bool bv = tmp_bus.isVisible();
QList<QGraphicsItem * > gi = scene_->items();
QList<BlockBusItem * > buses;
QList<BlockItem * > blockl;
match_bus = 0;
bool bv = tmp_bus.isVisible();
QList<QGraphicsItem *> gi = scene_->items();
QList<BlockBusItem *> buses;
QList<BlockItem *> blockl;
int sp = -1, ss = -1;
QPointF point;
iconnect = false;
if (!tmp_bus.pol.isEmpty())
point = tmp_bus.pol.back();
foreach (QGraphicsItem * i, gi) {
if (!tmp_bus.pol.isEmpty()) point = tmp_bus.pol.back();
foreach(QGraphicsItem * i, gi) {
if (i != bus_from) {
if (i->data(bvidType).toInt() == bvitBus)
buses << qgraphicsitem_cast<BlockBusItem*>(i);
if (i->data(bvidType).toInt() == bvitBlock)
blockl << qgraphicsitem_cast<BlockItem*>(i);
if (i->data(bvidType).toInt() == bvitBus) buses << qgraphicsitem_cast<BlockBusItem *>(i);
if (i->data(bvidType).toInt() == bvitBlock) blockl << qgraphicsitem_cast<BlockItem *>(i);
}
}
foreach (BlockBusItem * b, buses) {
foreach(BlockBusItem * b, buses) {
b->clearBusState();
b->selPoint = b->selSegment = -1;
}
if (!bv) return;
BlockBusItem * b(0);
if (m_pin_mc) {
foreach (BlockItem * b_, blockl)
foreach (BlockItemPin * p_, b_->pins())
foreach(BlockItem * b_, blockl)
foreach(BlockItemPin * p_, b_->pins())
if (p_->scenePos() == point) {
return;
}
}
//qDebug() << "1" << buses.size() << tmp_bus.pol;
// qDebug() << "1" << buses.size() << tmp_bus.pol;
for (int i = 0; i < buses.size(); ++i) {
b = buses[i];
b->testPoint(point, &sp, &ss, true);
//qDebug() << i << sp << ss;
// qDebug() << i << sp << ss;
if (sp >= 0 || ss >= 0) break;
}
if ((sp < 0 && ss < 0) || b == 0) return;
//qDebug("2");
// qDebug("2");
match_bus = b;
if ((b->busType() != tmp_bus.busType()) || b->connections_.value(sp, 0) != 0) {
b->setBusState(false);
@@ -1405,7 +1387,7 @@ void BlockView::matchBus() {
}
iconnect = true;
b->setBusState(true);
b->selPoint = sp;
b->selPoint = sp;
b->selSegment = ss;
}
}
@@ -1416,8 +1398,7 @@ bool BlockView::connectTmpToBus(BlockBusItem * bus) {
if (!bus->busState()) return false;
if (tmp_bus.pol.size() < 2) return false;
int np = bus->selPoint;
if (np < 0)
np = bus->addPoint(tmp_bus.pol.back());
if (np < 0) np = bus->addPoint(tmp_bus.pol.back());
if (np < 0) return false;
tmp_bus.pol.pop_back();
int lp = bus->pol.size();
@@ -1433,18 +1414,16 @@ bool BlockView::connectTmpToBus(BlockBusItem * bus) {
void BlockView::markPins(int bus_type) {
unhoverPins();
QList<QGraphicsItem * > gi = scene_->items();
foreach (QGraphicsItem * i, gi) {
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi) {
if (i->data(bvidType).toInt() == bvitPin) {
BlockItemPin * p = qgraphicsitem_cast<BlockItemPin*>(i);
BlockItemPin * p = qgraphicsitem_cast<BlockItemPin *>(i);
p->saveState();
if (m_pin_mc) {
if (p->busType() == bus_type)
p->setState(BlockItemPin::Accept);
if (p->busType() == bus_type) p->setState(BlockItemPin::Accept);
} else {
if (p->busType() == bus_type) {
if (p->state() == BlockItemPin::Disconnected)
p->setState(BlockItemPin::Accept);
if (p->state() == BlockItemPin::Disconnected) p->setState(BlockItemPin::Accept);
}
}
}
@@ -1454,12 +1433,13 @@ void BlockView::markPins(int bus_type) {
void BlockView::unmarkPins(bool to_normal) {
unhoverPins();
QList<QGraphicsItem * > gi = scene_->items();
foreach (QGraphicsItem * i, gi) {
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi) {
if (i->data(bvidType).toInt() == bvitPin) {
qgraphicsitem_cast<BlockItemPin*>(i)->restoreState();
qgraphicsitem_cast<BlockItemPin *>(i)->restoreState();
if (to_normal)
while (qgraphicsitem_cast<BlockItemPin*>(i)->restoreState());
while (qgraphicsitem_cast<BlockItemPin *>(i)->restoreState())
;
}
}
}
@@ -1471,60 +1451,71 @@ void BlockView::hoverAcceptedPin(BlockItemPin * pin, bool hover) {
}
void BlockView::unhoverPins(BlockItemPin* excl_pin) {
QList<QGraphicsItem * > gi = scene_->items();
foreach (QGraphicsItem * i, gi) {
if (excl_pin == ((BlockItemPin*)i)) continue;
void BlockView::unhoverPins(BlockItemPin * excl_pin) {
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi) {
if (excl_pin == ((BlockItemPin *)i)) continue;
if (i->data(bvidType).toInt() == bvitPin) {
((BlockItemPin*)i)->enlargePin(false);
((BlockItemPin *)i)->enlargePin(false);
}
}
}
void BlockView::simplifyBuses() {
QList<BlockBusItem*> bl = buses();
foreach (BlockBusItem * b, bl)
QList<BlockBusItem *> bl = buses();
foreach(BlockBusItem * b, bl)
b->simplify();
}
void BlockView::moveBuses(const QList<QGraphicsItem * > & items, QPointF dp) {
void BlockView::moveBuses(const QList<QGraphicsItem *> & items, QPointF dp) {
if (dp.isNull()) return;
QList<QGraphicsItem * > gi = scene_->items();
QVector<BlockItemPin * > pins;
QList<BlockBusItem * > buses;
//qDebug() << "move" << dp;
foreach (QGraphicsItem * i, items)
QList<QGraphicsItem *> gi = scene_->items();
QVector<BlockItemPin *> pins;
QList<BlockBusItem *> buses;
// qDebug() << "move" << dp;
foreach(QGraphicsItem * i, items)
if ((i->data(bvidType).toInt() == bvitBlock) && i->flags().testFlag(QGraphicsItem::ItemIsMovable))
pins << qgraphicsitem_cast<BlockItem*>(i)->pins();
foreach (QGraphicsItem * i, gi)
if (i->data(bvidType).toInt() == bvitBus)
buses << qgraphicsitem_cast<BlockBusItem*>(i);
foreach (BlockBusItem * b, buses) {
QList<BlockItemPin * > bpins = b->connections_.values();
pins << qgraphicsitem_cast<BlockItem *>(i)->pins();
foreach(QGraphicsItem * i, gi)
if (i->data(bvidType).toInt() == bvitBus) buses << qgraphicsitem_cast<BlockBusItem *>(i);
foreach(BlockBusItem * b, buses) {
QList<BlockItemPin *> bpins = b->connections_.values();
if (!bpins.isEmpty()) {
foreach (BlockItemPin * p, pins)
foreach(BlockItemPin * p, pins)
bpins.removeAll(p);
if (bpins.isEmpty()) {
b->movePolyline(dp);
continue;
}
}
foreach (BlockItemPin * p, pins) {
foreach(BlockItemPin * p, pins) {
QList<int> ends = b->connections_.keys(p);
for (int i = 0; i < ends.size(); ++i) {
QPointF pdp = dp;
double ang = 0.;
double ang = 0.;
switch (p->alignment()) {
case Qt::AlignRight : pdp.setX(0.); ang = 0.; break;
case Qt::AlignTop : pdp.setY(0.); ang = 90.; break;
case Qt::AlignLeft : pdp.setX(0.); ang = 180.; break;
case Qt::AlignBottom: pdp.setY(0.); ang = 270.; break;
case Qt::AlignRight:
pdp.setX(0.);
ang = 0.;
break;
case Qt::AlignTop:
pdp.setY(0.);
ang = 90.;
break;
case Qt::AlignLeft:
pdp.setX(0.);
ang = 180.;
break;
case Qt::AlignBottom:
pdp.setY(0.);
ang = 270.;
break;
default: break;
}
QVector<int> epl = b->endpointLine(ends[i], ang);
foreach (int e, epl)
foreach(int e, epl)
b->movePoint(e, pdp);
b->movePoint(ends[i], dp);
}
@@ -1533,27 +1524,26 @@ void BlockView::moveBuses(const QList<QGraphicsItem * > & items, QPointF dp) {
}
QList<BlockBusItem * > BlockView::internalBuses(const QList<BlockItem * > & items) {
QList<BlockBusItem * > ret;
QList<BlockBusItem *> BlockView::internalBuses(const QList<BlockItem *> & items) {
QList<BlockBusItem *> ret;
if (items.isEmpty()) return ret;
QList<BlockBusItem * > sbl = buses();
QSet<BlockItem * > sis = QList2QSet(items);
foreach (BlockBusItem * bi, sbl) {
QList<BlockBusItem *> sbl = buses();
QSet<BlockItem *> sis = QList2QSet(items);
foreach(BlockBusItem * bi, sbl) {
if (bi->connectedBlocks().isEmpty()) continue;
QSet<BlockItem * > bis = QList2QSet(bi->connectedBlocks());
if ((bis - sis).isEmpty())
ret << bi;
QSet<BlockItem *> bis = QList2QSet(bi->connectedBlocks());
if ((bis - sis).isEmpty()) ret << bi;
}
return ret;
}
QList<BlockItemPin * > BlockView::nearPins(BlockItemPin * pin, Qt::KeyboardModifiers km) {
QList<BlockItemPin * > ret;
QList<BlockItemPin *> BlockView::nearPins(BlockItemPin * pin, Qt::KeyboardModifiers km) {
QList<BlockItemPin *> ret;
bool up = km.testFlag(Qt::ShiftModifier);
bool down = km.testFlag(Qt::ControlModifier);
bool ab = km.testFlag(Qt::AltModifier);
//qDebug() << "nearPins" << km;
// qDebug() << "nearPins" << km;
if (!pin || (!up && !down)) return ret;
const BlockItem * src = pin->parent();
if (!src) return ret;
@@ -1562,17 +1552,16 @@ QList<BlockItemPin * > BlockView::nearPins(BlockItemPin * pin, Qt::KeyboardModif
for (int i = 0; i < 2; ++i) {
if (!dirs[i]) continue;
const BlockItem * cb = src;
BlockItemPin * cpin = pin;
//qDebug() << "dir" << dirs[i];
BlockItemPin * cpin = pin;
// qDebug() << "dir" << dirs[i];
while (cpin) {
//qDebug() << "cur" << cpin;
QList<QGraphicsItem * > il = scene_->items(cpin->scenePos() + QPointF(0., dy[i] * cb->pinsMargin()));
BlockItemPin * np = getPin(il);
// qDebug() << "cur" << cpin;
QList<QGraphicsItem *> il = scene_->items(cpin->scenePos() + QPointF(0., dy[i] * cb->pinsMargin()));
BlockItemPin * np = getPin(il);
if (np == cpin) break;
cpin = np;
if (cpin) {
if (((cb != cpin->parent()) && !ab) ||
(cpin->state() != BlockItemPin::Disconnected)) {
if (((cb != cpin->parent()) && !ab) || (cpin->state() != BlockItemPin::Disconnected)) {
break;
}
cb = cpin->parent();
@@ -1585,20 +1574,19 @@ QList<BlockItemPin * > BlockView::nearPins(BlockItemPin * pin, Qt::KeyboardModif
BlockItemPin * BlockView::getPin(const QList<QGraphicsItem *> & list) const {
foreach (QGraphicsItem * i, list) {
if (i->data(bvidType).toInt() == bvitPin)
return qgraphicsitem_cast<BlockItemPin*>(i);
foreach(QGraphicsItem * i, list) {
if (i->data(bvidType).toInt() == bvitPin) return qgraphicsitem_cast<BlockItemPin *>(i);
}
return 0;
}
void BlockView::highlightNearPins(BlockItemPin * pin, Qt::KeyboardModifiers km) {
//qDebug() << "restore for" << last_multiconnect_pl.size();
foreach (BlockItemPin * p, last_multiconnect_pl)
// qDebug() << "restore for" << last_multiconnect_pl.size();
foreach(BlockItemPin * p, last_multiconnect_pl)
p->restoreState();
QList<BlockItemPin * > pl = nearPins(pin, km);
foreach (BlockItemPin * p, pl) {
QList<BlockItemPin *> pl = nearPins(pin, km);
foreach(BlockItemPin * p, pl) {
p->saveState();
p->setState(BlockItemPin::Hover);
}
@@ -1616,21 +1604,19 @@ void BlockView::hideTmpBuses(bool clear) {
}
QList<BlockItem * > BlockView::selectedBlocks() const {
QList<BlockItem * > ret;
QList<QGraphicsItem * > sil = scene()->selectedItems();
foreach (QGraphicsItem * b, sil)
if ((b->data(bvidType).toInt() == bvitBlock) && !b->data(bvidTmpItem).toBool())
ret << qgraphicsitem_cast<BlockItem*>(b);
QList<BlockItem *> BlockView::selectedBlocks() const {
QList<BlockItem *> ret;
QList<QGraphicsItem *> sil = scene()->selectedItems();
foreach(QGraphicsItem * b, sil)
if ((b->data(bvidType).toInt() == bvitBlock) && !b->data(bvidTmpItem).toBool()) ret << qgraphicsitem_cast<BlockItem *>(b);
return ret;
}
QList<QGraphicsItem * > BlockView::selectedDecors() const {
QList<QGraphicsItem * > ret, sil = decors();
foreach (QGraphicsItem * b, sil)
if (b->isSelected())
ret << b;
QList<QGraphicsItem *> BlockView::selectedDecors() const {
QList<QGraphicsItem *> ret, sil = decors();
foreach(QGraphicsItem * b, sil)
if (b->isSelected()) ret << b;
return ret;
}
@@ -1643,7 +1629,7 @@ void BlockView::animateNav(QRectF d, double scl) {
return;
}
if (nav_anim.state() != QAbstractAnimation::Running) {
nav_prev_aa = renderHints().testFlag(QPainter::Antialiasing);
nav_prev_aa = renderHints().testFlag(QPainter::Antialiasing);
nav_prev_imaa = renderHints().testFlag(QPainter::SmoothPixmapTransform);
nav_prev_grid = isGridVisible();
setRenderHint(QPainter::Antialiasing, false);
@@ -1667,14 +1653,11 @@ void BlockView::adjustThumb() {
scl = thumb_size.width() / sr.width();
else
scl = thumb_size.height() / sr.height();
tr = sr * scl;
tr = sr * scl;
int sx = 0, sy = 0;
if (verticalScrollBar()->isVisible() && (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOff))
sx = verticalScrollBar()->width();
if (horizontalScrollBar()->isVisible() && (horizontalScrollBarPolicy() != Qt::ScrollBarAlwaysOff))
sy = horizontalScrollBar()->height();
widget_thumb.setGeometry(QRect(QPoint(width() - tr.width() - 10 - sx,
height() - tr.height() - 10 - sy), tr.toSize()));
if (verticalScrollBar()->isVisible() && (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOff)) sx = verticalScrollBar()->width();
if (horizontalScrollBar()->isVisible() && (horizontalScrollBarPolicy() != Qt::ScrollBarAlwaysOff)) sy = horizontalScrollBar()->height();
widget_thumb.setGeometry(QRect(QPoint(width() - tr.width() - 10 - sx, height() - tr.height() - 10 - sy), tr.toSize()));
}
@@ -1695,38 +1678,41 @@ void BlockView::newBranch(BlockBusItem * item) {
void BlockView::startBusPointMove(int bus_type) {
move_bus_point = true;
prev_tcb = m_trace_with_buses;
prev_tcb = m_trace_with_buses;
newBusStarted(bus_type);
markPins(bus_type);
}
void BlockView::pinHoverInOut(BlockItemPin * pin) {
//qDebug() << "pinHoverInOut" << pin << pin->state();
// qDebug() << "pinHoverInOut" << pin << pin->state();
highlightNearPins(pin, QApplication::keyboardModifiers());
}
void BlockView::checkPaste(bool queued) {
const QMimeData * mime = QApplication::clipboard()->mimeData();
bool ret = false;
if (mime)
ret = mime->hasFormat(_BlockView_Mime_);
if (queued) QMetaObject::invokeMethod(this, [this, ret](){pasteEnabledChanged(ret);}, Qt::QueuedConnection);
else emit pasteEnabledChanged(ret);
bool ret = false;
if (mime) ret = mime->hasFormat(_BlockView_Mime_);
if (queued)
QMetaObject::invokeMethod(
this,
[this, ret]() { pasteEnabledChanged(ret); },
Qt::QueuedConnection);
else
emit pasteEnabledChanged(ret);
}
void BlockView::setBusSquareNodes(bool yes) {
square_node = yes;
QList<BlockBusItem * > sbl = buses();
foreach (BlockBusItem * b, sbl) {
square_node = yes;
QList<BlockBusItem *> sbl = buses();
foreach(BlockBusItem * b, sbl) {
b->setSquareNodes(square_node);
}
}
void BlockView::newBranchTrace(BlockBusItem * item, QPointF to) {
trace(item->press_pos, to, &tmp_bus);
tmp_bus.show();
@@ -1740,17 +1726,18 @@ void BlockView::newBranchAccept(BlockBusItem * item) {
tmp_bus.hide();
if (tmp_bus.pol.size() < 2) return;
tmp_bus.pol.pop_front();
if ((tmp_bus.pol.size() == 1) && (tmp_bus.pol[0] == item->press_pos))
return;
if ((tmp_bus.pol.size() == 1) && (tmp_bus.pol[0] == item->press_pos)) return;
int np = item->addPoint(item->press_pos);
if (np < 0) return;
if (match_bus) {
if (iconnect) tmp_bus.pol.pop_back();
else return;
if (iconnect)
tmp_bus.pol.pop_back();
else
return;
}
if (item == match_bus) return;
int snp = np;
int lp = item->pol.size();
int lp = item->pol.size();
if (!tmp_bus.pol.isEmpty()) {
item->pol << tmp_bus.pol;
item->segments << QPair<int, int>(np, lp);
@@ -1760,8 +1747,7 @@ void BlockView::newBranchAccept(BlockBusItem * item) {
if (match_bus != 0) {
if (!iconnect) return;
np = match_bus->selPoint;
if (np < 0)
np = match_bus->addPoint(trace_to);
if (np < 0) np = match_bus->addPoint(trace_to);
if (np < 0) return;
lp = item->pol.size();
item->pol << match_bus->pol;
@@ -1777,7 +1763,7 @@ void BlockView::newBranchAccept(BlockBusItem * item) {
}
item->updateGeometry();
newBusCreated(item);
emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem*>() << item);
emitActionEvent(BlockItemBase::BusAdd, QList<QGraphicsItem *>() << item);
emit connectionsChanged();
tmp_bus.clear();
}
@@ -1785,7 +1771,7 @@ void BlockView::newBranchAccept(BlockBusItem * item) {
void BlockView::newBranchCancel() {
unmarkPins();
//qDebug() << "cancel";
// qDebug() << "cancel";
new_branch = false;
hideTmpBuses();
}
@@ -1794,25 +1780,25 @@ void BlockView::newBranchCancel() {
void BlockView::removedBus(QObject * o) {
mm_ci = 0;
reconnectAll();
BlockBusItem * bus = (BlockBusItem*)o;
BlockBusItem * bus = (BlockBusItem *)o;
if (bus->property("_nodelete_").toBool()) return;
emitActionEvent(BlockItemBase::BusRemove, QList<QGraphicsItem*>() << bus);
emitActionEvent(BlockItemBase::BusRemove, QList<QGraphicsItem *>() << bus);
emit connectionsChanged();
}
void BlockView::removedBlock(QObject * o) {
if (o == ghost_) return;
emit blockRemoved((BlockItem*)o);
emitActionEvent(BlockItemBase::BlockRemove, QList<QGraphicsItem*>() << qgraphicsitem_cast<QGraphicsItem*>((BlockItem*)o));
emit blockRemoved((BlockItem *)o);
emitActionEvent(BlockItemBase::BlockRemove, QList<QGraphicsItem *>() << qgraphicsitem_cast<QGraphicsItem *>((BlockItem *)o));
}
void BlockView::removeJunk() {
QList<QGraphicsItem * > gi = scene_->items();
foreach (QGraphicsItem * i, gi) {
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi) {
if (i->data(bvidType).toInt() != bvitBus) continue;
BlockBusItem * b = qgraphicsitem_cast<BlockBusItem*>(i);
BlockBusItem * b = qgraphicsitem_cast<BlockBusItem *>(i);
if (b->pol.size() <= 1) {
b->deleteLater();
}
@@ -1823,17 +1809,21 @@ void BlockView::removeJunk() {
void BlockView::sceneSelectionChanged() {
bool ie = scene()->selectedItems().isEmpty();
emit copyEnabledChanged(!ie);
if (!block_emit_selection)
emit selectionChanged();
if (!block_emit_selection) emit selectionChanged();
}
void BlockView::_setThumb(double v) {
_talpha = v;
_talpha = v;
QWidget * w = &widget_thumb;
QMetaObject::invokeMethod(this, [w](){w->update();}, Qt::QueuedConnection);
if (_talpha <= 0.01) widget_thumb.hide();
else widget_thumb.show();
QMetaObject::invokeMethod(
this,
[w]() { w->update(); },
Qt::QueuedConnection);
if (_talpha <= 0.01)
widget_thumb.hide();
else
widget_thumb.show();
}
@@ -1841,7 +1831,7 @@ void BlockView::_setNav(QRectF v) {
double vw = viewport()->width(), vh = viewport()->height();
if (vw < 1. || vh < 1. || v.isEmpty()) return;
QTransform matrix;
double scl = qMin(vw / v.width(), vh / v.height());
double scl = qMin(vw / v.width(), vh / v.height());
double ascl = appScale(this);
if (scl > 0.02 * ascl && scl < 50.0 * ascl) {
matrix.scale(scl, scl);
@@ -1865,23 +1855,21 @@ void BlockView::_navFinished() {
void BlockView::reconnectAll() {
//qDebug() << "reconnect";
// qDebug() << "reconnect";
removeJunk();
QList<QGraphicsItem * > gi = scene_->items();
QList<BlockItemPin * > pins;
QList<BlockBusItem * > buses;
foreach (QGraphicsItem * i, gi) {
if (i->data(bvidType).toInt() == bvitPin)
pins << qgraphicsitem_cast<BlockItemPin*>(i);
if (i->data(bvidType).toInt() == bvitBus)
buses << qgraphicsitem_cast<BlockBusItem*>(i);
QList<QGraphicsItem *> gi = scene_->items();
QList<BlockItemPin *> pins;
QList<BlockBusItem *> buses;
foreach(QGraphicsItem * i, gi) {
if (i->data(bvidType).toInt() == bvitPin) pins << qgraphicsitem_cast<BlockItemPin *>(i);
if (i->data(bvidType).toInt() == bvitBus) buses << qgraphicsitem_cast<BlockBusItem *>(i);
}
foreach (BlockItemPin * p, pins) {
foreach(BlockItemPin * p, pins) {
p->clearStateStack();
p->setState(BlockItemPin::Disconnected);
p->buses_.clear();
}
foreach (BlockBusItem * b, buses) {
foreach(BlockBusItem * b, buses) {
b->connections_.clear();
QVector<int> conns(b->endpoints());
for (int c = 0; c < conns.size(); ++c) {
@@ -1890,11 +1878,10 @@ void BlockView::reconnectAll() {
if (!pins[j]->isVisible()) continue;
QPointF pp = pins[j]->scenePos();
if ((cp - pp).manhattanLength() <= (grid_step / 2.)) {
//qDebug() << "found";
// qDebug() << "found";
if (b->busType() == pins[j]->busType()) {
b->connections_[conns[c]] = pins[j];
if (!pins[j]->buses_.contains(b))
pins[j]->buses_ << b;
if (!pins[j]->buses_.contains(b)) pins[j]->buses_ << b;
pins[j]->setState(BlockItemPin::Connected);
} else
pins[j]->setState(BlockItemPin::Reject);
@@ -1903,17 +1890,18 @@ void BlockView::reconnectAll() {
}
}
}
//qDebug() << pins.size() << buses.size();
// qDebug() << pins.size() << buses.size();
}
void BlockView::zoom(double factor) {
if (!is_nav_anim || (nav_anim.state() != QPropertyAnimation::Running))
nav_target = _nav();
if (!is_nav_anim || (nav_anim.state() != QPropertyAnimation::Running)) nav_target = _nav();
QRectF r = nav_target;
QPoint mp;
if (underMouse()) mp = mapFromGlobal(QCursor::pos());
else mp = QPoint(viewport()->width() / 2, viewport()->height() / 2);
if (underMouse())
mp = mapFromGlobal(QCursor::pos());
else
mp = QPoint(viewport()->width() / 2, viewport()->height() / 2);
double cx = double(mp.x()) / viewport()->width(), cy = double(mp.y()) / viewport()->height();
double pw = r.width(), ph = r.height();
r.setWidth(r.width() / factor);
@@ -1930,11 +1918,11 @@ void BlockView::zoomReset() {
void BlockView::copyToClipboard() {
QList<BlockItem*> bll = selectedBlocks();
QList<QGraphicsItem*> del = selectedDecors();
//qDebug() << "copy" << bll.size() << del.size();
QList<BlockItem *> bll = selectedBlocks();
QList<QGraphicsItem *> del = selectedDecors();
// qDebug() << "copy" << bll.size() << del.size();
if (bll.isEmpty() && del.isEmpty()) return;
QList<BlockBusItem*> bul = internalBuses(bll);
QList<BlockBusItem *> bul = internalBuses(bll);
QByteArray ba;
QDataStream s(&ba, QIODevice::ReadWrite);
s << uint(0x89abcdef) << bll << bul << del;
@@ -1949,11 +1937,11 @@ void BlockView::pasteFromClipboard() {
if (!mime) return;
if (!mime->hasFormat(_BlockView_Mime_)) return;
QByteArray ba = mime->data(_BlockView_Mime_);
//qDebug() << "paste" << ba.size();
// qDebug() << "paste" << ba.size();
if (ba.size() < 4) return;
QList<BlockItem*> bll;
QList<BlockBusItem*> bul;
QList<QGraphicsItem*> del, gl;
QList<BlockItem *> bll;
QList<BlockBusItem *> bul;
QList<QGraphicsItem *> del, gl;
uint hdr = 0;
QDataStream s(ba);
s >> hdr;
@@ -1962,24 +1950,26 @@ void BlockView::pasteFromClipboard() {
int all = bll.size() + bul.size() + del.size();
if (all == 0) return;
QRectF br;
foreach (BlockItem * b, bll) {
foreach(BlockItem * b, bll) {
br |= b->boundingRect().translated(b->pos());
gl << b;
}
foreach (BlockBusItem * b, bul)
foreach(BlockBusItem * b, bul)
gl << b;
foreach (QGraphicsItem * b, del)
foreach(QGraphicsItem * b, del)
br |= b->boundingRect().translated(b->pos());
gl << del;
QPointF copy_dp;
if (underMouse()) copy_dp = mapToScene(mapFromGlobal(QCursor::pos()));
else copy_dp = mapToScene(rect().center());
if (underMouse())
copy_dp = mapToScene(mapFromGlobal(QCursor::pos()));
else
copy_dp = mapToScene(rect().center());
copy_dp -= br.center();
copy_dp = quantize(copy_dp, grid_step);
copy_dp = quantize(copy_dp, grid_step);
ae_enabled = false;
if (!bll.isEmpty()) copyBlocks(bll, copy_dp);
if (!bul.isEmpty()) copyBuses(bul, copy_dp);
foreach (QGraphicsItem * i, del)
foreach(QGraphicsItem * i, del)
i->setPos(i->pos() + copy_dp);
addItems(del);
ae_enabled = true;
@@ -1988,80 +1978,75 @@ void BlockView::pasteFromClipboard() {
void BlockView::selectNone() {
bool pb = block_emit_selection;
block_emit_selection = true;
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
bool pb = block_emit_selection;
block_emit_selection = true;
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
i->setSelected(false);
block_emit_selection = pb;
if (!block_emit_selection)
emit selectionChanged();
if (!block_emit_selection) emit selectionChanged();
}
void BlockView::selectAll() {
bool pb = block_emit_selection;
block_emit_selection = true;
QList<QGraphicsItem*> gi = scene_->items();
foreach (QGraphicsItem * i, gi)
if (i->flags().testFlag(QGraphicsItem::ItemIsSelectable))
i->setSelected(true);
bool pb = block_emit_selection;
block_emit_selection = true;
QList<QGraphicsItem *> gi = scene_->items();
foreach(QGraphicsItem * i, gi)
if (i->flags().testFlag(QGraphicsItem::ItemIsSelectable)) i->setSelected(true);
block_emit_selection = pb;
if (!block_emit_selection)
emit selectionChanged();
if (!block_emit_selection) emit selectionChanged();
}
void BlockView::removeSelected() {
QList<QGraphicsItem*> gi = scene_->selectedItems(), ai;
QList<QGraphicsItem *> gi = scene_->selectedItems(), ai;
blockSignals(true);
QList<BlockBusItem * > sbuses = buses(), dbuses, wbuses = wrongConnectedBuses();
foreach (BlockBusItem * i, sbuses)
if (i->connectedBlocks().isEmpty())
dbuses << i;
foreach (QGraphicsItem * i, gi) {
QList<BlockBusItem *> sbuses = buses(), dbuses, wbuses = wrongConnectedBuses();
foreach(BlockBusItem * i, sbuses)
if (i->connectedBlocks().isEmpty()) dbuses << i;
foreach(QGraphicsItem * i, gi) {
if (i->data(bvidTmpItem).toBool()) continue;
if (i->data(bvidType).toInt() == bvitBlock)
ai << qgraphicsitem_cast<QGraphicsItem*>(i);
if ((i->data(bvidType).toInt() == bvitBlock) || (i->data(bvidType).toInt() == bvitBus) || (i->data(bvidType).toInt() == bvitDecor)) {
if (i->data(bvidType).toInt() == bvitBlock) ai << qgraphicsitem_cast<QGraphicsItem *>(i);
if ((i->data(bvidType).toInt() == bvitBlock) || (i->data(bvidType).toInt() == bvitBus) ||
(i->data(bvidType).toInt() == bvitDecor)) {
scene_->sendEvent(i, new QGraphicsSceneEvent(QEvent::Close));
delete i;
}
}
reconnectAll();
foreach (BlockBusItem * i, sbuses)
foreach(BlockBusItem * i, sbuses)
if (i->connectedBlocks().isEmpty())
if (!dbuses.contains(i))
delete i;
if (!dbuses.contains(i)) delete i;
blockSignals(false);
foreach (QGraphicsItem * i, ai)
emit blockRemoved((BlockItem*)i);
foreach(QGraphicsItem * i, ai)
emit blockRemoved((BlockItem *)i);
emitActionEvent(BlockItemBase::BlockRemove, ai);
}
void BlockView::removeAll() {
last_multiconnect_pl.clear();
QList<QGraphicsItem*> gi = scene_->items(), ai;
QList<QGraphicsItem *> gi = scene_->items(), ai;
blockSignals(true);
match_bus = bus_from = cur_bus = nullptr;
mm_ci = nullptr;
hpin = nullptr;
ghost_ = nullptr;
foreach (QGraphicsItem * i, gi) {
mm_ci = nullptr;
hpin = nullptr;
ghost_ = nullptr;
foreach(QGraphicsItem * i, gi) {
if (i->data(bvidTmpItem).toBool()) continue;
if (i->data(bvidType).toInt() == bvitBlock)
ai << qgraphicsitem_cast<QGraphicsItem*>(i);
if ((i->data(bvidType).toInt() == bvitBlock) || (i->data(bvidType).toInt() == bvitBus) || (i->data(bvidType).toInt() == bvitDecor)) {
if (i->data(bvidType).toInt() == bvitBlock) ai << qgraphicsitem_cast<QGraphicsItem *>(i);
if ((i->data(bvidType).toInt() == bvitBlock) || (i->data(bvidType).toInt() == bvitBus) ||
(i->data(bvidType).toInt() == bvitDecor)) {
if ((i != &sel_rect) && (i != &tmp_bus) && (i->parentItem() == 0) && !(i->data(bvidItemSelection).toBool())) {
//qDebug() << "delete" << i->data(1005).toInt();
// qDebug() << "delete" << i->data(1005).toInt();
scene_->sendEvent(i, new QGraphicsSceneEvent(QEvent::Close));
delete i;
}
}
}
blockSignals(false);
foreach (QGraphicsItem * i, ai)
emit blockRemoved((BlockItem*)i);
foreach(QGraphicsItem * i, ai)
emit blockRemoved((BlockItem *)i);
}

View File

@@ -1,45 +1,45 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLOCKVIEW_H
#define BLOCKVIEW_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>
#include <QDebug>
#include <QTime>
#include <QPropertyAnimation>
#include "blockviewwavetrace.h"
#include "blockbusitem.h"
#include "blockviewwavetrace.h"
#include "qad_blockview_export.h"
Q_DECLARE_METATYPE(BlockItem*)
Q_DECLARE_METATYPE(BlockItemPin*)
Q_DECLARE_METATYPE(BlockBusItem*)
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QPropertyAnimation>
#include <QTime>
class QAD_BLOCKVIEW_EXPORT BlockView: public QGraphicsView
{
Q_DECLARE_METATYPE(BlockItem *)
Q_DECLARE_METATYPE(BlockItemPin *)
Q_DECLARE_METATYPE(BlockBusItem *)
class QAD_BLOCKVIEW_EXPORT BlockView: public QGraphicsView {
Q_OBJECT
Q_ENUMS(SelectionMode)
@@ -77,34 +77,37 @@ public:
MultiSelection
};
QPen gridPen() const {return grid_pen;}
bool isGridVisible() const {return grid_visible;}
bool isSnapToGrid() const {return grid_snap;}
bool isPostMoveConnectEnabled() const {return pm_connect;}
bool isNavigationEnabled() const {return navigation;}
bool isNavigateAnimationEnabled() const {return is_nav_anim;}
bool isBlockAnimationEnabled() const {return is_block_anim;}
bool isConnectByMouseEnabled() const {return m_connect;}
bool isTraceConsiderBuses() const {return m_trace_with_buses;}
bool isPinMulticonnectEnabled() const {return m_pin_mc;}
bool isMiniMapEnabled() const {return minimap;}
bool isZoomWheelOnly() const {return wheel_zoom;}
bool isBusSquareNodes() const {return square_node;}
double gridStep() const {return grid_step;}
double gridPointsWidth() const {return grid_points;}
SelectionMode selectionMode() const {return smode;}
void setSelectionMode(SelectionMode mode) {smode = mode;}
QPen gridPen() const { return grid_pen; }
bool isGridVisible() const { return grid_visible; }
bool isSnapToGrid() const { return grid_snap; }
bool isPostMoveConnectEnabled() const { return pm_connect; }
bool isNavigationEnabled() const { return navigation; }
bool isNavigateAnimationEnabled() const { return is_nav_anim; }
bool isBlockAnimationEnabled() const { return is_block_anim; }
bool isConnectByMouseEnabled() const { return m_connect; }
bool isTraceConsiderBuses() const { return m_trace_with_buses; }
bool isPinMulticonnectEnabled() const { return m_pin_mc; }
bool isMiniMapEnabled() const { return minimap; }
bool isZoomWheelOnly() const { return wheel_zoom; }
bool isBusSquareNodes() const { return square_node; }
double gridStep() const { return grid_step; }
double gridPointsWidth() const { return grid_points; }
SelectionMode selectionMode() const { return smode; }
void setSelectionMode(SelectionMode mode) { smode = mode; }
void addItems(QList<QGraphicsItem * > items) {foreach (QGraphicsItem * i, items) addItem(i);}
QList<BlockBusItem * > buses() const;
QList<BlockBusItem * > wrongConnectedBuses() const;
QList<BlockItem * > blocks() const;
QList<QGraphicsItem * > decors() const;
void addItems(QList<QGraphicsItem *> items) {
foreach(QGraphicsItem * i, items)
addItem(i);
}
QList<BlockBusItem *> buses() const;
QList<BlockBusItem *> wrongConnectedBuses() const;
QList<BlockItem *> blocks() const;
QList<QGraphicsItem *> decors() const;
BlockBusItem * connectionBus(BlockItem * b0, BlockItem * b1) const;
QList<BlockBusItem * > connectionBuses(BlockItem * b0, BlockItem * b1) const;
QList<BlockBusItem *> connectionBuses(BlockItem * b0, BlockItem * b1) const;
bool connectPins(BlockItemPin * p0, BlockItemPin * p1);
QList<BlockItem * > selectedBlocks() const;
QList<QGraphicsItem * > selectedDecors() const;
QList<BlockItem *> selectedBlocks() const;
QList<QGraphicsItem *> selectedDecors() const;
void setTransform(const QTransform & matrix, bool combine = false);
void centerOn(const QPointF & pos);
@@ -149,42 +152,42 @@ protected:
void trace(QPointF scene_pos_from, QPointF scene_pos_to, BlockBusItem * bus, bool primary = true);
void clearBusStates();
void matchBus();
bool connectTmpToBus(BlockBusItem* bus);
bool connectTmpToBus(BlockBusItem * bus);
void markPins(int bus_type);
void unmarkPins(bool to_normal = false);
void hoverAcceptedPin(BlockItemPin * pin, bool hover);
void unhoverPins(BlockItemPin * excl_pin = 0);
void simplifyBuses();
void moveBuses(const QList<QGraphicsItem * > & items, QPointF dp);
QList<BlockBusItem * > internalBuses(const QList<BlockItem * > & items);
QList<BlockItemPin * > nearPins(BlockItemPin * pin, Qt::KeyboardModifiers km);
BlockItemPin * getPin(const QList<QGraphicsItem * > & list) const;
void moveBuses(const QList<QGraphicsItem *> & items, QPointF dp);
QList<BlockBusItem *> internalBuses(const QList<BlockItem *> & items);
QList<BlockItemPin *> nearPins(BlockItemPin * pin, Qt::KeyboardModifiers km);
BlockItemPin * getPin(const QList<QGraphicsItem *> & list) const;
void highlightNearPins(BlockItemPin * pin, Qt::KeyboardModifiers km);
void hideTmpBuses(bool clear = true);
double _thumb() const {return _talpha;}
double _thumb() const { return _talpha; }
QRectF _nav() const;
void animateNav(QRectF d, double scl = 0.);
void scrollFromThumb();
void deleteCopyTemp();
void emitActionEvent(BlockItemBase::Action action, QList<QGraphicsItem * > items);
void emitActionEvent(BlockItemBase::Action action, QList<QGraphicsItem *> items);
void setGhost(BlockItem * item);
void clearGhost();
BlockItem * ghost() const {return ghost_;}
BlockItem * ghost() const { return ghost_; }
virtual void loadBus(BlockBusItem * bus) {}
virtual void copyBlocks(QList<BlockItem * > items, QPointF offset) {}
virtual void copyBuses(QList<BlockBusItem * > items, QPointF offset) {}
virtual void copyBlocks(QList<BlockItem *> items, QPointF offset) {}
virtual void copyBuses(QList<BlockBusItem *> items, QPointF offset) {}
virtual void newBusStarted(int bus_type) {}
virtual void newBusCreated(BlockBusItem * bus) {}
QGraphicsScene * scene_;
QGraphicsRectItem sel_rect;
QGraphicsItem * mm_ci;
QList<QGraphicsItem * > sel_items;
QList<BlockItem * > copy_items;
QList<BlockItemPin * > last_multiconnect_pl;
QList<BlockBusItem * > copy_buses, tmp_buses;
BlockBusItem tmp_bus, * match_bus, * bus_from, * cur_bus;
QList<QGraphicsItem *> sel_items;
QList<BlockItem *> copy_items;
QList<BlockItemPin *> last_multiconnect_pl;
QList<BlockBusItem *> copy_buses, tmp_buses;
BlockBusItem tmp_bus, *match_bus, *bus_from, *cur_bus;
BlockItemPin * hpin;
BlockItem * ghost_;
BlockViewWavetrace wavetrace;
@@ -200,7 +203,8 @@ protected:
Qt::KeyboardModifiers mm_mods;
QPropertyAnimation thumb_anim, nav_anim;
int timer_thumb, thumb_hide_delay, thick;
bool mm_drag, new_bus, new_branch, moved, mm_cancel, iconnect, mm_copy, mm_thumb, ae_enabled, is_nav_anim, is_block_anim, move_bus_point;
bool mm_drag, new_bus, new_branch, moved, mm_cancel, iconnect, mm_copy, mm_thumb, ae_enabled, is_nav_anim, is_block_anim,
move_bus_point;
bool grid_visible, grid_snap, pm_connect, navigation, m_connect, m_trace_with_buses, m_pin_mc, minimap, prev_tcb, wheel_zoom;
bool nav_prev_aa, nav_prev_imaa, nav_prev_grid, square_node, block_emit_selection;
double grid_step, grid_points, cur_scl, _talpha, thumb_scl;
@@ -225,28 +229,41 @@ protected slots:
void startBusPointMove(int bus_type);
void pinHoverInOut(BlockItemPin * pin);
void checkPaste(bool queued);
void checkPaste() {checkPaste(false);}
void checkPaste() { checkPaste(false); }
public slots:
void setGridPen(const QPen & pen) {grid_pen = pen; _updateBack();}
void setGridVisible(bool yes) {grid_visible = yes; _updateBack();}
void setSnapToGrid(bool yes) {grid_snap = yes;}
void setGridStep(double step) {grid_step = step; applyGridStep(); _updateBack();}
void setGridPointsWidth(double width_) {grid_points = width_; _updateBack();}
void setPostMoveConnectEnabled(bool on) {pm_connect = on;}
void setNavigationEnabled(bool on) {navigation = on;}
void setNavigateAnimationEnabled(bool on) {is_nav_anim = on;}
void setBlockAnimationEnabled(bool on) {is_block_anim = on;}
void setConnectByMouseEnabled(bool on) {m_connect = on;}
void setTraceConsiderBuses(bool on) {m_trace_with_buses = on;}
void setPinMulticonnectEnabled(bool on) {m_pin_mc = on;}
void setMiniMapEnabled(bool on) {minimap = on;}
void setZoomWheelOnly(bool on) {wheel_zoom = on;}
void setGridPen(const QPen & pen) {
grid_pen = pen;
_updateBack();
}
void setGridVisible(bool yes) {
grid_visible = yes;
_updateBack();
}
void setSnapToGrid(bool yes) { grid_snap = yes; }
void setGridStep(double step) {
grid_step = step;
applyGridStep();
_updateBack();
}
void setGridPointsWidth(double width_) {
grid_points = width_;
_updateBack();
}
void setPostMoveConnectEnabled(bool on) { pm_connect = on; }
void setNavigationEnabled(bool on) { navigation = on; }
void setNavigateAnimationEnabled(bool on) { is_nav_anim = on; }
void setBlockAnimationEnabled(bool on) { is_block_anim = on; }
void setConnectByMouseEnabled(bool on) { m_connect = on; }
void setTraceConsiderBuses(bool on) { m_trace_with_buses = on; }
void setPinMulticonnectEnabled(bool on) { m_pin_mc = on; }
void setMiniMapEnabled(bool on) { minimap = on; }
void setZoomWheelOnly(bool on) { wheel_zoom = on; }
void setBusSquareNodes(bool yes);
void zoom(double factor);
void zoomIn() {zoom(1.2);}
void zoomOut() {zoom(1. / 1.2);}
void zoomIn() { zoom(1.2); }
void zoomOut() { zoom(1. / 1.2); }
void zoomReset();
void copyToClipboard();
@@ -261,17 +278,16 @@ public slots:
void addItem(QGraphicsItem * item, bool emit_action = true);
signals:
void blockDoubleClicked(BlockItem * );
void blockHoverEnter(BlockItem * );
void blockHoverLeave(BlockItem * );
void busDoubleClicked(BlockBusItem * );
void schemeAction(BlockItemBase::Action action, QList<QGraphicsItem * > items);
void blockDoubleClicked(BlockItem *);
void blockHoverEnter(BlockItem *);
void blockHoverLeave(BlockItem *);
void busDoubleClicked(BlockBusItem *);
void schemeAction(BlockItemBase::Action action, QList<QGraphicsItem *> items);
void blockRemoved(BlockItem * item);
void connectionsChanged();
void copyEnabledChanged(bool);
void pasteEnabledChanged(bool);
void selectionChanged();
};
#endif // BLOCKVIEW_H

View File

@@ -11,8 +11,7 @@ BlockViewWavetrace::BlockViewWavetrace(int width, int height) {
void BlockViewWavetrace::resize(int width, int height) {
wid = width;
hei = height;
if (field.size() != wid)
field.resize(wid);
if (field.size() != wid) field.resize(wid);
for (int i = 0; i < wid; ++i) {
if (field[i].size() != hei) {
field[i].resize(hei);
@@ -41,8 +40,7 @@ void BlockViewWavetrace::fill(const QRect & rect, short val) {
void BlockViewWavetrace::fill(int px, int py, short val) {
short p = field[px][py].value;
if ((val == HorizontalBus && p == VerticalBus ) ||
(val == VerticalBus && p == HorizontalBus))
if ((val == HorizontalBus && p == VerticalBus) || (val == VerticalBus && p == HorizontalBus))
field[px][py].value = Blocked;
else
field[px][py].value = val;
@@ -54,14 +52,13 @@ bool BlockViewWavetrace::trace(const QPoint & start, const QPoint & finish) {
st = start;
fn = finish;
if (dir_ == NoTrace) return true;
//qDebug() << "trace" << start << finish;
// qDebug() << "trace" << start << finish;
short cl = 0;
QRect frect(0, 0, wid - 1, hei - 1);
QVector<QPoint> cpnts, npnts;
fill(st, cl);
cpnts.push_back(st);
if (field[fn.x()][fn.y()].value == (short)Blocked)
return false;
if (field[fn.x()][fn.y()].value == (short)Blocked) return false;
auto checkAndFill = [this, &npnts, &frect](int x, int y, short acc_dir, short c) {
if (!frect.contains(x, y)) return;
short p = field[x][y].value;
@@ -73,18 +70,16 @@ bool BlockViewWavetrace::trace(const QPoint & start, const QPoint & finish) {
while (cpnts.size() > 0) {
npnts.clear();
cl++;
if (cl >= max_steps)
return false;
if (cl >= max_steps) return false;
for (int i = 0; i < cpnts.size(); ++i) {
if (cpnts[i] == fn)
return true;
checkAndFill(cpnts[i].x() - 1, cpnts[i].y() , (short)VerticalBus , cl);
checkAndFill(cpnts[i].x() + 1, cpnts[i].y() , (short)VerticalBus , cl);
checkAndFill(cpnts[i].x() , cpnts[i].y() - 1, (short)HorizontalBus, cl);
checkAndFill(cpnts[i].x() , cpnts[i].y() + 1, (short)HorizontalBus, cl);
if (cpnts[i] == fn) return true;
checkAndFill(cpnts[i].x() - 1, cpnts[i].y(), (short)VerticalBus, cl);
checkAndFill(cpnts[i].x() + 1, cpnts[i].y(), (short)VerticalBus, cl);
checkAndFill(cpnts[i].x(), cpnts[i].y() - 1, (short)HorizontalBus, cl);
checkAndFill(cpnts[i].x(), cpnts[i].y() + 1, (short)HorizontalBus, cl);
}
cpnts = npnts;
//qDebug() << cl << ": " << cpnts.size();
// qDebug() << cl << ": " << cpnts.size();
}
return false;
}
@@ -99,25 +94,22 @@ void BlockViewWavetrace::gatherPath() {
}
int pa = -1, ca = -1;
bool first = true;
short cl = field[fn.x()][fn.y()].value;
short cl = field[fn.x()][fn.y()].value;
QRect frect(0, 0, wid, hei);
QPoint cpnt = fn;
auto checkAndStep = [this, &cpnt, &first, &frect] (int dir, short c, int & ca_, int pa_)->bool {
QPoint cpnt = fn;
auto checkAndStep = [this, &cpnt, &first, &frect](int dir, short c, int & ca_, int pa_) -> bool {
int cx = cpnt.x() + dps[dir].x();
int cy = cpnt.y() + dps[dir].y();
if (frect.contains(cx, cy)) {
const Cell & cell(field[cx][cy]);
if (cell.value == c) {
if (cell.direction == HorizontalBus || cell.direction == VerticalBus) {
if (dps[dir].x() == 0 && cell.direction == VerticalBus)
return false;
if (dps[dir].y() == 0 && cell.direction == HorizontalBus)
return false;
if (dps[dir].x() == 0 && cell.direction == VerticalBus) return false;
if (dps[dir].y() == 0 && cell.direction == HorizontalBus) return false;
}
ca_ = QLineF(QPointF(cx, cy), cpnt).angle();
if (ca_ != pa_ && !first)
path_.push_front(cpnt);
cpnt = QPoint(cx, cy);
if (ca_ != pa_ && !first) path_.push_front(cpnt);
cpnt = QPoint(cx, cy);
first = false;
return true;
}
@@ -132,7 +124,6 @@ void BlockViewWavetrace::gatherPath() {
if (checkAndStep(1, cl, ca, pa)) continue;
if (checkAndStep(2, cl, ca, pa)) continue;
if (checkAndStep(3, cl, ca, pa)) continue;
}
path_.push_front(st);
}

View File

@@ -1,20 +1,20 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BLOCKVIEWWAVETRACE_H
@@ -27,28 +27,37 @@
class QAD_BLOCKVIEW_EXPORT BlockViewWavetrace {
public:
BlockViewWavetrace(int width = 1, int height = 1);
enum CellState {Empty = -1, Blocked = -2, HorizontalBus = -3, VerticalBus = -4};
enum Direction {NoTrace, Horizontal, Vertical};
int width() const {return wid;}
int height() const {return hei;}
enum CellState {
Empty = -1,
Blocked = -2,
HorizontalBus = -3,
VerticalBus = -4
};
enum Direction {
NoTrace,
Horizontal,
Vertical
};
int width() const { return wid; }
int height() const { return hei; }
void resize(int width, int height);
void resize(const QSize & sz) {resize(sz.width(), sz.height());}
void resize(const QSize & sz) { resize(sz.width(), sz.height()); }
void fill(short val = -1);
void fill(const QRect & rect, short val = -1);
void fill(const QPoint & point, short val = -1) {fill(point.x(), point.y(), val);}
void fill(const QPoint & point, short val = -1) { fill(point.x(), point.y(), val); }
void fill(int px, int py, short val);
void fill(BlockViewWavetrace::CellState val = Empty) {fill((short)val);}
void fill(const QRect & rect, BlockViewWavetrace::CellState val = Empty) {fill(rect, (short)val);}
void fill(const QPoint & point, BlockViewWavetrace::CellState val = Empty) {fill(point, (short)val);}
void fill(int px, int py, BlockViewWavetrace::CellState val = Empty) {fill(px, py, (short)val);}
void clear() {fill(-1);}
void fill(BlockViewWavetrace::CellState val = Empty) { fill((short)val); }
void fill(const QRect & rect, BlockViewWavetrace::CellState val = Empty) { fill(rect, (short)val); }
void fill(const QPoint & point, BlockViewWavetrace::CellState val = Empty) { fill(point, (short)val); }
void fill(int px, int py, BlockViewWavetrace::CellState val = Empty) { fill(px, py, (short)val); }
void clear() { fill(-1); }
bool trace(const QPoint & start, const QPoint & finish);
Direction preferredDirection() const {return dir_;}
Direction preferredDirection() const { return dir_; }
void setPreferredDirection(Direction dir);
void setMaximumSteps(int steps) {max_steps = steps;}
int maximumSteps() const {return max_steps;}
void setMaximumSteps(int steps) { max_steps = steps; }
int maximumSteps() const { return max_steps; }
void gatherPath();
const QVector<QPoint> & path() const;
@@ -61,11 +70,10 @@ private:
int wid, hei, max_steps;
Direction dir_;
QVector<QVector<Cell> > field;
QVector<QVector<Cell>> field;
QVector<QPoint> path_;
QVector<QPoint> jumps;
QPoint dps[4], st, fn;
};
#endif // BLOCKVIEWWAVETRACE_H

View File

@@ -1,23 +1,25 @@
#include "drawtools.h"
#include "ui_drawtools.h"
#include "alignedtextitem.h"
#include <QGraphicsLineItem>
#include <QLineEdit>
#include <QLabel>
#include <QMouseEvent>
#include <QFileDialog>
#include <QImageReader>
#include <QDialogButtonBox>
#include "ui_drawtools.h"
#include <QClipboard>
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QGraphicsLineItem>
#include <QImageReader>
#include <QLabel>
#include <QLineEdit>
#include <QMouseEvent>
_DTSizeItem::_DTSizeItem(): QGraphicsObject() {
cur_item = nullptr;
grid = 10.;
cur_item = nullptr;
grid = 10.;
in_process = can_drag = false;
setData(bvidItemSelection, true);
for (int i = 0; i < 8; ++i) {
//qDebug() << &(rects[i]);
// qDebug() << &(rects[i]);
rects[i].setData(bvidItemSelection, true);
rects[i].setData(bvidDTHandle, true);
rects[i].setFlag(QGraphicsItem::ItemIgnoresTransformations);
@@ -32,7 +34,7 @@ _DTSizeItem::_DTSizeItem(): QGraphicsObject() {
_DTSizeItem::~_DTSizeItem() {
assignObject(nullptr);
//qDebug() << "!!!";
// qDebug() << "!!!";
}
@@ -52,15 +54,15 @@ void _DTSizeItem::assignObject(QGraphicsItem * item) {
}
if (cur_item->scene()) {
if (!cur_item->scene()->views().isEmpty()) {
grid = qobject_cast<BlockView*>(cur_item->scene()->views()[0])->gridStep();
grid = qobject_cast<BlockView *>(cur_item->scene()->views()[0])->gridStep();
}
}
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
if (irect || iell || iline) {
resizeHandles();
is_line = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
is_line = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
for (int i = 0; i < (is_line ? 2 : 8); ++i) {
rects[i].setParentItem(cur_item);
rects[i].installSceneEventFilter(this);
@@ -78,26 +80,36 @@ void _DTSizeItem::moveRects() {
QRectF rect = itemRect(cur_item);
QPointF tl = rect.topLeft(), tr = rect.topRight(), bl = rect.bottomLeft(), br = rect.bottomRight();
if (is_line) {
rects[0].setPos(tl); rects[0].setData(2001, int(Qt::SizeAllCursor));
rects[1].setPos(br); rects[1].setData(2001, int(Qt::SizeAllCursor));
rects[0].setPos(tl);
rects[0].setData(2001, int(Qt::SizeAllCursor));
rects[1].setPos(br);
rects[1].setData(2001, int(Qt::SizeAllCursor));
} else {
rects[0].setPos(tl); rects[0].setData(2001, int(Qt::SizeFDiagCursor));
rects[1].setPos((tl + tr) / 2.); rects[1].setData(2001, int(Qt::SizeVerCursor));
rects[2].setPos(tr); rects[2].setData(2001, int(Qt::SizeBDiagCursor));
rects[3].setPos((tr + br) / 2.); rects[3].setData(2001, int(Qt::SizeHorCursor));
rects[4].setPos(br); rects[4].setData(2001, int(Qt::SizeFDiagCursor));
rects[5].setPos((br + bl) / 2.); rects[5].setData(2001, int(Qt::SizeVerCursor));
rects[6].setPos(bl); rects[6].setData(2001, int(Qt::SizeBDiagCursor));
rects[7].setPos((bl + tl) / 2.); rects[7].setData(2001, int(Qt::SizeHorCursor));
rects[0].setPos(tl);
rects[0].setData(2001, int(Qt::SizeFDiagCursor));
rects[1].setPos((tl + tr) / 2.);
rects[1].setData(2001, int(Qt::SizeVerCursor));
rects[2].setPos(tr);
rects[2].setData(2001, int(Qt::SizeBDiagCursor));
rects[3].setPos((tr + br) / 2.);
rects[3].setData(2001, int(Qt::SizeHorCursor));
rects[4].setPos(br);
rects[4].setData(2001, int(Qt::SizeFDiagCursor));
rects[5].setPos((br + bl) / 2.);
rects[5].setData(2001, int(Qt::SizeVerCursor));
rects[6].setPos(bl);
rects[6].setData(2001, int(Qt::SizeBDiagCursor));
rects[7].setPos((bl + tl) / 2.);
rects[7].setData(2001, int(Qt::SizeHorCursor));
}
}
void _DTSizeItem::applyRect() {
if (!cur_item) return;
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
if (irect) irect->setRect(nrect);
if (iell) iell->setRect(nrect);
if (iline) iline->setLine(QLineF(nrect.topLeft(), nrect.bottomRight()));
@@ -105,21 +117,27 @@ void _DTSizeItem::applyRect() {
void _DTSizeItem::doubleClick() {
QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<QGraphicsSimpleTextItem*>(cur_item);
AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem*>(cur_item);
QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(cur_item);
QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<QGraphicsSimpleTextItem *>(cur_item);
AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem *>(cur_item);
QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem *>(cur_item);
if (itext || iatext) {
QMetaObject::invokeMethod(this, [this](){textEditRequest();}, Qt::QueuedConnection);
QMetaObject::invokeMethod(
this,
[this]() { textEditRequest(); },
Qt::QueuedConnection);
}
if (ipixmap) {
QMetaObject::invokeMethod(this, [this](){pixmapEditRequest();}, Qt::QueuedConnection);
QMetaObject::invokeMethod(
this,
[this]() { pixmapEditRequest(); },
Qt::QueuedConnection);
}
}
void _DTSizeItem::resizeHandles() {
double sz = fontHeight() / 3.;
QRectF r(-sz, -sz, sz*2, sz*2);
QRectF r(-sz, -sz, sz * 2, sz * 2);
for (int i = 0; i < 8; ++i) {
rects[i].setRect(r);
}
@@ -128,9 +146,9 @@ void _DTSizeItem::resizeHandles() {
QRectF _DTSizeItem::itemRect(const QGraphicsItem *) const {
if (!cur_item) return QRectF();
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
if (irect) return irect->rect();
if (iell) return iell->rect();
if (iline) return QRectF(iline->line().p1(), iline->line().p2());
@@ -148,7 +166,7 @@ QRectF _DTSizeItem::boundingRect() const {
bool _DTSizeItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) {
QGraphicsSceneMouseEvent * me = (QGraphicsSceneMouseEvent * )event;
QGraphicsSceneMouseEvent * me = (QGraphicsSceneMouseEvent *)event;
if (watched == cur_item) {
if (event->type() == QEvent::Close) {
assignObject(nullptr);
@@ -166,11 +184,13 @@ bool _DTSizeItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) {
view_ = nullptr;
switch (event->type()) {
case QEvent::GraphicsSceneHoverEnter:
if (watched->scene()) if (!watched->scene()->views().isEmpty()) view_ = watched->scene()->views()[0];
if (watched->scene())
if (!watched->scene()->views().isEmpty()) view_ = watched->scene()->views()[0];
if (view_) view_->setCursor(Qt::CursorShape(watched->data(2001).toInt()));
break;
case QEvent::GraphicsSceneHoverLeave:
if (watched->scene()) if (!watched->scene()->views().isEmpty()) view_ = watched->scene()->views()[0];
if (watched->scene())
if (!watched->scene()->views().isEmpty()) view_ = watched->scene()->views()[0];
if (view_) view_->unsetCursor();
break;
case QEvent::GraphicsSceneMousePress:
@@ -181,7 +201,7 @@ bool _DTSizeItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) {
moveRects();
}
in_process = false;
pp = quantize(me->scenePos(), grid);
pp = quantize(me->scenePos(), grid);
cur_item->setData(2000, itemRect(cur_item));
return true;
case QEvent::GraphicsSceneMouseMove:
@@ -189,7 +209,7 @@ bool _DTSizeItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) {
sp = quantize(me->scenePos(), grid);
if (pp != sp && can_drag) {
in_process = true;
nrect = itemRect(cur_item);
nrect = itemRect(cur_item);
if (is_line) {
if (watched == &(rects[0])) nrect.setTopLeft(rects[0].pos() + (sp - pp));
if (watched == &(rects[1])) nrect.setBottomRight(rects[1].pos() + (sp - pp));
@@ -213,7 +233,7 @@ bool _DTSizeItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) {
case QEvent::GraphicsSceneMouseRelease:
if (in_process) emit sizeChanged();
in_process = false;
can_drag = false;
can_drag = false;
return true;
default: break;
}
@@ -221,19 +241,21 @@ bool _DTSizeItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) {
}
DrawTools::DrawTools(BlockView * parent): QWidget(parent),
actions_Z_up(this), actions_Z_top(this), actions_Z_down(this), actions_Z_bottom(this) {
DrawTools::DrawTools(BlockView * parent)
: QWidget(parent)
, actions_Z_up(this)
, actions_Z_top(this)
, actions_Z_down(this)
, actions_Z_bottom(this) {
widget_props = new QWidget();
ui = new Ui::DrawTools();
ui = new Ui::DrawTools();
ui->setupUi(widget_props);
ui->labelPen->setMinimumSize(preferredIconSize(1.5, widget_props));
ui->labelPen->setMaximumSize(ui->labelPen->minimumSize());
ui->labelBrush->setMinimumSize(ui->labelPen->minimumSize());
ui->labelBrush->setMaximumSize(ui->labelBrush->minimumSize());
widget_props->setEnabled(false);
int fh = qMax<int>(fontHeight(this), 22);
int fh = qMax<int>(fontHeight(this), 22);
int thick = lineThickness(this);
QSize sz(fh * 2.5, fh);
ui->comboLineStyle->setIconSize(sz);
@@ -251,36 +273,37 @@ actions_Z_up(this), actions_Z_top(this), actions_Z_down(this), actions_Z_bottom(
#else
setAlignCompact(false);
#endif
menu_hor.addActions(QList<QAction*>() << ui->actionLeft << ui->actionHCenter << ui->actionRight);
menu_ver.addActions(QList<QAction*>() << ui->actionTop << ui->actionVCenter << ui->actionBottom);
menu_hor.addActions(QList<QAction *>() << ui->actionLeft << ui->actionHCenter << ui->actionRight);
menu_ver.addActions(QList<QAction *>() << ui->actionTop << ui->actionVCenter << ui->actionBottom);
ui->buttonAlignHor->setMenu(&menu_hor);
ui->buttonAlignVer->setMenu(&menu_ver);
new_type = -1;
new_item = cur_item = nullptr;
view_ = nullptr;
resize_enabled = true;
view_ = nullptr;
resize_enabled = true;
text_dlg.setLayout(new QBoxLayout(QBoxLayout::TopToBottom));
QDialogButtonBox * bbox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
connect(bbox, SIGNAL(accepted()), &text_dlg, SLOT(accept()));
connect(bbox, SIGNAL(rejected()), &text_dlg, SLOT(reject()));
text_dlg.layout()->addWidget(&text_edit);
text_dlg.layout()->addWidget(bbox);
actions_Z_up.setIcon(QIcon(":/icons/z-up.png")); actions_Z_up.setEnabled(false);
actions_Z_top.setIcon(QIcon(":/icons/z-top.png")); actions_Z_top.setEnabled(false);
actions_Z_down.setIcon(QIcon(":/icons/z-down.png")); actions_Z_down.setEnabled(false);
actions_Z_bottom.setIcon(QIcon(":/icons/z-bottom.png")); actions_Z_bottom.setEnabled(false);
actions_add << newAction(QIcon(":/icons/draw-rectangle.png"), 1)
<< newAction(QIcon(":/icons/draw-ellipse.png"), 2)
<< newAction(QIcon(":/icons/draw-line.png"), 4)
<< newAction(QIcon(":/icons/draw-text.png"), 0)
actions_Z_up.setIcon(QIcon(":/icons/z-up.png"));
actions_Z_up.setEnabled(false);
actions_Z_top.setIcon(QIcon(":/icons/z-top.png"));
actions_Z_top.setEnabled(false);
actions_Z_down.setIcon(QIcon(":/icons/z-down.png"));
actions_Z_down.setEnabled(false);
actions_Z_bottom.setIcon(QIcon(":/icons/z-bottom.png"));
actions_Z_bottom.setEnabled(false);
actions_add << newAction(QIcon(":/icons/draw-rectangle.png"), 1) << newAction(QIcon(":/icons/draw-ellipse.png"), 2)
<< newAction(QIcon(":/icons/draw-line.png"), 4) << newAction(QIcon(":/icons/draw-text.png"), 0)
<< newAction(QIcon(":/icons/view-preview.png"), 3);
buttons_align << ui->buttonAlignTL << ui->buttonAlignTC << ui->buttonAlignTR
<< ui->buttonAlignCL << ui->buttonAlignCC << ui->buttonAlignCR
<< ui->buttonAlignBL << ui->buttonAlignBC << ui->buttonAlignBR;
foreach (QAction * a, actions_add) {
buttons_align << ui->buttonAlignTL << ui->buttonAlignTC << ui->buttonAlignTR << ui->buttonAlignCL << ui->buttonAlignCC
<< ui->buttonAlignCR << ui->buttonAlignBL << ui->buttonAlignBC << ui->buttonAlignBR;
foreach(QAction * a, actions_add) {
connect(a, SIGNAL(toggled(bool)), this, SLOT(toggleNewItem(bool)));
}
foreach (QToolButton * b, buttons_align) {
foreach(QToolButton * b, buttons_align) {
connect(b, SIGNAL(clicked(bool)), this, SLOT(alignClicked()));
}
connect(ui->buttonImage, SIGNAL(clicked(bool)), this, SLOT(buttonImage_clicked()));
@@ -328,8 +351,7 @@ DrawTools::~DrawTools() {
void DrawTools::retranslate() {
QStringList styles;
styles << tr("NoPen") << tr("Solid") << tr("Dash")
<< tr("Dot") << tr("Dash-Dot") << tr("Dash-Dot-Dot");
styles << tr("NoPen") << tr("Solid") << tr("Dash") << tr("Dot") << tr("Dash-Dot") << tr("Dash-Dot-Dot");
for (int i = 0; i < styles.size(); i++) {
ui->comboLineStyle->setItemText(i, styles[i]);
}
@@ -380,7 +402,7 @@ bool DrawTools::eventFilter(QObject * o, QEvent * e) {
delete new_item;
new_item = nullptr;
if (!me->modifiers().testFlag(Qt::ControlModifier)) {
foreach (QAction * a, actions_add) {
foreach(QAction * a, actions_add) {
a->setChecked(false);
}
new_type = -1;
@@ -388,26 +410,21 @@ bool DrawTools::eventFilter(QObject * o, QEvent * e) {
}
}
new_item = nullptr;
pp = sp;
pp = sp;
switch (new_type) {
case 0:
new_item = new AlignedTextItem();
qgraphicsitem_cast<AlignedTextItem *>(new_item)->setText("Text");
qgraphicsitem_cast<AlignedTextItem *>(new_item)->setPos(sp);
break;
case 1:
new_item = new QGraphicsRectItem();
break;
case 2:
new_item = new QGraphicsEllipseItem();
break;
case 1: new_item = new QGraphicsRectItem(); break;
case 2: new_item = new QGraphicsEllipseItem(); break;
case 3:
new_item = new QGraphicsPixmapItem(QPixmap(":/icons/view-preview.png"));
qgraphicsitem_cast<QGraphicsPixmapItem *>(new_item)->setPos(sp - QPointF(new_item->boundingRect().width() / 2, new_item->boundingRect().height() / 2));
break;
case 4:
new_item = new QGraphicsLineItem(QLineF(sp, sp));
qgraphicsitem_cast<QGraphicsPixmapItem *>(new_item)->setPos(
sp - QPointF(new_item->boundingRect().width() / 2, new_item->boundingRect().height() / 2));
break;
case 4: new_item = new QGraphicsLineItem(QLineF(sp, sp)); break;
};
if (new_item) {
if (new_type == 1 || new_type == 2) {
@@ -424,21 +441,14 @@ bool DrawTools::eventFilter(QObject * o, QEvent * e) {
if (new_item) {
mr = new_item->mapRectFromScene(QRectF(pp, sp).normalized());
switch (new_type) {
case 0:
qgraphicsitem_cast<AlignedTextItem *>(new_item)->setPos(sp);
break;
case 1:
qgraphicsitem_cast<QGraphicsRectItem *>(new_item)->setRect(mr);
break;
case 2:
qgraphicsitem_cast<QGraphicsEllipseItem *>(new_item)->setRect(mr);
break;
case 0: qgraphicsitem_cast<AlignedTextItem *>(new_item)->setPos(sp); break;
case 1: qgraphicsitem_cast<QGraphicsRectItem *>(new_item)->setRect(mr); break;
case 2: qgraphicsitem_cast<QGraphicsEllipseItem *>(new_item)->setRect(mr); break;
case 3:
qgraphicsitem_cast<QGraphicsPixmapItem *>(new_item)->setPos(sp - QPointF(new_item->boundingRect().width() / 2, new_item->boundingRect().height() / 2));
break;
case 4:
qgraphicsitem_cast<QGraphicsLineItem *>(new_item)->setLine(QLineF(pp, sp));
qgraphicsitem_cast<QGraphicsPixmapItem *>(new_item)->setPos(
sp - QPointF(new_item->boundingRect().width() / 2, new_item->boundingRect().height() / 2));
break;
case 4: qgraphicsitem_cast<QGraphicsLineItem *>(new_item)->setLine(QLineF(pp, sp)); break;
};
return true;
}
@@ -457,7 +467,7 @@ bool DrawTools::eventFilter(QObject * o, QEvent * e) {
}
new_item = nullptr;
if (!me->modifiers().testFlag(Qt::ControlModifier)) {
foreach (QAction * a, actions_add) {
foreach(QAction * a, actions_add) {
a->setChecked(false);
}
new_type = -1;
@@ -497,8 +507,8 @@ QAction * DrawTools::newAction(const QIcon & icon, int type) {
void DrawTools::toggleNewItem(bool on) {
QAction * sa = (QAction * )sender();
foreach (QAction * a, actions_add) {
QAction * sa = (QAction *)sender();
foreach(QAction * a, actions_add) {
if (a != sa) a->setChecked(false);
}
if (!on) {
@@ -512,12 +522,12 @@ void DrawTools::toggleNewItem(bool on) {
void DrawTools::alignClicked() {
QToolButton * sb = (QToolButton * )sender();
foreach (QToolButton * b, buttons_align) {
QToolButton * sb = (QToolButton *)sender();
foreach(QToolButton * b, buttons_align) {
if (b != sb) b->setChecked(false);
}
sb->setChecked(true);
align = Qt::Alignment();
align = Qt::Alignment();
QString als = sb->objectName().right(2).toLower();
if (als[0] == 't') align |= Qt::AlignTop;
if (als[0] == 'c') align |= Qt::AlignVCenter;
@@ -563,7 +573,7 @@ void DrawTools::blockPropSignals(bool block_) {
ui->actionHCenter->blockSignals(block_);
ui->actionLeft->blockSignals(block_);
ui->actionRight->blockSignals(block_);
foreach (QToolButton * b, buttons_align) {
foreach(QToolButton * b, buttons_align) {
b->blockSignals(block_);
}
}
@@ -571,8 +581,12 @@ void DrawTools::blockPropSignals(bool block_) {
void DrawTools::actionAlignTrigger(bool vert, Qt::AlignmentFlag value) {
blockPropSignals(true);
if (vert) foreach (QAction * a, menu_ver.actions()) a->setChecked(false);
else foreach (QAction * a, menu_hor.actions()) a->setChecked(false);
if (vert)
foreach(QAction * a, menu_ver.actions())
a->setChecked(false);
else
foreach(QAction * a, menu_hor.actions())
a->setChecked(false);
align = align & (vert ? Qt::AlignHorizontal_Mask : Qt::AlignVertical_Mask);
align |= value;
qobject_cast<QAction *>(sender())->setChecked(true);
@@ -583,7 +597,9 @@ void DrawTools::actionAlignTrigger(bool vert, Qt::AlignmentFlag value) {
void DrawTools::emitZAvailabe(QGraphicsItem * item) {
BlockView * view = nullptr;
if (item) if (item->scene()) if (!item->scene()->views().isEmpty()) view = qobject_cast<BlockView *>(item->scene()->views()[0]);
if (item)
if (item->scene())
if (!item->scene()->views().isEmpty()) view = qobject_cast<BlockView *>(item->scene()->views()[0]);
if (!view) {
moveZUpAvailable(false);
moveZDownAvailable(false);
@@ -633,11 +649,11 @@ void DrawTools::selectionChanged() {
return;
}
QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<QGraphicsSimpleTextItem *>(cur_item);
AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem *>(cur_item);
QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem *>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem *>(cur_item);
QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem *>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
blockPropSignals(true);
if (itext) {
ui->stackedProperties->setCurrentIndex(0);
@@ -658,20 +674,40 @@ void DrawTools::selectionChanged() {
font_dlg.setCurrentFont(iatext->font());
font_dlg.blockSignals(false);
setToolButtonsEnabled(true, false, false);
foreach (QAction * a, menu_hor.actions()) a->setChecked(false);
foreach (QAction * a, menu_ver.actions()) a->setChecked(false);
foreach(QAction * a, menu_hor.actions())
a->setChecked(false);
foreach(QAction * a, menu_ver.actions())
a->setChecked(false);
align = iatext->alignment();
QString als;
if (align.testFlag(Qt::AlignTop)) {als += "T"; ui->actionTop->setChecked(true);}
if (align.testFlag(Qt::AlignVCenter)) {als += "C"; ui->actionVCenter->setChecked(true);}
if (align.testFlag(Qt::AlignBottom)) {als += "B"; ui->actionBottom->setChecked(true);}
if (align.testFlag(Qt::AlignLeft)) {als += "L"; ui->actionLeft->setChecked(true);}
if (align.testFlag(Qt::AlignHCenter)) {als += "C"; ui->actionHCenter->setChecked(true);}
if (align.testFlag(Qt::AlignRight)) {als += "R"; ui->actionRight->setChecked(true);}
foreach (QToolButton * b, buttons_align) {
if (align.testFlag(Qt::AlignTop)) {
als += "T";
ui->actionTop->setChecked(true);
}
if (align.testFlag(Qt::AlignVCenter)) {
als += "C";
ui->actionVCenter->setChecked(true);
}
if (align.testFlag(Qt::AlignBottom)) {
als += "B";
ui->actionBottom->setChecked(true);
}
if (align.testFlag(Qt::AlignLeft)) {
als += "L";
ui->actionLeft->setChecked(true);
}
if (align.testFlag(Qt::AlignHCenter)) {
als += "C";
ui->actionHCenter->setChecked(true);
}
if (align.testFlag(Qt::AlignRight)) {
als += "R";
ui->actionRight->setChecked(true);
}
foreach(QToolButton * b, buttons_align) {
b->setChecked(false);
}
foreach (QToolButton * b, buttons_align) {
foreach(QToolButton * b, buttons_align) {
if (b->objectName().endsWith(als)) {
b->setChecked(true);
break;
@@ -724,7 +760,7 @@ void DrawTools::selectionChanged() {
void DrawTools::sizeChanged() {
blockPropSignals(true);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
if (irect || iell) {
if (irect) {
@@ -744,17 +780,17 @@ void DrawTools::sizeChanged() {
void DrawTools::propertyChanged() {
if (!cur_item) return;
QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<QGraphicsSimpleTextItem *>(cur_item);
AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem *>(cur_item);
QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem *>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem *>(cur_item);
QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem *>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
if (itext) {
QRectF obr = itext->boundingRect();
itext->setFont(font_dlg.currentFont());
itext->setText(ui->comboText->currentText());
QRectF nbr = itext->boundingRect();
QSizeF ds = (obr.size() - nbr.size()) / 2.;
QSizeF ds = (obr.size() - nbr.size()) / 2.;
itext->setPos(itext->pos() + QPointF(ds.width(), ds.height()));
itext->setBrush(ui->colorButtonPen->color());
} else if (iatext) {
@@ -764,8 +800,8 @@ void DrawTools::propertyChanged() {
iatext->setAlignment(align);
} else if (ipixmap) {
QTransform t = ipixmap->transform();
double det = sqrt(t.determinant());
QSizeF os = ipixmap->boundingRect().size() * det;
double det = sqrt(t.determinant());
QSizeF os = ipixmap->boundingRect().size() * det;
if (det != 0.) t.scale(1. / det, 1. / det);
det = ui->spinScale->value();
t.scale(det, det);
@@ -797,9 +833,9 @@ void DrawTools::propertyChanged() {
void DrawTools::comboLineStyleChanged() {
if (!cur_item) return;
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem *>(cur_item);
QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem *>(cur_item);
if (irect || iell) {
QAbstractGraphicsShapeItem * ishape = nullptr;
if (irect) ishape = irect;
@@ -824,7 +860,7 @@ void DrawTools::buttonImage_clicked() {
if (!pi) return;
QList<QByteArray> sif(QImageReader::supportedImageFormats());
QString f;
foreach (const QByteArray & i, sif) {
foreach(const QByteArray & i, sif) {
if (!f.isEmpty()) f += " ";
f += "*.";
f += i;
@@ -833,11 +869,11 @@ void DrawTools::buttonImage_clicked() {
if (ret.isEmpty()) return;
QImage im(ret);
if (im.isNull()) return;
prev_dir = ret;
prev_dir = ret;
QRectF obr = pi->boundingRect();
pi->setPixmap(QPixmap::fromImage(im));
QRectF nbr = pi->boundingRect();
QSizeF ds = (obr.size() - nbr.size()) / 2.;
QSizeF ds = (obr.size() - nbr.size()) / 2.;
pi->setPos(pi->pos() + QPointF(ds.width(), ds.height()));
changeFinished();
}
@@ -851,7 +887,7 @@ void DrawTools::buttonImagePaste_clicked() {
QRectF obr = pi->boundingRect();
pi->setPixmap(pm);
QRectF nbr = pi->boundingRect();
QSizeF ds = (obr.size() - nbr.size()) / 2.;
QSizeF ds = (obr.size() - nbr.size()) / 2.;
pi->setPos(pi->pos() + QPointF(ds.width(), ds.height()));
changeFinished();
}
@@ -860,7 +896,7 @@ void DrawTools::buttonImagePaste_clicked() {
void DrawTools::buttonFont_clicked() {
if (!cur_item) return;
QGraphicsSimpleTextItem * ti = qgraphicsitem_cast<QGraphicsSimpleTextItem *>(cur_item);
AlignedTextItem * ati = qgraphicsitem_cast<AlignedTextItem *>(cur_item);
AlignedTextItem * ati = qgraphicsitem_cast<AlignedTextItem *>(cur_item);
if (!ti && !ati) return;
QFont font_prev;
if (ti) font_prev = ti->font();
@@ -892,28 +928,32 @@ void DrawTools::actionZ_triggered() {
if (!cur_item) return;
if (cur_item->data(bvidType).toInt() == bvitDecor) {
BlockView * view = nullptr;
if (cur_item->scene()) if (!cur_item->scene()->views().isEmpty()) {
view = qobject_cast<BlockView *>(cur_item->scene()->views()[0]);
}
if (cur_item->scene())
if (!cur_item->scene()->views().isEmpty()) {
view = qobject_cast<BlockView *>(cur_item->scene()->views()[0]);
}
if (!view) return;
QGraphicsScene * scene = view->scene();
QGraphicsScene * scene = view->scene();
QList<QGraphicsItem *> dl = view->decors();
scene->blockSignals(true);
foreach (QGraphicsItem * d, dl) scene->removeItem(d);
foreach(QGraphicsItem * d, dl)
scene->removeItem(d);
int ind = dl.indexOf(cur_item);
dl.removeAt(ind);
if (sender() == &actions_Z_up) dl.insert(ind + 1, cur_item);
if (sender() == &actions_Z_top) dl.append(cur_item);
if (sender() == &actions_Z_down) dl.insert(ind - 1, cur_item);
if (sender() == &actions_Z_bottom) dl.prepend(cur_item);
foreach (QGraphicsItem * d, dl) scene->addItem(d);
foreach(QGraphicsItem * d, dl)
scene->addItem(d);
scene->blockSignals(false);
}
if (cur_item->data(bvidBlockDecor).toBool()) {
BlockItem * bi = qgraphicsitem_cast<BlockItem *>(cur_item->parentItem());
if (!bi) return;
QList<QGraphicsItem *> dl = bi->decors_;
foreach (QGraphicsItem * d, dl) d->setParentItem(nullptr);
foreach(QGraphicsItem * d, dl)
d->setParentItem(nullptr);
int ind = dl.indexOf(cur_item);
dl.removeAt(ind);
if (sender() == &actions_Z_up) dl.insert(ind + 1, cur_item);
@@ -921,7 +961,8 @@ void DrawTools::actionZ_triggered() {
if (sender() == &actions_Z_down) dl.insert(ind - 1, cur_item);
if (sender() == &actions_Z_bottom) dl.prepend(cur_item);
bi->decors_ = dl;
foreach (QGraphicsItem * d, dl) d->setParentItem(bi);
foreach(QGraphicsItem * d, dl)
d->setParentItem(bi);
}
size_item.assignObject(cur_item);
emitZAvailabe(cur_item);

View File

@@ -1,47 +1,48 @@
/*
QAD - Qt ADvanced
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DRAWTOOLS_H
#define DRAWTOOLS_H
#include <QObject>
#include <QAction>
#include <QFontDialog>
#include <QToolButton>
#include <QPlainTextEdit>
#include <QMenu>
#include "blockview.h"
#include "qad_blockview_export.h"
#include <QAction>
#include <QFontDialog>
#include <QMenu>
#include <QObject>
#include <QPlainTextEdit>
#include <QToolButton>
class QComboBox;
class QAD_BLOCKVIEW_EXPORT _DTSizeItem: public QGraphicsObject
{
class QAD_BLOCKVIEW_EXPORT _DTSizeItem: public QGraphicsObject {
Q_OBJECT
public:
_DTSizeItem();
~_DTSizeItem();
void assignObject(QGraphicsItem * item);
protected:
void moveRects();
void applyRect();
@@ -51,7 +52,7 @@ protected:
QRectF boundingRect() const;
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0) {}
bool sceneEventFilter(QGraphicsItem * watched, QEvent * event);
QGraphicsItem * cur_item;
QGraphicsView * view_;
QGraphicsRectItem rects[8];
@@ -59,55 +60,56 @@ protected:
QRectF nrect;
qreal grid;
bool in_process, can_drag, is_line;
signals:
void sizeChanged();
void textEditRequest();
void pixmapEditRequest();
};
namespace Ui {
class DrawTools;
class DrawTools;
}
class QAD_BLOCKVIEW_EXPORT DrawTools: public QWidget
{
class QAD_BLOCKVIEW_EXPORT DrawTools: public QWidget {
Q_OBJECT
Q_PROPERTY(bool resizeHandlesEnabled READ isResizeHandlesEnabled WRITE setResizeHandlesEnabled)
public:
explicit DrawTools(BlockView * parent = 0);
~DrawTools();
void setBlockView(BlockView * v);
void resetSizeTool() {size_item.assignObject(0);}
bool isResizeHandlesEnabled() const {return resize_enabled;}
void resetSizeTool() { size_item.assignObject(0); }
bool isResizeHandlesEnabled() const { return resize_enabled; }
void setAlignCompact(bool yes);
QComboBox * textEditCombo() const;
QList<QAction * > actionsForAdd() const {return actions_add;}
QList<QAction * > actionsForZ() const {return QList<QAction * >() << &actions_Z_bottom << &actions_Z_down << &actions_Z_up << &actions_Z_top;}
QWidget * propertyWidget() const {return widget_props;}
QList<QAction *> actionsForAdd() const { return actions_add; }
QList<QAction *> actionsForZ() const {
return QList<QAction *>() << &actions_Z_bottom << &actions_Z_down << &actions_Z_up << &actions_Z_top;
}
QWidget * propertyWidget() const { return widget_props; }
protected:
bool eventFilter(QObject * o, QEvent * e);
void changeEvent(QEvent * e);
QAction * newAction(const QIcon & icon, int type);
void setToolButtonsEnabled(bool pen, bool brush, bool wid_hei);
void blockPropSignals(bool block_);
void actionAlignTrigger(bool vert, Qt::AlignmentFlag value);
void emitZAvailabe(QGraphicsItem * item = 0);
QWidget * widget_props;
Ui::DrawTools * ui;
BlockView * view_;
QList<QAction * > actions_add;
QList<QAction *> actions_add;
mutable QAction actions_Z_up, actions_Z_top, actions_Z_down, actions_Z_bottom;
QList<QToolButton * > buttons_align;
QGraphicsItem * new_item, * cur_item;
QList<QToolButton *> buttons_align;
QGraphicsItem *new_item, *cur_item;
QFontDialog font_dlg;
QPointF pp;
QDialog text_dlg;
@@ -118,7 +120,7 @@ protected:
int new_type;
bool resize_enabled;
QString prev_dir;
private slots:
void toggleNewItem(bool on);
void alignClicked();
@@ -126,32 +128,40 @@ private slots:
void sizeChanged();
void propertyChanged();
void comboLineStyleChanged();
void changeFinished() {if (cur_item) emit itemEdited(cur_item);}
void moveZUpAvailable(bool yes) {actions_Z_up.setEnabled(yes); actions_Z_top.setEnabled(yes);}
void moveZDownAvailable(bool yes) {actions_Z_down.setEnabled(yes); actions_Z_bottom.setEnabled(yes);}
void changeFinished() {
if (cur_item) emit itemEdited(cur_item);
}
void moveZUpAvailable(bool yes) {
actions_Z_up.setEnabled(yes);
actions_Z_top.setEnabled(yes);
}
void moveZDownAvailable(bool yes) {
actions_Z_down.setEnabled(yes);
actions_Z_bottom.setEnabled(yes);
}
void buttonImage_clicked();
void buttonImagePaste_clicked();
void buttonFont_clicked();
void buttonTextEdit_clicked();
void actionTop_triggered(bool on) {actionAlignTrigger(true, Qt::AlignTop);}
void actionVCenter_triggered(bool on) {actionAlignTrigger(true, Qt::AlignVCenter);}
void actionBottom_triggered(bool on) {actionAlignTrigger(true, Qt::AlignBottom);}
void actionLeft_triggered(bool on) {actionAlignTrigger(false, Qt::AlignLeft);}
void actionHCenter_triggered(bool on) {actionAlignTrigger(false, Qt::AlignHCenter);}
void actionRight_triggered(bool on) {actionAlignTrigger(false, Qt::AlignRight);}
void actionTop_triggered(bool on) { actionAlignTrigger(true, Qt::AlignTop); }
void actionVCenter_triggered(bool on) { actionAlignTrigger(true, Qt::AlignVCenter); }
void actionBottom_triggered(bool on) { actionAlignTrigger(true, Qt::AlignBottom); }
void actionLeft_triggered(bool on) { actionAlignTrigger(false, Qt::AlignLeft); }
void actionHCenter_triggered(bool on) { actionAlignTrigger(false, Qt::AlignHCenter); }
void actionRight_triggered(bool on) { actionAlignTrigger(false, Qt::AlignRight); }
void actionZ_triggered();
public slots:
void setResizeHandlesEnabled(bool on);
signals:
void itemCreated(QGraphicsItem * item);
void itemAddConfirm(QGraphicsItem * item);
void itemEdited(QGraphicsItem * item);
void itemZChanged(QGraphicsItem * item);
private:
void retranslate();
};

View File

@@ -1,5 +1,7 @@
#include "blockview.h"
#include "blockviewplugin.h"
#include "blockview.h"
#include <QtCore/QtPlugin>
@@ -9,8 +11,7 @@ BlockViewPlugin::BlockViewPlugin(QObject * parent): QObject(parent) {
void BlockViewPlugin::initialize(QDesignerFormEditorInterface * /* core */) {
if (m_initialized)
return;
if (m_initialized) return;
// Add extension registrations, etc. here
@@ -66,4 +67,3 @@ QString BlockViewPlugin::domXml() const {
QString BlockViewPlugin::includeFile() const {
return QLatin1String("blockview.h");
}

View File

@@ -8,8 +8,9 @@
# include <QDesignerCustomWidgetInterface>
#endif
class BlockViewPlugin: public QObject, public QDesignerCustomWidgetInterface
{
class BlockViewPlugin
: public QObject
, public QDesignerCustomWidgetInterface {
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)
@@ -30,7 +31,6 @@ public:
private:
bool m_initialized;
};
#endif // BLOCKVIEWPLUGIN_H

View File

@@ -1,13 +1,13 @@
#include "qad_blockview.h"
#include "blockviewplugin.h"
QADBlockView::QADBlockView(QObject * parent): QObject(parent)
{
QADBlockView::QADBlockView(QObject * parent): QObject(parent) {
m_widgets.append(new BlockViewPlugin(this));
}
QList<QDesignerCustomWidgetInterface * > QADBlockView::customWidgets() const {
QList<QDesignerCustomWidgetInterface *> QADBlockView::customWidgets() const {
return m_widgets;
}

View File

@@ -1,23 +1,24 @@
#ifndef QAD_BLOCKVIEW_H
#define QAD_BLOCKVIEW_H
#include <QtDesigner/QtDesigner>
#include <QtCore/qplugin.h>
#include <QtDesigner/QtDesigner>
class QADBlockView: public QObject, public QDesignerCustomWidgetCollectionInterface
{
class QADBlockView
: public QObject
, public QDesignerCustomWidgetCollectionInterface {
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "qad.blockview")
#endif
public:
explicit QADBlockView(QObject * parent = 0);
virtual QList<QDesignerCustomWidgetInterface * > customWidgets() const;
virtual QList<QDesignerCustomWidgetInterface *> customWidgets() const;
private:
QList<QDesignerCustomWidgetInterface * > m_widgets;
QList<QDesignerCustomWidgetInterface *> m_widgets;
};
#endif // QAD_BLOCKVIEW_H