File: ModelObserver.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 261,632 kB
  • sloc: cpp: 650,836; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 17
file content (108 lines) | stat: -rw-r--r-- 3,600 bytes parent folder | download | duplicates (5)
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
107
108
/* This file is part of the KDE project

   Copyright 2009 Johannes Simon <johannes.simon@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "ModelObserver.h"
#include <QAbstractItemModel>
#include <QDebug>

ModelObserver::ModelObserver(QAbstractItemModel *source)
    : QObject()
{
    m_source = source;
    m_numRows = 0;
    m_numCols = 0;
    m_lastDataChange.valid = false;
    m_lastHeaderDataChange.valid = false;

    connect(source, SIGNAL(rowsInserted(QModelIndex,int,int)),
            this  , SLOT(slotRowsInserted(QModelIndex,int,int)));
    connect(source, SIGNAL(columnsInserted(QModelIndex,int,int)),
            this,   SLOT(slotColumnsInserted(QModelIndex,int,int)));
    connect(source, SIGNAL(rowsRemoved(QModelIndex,int,int)),
            this,   SLOT(slotRowsRemoved(QModelIndex,int,int)));
    connect(source, SIGNAL(columnsRemoved(QModelIndex,int,int)),
            this,   SLOT(slotColumnsRemoved(QModelIndex,int,int)));
    connect(source, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
            this,   SLOT(slotHeaderDataChanged(Qt::Orientation,int,int)));
    connect(source, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
            this,   SLOT(slotDataChanged(QModelIndex,QModelIndex)));
    connect(source, SIGNAL(modelReset()),
            this,   SLOT(slotModelReset()));
}

void ModelObserver::slotRowsInserted(const QModelIndex & /*parent*/, int start, int end)
{
    Q_ASSERT(start <= end);

    m_numRows += end - start + 1;

    qDebug() << "m_numRows: " << m_numRows;
}

void ModelObserver::slotColumnsInserted(const QModelIndex & /*parent*/, int start, int end)
{
    Q_ASSERT(start <= end);

    m_numCols += end - start + 1;

    qDebug() << "m_numCols: " << m_numCols;
}

void ModelObserver::slotRowsRemoved(const QModelIndex & /*parent*/, int start, int end)
{
    Q_ASSERT(start <= end);
    Q_ASSERT(end < m_numRows);

    m_numRows -= end - start + 1;

    qDebug() << "m_numRows: " << m_numRows;
}

void ModelObserver::slotColumnsRemoved(const QModelIndex & /*parent*/, int start, int end)
{
    Q_ASSERT(start <= end);
    Q_ASSERT(end < m_numCols);

    m_numCols -= end - start + 1;

    qDebug() << "m_numCols: " << m_numCols;
}

void ModelObserver::slotModelReset()
{
    qDebug() << "TestModel was reset";
    m_numRows = m_source->rowCount();
    m_numCols = m_source->columnCount();
}

void ModelObserver::slotHeaderDataChanged(Qt::Orientation orientation, int first, int last)
{
    m_lastHeaderDataChange.orientation = orientation;
    m_lastHeaderDataChange.first = first;
    m_lastHeaderDataChange.last = last;
    m_lastHeaderDataChange.valid = true;
}

void ModelObserver::slotDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight)
{
    m_lastDataChange.topLeft = topLeft;
    m_lastDataChange.bottomRight = bottomRight;
    m_lastHeaderDataChange.valid = true;
}