File: lessthanwidget.cpp

package info (click to toggle)
kitemmodels 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,844 kB
  • sloc: cpp: 18,018; python: 26; sh: 13; makefile: 7
file content (106 lines) | stat: -rw-r--r-- 3,198 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
    This file is part of the proxy model test suite.

    SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>

    SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include "lessthanwidget.h"

#include <QLabel>
#include <QTreeView>
#include <QVBoxLayout>

ColoredTreeModel::ColoredTreeModel(QObject *parent)
    : DynamicTreeModel(parent)
    , m_selectionModel(nullptr)
    , m_lessThanColour(Qt::yellow)
    , m_greaterThanColour(Qt::red)
{
}

void ColoredTreeModel::setSelectionModel(QItemSelectionModel *selectionModel)
{
    m_selectionModel = selectionModel;
    connect(selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(recolor()));
}

void ColoredTreeModel::recolor(const QModelIndex &parent)
{
    const QModelIndex topLeft = index(0, 0, parent);
    const int _rowCount = rowCount(parent);
    const QModelIndex bottomRight = index(_rowCount - 1, columnCount(parent) - 1, parent);
    Q_EMIT dataChanged(topLeft, bottomRight);

    static const int column = 0;
    QModelIndex idx;
    for (int row = 0; row < _rowCount; ++row) {
        idx = index(row, column, parent);
        if (hasChildren(idx)) {
            recolor(idx);
        }
    }
}

QVariant ColoredTreeModel::data(const QModelIndex &index, int role) const
{
    if (role != Qt::BackgroundRole || !m_selectionModel || m_selectionModel->selection().indexes().size() != 1) {
        return DynamicTreeModel::data(index, role);
    }

    const QModelIndex selectedIndex = m_selectionModel->selection().indexes().first();

    if (index == selectedIndex) {
        return DynamicTreeModel::data(index, role);
    }

    if (index < selectedIndex) {
        return m_lessThanColour;
    }

    Q_ASSERT(selectedIndex < index);

    return m_greaterThanColour;
}

void LessThanWidget::insertGrid(QList<int> address)
{
    ModelInsertCommand *ins = new ModelInsertCommand(m_coloredTreeModel, this);
    ins->setAncestorRowNumbers(address);
    ins->setNumCols(5);
    ins->setStartRow(0);
    ins->setEndRow(5);
    ins->doCommand();
}

LessThanWidget::LessThanWidget(QWidget *parent, Qt::WindowFlags f)
    : QWidget(parent, f)
{
    QLabel *explanation = new QLabel(this);
    explanation->setText(
        QLatin1String("The yellow items are 'less than' the selected item according to QModelIndex::operator<().\n"
                      "The red items are greater than the selected item (i.e, not less than and not equal)."));

    m_coloredTreeModel = new ColoredTreeModel(this);
    QTreeView *treeView = new QTreeView(this);
    treeView->setModel(m_coloredTreeModel);
    treeView->setSelectionBehavior(QAbstractItemView::SelectItems);
    treeView->setSelectionMode(QTreeView::SingleSelection);

    m_coloredTreeModel->setSelectionModel(treeView->selectionModel());

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(explanation);
    layout->addWidget(treeView);

    insertGrid(QList<int>());
    insertGrid(QList<int>() << 2);
    insertGrid(QList<int>() << 3);
    insertGrid(QList<int>() << 4);
    insertGrid(QList<int>() << 3 << 2);
    insertGrid(QList<int>() << 3 << 3);
    insertGrid(QList<int>() << 3 << 4);
}

#include "moc_lessthanwidget.cpp"