~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/itemviews/qtablewidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the item views module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qtablewidget.h"
 
30
#include <qheaderview.h>
 
31
#include <qitemdelegate.h>
 
32
#include <qpainter.h>
 
33
#include <qabstractitemmodel.h>
 
34
#include <private/qabstractitemmodel_p.h>
 
35
#include <private/qtableview_p.h>
 
36
#include <private/qwidgetitemdata_p.h>
 
37
 
 
38
// workaround for VC++ 6.0 linker bug (?)
 
39
typedef bool(*LessThan)(const QTableWidgetItem *left, const QTableWidgetItem *right);
 
40
 
 
41
class QTableWidgetMimeData : public QMimeData
 
42
{
 
43
    Q_OBJECT
 
44
public:
 
45
    QList<QTableWidgetItem*> items;
 
46
};
 
47
 
 
48
class QTableModel : public QAbstractTableModel
 
49
{
 
50
    Q_OBJECT
 
51
public:
 
52
    QTableModel(int rows, int columns, QTableWidget *parent);
 
53
    ~QTableModel();
 
54
 
 
55
    bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex());
 
56
    bool insertColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex());
 
57
 
 
58
    bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex());
 
59
    bool removeColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex());
 
60
 
 
61
    void setItem(int row, int column, QTableWidgetItem *item);
 
62
    QTableWidgetItem *takeItem(int row, int column);
 
63
    QTableWidgetItem *item(int row, int column) const;
 
64
    QTableWidgetItem *item(const QModelIndex &index) const;
 
65
    void removeItem(QTableWidgetItem *item);
 
66
 
 
67
    void setHorizontalHeaderItem(int section, QTableWidgetItem *item);
 
68
    void setVerticalHeaderItem(int section, QTableWidgetItem *item);
 
69
    QTableWidgetItem *horizontalHeaderItem(int section);
 
70
    QTableWidgetItem *verticalHeaderItem(int section);
 
71
 
 
72
    QModelIndex index(const QTableWidgetItem *item) const;
 
73
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
 
74
 
 
75
    void setRowCount(int rows);
 
76
    void setColumnCount(int columns);
 
77
 
 
78
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
 
79
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
 
80
 
 
81
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
 
82
    bool setData(const QModelIndex &index, const QVariant &value, int role);
 
83
 
 
84
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
 
85
    bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role);
 
86
 
 
87
    Qt::ItemFlags flags(const QModelIndex &index) const;
 
88
 
 
89
    void sort(int column, Qt::SortOrder order);
 
90
    static bool itemLessThan(const QTableWidgetItem *left, const QTableWidgetItem *right);
 
91
    static bool itemGreaterThan(const QTableWidgetItem *left, const QTableWidgetItem *right);
 
92
 
 
93
    bool isValid(const QModelIndex &index) const;
 
94
    inline long tableIndex(int row, int column) const
 
95
        { return (row * horizontal.count()) + column; }
 
96
 
 
97
    void clear();
 
98
    void itemChanged(QTableWidgetItem *item);
 
99
 
 
100
    inline QTableWidgetItem *createItem() const
 
101
        { return prototype ? prototype->clone() : new QTableWidgetItem(); }
 
102
    inline const QTableWidgetItem *itemPrototype() const
 
103
        { return prototype; }
 
104
    inline void setItemPrototype(const QTableWidgetItem *item)
 
105
        { prototype = item; }
 
106
 
 
107
    // dnd
 
108
    QStringList mimeTypes() const;
 
109
    QMimeData *mimeData(const QModelIndexList &indexes) const;
 
110
    bool dropMimeData(const QMimeData *data, Qt::DropAction action,
 
111
                      int row, int column, const QModelIndex &parent);
 
112
    Qt::DropActions supportedDropActions() const;
 
113
 
 
114
    QMimeData *internalMimeData()  const;
 
115
private:
 
116
    const QTableWidgetItem *prototype;
 
117
    QVector<QTableWidgetItem*> table;
 
118
    QVector<QTableWidgetItem*> vertical;
 
119
    QVector<QTableWidgetItem*> horizontal;
 
120
    mutable QChar strbuf[65];
 
121
 
 
122
    // A cache must be mutable if get-functions should have const modifiers
 
123
    mutable QModelIndexList cachedIndexes;
 
124
 
 
125
};
 
126
 
 
127
#include "qtablewidget.moc"
 
128
 
 
129
QTableModel::QTableModel(int rows, int columns, QTableWidget *parent)
 
130
    : QAbstractTableModel(parent),
 
131
      prototype(0),
 
132
      table(rows * columns), vertical(rows), horizontal(columns)
 
133
{}
 
134
 
 
135
QTableModel::~QTableModel()
 
136
{
 
137
    clear();
 
138
}
 
139
 
 
140
bool QTableModel::insertRows(int row, int count, const QModelIndex &)
 
141
{
 
142
    if (row < 0)
 
143
        row = 0;
 
144
    else if (row > vertical.count())
 
145
        row = vertical.count();
 
146
    beginInsertRows(QModelIndex(), row, row + count - 1);
 
147
    int rc = vertical.count();
 
148
    int cc = horizontal.count();
 
149
    vertical.insert(row, count, 0);
 
150
    if (rc == 0)
 
151
        table.resize(cc * count);
 
152
    else
 
153
        table.insert(tableIndex(row, 0), cc * count, 0);
 
154
    endInsertRows();
 
155
    return true;
 
156
}
 
157
 
 
158
bool QTableModel::insertColumns(int column, int count, const QModelIndex &)
 
159
{
 
160
    if (column < 0)
 
161
        column = 0;
 
162
    else if (column > horizontal.count())
 
163
        column = horizontal.count();
 
164
    beginInsertColumns(QModelIndex(), column, column + count - 1);
 
165
    int rc = vertical.count();
 
166
    int cc = horizontal.count();
 
167
    horizontal.insert(column, count, 0);
 
168
    if (cc == 0)
 
169
        table.resize(rc * count);
 
170
    else
 
171
        for (int row = 0; row < rc; ++row)
 
172
            table.insert(tableIndex(row, column), count, 0);
 
173
    endInsertColumns();
 
174
    return true;
 
175
}
 
176
 
 
177
bool QTableModel::removeRows(int row, int count, const QModelIndex &)
 
178
{
 
179
    if (row >= 0 && row < vertical.count()) {
 
180
        beginRemoveRows(QModelIndex(), row, row + count - 1);
 
181
        int i = tableIndex(row, 0);
 
182
        int n = count * columnCount();
 
183
        QTableWidgetItem *oldItem = 0;
 
184
        for (int j=i; j<n+i; ++j) {
 
185
            oldItem = table.at(j);
 
186
            if (oldItem)
 
187
                oldItem->model = 0;
 
188
            delete oldItem;
 
189
        }
 
190
        table.remove(qMax(i, 0), n);
 
191
        for (int v=row; v<row+count; ++v) {
 
192
            oldItem = vertical.at(v);
 
193
            if (oldItem)
 
194
                oldItem->model = 0;
 
195
            delete oldItem;
 
196
        }
 
197
        vertical.remove(row, count);
 
198
        endRemoveRows();
 
199
        return true;
 
200
    }
 
201
    return false;
 
202
}
 
203
 
 
204
bool QTableModel::removeColumns(int column, int count, const QModelIndex &)
 
205
{
 
206
    if (column >= 0 && column < horizontal.count()) {
 
207
        beginRemoveColumns(QModelIndex(), column, column + count - 1);
 
208
        QTableWidgetItem *oldItem = 0;
 
209
        for (int row = rowCount() - 1; row >= 0; --row) {
 
210
            int i = tableIndex(row, column);
 
211
            for (int j=i; j<i+count; ++j) {
 
212
                oldItem = table.at(j);
 
213
                if (oldItem)
 
214
                    oldItem->model = 0;
 
215
                delete oldItem;
 
216
            }
 
217
            table.remove(i, count);
 
218
        }
 
219
        for (int h=column; h<column+count; ++h) {
 
220
            oldItem = horizontal.at(h);
 
221
            if (oldItem)
 
222
                oldItem->model = 0;
 
223
            delete oldItem;
 
224
        }
 
225
        horizontal.remove(column, count);
 
226
        endRemoveColumns();
 
227
        return true;
 
228
    }
 
229
    return false;
 
230
}
 
231
 
 
232
void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
 
233
{
 
234
    int i = tableIndex(row, column);
 
235
    if (i < 0 || i >= table.count())
 
236
        return;
 
237
    QTableWidgetItem *oldItem = table.at(i);
 
238
    if (item == oldItem)
 
239
        return;
 
240
 
 
241
    // remove old
 
242
    if (oldItem)
 
243
        oldItem->model = 0;
 
244
    delete table.at(i);
 
245
 
 
246
    // set new
 
247
    if (item)
 
248
        item->model = this;
 
249
    table[i] = item;
 
250
    QModelIndex idx = index(row, column);
 
251
    emit dataChanged(idx, idx);
 
252
}
 
