Fix for crash which was introduced by commit

c2a0d3e889666539669562a6c1f7a080a0f29b11
(use QCollator instead of KStringHandler)

REVIEW: 121817
BUG: 342316
This commit is contained in:
Emmanuel Pescosta 2015-01-04 21:45:30 +01:00
parent 48788f71cf
commit 119f7a3fce
2 changed files with 35 additions and 23 deletions

View file

@ -1173,7 +1173,7 @@ void KFileItemModel::insertItems(QList<ItemData*>& newItems)
while (sourceIndexNewItems >= 0) { while (sourceIndexNewItems >= 0) {
ItemData* newItem = newItems.at(sourceIndexNewItems); ItemData* newItem = newItems.at(sourceIndexNewItems);
if (sourceIndexExistingItems >= 0 && lessThan(newItem, m_itemData.at(sourceIndexExistingItems))) { if (sourceIndexExistingItems >= 0 && lessThan(newItem, m_itemData.at(sourceIndexExistingItems), m_collator)) {
// Move an existing item to its new position. If any new items // Move an existing item to its new position. If any new items
// are behind it, push the item range to itemRanges. // are behind it, push the item range to itemRanges.
if (rangeCount > 0) { if (rangeCount > 0) {
@ -1389,14 +1389,14 @@ void KFileItemModel::emitItemsChangedAndTriggerResorting(const KItemRangeList& i
// (b) the successor of the last item is "lessThan" the last item, or // (b) the successor of the last item is "lessThan" the last item, or
// (c) the internal order of the items in the range is incorrect. // (c) the internal order of the items in the range is incorrect.
if (first > 0 if (first > 0
&& lessThan(m_itemData.at(first), m_itemData.at(first - 1))) { && lessThan(m_itemData.at(first), m_itemData.at(first - 1), m_collator)) {
needsResorting = true; needsResorting = true;
} else if (last < count() - 1 } else if (last < count() - 1
&& lessThan(m_itemData.at(last + 1), m_itemData.at(last))) { && lessThan(m_itemData.at(last + 1), m_itemData.at(last), m_collator)) {
needsResorting = true; needsResorting = true;
} else { } else {
for (int index = first; index < last; ++index) { for (int index = first; index < last; ++index) {
if (lessThan(m_itemData.at(index + 1), m_itemData.at(index))) { if (lessThan(m_itemData.at(index + 1), m_itemData.at(index), m_collator)) {
needsResorting = true; needsResorting = true;
break; break;
} }
@ -1583,7 +1583,7 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item,
return data; return data;
} }
bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b, const QCollator& collator) const
{ {
int result = 0; int result = 0;
@ -1628,7 +1628,7 @@ bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const
} }
} }
result = sortRoleCompare(a, b); result = sortRoleCompare(a, b, collator);
return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0; return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0;
} }
@ -1639,24 +1639,36 @@ bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const
class KFileItemModelLessThan class KFileItemModelLessThan
{ {
public: public:
KFileItemModelLessThan(const KFileItemModel* model) : KFileItemModelLessThan(const KFileItemModel* model, const QCollator& collator) :
m_model(model) m_model(model),
m_collator(collator)
{ {
} }
KFileItemModelLessThan(const KFileItemModelLessThan& other) :
m_model(other.m_model),
m_collator()
{
m_collator.setCaseSensitivity(other.m_collator.caseSensitivity());
m_collator.setIgnorePunctuation(other.m_collator.ignorePunctuation());
m_collator.setLocale(other.m_collator.locale());
m_collator.setNumericMode(other.m_collator.numericMode());
}
bool operator()(const KFileItemModel::ItemData* a, const KFileItemModel::ItemData* b) const bool operator()(const KFileItemModel::ItemData* a, const KFileItemModel::ItemData* b) const
{ {
return m_model->lessThan(a, b); return m_model->lessThan(a, b, m_collator);
} }
private: private:
const KFileItemModel* m_model; const KFileItemModel* m_model;
QCollator m_collator;
}; };
void KFileItemModel::sort(QList<KFileItemModel::ItemData*>::iterator begin, void KFileItemModel::sort(QList<KFileItemModel::ItemData*>::iterator begin,
QList<KFileItemModel::ItemData*>::iterator end) const QList<KFileItemModel::ItemData*>::iterator end) const
{ {
KFileItemModelLessThan lessThan(this); KFileItemModelLessThan lessThan(this, m_collator);
if (m_sortRole == NameRole) { if (m_sortRole == NameRole) {
// Sorting by name can be expensive, in particular if natural sorting is // Sorting by name can be expensive, in particular if natural sorting is
@ -1671,7 +1683,7 @@ void KFileItemModel::sort(QList<KFileItemModel::ItemData*>::iterator begin,
} }
} }
int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b, const QCollator& collator) const
{ {
const KFileItem& itemA = a->item; const KFileItem& itemA = a->item;
const KFileItem& itemB = b->item; const KFileItem& itemB = b->item;
@ -1734,8 +1746,8 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const
case ImageSizeRole: { case ImageSizeRole: {
// Alway use a natural comparing to interpret the numbers of a string like // Alway use a natural comparing to interpret the numbers of a string like
// "1600 x 1200" for having a correct sorting. // "1600 x 1200" for having a correct sorting.
result = m_collator.compare(a->values.value("imageSize").toString(), result = collator.compare(a->values.value("imageSize").toString(),
b->values.value("imageSize").toString()); b->values.value("imageSize").toString());
break; break;
} }
@ -1754,13 +1766,13 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const
} }
// Fallback #1: Compare the text of the items // Fallback #1: Compare the text of the items
result = stringCompare(itemA.text(), itemB.text()); result = stringCompare(itemA.text(), itemB.text(), collator);
if (result != 0) { if (result != 0) {
return result; return result;
} }
// Fallback #2: KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used // Fallback #2: KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used
result = stringCompare(itemA.name(), itemB.name()); result = stringCompare(itemA.name(), itemB.name(), collator);
if (result != 0) { if (result != 0) {
return result; return result;
} }
@ -1771,14 +1783,14 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const
return QString::compare(itemA.url().url(), itemB.url().url(), Qt::CaseSensitive); return QString::compare(itemA.url().url(), itemB.url().url(), Qt::CaseSensitive);
} }
int KFileItemModel::stringCompare(const QString& a, const QString& b) const int KFileItemModel::stringCompare(const QString& a, const QString& b, const QCollator& collator) const
{ {
if (m_naturalSorting) { if (m_naturalSorting) {
return m_collator.compare(a, b); return collator.compare(a, b);
} }
const int result = QString::compare(a, b, m_collator.caseSensitivity()); const int result = QString::compare(a, b, collator.caseSensitivity());
if (result != 0 || m_collator.caseSensitivity() == Qt::CaseSensitive) { if (result != 0 || collator.caseSensitivity() == Qt::CaseSensitive) {
// Only return the result, if the strings are not equal. If they are equal by a case insensitive // Only return the result, if the strings are not equal. If they are equal by a case insensitive
// comparison, still a deterministic sort order is required. A case sensitive // comparison, still a deterministic sort order is required. A case sensitive
// comparison is done as fallback. // comparison is done as fallback.
@ -2212,7 +2224,7 @@ bool KFileItemModel::isConsistent() const
} }
// Check if the items are sorted correctly. // Check if the items are sorted correctly.
if (i > 0 && !lessThan(m_itemData.at(i - 1), m_itemData.at(i))) { if (i > 0 && !lessThan(m_itemData.at(i - 1), m_itemData.at(i), m_collator)) {
qWarning() << "The order of items" << i - 1 << "and" << i << "is wrong:" qWarning() << "The order of items" << i - 1 << "and" << i << "is wrong:"
<< fileItem(i - 1) << fileItem(i); << fileItem(i - 1) << fileItem(i);
return false; return false;

View file

@ -362,7 +362,7 @@ private:
* @return True if the item-data \a a should be ordered before the item-data * @return True if the item-data \a a should be ordered before the item-data
* \b. The item-data may have different parent-items. * \b. The item-data may have different parent-items.
*/ */
bool lessThan(const ItemData* a, const ItemData* b) const; bool lessThan(const ItemData* a, const ItemData* b, const QCollator& collator) const;
/** /**
* Sorts the items between \a begin and \a end using the comparison * Sorts the items between \a begin and \a end using the comparison
@ -375,9 +375,9 @@ private:
* the passed item-data using m_sortRole as criteria. Both items must * the passed item-data using m_sortRole as criteria. Both items must
* have the same parent item, otherwise the comparison will be wrong. * have the same parent item, otherwise the comparison will be wrong.
*/ */
int sortRoleCompare(const ItemData* a, const ItemData* b) const; int sortRoleCompare(const ItemData* a, const ItemData* b, const QCollator& collator) const;
int stringCompare(const QString& a, const QString& b) const; int stringCompare(const QString& a, const QString& b, const QCollator& collator) const;
bool useMaximumUpdateInterval() const; bool useMaximumUpdateInterval() const;