~zsombi/ubuntu-ui-toolkit/colorSelector

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/plugin/ucstyleditembase.cpp

  • Committer: Zsombor Egri
  • Date: 2016-03-09 12:27:26 UTC
  • Revision ID: zsombor.egri@canonical.com-20160309122726-5piwa057h1t10ml6
introduce PropertyBinding

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    : oldParentItem(Q_NULLPTR)
30
30
    , styleComponent(Q_NULLPTR)
31
31
    , styleItem(Q_NULLPTR)
 
32
    , highlighted(Q_NULLPTR)
 
33
    , focused(Q_NULLPTR)
 
34
    , selected(Q_NULLPTR)
 
35
    , componentState(UCStyledItemBase::Undefined)
32
36
    , styleVersion(0)
33
37
    , keyNavigationFocus(false)
34
38
    , activeFocusOnPress(false)
62
66
    QObject::connect(q, &QQuickItem::activeFocusOnTabChanged, q, &UCStyledItemBase::activeFocusOnTabChanged2);
63
67
}
64
68
 
 
69
// initializes state builders
 
70
void UCStyledItemBasePrivate::setupHighlightedStateBinging()
 
71
{
 
72
    Q_Q(UCStyledItemBase);
 
73
    const QMetaObject *mo = q->metaObject();
 
74
    // detect highlight setup
 
75
    if (mo->indexOfProperty("pressed") > 0) {
 
76
        QMetaMethod change = mo->method(mo->indexOfSignal("pressedChanged()"));
 
77
        auto getter = [=] ()->bool {
 
78
            return QQmlProperty::read(q, "pressed", qmlContext(q)).toBool();
 
79
        };
 
80
        highlighted = new UbuntuToolkit::PropertyBinding<bool>(
 
81
                    getter,
 
82
                    std::bind(&UCStyledItemBase::componentStateChanged, q),
 
83
                    {UbuntuToolkit::Binding(q, change)});
 
84
    } else if (mo->indexOfProperty("highlighted") > 0) {
 
85
        QMetaMethod change = mo->method(mo->indexOfSignal("highlightedChanged()"));
 
86
        auto getter = [=] ()->bool {
 
87
            return QQmlProperty::read(q, "highlighted", qmlContext(q)).toBool();
 
88
        };
 
89
        highlighted = new UbuntuToolkit::PropertyBinding<bool>(
 
90
                    getter,
 
91
                    std::bind(&UCStyledItemBase::componentStateChanged, q),
 
92
                    {UbuntuToolkit::Binding(q, change)});
 
93
    }
 
94
}
 
95
 
 
96
void UCStyledItemBasePrivate::setupFocusedStateBinging()
 
97
{
 
98
    Q_Q(UCStyledItemBase);
 
99
    auto getter = [=] () -> bool {
 
100
        return q->window() && q->window()->activeFocusItem() == q;
 
101
    };
 
102
 
 
103
    const QMetaObject *mo = q->metaObject();
 
104
    focused = new UbuntuToolkit::PropertyBinding<bool> (
 
105
                getter,
 
106
                std::bind(&UCStyledItemBase::componentComplete, q),
 
107
                {
 
108
                    UbuntuToolkit::Binding(q, mo->method(mo->indexOfMethod("windowChanged(QQuickWindow*)")))
 
109
                }
 
110
                );
 
111
}
 
112
 
 
113
void UCStyledItemBasePrivate::setupSelectedStateBinging()
 
114
{
 
115
 
 
116
}
 
117
 
65
118
 
66
119
void UCStyledItemBasePrivate::setFocusable(bool focus)
67
120
{
480
533
    d->loadStyleItem();
481
534
}
482
535
 
 
536
/*!
 
537
 * \qmlproperty ComponentState StyledItem::componentState
 
538
 * \readonly
 
539
 * \since Ubuntu.Components 1.3
 
540
 * The property reports the current state of the component.
 
541
 */
 
542
UCStyledItemBase::ComponentState UCStyledItemBase::componentState()
 
543
{
 
544
    Q_D(const UCStyledItemBase);
 
545
    if (isEnabled()) {
 
546
        if (d->highlighted && d->highlighted->value()) {
 
547
            return Highlighted;
 
548
        }
 
549
        if (d->focused && d->focused->value()) {
 
550
            return Focused;
 
551
        }
 
552
        if (d->selected && d->selected->value()) {
 
553
            return Selected;
 
554
        }
 
555
        return Normal;
 
556
    } else if (d->selected && d->selected->value()) {
 
557
        return SelectedDisabled;
 
558
    } else {
 
559
        return Disabled;
 
560
    }
 
561
 
 
562
    return Undefined;
 
563
}
 
564
 
483
565
QString UCStyledItemBasePrivate::propertyForVersion(quint16 version) const
484
566
{
485
567
    switch (MINOR_VERSION(version)) {
510
592
     * The member defaults to true. Additionally, the focus scope flag must
511
593
     * be set before the parentItem is set and child items are added.
512
594
     */
513
 
    if (d_func()->isFocusScope) {
 
595
    Q_D(UCStyledItemBase);
 
596
    if (d->isFocusScope) {
514
597
        setFlag(QQuickItem::ItemIsFocusScope);
515
598
    }
516
599
    QQuickItem::classBegin();
 
600
 
 
601
    // build state transitioners
 
602
    d->setupHighlightedStateBinging();
 
603
    d->setupFocusedStateBinging();
 
604
    d->setupSelectedStateBinging();
517
605
}
518
606
 
519
607
void UCStyledItemBase::componentComplete()