Source

pynoto / src / Project / ProjectManagerWidget.cpp

Full commit
#include <QDebug>
#include <QFileInfo>
#include <QHeaderView>
#include <QMenu>
#include "ProjectManagerWidget.h"
#include "ProjectTreeModel.h"
#include "Include/IProject.h"
#include "Node.h"
#include "NewItems/AddNewWizard.h"

namespace Project {

ProjectManagerWidget::ProjectManagerWidget(QWidget * parent, IProject* project):
    QTreeView(parent),
    _project(project),
    _menu(NULL)
{
    header()->hide();
    setFrameStyle(QFrame::NoFrame);
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(_project, SIGNAL(loaded()), SLOT(onProjectLoaded()));
    connect(_project, SIGNAL(unloaded()), SLOT(onProjectUnloaded()));

    connect(this, SIGNAL(activated(QModelIndex)), SLOT(onItemActivated(QModelIndex)));
    connect(this, SIGNAL(collapsed(QModelIndex)), SLOT(onItemCollapsed(QModelIndex)));
    connect(this, SIGNAL(expanded(QModelIndex)), SLOT(onItemExpanded(QModelIndex)));
    connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(onMenuShow(QPoint)));
}

ProjectManagerWidget::~ProjectManagerWidget()
{

}

void ProjectManagerWidget::onProjectLoaded()
{
    ProjectTreeModel * model = new ProjectTreeModel(_project, this);
    setModel(model);
    restoreSession();
}

void ProjectManagerWidget::onProjectUnloaded()
{
    QAbstractItemModel *mod = model();
    setModel(NULL);
    delete mod;
}

void ProjectManagerWidget::restoreSession()
{
    blockSignals(true);
    foreach(QString dir, _project->openedDirs())
        foreach(QModelIndex index, qobject_cast<ProjectTreeModel *>(model())->indexForPath(dir))
            expand(index);

    blockSignals(false);
    foreach(QString file, _project->openedFiles()){
        _project->openFile(file);
    }
}

void ProjectManagerWidget::onItemActivated(const QModelIndex & index)
{
    QString path = index.data(Qt::EditRole).toString();
    if (QFileInfo(path).isFile()){
        _project->openFile(path);
    }
}

void ProjectManagerWidget::onItemCollapsed(const QModelIndex& index)
{
    QString path = index.data(Qt::EditRole).toString();
    _project->closeDir(path);
}

void ProjectManagerWidget::onItemExpanded(const QModelIndex& index)
{
    QString path = index.data(Qt::EditRole).toString();
    _project->openDir(path);
}

void ProjectManagerWidget::onMenuShow(const QPoint& pt)
{
    if (!_menu){
        _menu = new QMenu(this);
        _actions["new"] = _menu->addAction(tr("Add new"), this, SLOT(addNew()));
        _actions["delete"] = _menu->addAction(tr("Delete"));
        _actions["rename"] = _menu->addAction(tr("Rename"));
        _menu->addSeparator();
        _actions["open"] = _menu->addAction(tr("Open"), this, SLOT(openFile()));
        _actions["compile"] = _menu->addAction(tr("Compile"));
    }
    QModelIndex index = indexAt(pt);
    if (index.isValid()){
        NodeTypes type = (NodeTypes)index.data(Qt::UserRole+1).toInt();
        _actions["new"]->setEnabled(type == DirectoryType || type == PyModuleType);
        _actions["delete"]->setEnabled(true);
        _actions["rename"]->setEnabled(true);
        _actions["open"]->setEnabled(type != DirectoryType && type != PyModuleType);
        _actions["compile"]->setEnabled(type == DesignerType || type == ResourcesType);
    } else {
        _actions["new"]->setEnabled(true);
        _actions["delete"]->setEnabled(false);
        _actions["rename"]->setEnabled(false);
        _actions["open"]->setEnabled(false);
        _actions["compile"]->setEnabled(false);
    }
    _menu->popup(mapToGlobal(pt));
}

QSize ProjectManagerWidget::minimumSizeHint()
{
    return QSize(250, 200);
}

void ProjectManagerWidget::openFile()
{
    QString path = currentIndex().data(Qt::EditRole).toString();
    if (QFileInfo(path).isFile()){
        _project->openFile(path);
    }
}

void ProjectManagerWidget::addNew()
{
    QString path = currentIndex().data(Qt::EditRole).toString();
    NodeTypes type = (NodeTypes)currentIndex().data(Qt::UserRole+1).toInt();
    AddNewWizard wiz(path, type, this);
    wiz.exec();
}

}