|
@@ -15,6 +15,8 @@ CMindView::CMindView(QWidget *parent) : QGraphicsView(new QGraphicsScene(), pare
|
|
|
{
|
|
|
setRenderHints(QPainter::Antialiasing); // 抗锯齿
|
|
|
|
|
|
+ // setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
|
|
+
|
|
|
m_mind = new CMind(this);
|
|
|
m_group = new QGraphicsItemGroup();
|
|
|
m_group->setFlags(QGraphicsItem::ItemIsMovable);
|
|
@@ -66,6 +68,7 @@ void CMindView::addNode(CNodeData n)
|
|
|
connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::slotAddSubNode);
|
|
|
connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::slotRemoveNode);
|
|
|
connect(item, &CNodeItem::sigTextChanged, this, &CMindView::slotTextChanged);
|
|
|
+ connect(item, &CNodeItem::sigWillBeginEditing, this, &CMindView::slotWillBeginEditing);
|
|
|
|
|
|
if (m_root == nullptr) {
|
|
|
m_root = item;
|
|
@@ -95,14 +98,15 @@ void CMindView::refreshItems()
|
|
|
}
|
|
|
m_items.clear();
|
|
|
|
|
|
+ qDebug() << __FUNCTION__ << __LINE__ << m_group->boundingRect() << m_group->pos() << endl;
|
|
|
+
|
|
|
refreshNodeGeometry(m_root);
|
|
|
collectItems(m_root);
|
|
|
for (QGraphicsItem *item : m_items) {
|
|
|
m_group->addToGroup(item);
|
|
|
}
|
|
|
|
|
|
- QRectF r = m_group->childrenBoundingRect();
|
|
|
- setSceneRect(QRectF(QPointF(), r.size()));
|
|
|
+ moveToCenter();
|
|
|
}
|
|
|
|
|
|
void CMindView::collectItems(CNodeItem *node)
|
|
@@ -192,13 +196,41 @@ void CMindView::mousePressEvent(QMouseEvent *event)
|
|
|
{
|
|
|
QGraphicsItem *item = itemAt(event->pos());
|
|
|
CTextItem *text = dynamic_cast<CTextItem *>(item);
|
|
|
- if (text == nullptr && m_root != nullptr && m_root->isEditing()) {
|
|
|
+ if (text == nullptr && m_root != nullptr && m_root->editingNode() != nullptr) {
|
|
|
m_root->endEditing();
|
|
|
- refreshItems();
|
|
|
}
|
|
|
+
|
|
|
+ if (isCloseToEdge()) {
|
|
|
+ moveToCenter();
|
|
|
+ }
|
|
|
+
|
|
|
QGraphicsView::mousePressEvent(event);
|
|
|
}
|
|
|
|
|
|
+void CMindView::mouseMoveEvent(QMouseEvent *event)
|
|
|
+{
|
|
|
+ QGraphicsView::mouseMoveEvent(event);
|
|
|
+}
|
|
|
+
|
|
|
+bool CMindView::isCloseToEdge()
|
|
|
+{
|
|
|
+ qreal left = m_group->pos().x();
|
|
|
+ qreal right = m_group->pos().x() + m_group->boundingRect().width();
|
|
|
+ qreal top = m_group->pos().y();
|
|
|
+ qreal bottom = m_group->pos().y() + m_group->boundingRect().height();
|
|
|
+ qreal allowedArea = 0.8;
|
|
|
+
|
|
|
+ return left > width() * allowedArea || right < width() * (1 - allowedArea) || top > height() * allowedArea
|
|
|
+ || bottom < height() * (1 - allowedArea);
|
|
|
+}
|
|
|
+
|
|
|
+void CMindView::moveToCenter()
|
|
|
+{
|
|
|
+ m_group->setPos(QPointF());
|
|
|
+ QRectF r = m_group->childrenBoundingRect();
|
|
|
+ setSceneRect(QRectF(QPointF(), r.size()));
|
|
|
+}
|
|
|
+
|
|
|
void CMindView::testData()
|
|
|
{
|
|
|
int num[19] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
|
|
@@ -234,6 +266,12 @@ void CMindView::slotRemoveNode(int number)
|
|
|
void CMindView::slotTextChanged()
|
|
|
{
|
|
|
CNodeItem *item = (CNodeItem *)sender();
|
|
|
- qDebug() << __FUNCTION__ << __LINE__ << item->data().name << endl;
|
|
|
refreshItems();
|
|
|
+ qDebug() << __FUNCTION__ << __LINE__ << item->data().name << endl;
|
|
|
+}
|
|
|
+
|
|
|
+void CMindView::slotWillBeginEditing()
|
|
|
+{
|
|
|
+ m_root->endEditing();
|
|
|
+ // refreshItems();
|
|
|
}
|