Qt 4.8
qdeclarativestategroup.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativestategroup_p.h"
43 
44 #include "private/qdeclarativetransition_p.h"
45 #include "private/qdeclarativestate_p_p.h"
46 
47 #include <qdeclarativebinding_p.h>
48 #include <qdeclarativeglobal_p.h>
49 
50 #include <QtCore/qstringbuilder.h>
51 #include <QtCore/qdebug.h>
52 
53 #include <private/qobject_p.h>
54 #include <qdeclarativeinfo.h>
55 
57 
58 DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG);
59 
61 {
63 public:
65  : nullState(0), componentComplete(true),
66  ignoreTrans(false), applyingState(false), unnamedCount(0) {}
67 
70 
75 
80 
83 
88 
89  QDeclarativeTransition *findTransition(const QString &from, const QString &to);
90  void setCurrentStateInternal(const QString &state, bool = false);
91  bool updateAutoState();
92 };
93 
128  : QObject(*(new QDeclarativeStateGroupPrivate), parent)
129 {
130 }
131 
133 {
135  for (int i = 0; i < d->states.count(); ++i)
136  d->states.at(i)->setStateGroup(0);
137 }
138 
140 {
142  return d->states;
143 }
144 
169 {
175 }
176 
178 {
179  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
180  if (state) {
181  _this->d_func()->states.append(state);
182  state->setStateGroup(_this);
183  }
184 
185 }
186 
188 {
189  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
190  return _this->d_func()->states.count();
191 }
192 
194 {
195  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
196  return _this->d_func()->states.at(index);
197 }
198 
200 {
201  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
202  _this->d_func()->setCurrentStateInternal(QString(), true);
203  for (int i = 0; i < _this->d_func()->states.count(); ++i) {
204  _this->d_func()->states.at(i)->setStateGroup(0);
205  }
206  _this->d_func()->states.clear();
207 }
208 
233 {
239 }
240 
242 {
243  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
244  if (trans)
245  _this->d_func()->transitions.append(trans);
246 }
247 
249 {
250  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
251  return _this->d_func()->transitions.count();
252 }
253 
255 {
256  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
257  return _this->d_func()->transitions.at(index);
258 }
259 
261 {
262  QDeclarativeStateGroup *_this = static_cast<QDeclarativeStateGroup *>(list->object);
263  _this->d_func()->transitions.clear();
264 }
265 
293 {
295  return d->currentState;
296 }
297 
299 {
301  if (d->currentState == state)
302  return;
303 
304  d->setCurrentStateInternal(state);
305 }
306 
308 {
310  d->componentComplete = false;
311 }
312 
314 {
316  d->componentComplete = true;
317 
318  for (int ii = 0; ii < d->states.count(); ++ii) {
319  QDeclarativeState *state = d->states.at(ii);
320  if (!state->isNamed())
321  state->setName(QLatin1String("anonymousState") % QString::number(++d->unnamedCount));
322  }
323 
324  if (d->updateAutoState()) {
325  return;
326  } else if (!d->currentState.isEmpty()) {
327  QString cs = d->currentState;
328  d->currentState.clear();
329  d->setCurrentStateInternal(cs, true);
330  }
331 }
332 
337 {
339  return d->updateAutoState();
340 }
341 
343 {
345  if (!componentComplete)
346  return false;
347 
348  bool revert = false;
349  for (int ii = 0; ii < states.count(); ++ii) {
351  if (state->isWhenKnown()) {
352  if (state->isNamed()) {
353  if (state->when() && state->when()->evaluate().toBool()) {
354  if (stateChangeDebug())
355  qWarning() << "Setting auto state due to:"
356  << state->when()->expression();
357  if (currentState != state->name()) {
358  q->setState(state->name());
359  return true;
360  } else {
361  return false;
362  }
363  } else if (state->name() == currentState) {
364  revert = true;
365  }
366  }
367  }
368  }
369  if (revert) {
370  bool rv = !currentState.isEmpty();
371  q->setState(QString());
372  return rv;
373  } else {
374  return false;
375  }
376 }
377 
379 {
380  QDeclarativeTransition *highest = 0;
381  int score = 0;
382  bool reversed = false;
383  bool done = false;
384 
385  for (int ii = 0; !done && ii < transitions.count(); ++ii) {
387  for (int ii = 0; ii < 2; ++ii)
388  {
389  if (ii && (!t->reversible() ||
390  (t->fromState() == QLatin1String("*") &&
391  t->toState() == QLatin1String("*"))))
392  break;
393  QStringList fromState;
394  QStringList toState;
395 
396  fromState = t->fromState().split(QLatin1Char(','));
397  toState = t->toState().split(QLatin1Char(','));
398  if (ii == 1)
399  qSwap(fromState, toState);
400  int tScore = 0;
401  if (fromState.contains(from))
402  tScore += 2;
403  else if (fromState.contains(QLatin1String("*")))
404  tScore += 1;
405  else
406  continue;
407 
408  if (toState.contains(to))
409  tScore += 2;
410  else if (toState.contains(QLatin1String("*")))
411  tScore += 1;
412  else
413  continue;
414 
415  if (ii == 1)
416  reversed = true;
417  else
418  reversed = false;
419 
420  if (tScore == 4) {
421  highest = t;
422  done = true;
423  break;
424  } else if (tScore > score) {
425  score = tScore;
426  highest = t;
427  }
428  }
429  }
430 
431  if (highest)
432  highest->setReversed(reversed);
433 
434  return highest;
435 }
436 
438  bool ignoreTrans)
439 {
441  if (!componentComplete) {
442  currentState = state;
443  return;
444  }
445 
446  if (applyingState) {
447  qmlInfo(q) << "Can't apply a state change as part of a state definition.";
448  return;
449  }
450 
451  applyingState = true;
452 
453  QDeclarativeTransition *transition = (ignoreTrans || ignoreTrans) ? 0 : findTransition(currentState, state);
454  if (stateChangeDebug()) {
455  qWarning() << this << "Changing state. From" << currentState << ". To" << state;
456  if (transition)
457  qWarning() << " using transition" << transition->fromState()
458  << transition->toState();
459  }
460 
461  QDeclarativeState *oldState = 0;
462  if (!currentState.isEmpty()) {
463  for (int ii = 0; ii < states.count(); ++ii) {
464  if (states.at(ii)->name() == currentState) {
465  oldState = states.at(ii);
466  break;
467  }
468  }
469  }
470 
471  currentState = state;
472  emit q->stateChanged(currentState);
473 
474  QDeclarativeState *newState = 0;
475  for (int ii = 0; ii < states.count(); ++ii) {
476  if (states.at(ii)->name() == currentState) {
477  newState = states.at(ii);
478  break;
479  }
480  }
481 
482  if (oldState == 0 || newState == 0) {
483  if (!nullState) { nullState = new QDeclarativeState; QDeclarative_setParent_noEvent(nullState, q); }
484  if (!oldState) oldState = nullState;
485  if (!newState) newState = nullState;
486  }
487 
488  newState->apply(q, transition, oldState);
489  applyingState = false;
490  if (!transition)
491  static_cast<QDeclarativeStatePrivate*>(QObjectPrivate::get(newState))->complete();
492 }
493 
495 {
497  for (int i = 0; i < d->states.count(); ++i) {
498  QDeclarativeState *state = d->states.at(i);
499  if (state->name() == name)
500  return state;
501  }
502 
503  return 0;
504 }
505 
507 {
509  d->states.removeOne(state);
510 }
511 
513 
514 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
bool updateAutoState()
Returns true if the state was changed, otherwise false.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDeclarativeParserStatus ** d
void setCurrentStateInternal(const QString &state, bool=false)
static void clear_states(QDeclarativeListProperty< QDeclarativeState > *list)
static int count_state(QDeclarativeListProperty< QDeclarativeState > *list)
QList< QDeclarativeState * > states() const
QDeclarativeListProperty< QDeclarativeTransition > transitions
virtual void componentComplete()
Invoked after the root component that caused this instantiation has completed construction.
static void append_state(QDeclarativeListProperty< QDeclarativeState > *list, QDeclarativeState *state)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
QVariant evaluate(bool *valueIsUndefined=0)
Evaulates the expression, returning the result of the evaluation, or an invalid QVariant if the expre...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
static void append_transition(QDeclarativeListProperty< QDeclarativeTransition > *list, QDeclarativeTransition *state)
The QString class provides a Unicode character string.
Definition: qstring.h:83
QDeclarativeState * findState(const QString &name) const
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QDeclarativeListProperty< QDeclarativeState > statesProperty()
#define Q_D(Class)
Definition: qglobal.h:2482
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
static int count_transitions(QDeclarativeListProperty< QDeclarativeTransition > *list)
#define Q_Q(Class)
Definition: qglobal.h:2483
virtual void componentComplete()=0
Invoked after the root component that caused this instantiation has completed construction.
static void clear_transitions(QDeclarativeListProperty< QDeclarativeTransition > *list)
QDeclarativeBinding * when
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setState(const QString &)
Q_CORE_EXPORT void qWarning(const char *,...)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
void setName(const QString &)
void removeState(QDeclarativeState *state)
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
QDeclarativeListProperty< QDeclarativeTransition > transitionsProperty()
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QList< QDeclarativeTransition * > transitions
void QDeclarative_setParent_noEvent(QObject *object, QObject *parent)
Makes the object a child of parent.
QList< QDeclarativeState * > states
DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG)
virtual QString expression() const
static QDeclarativeTransition * at_transition(QDeclarativeListProperty< QDeclarativeTransition > *list, int index)
void apply(QDeclarativeStateGroup *, QDeclarativeTransition *, QDeclarativeState *revert)
quint16 index
QObject * parent
Definition: qobject.h:92
QString state() const
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
QDeclarativeListProperty< QDeclarativeState > states
QDeclarativeInfo qmlInfo(const QObject *me)
static QDeclarativeState * at_state(QDeclarativeListProperty< QDeclarativeState > *list, int index)
virtual void classBegin()
Invoked after class creation, but before any properties have been set.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QDeclarativeTransition * findTransition(const QString &from, const QString &to)
void setStateGroup(QDeclarativeStateGroup *)