00001 #include "mainwindow.h"
00002 #include <iostream>
00003
00004 using namespace std;
00005
00006 MainWindow::MainWindow() {
00007 createActions();
00008 createToolBar();
00009 createDock();
00010 createCentralWidget();
00011
00012 scale = 1.0;
00013
00014 setUnifiedTitleAndToolBarOnMac(true);
00015
00016 connect(openAction, SIGNAL(triggered()), this, SLOT(chooseFolder()));
00017 connect(zoomInAction, SIGNAL(triggered()), this, SLOT(zoomIn()));
00018 connect(zoomOutAction, SIGNAL(triggered()), this, SLOT(zoomOut()));
00019 connect(slider, SIGNAL(valueChanged(int)), scene, SLOT(setScale(int)));
00020 connect( treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
00021 this, SLOT(setRoot(QTreeWidgetItem*,int)) );
00022 }
00023
00024 void MainWindow::createActions() {
00025
00026 openAction = new QAction(QIcon(":/images/open.png"), tr("&Open Folder"), this);
00027 openAction->setShortcut(tr("Ctrl+O"));
00028 openAction->setStatusTip(tr("Select a folder to visualize..."));
00029
00030
00031 zoomInAction = new QAction(QIcon(":/images/zoomin.png"), tr("&Zoom In"), this);
00032 zoomInAction->setShortcut(tr("Ctrl++"));
00033 zoomInAction->setStatusTip(tr("Zoom out..."));
00034
00035
00036 zoomOutAction = new QAction(QIcon(":/images/zoomout.png"), tr("&Zoom Out"), this);
00037 zoomOutAction->setShortcut(tr("Ctrl+-"));
00038 zoomOutAction->setStatusTip(tr("Zoom in..."));
00039 }
00040
00041 void MainWindow::createToolBar() {
00042 toolBar = addToolBar("Main");
00043 toolBar->addAction(openAction);
00044 toolBar->addAction(zoomInAction);
00045 toolBar->addAction(zoomOutAction);
00046
00047 slider = new QSlider(Qt::Horizontal, this);
00048 slider->setRange(0, 100);
00049 slider->setValue(100);
00050 slider->setMaximumWidth(150);
00051 toolBar->addWidget(slider);
00052 }
00053
00054 void MainWindow::createDock() {
00055 treeWidget = new QTreeWidget();
00056 treeWidget->setColumnCount(2);
00057 treeWidget->setHeaderLabels( QStringList() << "dir" << "size" );
00058
00059 node::setTreeWidget(treeWidget);
00060
00061 dockWidget = new QDockWidget("Directory Tree", this);
00062 dockWidget->setWidget(treeWidget);
00063 dockWidget->setTitleBarWidget(new QWidget);
00064
00065 addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
00066 }
00067
00068 void MainWindow::createCentralWidget() {
00069 scene = new BeamTreeScene;
00070 view = new QGraphicsView(scene);
00071 scene->setView(view);
00072 setCentralWidget(view);
00073 }
00074
00075 void MainWindow::chooseFolder() {
00076 QString dir = QFileDialog::getExistingDirectory(
00077 this,
00078 "Choose directory...",
00079 "",
00080 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
00081 );
00082
00083 qDebug() << "dir:" << dir;
00084
00085 node *n = new node(dir);
00086 n->setRect( 0, 0, 500, 500 );
00087
00088 n->traverse();
00089 treeWidget->clear();
00090 treeWidget->addTopLevelItem(fillTree(n));
00091 scene->reset();
00092 scene->render(n);
00093 }
00094
00095
00096 TreeItem* MainWindow::fillTree(node *root, TreeItem *rootItem){
00097
00098 if (rootItem == 0)
00099 {
00100 QStringList data;
00101 data << root->getPath() << QString::number(root->getTotalSize());
00102 TreeItem *rootItem = new TreeItem(data);
00103 rootItem->setNode(root);
00104 fillTree(root, rootItem);
00105 root->setTreeItem(rootItem);
00106 return rootItem;
00107 }
00108 else
00109 {
00110 QVector<node*> children = root->getChildren();
00111 for (int i=0; i < children.size(); i++) {
00112 node *n = children[i];
00113 QStringList data;
00114 data << n->getPath() << QString::number(n->getTotalSize());
00115 TreeItem *child = new TreeItem(rootItem, data);
00116 child->setNode(n);
00117 rootItem->addChild(child);
00118 rootItem->setExpanded(true);
00119 n->setTreeItem(child);
00120 if (n->isDir()) {
00121 fillTree(n, child);
00122 }
00123 }
00124 return 0;
00125 }
00126 }
00127
00128 void MainWindow::zoomIn() {
00129 scale += 0.1;
00130 view->resetTransform();
00131 view->scale(scale, scale);
00132 }
00133
00134 void MainWindow::zoomOut() {
00135 scale -= 0.1;
00136 if (scale < 0.5) scale = 0.5;
00137 view->resetTransform();
00138 view->scale(scale, scale);
00139 }
00140
00141 void MainWindow::setRoot(QTreeWidgetItem *item, int column) {
00142 TreeItem *treeItem = (TreeItem *) item;
00143 node *n = treeItem->getNode();
00144 scene->hilight(n);
00145 }