#if 1 #include #include "pictextitem.h" #include "picscene.h" #include "moveinput.h" extern MoveInput* moveInput; PicTextItem::PicTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsTextItem(parent,scene) { setFlag(QGraphicsItem::ItemIsMovable); setFlag(QGraphicsItem::ItemIsSelectable); //setFlag(QGraphicsItem::ItemIsFocusScope); setFlag(QGraphicsItem::ItemIsFocusable); //setTextInteractionFlags(Qt::NoTextInteraction); QFont font; font.setFamily("wenquanyi"); font.setPixelSize(12); //document()->setDefaultFont(font);//如果加上这句后,默认的都是wenquanyi 12点阵,看起来这个字体有问题,会黑屏,所以用界面的字体 WenQuanYiMH 代替 document()->setDefaultStyleSheet("p { margin: 0; }"); document()->setDocumentMargin(0); SpecialConditions = false; CatchCnt1CurrValue = -1; CatchCnt1StartValue = -1; } //! [1] QVariant PicTextItem::itemChange(GraphicsItemChange change, const QVariant &value) { #if 0 if (change == QGraphicsItem::ItemSelectedHasChanged) emit selectedChange(this); return value; #else //qDebug()<<"item posi: "<< pos(); if (change == ItemSceneChange && scene()) { // value is the new position. QPointF newPos = value.toPoint(); QRectF rect = scene()->sceneRect(); if (!rect.contains(newPos)) { // Keep the item inside the scene rect. newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); //return newPos; } } #if 0 if (change == ItemSelectedChange && scene()) { if(moveInput->isHidden() && value == 1){ QRect rec = QApplication::desktop()->screenGeometry(); //QPoint point = widget->mapToGlobal (QPoint(0,0)); //全局坐标 QRect myRec = moveInput->geometry(); moveInput->setGeometry(rec.width()/2,rec.height()/2 ,myRec.width(),myRec.height()); moveInput->show(); } if(value == 0) { moveInput->hide(); } } #endif return QGraphicsItem::itemChange(change, value); #endif } //! [1] //! [2] void PicTextItem::focusOutEvent(QFocusEvent *event) { // setTextInteractionFlags(Qt::NoTextInteraction); 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(); //qDebug()<<"mouseReleaseEvent-tem->pos="<setPos(qCeil(newPos.x()),qCeil(newPos.y())); scene()->update(); // QRectF rect = scene()->sceneRect(); // if (!rect.contains(newPos)) { // // Keep the item inside the scene rect. // newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); // newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); // this->setPos(newPos); // } 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(); } //QPointF newPos = this->pos(); //const QRectF rect = scene()->sceneRect(); // if (!rect.contains(newPos)) { // // Keep the item inside the scene rect. // newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); // newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); // //this->setPos(newPos); // //edit@12-09 // this->setPos(view->mapToScene(newPos.x(),newPos.y())); // } QGraphicsTextItem::keyPressEvent(event); } #endif