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

« back to all changes in this revision

Viewing changes to tools/linguist/linguist/contextmodel.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 linguist application 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 <qapplication.h>
 
30
 
 
31
#include "contextmodel.h"
 
32
#include "messagemodel.h"
 
33
#include "trwindow.h"
 
34
 
 
35
static Qt::SortOrder sSortOrder = Qt::AscendingOrder;
 
36
static int sSortColumn = 1;
 
37
 
 
38
ContextItem::ContextItem(QString c)
 
39
: sortColumn(-1), com(""), ctxt(c)
 
40
{
 
41
    unfinishedCount = 0;
 
42
    dangerCount   = 0;
 
43
    obsoleteCount = 0;
 
44
}
 
45
 
 
46
ContextItem::~ContextItem()
 
47
{
 
48
    // delete all the message items
 
49
    for (int i=0; i<msgItemList.count(); ++i)
 
50
    {
 
51
        delete msgItemList[i];
 
52
    }
 
53
}
 
54
 
 
55
void ContextItem::appendToComment(const QString& x)
 
56
{
 
57
    if (!com.isEmpty())
 
58
        com += QString("\n\n");
 
59
    com += x;
 
60
}
 
61
 
 
62
MessageItem *ContextItem::messageItem(int i)
 
63
{
 
64
    if ((i >= msgItemList.count()) || (i < 0))
 
65
        return 0;
 
66
 
 
67
    return msgItemList[i];
 
68
}
 
69
 
 
70
bool ContextItem::sortParameters(Qt::SortOrder &so, int &sc) const
 
71
{
 
72
    if (sortColumn == -1)
 
73
        return false;
 
74
 
 
75
    so = sortOrder;
 
76
    sc = sortColumn;
 
77
 
 
78
    return true;
 
79
}
 
80
 
 
81
void ContextItem::sortMessages(int column, Qt::SortOrder order)
 
82
{
 
83
    sortOrder = sSortOrder = order;
 
84
    sortColumn = sSortColumn = column;
 
85
 
 
86
    qSort(msgItemList.begin(), msgItemList.end(), ContextItem::compare);
 
87
}
 
88
 
 
89
bool ContextItem::compare(const MessageItem *left, const MessageItem *right)
 
90
{
 
91
    int res, nleft, nright;
 
92
    if (sSortColumn == 0) {
 
93
        nleft = left->danger() + left->finished() + left->translation().isEmpty();
 
94
        nright = right->danger() + right->finished() + right->translation().isEmpty();
 
95
        if ((sSortOrder == Qt::AscendingOrder) ? (nleft < nright) : !(nleft < nright))
 
96
            return true;
 
97
    }
 
98
    else if (sSortColumn == 1) {
 
99
        res = QString::localeAwareCompare(left->sourceText().remove('&'),
 
100
            right->sourceText().remove('&'));
 
101
        if ((sSortOrder == Qt::AscendingOrder) ? (res < 0) : !(res < 0))
 
102
            return true;
 
103
    }
 
104
    else if (sSortColumn == 2) {
 
105
        res = QString::localeAwareCompare(left->translation().remove('&'),
 
106
            right->translation().remove('&'));
 
107
        if ((sSortOrder == Qt::AscendingOrder) ? (res < 0) : !(res < 0))
 
108
            return true;
 
109
    }
 
110
 
 
111
    return false;
 
112
}
 
113
 
 
114
ContextModel::ContextModel(QObject *parent)
 
115
: QAbstractTableModel(parent), sortColumn(-1)
 
116
{
 
117
 
 
118
}
 
119
 
 
120
ContextItem *ContextModel::contextItem(const QModelIndex &indx) const
 
121
{
 
122
    if (indx.isValid())
 
123
        return cntxtList.at(indx.row());
 
124
 
 
125
    return 0;
 
126
}
 
127
 
 
128
bool ContextModel::sortParameters(Qt::SortOrder &so, int &sc) const
 
129
{
 
130
    if (sortColumn == -1)
 
131
        return false;
 
132
 
 
133
    so = sortOrder;
 
134
    sc = sortColumn;
 
135
 
 
136
    return true;
 
137
}
 
138
 
 
139
void ContextModel::updateItem(QModelIndex indx)
 
140
{
 
141
    QModelIndex strtindx = createIndex(indx.row(), 0);
 
142
    QModelIndex endindx = createIndex(indx.row(), 2);
 
143
 
 
144
    emit dataChanged(strtindx, endindx);
 
145
}
 
146
 
 
147
void ContextModel::clearContextList()
 
