101 lines
2.7 KiB
C++
101 lines
2.7 KiB
C++
#include <QtGui>
|
|
#include "pictextitem.h"
|
|
#include "picscene.h"
|
|
#include "moveinput.h"
|
|
|
|
extern MoveInput* moveInput;
|
|
PicTextItem::PicTextItem(QGraphicsItem *parent, QGraphicsScene *scene) :
|
|
QGraphicsTextItem(parent)//edit@02-07
|
|
{
|
|
setFlag(QGraphicsItem::ItemIsMovable);
|
|
setFlag(QGraphicsItem::ItemIsSelectable);
|
|
setFlag(QGraphicsItem::ItemIsFocusable);
|
|
QFont font;
|
|
font.setFamily("wenquanyi");
|
|
font.setPixelSize(12);
|
|
document()->setDefaultStyleSheet("p { margin: 0; }");
|
|
document()->setDocumentMargin(0);
|
|
SpecialConditions = false;
|
|
CatchCnt1CurrValue = -1;
|
|
CatchCnt1StartValue = -1;
|
|
|
|
// 插入的变量贴底部,防止错位,修复打开文件时变量图片的垂直对齐
|
|
document()->setDefaultStyleSheet("img { vertical-align: bottom; }");
|
|
}
|
|
//! [1]
|
|
QVariant PicTextItem::itemChange(GraphicsItemChange change,
|
|
const QVariant &value)
|
|
{
|
|
if (change == ItemSceneChange && scene()) {
|
|
QPointF newPos = value.toPoint();
|
|
QRectF rect = scene()->sceneRect();
|
|
if (!rect.contains(newPos)) {
|
|
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
|
|
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
|
|
}
|
|
}
|
|
return QGraphicsItem::itemChange(change, value);
|
|
}
|
|
//! [1]
|
|
|
|
//! [2]
|
|
void PicTextItem::focusOutEvent(QFocusEvent *event)
|
|
{
|
|
moveInput->hide();
|
|
emit lostFocus(this);
|
|
QGraphicsTextItem::focusOutEvent(event);
|
|
//
|
|
}
|
|
//! [2]
|
|
|
|
//! [5]
|
|
void PicTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
//if (textInteractionFlags() == Qt::NoTextInteraction)
|
|
// setTextInteractionFlags(Qt::TextEditorInteraction);
|
|
//setTextInteractionFlags(Qt::TextBrowserInteraction);
|
|
QGraphicsTextItem::mouseDoubleClickEvent(event);
|
|
//emit itemEdit(toHtml());
|
|
}
|
|
//! [5]
|
|
|
|
//限制图元拖动范围
|
|
void PicTextItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
|
|
{
|
|
//edit@12-07
|
|
QPointF newPos = this->pos();
|
|
this->setPos(qCeil(newPos.x()),qCeil(newPos.y()));
|
|
|
|
if(scene())
|
|
scene()->update();
|
|
|
|
QGraphicsTextItem::mouseReleaseEvent(event);
|
|
}
|
|
|
|
void PicTextItem::keyPressEvent(QKeyEvent *event)
|
|
{
|
|
foreach (QGraphicsItem *item, scene()->selectedItems()) {
|
|
if(event->key() == Qt::Key_Left)
|
|
{
|
|
item->moveBy(-1, 0);
|
|
}
|
|
else if(event->key() == Qt::Key_Right)
|
|
{
|
|
item->moveBy(1, 0);
|
|
}
|
|
else if(event->key() == Qt::Key_Up)
|
|
{
|
|
item->moveBy(0, -1);
|
|
}
|
|
else if(event->key() == Qt::Key_Down)
|
|
{
|
|
item->moveBy(0, 1);
|
|
}
|
|
}
|
|
if(!scene()->selectedItems().empty())
|
|
{
|
|
scene()->update();
|
|
}
|
|
QGraphicsTextItem::keyPressEvent(event);
|
|
}
|