文档库 最新最全的文档下载
当前位置:文档库 › qt源码

qt源码

qt源码
qt源码

#ifndef NODE_H

#define NODE_H

#include

#include

#include

#include

class Link;

class Node : public QGraphicsItem

{

Q_DECLARE_TR_FUNCTIONS(Node)

public:

Node();

~Node();

void setText(const QString &text);

QString text() const;

void setTextColor(const QColor &color);

QColor textColor() const;

void setOutlineColor(const QColor &color);

QColor outlineColor() const;

void setBackgroundColor(const QColor &color);

QColor backgroundColor() const;

void addLink(Link *link);

void removeLink(Link *link);

QRectF boundingRect() const;

QPainterPath shape() const;

void paint(QPainter *painter,

const QStyleOptionGraphicsItem *option, QWidget *widget);

protected:

void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); QVariant itemChange(GraphicsItemChange change,

const QVariant &value);

private:

QRectF outlineRect() const;

int roundness(double size) const;

QSet myLinks;

QString myText;

QColor myTextColor;

QColor myBackgroundColor;

QColor myOutlineColor;

};

#endif

CPP

#include

#include "link.h"

#include "node.h"

Node::Node()

{

myTextColor = Qt::darkGreen;

myOutlineColor = Qt::darkBlue;

myBackgroundColor = Qt::green;

setFlags(ItemIsMovable | ItemIsSelectable); }

Node::~Node()

{

foreach (Link *link, myLinks)

delete link;

}

void Node::setText(const QString &text)

{

prepareGeometryChange();

myText = text;

update();

}

QString Node::text() const

{

return myText;

}

void Node::setTextColor(const QColor &color)

{

myTextColor = color;

update();

}

QColor Node::textColor() const

{

return myTextColor;

}

void Node::setOutlineColor(const QColor &color) {

myOutlineColor = color;

update();

}

QColor Node::outlineColor() const

{

return myOutlineColor;

}

void Node::setBackgroundColor(const QColor &color) {

myBackgroundColor = color;

update();

}

QColor Node::backgroundColor() const

{

return myBackgroundColor;

}

void Node::addLink(Link *link)

{

myLinks.insert(link);

}

void Node::removeLink(Link *link)

{

myLinks.remove(link);

}

QRectF Node::boundingRect() const

const int Margin = 1;

return outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin) ;

}

QPainterPath Node::shape() const

{

QRectF rect = outlineRect();

QPainterPath path;

path.addRoundRect(rect, roundness(rect.width()),

roundness(rect.height()));

return path;

}

void Node::paint(QPainter *painter,

const QStyleOptionGraphicsItem *option,

QWidget * /* widget */)

{

QPen pen(myOutlineColor);

if (option->state & QStyle::State_Selected) {

pen.setStyle(Qt::DotLine);

pen.setWidth(2);

}

painter->setPen(pen);

painter->setBrush(myBackgroundColor);

QRectF rect = outlineRect();

painter->drawRoundRect(rect, roundness(rect.width()),

roundness(rect.height()));

painter->setPen(myTextColor);

painter->drawText(rect, Qt::AlignCenter, myText);

}

void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {

QString text = QInputDialog::getText(event->widget(),

tr("Edit Text"), tr("Enter new text:"),

QLineEdit::Normal, myText);

if (!text.isEmpty())

setText(text);

}

QVariant Node::itemChange(GraphicsItemChange change,

const QVariant &value)

{

if (change == ItemPositionHasChanged) {

foreach (Link *link, myLinks)

link->trackNodes();

}

return QGraphicsItem::itemChange(change, value);

}

QRectF Node::outlineRect() const

{

const int Padding = 1;////////////////control the gap between tex t and node bound

QFontMetricsF metrics = qApp->font();

QRectF rect = metrics.boundingRect(myText);

rect.adjust(-Padding, -Padding, +Padding, +Padding);

rect.translate(-rect.center());

return rect;

}

int Node::roundness(double size) const

{

const int Diameter = 12;

return 100 * Diameter / int(size);

}

你这里面和显示有关的不就一个 paint 函数么?这个函数的核心不就是QPainter?字体不就是font?打开QPainter的Manual,自己去看font

相关文档