src/declarative/util/qdeclarativelistmodelworkeragent.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "private/qdeclarativelistmodelworkeragent_p.h"
       
    43 #include "private/qdeclarativelistmodel_p_p.h"
       
    44 #include "private/qdeclarativedata_p.h"
       
    45 #include "private/qdeclarativeengine_p.h"
       
    46 #include "qdeclarativeinfo.h"
       
    47 
       
    48 #include <QtCore/qcoreevent.h>
       
    49 #include <QtCore/qcoreapplication.h>
       
    50 #include <QtCore/qdebug.h>
       
    51 
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 
       
    56 void QDeclarativeListModelWorkerAgent::Data::clearChange() 
       
    57 { 
       
    58     changes.clear(); 
       
    59 }
       
    60 
       
    61 void QDeclarativeListModelWorkerAgent::Data::insertChange(int index, int count) 
       
    62 {
       
    63     Change c = { Change::Inserted, index, count, 0 };
       
    64     changes << c;
       
    65 }
       
    66 
       
    67 void QDeclarativeListModelWorkerAgent::Data::removeChange(int index, int count) 
       
    68 {
       
    69     Change c = { Change::Removed, index, count, 0 };
       
    70     changes << c;
       
    71 }
       
    72 
       
    73 void QDeclarativeListModelWorkerAgent::Data::moveChange(int index, int count, int to)
       
    74 {
       
    75     Change c = { Change::Moved, index, count, to };
       
    76     changes << c;
       
    77 }
       
    78 
       
    79 void QDeclarativeListModelWorkerAgent::Data::changedChange(int index, int count)
       
    80 {
       
    81     Change c = { Change::Changed, index, count, 0 };
       
    82     changes << c;
       
    83 }
       
    84 
       
    85 QDeclarativeListModelWorkerAgent::QDeclarativeListModelWorkerAgent(QDeclarativeListModel *model)
       
    86 : m_engine(0), m_ref(1), m_orig(model), m_copy(new QDeclarativeListModel(true, this))
       
    87 {
       
    88     m_copy->m_flat->m_roles = m_orig->m_flat->m_roles;
       
    89     m_copy->m_flat->m_strings = m_orig->m_flat->m_strings;
       
    90     m_copy->m_flat->m_values = m_orig->m_flat->m_values;
       
    91 }
       
    92 
       
    93 QDeclarativeListModelWorkerAgent::~QDeclarativeListModelWorkerAgent()
       
    94 {
       
    95 }
       
    96 
       
    97 void QDeclarativeListModelWorkerAgent::setScriptEngine(QScriptEngine *eng)
       
    98 {
       
    99     m_engine = eng;
       
   100     if (m_copy->m_flat)
       
   101         m_copy->m_flat->m_scriptEngine = eng;
       
   102 }
       
   103 
       
   104 QScriptEngine *QDeclarativeListModelWorkerAgent::scriptEngine() const
       
   105 {
       
   106     return m_engine;
       
   107 }
       
   108 
       
   109 void QDeclarativeListModelWorkerAgent::addref()
       
   110 {
       
   111     m_ref.ref();
       
   112 }
       
   113 
       
   114 void QDeclarativeListModelWorkerAgent::release()
       
   115 {
       
   116     bool del = !m_ref.deref();
       
   117 
       
   118     if (del)
       
   119         delete this;
       
   120 }
       
   121 
       
   122 int QDeclarativeListModelWorkerAgent::count() const
       
   123 {
       
   124     return m_copy->count();
       
   125 }
       
   126 
       
   127 void QDeclarativeListModelWorkerAgent::clear()
       
   128 {
       
   129     data.clearChange();
       
   130     data.removeChange(0, m_copy->count());
       
   131     m_copy->clear();
       
   132 }
       
   133 
       
   134 void QDeclarativeListModelWorkerAgent::remove(int index)
       
   135 {
       
   136     int count = m_copy->count();
       
   137     m_copy->remove(index);
       
   138 
       
   139     if (m_copy->count() != count)
       
   140         data.removeChange(index, 1);
       
   141 }
       
   142 
       
   143 void QDeclarativeListModelWorkerAgent::append(const QScriptValue &value)
       
   144 {
       
   145     int count = m_copy->count();
       
   146     m_copy->append(value);
       
   147 
       
   148     if (m_copy->count() != count)
       
   149         data.insertChange(m_copy->count() - 1, 1);
       
   150 }
       
   151 
       
   152 void QDeclarativeListModelWorkerAgent::insert(int index, const QScriptValue &value)
       
   153 {
       
   154     int count = m_copy->count();
       
   155     m_copy->insert(index, value);
       
   156 
       
   157     if (m_copy->count() != count)
       
   158         data.insertChange(index, 1);
       
   159 }
       
   160 
       
   161 QScriptValue QDeclarativeListModelWorkerAgent::get(int index) const
       
   162 {
       
   163     return m_copy->get(index);
       
   164 }
       
   165 
       
   166 void QDeclarativeListModelWorkerAgent::set(int index, const QScriptValue &value)
       
   167 {
       
   168     m_copy->set(index, value);
       
   169     data.changedChange(index, 1);
       
   170 }
       
   171 
       
   172 void QDeclarativeListModelWorkerAgent::setProperty(int index, const QString& property, const QVariant& value)
       
   173 {
       
   174     m_copy->setProperty(index, property, value);
       
   175     data.changedChange(index, 1);
       
   176 }
       
   177 
       
   178 void QDeclarativeListModelWorkerAgent::move(int from, int to, int count)
       
   179 {
       
   180     m_copy->move(from, to, count);
       
   181     data.moveChange(from, to, count);
       
   182 }
       
   183 
       
   184 void QDeclarativeListModelWorkerAgent::sync()
       
   185 {
       
   186     Sync *s = new Sync;
       
   187     s->data = data;
       
   188     s->list = m_copy;
       
   189     data.changes.clear();
       
   190     QCoreApplication::postEvent(this, s);
       
   191 }
       
   192 
       
   193 bool QDeclarativeListModelWorkerAgent::event(QEvent *e)
       
   194 {
       
   195     if (e->type() == QEvent::User) {
       
   196         Sync *s = static_cast<Sync *>(e);
       
   197 
       
   198         const QList<Change> &changes = s->data.changes;
       
   199 
       
   200         if (m_copy) {
       
   201             bool cc = m_copy->count() != s->list->count();
       
   202 
       
   203             FlatListModel *orig = m_orig->m_flat;
       
   204             FlatListModel *copy = s->list->m_flat;
       
   205             if (!orig || !copy) 
       
   206                 return QObject::event(e);
       
   207             
       
   208             orig->m_roles = copy->m_roles;
       
   209             orig->m_strings = copy->m_strings;
       
   210             orig->m_values = copy->m_values;
       
   211 
       
   212             for (int ii = 0; ii < changes.count(); ++ii) {
       
   213                 const Change &change = changes.at(ii);
       
   214                 switch (change.type) {
       
   215                 case Change::Inserted:
       
   216                     emit m_orig->itemsInserted(change.index, change.count);
       
   217                     break;
       
   218                 case Change::Removed:
       
   219                     emit m_orig->itemsRemoved(change.index, change.count);
       
   220                     break;
       
   221                 case Change::Moved:
       
   222                     emit m_orig->itemsMoved(change.index, change.to, change.count);
       
   223                     break;
       
   224                 case Change::Changed:
       
   225                     emit m_orig->itemsMoved(change.index, change.to, change.count);
       
   226                     break;
       
   227                 }
       
   228             }
       
   229 
       
   230             if (cc)
       
   231                 emit m_orig->countChanged();
       
   232         }
       
   233     }
       
   234 
       
   235     return QObject::event(e);
       
   236 }
       
   237 
       
   238 QT_END_NAMESPACE
       
   239