~ubuntu-branches/ubuntu/trusty/grantlee/trusty

« back to all changes in this revision

Viewing changes to defaulttags/cycle.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee template system.
 
3
 
 
4
  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
 
5
 
 
6
  This library is free software; you can redistribute it and/or
 
7
  modify it under the terms of the GNU Lesser General Public
 
8
  License as published by the Free Software Foundation; either version
 
9
  2 of the Licence, or (at your option) any later version.
 
10
 
 
11
  This library is distributed in the hope that it will be useful,
 
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
  Library General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU Lesser General Public
 
17
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
*/
 
20
 
 
21
#include "cycle.h"
 
22
 
 
23
#include <QtCore/QStringList>
 
24
#include "parser.h"
 
25
#include "exception.h"
 
26
 
 
27
#include "util.h"
 
28
 
 
29
static const char * _namedCycleNodes = "_namedCycleNodes";
 
30
 
 
31
CycleNodeFactory::CycleNodeFactory()
 
32
{
 
33
 
 
34
}
 
35
 
 
36
Node* CycleNodeFactory::getNode( const QString &tagContent, Parser *p ) const
 
37
{
 
38
  QStringList expr = smartSplit( tagContent );
 
39
 
 
40
  if ( expr.size() < 1 ) {
 
41
    throw Grantlee::Exception( TagSyntaxError, QString( "%1 expects at least one argument" ).arg( expr.at( 0 ) ) );
 
42
  }
 
43
 
 
44
  if ( expr.at( 1 ).contains( ',' ) ) {
 
45
    QStringList csvlist = expr.at( 1 ).split( ',' );
 
46
    expr.removeAt( 1 );
 
47
    for ( int i = 0; i < csvlist.size() ; ++i ) {
 
48
      expr.insert( i + 1, '"' + csvlist.at( i ) + '"' );
 
49
    }
 
50
  }
 
51
 
 
52
  if ( expr.size() == 2 ) {
 
53
    // {% cycle var %}
 
54
    QString name = expr.at( 1 );
 
55
    QVariant cycleNodes = p->property( _namedCycleNodes );
 
56
    if ( !cycleNodes.isValid() || cycleNodes.type() != QVariant::Hash ) {
 
57
      throw Grantlee::Exception( TagSyntaxError, QString( "No named cycles in template. '%1' is not defined" ).arg( name ) );
 
58
    }
 
59
    QVariantHash hash = cycleNodes.toHash();
 
60
    if ( !hash.contains( name ) ) {
 
61
      throw Grantlee::Exception( TagSyntaxError, QString( "Node not found: %1" ).arg( name ) );
 
62
    }
 
63
    QVariant nodeVariant = hash.value( name );
 
64
    if ( nodeVariant.userType() != QMetaType::QObjectStar ) {
 
65
      throw Grantlee::Exception( TagSyntaxError, "Invalid object in node cycle list" );
 
66
    }
 
67
    QObject *obj = nodeVariant.value<QObject*>();
 
68
    Node *node = qobject_cast<Node*>( obj );
 
69
    if ( !node ) {
 
70
      throw Grantlee::Exception( TagSyntaxError, "Invalid object in node cycle list" );
 
71
    }
 
72
    return node;
 
73
  }
 
74
 
 
75
  int exprSize = expr.size();
 
76
  if ( exprSize > 4 && expr.at( exprSize - 2 ) == "as" ) {
 
77
    // {% cycle "foo" "bar" "bat" as var %}
 
78
    QString name = expr.at( exprSize - 1 );
 
79
    QStringList list = expr.mid( 1, exprSize - 3 );
 
80
    Node *node = new CycleNode( getFilterExpressionList( list, p ), name, p );
 
81
    QVariant hashVariant = p->property( _namedCycleNodes );
 
82
    QVariantHash hash;
 
83
    if ( hashVariant.isValid() && hashVariant.type() == QVariant::Hash ) {
 
84
      hash = hashVariant.toHash();
 
85
    }
 
86
    QObject *nodeObject = node;
 
87
    QVariant nodeVariant = QVariant::fromValue( nodeObject );
 
88
    hash.insert( name, nodeVariant );
 
89
    p->setProperty( _namedCycleNodes, QVariant( hash ) );
 
90
    return node;
 
91
  } else {
 
92
    QStringList list = expr.mid( 1, exprSize - 1 );
 
93
    return new CycleNode( getFilterExpressionList( list, p ), QString(), p );
 
94
  }
 
95
}
 
96
 
 
97
CycleNode::CycleNode( QList<FilterExpression> list, const QString &name, QObject *parent )
 
98
    : Node( parent ), m_variableIterator( list )
 
99
{
 
100
  m_name = name;
 
101
}
 
102
 
 
103
void CycleNode::render( OutputStream *stream, Context *c )
 
104
{
 
105
  QString value;
 
106
  QTextStream textStream( &value );
 
107
  QSharedPointer<OutputStream> temp = stream->clone( &textStream );
 
108
 
 
109
  m_variableIterator.next().resolve( temp.data(), c );
 
110
  if ( !m_name.isEmpty() ) {
 
111
    c->insert( m_name, value );
 
112
  }
 
113
  ( *stream ) << value;
 
114
}
 
115