From 3cade9b1c1d5f6c682fedb5a2bbe77ea339ec0fa Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 17 Jun 2011 21:21:32 +0200 Subject: [PATCH] colorful background, export bug fix: no more transparent bg --- graphwidget.cpp | 54 ++++++++++++++++++++++++++----------------------- graphwidget.h | 2 ++ mainwindow.cpp | 2 ++ node.cpp | 18 ++++++++++++----- node.h | 2 ++ 5 files changed, 48 insertions(+), 30 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index 27598b9..bf24a40 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -11,6 +11,9 @@ #include "math.h" #include "mainwindow.h" +//const QColor GraphWidget::m_paper(255,255,105); +const QColor GraphWidget::m_paper(255,255,153); + GraphWidget::GraphWidget(MainWindow *parent) : QGraphicsView(parent), @@ -27,14 +30,12 @@ GraphWidget::GraphWidget(MainWindow *parent) : m_scene->setItemIndexMethod(QGraphicsScene::NoIndex); m_scene->setSceneRect(-400, -400, 800, 800); setScene(m_scene); + setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::Antialiasing); setTransformationAnchor(AnchorUnderMouse); setMinimumSize(400, 400); - -// connect(m_scene, SIGNAL(changed(const QList &)), -// m_parent, SLOT(contentChanged())); } void GraphWidget::newScene() @@ -93,14 +94,15 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName) QDomElement e = edges.item(i).toElement(); if(!e.isNull()) { - m_scene->addItem(new Edge(m_nodeList[e.attribute("source").toInt()], - - m_nodeList[e.attribute("destination").toInt()])); + m_scene->addItem(new Edge( + m_nodeList[e.attribute("source").toInt()], + m_nodeList[e.attribute("destination").toInt()])); } } m_activeNode = m_nodeList.first(); m_activeNode->setActive(); + m_activeNode->setFocus(); this->show(); } @@ -131,8 +133,10 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName) foreach(Edge *edge, edges()) { QDomElement cn = doc.createElement("edge"); - cn.setAttribute( "source", QString::number(m_nodeList.indexOf(edge->sourceNode()))); - cn.setAttribute( "destination", QString::number(m_nodeList.indexOf(edge->destNode()))); + cn.setAttribute( "source", + QString::number(m_nodeList.indexOf(edge->sourceNode()))); + cn.setAttribute( "destination", + QString::number(m_nodeList.indexOf(edge->destNode()))); edges_root.appendChild(cn); } @@ -159,8 +163,10 @@ void GraphWidget::writeContentToPngFile(const QString &fileName) painter.setRenderHint(QPainter::Antialiasing); - /// @bug scene background is not rendered + m_scene->setBackgroundBrush(GraphWidget::m_paper); + m_scene->render(&painter); + painter.setBackground(GraphWidget::m_paper); painter.end(); img.save(fileName); @@ -170,6 +176,8 @@ void GraphWidget::writeContentToPngFile(const QString &fileName) void GraphWidget::keyPressEvent(QKeyEvent *event) { + qDebug() << __PRETTY_FUNCTION__; + // esc leaves node editing mode if (event->key() == Qt::Key_Escape && m_editingNode) { @@ -367,18 +375,11 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect) { Q_UNUSED(rect); - QRectF sceneRect = this->sceneRect(); - - // Fill - QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight()); - gradient.setColorAt(0, Qt::white); - gradient.setColorAt(1, Qt::lightGray); - painter->fillRect(rect.intersect(sceneRect), gradient); + painter->fillRect(m_scene->sceneRect(), GraphWidget::m_paper); painter->setBrush(Qt::NoBrush); - painter->drawRect(sceneRect); + painter->drawRect(m_scene->sceneRect()); } - void GraphWidget::scaleView(qreal scaleFactor) { qreal factor = transform().scale(scaleFactor, scaleFactor). @@ -499,7 +500,8 @@ void GraphWidget::addEdge(Node *source, Node *destination) { if (source->isConnected(destination)) { - m_parent->statusBarMsg(tr("There is already an edge between these two nodes.")); + m_parent->statusBarMsg( + tr("There is already an edge between these two nodes.")); } else { @@ -546,15 +548,17 @@ void GraphWidget::removeAllNodes() void GraphWidget::addFirstNode() { - Node *node1 = new Node(this); - node1->setHtml(QString("")); - m_scene->addItem(node1); - node1->setPos(-10, -10); - node1->setBorder(false); - m_nodeList.append(node1); + Node *node = new Node(this); + node->setHtml( + QString("")); + m_scene->addItem(node); + node->setPos(-25, -25); + node->setBorder(false); + m_nodeList.append(node); m_activeNode = m_nodeList.first(); m_activeNode->setActive(); +// m_scene->setFocusItem(m_activeNode); } QList GraphWidget::edges() const diff --git a/graphwidget.h b/graphwidget.h index 7938149..87086f1 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -64,6 +64,8 @@ private: bool m_edgeDeleting; bool m_contentChanged; QString m_fileName; + + static const QColor m_paper; }; #endif // GRAPHWIDGET_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 6bbd34c..de831b6 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -75,6 +75,8 @@ void MainWindow::newFile() contentChanged(false); m_fileName = "untitled"; setTitle(m_fileName); + + m_graphicsView->setFocus(); } void MainWindow::openFile(const QString &fileName) diff --git a/node.cpp b/node.cpp index b877628..48c3d71 100644 --- a/node.cpp +++ b/node.cpp @@ -10,11 +10,13 @@ const double Node::m_pi = 3.14159265358979323846264338327950288419717; const double Node::m_oneAndHalfPi = Node::m_pi * 1.5; const double Node::m_twoPi = Node::m_pi * 2.0; +const QColor Node::m_orange(255,215,0); + Node::Node(GraphWidget *parent) : m_graph(parent), m_isActive(false), m_number(-1), - m_hasBorder(true), + m_hasBorder(false), m_numberIsSpecial(false) { setFlag(ItemIsMovable); @@ -217,10 +219,16 @@ void Node::paint(QPainter *painter, painter->setPen(Qt::transparent); painter->setBrush(m_numberIsSpecial ? Qt::green : Qt::yellow); - /// @bug is there a 1pixel wide yellow line at the - /// bottom of borderless items? - painter->drawRect(QRect(boundingRect().topLeft().toPoint(), - boundingRect().bottomRight().toPoint())); + painter->drawRoundedRect(QRect(boundingRect().topLeft().toPoint(), + boundingRect().bottomRight().toPoint()), 20.0, 15.0); + painter->setBrush(Qt::NoBrush); + } + else if (m_isActive) // draw background for active node + { + painter->setPen(Qt::transparent); + painter->setBrush(Node::m_orange); + painter->drawRoundedRect(QRect(boundingRect().topLeft().toPoint(), + boundingRect().bottomRight().toPoint()), 20.0, 15.0); painter->setBrush(Qt::NoBrush); } diff --git a/node.h b/node.h index 6b80690..88f0280 100644 --- a/node.h +++ b/node.h @@ -65,6 +65,8 @@ private: static const double m_pi; static const double m_oneAndHalfPi; static const double m_twoPi; + + static const QColor m_orange; }; #endif // NODE_H