253
 
 
254
QTableWidgetItem *QTableModel::takeItem(int row, int column)
 
255
{
 
256
    long i = tableIndex(row, column);
 
257
    QTableWidgetItem *itm = table.value(i);
 
258
    if (itm) {
 
259
        itm->model = 0;
 
260
        table[i] = 0;
 
261
    }
 
262
    return itm;
 
263
}
 
264
 
 
265
QTableWidgetItem *QTableModel::item(int row, int column) const
 
266
{
 
267
    return table.value(tableIndex(row, column));
 
268
}
 
269
 
 
270
QTableWidgetItem *QTableModel::item(const QModelIndex &index) const
 
271
{
 
272
    if (!isValid(index))
 
273
        return 0;
 
274
    return table.at(tableIndex(index.row(), index.column()));
 
275
}
 
276
 
 
277
void QTableModel::removeItem(QTableWidgetItem *item)
 
278
{
 
279
    int i = table.indexOf(item);
 
280
    if (i != -1) {
 
281
        table[i] = 0;
 
282
        QModelIndex idx = index(item);
 
283
        emit dataChanged(idx, idx);
 
284
        return;
 
285
    }
 
286
 
 
287
    i = vertical.indexOf(item);
 
288
 
 
289
    if (i != -1) {
 
290
        vertical[i] = 0;
 
291
        emit headerDataChanged(Qt::Vertical, i, i);
 
292
        return;
 
293
    }
 
294
    i = horizontal.indexOf(item);
 
295
    if (i != -1) {
 
296
        horizontal[i] = 0;
 
297
        emit headerDataChanged(Qt::Horizontal, i, i);
 
298
        return;
 
299
    }
 
300
}
 
301
 
 
302
void QTableModel::setHorizontalHeaderItem(int section, QTableWidgetItem *item)
 
303
{
 
304
    if (section < 0 || section >= horizontal.count())
 
305
        return;
 
306
    QTableWidgetItem *oldItem = horizontal.at(section);
 
307
    if (item == oldItem)
 
308
        return;
 
309
 
 
310
    if (oldItem)
 
311
        oldItem->model = 0;
 
312
    delete oldItem;
 
313
 
 
314
    if (item)
 
315
        item->model = this;
 
316
    horizontal[section] = item;
 
317
    emit headerDataChanged(Qt::Horizontal, section, section);
 
318
}
 
319
 
 
320
void QTableModel::setVerticalHeaderItem(int section, QTableWidgetItem *item)
 
321
{
 
322
    if (section < 0 || section >= vertical.count())
 
323
        return;
 
324
    QTableWidgetItem *oldItem = vertical.at(section);
 
325
    if (item == oldItem)
 
326
        return;
 
327
 
 
328
    if (oldItem)
 
329
        oldItem->model = 0;
 
330
    delete oldItem;
 
331
 
 
332
    if (item)
 
333
        item->model = this;
 
334
    vertical[section] = item;
 
335
    emit headerDataChanged(Qt::Vertical, section, section);
 
336
}
 
337
 
 
338
QTableWidgetItem *QTableModel::horizontalHeaderItem(int section)
 
339
{
 
340
    return horizontal.value(section);
 
341
}
 
342
 
 
343
QTableWidgetItem *QTableModel::verticalHeaderItem(int section)
 
344
{
 
345
    return vertical.value(section);
 
346
}
 
347
 
 
348
QModelIndex QTableModel::index(const QTableWidgetItem *item) const
 
349
{
 
350
    int i = table.indexOf(const_cast<QTableWidgetItem*>(item));
 
351
    int row = i / columnCount();
 
352
    int col = i % columnCount();
 
353
    return index(row, col);
 
354
}
 
355
 
 
356
QModelIndex QTableModel::index(int row, int column, const QModelIndex &parent) const
 
357
{
 
358
    if (hasIndex(row, column, parent)) {
 
359
        QTableWidgetItem *item = table.at(tableIndex(row, column));
 
360
        return createIndex(row, column, item);
 
361
    }
 
362
    return QModelIndex();
 
363
}
 
364
 
 
365
void QTableModel::setRowCount(int rows)
 
366
{
 
367
    int rc = vertical.count();
 
368
    if (rc == rows)
 
369
        return;
 
370
    if (rc < rows)
 
371
        insertRows(qMax(rc, 0), rows - rc);
 
372
    else
 
373
        removeRows(qMax(rows, 0), rc - rows);
 
374
}
 
375
 
 
376
void QTableModel::setColumnCount(int columns)
 
377
{
 
378
    int cc = horizontal.count();
 
379
    if (cc == columns)
 
380
        return;
 
381
    if (cc < columns)
 
382
        insertColumns(qMax(cc, 0), columns - cc);
 
383
    else
 
384
        removeColumns(qMax(columns, 0), cc - columns);
 
385
}
 
386
 
 
387
int QTableModel::rowCount(const QModelIndex &) const
 
388
{
 
389
    return vertical.count();
 
390
}
 
391
 
 
392
int QTableModel::columnCount(const QModelIndex &) const
 
393
{
 
394
    return horizontal.count();
 
395
}
 
396
 
 
397
QVariant QTableModel::data(const QModelIndex &index, int role) const
 
398
{
 
399
    QTableWidgetItem *itm = item(index);
 
400
    if (itm)
 
401
        return itm->data(role);
 
402
    return QVariant();
 
403
}
 
404
 
 
405
bool QTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
 
406
{
 
407
    QTableWidgetItem *itm = item(index);
 
408
 
 
409
    if (itm) {
 
410
        itm->setData(role, value);
 
411
        return true;
 
412
    }
 
413
 
 
414
    QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
 
415
    if (!view)
 
416
        return false;
 
417
 
 
418
    itm = createItem();
 
419
    itm->setData(role, value);
 
420
    view->setItem(index.row(), index.column(), itm);
 
421
    return true;
 
422
}
 
423
 
 
424
Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
 
425
{
 
426
    QTableWidgetItem *itm = item(index);
 
427
    if (itm)
 
428
        return itm->flags();
 
429
    return Qt::ItemIsEditable
 
430
        |Qt::ItemIsSelectable
 
431
        |Qt::ItemIsUserCheckable
 
432
        |Qt::ItemIsEnabled;
 
433
}
 
434
 
 
435
void QTableModel::sort(int column, Qt::SortOrder order)
 
436
{
 
437
    QVector<QTableWidgetItem*> sorting(rowCount());
 
438
    for (int i = 0; i < sorting.count(); ++i)
 
439
        sorting[i] = item(i, column);
 
440
    LessThan compare = order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan;
 
441
    qSort(sorting.begin(), sorting.end(), compare);
 
442
    for (int j = 0; j < sorting.count(); ++j)
 
443
        table[tableIndex(j, column)] = sorting.at(j);
 
444
}
 
445
 
 
446
bool QTableModel::itemLessThan(const QTableWidgetItem *left, const QTableWidgetItem *right)
 
447
{
 
448
    return *left < *right;
 
449
}
 
450
 
 
451
bool QTableModel::itemGreaterThan(const QTableWidgetItem *left, const QTableWidgetItem *right)
 
452
{
 
453
    return !(*left < *right);
 
454
}
 
455
 
 
456
QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int role) const
 
457
{
 
458
    QTableWidgetItem *itm = 0;
 
459
    if (section < 0)
 
460
        return QVariant();
 
461
    if (orientation == Qt::Horizontal && section < horizontal.count())
 
462
        itm = horizontal.at(section);
 
463
    else if (section < vertical.count())
 
464
        itm = vertical.at(section);
 
465
    if (itm)
 
466
        return itm->data(role);
 
467
    if (role == Qt::DisplayRole)
 
468
        return QVariant(section);
 
469
    if (role == Qt::TextAlignmentRole)
 
470
        return Qt::AlignVCenter;
 
471
    return QVariant();
 
472
}
 
473
 
 
474
bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
 
475
                                const QVariant &value, int role)
 
476
{
 
477
    QTableWidgetItem *itm = 0;
 
478
    if (orientation == Qt::Horizontal)
 
479
        itm = horizontal.at(section);
 
480
    else
 
481
        itm = vertical.at(section);
 
482
    if (itm) {
 
483
        itm->setData(role, value);
 
484
        return true;
 
485
    }
 
486
    return false;
 
487
}
 
488
 
 
489
bool QTableModel::isValid(const QModelIndex &index) const
 
