Warning, file /education/kiten/radselect/radselectview.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 <KLocalizedString>
0019 #include <KMessageBox>
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         KMessageBox::error(nullptr,
0039                            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     } else {
0043         QString kanjidicname = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kiten/kanjidic");
0044         if (kanjidicname.isNull()) {
0045             KMessageBox::error(nullptr,
0046                                i18n("Kanji dictionary does not seem to "
0047                                     "be installed (file kiten/kanjidic), stroke "
0048                                     "count information will be unavailable."));
0049             strokes_low->setEnabled(false);
0050             strokes_high->setEnabled(false);
0051         }
0052 
0053         m_radicalInfo = new RadicalFile(radkfilename, kanjidicname);
0054     }
0055 
0056     // Configure the scrolling area
0057     m_buttongrid = new ButtonGrid(radical_box, m_radicalInfo);
0058     radical_box->setWidget(m_buttongrid);
0059     radical_box->setWidgetResizable(true);
0060 
0061     // Configure the stroke selectors
0062     strokes_low->setSpecialValueText(i18nc("Minimum number of strokes for a kanji", "Min"));
0063     strokes_high->setSpecialValueText(i18nc("Maximum number of strokes for a kanji", "Max"));
0064 
0065     //== Now we connect all our signals ==
0066     // Connect our radical grid to our adding method
0067     connect(m_buttongrid, &ButtonGrid::possibleKanji, this, &RadSelectView::listPossibleKanji);
0068     // Connect the results selection to our logic
0069     connect(selected_radicals, &QListWidget::itemClicked, this, &RadSelectView::kanjiClicked);
0070     connect(selected_radicals, &QListWidget::itemDoubleClicked, this, &RadSelectView::kanjiDoubleClicked);
0071     // Connect our stroke limit actions
0072     connect(strokes_low, SIGNAL(valueChanged(int)), this, SLOT(strokeLimitChanged(int)));
0073     connect(strokes_high, SIGNAL(valueChanged(int)), this, SLOT(strokeLimitChanged(int)));
0074     // Connect statusbar updates
0075     connect(m_buttongrid, &ButtonGrid::signalChangeStatusbar, this, &RadSelectView::signalChangeStatusbar);
0076 
0077     // Connect our clear button
0078     connect(clear_button, &QAbstractButton::clicked, this, &RadSelectView::clearSearch);
0079 
0080     // copy text from copied_line (QLineEdit) to clipboard
0081     connect(copy_button, &QAbstractButton::clicked, this, &RadSelectView::toClipboard);
0082 
0083     loadSettings();
0084 }
0085 
0086 RadSelectView::~RadSelectView()
0087 {
0088     delete m_radicalInfo;
0089 }
0090 
0091 void RadSelectView::changedSearch()
0092 {
0093     Q_EMIT searchModified();
0094 }
0095 
0096 void RadSelectView::clearSearch()
0097 {
0098     m_possibleKanji.clear();
0099     m_buttongrid->clearSelections();
0100     selected_radicals->clear();
0101     strokes_low->setValue(0);
0102     strokes_high->setValue(0);
0103 }
0104 
0105 void RadSelectView::kanjiClicked(QListWidgetItem *item)
0106 {
0107     QString allText = i18nc("@item:inlist all matches should be found", "(ALL)");
0108     QString finalText;
0109     if (item->text() == allText) {
0110         foreach (QListWidgetItem *listItem, selected_radicals->findItems("*", Qt::MatchWildcard)) {
0111             if (listItem->text() != allText) {
0112                 finalText += listItem->text();
0113             }
0114         }
0115     } else {
0116         finalText = item->text();
0117     }
0118 
0119     QApplication::clipboard()->setText(finalText, QClipboard::Selection);
0120 }
0121 
0122 void RadSelectView::kanjiDoubleClicked(QListWidgetItem *item)
0123 {
0124     QString str = copied_line->text();
0125     int pos = copied_line->cursorPosition();
0126     str.insert(pos, item->text());
0127     copied_line->setText(str);
0128     copied_line->setCursorPosition(pos + 1);
0129 }
0130 
0131 void RadSelectView::listPossibleKanji(const QList<Kanji> &list)
0132 {
0133     unsigned int low = strokes_low->value();
0134     unsigned int high = strokes_high->value();
0135 
0136     // Modification of the stroke boxes
0137     // We want to move the max value to something reasonable...
0138     // for example, 5 above the current max value so that rollover
0139     // works nicely. We want to reset to all if the list is empty.
0140     // And we also don't limit if the current value is higher than
0141     // max value in the list
0142     int newMax = 20;
0143     if (list.count() < 1 || list.last().strokes() < low || list.last().strokes() + 5 < high) {
0144         newMax = 99;
0145     } else {
0146         newMax = list.last().strokes() + 5;
0147     }
0148 
0149     strokes_low->setMaximum(newMax);
0150     strokes_high->setMaximum(newMax);
0151     if (high == 0) {
0152         high = 99;
0153     }
0154 
0155     selected_radicals->clear();
0156     foreach (const Kanji &it, list) {
0157         if (low <= it.strokes() && it.strokes() <= high) {
0158             new QListWidgetItem((QString)it, selected_radicals);
0159         }
0160     }
0161 
0162     m_possibleKanji = list;
0163 
0164     Q_EMIT searchModified();
0165 }
0166 
0167 void RadSelectView::loadKanji(QString &kanji)
0168 {
0169     Q_UNUSED(kanji);
0170     // TODO: loadKanji method
0171 }
0172 
0173 void RadSelectView::loadRadicals(const QString &radicals, int strokeMin, int strokeMax)
0174 {
0175     Q_UNUSED(radicals);
0176     Q_UNUSED(strokeMin);
0177     Q_UNUSED(strokeMax);
0178     // TODO: loadRadicals method
0179     Q_EMIT searchModified();
0180 }
0181 
0182 void RadSelectView::loadSettings()
0183 {
0184     // TODO: Add preferences for what to do on single/double click
0185     // Suggested options: Lookup in Kiten, Add to Search Bar, Copy to Clipboard
0186     selected_radicals->setFont(RadSelectConfigSkeleton::self()->resultListFont());
0187     m_buttongrid->setFont(RadSelectConfigSkeleton::self()->font());
0188 
0189     m_buttongrid->setSortByFrequency(RadSelectConfigSkeleton::self()->sortByFrequency());
0190 }
0191 
0192 void RadSelectView::strokeLimitChanged(int newvalue)
0193 {
0194     int low = strokes_low->value();
0195     int high = strokes_high->value();
0196     if (low > high) {
0197         if (low == newvalue) {
0198             strokes_high->setValue(newvalue);
0199         } else {
0200             strokes_low->setValue(newvalue);
0201         }
0202     }
0203 
0204     // This will force reevaluation of the list if it's needed
0205     QList<Kanji> newList = m_possibleKanji;
0206     listPossibleKanji(newList);
0207 }
0208 
0209 void RadSelectView::toClipboard()
0210 {
0211     QClipboard *cb = QApplication::clipboard();
0212     cb->setText(copied_line->text(), QClipboard::Clipboard);
0213     cb->setText(copied_line->text(), QClipboard::Selection);
0214 }
0215 
0216 #include "moc_radselectview.cpp"