Even though the last alpha was .91 this one is going to be 82 as I'd like to have...
[kdevelopdvcssupport.git] / plugins / projectmanagerview / projectbuildsetmodel.cpp
blob2da467965586bfde0714877f67bb2b802f450951
1 /***************************************************************************
2 * This file is part of KDevelop *
3 * Copyright 2007 Andreas Pakulat <apaku@gmx.de> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "projectbuildsetmodel.h"
23 #include <QVariant>
25 #include <kurl.h>
26 #include <klocale.h>
27 #include <kdebug.h>
28 #include <kconfiggroup.h>
30 #include <interfaces/icore.h>
31 #include <interfaces/iproject.h>
32 #include <interfaces/iprojectcontroller.h>
34 #include <project/projectmodel.h>
36 QString getRelativeFolder( KDevelop::ProjectBaseItem* item )
38 if( !item )
39 return "";
41 if( item->type() == KDevelop::ProjectBaseItem::Folder
42 || item->type() == KDevelop::ProjectBaseItem::BuildFolder )
45 return item->project()->relativeUrl( item->folder()->url() ).path();
46 }else
48 return getRelativeFolder( dynamic_cast<KDevelop::ProjectBaseItem*>( item->parent() ) );
52 KDevelop::ProjectBaseItem* findItem( const QString& item, const QString& path, KDevelop::ProjectBaseItem* top )
54 if( top && top->text() == item && getRelativeFolder( top ) == path )
56 return top;
57 }else if( top->hasChildren() )
59 for( int i = 0; i < top->rowCount(); i++ )
61 QStandardItem* sitem = top->child( i );
62 KDevelop::ProjectBaseItem* prjitem = dynamic_cast<KDevelop::ProjectBaseItem*>(sitem);
63 if( prjitem )
65 if( prjitem->file()
66 && prjitem->text() == item
67 && path == getRelativeFolder( prjitem->file() ) )
69 return prjitem;
70 }else if( prjitem->folder()
71 && prjitem->text() == item
72 && path == getRelativeFolder( prjitem->folder() ) )
74 return prjitem;
75 }else if( prjitem->target()
76 && prjitem->text() == item
77 && path == getRelativeFolder( prjitem->target() ) )
79 return prjitem;
80 }else
82 KDevelop::ProjectBaseItem* tmp = findItem( item, path, prjitem );
83 if( tmp )
84 return tmp;
89 return 0;
93 BuildItem::BuildItem()
97 BuildItem::BuildItem( const QString& itemName, const QString& projectName, const QString& itemPath )
98 : m_itemName( itemName ), m_projectName( projectName ), m_itemPath( itemPath )
102 BuildItem::BuildItem( KDevelop::ProjectBaseItem* item )
104 initializeFromItem( item );
107 BuildItem::BuildItem( const BuildItem& rhs )
109 m_itemName = rhs.itemName();
110 m_projectName = rhs.projectName();
111 m_itemPath = rhs.itemPath();
114 void BuildItem::initializeFromItem( KDevelop::ProjectBaseItem* item )
116 if( item )
118 m_itemName = item->text();
119 m_itemPath = getRelativeFolder( item );
120 m_projectName = item->project()->name();
124 KDevelop::ProjectBaseItem* BuildItem::findItem() const
126 KDevelop::ProjectBaseItem* top = 0;
127 KDevelop::IProject* project = KDevelop::ICore::self()->projectController()->findProjectByName( projectName() );
128 if( project )
130 top = ::findItem( itemName(), itemPath(), project->projectItem() );
132 return top;
135 bool operator==( const BuildItem& rhs, const BuildItem& lhs )
137 return( rhs.itemName() == lhs.itemName() && rhs.projectName() == lhs.projectName() && rhs.itemPath() == lhs.itemPath() );
140 BuildItem& BuildItem::operator=( const BuildItem& rhs )
142 if( this == &rhs )
143 return *this;
144 m_itemName = rhs.itemName();
145 m_projectName = rhs.projectName();
146 m_itemPath = rhs.itemPath();
147 return *this;
150 ProjectBuildSetModel::ProjectBuildSetModel( QObject* parent )
151 : QAbstractTableModel( parent )
155 QVariant ProjectBuildSetModel::data( const QModelIndex& idx, int role ) const
157 if( !idx.isValid() || idx.row() < 0 || idx.column() < 0
158 || idx.row() >= rowCount() || idx.column() >= columnCount()
159 || role != Qt::DisplayRole )
161 return QVariant();
163 switch( idx.column() )
165 case 0:
166 return m_items.at( idx.row() ).itemName();
167 break;
168 case 1:
169 return m_items.at( idx.row() ).projectName();
170 break;
171 case 2:
172 return m_items.at( idx.row() ).itemPath();
173 break;
175 return QVariant();
178 QVariant ProjectBuildSetModel::headerData( int section, Qt::Orientation orientation, int role ) const
180 if( section < 0 || section >= columnCount()
181 || orientation != Qt::Horizontal || role != Qt::DisplayRole )
182 return QVariant();
184 switch( section )
186 case 0:
187 return i18n("Name");
188 break;
189 case 1:
190 return i18n("Project");
191 break;
192 case 2:
193 return i18n("Folder");
194 break;
196 return QVariant();
199 int ProjectBuildSetModel::rowCount( const QModelIndex& parent ) const
201 if( parent.isValid() )
202 return 0;
203 return m_items.count();
206 int ProjectBuildSetModel::columnCount( const QModelIndex& parent ) const
208 if( parent.isValid() )
209 return 0;
210 return 3;
213 void ProjectBuildSetModel::addProjectItem( KDevelop::ProjectBaseItem* item )
215 if( m_items.contains( item ) )
216 return;
217 beginInsertRows( QModelIndex(), rowCount(), rowCount() );
218 m_items.append(BuildItem(item));
219 endInsertRows();
222 bool ProjectBuildSetModel::removeRows( int row, int count, const QModelIndex& parent )
224 if( parent.isValid() || row > rowCount() || row < 0 || (row+count) > rowCount() || count <= 0 )
225 return false;
227 beginRemoveRows( QModelIndex(), row, row+count-1 );
228 for( int i = row; i < row+count; i++ )
230 m_items.removeAt( row );
232 endRemoveRows();
233 return true;
236 KDevelop::ProjectBaseItem* ProjectBuildSetModel::itemForIndex( const QModelIndex& idx )
238 if( !idx.isValid() || idx.row() < 0 || idx.column() < 0
239 || idx.column() >= columnCount() || idx.row() >= rowCount() )
240 return 0;
241 return m_items.at( idx.row() ).findItem();
244 QList<BuildItem> ProjectBuildSetModel::items()
246 return m_items ;
249 void ProjectBuildSetModel::saveToProject( KDevelop::IProject* project ) const
251 KConfigGroup base = project->projectConfiguration()->group("Buildset");
252 int count = 0;
253 foreach( BuildItem item, m_items)
255 if( item.projectName() == project->name() )
257 KConfigGroup grp = base.group(QString("Builditem%1").arg(count));
258 grp.writeEntry("Projectname", item.projectName());
259 grp.writeEntry("Itemname", item.itemName());
260 grp.writeEntry("Itempath", item.itemPath());
261 count++;
264 base.writeEntry("Number of Builditems", count);
265 base.sync();
268 void ProjectBuildSetModel::loadFromProject( KDevelop::IProject* project )
270 KConfigGroup base = project->projectConfiguration()->group("Buildset");
271 int count = base.readEntry("Number of Builditems", 0);
272 for( int i = 0; i < count; i++ )
274 KConfigGroup grp = base.group(QString("Builditem%1").arg(i));
275 QString name = grp.readEntry("Projectname");
276 QString item = grp.readEntry("Itemname");
277 QString path = grp.readEntry("Itempath");
278 beginInsertRows( QModelIndex(), rowCount(), rowCount() );
279 m_items.append( BuildItem( item, name, path ) );
280 endInsertRows();