490
{
 
491
    return index.isValid() && index.row() < vertical.count() && index.column() < horizontal.count();
 
492
}
 
493
 
 
494
void QTableModel::clear()
 
495
{
 
496
    for (int i = 0; i < table.count(); ++i) {
 
497
        if (table.at(i)) {
 
498
            table.at(i)->model = 0;
 
499
            delete table.at(i);
 
500
            table[i] = 0;
 
501
        }
 
502
    }
 
503
    for (int j = 0; j < vertical.count(); ++j) {
 
504
        if (vertical.at(j)) {
 
505
            vertical.at(j)->model = 0;
 
506
            delete vertical.at(j);
 
507
            vertical[j] = 0;
 
508
        }
 
509
    }
 
510
    for (int k = 0; k < horizontal.count(); ++k) {
 
511
        if (horizontal.at(k)) {
 
512
            horizontal.at(k)->model = 0;
 
513
            delete horizontal.at(k);
 
514
            horizontal[k] = 0;
 
515
        }
 
516
    }
 
517
    reset();
 
518
}
 
519
 
 
520
void QTableModel::itemChanged(QTableWidgetItem *item)
 
521
{
 
522
    QModelIndex idx = index(item);
 
523
    emit dataChanged(idx, idx);
 
524
}
 
525
 
 
526
QStringList QTableModel::mimeTypes() const
 
527
{
 
528
    const QTableWidget *view = ::qobject_cast<const QTableWidget*>(QObject::parent());
 
529
    return view->mimeTypes();
 
530
}
 
531
 
 
532
QMimeData *QTableModel::internalMimeData()  const
 
533
{
 
534
    return QAbstractItemModel::mimeData(cachedIndexes);
 
535
}
 
536
 
 
537
QMimeData *QTableModel::mimeData(const QModelIndexList &indexes) const
 
538
{
 
539
    QList<QTableWidgetItem*> items;
 
540
    for (int i = 0; i < indexes.count(); ++i)
 
541
        items << item(indexes.at(i));
 
542
    const QTableWidget *view = ::qobject_cast<const QTableWidget*>(QObject::parent());
 
543
 
 
544
    // cachedIndexes is a little hack to avoid copying from QModelIndexList to QList<QTreeWidgetItem*> and back again in the view
 
545
    cachedIndexes = indexes;
 
546
    QMimeData *mimeData = view->mimeData(items);
 
547
    cachedIndexes.clear();
 
548
    return mimeData;
 
549
}
 
550
 
 
551
bool QTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
 
552
                              int, int, const QModelIndex &index)
 
553
{
 
554
    QTableWidget *view = ::qobject_cast<QTableWidget*>(QObject::parent());
 
555
    return view->dropMimeData(index.row(), index.column(), data, action);
 
556
}
 
557
 
 
558
Qt::DropActions QTableModel::supportedDropActions() const
 
559
{
 
560
    const QTableWidget *view = ::qobject_cast<const QTableWidget*>(QObject::parent());
 
561
    return view->supportedDropActions();
 
562
}
 
563
 
 
564
/*!
 
565
  \class QTableWidgetSelectionRange
 
566
 
 
567
  \brief The QTableWidgetSelectionRange class provides a container for
 
568
  storing a selection range in a QTableWidget.
 
569
 
 
570
  \ingroup model-view
 
571
 
 
572
  The QTableWidgetSelectionRange class stores the top left and bottom
 
573
  right rows and columns of a selection range in a table. The
 
574
  selections in the table may consist of several selection ranges.
 
575
 
 
576
  \sa QTableWidget
 
577
*/
 
578
 
 
579
/*!
 
580
  Constructs the table selection range.
 
581
*/
 
582
QTableWidgetSelectionRange::QTableWidgetSelectionRange()
 
583
    : top(-1), left(-1), bottom(-1), right(-1)
 
584
{
 
585
}
 
586
 
 
587
/*!
 
588
  Constructs the table selection range from the given \a top, \a left, \a bottom and \a right table rows and columns.
 
589
*/
 
590
QTableWidgetSelectionRange::QTableWidgetSelectionRange(int top, int left, int bottom, int right)
 
591
    : top(top), left(left), bottom(bottom), right(right)
 
592
{
 
593
}
 
594
 
 
595
/*!
 
596
  Constructs a the table selection range by copying the given \a other table selection range.
 
597
*/
 
598
QTableWidgetSelectionRange::QTableWidgetSelectionRange(const QTableWidgetSelectionRange &other)
 
599
    : top(other.top), left(other.left), bottom(other.bottom), right(other.right)
 
600
{
 
601
}
 
602
 
 
603
/*!
 
604
  Destroys the table selection range.
 
605
*/
 
606
QTableWidgetSelectionRange::~QTableWidgetSelectionRange()
 
607
{
 
608
}
 
609
 
 
610
/*!
 
611
  \fn inline int QTableWidgetSelectionRange::topRow() const
 
612
 
 
613
  Returns the top row of the range.
 
614
*/
 
615
 
 
616
/*!
 
617
  \fn inline int QTableWidgetSelectionRange::bottomRow() const
 
618
 
 
619
  Returns the bottom row of the range.
 
620
*/
 
621
 
 
622
/*!
 
623
  \fn inline int QTableWidgetSelectionRange::leftColumn() const
 
624
 
 
625
  Returns the left column of the range.
 
626
*/
 
627
 
 
628
/*!
 
629
  \fn inline int QTableWidgetSelectionRange::rightColumn() const
 
630
 
 
631
  Returns the right column of the range.
 
632
*/
 
633
 
 
634
 
 
635
/*!
 
636
    \class QTableWidgetItem
 
637
    \brief The QTableWidgetItem class provides an item for use with the
 
638
    QTableWidget class.
 
639
 
 
640
    \ingroup model-view
 
641
 
 
642
    Table items are used to hold pieces of information for table widgets.
 
643
    Items usually contain text, icons, or checkboxes
 
644
 
 
645
    The QTableWidgetItem class is a convenience class that replaces the
 
646
    \c QTableItem class in Qt 3. It provides an item for use with
 
647
    the QTableWidget class.
 
648
 
 
649
    Items are usually constructed with a table widget as their parent then
 
650
    inserted at a particular position specified by row and column numbers:
 
651
 
 
652
    \quotefile snippets/qtablewidget-using/mainwindow.cpp
 
653
    \skipto QTableWidgetItem *newItem
 
654
    \printuntil tableWidget->setItem(
 
655
 
 
656
    Each item can have its own background color which is set with
 
657
    the setBackgroundColor() function. The current background color can be
 
658
    found with backgroundColor().
 
659
    The text label for each item can be rendered with its own font and text
 
660
    color. These are specified with the setFont() and setTextColor() functions,
 
661
    and read with font() and textColor().
 
662
 
 
663
    Items can be made checkable by setting the appropriate flag value with the
 
664
    setFlags() function. The current state of the item's flags can be read
 
665
    with flags().
 
666
 
 
667
    \sa QTableWidget
 
668
*/
 
669
 
 
670
/*!
 
671
    \fn Qt::CheckState QTableWidgetItem::checkState() const
 
672
 
 
673
    Returns the checked state of the list item (see \l{Qt::CheckState}).
 
674
 
 
675
    \sa flags()
 
676
*/
 
677
 
 
678
/*!
 
679
    \fn void QTableWidgetItem::setCheckState(Qt::CheckState state)
 
680
 
 
681
    Sets the check state of the table item to be \a state.
 
682
*/
 
683
 
 
684
/*!
 
685
    \fn QTableWidget *QTableWidgetItem::tableWidget() const
 
686
 
 
687
    Returns the table widget that contains the item.
 
688
*/
 
689
 
 
690
/*!
 
691
    \fn Qt::ItemFlags QTableWidgetItem::flags() const
 
692
 
 
693
    Returns the flags used to describe the item. These determine whether
 
694
    the item can be checked, edited, and selected.
 
695
 
 
696
    \sa setFlags()
 
697
*/
 
698
 
 
699
/*!
 
700
    \fn void QTableWidgetItem::setFlags(Qt::ItemFlags flags)
 
701
 
 
702
    Sets the flags for the item to the given \a flags. These determine whether
 
703
    the item can be selected or modified.
 
704
 
 
705
    \sa flags()
 
706
*/
 
707
 
 
708
/*!
 
709
    \fn QString QTableWidgetItem::text() const
 
710
 
 
711
    Returns the item's text.
 
712
 
 
713
    \sa setText()
 
714
*/
 
715
 
 
716
/*!
 
717
    \fn void QTableWidgetItem::setText(const QString &text)
 
718
 
 
719
    Sets the item's text to the \a text specified.
 
720
 
 
721
    \sa text() setFont() setTextColor()
 
722
*/
 
