Example #1
0
void TestProjectModel::testItemSanity()
{
    ProjectBaseItem* parent = new ProjectBaseItem( nullptr, QStringLiteral("test") );
    ProjectBaseItem* child = new ProjectBaseItem( nullptr, QStringLiteral("test"), parent );
    ProjectBaseItem* child2 = new ProjectBaseItem( nullptr, QStringLiteral("ztest"), parent );
    ProjectFileItem* child3 = new ProjectFileItem( nullptr, Path(QUrl(QStringLiteral("file:///bcd"))), parent );
    ProjectFileItem* child4 = new ProjectFileItem(  nullptr, Path(QUrl(QStringLiteral("file:///abcd"))), parent  );

    // Just some basic santiy checks on the API
    QCOMPARE( parent->child( 0 ), child );
    QCOMPARE( parent->row(), -1 );
    QVERIFY( !parent->child( -1 ) );
    QVERIFY( !parent->file() );
    QVERIFY( !parent->folder() );
    QVERIFY( !parent->project() );
    QVERIFY( !parent->child( parent->rowCount() ) );
    QCOMPARE( parent->iconName(), QString() );
    QCOMPARE( parent->index(), QModelIndex() );

    QCOMPARE( child->type(), (int)ProjectBaseItem::BaseItem );

    QCOMPARE( child->lessThan( child2 ), true );
    QCOMPARE( child3->lessThan( child4 ), false );

    // Check that model is properly emitting data-changes
    model->appendRow( parent );
    QCOMPARE( parent->index(), model->index(0, 0, QModelIndex()) );
    QSignalSpy s( model, SIGNAL(dataChanged(QModelIndex,QModelIndex)) );
    parent->setPath( Path(QStringLiteral("/newtest")) );
    QCOMPARE( s.count(), 1 );
    QCOMPARE( model->data( parent->index() ).toString(), QStringLiteral("newtest") );

    parent->removeRow( child->row() );
}
Example #2
0
    /**
    * Find sub-project represented by @param item in the projects DOM document.
    * This will return the root-node (i.e. documentElement) if sub-project
    * is not found.
    * @param item - the ProjectFolderItem representing the sub-project
    * @param name - the value of the name attribute of the element to look for
    * @return a QDomElement representing @param item in the DOM document or the document element
    */
    QDomElement findElementInDocument( ProjectBaseItem *item, const QString &name )
    {
        QStack<QString> domPath;
        domPath.push( name );
        #if KDEV_PLUGIN_VERSION >= 10
        if (!item->parent()) {
            //this must be the project root
            return projectDomDocument.documentElement();
        }
        ProjectBaseItem *wItem = item;
        while ((wItem = wItem->parent())){
            if (wItem->folder())
                domPath.push( wItem->folder()->folderName() );
        };
        //remove last element, because it’s the root element, we don’t want to find that
        domPath.pop();
        #else
        // for older kdevplatform versions
        //TODO: can this be done without dynamic_cast?
        ProjectFolderItem *wItem = dynamic_cast<ProjectFolderItem*>( item->parent() );
        if (!wItem) {
             //this must be the project root
             return projectDomDocument.documentElement();
         }
        while (!wItem->isProjectRoot()){
            domPath.push( wItem->folderName() );
            //TODO: can this be done without dynamic_cast?
            wItem = dynamic_cast<ProjectFolderItem*>( wItem->parent() );
         };
        #endif

        QDomElement child = projectDomDocument.documentElement();
        while ( !domPath.isEmpty() ){
            QString nextFolder = domPath.pop();
            QDomElement d = child.firstChildElement("item");
            while (!d.isNull() && d.attribute("name") != nextFolder){
                if (d.attribute("name") == ""){
                    kWarning() << "no, this shouldn't happen";
                    return QDomElement();
                }
                d = d.nextSiblingElement("item");
            }
            child = d;
        }
        return child;
    }