[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Patch for kcontrol/locale
From:       Antonio Larrosa =?iso-8859-1?q?Jim=E9nez?= <larrosa () kde ! org>
Date:       2002-02-23 20:36:17
[Download RAW message or body]

Hi,

There's a problem in kcontrol regarding the parsing of locales.
I saw it when using the spanish translations, but it can also happen in 
other languages.
Suppose that "MONTH" is translated as "MES" and should be replaced by 
"Febrero" (February) and then "SHORTMONTH" is translated as "MESCORTO" and 
should be replaced by "Feb".

Currently, when kcmlocale gets "MESCORTO", it is translated to 
"FebreroCORTO" :-)

My patch changes the use of QMap<QChar, QString> to QValueList<QStringList>
and then for each element of the valuelist, it stores the key in each 
item[0] and the value in each item[1]. This is done to be able to sort the 
list of elements as desired and iterate through them correctly (first for 
longer elements and then for shorter ones).
Then, in a separated file, I implemented
operator<(const QStringList &, const QStringList &);
Which compares the i-th string of the stringLists (in this case, the 
strings in [1]).

Note that this way to sort sets of strings may be useful in other 
applications (I remember doing a similar patch for the dialog that shows 
the icons in konqueror, but I don't know if it's still used). In any case, 
I may clean it a bit (I added a static method to that class in order not 
to leave it in the global namespace although the method doesn't really fit 
there completely) and add it to kdecore (after 3.0 of course) if you think 
it would be nice.

Anyway, can I commit this to kcontrol?

Greetings,

--
Antonio Larrosa Jimenez
KDE Core developer  - larrosa@kde.org
http://perso.wanadoo.es/antlarr
KDE - The development framework of the future, today.


["sortstringlist.cpp" (text/x-c++src)]

#include "sortstringlist.h"

int SortQStringList::field=0;
bool SortQStringList::ascending=true;
  
bool operator<( const QStringList &s1, const QStringList &s2 )
{
  bool r=s1[SortQStringList::field]<s2[SortQStringList::field];
  return (SortQStringList::ascending) ? r : !r ;
}
  
bool operator>( const QStringList &s1, const QStringList &s2 )
{
  bool r=s1[SortQStringList::field]>s2[SortQStringList::field];
  return (SortQStringList::ascending) ? r : !r ;
}
bool operator<=( const QStringList &s1, const QStringList &s2 )
{
  bool r=s1[SortQStringList::field]<=s2[SortQStringList::field];
  return (SortQStringList::ascending) ? r : !r ;
}
bool operator>=( const QStringList &s1, const QStringList &s2 )
{
  bool r=s1[SortQStringList::field]>=s2[SortQStringList::field];
  return (SortQStringList::ascending) ? r : !r ;
}

QStringList SortQStringList::find( const QValueList <QStringList> &list, int field, const QString &s)
{
  for ( QValueList<QStringList>::ConstIterator it = list.begin();
      it != list.end();
      ++it )
    if ((*it)[field]==s) return (*it);

  return QStringList();
}
  

["sortstringlist.h" (text/x-chdr)]

#include <qstringlist.h>
#include <qvaluevector.h>

/**
 * Namespace class used to store some settings for the sorting of QStringLists
 */
class SortQStringList
{
  private:
  SortQStringList() {};

  public:

  /**
   * Search for string s in the field "field" on each QStringList on list and
   * returns the first StringList that matches
   */
  static QStringList find( const QValueList <QStringList> &list, int field, const QString &s);

  /**
   * Remember to always set the field and ascending variables before calling
   * qHeapSort(valueList);
   * You should _never_ use the default values.
   */
  static int field;
  static bool ascending;
};
  
bool operator<( const QStringList &s1, const QStringList &s2 );
bool operator>( const QStringList &s1, const QStringList &s2 );
bool operator<=( const QStringList &s1, const QStringList &s2 );
bool operator>=( const QStringList &s1, const QStringList &s2 );

  

["locale.diff" (text/x-diff)]

