~zsombi/ubuntu-ui-toolkit/110-captions

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/plugin/uclistitem.cpp

  • Committer: Zsombor Egri
  • Date: 2014-12-09 06:29:55 UTC
  • mfrom: (1246.1.51 100-listlayout)
  • Revision ID: zsombor.egri@canonical.com-20141209062955-zoskh1aezfqgurvz
prereq sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include <QtQuick/private/qquickanimation_p.h>
36
36
#include <QtQuick/private/qquickmousearea_p.h>
37
37
#include "uclistitemstyle.h"
 
38
#include "listener.h"
38
39
 
39
40
#define MIN(x, y)           ((x < y) ? x : y)
40
41
#define MAX(x, y)           ((x > y) ? x : y)
337
338
    , swiped(false)
338
339
    , suppressClick(false)
339
340
    , ready(false)
340
 
    , selectable(false)
341
341
    , selected(false)
342
342
    , customStyle(false)
343
343
    , customColor(false)
418
418
    loadStyle(true);
419
419
}
420
420
 
 
421
// returns true if the ListItem is in selectable mode, false otherwise (also if the
 
422
// attached property is NULL)
 
423
bool UCListItemPrivate::isSelectable()
 
424
{
 
425
    UCListItemAttachedPrivate *attached = UCListItemAttachedPrivate::get(attachedProperties);
 
426
    return attached ? attached->isSelectable() : false;
 
427
}
 
428
 
 
429
void UCListItemPrivate::_q_selectableUpdated()
 
430
{
 
431
    // make sure the selection mode panel is prepared; selection panel must take care of the visuals
 
432
    bool selectable = isSelectable();
 
433
    if (selectable) {
 
434
        promptRebound();
 
435
    }
 
436
    setupSelectionMode();
 
437
    // disable contentIten from handling events
 
438
    contentItem->setEnabled(!selectable);
 
439
    // and finaly update visuals
 
440
    update();
 
441
}
 
442
 