723
 
 
724
/*!
 
725
    \fn QIcon QTableWidgetItem::icon() const
 
726
 
 
727
    Returns the item's icon.
 
728
 
 
729
    \sa setIcon()
 
730
*/
 
731
 
 
732
/*!
 
733
    \fn void QTableWidgetItem::setIcon(const QIcon &icon)
 
734
 
 
735
    Sets the item's icon to the \a icon specified.
 
736
 
 
737
    \sa icon() setText()
 
738
*/
 
739
 
 
740
/*!
 
741
    \fn QString QTableWidgetItem::statusTip() const
 
742
 
 
743
    Returns the item's status tip.
 
744
 
 
745
    \sa setStatusTip()
 
746
*/
 
747
 
 
748
/*!
 
749
    \fn void QTableWidgetItem::setStatusTip(const QString &statusTip)
 
750
 
 
751
    Sets the item's status tip to the string specified by \a statusTip.
 
752
 
 
753
    \sa statusTip() setToolTip() setWhatsThis()
 
754
*/
 
755
 
 
756
/*!
 
757
    \fn QString QTableWidgetItem::toolTip() const
 
758
 
 
759
    Returns the item's tooltip.
 
760
 
 
761
    \sa setToolTip()
 
762
*/
 
763
 
 
764
/*!
 
765
    \fn void QTableWidgetItem::setToolTip(const QString &toolTip)
 
766
 
 
767
    Sets the item's tooltip to the string specified by \a toolTip.
 
768
 
 
769
    \sa toolTip() setStatusTip() setWhatsThis()
 
770
*/
 
771
 
 
772
/*!
 
773
    \fn QString QTableWidgetItem::whatsThis() const
 
774
 
 
775
    Returns the item's "What's This?" help.
 
776
 
 
777
    \sa setWhatsThis()
 
778
*/
 
779
 
 
780
/*!
 
781
    \fn void QTableWidgetItem::setWhatsThis(const QString &whatsThis)
 
782
 
 
783
    Sets the item's "What's This?" help to the string specified by \a whatsThis.
 
784
 
 
785
    \sa whatsThis() setStatusTip() setToolTip()
 
786
*/
 
787
 
 
788
/*!
 
789
    \fn QFont QTableWidgetItem::font() const
 
790
 
 
791
    Returns the font used to render the item's text.
 
792
 
 
793
    \sa setFont()
 
794
*/
 
795
 
 
796
/*!
 
797
    \fn void QTableWidgetItem::setFont(const QFont &font)
 
798
 
 
799
    Sets the font used to display the item's text to the given \a font.
 
800
 
 
801
    \sa font() setText() setTextColor()
 
802
*/
 
803
 
 
804
/*!
 
805
    \fn QColor QTableWidgetItem::backgroundColor() const
 
806
 
 
807
    Returns the color used to render the item's background.
 
808
 
 
809
    \sa textColor() setBackgroundColor()
 
810
*/
 
811
 
 
812
/*!
 
813
    \fn void QTableWidgetItem::setBackgroundColor(const QColor &color)
 
814
 
 
815
    Sets the item's background color to the specified \a color.
 
816
 
 
817
    \sa backgroundColor() setTextColor()
 
818
*/
 
819
 
 
820
/*!
 
821
    \fn QColor QTableWidgetItem::textColor() const
 
822
 
 
823
    Returns the color used to render the item's text.
 
824
 
 
825
    \sa backgroundColor() setTextColor()
 
826
*/
 
827
 
 
828
/*!
 
829
    \fn void QTableWidgetItem::setTextColor(const QColor &color)
 
830
 
 
831
    Sets the color used to display the item's text to the given \a color.
 
832
 
 
833
    \sa textColor() setFont() setText()
 
834
*/
 
835
 
 
836
/*!
 
837
  \fn int QTableWidgetItem::textAlignment() const
 
838
 
 
839
  Returns the text alignment for the item's text (see \l{Qt::AlignmentFlag}).
 
840
*/
 
841
 
 
842
/*!
 
843
  \fn void QTableWidgetItem::setTextAlignment(int alignment)
 
844
 
 
845
  Sets the text alignment for the item's text to the \a alignment
 
846
  specified (see \l{Qt::AlignmentFlag}).
 
847
*/
 
848
 
 
849
/*!
 
850
    Constructs a table item of the specified \a type that does not belong
 
851
    to any table.
 
852
 
 
853
    \sa type()
 
854
*/
 
855
QTableWidgetItem::QTableWidgetItem(int type)
 
856
    :  rtti(type), view(0), model(0),
 
857
      itemFlags(Qt::ItemIsEditable
 
858
                |Qt::ItemIsSelectable
 
859
                |Qt::ItemIsUserCheckable
 
860
                |Qt::ItemIsEnabled
 
861
                |Qt::ItemIsDragEnabled
 
862
                |Qt::ItemIsDropEnabled)
 
863
{
 
864
}
 
865
 
 
866
/*!
 
867
    Constructs a table item with the given \a text.
 
868
 
 
869
    \sa type()
 
870
*/
 
871
QTableWidgetItem::QTableWidgetItem(const QString &text, int type)
 
872
    :  rtti(type), view(0), model(0),
 
873
      itemFlags(Qt::ItemIsEditable
 
874
                |Qt::ItemIsSelectable
 
875
                |Qt::ItemIsUserCheckable
 
876
                |Qt::ItemIsEnabled
 
877
                |Qt::ItemIsDragEnabled
 
878
                |Qt::ItemIsDropEnabled)
 
879
{
 
880
    setData(Qt::DisplayRole, text);
 
881
}
 
882
 
 
883
/*!
 
884
    Destroys the table item.
 
885
*/
 
886
QTableWidgetItem::~QTableWidgetItem()
 
887
{
 
888
    if (model)
 
889
        model->removeItem(this);
 
890
}
 
891
 
 
892
/*!
 
893
  Creates an exact copy of the item.
 
894
*/
 
895
QTableWidgetItem *QTableWidgetItem::clone() const
 
896
{
 
897
    QTableWidgetItem *item = new QTableWidgetItem();
 
898
    *item = *this;
 
899
    return item;
 
900
}
 
901
 
 
902
/*!
 
903
    Sets the item's data for the given \a role to the specified \a value.
 
904
*/
 
905
void QTableWidgetItem::setData(int role, const QVariant &value)
 
906
{
 
907
    role = (role == Qt::EditRole ? Qt::DisplayRole : role);
 
908
    for (int i = 0; i < values.count(); ++i) {
 
909
        if (values.at(i).role == role) {
 
910
            values[i].value = value;
 
911
            break;
 
912
        }
 
913
    }
 
914
    values.append(QWidgetItemData(role, value));
 
915
    if (model)
 
916
        model->itemChanged(this);
 
917
}
 
918
 
 
919
/*!
 
920
    Returns the item's data for the given \a role.
 
921
*/
 
922
QVariant QTableWidgetItem::data(int role) const
 
923
{
 
924
    role = (role == Qt::EditRole ? Qt::DisplayRole : role);
 
925
    for (int i = 0; i < values.count(); ++i)
 
926
        if (values.at(i).role == role)
 
927
            return values.at(i).value;
 
928
    return QVariant();
 
929
}
 
930
 
 
931
/*!
 
932
    Returns true if the item is less than the \a other item; otherwise returns
 
933
    false.
 
934
*/
 
935
bool QTableWidgetItem::operator<(const QTableWidgetItem &other) const
 
936
{
 
937
    return text() < other.text();
 
938
}
 
939
 
 
940
#ifndef QT_NO_DATASTREAM
 
941
 
 
942
/*!
 
943
    Reads the item from stream \a in.
 
944
 
 
945
    \sa write()
 
946
*/
 
947
void QTableWidgetItem::read(QDataStream &in)
 
948
{
 
949
    in >> values;
 
950
}
 
951
 
 
952
/*!
 
953
    Writes the item to stream \a out.
 
954
 
 
955
    \sa read()
 
956
*/
 
957
void QTableWidgetItem::write(QDataStream &out) const
 
958
{
 
959
    out << values;
 
960
}
 
961
 
 
962
/*!
 
963
    \relates QTableWidgetItem
 
964
 
 
965
    Reads a table widget item from stream \a in into \a item.
 
966
 
 
967
    This operator uses QTableWidgetItem::read().
 
968
 
 
969
    \sa {Format of the QDataStream Operators}
 
970
*/
 
971
QDataStream &operator>>(QDataStream &in, QTableWidgetItem &item)
 
972
{
 
973
    item.read(in);
 
974
    return in;
 
975
}
 