? sortstringlist.cpp
? sortstringlist.h
Index: Makefile.am
===================================================================
RCS file: /home/kde/kdebase/kcontrol/locale/Makefile.am,v
retrieving revision 1.50
diff -u -p -r1.50 Makefile.am
--- Makefile.am	2002/02/19 13:43:25	1.50
+++ Makefile.am	2002/02/23 20:33:16
@@ -5,7 +5,7 @@ INCLUDES = $(all_includes)
 SUBDIRS = default pics
 
 kde_module_LTLIBRARIES = kcm_locale.la
-kcm_locale_la_SOURCES = main.cpp toplevel.cpp locale.cpp klanguagebutton.cpp \
klocalesample.cpp localenum.cpp localemon.cpp localetime.cpp localeother.cpp \
kmenubutton.cpp +kcm_locale_la_SOURCES = main.cpp toplevel.cpp locale.cpp \
klanguagebutton.cpp klocalesample.cpp localenum.cpp localemon.cpp localetime.cpp \
localeother.cpp kmenubutton.cpp sortstringlist.cpp  
 METASOURCES = AUTO
 noinst_HEADERS = toplevel.h locale.h klanguagebutton.h klocalesample.h menuitem.h \
                localenum.h localemon.h localetime.h localeother.h kmenubutton.h
Index: localetime.cpp
===================================================================
RCS file: /home/kde/kdebase/kcontrol/locale/localetime.cpp,v
retrieving revision 1.44
diff -u -p -r1.44 localetime.cpp
--- localetime.cpp	2001/09/16 11:58:10	1.44
+++ localetime.cpp	2002/02/23 20:33:17
@@ -27,6 +27,7 @@
 #include <qlayout.h>
 #include <qwhatsthis.h>
 #include <qcombobox.h>
+#include <qtl.h>
 
 #include <klocale.h>
 #include <kglobal.h>
@@ -36,44 +37,62 @@
 #include <kstandarddirs.h>
 #include <kdebug.h>
 
+#include "sortstringlist.h"
 #include "toplevel.h"
 #include "localetime.h"
 #include "localetime.moc"
 
-QMap<QChar, QString> KLocaleConfigTime::timeMap() const
+QStringList KLocaleConfigTime::buildStringList(const QString &s1, const QString &s2) \
const  {
-  QMap<QChar, QString> map;
+  QStringList list;
+  list+=s1;
+  list+=s2;
+  return list;
+}
+
+QValueList<QStringList> KLocaleConfigTime::timeMap() const
+{
 
-  map['H'] = m_locale->translate("HH");
-  map['k'] = m_locale->translate("hH");
-  map['I'] = m_locale->translate("PH");
-  map['l'] = m_locale->translate("pH");
-  map['M'] = m_locale->translate("MM");
-  map['S'] = m_locale->translate("SS");
-  map['p'] = m_locale->translate("AMPM");
+  QValueList < QStringList > list;
+  list+=buildStringList("H",m_locale->translate("HH"));
+  list+=buildStringList("k",m_locale->translate("hH"));
+  list+=buildStringList("I",m_locale->translate("PH"));
+  list+=buildStringList("l",m_locale->translate("pH"));
+  list+=buildStringList("M",m_locale->translate("MM"));
+  list+=buildStringList("S",m_locale->translate("SS"));
+  list+=buildStringList("p",m_locale->translate("AMPM"));
+
+  SortQStringList::field=1;
+  SortQStringList::ascending=false;
+  qHeapSort( list );
 
-  return map;
+  return list;
 }
 
