cleaned the network graph a little bit. Removed text, updated bounding boxes, removed background

This commit is contained in:
csoler 2016-01-22 21:32:35 -05:00
parent b3625448e9
commit c428739abd
4 changed files with 75 additions and 57 deletions

View File

@ -43,7 +43,7 @@ NetworkView::NetworkView(QWidget *parent)
mScene = new QGraphicsScene();
mScene->setItemIndexMethod(QGraphicsScene::NoIndex);
mScene->setSceneRect(-200, -200, 1200, 1200);
mScene->setSceneRect(0, 0, ui.graphicsView->width(), ui.graphicsView->height());
ui.graphicsView->setScene(mScene);
ui.graphicsView->setEdgeLength(ui.edgeLengthSB->value()) ;

View File

@ -127,7 +127,8 @@ GraphWidget::GraphWidget(QWidget *)
// scene->setItemIndexMethod(QGraphicsScene::NoIndex);
// scene->clear() ;
// setScene(scene);
// scene->setSceneRect(0, 0, 500, 500);
// scene()->setSceneRect(0, 0, width(), height());
setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate);
@ -154,7 +155,8 @@ void GraphWidget::clearGraph()
// }
scene()->clear();
scene()->setSceneRect(-200, -200, 1000, 1000);
scene()->setSceneRect(0, 0, width(), height());
_edges.clear();
_nodes.clear();
_friction_factor = 1.0f ;
@ -391,9 +393,11 @@ void GraphWidget::setEdgeLength(uint32_t l)
void GraphWidget::setNameSearch(QString s)
{
float f = QFontMetrics(font()).height()/16.0 ;
if (s.length() == 0){
for(uint32_t i=0;i<_nodes.size();++i)
_nodes[i]->setNodeDrawSize(20);
_nodes[i]->setNodeDrawSize(20 * f);
forceRedraw();
return;
}
@ -405,10 +409,10 @@ void GraphWidget::setNameSearch(QString s)
if (ns.find(qs) != std::string::npos) {
//std::cout << "found!" << '\n';
ni->setNodeDrawSize(22);
ni->setNodeDrawSize(22 * f);
//std::cout << ni->getNodeDrawSize() << '\n';
} else {
ni->setNodeDrawSize(12);
ni->setNodeDrawSize(12 * f);
}
}
@ -420,47 +424,53 @@ void GraphWidget::forceRedraw()
for(uint32_t i=0;i<_nodes.size();++i)
_nodes[i]->update(_nodes[i]->boundingRect()) ;
}
void GraphWidget::resizeEvent(QResizeEvent *event)
{
scene()->setSceneRect(QRectF(QPointF(0,0),event->size()));
}
void GraphWidget::wheelEvent(QWheelEvent *event)
{
scaleView(pow((double)2, -event->delta() / 240.0));
}
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
// Shadow
QRectF sceneRect = this->sceneRect();
QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
if (rightShadow.intersects(rect) || rightShadow.contains(rect))
painter->fillRect(rightShadow, Qt::darkGray);
if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
painter->fillRect(bottomShadow, Qt::darkGray);
// Fill
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, Qt::lightGray);
painter->fillRect(rect.intersected(sceneRect), gradient);
painter->setBrush(Qt::NoBrush);
painter->drawRect(sceneRect);
// Text
QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
sceneRect.width() - 4, sceneRect.height() - 4);
QString message(tr("Click and drag the nodes around, and zoom with the mouse "
"wheel or the '+' and '-' keys"));
QFont font = painter->font();
font.setBold(true);
font.setPointSize(14);
painter->setFont(font);
painter->setPen(Qt::lightGray);
painter->drawText(textRect.translated(2, 2), message);
painter->setPen(Qt::black);
painter->drawText(textRect, message);
}
//void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
//{
// Q_UNUSED(rect);
//
// // Shadow
// QRectF sceneRect = this->sceneRect();
// QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
// QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
// if (rightShadow.intersects(rect) || rightShadow.contains(rect))
// painter->fillRect(rightShadow, Qt::darkGray);
// if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
// painter->fillRect(bottomShadow, Qt::darkGray);
//
// // Fill
// QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
// gradient.setColorAt(0, Qt::white);
// gradient.setColorAt(1, Qt::lightGray);
// painter->fillRect(rect.intersected(sceneRect), gradient);
// painter->setBrush(Qt::NoBrush);
// painter->drawRect(sceneRect);
//
// // Text
// QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
// sceneRect.width() - 4, sceneRect.height() - 4);
// QString message(tr("Click and drag the nodes around, and zoom with the mouse "
// "wheel or the '+' and '-' keys"));
//
// QFont font = painter->font();
// font.setBold(true);
// font.setPointSize(14);
// painter->setFont(font);
// painter->setPen(Qt::lightGray);
// painter->drawText(textRect.translated(2, 2), message);
// painter->setPen(Qt::black);
// painter->drawText(textRect, message);
//}
void GraphWidget::scaleView(qreal scaleFactor)
{

View File

@ -91,10 +91,11 @@ public:
void forceRedraw() ;
protected:
void keyPressEvent(QKeyEvent *event);
void timerEvent(QTimerEvent *event);
void wheelEvent(QWheelEvent *event);
void drawBackground(QPainter *painter, const QRectF &rect);
virtual void keyPressEvent(QKeyEvent *event);
virtual void timerEvent(QTimerEvent *event);
virtual void wheelEvent(QWheelEvent *event);
virtual void resizeEvent(QResizeEvent *event);
//void drawBackground(QPainter *painter, const QRectF &rect);
void scaleView(qreal scaleFactor);

View File

@ -205,8 +205,8 @@ void Node::calculateForces(const double *map,int width,int height,int W,int H,fl
QRectF sceneRect = scene()->sceneRect();
newPos = pos() + QPointF(_speedx, _speedy) / friction_factor;
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left()), sceneRect.right()));
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top()) , sceneRect.bottom()));
}
bool Node::advance()
@ -222,20 +222,22 @@ bool Node::advance()
QRectF Node::boundingRect() const
{
qreal adjust = 2;
float m = QFontMetricsF(graph->font()).height();
float f = m/16.0;
qreal adjust = 2*f;
/* add in the size of the text */
qreal realwidth = 40;
qreal realwidth = 40*f;
if (mDeterminedBB)
{
realwidth = mBBWidth + adjust;
}
if (realwidth < 23 + adjust)
if (realwidth < 23*f + adjust)
{
realwidth = 23 + adjust;
realwidth = 23*f + adjust;
}
return QRectF(-10 - adjust, -10 - adjust,
realwidth, 23 + adjust);
return QRectF(-10*f - adjust, -10*f - adjust, realwidth, 23*f + adjust);
}
QPainterPath Node::shape() const
@ -314,12 +316,17 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
painter->setBrush(gradient);
painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-mNodeDrawSize2, -mNodeDrawSize2, mNodeDrawSize, mNodeDrawSize);
painter->drawText(-10, 0, QString::fromUtf8(_desc_string.c_str()));
QString txt = QString::fromUtf8(_desc_string.c_str());
float m = QFontMetricsF(graph->font()).height();
float f = m/16.0;
painter->drawText(-10, 5*f, txt) ;
if (!mDeterminedBB)
{
QRect textBox = painter->boundingRect(-10, 0, 400, 20, 0, QString::fromUtf8(_desc_string.c_str()));
mBBWidth = textBox.width();
QRect textBox = painter->boundingRect(-10, 5*f, QFontMetricsF(graph->font()).width(txt), 1.5*m, Qt::AlignVCenter, QString::fromUtf8(_desc_string.c_str()));
mBBWidth = textBox.width()+40*f;
mDeterminedBB = true;
}
}