976
 
 
977
/*!
 
978
    \relates QTableWidgetItem
 
979
 
 
980
    Writes the table widget item \a item to stream \a out.
 
981
 
 
982
    This operator uses QTableWidgetItem::write().
 
983
 
 
984
    \sa {Format of the QDataStream Operators}
 
985
*/
 
986
QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item)
 
987
{
 
988
    item.write(out);
 
989
    return out;
 
990
}
 
991
 
 
992
#endif // QT_NO_DATASTREAM
 
993
 
 
994
/*!
 
995
    Assigns \a other to this object.
 
996
*/
 
997
QTableWidgetItem &QTableWidgetItem::operator=(const QTableWidgetItem &other)
 
998
{
 
999
    values = other.values;
 
1000
    view = other.view;
 
1001
    model = other.model;
 
1002
    itemFlags = other.itemFlags;
 
1003
    return *this;
 
1004
}
 
1005
 
 
1006
/*!
 
1007
    \class QTableWidget
 
1008
    \brief The QTableWidget class provides an item-based table view with a default model.
 
1009
 
 
1010
    \ingroup model-view
 
1011
    \mainclass
 
1012
 
 
1013
    Table widgets provide standard table display facilities for applications.
 
1014
    The items in a QTableWidget are provided by QTableWidgetItem.
 
1015
 
 
1016
    If you want a table that uses your own data model you should
 
1017
    use QTableView rather than this class.
 
1018
 
 
1019
    Table widgets can be constructed with the required numbers of rows and
 
1020
    columns:
 
1021
 
 
1022
    \quotefile snippets/qtablewidget-using/mainwindow.cpp
 
1023
    \skipto tableWidget = new
 
1024
    \printuntil tableWidget = new
 
1025
 
 
1026
    Alternatively, tables can be constructed without a given size and resized
 
1027
    later:
 
1028
 
 
1029
    \quotefile snippets/qtablewidget-resizing/mainwindow.cpp
 
1030
    \skipto tableWidget = new
 
1031
    \printuntil tableWidget = new
 
1032
    \skipto tableWidget->setRowCount(
 
1033
    \printuntil tableWidget->setColumnCount(
 
1034
 
 
1035
    Items are created ouside the table (with no parent widget) and inserted
 
1036
    into the table with setItem():
 
1037
 
 
1038
    \skipto QTableWidgetItem *newItem
 
1039
    \printuntil tableWidget->setItem(
 
1040
 
 
1041
    Tables can be given both horizontal and vertical headers. The simplest way
 
1042
    to create the headers is to supply a list of strings to the
 
1043
    setHorizontalHeaderLabels() and setVerticalHeaderLabels() functions. These
 
1044
    will provide simple textual headers for the table's columns and rows.
 
1045
    More sophisticated headers can be created from existing table items
 
1046
    that are usually constructed outside the table. For example, we can
 
1047
    construct a table item with an icon and aligned text, and use it as the
 
1048
    header for a particular column:
 
1049
 
 
1050
    \quotefile snippets/qtablewidget-using/mainwindow.cpp
 
1051
    \skipto QTableWidgetItem *cubesHeaderItem
 
1052
    \printuntil cubesHeaderItem->setTextAlignment
 
1053
 
 
1054
    The number of rows in the table can be found with rowCount(), and the
 
1055
    number of columns with columnCount(). The table can be cleared with the
 
1056
    clear() function.
 
1057
 
 
1058
    \sa QTableWidgetItem \link model-view-programming.html Model/View Programming\endlink
 
1059
*/
 
1060
 
 
1061
/*!
 
1062
    \property QTableWidget::rowCount
 
1063
    \brief the number of rows in the table
 
1064
*/
 
1065
 
 
1066
/*!
 
1067
    \property QTableWidget::columnCount
 
1068
    \brief the number of columns in the table
 
1069
*/
 
1070
 
 
1071
/*!
 
1072
    \property QTableWidget::sortingEnabled
 
1073
    \brief whether the items in the table can be sorted
 
1074
    by clicking on the horizontal header.
 
1075
*/
 
1076
 
 
1077
class QTableWidgetPrivate : public QTableViewPrivate
 
1078
{
 
1079
    Q_DECLARE_PUBLIC(QTableWidget)
 
1080
public:
 
1081
    QTableWidgetPrivate() : QTableViewPrivate(), sortingEnabled(false) {}
 
1082
    inline QTableModel *model() const { return ::qobject_cast<QTableModel*>(q_func()->model()); }
 
1083
    void setup();
 
1084
    // view signals
 
1085
    void emitItemPressed(const QModelIndex &index);
 
1086
    void emitItemClicked(const QModelIndex &index);
 
1087
    void emitItemDoubleClicked(const QModelIndex &index);
 
1088
    void emitItemActivated(const QModelIndex &index);
 
1089
    void emitItemEntered(const QModelIndex &index);
 
1090
    // model signals
 
1091
    void emitItemChanged(const QModelIndex &index);
 
1092
    // selection signals
 
1093
    void emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current);
 
1094
    // data
 
1095
    bool sortingEnabled;
 
1096
};
 
1097
 
 
1098
void QTableWidgetPrivate::setup()
 
1099
{
 
1100
    Q_Q(QTableWidget);
 
1101
    // view signals
 
1102
    QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(emitItemPressed(QModelIndex)));
 
1103
    QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(emitItemClicked(QModelIndex)));
 
1104
    QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
 
1105
                     q, SLOT(emitItemDoubleClicked(QModelIndex)));
 
1106
    QObject::connect(q, SIGNAL(activated(QModelIndex)), q, SLOT(emitItemActivated(QModelIndex)));
 
1107
    QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(emitItemEntered(QModelIndex)));
 
1108
    // model signals
 
1109
    QObject::connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
 
1110
                     q, SLOT(emitItemChanged(QModelIndex)));
 
1111
    // selection signals
 
1112
    QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
 
1113
                     q, SLOT(emitCurrentItemChanged(QModelIndex,QModelIndex)));
 
1114
    QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
 
1115
                     q, SIGNAL(itemSelectionChanged()));
 
1116
}
 
1117
 
 
1118
void QTableWidgetPrivate::emitItemPressed(const QModelIndex &index)
 
1119
{
 
1120
    Q_Q(QTableWidget);
 
1121
    emit q->itemPressed(model()->item(index));
 
1122
}
 
1123
 
 
1124
void QTableWidgetPrivate::emitItemClicked(const QModelIndex &index)
 
1125
{
 
1126
    Q_Q(QTableWidget);
 
1127
    emit q->itemClicked(model()->item(index));
 
1128
}
 
1129
 
 
1130
void QTableWidgetPrivate::emitItemDoubleClicked(const QModelIndex &index)
 
1131
{
 
1132
    Q_Q(QTableWidget);
 
1133
    emit q->itemDoubleClicked(model()->item(index));
 
1134
}
 
1135
 
 
1136
void QTableWidgetPrivate::emitItemActivated(const QModelIndex &index)
 
1137
{
 
1138
    Q_Q(QTableWidget);
 
1139
    emit q->itemActivated(model()->item(index));
 
1140
}
 
1141
 
 
1142
void QTableWidgetPrivate::emitItemEntered(const QModelIndex &index)
 
1143
{
 
1144
    Q_Q(QTableWidget);
 
1145
    emit q->itemEntered(model()->item(index));
 
1146
}
 
1147
 
 
1148
void QTableWidgetPrivate::emitItemChanged(const QModelIndex &index)
 
1149
{
 
1150
    Q_Q(QTableWidget);
 
1151
    emit q->itemChanged(model()->item(index));
 
1152
}
 
1153
 
 
1154
void QTableWidgetPrivate::emitCurrentItemChanged(const QModelIndex &current,
 
1155
                                                 const QModelIndex &previous)
 
1156
{
 
1157
    Q_Q(QTableWidget);
 
1158
    emit q->currentItemChanged(model()->item(current), model()->item(previous));
 
1159
}
 
1160
 
 
1161
/*!
 
1162
    \fn void QTableWidget::itemActivated(QTableWidgetItem *item)
 
1163
 
 
1164
    This signal is emitted when the specified \a item has been activated
 
1165
*/
 
1166
 
 
1167
/*!
 
1168
    \fn void QTableWidget::currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
 
1169
 
 
1170
    This signal is emitted whenever the current item changes. The \a
 
1171
    previous item is the item that previously had the focus, \a
 
1172
    current is the new current item.
 
1173
*/
 
1174
 
 
1175
/*!
 
1176
    \fn void QTableWidget::itemSelectionChanged()
 
1177
 
 
1178
    This signal is emitted whenever the selection changes.
 
1179
 
 
1180
    \sa selectedItems() isItemSelected()
 
1181
*/
 
