File indexing completed on 2022-09-27 12:25:40

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool
0003     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 /*
0009  *    Future Plans:
0010  *  Build a proper exception handling framework
0011  */
0012 
0013 #include "radselectview.h"
0014 
0015 #include "buttongrid.h"
0016 #include "radselectconfig.h"
0017 
0018 #include <KMessageBox>
0019 #include <KLocalizedString>
0020 
0021 #include <QApplication>
0022 #include <QClipboard>
0023 #include <QListWidget>
0024 #include <QListWidgetItem>
0025 #include <QPushButton>
0026 #include <QString>
0027 
0028 RadSelectView::RadSelectView( QWidget *parent )
0029 : QWidget( parent )
0030 {
0031   //Setup the ui from the .ui file
0032   setupUi( this );
0033   m_radicalInfo = nullptr;
0034 
0035   //Load the radical information
0036   QString radkfilename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kiten/radkfile"));
0037   if ( radkfilename.isNull() )
0038   {
0039     KMessageBox::error( nullptr, i18n( "Kanji radical information does not seem to "
0040                                  "be installed (file kiten/radkfile), this "
0041                                  "file is required for this app to function." ) );
0042   }
0043   else
0044   {
0045     QString kanjidicname = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kiten/kanjidic");
0046     if ( kanjidicname.isNull() )
0047     {
0048       KMessageBox::error( nullptr, i18n( "Kanji dictionary does not seem to "
0049                                    "be installed (file kiten/kanjidic), stroke "
0050                                    "count information will be unavailable." ) );
0051       strokes_low->setEnabled( false );
0052       strokes_high->setEnabled( false );
0053     }
0054 
0055     m_radicalInfo = new RadicalFile( radkfilename, kanjidicname );
0056   }
0057 
0058   //Configure the scrolling area
0059   m_buttongrid = new ButtonGrid( radical_box, m_radicalInfo );
0060   radical_box->setWidget( m_buttongrid );
0061   radical_box->setWidgetResizable( true );
0062 
0063   //Configure the stroke selectors
0064   strokes_low->setSpecialValueText(  i18nc( "Minimum number of strokes for a kanji", "Min" ) );
0065   strokes_high->setSpecialValueText( i18nc( "Maximum number of strokes for a kanji", "Max") );
0066 
0067   //== Now we connect all our signals ==
0068   //Connect our radical grid to our adding method
0069   connect( m_buttongrid, &ButtonGrid::possibleKanji,
0070                    this,   &RadSelectView::listPossibleKanji );
0071   //Connect the results selection to our logic
0072   connect( selected_radicals, &QListWidget::itemClicked,
0073                         this,   &RadSelectView::kanjiClicked );
0074   connect( selected_radicals, &QListWidget::itemDoubleClicked,
0075                         this,   &RadSelectView::kanjiDoubleClicked );
0076   //Connect our stroke limit actions
0077   connect( strokes_low, SIGNAL(valueChanged(int)),
0078                   this,   SLOT(strokeLimitChanged(int)) );
0079   connect( strokes_high, SIGNAL(valueChanged(int)),
0080                    this,   SLOT(strokeLimitChanged(int)) );
0081   //Connect statusbar updates
0082   connect( m_buttongrid, &ButtonGrid::signalChangeStatusbar,
0083                    this, &RadSelectView::signalChangeStatusbar );
0084 
0085   //Connect our clear button
0086   connect( clear_button, &QAbstractButton::clicked,
0087                    this,   &RadSelectView::clearSearch );
0088 
0089   // copy text from copied_line (QLineEdit) to clipboard
0090   connect( copy_button, &QAbstractButton::clicked,
0091                   this,   &RadSelectView::toClipboard );
0092 
0093   loadSettings();
0094 }
0095 
0096 RadSelectView::~RadSelectView()
0097 {
0098   delete m_radicalInfo;
0099 }
0100 
0101 void RadSelectView::changedSearch()
0102 {
0103   Q_EMIT searchModified();
0104 }
0105 
0106 void RadSelectView::clearSearch()
0107 {
0108   m_possibleKanji.clear();
0109   m_buttongrid->clearSelections();
0110   selected_radicals->clear();
0111   strokes_low->setValue( 0 );
0112   strokes_high->setValue( 0 );
0113 }
0114 
0115 void RadSelectView::kanjiClicked( QListWidgetItem *item )
0116 {
0117   QString allText = i18nc( "@item:inlist all matches should be found", "(ALL)" );
0118   QString finalText;
0119   if( item->text() == allText )
0120   {
0121     foreach(  QListWidgetItem *listItem
0122             , selected_radicals->findItems( "*", Qt::MatchWildcard ) )
0123     {
0124       if( listItem->text() != allText )
0125       {
0126         finalText += listItem->text();
0127       }
0128     }
0129   }
0130   else
0131   {
0132     finalText = item->text();
0133   }
0134 
0135   QApplication::clipboard()->setText( finalText, QClipboard::Selection );
0136 }
0137 
0138 void RadSelectView::kanjiDoubleClicked( QListWidgetItem *item )
0139 {
0140   QString str = copied_line->text();
0141   int pos = copied_line->cursorPosition();
0142   str.insert( pos, item->text() );
0143   copied_line->setText( str );
0144   copied_line->setCursorPosition( pos + 1 );
0145 }
0146 
0147 void RadSelectView::listPossibleKanji( const QList<Kanji>& list )
0148 {
0149   unsigned int low  = strokes_low->value();
0150   unsigned int high = strokes_high->value();
0151 
0152   //Modification of the stroke boxes
0153   //We want to move the max value to something reasonable...
0154   //for example, 5 above the current max value so that rollover
0155   //works nicely. We want to reset to all if the list is empty.
0156   //And we also don't limit if the current value is higher than
0157   //max value in the list
0158   int newMax = 20;
0159   if( list.count() < 1
0160     || list.last().strokes() < low
0161     || list.last().strokes() + 5 < high )
0162   {
0163     newMax = 99;
0164   }
0165   else
0166   {
0167     newMax = list.last().strokes() + 5;
0168   }
0169 
0170   strokes_low->setMaximum( newMax );
0171   strokes_high->setMaximum( newMax );
0172   if( high == 0 )
0173   {
0174     high = 99;
0175   }
0176 
0177   selected_radicals->clear();
0178   foreach( const Kanji &it, list )
0179   {
0180     if( low <= it.strokes() && it.strokes() <= high )
0181     {
0182       new QListWidgetItem( (QString)it, selected_radicals );
0183     }
0184   }
0185 
0186   m_possibleKanji = list;
0187 
0188   Q_EMIT searchModified();
0189 }
0190 
0191 void RadSelectView::loadKanji( QString &kanji )
0192 {
0193   Q_UNUSED( kanji );
0194   //TODO: loadKanji method
0195 }
0196 
0197 void RadSelectView::loadRadicals(  const QString &radicals
0198                                  , int strokeMin
0199                                  , int strokeMax )
0200 {
0201   Q_UNUSED( radicals );
0202   Q_UNUSED( strokeMin );
0203   Q_UNUSED( strokeMax );
0204   //TODO: loadRadicals method
0205   Q_EMIT searchModified();
0206 }
0207 
0208 void RadSelectView::loadSettings()
0209 {
0210   //TODO: Add preferences for what to do on single/double click
0211   //Suggested options: Lookup in Kiten, Add to Search Bar, Copy to Clipboard
0212   selected_radicals->setFont( RadSelectConfigSkeleton::self()->resultListFont() );
0213   m_buttongrid->setFont( RadSelectConfigSkeleton::self()->font() );
0214 
0215   m_buttongrid->setSortByFrequency( RadSelectConfigSkeleton::self()->sortByFrequency() );
0216 }
0217 
0218 void RadSelectView::strokeLimitChanged( int newvalue )
0219 {
0220   int low  = strokes_low->value();
0221   int high = strokes_high->value();
0222   if( low > high )
0223   {
0224     if( low == newvalue )
0225     {
0226       strokes_high->setValue( newvalue );
0227     }
0228     else
0229     {
0230       strokes_low->setValue( newvalue );
0231     }
0232   }
0233 
0234   //This will force reevaluation of the list if it's needed
0235   QList<Kanji> newList = m_possibleKanji;
0236   listPossibleKanji( newList );
0237 }
0238 
0239 void RadSelectView::toClipboard()
0240 {
0241   QClipboard *cb = QApplication::clipboard();
0242   cb->setText( copied_line->text(), QClipboard::Clipboard );
0243   cb->setText( copied_line->text(), QClipboard::Selection );
0244 }
0245 
0246