421
443
void UCListItemPrivate::_q_rebound()
422
444
{
423
445
    setPressed(false);
583
605
        this->pressed = pressed;
584
606
        Q_Q(UCListItem);
585
607
        q->update();
586
 
        if (pressed && !selectable) {
 
608
        if (pressed && !isSelectable()) {
587
609
            // start pressandhold timer
588
610
            pressAndHoldTimer.start(QGuiApplication::styleHints()->mousePressAndHoldInterval(), q);
589
611
        } else {
675
697
        initStyleItem();
676
698
        if (styleItem && styleItem->m_selectionDelegate) {
677
699
            if (!styleItem->m_selectionDelegate->isError()) {
 
700
                // create a new context so we can expose context properties
 
701
                QQmlContext *context = new QQmlContext(qmlContext(q), q);
 
702
                context->setContextProperty("inSelectionMode", QVariant(false));
 
703
                ContextPropertyChangeListener *listener = new ContextPropertyChangeListener(
 
704
                            context, "inSelectionMode");
 
705
                listener->setUpdaterProperty(attachedProperties, "selectable");
 
706
 
678
707
                selectionPanel = qobject_cast<QQuickItem*>(
679
 
                            styleItem->m_selectionDelegate->beginCreate(qmlContext(q)));
 
708
                            styleItem->m_selectionDelegate->beginCreate(context));
680
709
                if (selectionPanel) {
681
710
                    QQml_setParent_noEvent(selectionPanel, q);
682
711
                    selectionPanel->setParentItem(q);
683
712
                    // complete component creation
684
713
                    styleItem->m_selectionDelegate->completeCreate();
 
714
                    // turn on selectable
 
715
                    context->setContextProperty("inSelectionMode", isSelectable());
685
716
                }
686
717
            } else {
687
718
                qmlInfo(q) << styleItem->m_selectionDelegate->errorString();
696
727
        return;
697
728
    }
698
729
    Q_Q(UCListItem);
699
 
    if (selectable) {
 
730
    if (isSelectable()) {
700
731
        // sync selected flag with the attached selection array
701
 
        if (attachedProperties) {
702
 
            q->setSelected(UCListItemAttachedPrivate::get(attachedProperties)->isItemSelected(q));
703
 
        }
 
732
        q->setSelected(UCListItemAttachedPrivate::get(attachedProperties)->isItemSelected(q));
704
733
    }
705
734
}
706
735
 
910
939
        setSelected(UCListItemAttachedPrivate::get(d->attachedProperties)->isItemSelected(this));
911
940
    }
912
941
    // toggle selection mode if has been set by a binding
913
 
    if (d->selectable) {
 
942
    if (d->isSelectable()) {
914
943
        d->setupSelectionMode();
915
944
        update();
916
945
    }
945
974
            d->attachedProperties = 0;
946
975
        }
947
976
 
 
977
        // get notified about selectable change
 
978
        if (d->attachedProperties) {
 
979
            QObject::connect(d->attachedProperties, SIGNAL(selectableChanged()),
 
980
                             this, SLOT(_q_selectableUpdated()));
 
981
        }
 
982
 
948
983
        if (data.item) {
949
984
            QObject::connect(d->flickable ? d->flickable : data.item, SIGNAL(widthChanged()), this, SLOT(_q_updateSize()));
950
985
        }
969
1004
    Q_D(UCListItem);
970
1005
    // do highlight only if at least one of action, leadingActions or trailingAction
971
1006
    // properties is set
972
 
    QColor color = (d->pressed || (d->selectable && d->selected))? d->highlightColor : d->color;
 
1007
    QColor color = (d->pressed || (d->isSelectable() && d->selected))? d->highlightColor : d->color;
973
1008
 
974
1009
    if (width() <= 0 || height() <= 0) {
975
1010
        delete oldNode;
1030
1065
        d->lastPos = d->pressedPos = event->localPos();
1031
1066
        // connect the Flickable to know when to rebound
1032
1067
        d->listenToRebind(true);
1033
 
        if (!d->selectable) {
 
1068
        if (!d->isSelectable()) {
1034
1069
            // if it was moved, grab the panels
1035
1070
            if (d->swiped) {
1036
1071
                d->grabPanel(d->leadingActions, true);
1053
1088
            d->attachedProperties->disableInteractive(this, false);
1054
1089
        }
1055
1090
 
1056
 
        if (d->selectable) {
 
1091
        if (d->isSelectable()) {
1057
1092
            // toggle selection
1058
1093
            setSelected(!d->selected);
1059
1094
        } else if (!d->suppressClick) {
1074
1109
    Q_D(UCListItem);
1075
1110
    UCStyledItemBase::mouseMoveEvent(event);
1076
1111
 
1077
 
    if (d->selectable) {
 
1112
    if (d->isSelectable()) {
1078
1113
        // no move is allowed while selectable mode is on
1079
1114
        return;
1080
1115
    }
1427
1462
}
1428
1463
 
1429
1464
/*!
1430
 
 * \qmlproperty bool ListItem::selectable
1431
 
 * The property drives whether a list item is selectable or not. When set, the item
1432
 
 * will show a check box on the leading side hanving the content item pushed towards
1433
 
 * trailing side and dimmed. The checkbox which will reflect and drive the \l selected
1434
 
 * state.
1435
 
 * Defaults to false.
1436
 
 */
1437
 
bool UCListItem::selectable() const
1438
 
{
1439
 
    Q_D(const UCListItem);
1440
 
    return d->selectable;
1441
 
}
1442
 
void UCListItem::setSelectable(bool selectable)
1443
 
{
1444
 
    Q_D(UCListItem);
1445
 
    if (d->selectable == selectable) {
1446
 
        return;
1447
 
    }
1448
 
    // make sure the selection mode panel is prepared; selection panel must take care of the visuals
1449
 
    if (selectable) {
1450
 
        d->promptRebound();
1451
 
    }
1452
 
    d->setupSelectionMode();
1453
 
    d->selectable = selectable;
1454
 
    // disable contentIten from handling events
1455
 
    d->contentItem->setEnabled(!d->selectable);
1456
 
    // and finaly update visuals
1457
 
    update();
1458
 
    Q_EMIT selectableChanged();
1459
 
}
1460
 
 
1461
 
/*!
1462
1465
 * \qmlproperty bool ListItem::selected
1463
1466
 * The property drives whether a list item is selected or not. While selected, the
1464
1467
 * ListItem is dimmed and cannot be tugged. The default value is false.
 
1468
 *
 
1469
 * \sa ListItem::selectable
1465
1470
 */
1466
1471
bool UCListItem::selected() const
1467
1472
{
1483
1488
            UCListItemAttachedPrivate::get(d->attachedProperties)->removeSelectedItem(this);
1484
1489
        }
1485
1490
    }
1486
 
    if (d->selectable) {
 
1491
    if (d->isSelectable()) {
1487
1492
        update();
1488
1493
    }
1489
1494
    Q_EMIT selectedChanged();