1182
 
 
1183
/*!
 
1184
    \fn void QTableWidget::itemClicked(QTableWidgetItem *item)
 
1185
 
 
1186
    This signal is emitted whenever an item in the table is clicked.
 
1187
    The \a item specified is the item that was clicked.
 
1188
*/
 
1189
 
 
1190
/*!
 
1191
    \fn void QTableWidget::itemDoubleClicked(QTableWidgetItem *item)
 
1192
 
 
1193
    This signal is emitted whenever an item in the table is double
 
1194
    clicked. The \a item specified is the item that was double clicked.
 
1195
*/
 
1196
 
 
1197
/*!
 
1198
    \fn void QTableWidget::itemEntered(QTableWidgetItem *item)
 
1199
 
 
1200
    This signal is emitted when the mouse cursor enters an item. The
 
1201
    \a item is the item entered.
 
1202
 
 
1203
    This signal is only emitted when mouseTracking is turned on, or when a
 
1204
    mouse button is pressed while moving into an item.
 
1205
*/
 
1206
 
 
1207
/*!
 
1208
    \fn void QTableWidget::itemPressed(QTableWidgetItem *item)
 
1209
 
 
1210
    This signal is emitted whenever an item in the table is pressed.
 
1211
    The \a item specified is the item that was pressed.
 
1212
*/
 
1213
 
 
1214
/*!
 
1215
    \fn void QTableWidget::itemChanged(QTableWidgetItem *item)
 
1216
 
 
1217
    This signal is emitted whenever the data of \a item has changed.
 
1218
*/
 
1219
 
 
1220
/*!
 
1221
    \fn QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const
 
1222
 
 
1223
    Returns the item at the position (\a{ax}, \a{ay}) in the table's
 
1224
    coordinate system.
 
1225
*/
 
1226
 
 
1227
/*!
 
1228
    \variable QTableWidgetItem::Type
 
1229
 
 
1230
    The default type for table widget items.
 
1231
 
 
1232
    \sa UserType, type()
 
1233
*/
 
1234
 
 
1235
/*!
 
1236
    \variable QTableWidgetItem::UserType
 
1237
 
 
1238
    The minimum value for custom types. Values below UserType are
 
1239
    reserved by Qt.
 
1240
 
 
1241
    \sa Type, type()
 
1242
*/
 
1243
 
 
1244
/*!
 
1245
    \fn int QTableWidgetItem::type() const
 
1246
 
 
1247
    Returns the type passed to the QTableWidgetItem constructor.
 
1248
*/
 
1249
 
 
1250
/*!
 
1251
    Creates a new table view with the given \a parent.
 
1252
*/
 
1253
QTableWidget::QTableWidget(QWidget *parent)
 
1254
    : QTableView(*new QTableWidgetPrivate, parent)
 
1255
{
 
1256
    Q_D(QTableWidget);
 
1257
    setModel(new QTableModel(0, 0, this));
 
1258
    d->setup();
 
1259
}
 
1260
 
 
1261
/*!
 
1262
    Creates a new table view with the given \a rows and \a columns, and with the given \a parent.
 
1263
*/
 
1264
QTableWidget::QTableWidget(int rows, int columns, QWidget *parent)
 
1265
    : QTableView(*new QTableWidgetPrivate, parent)
 
1266
{
 
1267
    Q_D(QTableWidget);
 
1268
    setModel(new QTableModel(rows, columns, this));
 
1269
    d->setup();
 
1270
}
 
1271
 
 
1272
/*!
 
1273
    Destroys this QTableWidget.
 
1274
*/
 
1275
QTableWidget::~QTableWidget()
 
1276
{
 
1277
}
 
1278
 
 
1279
/*!
 
1280
    Sets the number of rows in this table's model to \a rows. If
 
1281
    this is less than rowCount(), the data in the unwanted rows
 
1282
    is discarded.
 
1283
 
 
1284
    \sa setColumnCount()
 
1285
*/
 
1286
void QTableWidget::setRowCount(int rows)
 
1287
{
 
1288
    Q_D(QTableWidget);
 
1289
    d->model()->setRowCount(rows);
 
1290
}
 
1291
 
 
1292
/*!
 
1293
  Returns the number of rows.
 
1294
*/
 
1295
 
 
1296
int QTableWidget::rowCount() const
 
1297
{
 
1298
    Q_D(const QTableWidget);
 
1299
    return d->model()->rowCount();
 
1300
}
 
1301
 
 
1302
/*!
 
1303
    Sets the number of columns in this table's model to \a columns. If
 
1304
    this is less than columnCount(), the data in the unwanted columns
 
1305
    is discarded.
 
1306
 
 
1307
    \sa setRowCount()
 
1308
*/
 
1309
void QTableWidget::setColumnCount(int columns)
 
1310
{
 
1311
    Q_D(QTableWidget);
 
1312
    d->model()->setColumnCount(columns);
 
1313
}
 
1314
 
 
1315
/*!
 
1316
  Returns the number of columns.
 
1317
*/
 
1318
 
 
1319
int QTableWidget::columnCount() const
 
1320
{
 
1321
    Q_D(const QTableWidget);
 
1322
    return d->model()->columnCount();
 
1323
}
 
1324
 
 
1325
/*!
 
1326
  Returns the row for the \a item.
 
1327
*/
 
1328
int QTableWidget::row(const QTableWidgetItem *item) const
 
1329
{
 
1330
    Q_ASSERT(item);
 
1331
    Q_D(const QTableWidget);
 
1332
    return d->model()->index(item).row();
 
1333
}
 
1334
 
 
1335
/*!
 
1336
  Returns the column for the \a item.
 
1337
*/
 
1338
int QTableWidget::column(const QTableWidgetItem *item) const
 
1339
{
 
1340
    Q_ASSERT(item);
 
1341
    Q_D(const QTableWidget);
 
1342
    return d->model()->index(item).column();
 
1343
}
 
1344
 
 
1345
 
 
1346
/*!
 
1347
    Returns the item for the given \a row and \a column.
 
1348
 
 
1349
    \sa setItem()
 
1350
*/
 
1351
QTableWidgetItem *QTableWidget::item(int row, int column) const
 
1352
{
 
1353
    Q_D(const QTableWidget);
 
1354
    return d->model()->item(row, column);
 
1355
}
 
1356
 
 
1357
/*!
 
1358
    Sets the item for the given \a row and \a column to \a item.
 
1359
 
 
1360
    \sa item()
 
1361
*/
 
1362
void QTableWidget::setItem(int row, int column, QTableWidgetItem *item)
 
1363
{
 
1364
    Q_ASSERT(item);
 
1365
    Q_D(QTableWidget);
 
1366
    item->view = this;
 
1367
    d->model()->setItem(row, column, item);
 
1368
}
 
1369
 
 
1370
/*!
 
1371
    Removes the item at \a row and \a column from the table without deleting it.
 
1372
*/
 
1373
QTableWidgetItem *QTableWidget::takeItem(int row, int column)
 
1374
{
 
1375
    Q_D(QTableWidget);
 
1376
    QTableWidgetItem *item = d->model()->takeItem(row, column);
 
1377
    item->view = 0;
 
1378
    return item;
 
1379
}
 
1380
 
 
1381
/*!
 
1382
  Returns the vertical header item for row \a row.
 
1383
*/
 
1384
QTableWidgetItem *QTableWidget::verticalHeaderItem(int row) const
 
1385
{
 
1386
    Q_D(const QTableWidget);
 
1387
    return d->model()->verticalHeaderItem(row);
 
1388
}
 
1389
 
 
1390
/*!
 
1391
  Sets the vertical header item for row \a row to \a item.
 
1392
*/
 
1393
void QTableWidget::setVerticalHeaderItem(int row, QTableWidgetItem *item)
 
1394
{
 
1395
    Q_D(QTableWidget);
 
1396
    item->view = this;
 
1397
    d->model()->setVerticalHeaderItem(row, item);
 
1398
}
 
1399
 
 
1400
/*!
 
1401
  Returns the horizontal header item for column \a column.
 
1402
*/
 
1403
QTableWidgetItem *QTableWidget::horizontalHeaderItem(int column) const
 
1404
{
 
1405
    Q_D(const QTableWidget);
 
1406
    return d->model()->horizontalHeaderItem(column);
 
1407
}
 
1408
 
 
1409
/*!
 
1410
  Sets the horizontal header item for column \a column to \a item.
 
1411
*/
 
1412
void QTableWidget::setHorizontalHeaderItem(int column, QTableWidgetItem *item)
 
1413
{
 
1414
    Q_D(QTableWidget);
 
1415
    item->view = this;
 
1416
    d->model()->setHorizontalHeaderItem(column, item);
 
1417
}
 
1418
 
 
1419
/*!
 
1420
  Sets the vertical header labels using \a labels.
 
1421
*/
 
