1 /***************************************************************************
2 * This file is part of KDevelop *
3 * Copyright 2007 Andreas Pakulat <apaku@gmx.de> *
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. *
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. *
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"
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
)
41 if( item
->type() == KDevelop::ProjectBaseItem::Folder
42 || item
->type() == KDevelop::ProjectBaseItem::BuildFolder
)
45 return item
->project()->relativeUrl( item
->folder()->url() ).path();
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
)
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
);
66 && prjitem
->text() == item
67 && path
== getRelativeFolder( prjitem
->file() ) )
70 }else if( prjitem
->folder()
71 && prjitem
->text() == item
72 && path
== getRelativeFolder( prjitem
->folder() ) )
75 }else if( prjitem
->target()
76 && prjitem
->text() == item
77 && path
== getRelativeFolder( prjitem
->target() ) )
82 KDevelop::ProjectBaseItem
* tmp
= findItem( item
, path
, prjitem
);
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
)
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() );
130 top
= ::findItem( itemName(), itemPath(), project
->projectItem() );
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
)
144 m_itemName
= rhs
.itemName();
145 m_projectName
= rhs
.projectName();
146 m_itemPath
= rhs
.itemPath();
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
)
163 switch( idx
.column() )
166 return m_items
.at( idx
.row() ).itemName();
169 return m_items
.at( idx
.row() ).projectName();
172 return m_items
.at( idx
.row() ).itemPath();
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
)
190 return i18n("Project");
193 return i18n("Folder");
199 int ProjectBuildSetModel::rowCount( const QModelIndex
& parent
) const
201 if( parent
.isValid() )
203 return m_items
.count();
206 int ProjectBuildSetModel::columnCount( const QModelIndex
& parent
) const
208 if( parent
.isValid() )
213 void ProjectBuildSetModel::addProjectItem( KDevelop::ProjectBaseItem
* item
)
215 if( m_items
.contains( item
) )
217 beginInsertRows( QModelIndex(), rowCount(), rowCount() );
218 m_items
.append(BuildItem(item
));
222 bool ProjectBuildSetModel::removeRows( int row
, int count
, const QModelIndex
& parent
)
224 if( parent
.isValid() || row
> rowCount() || row
< 0 || (row
+count
) > rowCount() || count
<= 0 )
227 beginRemoveRows( QModelIndex(), row
, row
+count
-1 );
228 for( int i
= row
; i
< row
+count
; i
++ )
230 m_items
.removeAt( row
);
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() )
241 return m_items
.at( idx
.row() ).findItem();
244 QList
<BuildItem
> ProjectBuildSetModel::items()
249 void ProjectBuildSetModel::saveToProject( KDevelop::IProject
* project
) const
251 KConfigGroup base
= project
->projectConfiguration()->group("Buildset");
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());
264 base
.writeEntry("Number of Builditems", count
);
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
) );