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"
27 #include <QTextStream>
31 ProjectModel::ProjectModel(QString config
, QObject
*parent
) :
32 QAbstractItemModel(parent
)
34 root
= new ProjectRoot(config
);
37 ProjectModel::~ProjectModel()
43 QModelIndex
ProjectModel::index(int row
, int column
,
44 const QModelIndex
& parent
) const
47 if(!hasIndex(row
, column
, parent
))
50 ProjectNode
* foundParent
= root
;
52 foundParent
= static_cast<ProjectNode
*>(parent
.internalPointer());
54 if(row
< foundParent
->numChildren() && row
>= 0)
55 return createIndex(row
, column
, foundParent
->child(row
));
60 QModelIndex
ProjectModel::parent(const QModelIndex
&child
) const
65 ProjectNode
* foundParent
= static_cast<ProjectNode
*>
66 (child
.internalPointer())->parent();
68 if(foundParent
== root
)
71 return createIndex(foundParent
->row(), 0, foundParent
);
74 int ProjectModel::rowCount(const QModelIndex
&parent
) const
80 return root
->numChildren();
82 if(parent
.column() != 0)
85 return static_cast<ProjectNode
*>(parent
.internalPointer())->numChildren();
88 int ProjectModel::columnCount(const QModelIndex
&parent
) const
93 QVariant
ProjectModel::data(const QModelIndex
&index
, int role
) const
98 if(role
!= Qt::DisplayRole
)
101 return static_cast<ProjectNode
*>
102 (index
.internalPointer())->data(index
.column());
105 QVariant
ProjectModel::headerData(int col
, Qt::Orientation orientation
,
111 Qt::ItemFlags
ProjectModel::flags(const QModelIndex
&index
) const
113 return static_cast<ProjectNode
*>
114 (index
.internalPointer())->flags(index
.column());
117 bool ProjectModel::setData(const QModelIndex
&index
, const QVariant
&value
,
123 /* Constructor and destructor for the root class */
124 ProjectRoot::ProjectRoot(QString config
)
126 /* Reading the config file */
128 cfg
.open(QFile::ReadOnly
| QFile::Text
);
129 if(!cfg
.isReadable())
132 QTextStream
fin(&cfg
);
134 /* Storing the base directory */
135 QString confDir
= config
;
136 confDir
.chop(confDir
.length() - confDir
.lastIndexOf('/') - 1);
139 settings
.insert("themebase", base
.canonicalPath());
143 QString current
= fin
.readLine();
144 QList
<QString
> parts
= current
.split(':');
146 /* A valid setting has at least one : */
147 if(parts
.count() < 2)
151 for(int i
= 1; i
< parts
.count(); i
++)
152 setting
.append(parts
[i
].trimmed());
154 settings
.insert(parts
[0].trimmed(), setting
);
159 /* Showing the files */
160 children
.append(new ProjectFiles(settings
, this));
164 ProjectRoot::~ProjectRoot()
166 for(int i
= 0; i
< children
.count(); i
++)