1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the linguist application of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include <qapplication.h>
31
#include "contextmodel.h"
32
#include "messagemodel.h"
35
static Qt::SortOrder sSortOrder = Qt::AscendingOrder;
36
static int sSortColumn = 1;
38
ContextItem::ContextItem(QString c)
39
: sortColumn(-1), com(""), ctxt(c)
46
ContextItem::~ContextItem()
48
// delete all the message items
49
for (int i=0; i<msgItemList.count(); ++i)
51
delete msgItemList[i];
55
void ContextItem::appendToComment(const QString& x)
58
com += QString("\n\n");
62
MessageItem *ContextItem::messageItem(int i)
64
if ((i >= msgItemList.count()) || (i < 0))
67
return msgItemList[i];
70
bool ContextItem::sortParameters(Qt::SortOrder &so, int &sc) const
81
void ContextItem::sortMessages(int column, Qt::SortOrder order)
83
sortOrder = sSortOrder = order;
84
sortColumn = sSortColumn = column;
86
qSort(msgItemList.begin(), msgItemList.end(), ContextItem::compare);
89
bool ContextItem::compare(const MessageItem *left, const MessageItem *right)
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))
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))
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))
114
ContextModel::ContextModel(QObject *parent)
115
: QAbstractTableModel(parent), sortColumn(-1)
120
ContextItem *ContextModel::contextItem(const QModelIndex &indx) const
123
return cntxtList.at(indx.row());
128
bool ContextModel::sortParameters(Qt::SortOrder &so, int &sc) const
130
if (sortColumn == -1)
139
void ContextModel::updateItem(QModelIndex indx)
141
QModelIndex strtindx = createIndex(indx.row(), 0);
142
QModelIndex endindx = createIndex(indx.row(), 2);
144
emit dataChanged(strtindx, endindx);
147
void ContextModel::clearContextList()
149
int r = cntxtList.count();
151
if (r <= 0) // no items
154
for (int i=0; i<r; ++i)
162
// since we don't add or remove single rows, update all at once...
163
void ContextModel::updateAll()
168
int ContextModel::rowCount(const QModelIndex &) const
170
return cntxtList.count();
173
int ContextModel::columnCount(const QModelIndex &) const
178
QVariant ContextModel::headerData(int section, Qt::Orientation orientation, int role) const
180
if ((role == Qt::DisplayRole) && (orientation == Qt::Horizontal)) {
185
return tr("Context");
196
QVariant ContextModel::data(const QModelIndex &index, int role) const
198
int row = index.row();
199
int column = index.column();
201
if (row >= cntxtList.count() || !index.isValid())
204
ContextItem *cntxtItem = cntxtList.at(row);
206
if (role == Qt::DisplayRole) {
211
return cntxtItem->context().simplified();
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);
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);
228
return qVariantFromValue(*TrWindow::pxOff);
234
void ContextModel::sort(int column, Qt::SortOrder order)
236
if (cntxtList.count() <= 0)
239
sortOrder = sSortOrder = order;
240
sortColumn = sSortColumn = column;
242
qSort(cntxtList.begin(), cntxtList.end(), ContextModel::compare);
243
emit dataChanged(index(0,0), index(cntxtList.count()-1, 2));
246
bool ContextModel::compare(const ContextItem *left, const ContextItem *right)
253
nleft = left->isContextObsolete() + left->finished();
254
nright = right->isContextObsolete() + right->finished();
256
if ((sSortOrder == Qt::AscendingOrder) ? (nleft < nright) : !(nleft < nright))
260
res = QString::localeAwareCompare(left->context(), right->context());
261
if ((sSortOrder == Qt::AscendingOrder) ? (res < 0) : !(res < 0))
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))