-QMap<QChar, QString> KLocaleConfigTime::dateMap() const
+QValueList <QStringList> KLocaleConfigTime::dateMap() const
 {
-  QMap <QChar, QString> map;
+  QStringList t;
 
-  map['Y'] = m_locale->translate("YYYY");
-  map['y'] = m_locale->translate("YY");
-  map['n'] = m_locale->translate("mM");
-  map['m'] = m_locale->translate("MM");
-  map['b'] = m_locale->translate("SHORTMONTH");
-  map['B'] = m_locale->translate("MONTH");
-  map['e'] = m_locale->translate("dD");
-  map['d'] = m_locale->translate("DD");
-  map['a'] = m_locale->translate("SHORTWEEKDAY");
-  map['A'] = m_locale->translate("WEEKDAY");
+  QValueList < QStringList > list;
+  list+=buildStringList("Y",m_locale->translate("YYYY"));
+  list+=buildStringList("y",m_locale->translate("YY"));
+  list+=buildStringList("n",m_locale->translate("mM"));
+  list+=buildStringList("m",m_locale->translate("MM"));
+  list+=buildStringList("b",m_locale->translate("SHORTMONTH"));
+  list+=buildStringList("B",m_locale->translate("MONTH"));
+  list+=buildStringList("e",m_locale->translate("dD"));
+  list+=buildStringList("d",m_locale->translate("DD"));
+  list+=buildStringList("a",m_locale->translate("SHORTWEEKDAY"));
+  list+=buildStringList("A",m_locale->translate("WEEKDAY"));
+
+  SortQStringList::field=1;
+  SortQStringList::ascending=false;
+  qHeapSort( list );
 
-  return map;
+  return list;
 }
 
-QString KLocaleConfigTime::userToStore(const QMap<QChar, QString> & map,
+QString KLocaleConfigTime::userToStore(const QValueList<QStringList> & list,
 		    const QString & userFormat) const
 {
   QString result;
@@ -81,16 +100,16 @@ QString KLocaleConfigTime::userToStore(c
   for ( uint pos = 0; pos < userFormat.length(); ++pos )
     {
       bool bFound = false;
-      for ( QMap<QChar, QString>::ConstIterator it = map.begin();
-	    it != map.end() && !bFound;
+      for ( QValueList<QStringList>::ConstIterator it = list.begin();
+	    it != list.end() && !bFound;
 	    ++it )
 	{
-	  QString s = it.data();
+	  QString s = (*it)[1];
 
 	  if ( userFormat.mid( pos, s.length() ) == s )
 	    {
 	      result += '%';
-	      result += it.key();
+	      result += (*it)[0];
 
 	      pos += s.length() - 1;
 
@@ -111,7 +130,7 @@ QString KLocaleConfigTime::userToStore(c
   return result;
 }
 
-QString KLocaleConfigTime::storeToUser(const QMap<QChar, QString> & map,
+QString KLocaleConfigTime::storeToUser(const QValueList<QStringList> & list,
 				       const QString & storeFormat) const
 {
   QString result;
@@ -122,9 +141,9 @@ QString KLocaleConfigTime::storeToUser(c
       QChar c = storeFormat.at(pos);
       if ( escaped )
 	{
-	  QMap<QChar, QString>::ConstIterator it = map.find( c );
-	  if ( it != map.end() )
-	    result += it.data();
+	  QStringList it = SortQStringList::find( list, 0, c );
+	  if ( !it[0].isEmpty() )
+	    result += (it)[1];
 	  else
 	    result += c;
 
Index: localetime.h
===================================================================
RCS file: /home/kde/kdebase/kcontrol/locale/localetime.h,v
retrieving revision 1.12
diff -u -p -r1.12 localetime.h
--- localetime.h	2001/08/17 13:39:22	1.12
+++ localetime.h	2002/02/23 20:33:17
@@ -67,13 +67,15 @@ private slots:
   void slotWeekStartsMondayChanged();
 
 private:
-  QMap<QChar, QString> timeMap() const;
-  QMap<QChar, QString> dateMap() const;
+  QValueList<QStringList> timeMap() const;
+  QValueList<QStringList> dateMap() const;
 
-  QString storeToUser(const QMap<QChar, QString> & map,
+  QString storeToUser(const QValueList<QStringList> & map,
 		      const QString & storeFormat) const;
-  QString userToStore(const QMap<QChar, QString> & map,
+  QString userToStore(const QValueList<QStringList> & map,
 		      const QString & userFormat) const;
+  QStringList buildStringList(const QString &s1, const QString &s2) const;
+		      
 
   KLocale *m_locale;
 



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic