File indexing completed on 2024-04-21 03:56:14

0001 /*
0002     This file is part of the proxy model test suite.
0003 
0004     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "lessthanwidget.h"
0010 
0011 #include <QLabel>
0012 #include <QTreeView>
0013 #include <QVBoxLayout>
0014 
0015 ColoredTreeModel::ColoredTreeModel(QObject *parent)
0016     : DynamicTreeModel(parent)
0017     , m_selectionModel(nullptr)
0018     , m_lessThanColour(Qt::yellow)
0019     , m_greaterThanColour(Qt::red)
0020 {
0021 }
0022 
0023 void ColoredTreeModel::setSelectionModel(QItemSelectionModel *selectionModel)
0024 {
0025     m_selectionModel = selectionModel;
0026     connect(selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(recolor()));
0027 }
0028 
0029 void ColoredTreeModel::recolor(const QModelIndex &parent)
0030 {
0031     const QModelIndex topLeft = index(0, 0, parent);
0032     const int _rowCount = rowCount(parent);
0033     const QModelIndex bottomRight = index(_rowCount - 1, columnCount(parent) - 1, parent);
0034     Q_EMIT dataChanged(topLeft, bottomRight);
0035 
0036     static const int column = 0;
0037     QModelIndex idx;
0038     for (int row = 0; row < _rowCount; ++row) {
0039         idx = index(row, column, parent);
0040         if (hasChildren(idx)) {
0041             recolor(idx);
0042         }
0043     }
0044 }
0045 
0046 QVariant ColoredTreeModel::data(const QModelIndex &index, int role) const
0047 {
0048     if (role != Qt::BackgroundRole || !m_selectionModel || m_selectionModel->selection().indexes().size() != 1) {
0049         return DynamicTreeModel::data(index, role);
0050     }
0051 
0052     const QModelIndex selectedIndex = m_selectionModel->selection().indexes().first();
0053 
0054     if (index == selectedIndex) {
0055         return DynamicTreeModel::data(index, role);
0056     }
0057 
0058     if (index < selectedIndex) {
0059         return m_lessThanColour;
0060     }
0061 
0062     Q_ASSERT(selectedIndex < index);
0063 
0064     return m_greaterThanColour;
0065 }
0066 
0067 void LessThanWidget::insertGrid(QList<int> address)
0068 {
0069     ModelInsertCommand *ins = new ModelInsertCommand(m_coloredTreeModel, this);
0070     ins->setAncestorRowNumbers(address);
0071     ins->setNumCols(5);
0072     ins->setStartRow(0);
0073     ins->setEndRow(5);
0074     ins->doCommand();
0075 }
0076 
0077 LessThanWidget::LessThanWidget(QWidget *parent, Qt::WindowFlags f)
0078     : QWidget(parent, f)
0079 {
0080     QLabel *explanation = new QLabel(this);
0081     explanation->setText(
0082         QLatin1String("The yellow items are 'less than' the selected item according to QModelIndex::operator<().\n"
0083                       "The red items are greater than the selected item (i.e, not less than and not equal)."));
0084 
0085     m_coloredTreeModel = new ColoredTreeModel(this);
0086     QTreeView *treeView = new QTreeView(this);
0087     treeView->setModel(m_coloredTreeModel);
0088     treeView->setSelectionBehavior(QAbstractItemView::SelectItems);
0089     treeView->setSelectionMode(QTreeView::SingleSelection);
0090 
0091     m_coloredTreeModel->setSelectionModel(treeView->selectionModel());
0092 
0093     QVBoxLayout *layout = new QVBoxLayout(this);
0094     layout->addWidget(explanation);
0095     layout->addWidget(treeView);
0096 
0097     insertGrid(QList<int>());
0098     insertGrid(QList<int>() << 2);
0099     insertGrid(QList<int>() << 3);
0100     insertGrid(QList<int>() << 4);
0101     insertGrid(QList<int>() << 3 << 2);
0102     insertGrid(QList<int>() << 3 << 3);
0103     insertGrid(QList<int>() << 3 << 4);
0104 }
0105 
0106 #include "moc_lessthanwidget.cpp"