Theme Editor: Enabled loading project files from the project panel
[kugel-rb.git] / utils / themeeditor / projectmodel.cpp
blobf7451393381f8a7a36f18172f7ce115c6828b071
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include "projectmodel.h"
24 #include "projectfiles.h"
25 #include "editorwindow.h"
27 #include <QFile>
28 #include <QTextStream>
29 #include <QHash>
30 #include <QDir>
32 ProjectModel::ProjectModel(QString config, EditorWindow* mainWindow,
33 QObject *parent)
34 : QAbstractItemModel(parent),
35 mainWindow(mainWindow)
37 root = new ProjectRoot(config, this);
40 ProjectModel::~ProjectModel()
42 if(root)
43 delete root;
46 QModelIndex ProjectModel::index(int row, int column,
47 const QModelIndex& parent) const
50 if(!hasIndex(row, column, parent))
51 return QModelIndex();
53 ProjectNode* foundParent = root;
54 if(parent.isValid())
55 foundParent = static_cast<ProjectNode*>(parent.internalPointer());
57 if(row < foundParent->numChildren() && row >= 0)
58 return createIndex(row, column, foundParent->child(row));
59 else
60 return QModelIndex();
63 QModelIndex ProjectModel::parent(const QModelIndex &child) const
65 if(!child.isValid())
66 return QModelIndex();
68 ProjectNode* foundParent = static_cast<ProjectNode*>
69 (child.internalPointer())->parent();
71 if(foundParent == root)
72 return QModelIndex();
74 return createIndex(foundParent->row(), 0, foundParent);
77 int ProjectModel::rowCount(const QModelIndex &parent) const
79 if(!root)
80 return 0;
82 if(!parent.isValid())
83 return root->numChildren();
85 if(parent.column() != 0)
86 return 0;
88 return static_cast<ProjectNode*>(parent.internalPointer())->numChildren();
91 int ProjectModel::columnCount(const QModelIndex &parent) const
93 return numColumns;
96 QVariant ProjectModel::data(const QModelIndex &index, int role) const
98 if(!index.isValid())
99 return QVariant();
101 if(role != Qt::DisplayRole)
102 return QVariant();
104 return static_cast<ProjectNode*>
105 (index.internalPointer())->data(index.column());
108 QVariant ProjectModel::headerData(int col, Qt::Orientation orientation,
109 int role) const
111 return QVariant();
114 Qt::ItemFlags ProjectModel::flags(const QModelIndex &index) const
116 return static_cast<ProjectNode*>
117 (index.internalPointer())->flags(index.column());
120 bool ProjectModel::setData(const QModelIndex &index, const QVariant &value,
121 int role)
123 return true;
126 void ProjectModel::loadFile(QString file)
128 mainWindow->loadTabFromFile(file);
131 void ProjectModel::activated(const QModelIndex &index)
133 static_cast<ProjectNode*>(index.internalPointer())->activated();
136 /* Constructor and destructor for the root class */
137 ProjectRoot::ProjectRoot(QString config, ProjectModel* model)
139 this->model = model;
141 /* Reading the config file */
142 QFile cfg(config);
143 cfg.open(QFile::ReadOnly | QFile::Text);
144 if(!cfg.isReadable())
145 return;
147 QTextStream fin(&cfg);
149 /* Storing the base directory */
150 QString confDir = config;
151 confDir.chop(confDir.length() - confDir.lastIndexOf('/') - 1);
152 QDir base(confDir);
153 base.cdUp();
154 settings.insert("themebase", base.canonicalPath());
156 while(!fin.atEnd())
158 QString current = fin.readLine();
159 QList<QString> parts = current.split(':');
161 /* A valid setting has at least one : */
162 if(parts.count() < 2)
163 continue;
165 QString setting;
166 for(int i = 1; i < parts.count(); i++)
167 setting.append(parts[i].trimmed());
169 settings.insert(parts[0].trimmed(), setting);
172 cfg.close();
174 /* Showing the files */
175 children.append(new ProjectFiles(settings, model, this));
179 ProjectRoot::~ProjectRoot()
181 for(int i = 0; i < children.count(); i++)
182 delete children[i];