Commit 28d3e11f authored by Michał Poteralski's avatar Michał Poteralski Committed by Jarosław Staniek

Fix text sorting in tabular views

Use ICU-based collator for sorting text, the same that is used by an SQLite
extension.

BUG:338808
REVIEW:120274
FIXED-IN:2.8.6
parent a83877b6
......@@ -18,6 +18,7 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/parser
${KOPLUGIN_INCLUDES}
${KDE4_INCLUDES}
${ICU_INCLUDE_DIRS}
)
add_subdirectory( drivers )
......@@ -92,7 +93,7 @@ set(calligradb_LIB_SRCS
kde4_add_library(calligradb SHARED ${calligradb_LIB_SRCS})
target_link_libraries(calligradb koplugin ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY}
${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS})
${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS} ${ICU_I18N_LIBRARY})
target_link_libraries(calligradb LINK_INTERFACE_LIBRARIES koplugin ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY}
${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS})
......
......@@ -2,6 +2,7 @@
Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
Copyright (C) 2003-2007 Jarosław Staniek <staniek@kde.org>
Copyright (C) 2014 Michał Poteralski <michalpoteralskikde@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -29,6 +30,7 @@
#include "cursor.h"
#include "utils.h"
#include "error.h"
#include <unicode/coll.h>
#include <kdebug.h>
#include <klocale.h>
......@@ -44,6 +46,37 @@ unsigned short charTable[] = {
//-------------------------------
class CollatorInstance
{
public:
CollatorInstance() {
UErrorCode status = U_ZERO_ERROR;
m_collator = Collator::createInstance(status);
if (U_FAILURE(status)) {
kWarning() << "Could not create instance of collator: " << status;
m_collator = NULL;
}
// enable normalization by default
m_collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
if (U_FAILURE(status))
kWarning() << "Could not set collator attribute: " << status;
}
Collator* getCollator() {
return m_collator;
}
~CollatorInstance() {
delete m_collator;
}
private:
Collator *m_collator;
};
K_GLOBAL_STATIC(CollatorInstance, _collator)
//! @internal A functor used in qSort() in order to sort by a given column
class LessThanFunctor
{
......@@ -125,6 +158,13 @@ private:
return false;
}
static bool cmpStringWithCollator(const QVariant& left, const QVariant& right) {
const QString &as = left.toString();
const QString &bs = right.toString();
return (Collator::LESS == _collator->getCollator()->compare((const UChar *)as.constData(), as.size(),
(const UChar *)bs.constData(), bs.size())) ? true : false;
}
//! Compare function for BLOB data (QByteArray). Uses size as the weight.
static bool cmpBLOB(const QVariant& left, const QVariant& right) {
return left.toByteArray().size() < right.toByteArray().size();
......@@ -162,8 +202,13 @@ public:
else if (t == Field::BLOB)
//! @todo allow users to define BLOB sorting function?
m_lessThanFunction = &cmpBLOB;
else
m_lessThanFunction = &cmpString; //anything else
else { // anything else
// check if CollatorInstance is not destroyed and has valid collator
if (!_collator.isDestroyed() && _collator->getCollator())
m_lessThanFunction = &cmpStringWithCollator;
else
m_lessThanFunction = &cmpString;
}
}
void setAscendingOrder(bool ascending) {
......
......@@ -2,6 +2,7 @@
Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
Copyright (C) 2003-2007 Jarosław Staniek <staniek@kde.org>
Copyright (C) 2014 Michał Poteralski <michalpoteralskikde@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment