1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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"
28 #include <QTextStream>
32 ProjectModel::ProjectModel(QString config
, EditorWindow
* mainWindow
,
34 : QAbstractItemModel(parent
),
35 mainWindow(mainWindow
)
37 root
= new ProjectRoot(config
, this);
40 ProjectModel::~ProjectModel()
46 QModelIndex
ProjectModel::index(int row
, int column
,
47 const QModelIndex
& parent
) const
50 if(!hasIndex(row
, column
, parent
))
53 ProjectNode
* foundParent
= root
;
55 foundParent
= static_cast<ProjectNode
*>(parent
.internalPointer());
57 if(row
< foundParent
->numChildren() && row
>= 0)
58 return createIndex(row
, column
, foundParent
->child(row
));
63 QModelIndex
ProjectModel::parent(const QModelIndex
&child
) const
68 ProjectNode
* foundParent
= static_cast<ProjectNode
*>
69 (child
.internalPointer())->parent();
71 if(foundParent
== root
)
74 return createIndex(foundParent
->row(), 0, foundParent
);
77 int ProjectModel::rowCount(const QModelIndex
&parent
) const
83 return root
->numChildren();
85 if(parent
.column() != 0)
88 return static_cast<ProjectNode
*>(parent
.internalPointer())->numChildren();
91 int ProjectModel::columnCount(const QModelIndex
&parent
) const
96 QVariant
ProjectModel::data(const QModelIndex
&index
, int role
) const
101 if(role
!= Qt::DisplayRole
)
104 return static_cast<ProjectNode
*>
105 (index
.internalPointer())->data(index
.column());
108 QVariant
ProjectModel::headerData(int col
, Qt::Orientation orientation
,
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
,
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
)
141 /* Reading the config file */
143 cfg
.open(QFile::ReadOnly
| QFile::Text
);
144 if(!cfg
.isReadable())
147 QTextStream
fin(&cfg
);
149 /* Storing the base directory */
150 QString confDir
= config
;
151 confDir
.chop(confDir
.length() - confDir
.lastIndexOf('/') - 1);
154 settings
.insert("themebase", base
.canonicalPath());
158 QString current
= fin
.readLine();
159 QList
<QString
> parts
= current
.split(':');
161 /* A valid setting has at least one : */
162 if(parts
.count() < 2)
166 for(int i
= 1; i
< parts
.count(); i
++)
167 setting
.append(parts
[i
].trimmed());
169 settings
.insert(parts
[0].trimmed(), setting
);
174 /* Showing the files */
175 children
.append(new ProjectFiles(settings
, model
, this));
179 ProjectRoot::~ProjectRoot()
181 for(int i
= 0; i
< children
.count(); i
++)