~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/oscar/icq/xtrazstatusmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    xtrazstatusmodel.cpp  -  Xtraz Status Model
 
3
 
 
4
    Copyright (c) 2007 by Roman Jarosz <kedgedev@centrum.cz>
 
5
    Kopete    (c) 2007 by the Kopete developers  <kopete-devel@kde.org>
 
6
 
 
7
    *************************************************************************
 
8
    *                                                                       *
 
9
    * This program is free software; you can redistribute it and/or modify  *
 
10
    * it under the terms of the GNU General Public License as published by  *
 
11
    * the Free Software Foundation; either version 2 of the License, or     *
 
12
    * (at your option) any later version.                                   *
 
13
    *                                                                       *
 
14
    *************************************************************************
 
15
*/
 
16
 
 
17
#include "xtrazstatusmodel.h"
 
18
 
 
19
#include <kicon.h>
 
20
#include <klocale.h>
 
21
 
 
22
namespace Xtraz
 
23
{
 
24
 
 
25
StatusModel::StatusModel( QObject * parent )
 
26
: QAbstractTableModel( parent )
 
27
{
 
28
}
 
29
 
 
30
QList<Xtraz::Status> StatusModel::getStatuses() const
 
31
{
 
32
        return mStatuses;
 
33
}
 
34
 
 
35
void StatusModel::setStatuses( const QList<Xtraz::Status> &statusList )
 
36
{
 
37
        mStatuses = statusList;
 
38
        reset();
 
39
}
 
40
 
 
41
int StatusModel::rowCount( const QModelIndex& ) const
 
42
{
 
43
        return mStatuses.count();
 
44
}
 
45
 
 
46
int StatusModel::columnCount( const QModelIndex& ) const
 
47
{
 
48
        return 2;
 
49
}
 
50
 
 
51
bool StatusModel::setData( const QModelIndex &index, const QVariant &value, int role )
 
52
{
 
53
        if ( !index.isValid() )
 
54
                return false;
 
55
        
 
56
        if ( index.row() >= mStatuses.count() || index.row() < 0 )
 
57
                return false;
 
58
        
 
59
        if ( role == Qt::EditRole )
 
60
        {
 
61
                switch ( index.column() )
 
62
                {
 
63
                case 0:
 
64
                        mStatuses[index.row()].setDescription( value.toString() );
 
65
                        return true;
 
66
                case 1:
 
67
                        mStatuses[index.row()].setMessage( value.toString() );
 
68
                        return true;
 
69
                }
 
70
        }
 
71
        else if ( role == Qt::UserRole )
 
72
        {
 
73
                if ( index.column() == 0 )
 
74
                {
 
75
                        mStatuses[index.row()].setStatus( value.toInt() );
 
76
                        return true;
 
77
                }
 
78
        }
 
79
        return false;
 
80
}
 
81
 
 
82
QVariant StatusModel::data( const QModelIndex &index, int role ) const
 
83
{
 
84
        if ( !index.isValid() )
 
85
                return QVariant();
 
86
 
 
87
        if ( index.row() >= mStatuses.count() || index.row() < 0 )
 
88
                return QVariant();
 
89
 
 
90
        Xtraz::Status status = mStatuses.at(index.row());
 
91
 
 
92
        if ( role == Qt::DisplayRole )
 
93
        {
 
94
                switch ( index.column() )
 
95
                {
 
96
                case 0:
 
97
                        return status.description();
 
98
                case 1:
 
99
                        return status.message();
 
100
                }
 
101
        }
 
102
        else if ( role == Qt::UserRole )
 
103
        {
 
104
                if ( index.column() == 0 )
 
105
                        return status.status();
 
106
        }
 
107
        else if ( role == Qt::DecorationRole )
 
108
        {
 
109
                if ( index.column() == 0 )
 
110
                {
 
111
                        return KIcon( QString( "icq_xstatus%1" ).arg( status.status() ) );
 
112
                }
 
113
        }
 
114
 
 
115
        return QVariant();
 
116
}
 
117
 
 
118
 
 
119
QVariant StatusModel::headerData( int section, Qt::Orientation orientation, int role ) const
 
120
{
 
121
        if ( orientation == Qt::Vertical && role == Qt::DisplayRole )
 
122
                return (section + 1);
 
123
        
 
124
        if ( role == Qt::DisplayRole )
 
125
        {
 
126
                switch ( section )
 
127
                {
 
128
                case 0:
 
129
                        return i18n( "Description" );
 
130
                case 1:
 
131
                        return i18n( "Message" );
 
132
                        break;
 
133
                default:
 
134
                        return QVariant();
 
135
                }
 
136
        }
 
137
        
 
138
        return QVariant();
 
139
}
 
140
 
 
141
Qt::ItemFlags StatusModel::flags( const QModelIndex &index ) const
 
142
{
 
143
        if ( !index.isValid() )
 
144
                return Qt::ItemIsEnabled;
 
145
        
 
146
        if ( index.row() >= mStatuses.count() || index.row() < 0 )
 
147
                return Qt::ItemIsEnabled;
 
148
 
 
149
        return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
 
150
}
 
151
 
 
152
bool StatusModel::swapRows( int i, int j )
 
153
{
 
154
        if ( i == j || i < 0 || mStatuses.count() <= i ||
 
155
             j < 0 || mStatuses.count() <= j )
 
156
                return false;
 
157
        
 
158
        mStatuses.swap( i, j );
 
159
        emit dataChanged( index( qMin( i, j ), 0 ), index( qMax( i, j ), columnCount() ) );
 
160
        return true;
 
161
}
 
162
 
 
163
bool StatusModel::insertRows( int row, int count, const QModelIndex &parent )
 
164
{
 
165
        if ( row > mStatuses.count() || row < 0 )
 
166
                return false;
 
167
 
 
168
        beginInsertRows( parent, row, row + count - 1 );
 
169
 
 
170
        for ( int i = 0; i < count; i++ )
 
171
                mStatuses.insert( row, Xtraz::Status() );
 
172
 
 
173
        endInsertRows();
 
174
        return true;
 
175
}
 
176
 
 
177
bool StatusModel::removeRows( int row, int count, const QModelIndex &parent )
 
178
{
 
179
        if ( row >= mStatuses.count() || row < 0 || row + count > mStatuses.count() )
 
180
                return false;
 
181
        
 
182
        beginRemoveRows( parent, row, row + count - 1 );
 
183
 
 
184
        for ( int i = 0; i < count; i++ )
 
185
                mStatuses.removeAt( row );
 
186
 
 
187
        endRemoveRows();
 
188
        return true;
 
189
}
 
190
 
 
191
}