148
{
 
149
    int r = cntxtList.count();
 
150
 
 
151
    if (r <= 0) // no items
 
152
        return;
 
153
 
 
154
    for (int i=0; i<r; ++i)
 
155
        delete cntxtList[i];
 
156
 
 
157
    cntxtList.clear();
 
158
 
 
159
    reset();
 
160
}
 
161
 
 
162
// since we don't add or remove single rows, update all at once...
 
163
void ContextModel::updateAll()
 
164
{
 
165
    reset();
 
166
}
 
167
 
 
168
int ContextModel::rowCount(const QModelIndex &) const
 
169
{
 
170
    return cntxtList.count();
 
171
}
 
172
 
 
173
int ContextModel::columnCount(const QModelIndex &) const
 
174
{
 
175
    return 3;
 
176
}
 
177
 
 
178
QVariant ContextModel::headerData(int section, Qt::Orientation orientation, int role) const
 
179
{
 
180
    if ((role == Qt::DisplayRole) && (orientation == Qt::Horizontal)) {
 
181
        switch(section)    {
 
182
        case 0:
 
183
            return tr("Done");
 
184
        case 1:
 
185
            return tr("Context");
 
186
        case 2:
 
187
            return tr("Items");
 
188
        }
 
189
 
 
190
        return "Error";
 
191
    }
 
192
 
 
193
    return QVariant();
 
194
}
 
195
 
 
196
QVariant ContextModel::data(const QModelIndex &index, int role) const
 
197
{
 
198
    int row = index.row();
 
199
    int column = index.column();
 
200
 
 
201
    if (row >= cntxtList.count() || !index.isValid())
 
202
        return QVariant();
 
203
 
 
204
    ContextItem *cntxtItem = cntxtList.at(row);
 
205
 
 
206
    if (role == Qt::DisplayRole) {
 
207
        switch(column) {
 
208
        case 0: // done
 
209
            return QVariant();
 
210
        case 1: // context
 
211
            return cntxtItem->context().simplified();
 
212
        case 2: // items
 
213
            QString s;
 
214
            int itemCount = cntxtItem->messageItemsInList();
 
215
            int obsoleteCount = cntxtItem->obsolete();
 
216
            int unfinishedCount = cntxtItem->unfinished();
 
217
            s.sprintf("%d/%d", itemCount - unfinishedCount - obsoleteCount,
 
218
                itemCount - obsoleteCount);
 
219
            return s;
 
220
        }
 
221
    }
 
222
    else if ((role == Qt::DecorationRole) && (column == 0)) {
 
223
        if (cntxtItem->isContextObsolete())
 
224
            return qVariantFromValue(*TrWindow::pxObsolete);
 
225
        else if (cntxtItem->finished())
 
226
            return qVariantFromValue(*TrWindow::pxOn);
 
227
        else
 
228
            return qVariantFromValue(*TrWindow::pxOff);
 
229
    }
 
230
 
 
231
    return QVariant();
 
232
}
 
233
 
 
234
void ContextModel::sort(int column, Qt::SortOrder order)
 
235
{
 
236
    if (cntxtList.count() <= 0)
 
237
        return;
 
238
 
 
239
    sortOrder = sSortOrder = order;
 
240
    sortColumn = sSortColumn = column;
 
241
 
 
242
    qSort(cntxtList.begin(), cntxtList.end(), ContextModel::compare);
 
243
    emit dataChanged(index(0,0), index(cntxtList.count()-1, 2));
 
244
}
 
245
 
 
246
bool ContextModel::compare(const ContextItem *left, const ContextItem *right)
 
247
{
 
248
    int res;
 
249
    int nleft, nright;
 
250
    switch (sSortColumn)
 
251
    {
 
252
    case 0:
 
253
        nleft = left->isContextObsolete() + left->finished();
 
254
        nright = right->isContextObsolete() + right->finished();
 
255
 
 
256
        if ((sSortOrder == Qt::AscendingOrder) ? (nleft < nright) : !(nleft < nright))
 
257
            return true;
 
258
        break;
 
259
    case 1:
 
260
        res = QString::localeAwareCompare(left->context(), right->context());
 
261
        if ((sSortOrder == Qt::AscendingOrder) ? (res < 0) : !(res < 0))
 
262
            return true;
 
263
        break;
 
264
    case 2:
 
265
        nleft = left->messageItemsInList() - left->unfinished() - left->obsolete();
 
266
        nright = right->messageItemsInList() - right->unfinished() - right->obsolete();
 
267
        if ((sSortOrder == Qt::AscendingOrder) ? (nleft < nright) : !(nleft < nright))
 
268
            return true;
 
269
        break;
 
270
    }
 
271
 
 
272
    return false;
 
273
}