git-svn-id: svn://db.shs.com.ru/libs@316 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2017-11-15 14:27:51 +00:00
parent efeb95c1c3
commit ed362a6b55
8 changed files with 473 additions and 215 deletions

View File

@@ -1,94 +1,7 @@
#include "blockitem.h"
#include "blockview.h"
#include <QApplication>
BlockItemPin::BlockItemPin(Qt::Alignment a, int bus_type_, const QString & text_, QGraphicsItem * _parent): QGraphicsItem(_parent), ell_item(this), text_item(this) {
parent_ = 0;
setData(1004, "pin");
setAcceptHoverEvents(true);
text_item.setData(1002, true);
ell_item.setData(1003, 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);
setState(Disconnected);
setAlignment(a);
setBusType(bus_type_);
setText(text_);
setZValue(2.);
setDirection(BlockItemPin::InputOutput);
_reparent();
}
void BlockItemPin::resizePin(double r) {
ell_item.setRect(-r, -r, r+r, r+r);
}
void BlockItemPin::_init(bool affect_parent) {
text_item.setFont(AlignedTextItem::sceneFont(QApplication::font()));
QRectF tbr = text_item.boundingRect();
const double r = 7.;
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;
default: break;
}
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();
ell_item.setPen(p);
if (scene()) {
text_item.setFont(AlignedTextItem::sceneFont(scene()->font()));
QRectF tbr = text_item.boundingRect();
text_item.resetTransform();
text_item.setPos(0, -tbr.height() / 2.);
text_item.setTransformOriginPoint(0, tbr.height() / 2.);
}
}
QVariant BlockItemPin::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant & value) {
if (change == QGraphicsItem::ItemParentChange)
_reparent();
return QGraphicsItem::itemChange(change, value);
}
void BlockItemPin::hoverEnterEvent(QGraphicsSceneHoverEvent * e) {
bool m_pin_mc(false);
if (scene()) if (!scene()->views().isEmpty())
QMetaObject::invokeMethod(scene()->views()[0], "getPinMC", Q_ARG(bool*, &m_pin_mc));
if ((state() != Disconnected) && !m_pin_mc) return;
saveState();
setState(BlockItemPin::Hover);
update();
}
void BlockItemPin::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) {
restoreState();
update();
}
#define BLOCKITEM_DEFAULT_PIN_MARGIN 20
BlockItem::BlockItem(QGraphicsItem * parent): QGraphicsObject(parent), PropertyStorage(),
@@ -110,6 +23,15 @@ g_main(this), g_selection(this) {
g_selection.setPen(p);
g_selection.setBrush(QColor(128, 128, 255, 32));
pins_margin = BLOCKITEM_DEFAULT_PIN_MARGIN;
anim_thick.setTargetObject(this);
anim_thick.setPropertyName("_thickness");
anim_thick.setEasingCurve(QEasingCurve::OutQuad);
anim_thick.setDuration(300);
anim_sel.setTargetObject(this);
anim_sel.setPropertyName("_selRect");
anim_sel.setEasingCurve(QEasingCurve::OutCubic);
anim_sel.setDuration(400);
t_sel.start();
//g_main.setBrush(QColor(128, 128, 128, 64));
/*
BlockItemPin * pin = new BlockItemPin(Qt::AlignRight, 0, this);
@@ -447,11 +369,25 @@ void BlockItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
void BlockItem::hoverEnterEvent(QGraphicsSceneHoverEvent * e) {
bool anim = ((BlockView *)scene()->views().back())->isBlockAnimationEnabled();
if (anim) {
anim_thick.stop();
anim_thick.setStartValue(thickness());
anim_thick.setEndValue(2);
anim_thick.start();
} else setThickness(2);
emit blockHoverEnter(this);
}
void BlockItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) {
bool anim = ((BlockView *)scene()->views().back())->isBlockAnimationEnabled();
if (anim) {
anim_thick.stop();
anim_thick.setStartValue(thickness());
anim_thick.setEndValue(1);
anim_thick.start();
} else setThickness(1);
emit blockHoverLeave(this);
}
@@ -484,7 +420,39 @@ void BlockItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * optio
QVariant BlockItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant & value) {
if (change == QGraphicsItem::ItemSelectedChange)
if (change == QGraphicsItem::ItemSelectedChange) {
// 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());
anim_sel.setEndValue(enlargedRect(g_main.rect(), 0, 0, 8));
anim_sel.start();
}
t_sel.restart();
g_selection.setVisible(value.toBool());
}
return QGraphicsItem::itemChange(change, value);
}
double BlockItem::thickness() const {
return g_main.pen().widthF();
}
void BlockItem::setThickness(double w) {
QPen p = g_main.pen();
p.setWidthF(w);
g_main.setPen(p);
}
QRectF BlockItem::selectionRect() const {
return g_selection.rect();
}
void BlockItem::setSelectionRect(const QRectF & r) {
g_selection.setRect(r);
}