1422
void QTableWidget::setVerticalHeaderLabels(const QStringList &labels)
 
1423
{
 
1424
    Q_D(QTableWidget);
 
1425
    QTableModel *model = d->model();
 
1426
    QTableWidgetItem *item = 0;
 
1427
    for (int i = 0; i < model->rowCount() && i < labels.count(); ++i) {
 
1428
        item = model->verticalHeaderItem(i);
 
1429
        if (!item) {
 
1430
            item = model->createItem();
 
1431
            setVerticalHeaderItem(i, item);
 
1432
        }
 
1433
        item->setText(labels.at(i));
 
1434
    }
 
1435
}
 
1436
 
 
1437
/*!
 
1438
  Sets the horizontal header labels using \a labels.
 
1439
*/
 
1440
void QTableWidget::setHorizontalHeaderLabels(const QStringList &labels)
 
1441
{
 
1442
    Q_D(QTableWidget);
 
1443
    QTableModel *model = d->model();
 
1444
    QTableWidgetItem *item = 0;
 
1445
    for (int i = 0; i < model->columnCount() && i < labels.count(); ++i) {
 
1446
        item = model->horizontalHeaderItem(i);
 
1447
        if (!item) {
 
1448
            item = model->createItem();
 
1449
            setHorizontalHeaderItem(i, item);
 
1450
        }
 
1451
        item->setText(labels.at(i));
 
1452
    }
 
1453
}
 
1454
 
 
1455
/*!
 
1456
  Returns the row of the current item.
 
1457
*/
 
1458
int QTableWidget::currentRow() const
 
1459
{
 
1460
    return currentIndex().row();
 
1461
}
 
1462
 
 
1463
/*!
 
1464
  Returns the column of the current item.
 
1465
*/
 
1466
int QTableWidget::currentColumn() const
 
1467
{
 
1468
    return currentIndex().column();
 
1469
}
 
1470
 
 
1471
/*!
 
1472
  Returns the current item.
 
1473
*/
 
1474
QTableWidgetItem *QTableWidget::currentItem() const
 
1475
{
 
1476
    Q_D(const QTableWidget);
 
1477
    return d->model()->item(currentIndex());
 
1478
}
 
1479
 
 
1480
/*!
 
1481
  Sets the current item to \a item.
 
1482
*/
 
1483
void QTableWidget::setCurrentItem(QTableWidgetItem *item)
 
1484
{
 
1485
    Q_D(QTableWidget);
 
1486
    setCurrentIndex(d->model()->index(item));
 
1487
}
 
1488
 
 
1489
/*!
 
1490
  Sorts all the rows in the table widget based on \a column and \a order.
 
1491
*/
 
1492
void QTableWidget::sortItems(int column, Qt::SortOrder order)
 
1493
{
 
1494
    Q_D(QTableWidget);
 
1495
    d->model()->sort(column, order);
 
1496
    horizontalHeader()->setSortIndicator(column, order);
 
1497
}
 
1498
 
 
1499
/*!
 
1500
  If \a enable is true, the items in the widget will be sorted if the user
 
1501
  clicks on a horizontal header section; otherwise sorting is disabled.
 
1502
*/
 
1503
 
 
1504
void QTableWidget::setSortingEnabled(bool enable)
 
1505
{
 
1506
    Q_D(QTableWidget);
 
1507
    d->sortingEnabled = enable;
 
1508
    if (!enable && horizontalHeader()->isSortIndicatorShown())
 
1509
        horizontalHeader()->setSortIndicatorShown(false);
 
1510
}
 
1511
 
 
1512
/*!
 
1513
  Returns if sorting is enabled; otherwise returns false.
 
1514
  Sorting is enabled when the user clicks on a horizontal header section.
 
1515
*/
 
1516
 
 
1517
bool QTableWidget::isSortingEnabled() const
 
1518
{
 
1519
    Q_D(const QTableWidget);
 
1520
    return d->sortingEnabled;
 
1521
}
 
1522
 
 
1523
/*!
 
1524
  Starts editing the \a item if it is editable.
 
1525
*/
 
1526
 
 
1527
void QTableWidget::editItem(QTableWidgetItem *item)
 
1528
{
 
1529
    Q_ASSERT(item);
 
1530
    Q_D(QTableWidget);
 
1531
    edit(d->model()->index(item));
 
1532
}
 
1533
 
 
1534
/*!
 
1535
  Opens an editor for the give \a item. The editor remains open after editing.
 
1536
 
 
1537
  \sa closePersistentEditor()
 
1538
*/
 
1539
void QTableWidget::openPersistentEditor(QTableWidgetItem *item)
 
1540
{
 
1541
    Q_ASSERT(item);
 
1542
    Q_D(QTableWidget);
 
1543
    QModelIndex index = d->model()->index(item);
 
1544
    QAbstractItemView::openPersistentEditor(index);
 
1545
}
 
1546
 
 
1547
/*!
 
1548
  Closes the persistent editor for \a item.
 
1549
 
 
1550
  \sa openPersistentEditor()
 
1551
*/
 
1552
void QTableWidget::closePersistentEditor(QTableWidgetItem *item)
 
1553
{
 
1554
    Q_ASSERT(item);
 
1555
    Q_D(QTableWidget);
 
1556
    QModelIndex index = d->model()->index(item);
 
1557
    QAbstractItemView::closePersistentEditor(index);
 
1558
}
 
1559
 
 
1560
/*!
 
1561
  Returns true if the \a item is selected, otherwise returns false.
 
1562
*/
 
1563
 
 
1564
bool QTableWidget::isItemSelected(const QTableWidgetItem *item) const
 
1565
{
 
1566
    Q_D(const QTableWidget);
 
1567
    QModelIndex index = d->model()->index(item);
 
1568
    return selectionModel()->isSelected(index) && !isIndexHidden(index);
 
1569
}
 
1570
 
 
1571
/*!
 
1572
  Selects or deselects \a item depending on \a select.
 
1573
*/
 
1574
void QTableWidget::setItemSelected(const QTableWidgetItem *item, bool select)
 
