00001 #include "beamtreescene.h" 00002 00003 BeamTreeScene::BeamTreeScene(QObject *parent) : 00004 QGraphicsScene(parent), 00005 timerRunning(false) 00006 { 00007 pen.setWidthF(0); 00008 } 00009 00010 void BeamTreeScene::render(node *dir) { 00011 dir->setPen(pen); 00012 addItem(dir); 00013 QVector<node*> children = dir->getChildren(); 00014 for (int i=0; i < children.size(); i++) { 00015 node *n = children[i]; 00016 QRectF rect = n->calculateRect(); 00017 n->setZValue(n->getDepth()); 00018 n->addParentOffset(rect); 00019 00020 QLinearGradient lingrad; 00021 00022 if (n->isVertical()) 00023 { 00024 lingrad.setStart(n->rect().topLeft().x(), n->rect().topLeft().y() + n->rect().height()/2); 00025 lingrad.setFinalStop(n->rect().bottomRight().x(), n->rect().topLeft().y() + n->rect().height()/2); 00026 } 00027 else 00028 { 00029 lingrad.setStart(n->rect().topLeft().x() + n->rect().width()/2, n->rect().topLeft().y()); 00030 lingrad.setFinalStop(n->rect().topLeft().x() + n->rect().width()/2, n->rect().bottomRight().y()); 00031 00032 } 00033 00034 if (n->isDir()){ 00035 lingrad.setColorAt(0, QColor(50,50,50,150)); 00036 lingrad.setColorAt(0.5, QColor(150,150,150,150)); 00037 lingrad.setColorAt(1, QColor(50,50,50,150)); 00038 n->setBrush(QBrush(lingrad)); 00039 } 00040 else{ 00041 lingrad.setColorAt(0, QColor(50,50,255,150)); 00042 lingrad.setColorAt(0.5, QColor(100,100,255,150)); 00043 lingrad.setColorAt(1, QColor(50,50,255, 150)); 00044 n->setBrush(QBrush(lingrad)); 00045 } 00046 n->setPen(QPen()); 00047 addItem(n); 00048 render(n); 00049 } 00050 } 00051 00052 void BeamTreeScene::draw() { 00053 draw(1.0); 00054 } 00055 00056 void BeamTreeScene::draw(float scale) { 00057 QList<QGraphicsItem *> list = items(); 00058 QListIterator<QGraphicsItem *> it(list); 00059 while(it.hasNext()) { 00060 00061 node *n = (node *) it.next(); 00062 n->resetTransform(); 00063 QPointF origin = n->rect().topLeft(); 00064 if (n->isDir()) { 00065 if (n->isVertical()) { 00066 n->translate(n->rect().center().x(),n->rect().center().y() ); 00067 n->scale(1,scale); 00068 n->translate((-1)*n->rect().center().x(),(-1)*n->rect().center().y() ); 00069 } else { 00070 n->translate(n->rect().center().x(),n->rect().center().y() ); 00071 n->scale(scale,1); 00072 n->translate((-1)*n->rect().center().x(),(-1)*n->rect().center().y() ); 00073 } 00074 } 00075 } 00076 } 00077 00078 void BeamTreeScene::setScale(int scale) { 00079 float fScale = ((float)scale) / 100; 00080 draw(fScale); 00081 } 00082 00083 void BeamTreeScene::reset() { 00084 clear(); 00085 } 00086 00087 void BeamTreeScene::hilight(node *n) { 00088 if (timerRunning) return; 00089 oldBrush = n->brush(); 00090 oldNode = n; 00091 textItem = new QGraphicsSimpleTextItem(n->getPath(), n); 00092 textItem->setPos(n->rect().topLeft()); 00093 textItem->scale(2,2); 00094 n->setBrush(QBrush(QColor(255,0,0,200))); 00095 n->update(n->rect()); 00096 timerRunning = true; 00097 QTimer::singleShot(500, this, SLOT(resetColor())); 00098 } 00099 00100 void BeamTreeScene::resetColor() { 00101 textItem->setVisible(false); 00102 oldNode->setBrush(oldBrush); 00103 oldNode->update(oldNode->rect()); 00104 timerRunning = false; 00105 }