~schumski-deactivatedaccount-deactivatedaccount/k3b/master

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
/* 
 *
 * Copyright (C) 2011 Michal Malek <michalm@jabster.pl>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bmodelutils.h"
#include <QModelIndex>

namespace K3b
{
namespace ModelUtils
{
    
    Qt::CheckState commonCheckState( const QModelIndexList& indexes )
    {
        int checked = 0;
        foreach( const QModelIndex& index, indexes ) {
            if ( index.data( Qt::CheckStateRole ).toInt() == Qt::Checked ) {
                ++checked;
            }
        }
        
        if ( checked == 0 )
            return Qt::Unchecked;
        else if( checked == indexes.count() )
            return Qt::Checked;
        else
            return Qt::PartiallyChecked;
    }
    
    void toggleCommonCheckState( QAbstractItemModel* model, const QModelIndexList& indexes )
    {
        if ( model != 0 ) {
            Qt::CheckState commonState = commonCheckState( indexes );
            if ( commonState == Qt::Checked )
                commonState = Qt::Unchecked;
            else
                commonState = Qt::Checked;
            
            foreach( const QModelIndex& index, indexes ) {
                model->setData( index, commonState, Qt::CheckStateRole );
            }
        }
    }
    
    QString commonText( const QModelIndexList& indexes, int role )
    {
        if ( !indexes.isEmpty() ) {
            QString firstData = indexes.first().data( role ).toString();
            for ( int i = 1; i < indexes.size(); ++i ) {
                if ( indexes[i].data( role ).toString() != firstData )
                    return QString();
            }
            return firstData;
        }
        return QString();
    }
    
    void setCommonText( QAbstractItemModel* model, const QModelIndexList& indexes, const QString& value, int role )
    {
        if ( model != 0 ) {
            Q_FOREACH( QModelIndex index, indexes ) {
                if( !value.isEmpty() || indexes.size() == 1 )
                    model->setData( index, value, role );
            }
        }
    }
    
} // namespace ModelUtils
} // namespace K3b