1575
{
 
1576
    Q_D(QTableWidget);
 
1577
    QModelIndex index = d->model()->index(item);
 
1578
    selectionModel()->select(index, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
 
1579
}
 
1580
 
 
1581
/*!
 
1582
  Selects or deselects the \a range depending on \a select.
 
1583
*/
 
1584
void QTableWidget::setRangeSelected(const QTableWidgetSelectionRange &range, bool select)
 
1585
{
 
1586
    if (!model()->hasIndex(range.topRow(), range.leftColumn(), rootIndex()) ||
 
1587
        !model()->hasIndex(range.bottomRow(), range.rightColumn(), rootIndex()))
 
1588
        return;
 
1589
 
 
1590
    QModelIndex topLeft = model()->index(range.topRow(), range.leftColumn(), rootIndex());
 
1591
    QModelIndex bottomRight = model()->index(range.bottomRow(), range.rightColumn(), rootIndex());
 
1592
 
 
1593
    selectionModel()->select(QItemSelection(topLeft, bottomRight),
 
1594
                             select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
 
1595
}
 
1596
 
 
1597
/*!
 
1598
  Returns a list of all selected ranges.
 
1599
 
 
1600
  \sa QTableWidgetSelectionRange
 
1601
*/
 
1602
 
 
1603
QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
 
1604
{
 
1605
    const QList<QItemSelectionRange> ranges = selectionModel()->selection();
 
1606
    QList<QTableWidgetSelectionRange> result;
 
1607
    for (int i = 0; i < ranges.count(); ++i)
 
1608
        result.append(QTableWidgetSelectionRange(ranges.at(i).top(),
 
1609
                                                 ranges.at(i).left(),
 
1610
                                                 ranges.at(i).bottom(),
 
1611
                                                 ranges.at(i).right()));
 
1612
    return result;
 
1613
}
 
1614
 
 
1615
/*!
 
1616
  Returns a list of all selected items.
 
1617
*/
 
1618
 
 
1619
QList<QTableWidgetItem*> QTableWidget::selectedItems()
 
1620
{
 
1621
    Q_D(QTableWidget);
 
1622
    QModelIndexList indexes = selectedIndexes();
 
1623
    QList<QTableWidgetItem*> items;
 
1624
    for (int i = 0; i < indexes.count(); ++i) {
 
1625
        QModelIndex index = indexes.at(i);
 
1626
        QTableWidgetItem *item = d->model()->item(index);
 
1627
        if (item)
 
1628
            items.append(item);
 
1629
    }
 
1630
    return items;
 
1631
}
 
1632
 
 
1633
/*!
 
1634
  Finds items that matches the \a text using the given \a flags.
 
1635
*/
 
1636
 
 
1637
QList<QTableWidgetItem*> QTableWidget::findItems(const QString &text, Qt::MatchFlags flags) const
 
1638
{
 
1639
    Q_D(const QTableWidget);
 
1640
    QModelIndexList indexes;
 
1641
    for (int column = 0; column < columnCount(); ++column)
 
1642
        indexes += d->model()->match(model()->index(0, column, QModelIndex()),
 
1643
                                     Qt::DisplayRole, text, -1, flags);
 
1644
    QList<QTableWidgetItem*> items;
 
1645
    for (int i = 0; i < indexes.size(); ++i)
 
1646
        items.append(d->model()->item(indexes.at(i)));
 
1647
    return items;
 
1648
}
 
1649
 
 
1650
/*!
 
1651
  Returns the visual row of the given \a logicalRow.
 
1652
*/
 
1653
 
 
1654
int QTableWidget::visualRow(int logicalRow) const
 
1655
{
 
1656
    return verticalHeader()->visualIndex(logicalRow);
 
1657
}
 
1658
 
 
1659
/*!
 
1660
  Returns the visual column of the given \a logicalColumn.
 
1661
*/
 
1662
 
 
1663
int QTableWidget::visualColumn(int logicalColumn) const
 
1664
{
 
1665
    return horizontalHeader()->visualIndex(logicalColumn);
 
1666
}
 
1667
 
 
1668
/*!
 
1669
  Returns a pointer to the item at the coordinates \a p.
 
1670
*/
 
1671
 
 
1672
QTableWidgetItem *QTableWidget::itemAt(const QPoint &p) const
 
1673
{
 
1674
    Q_D(const QTableWidget);
 
1675
    return d->model()->item(indexAt(p));
 
1676
}
 
1677
 
 
1678
/*!
 
1679
  Returns the rectangle on the viewport occupied by the item at \a item.
 
1680
*/
 
1681
QRect QTableWidget::visualItemRect(const QTableWidgetItem *item) const
 
1682
{
 
1683
    Q_ASSERT(item);
 
1684
    Q_D(const QTableWidget);
 
1685
    QModelIndex index = d->model()->index(const_cast<QTableWidgetItem*>(item));
 
1686
    Q_ASSERT(index.isValid());
 
1687
    return visualRect(index);
 
1688
}
 
1689
 
 
1690
/*!
 
1691
    Scrolls the view if necessary to ensure that the \a item is visible.
 
1692
    The \a hint parameter specifies more precisely where the
 
1693
    \a item should be located after the operation.
 
1694
*/
 
1695
 
 
1696
void QTableWidget::scrollToItem(const QTableWidgetItem *item, ScrollHint hint)
 
1697
{
 
1698
    Q_ASSERT(item);
 
1699
    Q_D(QTableWidget);
 
1700
    QModelIndex index = d->model()->index(const_cast<QTableWidgetItem*>(item));
 
1701
    Q_ASSERT(index.isValid());
 
1702
    QTableView::scrollTo(index, hint);
 
1703
}
 
1704
 
 
1705
/*!
 
1706
    Returns the item prototype used by the table.
 
1707
 
 
1708
    Copies of the item prototype are returned by the createItem()
 
1709
    function.
 
1710
 
 
1711
    \sa setItemPrototype()
 
1712
*/
 
1713
const QTableWidgetItem *QTableWidget::itemPrototype() const
 
1714
{
 
1715
    Q_D(const QTableWidget);
 
1716
    return d->model()->itemPrototype();
 
1717
}
 
1718
 
 
1719
/*!
 
1720
    Sets the item prototype for the table to the specified \a item.
 
1721
 
 
1722
    \sa itemPrototype()
 
1723
*/
 
1724
void QTableWidget::setItemPrototype(const QTableWidgetItem *item)
 
1725
{
 
1726
    Q_D(QTableWidget);
 
1727
    d->model()->setItemPrototype(item);
 
1728
}
 
1729
 
 
1730
/*!
 
1731
  Inserts an empty row into the table at \a row.
 
1732
*/
 
1733
void QTableWidget::insertRow(int row)
 
1734
{
 
1735
    Q_D(QTableWidget);
 
1736
    d->model()->insertRows(row);
 
1737
}
 
1738
 
 
1739
/*!
 
1740
  Inserts an empty column into the table at \a column.
 
1741
*/
 
1742
void QTableWidget::insertColumn(int column)
 
1743
{
 
1744
    Q_D(QTableWidget);
 
1745
    d->model()->insertColumns(column);
 
1746
}
 
1747
 
 
1748
/*!
 
1749
  Removes the row \a row and all its items from the table.
 
1750
*/
 
1751
void QTableWidget::removeRow(int row)
 
1752
{
 
1753
    Q_D(QTableWidget);
 
1754
    d->model()->removeRows(row);
 
1755
}
 
1756
 
 
1757
/*!
 
1758
  Removes the column \a column and all its items from the table.
 
1759
*/
 
1760
void QTableWidget::removeColumn(int column)
 
1761
{
 
1762
    Q_D(QTableWidget);
 
1763
    d->model()->removeColumns(column);
 
1764
}
 
1765
 
 
1766
/*!
 
1767
  Removes all items and selections in the view.
 
1768
*/
 
1769
 
 
1770
void QTableWidget::clear()
 
1771
{
 
1772
    Q_D(QTableWidget);
 
1773
    selectionModel()->clear();
 
1774
    d->model()->clear();
 
1775
}
 
1776
 
 
1777
/*!
 
1778
    Returns a list of MIME types that can be used to describe a list of
 
1779
    tablewidget items.
 
1780
 
 
1781
    \sa mimeData()
 
1782
*/
 
1783
QStringList QTableWidget::mimeTypes() const
 
1784
{
 
1785
    return model()->QAbstractItemModel::mimeTypes();
 
1786
}
 
1787
 
 
1788
/*!
 
1789
    Returns an object that contains a serialized description of the specified
 
1790
    \a items. The format used to describe the items is obtained from the
 
1791
    mimeTypes() function.
 
1792
 
 
1793
    If the list of items is empty, 0 is returned rather than a serialized
 
1794
    empty list.
 
1795
*/
 
1796
QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem*>) const
 
1797
{
 
1798
    return d_func()->model()->internalMimeData();
 
1799
}
 
1800
 
 
1801
/*!
 
1802
    Handles the \a data supplied by a drag and drop operation that ended with
 
1803
    the given \a action in the given \a row and \a column.
 
1804
 
 
1805
    \sa supportedDropActions()
 
1806
*/
 
1807
bool QTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
 
1808
{
 
1809
    return model()->QAbstractItemModel::dropMimeData(data, action , row, column, QModelIndex());
 
1810
}
 
1811
 
 
1812
/*!
 
1813
  Returns the drop actions supported by this view.
 
1814
 
 
1815
  \sa Qt::DropActions
 
1816
*/
 
1817
Qt::DropActions QTableWidget::supportedDropActions() const
 
1818
{
 
1819
    return model()->QAbstractItemModel::supportedDropActions();
 
1820
}
 
1821
 
 
1822
/*!
 
1823
  Returns a list of pointers to the items contained in the \a data object.
 
1824
  If the object was not created by a QTreeWidget in the same process, the list
 
1825
  is empty.
 
1826
 
 
1827
*/
 
1828
QList<QTableWidgetItem*> QTableWidget::items(const QMimeData *data) const
 
1829
{
 
1830
    const QTableWidgetMimeData *twd = qobject_cast<const QTableWidgetMimeData*>(data);
 
1831
    if (twd)
 
1832
        return twd->items;
 
1833
    return QList<QTableWidgetItem*>();
 
1834
}
 
1835
 
 
1836
/*!
 
1837
  Returns the QModelIndex assocated with the given \a item.
 
1838
*/
 
1839
 
 
1840
QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const
 
1841
{
 
1842
    Q_D(const QTableWidget);
 
1843
    Q_ASSERT(item);
 
1844
    return d->model()->index(item);
 
1845
}
 
1846
 
 
1847
/*!
 
1848
  Returns a pointer to the QTableWidgetItem assocated with the given \a index.
 
1849
*/
 
1850
 
 
1851
QTableWidgetItem *QTableWidget::itemFromIndex(const QModelIndex &index) const
 
1852
{
 
1853
    Q_D(const QTableWidget);
 
1854
    Q_ASSERT(index.isValid());
 
1855
    return d->model()->item(index);
 
1856
}
 
1857
 
 
1858
/*!
 
1859
  \internal
 
1860
*/
 
1861
void QTableWidget::setModel(QAbstractItemModel *model)
 
1862
{
 
1863
    QTableView::setModel(model);
 
1864
}
 
1865
 
 
1866
#include "moc_qtablewidget.cpp"