Source

pynoto / src / Project / ProjectFile.cpp

Full commit
#include <QSettings>
#include <QFileInfo>
#include <QDir>
#include <QTimer>
#include "ProjectFile.h"
#include "Include/PynotoApplication.h"
#include "Include/IPythonCodePlugin.h"
#include "Include/IPythonCode.h"
#include "Include/IPythonPlugin.h"

namespace Project {

ProjectFile::ProjectFile():
    _loaded(false),
    _settings(NULL),
    _pythonCode(NULL)
{

}

ProjectFile::~ProjectFile()
{
    delete _settings;
    delete _pythonCode;
}

bool ProjectFile::load(const QString& fileName)
{
    if (_loaded){
        unload();
    }
    QString projectFile = fileName;
    QFileInfo info(projectFile);
    if (info.isDir()){
        QDir dir(projectFile);
        foreach(QString item, dir.entryList(QDir::Files)){
            if (item.split(".").last() == "pynotoprj")
                projectFile = projectFile+QDir::separator()+item;
        }
    }
    QFileInfo file(projectFile);

    _settings = new QSettings(file.absoluteFilePath(), QSettings::IniFormat);
    _loaded = true;

    emit loaded();
    if (QColor(_settings->value("common/color", "transparent").toString()).alpha() != 0){
        Aux::emitPreferencesChanged("panel-color", QColor(_settings->value("common/color", "transparent").toString()));
    }
    return _loaded;
}

void ProjectFile::unload()
{
    delete _settings;
    _settings = NULL;
    _loaded = false;
    emit unloaded();
}

QString ProjectFile::path()
{
    return QFileInfo(_settings->fileName()).absolutePath();
}

void ProjectFile::setPath(const QString& p)
{
    if (_settings)
        delete _settings;
    _settings = new QSettings(p, QSettings::IniFormat);
}


QStringList ProjectFile::shownFiles()
{
    return _settings->value("common/showfiles", "*.py;*.ui;*.qrc;*.png;*.jpg;*.svg").toString().split(";");
}

QString ProjectFile::name()
{
    return _settings->value("common/name", "").toString();
}

void ProjectFile::setName(const QString& name)
{
    _settings->setValue("common/name", name);
}

QString ProjectFile::interpretName()
{
    return _settings->value("python/interpret", "").toString();
}

QString ProjectFile::interpretExec()
{
    Python::IPythonPlugin* py = Aux::plugin<Python::IPythonPlugin*>("python");
    Python::IPythonPlugin::Version *ver = py->pyVersion(interpretName());
    if (ver)
        return ver->exec;
    return "";
}

QString ProjectFile::fileName()
{
    return _settings->fileName();
}

QStringList ProjectFile::openedDirs()
{
    QStringList list;
    int size = _settings->beginReadArray("openedDirs");
    for (int i = 0; i < size; ++i) {
        _settings->setArrayIndex(i);
        list.append(_settings->value("dir").toString());
    }
    _settings->endArray();
    return list;
}

QStringList ProjectFile::openedFiles()
{
    QStringList list;
    int size = _settings->beginReadArray("openedFiles");
    for (int i = 0; i < size; ++i) {
        _settings->setArrayIndex(i);
        list.append(_settings->value("file").toString());
    }
    _settings->endArray();
    return list;
}

void ProjectFile::openFile(const QString& fileName, int offset)
{
    QStringList files = openedFiles();
    if (!files.contains(fileName)){
        files.append(fileName);
        _settings->remove("openedFiles");
        _settings->beginWriteArray("openedFiles");
        for (int i = 0; i < files.length(); ++i) {
            _settings->setArrayIndex(i);
            _settings->setValue("file", files[i]);
        }
        _settings->endArray();

    }
    emit openFileOffset(fileName, offset);
}

QString ProjectFile::mainScript()
{
    return _settings->value("common/mainscript", "").toString();
}

QString ProjectFile::fileDisplayName(const QString& fileName)
{
    if (!_loaded)
        return fileName;
    QDir info(path());
    return info.relativeFilePath(fileName);
}

PythonCode::IPythonCode* ProjectFile::pythonCode()
{
    if (!_pythonCode && !interpretName().isEmpty()){
        _pythonCode = Aux::plugin<PythonCode::IPythonCodePlugin*>("pythoncode")->python();
        _pythonCode->init(interpretExec(), path(), mainScript(), _settings->fileName());
    }
    connect(_pythonCode, SIGNAL(systemError(QString)), SIGNAL(systemError(QString)));
    return _pythonCode;
}

QString ProjectFile::guiName()
{
    return _settings->value("python/guiname", "").toString();
}

void ProjectFile::setShownFiles(const QStringList& list)
{
    QStringList newList;
    foreach(QString it, list){
        newList.append(it.trimmed());
    }
    newList.removeDuplicates();
    if (shownFiles() != newList){
        _settings->setValue("common/showfiles", newList.join(";"));
        emit shownFilesChanged();
    }
}

void ProjectFile::setMainScript(const QString& mainScript)
{
    _settings->setValue("common/mainscript", mainScript);
}

void ProjectFile::setInterpretName(const QString& name)
{
    if (interpretName() != name){
        _settings->setValue("python/interpret", name);
        if (_pythonCode)
            _pythonCode->init(interpretExec(), path(), mainScript(), _settings->fileName());
        emit interpretChanged();
    }
}

void ProjectFile::setGuiName(const QString& name)
{
    _settings->setValue("python/guiname", name);
}

QMap<QString, QVariant> ProjectFile::codeCheck()
{
    QMap<QString, QVariant> check;

    check.insert("dummyVars"        , _settings->value("lint/dummyVar", "_|dummy"));
    check.insert("badFunctions"     , _settings->value("lint/badFunc", "map, filter, apply, input"));
    check.insert("goodNames"        , _settings->value("lint/goodNames", "i, j, k, ex, Run, _"));
    check.insert("badNames"         , _settings->value("lint/badNames", "foo, bar, baz"));
    check.insert("maxLineLength"    , _settings->value("lint/maxLineLength", 80));
    check.insert("maxModuleLenght"  , _settings->value("lint/maxModLines", 1000));


    check.insert("moduleStyle"      , _settings->value("lint/checkModule", "(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$"));
    check.insert("constStyle"       , _settings->value("lint/checkConst", "(([A-Z_][A-Z0-9_]*)|(__.*__))$"));
    check.insert("classStyle"       , _settings->value("lint/checkClass", "[A-Z_][a-zA-Z0-9]+$"));
    check.insert("funcStyle"        , _settings->value("lint/checkFunc", "[a-z_][a-z0-9_]{2,30}$"));
    check.insert("methodStyle"      , _settings->value("lint/checkMethod", "[a-z_][a-z0-9_]{2,30}$"));
    check.insert("attrStyle"        , _settings->value("lint/checkAttr", "[a-z_][a-z0-9_]{2,30}$"));
    check.insert("argStyle"         , _settings->value("lint/checkArg", "[a-z_][a-z0-9_]{2,30}$"));
    check.insert("varStyle"         , _settings->value("lint/checkVar", "[a-z_][a-z0-9_]{2,30}$"));
    check.insert("inlVarStyle"      , _settings->value("lint/checkInlineVar", "[A-Za-z_][A-Za-z0-9_]*$"));


    check.insert("maxArgs"          , _settings->value("lint/maxArgs", 5));
    check.insert("ignoreNames"      , _settings->value("lint/ignArgNames", "_.*"));
    check.insert("maxLocals"        , _settings->value("lint/maxLocals", 15));
    check.insert("maxReturns"       , _settings->value("lint/maxReturns", 6));
    check.insert("maxBranchs"       , _settings->value("lint/maxBranchs", 12));
    check.insert("maxStats"         , _settings->value("lint/maxStats", 50));
    check.insert("maxParents"       , _settings->value("lint/maxParents", 7));
    check.insert("maxAttr"          , _settings->value("lint/maxAttrs", 7));
    check.insert("minPublic"        , _settings->value("lint/minPublic", 2));
    check.insert("maxPublic"        , _settings->value("lint/maxPublic", 20));

    check.insert("useStyle"         , _settings->value("lint/style", 0));

    return check;
}

#define SETINT(confname, mapname) \
    if (_settings->value(confname).toInt() != check[mapname].toInt()){ \
        changed |= true; \
        _settings->setValue(confname, check[mapname]); \
    }

#define SETTEXT(confname, mapname) \
    if (_settings->value(confname, "").toString() != check[mapname].toString()){ \
        changed |= true; \
        _settings->setValue(confname, check[mapname]); \
    }

void ProjectFile::setCodeCheck(const QMap<QString, QVariant>& check)
{
    bool changed = false;

    SETTEXT("lint/dummyVar"      , "dummyVars");
    SETTEXT("lint/badFunc"       , "badFunctions");
    SETTEXT("lint/goodNames"     , "goodNames");
    SETTEXT("lint/badNames"      , "badNames");
    SETINT ("lint/maxLineLength" , "maxLineLength");
    SETINT ("lint/maxModLines"   , "maxModuleLenght");

    SETTEXT("lint/checkModule"   , "moduleStyle");
    SETTEXT("lint/checkConst"    , "constStyle");
    SETTEXT("lint/checkClass"    , "classStyle");
    SETTEXT("lint/checkFunc"     , "funcStyle");
    SETTEXT("lint/checkMethod"   , "methodStyle");
    SETTEXT("lint/checkAttr"     , "attrStyle");
    SETTEXT("lint/checkArg"      , "argStyle");
    SETTEXT("lint/checkVar"      , "varStyle");
    SETTEXT("lint/checkInlineVar", "inlVarStyle");

    SETINT ("lint/maxArgs"       , "maxArgs");
    SETTEXT("lint/ignArgNames"   , "ignoreNames");
    SETINT ("lint/maxLocals"     , "maxLocals");
    SETINT ("lint/maxReturns"    , "maxReturns");
    SETINT ("lint/maxBranchs"    , "maxBranchs");
    SETINT ("lint/maxStats"      , "maxStats");
    SETINT ("lint/maxParents"    , "maxParents");
    SETINT ("lint/maxAttrs"      , "maxAttr");
    SETINT ("lint/minPublic"     , "minPublic");
    SETINT ("lint/maxPublic"     , "maxPublic");

    SETINT ("lint/style"         , "useStyle");

    _settings->sync();
    if (changed){
        _pythonCode->init(interpretExec(), path(), mainScript(), _settings->fileName());
        emit lintChanged();
    }
}

QColor ProjectFile::color()
{
    return QColor(_settings->value("common/color", "transparent").toString());
}

void ProjectFile::setColor(const QColor& color)
{
    if (color.alpha() == 0)
        _settings->setValue("common/color", "transparent");
    else
        _settings->setValue("common/color", color.name());

    Aux::emitPreferencesChanged("panel-color", color);
}

}