Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/schematicnode.h"
Toshihiro Shimizu 890ddd
#include <qgraphicsscenemouseevent></qgraphicsscenemouseevent>
Toshihiro Shimizu 890ddd
#include <qstyleoptiongraphicsitem></qstyleoptiongraphicsitem>
Toshihiro Shimizu 890ddd
#include <qkeyevent></qkeyevent>
Toshihiro Shimizu 890ddd
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
#include <qapplication></qapplication>
Toshihiro Shimizu 890ddd
#include <qtextdocument></qtextdocument>
Toshihiro Shimizu 890ddd
#include <qtextcursor></qtextcursor>
Toshihiro Shimizu 890ddd
#include <qtextblock></qtextblock>
Toshihiro Shimizu 890ddd
#include <qmenubar></qmenubar>
Toshihiro Shimizu 890ddd
#include <qpolygonf></qpolygonf>
Toshihiro Shimizu 890ddd
#include "tundo.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/menubarcommand.h"
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//StageSchematicName
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicName::SchematicName(QGraphicsItem *parent, double width, double height)
Toshihiro Shimizu 890ddd
	: QGraphicsTextItem("", parent), m_width(width), m_height(height)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsSelectable, true);
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsFocusable, true);
Toshihiro Shimizu 890ddd
	setTextInteractionFlags(Qt::TextEditorInteraction);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	connect(document(), SIGNAL(contentsChanged()), this, SLOT(onContentsChanged()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicName::~SchematicName()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicName::setName(const QString &name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setPlainText(name);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicName::onContentsChanged()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString text = document()->toPlainText();
Toshihiro Shimizu 890ddd
	QTextCursor cursor = textCursor();
Toshihiro Shimizu 890ddd
	int position = cursor.position();
Toshihiro Shimizu 890ddd
	if (position > 0 && text.at(position - 1) == '\n') {
Toshihiro Shimizu 890ddd
		text.remove("\n");
Toshihiro Shimizu 890ddd
		setPlainText(text);
Toshihiro Shimizu 890ddd
		;
Toshihiro Shimizu 890ddd
		emit focusOut();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicName::focusOutEvent(QFocusEvent *fe)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	qApp->removeEventFilter(this);
Toshihiro Shimizu 890ddd
	if (fe->reason() == Qt::MouseFocusReason)
Toshihiro Shimizu 890ddd
		emit focusOut();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicName::keyPressEvent(QKeyEvent *ke)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (ke->key() == Qt::Key_Left || ke->key() == Qt::Key_Right) {
Toshihiro Shimizu 890ddd
		QTextCursor cursor = textCursor();
Toshihiro Shimizu 890ddd
		int currentPos = cursor.position();
Toshihiro Shimizu 890ddd
		if (ke->key() == Qt::Key_Left)
Toshihiro Shimizu 890ddd
			cursor.setPosition(currentPos - 1);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			cursor.setPosition(currentPos + 1);
Toshihiro Shimizu 890ddd
		setTextCursor(cursor);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		QGraphicsTextItem::keyPressEvent(ke);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool SchematicName::eventFilter(QObject *object, QEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (event->type() == QEvent::Shortcut ||
Toshihiro Shimizu 890ddd
		event->type() == QEvent::ShortcutOverride) {
Toshihiro Shimizu 890ddd
		if (!object->inherits("QGraphicsView")) {
Toshihiro Shimizu 890ddd
			event->accept();
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicName::focusInEvent(QFocusEvent *fe)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QGraphicsTextItem::focusInEvent(fe);
Toshihiro Shimizu 890ddd
	qApp->installEventFilter(this);
Toshihiro Shimizu 890ddd
	QTextDocument *doc = document();
Toshihiro Shimizu 890ddd
	QTextCursor cursor(doc->begin());
Toshihiro Shimizu 890ddd
	cursor.select(QTextCursor::Document);
Toshihiro Shimizu 890ddd
	setTextCursor(cursor);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//class SchematicThumbnailToggle
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicThumbnailToggle::SchematicThumbnailToggle(SchematicNode *parent, bool isOpened)
Toshihiro Shimizu 890ddd
	: QGraphicsItem(parent), m_isDown(!isOpened)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicThumbnailToggle::~SchematicThumbnailToggle()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QRectF SchematicThumbnailToggle::boundingRect() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QRectF(0, 0, 14, 14);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicThumbnailToggle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPixmap onPixmap(":Resources/schematic_thumbtoggle_on.png");
Toshihiro Shimizu 890ddd
	QPixmap offPixmap(":Resources/schematic_thumbtoggle_off.png");
Toshihiro Shimizu 890ddd
	QRect rect(3, 3, 8, 8);
Toshihiro Shimizu 890ddd
	if (m_isDown) {
Toshihiro Shimizu 890ddd
		painter->drawPixmap(rect, offPixmap);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		painter->drawPixmap(rect, onPixmap);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicThumbnailToggle::setIsDown(bool value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_isDown = value;
Toshihiro Shimizu 890ddd
	emit(toggled(!m_isDown));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicThumbnailToggle::mousePressEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_isDown = !m_isDown;
Toshihiro Shimizu 890ddd
	emit(toggled(!m_isDown));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//SchematicToggle
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicToggle::SchematicToggle(SchematicNode *parent, const QPixmap &pixmap, int flags, bool isLargeScaled)
Toshihiro Shimizu 890ddd
	: QGraphicsItem(parent), m_pixmap1(pixmap), m_pixmap2(), m_state(0), m_flags(flags), m_width(isLargeScaled ? 18 : 30), m_height(isLargeScaled ? 7 : 5)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicToggle::SchematicToggle(SchematicNode *parent, const QPixmap &pixmap1, const QPixmap &pixmap2, int flags, bool isLargeScaled)
Toshihiro Shimizu 890ddd
	: QGraphicsItem(parent), m_pixmap1(pixmap1), m_pixmap2(pixmap2), m_state(0), m_flags(flags), m_width(isLargeScaled ? 18 : 30), m_height(isLargeScaled ? 7 : 5)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicToggle::~SchematicToggle()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QRectF SchematicToggle::boundingRect() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QRectF(0, 0, m_width, m_height);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicToggle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_state != 0) {
Toshihiro Shimizu 890ddd
		QPixmap &pix = (m_state == 2 && !m_pixmap2.isNull()) ? m_pixmap2 : m_pixmap1;
Toshihiro Shimizu 890ddd
		painter->drawPixmap(boundingRect().toRect(), pix);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicToggle::mousePressEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (me->button() == Qt::LeftButton) {
Toshihiro Shimizu 890ddd
		if (m_pixmap2.isNull()) {
Toshihiro Shimizu 890ddd
			m_state = 1 - m_state;
Toshihiro Shimizu 890ddd
			emit(toggled(m_state != 0));
Toshihiro Shimizu 890ddd
		} else if (m_flags & eEnableNullState) {
Toshihiro Shimizu 890ddd
			m_state = (m_state + 1) % 3;
Toshihiro Shimizu 890ddd
			emit(stateChanged(m_state));
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			m_state = 3 - m_state;
Toshihiro Shimizu 890ddd
			emit(stateChanged(m_state));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (me->button() == Qt::RightButton) {
Toshihiro Shimizu 890ddd
		SchematicNode *parent = dynamic_cast<schematicnode *="">(this->parentItem());</schematicnode>
Toshihiro Shimizu 890ddd
		if (parent)
Toshihiro Shimizu 890ddd
			parent->onClicked();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicToggle::contextMenuEvent(QGraphicsSceneContextMenuEvent *cme)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!(m_flags & eIsParentColumn))
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (m_pixmap2.isNull()) {
Toshihiro Shimizu 890ddd
		QMenu *menu = new QMenu(0);
Toshihiro Shimizu 890ddd
		CommandManager *cmdManager = CommandManager::instance();
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_EnableThisColumnOnly"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_EnableSelectedColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_EnableAllColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_DisableAllColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_DisableSelectedColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_SwapEnabledColumns"));
Toshihiro Shimizu 890ddd
		QAction *action = menu->exec(cme->screenPos());
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		QMenu *menu = new QMenu(0);
Toshihiro Shimizu 890ddd
		CommandManager *cmdManager = CommandManager::instance();
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_ActivateThisColumnOnly"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_ActivateSelectedColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_ActivateAllColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_DeactivateAllColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_DeactivateSelectedColumns"));
Toshihiro Shimizu 890ddd
		menu->addAction(cmdManager->getAction("MI_ToggleColumnsActivation"));
Toshihiro Shimizu 890ddd
		QAction *action = menu->exec(cme->screenPos());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! for Spline Aim and CP toggles
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SchematicToggle_SplineOptions::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QRectF rect = boundingRect();
Toshihiro Shimizu 890ddd
	painter->fillRect(rect, Qt::white);
Toshihiro Shimizu 890ddd
	if (m_state != 0) {
Toshihiro Shimizu 890ddd
		QPixmap &pix = (m_state == 2 && !m_pixmap2.isNull()) ? m_pixmap2 : m_pixmap1;
Toshihiro Shimizu 890ddd
		painter->drawPixmap(boundingRect().toRect(), pix);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	painter->setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
	painter->setPen(QColor(180, 180, 180, 255));
Toshihiro Shimizu 890ddd
	painter->drawRect(rect);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! for Spline Aim and CP toggles
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SchematicToggle_SplineOptions::mousePressEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	SchematicToggle::mousePressEvent(me);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//SchematicHandleSpinBox
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicHandleSpinBox::SchematicHandleSpinBox(QGraphicsItem *parent)
Toshihiro Shimizu 890ddd
	: QGraphicsItem(parent), m_buttonState(Qt::NoButton), m_delta(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsSelectable, false);
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsFocusable, false);
Toshihiro Shimizu 890ddd
	m_pixmap = QPixmap(":Resources/schematic_spin_arrows.svg");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicHandleSpinBox::~SchematicHandleSpinBox()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QRectF SchematicHandleSpinBox::boundingRect() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QRectF(0, 0, 10, 10);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicHandleSpinBox::paint(QPainter *painter,
Toshihiro Shimizu 890ddd
								   const QStyleOptionGraphicsItem *option, QWidget *widget)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QRectF rect = boundingRect();
Toshihiro Shimizu 890ddd
	painter->drawPixmap(rect.toRect(), m_pixmap);
Toshihiro Shimizu 890ddd
	painter->setBrush(QColor(0, 0, 0, 0));
Toshihiro Shimizu 890ddd
	painter->setPen(QColor(128, 128, 128, 255));
Toshihiro Shimizu 890ddd
	painter->drawRect(rect);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicHandleSpinBox::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_buttonState == Qt::LeftButton) {
Toshihiro Shimizu 890ddd
		bool increase = false;
Toshihiro Shimizu 890ddd
		int delta = me->screenPos().y() - me->lastScreenPos().y();
Toshihiro Shimizu 890ddd
		if (delta < 0)
Toshihiro Shimizu 890ddd
			increase = true;
Toshihiro Shimizu 890ddd
		m_delta += abs(delta);
Toshihiro Shimizu 890ddd
		if (m_delta > 5) {
Toshihiro Shimizu 890ddd
			if (increase)
Toshihiro Shimizu 890ddd
				emit(modifyHandle(1));
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				emit(modifyHandle(-1));
Toshihiro Shimizu 890ddd
			m_delta = 0;
Toshihiro Shimizu 890ddd
			emit sceneChanged();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicHandleSpinBox::mousePressEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_buttonState = me->button();
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->beginBlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicHandleSpinBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_buttonState = Qt::NoButton;
Toshihiro Shimizu 890ddd
	m_delta = 0;
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->endBlock();
Toshihiro Shimizu 890ddd
	emit handleReleased();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//class SchematicLink
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicLink::SchematicLink(QGraphicsItem *parent, QGraphicsScene *scene)
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	: QGraphicsItem(parent)
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	: QGraphicsItem(parent, scene)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	  ,
Toshihiro Shimizu 890ddd
	  m_startPort(0), m_endPort(0), m_path(), m_hitPath(), m_lineShaped(false), m_highlighted(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	scene->addItem(this);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsMovable, false);
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsSelectable, true);
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsFocusable, false);
Toshihiro Shimizu 890ddd
	setZValue(0.0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicLink::~SchematicLink()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_startPort = m_endPort = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QRectF SchematicLink::boundingRect() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_hitPath.boundingRect().adjusted(-5, -5, 5, 5);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QPainterPath SchematicLink::shape() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_hitPath;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicLink::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (getStartPort() &&
Toshihiro Shimizu 890ddd
		(getStartPort()->getType() == 100		 //eStageSplinePort
Toshihiro Shimizu 890ddd
		 || getStartPort()->getType() == 202)) { //eFxLinkPort
Toshihiro Shimizu 890ddd
		if (isSelected() || isHighlighted())
Toshihiro Shimizu 890ddd
			painter->setPen(QColor(255, 255, 10));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			painter->setPen(QColor(50, 255, 50, 128));
Toshihiro Shimizu 890ddd
	} else if (isSelected() || isHighlighted())
Toshihiro Shimizu 890ddd
		painter->setPen(QPen(Qt::cyan));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	else if (!m_lineShaped)
Toshihiro Shimizu 890ddd
		painter->setPen(QPen(Qt::white));
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		painter->setPen(QPen(QColor(170, 170, 10), 0, Qt::DashLine));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter->setRenderHint(QPainter::Antialiasing, true);
Toshihiro Shimizu 890ddd
	painter->drawPath(m_path);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicLink::updatePath(const QPointF &startPos, const QPointF &endPos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	prepareGeometryChange();
Toshihiro Shimizu 890ddd
	setPos(startPos);
Toshihiro Shimizu 890ddd
	if (!m_lineShaped) {
Toshihiro Shimizu 890ddd
		QPointF p0((endPos.x() - startPos.x()) * 0.5, 0);
Toshihiro Shimizu 890ddd
		QPointF p1(p0.x(), endPos.y() - startPos.y());
Toshihiro Shimizu 890ddd
		QPointF p2(endPos - startPos);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_path = QPainterPath(QPointF(0, 0));
Toshihiro Shimizu 890ddd
		m_path.cubicTo(p0, p1, p2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QPointF h(0, 5);
Toshihiro Shimizu 890ddd
		QPointF p = h;
Toshihiro Shimizu 890ddd
		if (p2.y() > 0)
Toshihiro Shimizu 890ddd
			p.setX(p2.x() > 0 ? -5 : 5);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			p.setX(p2.x() > 0 ? 5 : -5);
Toshihiro Shimizu 890ddd
		m_hitPath = QPainterPath(QPointF(0, 0));
Toshihiro Shimizu 890ddd
		m_hitPath.lineTo(h);
Toshihiro Shimizu 890ddd
		m_hitPath.cubicTo(p0 + p, p1 + p, p2 + h);
Toshihiro Shimizu 890ddd
		m_hitPath.lineTo(p2 - h);
Toshihiro Shimizu 890ddd
		m_hitPath.cubicTo(p1 - p, p0 - p, -h);
Toshihiro Shimizu 890ddd
		m_hitPath.lineTo(0, 0);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		m_path = QPainterPath(QPointF(0, 0));
Toshihiro Shimizu 890ddd
		m_path.lineTo(endPos - startPos);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_hitPath = QPainterPath(QPointF(0, 0));
Toshihiro Shimizu 890ddd
		m_hitPath.lineTo(endPos - startPos);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicLink::updatePath(SchematicPort *startPort, SchematicPort *endPort)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	updatePath(startPort->getLinkEndPoint(), endPort->getLinkEndPoint());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicPort *SchematicLink::getOtherPort(const SchematicPort *port) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (port == m_startPort)
Toshihiro Shimizu 890ddd
		return m_endPort;
Toshihiro Shimizu 890ddd
	else if (port == m_endPort)
Toshihiro Shimizu 890ddd
		return m_startPort;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicNode *SchematicLink::getOtherNode(const SchematicNode *node) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (node == m_startPort->getNode())
Toshihiro Shimizu 890ddd
		return m_endPort->getNode();
Toshihiro Shimizu 890ddd
	else if (node == m_endPort->getNode())
Toshihiro Shimizu 890ddd
		return m_startPort->getNode();
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicLink::mousePressEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPointF pos = me->scenePos();
Toshihiro Shimizu 890ddd
	SchematicPort *startPort = getStartPort();
Toshihiro Shimizu 890ddd
	SchematicPort *endPort = getEndPort();
Toshihiro Shimizu 890ddd
	if (startPort && endPort) {
Toshihiro Shimizu 890ddd
		QRectF startRect = startPort->boundingRect();
Toshihiro Shimizu 890ddd
		startRect.moveTopLeft(startPort->scenePos());
Toshihiro Shimizu 890ddd
		QRectF endRect = endPort->boundingRect();
Toshihiro Shimizu 890ddd
		endRect.moveTopLeft(endPort->scenePos());
Toshihiro Shimizu 890ddd
		if (startRect.contains(pos) || endRect.contains(pos)) {
Toshihiro Shimizu 890ddd
			me->ignore();
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QMatrix matrix = scene()->views()[0]->matrix();
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	double scaleFactor = sqrt(matrix.determinant());
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	double scaleFactor = sqrt(matrix.det());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPointF startPos = getStartPort()->getLinkEndPoint();
Toshihiro Shimizu 890ddd
	QPointF endPos = getEndPort()->getLinkEndPoint();
Toshihiro Shimizu 890ddd
	QPointF p0((endPos.x() - startPos.x()) * 0.5, 0);
Toshihiro Shimizu 890ddd
	QPointF p1(p0.x(), endPos.y() - startPos.y());
Toshihiro Shimizu 890ddd
	QPointF p2(endPos - startPos);
Toshihiro Shimizu 890ddd
	double sensibility = 5 / scaleFactor;
Toshihiro Shimizu 890ddd
	QPointF h(0, sensibility);
Toshihiro Shimizu 890ddd
	QPointF p = h;
Toshihiro Shimizu 890ddd
	if (p2.y() > 0)
Toshihiro Shimizu 890ddd
		p.setX(p2.x() > 0 ? -sensibility : sensibility);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		p.setX(p2.x() > 0 ? sensibility : -sensibility);
Toshihiro Shimizu 890ddd
	QPainterPath path(QPointF(0, 0));
Toshihiro Shimizu 890ddd
	path.lineTo(h);
Toshihiro Shimizu 890ddd
	path.cubicTo(p0 + p, p1 + p, p2 + h);
Toshihiro Shimizu 890ddd
	path.lineTo(p2 - h);
Toshihiro Shimizu 890ddd
	path.cubicTo(p1 - p, p0 - p, -h);
Toshihiro Shimizu 890ddd
	path.lineTo(0, 0);
Toshihiro Shimizu 890ddd
	if (!path.contains(me->scenePos() - scenePos())) {
Toshihiro Shimizu 890ddd
		me->ignore();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!isSelected()) {
Toshihiro Shimizu 890ddd
		if (me->modifiers() != Qt::ControlModifier)
Toshihiro Shimizu 890ddd
			scene()->clearSelection();
Toshihiro Shimizu 890ddd
		if (me->button() == Qt::LeftButton || me->button() == Qt::RightButton)
Toshihiro Shimizu 890ddd
			setSelected(true);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (me->modifiers() == Qt::ControlModifier && me->button() == Qt::LeftButton)
Toshihiro Shimizu 890ddd
			setSelected(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicLink::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (me->modifiers() != Qt::ControlModifier && me->button() != Qt::RightButton)
Toshihiro Shimizu 890ddd
		QGraphicsItem::mouseReleaseEvent(me);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//class SchematicPort
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicPort::SchematicPort(QGraphicsItem *parent, SchematicNode *node, int type)
Toshihiro Shimizu 890ddd
	: QGraphicsItem(parent), m_node(node), m_buttonState(Qt::NoButton), m_highlighted(false), m_ghostLink(0), m_linkingTo(0), m_hook(0, 0), m_type(type)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	setAcceptHoverEvents(false);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	setAcceptsHoverEvents(false);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsSelectable, false);
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsFocusable, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicPort::~SchematicPort()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_links.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicPort::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_buttonState != Qt::LeftButton)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (!m_ghostLink)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_linkingTo) {
Toshihiro Shimizu 890ddd
		showSnappedLinks();
Toshihiro Shimizu 890ddd
		m_linkingTo = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//Snapping
Toshihiro Shimizu 890ddd
	SchematicPort *linkingTo = searchPort(me->scenePos());
Toshihiro Shimizu 890ddd
	if (!linkingTo) {
Toshihiro Shimizu 890ddd
		if (m_linkingTo) {
Toshihiro Shimizu 890ddd
			m_linkingTo->highLight(false);
Toshihiro Shimizu 890ddd
			m_linkingTo->update();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		m_ghostLink->updatePath(this->getLinkEndPoint(), me->scenePos());
Toshihiro Shimizu 890ddd
		m_linkingTo = linkingTo;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	// if to be connected something
Toshihiro Shimizu 890ddd
	else if (linkingTo != this) {
Toshihiro Shimizu 890ddd
		m_ghostLink->updatePath(this, linkingTo);
Toshihiro Shimizu 890ddd
		m_linkingTo = linkingTo;
Toshihiro Shimizu 890ddd
		hideSnappedLinks();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//autopan
Toshihiro Shimizu 890ddd
	QGraphicsView *viewer = scene()->views()[0];
Toshihiro Shimizu 890ddd
	viewer->setInteractive(false);
Toshihiro Shimizu 890ddd
	viewer->ensureVisible(QRectF(me->scenePos(), QSizeF(1, 1)), 5, 5);
Toshihiro Shimizu 890ddd
	viewer->setInteractive(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicPort::mousePressEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!isSelected()) {
Toshihiro Shimizu 890ddd
		if (me->modifiers() != Qt::ControlModifier)
Toshihiro Shimizu 890ddd
			scene()->clearSelection();
Toshihiro Shimizu 890ddd
		if (me->button() == Qt::LeftButton || me->button() == Qt::RightButton)
Toshihiro Shimizu 890ddd
			getNode()->setSelected(true);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (me->modifiers() == Qt::ControlModifier && me->button() == Qt::LeftButton)
Toshihiro Shimizu 890ddd
			getNode()->setSelected(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	getNode()->onClicked();
Toshihiro Shimizu 890ddd
	if (me->button() == Qt::LeftButton && getType() != 202 // eFxLinkPort
Toshihiro Shimizu 890ddd
		&& getType() != 203								   // eFxGroupedInPort
Toshihiro Shimizu 890ddd
		&& getType() != 204								   // eFxGroupedOutPort
Toshihiro Shimizu 890ddd
		&& getType() != 103								   // eStageSplineGroupPort
Toshihiro Shimizu 890ddd
		&& getType() != 104								   // eStageParentGroupPort
Toshihiro Shimizu 890ddd
		&& getType() != 105)							   // eStageChildGroupPort
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_buttonState = Qt::LeftButton;
Toshihiro Shimizu 890ddd
		QPointF endPos(me->pos());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_ghostLink = new SchematicLink(0, scene());
Toshihiro Shimizu 890ddd
		m_ghostLink->setZValue(3.0);
Toshihiro Shimizu 890ddd
		m_ghostLink->updatePath(this->getLinkEndPoint(), me->scenePos());
Toshihiro Shimizu 890ddd
		emit(isClicked());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicPort::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (me->modifiers() != Qt::ControlModifier && me->button() != Qt::RightButton)
Toshihiro Shimizu 890ddd
		QGraphicsItem::mouseReleaseEvent(me);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_ghostLink)
Toshihiro Shimizu 890ddd
		m_ghostLink->hide();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_buttonState == Qt::LeftButton)
Toshihiro Shimizu 890ddd
		emit(isReleased(me->scenePos()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//The link is added to the scene only if the user released the left mouse button over
Toshihiro Shimizu 890ddd
	//a SchematicPort different from SchematicPort of the parent node.
Toshihiro Shimizu 890ddd
	if (m_buttonState == Qt::LeftButton &&
Toshihiro Shimizu 890ddd
		m_linkingTo && !isLinkedTo(m_linkingTo) &&
Toshihiro Shimizu 890ddd
		linkTo(m_linkingTo, true)) {
Toshihiro Shimizu 890ddd
		linkTo(m_linkingTo);
Toshihiro Shimizu 890ddd
		m_buttonState = Qt::NoButton;
Toshihiro Shimizu 890ddd
		m_linkingTo = 0;
Toshihiro Shimizu 890ddd
		emit sceneChanged();
Toshihiro Shimizu 890ddd
		emit xsheetChanged();
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		showSnappedLinks();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicPort::removeLink(SchematicLink *link)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_links.removeAll(link);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicPort::eraseLink(SchematicLink *link)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	SchematicPort *otherPort = link->getOtherPort(this);
Toshihiro Shimizu 890ddd
	if (otherPort)
Toshihiro Shimizu 890ddd
		otherPort->removeLink(link);
Toshihiro Shimizu 890ddd
	removeLink(link);
Toshihiro Shimizu 890ddd
	if (link->scene())
Toshihiro Shimizu 890ddd
		link->scene()->removeItem(link);
Toshihiro Shimizu 890ddd
	delete link;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicPort::eraseAllLinks()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	while (!m_links.empty())
Toshihiro Shimizu 890ddd
		eraseLink(m_links[0]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicLink *SchematicPort::makeLink(SchematicPort *port)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (isLinkedTo(port) || !port)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	SchematicLink *link = new SchematicLink(0, scene());
Toshihiro Shimizu 890ddd
	if (getType() == 202 && port->getType() == 202)
Toshihiro Shimizu 890ddd
		link->setLineShaped(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	link->setStartPort(this);
Toshihiro Shimizu 890ddd
	link->setEndPort(port);
Toshihiro Shimizu 890ddd
	addLink(link);
Toshihiro Shimizu 890ddd
	port->addLink(link);
Toshihiro Shimizu 890ddd
	link->updatePath();
Toshihiro Shimizu 890ddd
	return link;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool SchematicPort::isLinkedTo(SchematicPort *port) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_links.size() == 0)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_links.size(); i++) {
Toshihiro Shimizu 890ddd
		SchematicLink *link = m_links[i];
Toshihiro Shimizu 890ddd
		if (((link->getStartPort() == this && link->getEndPort() == port) ||
Toshihiro Shimizu 890ddd
			 (link->getEndPort() == this && link->getStartPort() == port)) &&
Toshihiro Shimizu 890ddd
			link->isVisible())
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicPort::updateLinksGeometry()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int linkCount = getLinkCount();
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < linkCount; i++) {
Toshihiro Shimizu 890ddd
		SchematicLink *link = getLink(i);
Toshihiro Shimizu 890ddd
		SchematicPort *startPort = link->getStartPort();
Toshihiro Shimizu 890ddd
		SchematicPort *endPort = link->getEndPort();
Toshihiro Shimizu 890ddd
		if (startPort && endPort) {
Toshihiro Shimizu 890ddd
			link->updatePath(startPort, endPort);
Toshihiro Shimizu 890ddd
			link->setPos(startPort->getLinkEndPoint());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QPointF SchematicPort::getLinkEndPoint() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return scenePos() + m_hook;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//class SchematicNode
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! \class SchematicNode schematicnode.h "../inlcude/toonzqt/schematicnode.h"
Toshihiro Shimizu 890ddd
	\brief The class provides methods to draw and handle a node item in the SchematicScene.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! \fn SchematicPort *SchematicNode::getInputPort() const
Toshihiro Shimizu 890ddd
	Returns the input SchematicPort of the node.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! \fn SchematicPort *SchematicNode::getOutputPort() const
Toshihiro Shimizu 890ddd
	Returns the output SchematicPort of the node.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! \fn SchematicScene* SchematicNode::getScene() const
Toshihiro Shimizu 890ddd
	Returns the scene where the node is placed.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicNode::SchematicNode(SchematicScene *scene)
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	: QGraphicsItem(0)
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	: QGraphicsItem(0, scene)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	  ,
Toshihiro Shimizu 890ddd
	  m_scene(scene), m_width(0), m_height(0), m_buttonState(Qt::NoButton)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	scene->addItem(this);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsMovable, false);
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsSelectable, true);
Toshihiro Shimizu 890ddd
	setFlag(QGraphicsItem::ItemIsFocusable, false);
Toshihiro Shimizu 890ddd
	setZValue(1.0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SchematicNode::~SchematicNode()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!Reimplements the pure virtual QGraphicsItem::boundingRect() method.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
QRectF SchematicNode::boundingRect() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QRectF(0, 0, 1, 1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! Reimplements the pure virtual QGraphicsItem::paint() method.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SchematicNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPen pen;
Toshihiro Shimizu 890ddd
	if (isSelected()) {
Toshihiro Shimizu 890ddd
		painter->setBrush(QColor(0, 0, 0, 0));
Toshihiro Shimizu 890ddd
		pen.setColor(QColor(255, 255, 255, 255));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pen.setWidth(4.0);
Toshihiro Shimizu 890ddd
		pen.setJoinStyle(Qt::RoundJoin);
Toshihiro Shimizu 890ddd
		painter->setPen(pen);
Toshihiro Shimizu 890ddd
		painter->drawRect(-2, -2, m_width + 4, m_height + 4);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	pen.setColor(QColor(0, 0, 0, 255));
Toshihiro Shimizu 890ddd
	pen.setWidth(0);
Toshihiro Shimizu 890ddd
	painter->setPen(pen);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! Reimplements the QGraphicsItem::mouseMoveEvent() method.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SchematicNode::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QList<qgraphicsitem *=""> items = scene()->selectedItems();</qgraphicsitem>
Toshihiro Shimizu 890ddd
	if (items.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	QList<qgraphicsitem *="">::iterator it = items.begin();</qgraphicsitem>
Toshihiro Shimizu 890ddd
	QPointF delta = me->scenePos() - me->lastScenePos();
Toshihiro Shimizu 890ddd
	QGraphicsView *viewer = scene()->views()[0];
Toshihiro Shimizu 890ddd
	it = items.begin();
Toshihiro Shimizu 890ddd
	for (it; it != items.end(); it++) {
Toshihiro Shimizu 890ddd
		SchematicNode *node = dynamic_cast<schematicnode *="">(*it);</schematicnode>
Toshihiro Shimizu 890ddd
		if (node) {
Toshihiro Shimizu 890ddd
			node->setPosition(node->scenePos() + delta);
Toshihiro Shimizu 890ddd
			node->setSchematicNodePos(node->scenePos());
Toshihiro Shimizu 890ddd
			node->updateLinksGeometry();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	viewer->setInteractive(false);
Toshihiro Shimizu 890ddd
	viewer->ensureVisible(QRectF(me->scenePos(), QSizeF(1, 1)), 5, 5);
Toshihiro Shimizu 890ddd
	viewer->setInteractive(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicNode::mousePressEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!isSelected()) {
Toshihiro Shimizu 890ddd
		if (me->modifiers() != Qt::ControlModifier)
Toshihiro Shimizu 890ddd
			scene()->clearSelection();
Toshihiro Shimizu 890ddd
		if (me->button() == Qt::LeftButton || me->button() == Qt::RightButton)
Toshihiro Shimizu 890ddd
			setSelected(true);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (me->modifiers() == Qt::ControlModifier && me->button() == Qt::LeftButton)
Toshihiro Shimizu 890ddd
			setSelected(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	onClicked();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicNode::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (me->modifiers() != Qt::ControlModifier && me->button() != Qt::RightButton)
Toshihiro Shimizu 890ddd
		QGraphicsItem::mouseReleaseEvent(me);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
/* Add a pair (portId, SchematicPort*port) in the mapping
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
SchematicPort *SchematicNode::addPort(int portId, SchematicPort *port)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMap<int, *="" schematicport="">::iterator it;</int,>
Toshihiro Shimizu 890ddd
	it = m_ports.find(portId);
Toshihiro Shimizu 890ddd
	if (it != m_ports.end() && m_ports[portId] != port) {
Toshihiro Shimizu 890ddd
		SchematicPort *oldPort = m_ports[portId];
Toshihiro Shimizu 890ddd
		m_ports.erase(it);
Toshihiro Shimizu 890ddd
		scene()->removeItem(oldPort);
Toshihiro Shimizu 890ddd
		delete oldPort;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	m_ports[portId] = port;
Toshihiro Shimizu 890ddd
	return port;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!Erase the pair (portId, SchematicPort*port) from the mapping*/
Toshihiro Shimizu 890ddd
void SchematicNode::erasePort(int portId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMap<int, *="" schematicport="">::iterator it = m_ports.find(portId);</int,>
Toshihiro Shimizu 890ddd
	if (it != m_ports.end()) {
Toshihiro Shimizu 890ddd
		delete it.value();
Toshihiro Shimizu 890ddd
		m_ports.erase(it);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! Returns a pointer to the SchematicPort mapped from \b portId.\n
Toshihiro Shimizu 890ddd
    Returns 0 if \b portId doesn't map no SchematicPort.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
SchematicPort *SchematicNode::getPort(int portId) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMap<int, *="" schematicport="">::const_iterator it = m_ports.find(portId);</int,>
Toshihiro Shimizu 890ddd
	if (it != m_ports.end())
Toshihiro Shimizu 890ddd
		return it.value();
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! Returns a list of all node connected by links to a SchematicPort identified by \b portId.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
QList<schematicnode *=""> SchematicNode::getLinkedNodes(int portId) const</schematicnode>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QList<schematicnode *=""> list;</schematicnode>
Toshihiro Shimizu 890ddd
	SchematicPort *port = getPort(portId);
Toshihiro Shimizu 890ddd
	if (port) {
Toshihiro Shimizu 890ddd
		int linkCount = port->getLinkCount();
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		for (i = 0; i < linkCount; i++)
Toshihiro Shimizu 890ddd
			list.push_back(port->getLinkedNode(i));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return list;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SchematicNode::updateLinksGeometry()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMap<int, *="" schematicport="">::iterator it;</int,>
Toshihiro Shimizu 890ddd
	for (it = m_ports.begin(); it != m_ports.end(); ++it)
Toshihiro Shimizu 890ddd
		it.value()->updateLinksGeometry();
Toshihiro Shimizu 890ddd
}