Theme Editor: Got project viewer displaying WPS files
[kugel-rb.git] / utils / themeeditor / projectmodel.cpp
blobd3a338ea4ff09837e579347268df1babd6f01c34
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"
26 #include <QFile>
27 #include <QTextStream>
28 #include <QHash>
29 #include <QDir>
31 ProjectModel::ProjectModel(QString config, QObject *parent) :
32 QAbstractItemModel(parent)
34 root = new ProjectRoot(config);
37 ProjectModel::~ProjectModel()
39 if(root)
40 delete root;
43 QModelIndex ProjectModel::index(int row, int column,
44 const QModelIndex& parent) const
47 if(!hasIndex(row, column, parent))
48 return QModelIndex();
50 ProjectNode* foundParent = root;
51 if(parent.isValid())
52 foundParent = static_cast<ProjectNode*>(parent.internalPointer());
54 if(row < foundParent->numChildren() && row >= 0)
55 return createIndex(row, column, foundParent->child(row));
56 else
57 return QModelIndex();
60 QModelIndex ProjectModel::parent(const QModelIndex &child) const
62 if(!child.isValid())
63 return QModelIndex();
65 ProjectNode* foundParent = static_cast<ProjectNode*>
66 (child.internalPointer())->parent();
68 if(foundParent == root)
69 return QModelIndex();
71 return createIndex(foundParent->row(), 0, foundParent);
74 int ProjectModel::rowCount(const QModelIndex &parent) const
76 if(!root)
77 return 0;
79 if(!parent.isValid())
80 return root->numChildren();
82 if(parent.column() != 0)
83 return 0;
85 return static_cast<ProjectNode*>(parent.internalPointer())->numChildren();
88 int ProjectModel::columnCount(const QModelIndex &parent) const
90 return numColumns;
93 QVariant ProjectModel::data(const QModelIndex &index, int role) const
95 if(!index.isValid())
96 return QVariant();
98 if(role != Qt::DisplayRole)
99 return QVariant();
101 return static_cast<ProjectNode*>
102 (index.internalPointer())->data(index.column());
105 QVariant ProjectModel::headerData(int col, Qt::Orientation orientation,
106 int role) const
108 return QVariant();
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,
118 int role)
120 return true;
123 /* Constructor and destructor for the root class */
124 ProjectRoot::ProjectRoot(QString config)
126 /* Reading the config file */
127 QFile cfg(config);
128 cfg.open(QFile::ReadOnly | QFile::Text);
129 if(!cfg.isReadable())
130 return;
132 QTextStream fin(&cfg);
134 /* Storing the base directory */
135 QString confDir = config;
136 confDir.chop(confDir.length() - confDir.lastIndexOf('/') - 1);
137 QDir base(confDir);
138 base.cdUp();
139 settings.insert("themebase", base.canonicalPath());
141 while(!fin.atEnd())
143 QString current = fin.readLine();
144 QList<QString> parts = current.split(':');
146 /* A valid setting has at least one : */
147 if(parts.count() < 2)
148 continue;
150 QString setting;
151 for(int i = 1; i < parts.count(); i++)
152 setting.append(parts[i].trimmed());
154 settings.insert(parts[0].trimmed(), setting);
157 cfg.close();
159 /* Showing the files */
160 children.append(new ProjectFiles(settings, this));
164 ProjectRoot::~ProjectRoot()
166 for(int i = 0; i < children.count(); i++)
167 delete children[i];