Commit 7f647a52 authored by Frédéric Brière's avatar Frédéric Brière
Browse files

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
parent 7579f705
......@@ -37,13 +37,9 @@ void Kanji::addRadical( const QString &it )
components += it;
}
void Kanji::calculateStrokes( const QList<Radical> &lookup )
void Kanji::setStrokes( unsigned int strokes )
{
strokeCount = 0;
foreach( const QString &it, components )
{
strokeCount += lookup.value( lookup.indexOf( Radical( it ) ) ).strokes();
}
strokeCount = strokes;
}
const QSet<QString>& Kanji::getRadicals() const
......
......@@ -35,7 +35,7 @@ class Kanji : public QString
, const QSet<QString> &radicals );
void addRadical( const QString &it );
void calculateStrokes( const QList<Radical> &lookup );
void setStrokes( unsigned int strokes );
const QSet<QString>& getRadicals() const;
unsigned int strokes() const;
......
......@@ -18,6 +18,8 @@
* Boston, MA 02110-1301, USA. *
*****************************************************************************/
#include "DictKanjidic/dictfilekanjidic.h"
#include "kitenmacros.h"
#include "radicalfile.h"
#include <QFile>
......@@ -25,9 +27,13 @@
#include <QTextCodec>
#include <QTextStream>
RadicalFile::RadicalFile( QString &radkfile )
RadicalFile::RadicalFile( QString &radkfile, const QString &kanjidic )
{
loadRadicalFile( radkfile );
if( ! kanjidic.isEmpty() )
{
loadKanjidic( kanjidic );
}
}
QSet<Kanji> RadicalFile::kanjiContainingRadicals( QSet<QString> &radicallist ) const
......@@ -121,12 +127,41 @@ bool RadicalFile::loadRadicalFile( QString &radkfile )
{
m_kanji.insert( it.key()
, Kanji( it.key()
, it.value() ) )->calculateStrokes( m_radicals.values() );
, it.value() ) );
}
f.close();
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> *result = new QMultiMap<int, Radical>();
......
......@@ -33,10 +33,11 @@
class RadicalFile
{
public:
explicit RadicalFile( QString &radkfile );
explicit RadicalFile( QString &radkfile, const QString &kanjidic = QString() );
QSet<Kanji> kanjiContainingRadicals( QSet<QString> &radicalList ) const;
bool loadRadicalFile( QString &radkfile );
bool loadKanjidic( const QString &kanjidic );
QMultiMap<int,Radical> *mapRadicalsByStrokes() const;
QSet<QString> radicalsInKanji( QSet<Kanji> &kanjiList ) const;
......
......@@ -44,6 +44,7 @@ RadSelectView::RadSelectView( QWidget *parent )
//Setup the ui from the .ui file
setupUi( this );
m_radicalInfo = nullptr;
//Load the radical information
QString radkfilename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kiten/radkfile"));
if ( radkfilename.isNull() )
......@@ -54,7 +55,17 @@ RadSelectView::RadSelectView( QWidget *parent )
}
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
......
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