Commit c46baf4b authored by Frédéric Brière's avatar Frédéric Brière Committed by Albert Astals Cid

radselect: Properly fetch kanji stroke count from kanjidic

The stroke count of a kanji cannot be calculated from the mere list of
the radicals it contains; the correct value can only be retrieved from
kanjidic.

BUG: 385070
(cherry picked from commit 7f647a52)
parent 8c29ed6e
...@@ -37,13 +37,9 @@ void Kanji::addRadical( const QString &it ) ...@@ -37,13 +37,9 @@ void Kanji::addRadical( const QString &it )
components += it; components += it;
} }
void Kanji::calculateStrokes( const QList<Radical> &lookup ) void Kanji::setStrokes( unsigned int strokes )
{ {
strokeCount = 0; strokeCount = strokes;
foreach( const QString &it, components )
{
strokeCount += lookup.value( lookup.indexOf( Radical( it ) ) ).strokes();
}
} }
const QSet<QString>& Kanji::getRadicals() const const QSet<QString>& Kanji::getRadicals() const
......
...@@ -35,7 +35,7 @@ class Kanji : public QString ...@@ -35,7 +35,7 @@ class Kanji : public QString
, const QSet<QString> &radicals ); , const QSet<QString> &radicals );
void addRadical( const QString &it ); void addRadical( const QString &it );
void calculateStrokes( const QList<Radical> &lookup ); void setStrokes( unsigned int strokes );
const QSet<QString>& getRadicals() const; const QSet<QString>& getRadicals() const;
unsigned int strokes() const; unsigned int strokes() const;
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
* Boston, MA 02110-1301, USA. * * Boston, MA 02110-1301, USA. *
*****************************************************************************/ *****************************************************************************/
#include "DictKanjidic/dictfilekanjidic.h"
#include "kitenmacros.h"
#include "radicalfile.h" #include "radicalfile.h"
#include <QFile> #include <QFile>
...@@ -25,9 +27,13 @@ ...@@ -25,9 +27,13 @@
#include <QTextCodec> #include <QTextCodec>
#include <QTextStream> #include <QTextStream>
RadicalFile::RadicalFile( QString &radkfile ) RadicalFile::RadicalFile( QString &radkfile, const QString &kanjidic )
{ {
loadRadicalFile( radkfile ); loadRadicalFile( radkfile );
if( ! kanjidic.isEmpty() )
{
loadKanjidic( kanjidic );
}
} }
QSet<Kanji> RadicalFile::kanjiContainingRadicals( QSet<QString> &radicallist ) const QSet<Kanji> RadicalFile::kanjiContainingRadicals( QSet<QString> &radicallist ) const
...@@ -121,12 +127,41 @@ bool RadicalFile::loadRadicalFile( QString &radkfile ) ...@@ -121,12 +127,41 @@ bool RadicalFile::loadRadicalFile( QString &radkfile )
{ {
m_kanji.insert( it.key() m_kanji.insert( it.key()
, Kanji( it.key() , Kanji( it.key()
, it.value() ) )->calculateStrokes( m_radicals.values() ); , it.value() ) );
} }
f.close(); f.close();
return true; return true;
} }
// Mostly copied from KanjiBrowser::loadKanji()
bool RadicalFile::loadKanjidic( const QString &kanjidic )
{
DictFileKanjidic dictFileKanjidic;
dictFileKanjidic.loadSettings();
dictFileKanjidic.loadDictionary( kanjidic, KANJIDIC );
QRegExp strokeMatch( "^S\\d+" );
foreach( const QString &line, dictFileKanjidic.dumpDictionary() )
{
const QString kanji = line[ 0 ];
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
QStringList strokesSection = line.split( " ", QString::SkipEmptyParts )
#else
QStringList strokesSection = line.split( " ", Qt::SkipEmptyParts )
#endif
.filter( strokeMatch );
unsigned int strokes = strokesSection.first().remove( 0, 1 ).toInt();
if( m_kanji.contains( kanji ) ) {
m_kanji[ kanji ].setStrokes( strokes );
}
}
return true;
}
QMultiMap<int,Radical>* RadicalFile::mapRadicalsByStrokes() const QMultiMap<int,Radical>* RadicalFile::mapRadicalsByStrokes() const
{ {
QMultiMap<int, Radical> *result = new QMultiMap<int, Radical>(); QMultiMap<int, Radical> *result = new QMultiMap<int, Radical>();
......
...@@ -33,10 +33,11 @@ ...@@ -33,10 +33,11 @@
class RadicalFile class RadicalFile
{ {
public: public:
explicit RadicalFile( QString &radkfile ); explicit RadicalFile( QString &radkfile, const QString &kanjidic = QString() );
QSet<Kanji> kanjiContainingRadicals( QSet<QString> &radicalList ) const; QSet<Kanji> kanjiContainingRadicals( QSet<QString> &radicalList ) const;
bool loadRadicalFile( QString &radkfile ); bool loadRadicalFile( QString &radkfile );
bool loadKanjidic( const QString &kanjidic );
QMultiMap<int,Radical> *mapRadicalsByStrokes() const; QMultiMap<int,Radical> *mapRadicalsByStrokes() const;
QSet<QString> radicalsInKanji( QSet<Kanji> &kanjiList ) const; QSet<QString> radicalsInKanji( QSet<Kanji> &kanjiList ) const;
......
...@@ -44,6 +44,7 @@ RadSelectView::RadSelectView( QWidget *parent ) ...@@ -44,6 +44,7 @@ RadSelectView::RadSelectView( QWidget *parent )
//Setup the ui from the .ui file //Setup the ui from the .ui file
setupUi( this ); setupUi( this );
m_radicalInfo = nullptr; m_radicalInfo = nullptr;
//Load the radical information //Load the radical information
QString radkfilename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kiten/radkfile")); QString radkfilename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kiten/radkfile"));
if ( radkfilename.isNull() ) if ( radkfilename.isNull() )
...@@ -54,7 +55,17 @@ RadSelectView::RadSelectView( QWidget *parent ) ...@@ -54,7 +55,17 @@ RadSelectView::RadSelectView( QWidget *parent )
} }
else else
{ {
m_radicalInfo = new RadicalFile( radkfilename ); QString kanjidicname = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kiten/kanjidic");
if ( kanjidicname.isNull() )
{
KMessageBox::error( nullptr, i18n( "Kanji dictionary does not seem to "
"be installed (file kiten/kanjidic), stroke "
"count information will be unavailable." ) );
strokes_low->setEnabled( false );
strokes_high->setEnabled( false );
}
m_radicalInfo = new RadicalFile( radkfilename, kanjidicname );
} }
//Configure the scrolling area //Configure the scrolling area
......
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