colorful background, export bug fix: no more transparent bg

master
Denes Matetelki 14 years ago
parent e039fce7f4
commit 3cade9b1c1

@ -11,6 +11,9 @@
#include "math.h" #include "math.h"
#include "mainwindow.h" #include "mainwindow.h"
//const QColor GraphWidget::m_paper(255,255,105);
const QColor GraphWidget::m_paper(255,255,153);
GraphWidget::GraphWidget(MainWindow *parent) : GraphWidget::GraphWidget(MainWindow *parent) :
QGraphicsView(parent), QGraphicsView(parent),
@ -27,14 +30,12 @@ GraphWidget::GraphWidget(MainWindow *parent) :
m_scene->setItemIndexMethod(QGraphicsScene::NoIndex); m_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
m_scene->setSceneRect(-400, -400, 800, 800); m_scene->setSceneRect(-400, -400, 800, 800);
setScene(m_scene); setScene(m_scene);
setCacheMode(CacheBackground); setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate); setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing); setRenderHint(QPainter::Antialiasing);
setTransformationAnchor(AnchorUnderMouse); setTransformationAnchor(AnchorUnderMouse);
setMinimumSize(400, 400); setMinimumSize(400, 400);
// connect(m_scene, SIGNAL(changed(const QList<QRectF> &)),
// m_parent, SLOT(contentChanged()));
} }
void GraphWidget::newScene() void GraphWidget::newScene()
@ -93,14 +94,15 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName)
QDomElement e = edges.item(i).toElement(); QDomElement e = edges.item(i).toElement();
if(!e.isNull()) if(!e.isNull())
{ {
m_scene->addItem(new Edge(m_nodeList[e.attribute("source").toInt()], m_scene->addItem(new Edge(
m_nodeList[e.attribute("source").toInt()],
m_nodeList[e.attribute("destination").toInt()])); m_nodeList[e.attribute("destination").toInt()]));
} }
} }
m_activeNode = m_nodeList.first(); m_activeNode = m_nodeList.first();
m_activeNode->setActive(); m_activeNode->setActive();
m_activeNode->setFocus();
this->show(); this->show();
} }
@ -131,8 +133,10 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName)
foreach(Edge *edge, edges()) foreach(Edge *edge, edges())
{ {
QDomElement cn = doc.createElement("edge"); QDomElement cn = doc.createElement("edge");
cn.setAttribute( "source", QString::number(m_nodeList.indexOf(edge->sourceNode()))); cn.setAttribute( "source",
cn.setAttribute( "destination", QString::number(m_nodeList.indexOf(edge->destNode()))); QString::number(m_nodeList.indexOf(edge->sourceNode())));
cn.setAttribute( "destination",
QString::number(m_nodeList.indexOf(edge->destNode())));
edges_root.appendChild(cn); edges_root.appendChild(cn);
} }
@ -159,8 +163,10 @@ void GraphWidget::writeContentToPngFile(const QString &fileName)
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
/// @bug scene background is not rendered m_scene->setBackgroundBrush(GraphWidget::m_paper);
m_scene->render(&painter); m_scene->render(&painter);
painter.setBackground(GraphWidget::m_paper);
painter.end(); painter.end();
img.save(fileName); img.save(fileName);
@ -170,6 +176,8 @@ void GraphWidget::writeContentToPngFile(const QString &fileName)
void GraphWidget::keyPressEvent(QKeyEvent *event) void GraphWidget::keyPressEvent(QKeyEvent *event)
{ {
qDebug() << __PRETTY_FUNCTION__;
// esc leaves node editing mode // esc leaves node editing mode
if (event->key() == Qt::Key_Escape && m_editingNode) if (event->key() == Qt::Key_Escape && m_editingNode)
{ {
@ -367,18 +375,11 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{ {
Q_UNUSED(rect); Q_UNUSED(rect);
QRectF sceneRect = this->sceneRect(); painter->fillRect(m_scene->sceneRect(), GraphWidget::m_paper);
// Fill
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, Qt::lightGray);
painter->fillRect(rect.intersect(sceneRect), gradient);
painter->setBrush(Qt::NoBrush); painter->setBrush(Qt::NoBrush);
painter->drawRect(sceneRect); painter->drawRect(m_scene->sceneRect());
} }
void GraphWidget::scaleView(qreal scaleFactor) void GraphWidget::scaleView(qreal scaleFactor)
{ {
qreal factor = transform().scale(scaleFactor, scaleFactor). qreal factor = transform().scale(scaleFactor, scaleFactor).
@ -499,7 +500,8 @@ void GraphWidget::addEdge(Node *source, Node *destination)
{ {
if (source->isConnected(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 else
{ {
@ -546,15 +548,17 @@ void GraphWidget::removeAllNodes()
void GraphWidget::addFirstNode() void GraphWidget::addFirstNode()
{ {
Node *node1 = new Node(this); Node *node = new Node(this);
node1->setHtml(QString("<img src=:/qtmindmap.svg width=50 height=50></img>")); node->setHtml(
m_scene->addItem(node1); QString("<img src=:/qtmindmap.svg width=50 height=50></img>"));
node1->setPos(-10, -10); m_scene->addItem(node);
node1->setBorder(false); node->setPos(-25, -25);
m_nodeList.append(node1); node->setBorder(false);
m_nodeList.append(node);
m_activeNode = m_nodeList.first(); m_activeNode = m_nodeList.first();
m_activeNode->setActive(); m_activeNode->setActive();
// m_scene->setFocusItem(m_activeNode);
} }
QList<Edge *> GraphWidget::edges() const QList<Edge *> GraphWidget::edges() const

@ -64,6 +64,8 @@ private:
bool m_edgeDeleting; bool m_edgeDeleting;
bool m_contentChanged; bool m_contentChanged;
QString m_fileName; QString m_fileName;
static const QColor m_paper;
}; };
#endif // GRAPHWIDGET_H #endif // GRAPHWIDGET_H

@ -75,6 +75,8 @@ void MainWindow::newFile()
contentChanged(false); contentChanged(false);
m_fileName = "untitled"; m_fileName = "untitled";
setTitle(m_fileName); setTitle(m_fileName);
m_graphicsView->setFocus();
} }
void MainWindow::openFile(const QString &fileName) void MainWindow::openFile(const QString &fileName)

@ -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_oneAndHalfPi = Node::m_pi * 1.5;
const double Node::m_twoPi = Node::m_pi * 2.0; const double Node::m_twoPi = Node::m_pi * 2.0;
const QColor Node::m_orange(255,215,0);
Node::Node(GraphWidget *parent) : Node::Node(GraphWidget *parent) :
m_graph(parent), m_graph(parent),
m_isActive(false), m_isActive(false),
m_number(-1), m_number(-1),
m_hasBorder(true), m_hasBorder(false),
m_numberIsSpecial(false) m_numberIsSpecial(false)
{ {
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
@ -217,10 +219,16 @@ void Node::paint(QPainter *painter,
painter->setPen(Qt::transparent); painter->setPen(Qt::transparent);
painter->setBrush(m_numberIsSpecial ? Qt::green : Qt::yellow); painter->setBrush(m_numberIsSpecial ? Qt::green : Qt::yellow);
/// @bug is there a 1pixel wide yellow line at the painter->drawRoundedRect(QRect(boundingRect().topLeft().toPoint(),
/// bottom of borderless items? boundingRect().bottomRight().toPoint()), 20.0, 15.0);
painter->drawRect(QRect(boundingRect().topLeft().toPoint(), painter->setBrush(Qt::NoBrush);
boundingRect().bottomRight().toPoint())); }
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); painter->setBrush(Qt::NoBrush);
} }

@ -65,6 +65,8 @@ private:
static const double m_pi; static const double m_pi;
static const double m_oneAndHalfPi; static const double m_oneAndHalfPi;
static const double m_twoPi; static const double m_twoPi;
static const QColor m_orange;
}; };
#endif // NODE_H #endif // NODE_H

Loading…
Cancel
Save