00001 #include "node.h"
00002
00003 float node::scaleFactor = 1.0;
00004 QTreeWidget * node::treeWidget = NULL;
00005
00006 node::node(QString path, QObject *parent) :
00007 QObject(parent),
00008 depth(0),
00009 fileSize(0),
00010 totalSize(0),
00011 offset(0)
00012 {
00013 this->path = path;
00014 }
00015
00016 void node::addChild(node *childNode) {
00017 children.append(childNode);
00018
00019 if (!childNode->bDir) {
00020 childNode->setParentItem(this);
00021 }
00022 }
00023
00024 void node::addToTotalSize(qint64 size) {
00025 totalSize += size;
00026 }
00027
00028 void node::traverse() {
00029 QDir dir(path);
00030 QDirIterator it(dir);
00031 traverse(it);
00032 }
00033
00034 qint64 node::traverse(QDirIterator& it) {
00035 while (it.hasNext()) {
00036 it.next();
00037
00038 if (it.fileName() == "." || it.fileName() == "..")
00039 continue;
00040
00041 QFileInfo fileInfo = it.fileInfo();
00042 QString indent;
00043 for (int i=0; i<depth; i++) {
00044 indent += " ";
00045 }
00046 node *childNode = new node(it.fileName(), this);
00047 childNode->setSize(fileInfo.size());
00048 childNode->addToTotalSize(fileInfo.size());
00049 childNode->bDir = fileInfo.isDir();
00050 childNode->depth = depth+1;
00051
00052 addChild(childNode);
00053
00054 addToTotalSize( childNode->getSize() );
00055
00056 if (fileInfo.isDir()) {
00057 QDir dir(it.filePath());
00058 QDirIterator subIt(dir);
00059
00060 addToTotalSize(childNode->traverse(subIt));
00061 }
00062 }
00063 return totalSize;
00064 }
00065
00066 float node::getRelativeSize() {
00067
00068
00069 float div = totalSize / parent()->totalSize;
00070
00071 return totalSize / parent()->totalSize;
00072
00073 }
00074
00075 bool node::isVertical() {
00076 return (depth % 2 == 1);
00077 }
00078
00079 QRectF node::calculateRect() {
00080 node *p = parent();
00081 if (p == NULL)
00082 return rect();
00083
00084 float x, y, w, h;
00085
00086 if (isVertical()) {
00087
00088 float newY = p->rect().y() + p->getOffset();
00089 float newH = p->rect().height() * getRelativeSize();
00090
00091 x = p->rect().x();
00092 y = newY;
00093 w = p->rect().width();
00094 h = newH;
00095 } else {
00096
00097 float newX = p->rect().x() + p->getOffset();
00098
00099 float newW = p->rect().width() * getRelativeSize();
00100
00101 x = newX;
00102 y = p->rect().y();
00103 w = newW;
00104 h = p->rect().height();
00105 }
00106
00107 setRect(x, y, w, h);
00108
00109 return rect();
00110 }
00111
00112 void node::addParentOffset(float offset) {
00113 node *p = parent();
00114 if (p == NULL) return;
00115 p->offset += offset;
00116 }
00117
00118 void node::addParentOffset(QRectF rect) {
00119 float offset;
00120 if (isVertical())
00121 offset = rect.height();
00122 else offset = rect.width();
00123 addParentOffset(offset);
00124 }
00125
00126 node* node::parent() {
00127 return (node *) QObject::parent();
00128 }
00129
00130 void node::mousePressEvent(QGraphicsSceneMouseEvent *event) {
00131 qDebug();
00132 if (parent() == NULL)
00133 qDebug() << "clicked:" << path << "isVertical:" << isVertical();
00134 else
00135 qDebug() << "clicked:" << path << "isVertical:" << isVertical() << "parent:" << parent()->path;
00136 qDebug() << " rect: " << rect();
00137 qDebug() << " mapped rect:" << mapToScene(rect()).boundingRect();
00138 if (parent() == NULL) return;
00139 qDebug() << " parent rect:" << parent()->rect();
00140 qDebug() << " par. rect m:" << (parent()->mapToScene(rect())).boundingRect();
00141 qDebug() << " pos:" << event->pos() << " scenePos:" << event->scenePos();
00142 qDebug() << " buttonDownPos:" << event->buttonDownPos(Qt::LeftButton) << " screenPos:" << event->screenPos();
00143
00144 if (treeWidget != NULL && treeItem != NULL) {
00145 treeWidget->setCurrentItem(treeItem);
00146 }
00147 }