| 1 |
/* |
| 2 |
* Copyright 2013 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Zsombor Egri <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "ullayouts_p.h" |
| 20 |
|
| 21 |
#include <QtQml/QQmlInfo> |
| 22 |
#include <QtQml/private/qqmlcomponentattached_p.h> |
| 23 |
|
| 24 |
ULLayoutsAttached::ULLayoutsAttached(QObject *parent) |
| 25 |
: QObject(parent) |
| 26 |
, m_valid(false) |
| 27 |
{ |
| 28 |
// workaround to get notified about attachee completion to check |
| 29 |
// whether the attachee belongs to a Layouts component or not |
| 30 |
// connect to one of the attached components to receive completion |
| 31 |
QQmlComponentAttached *component = QQmlComponent::qmlAttachedProperties(parent); |
| 32 |
if (component) { |
| 33 |
QObject::connect(component, SIGNAL(completed()), this, SLOT(validateAttachedProperties())); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/*! |
| 38 |
* \internal |
| 39 |
* Validates the attachee by checking whether its ascendant is a Layouts component or not. |
| 40 |
*/ |
| 41 |
void ULLayoutsAttached::validateAttachedProperties() |
| 42 |
{ |
| 43 |
QQuickItem *pl = qobject_cast<QQuickItem*>(parent()); |
| 44 |
while (pl) { |
| 45 |
if (qobject_cast<ULLayouts*>(pl)) { |
| 46 |
m_valid = !m_name.isEmpty(); |
| 47 |
return; |
| 48 |
} |
| 49 |
pl = pl->parentItem(); |
| 50 |
} |
| 51 |
ULLayoutsPrivate::error(parent(), |
| 52 |
"Item that is not a child of a Layouts component will not be laid out."); |
| 53 |
m_valid = false; |
| 54 |
} |
| 55 |
|
| 56 |
/*! |
| 57 |
* \qmlattachedproperty string Layouts::item |
| 58 |
* This attached property identifies a component to be laid out. It is the unique |
| 59 |
* identifier of the component within a layout block. The property can be attached |
| 60 |
* to components to be laid out and has no effect when applied on layout containers. |
| 61 |
* \sa ItemLayout |
| 62 |
*/ |
| 63 |
QString ULLayoutsAttached::item() const |
| 64 |
{ |
| 65 |
return m_name; |
| 66 |
} |
| 67 |
void ULLayoutsAttached::setItem(const QString &name) |
| 68 |
{ |
| 69 |
if (name == m_name) { |
| 70 |
return; |
| 71 |
} |
| 72 |
m_name = name; |
| 73 |
Q_EMIT itemChanged(); |
| 74 |
} |
| 75 |
|
| 76 |
bool ULLayoutsAttached::isValid() |
| 77 |
{ |
| 78 |
return m_valid; |
| 79 |
} |