radselectview.cpp 7.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*****************************************************************************
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
 *                                                                           *
 * This library is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Library General Public               *
 * License as published by the Free Software Foundation; either              *
 * version 2 of the License, or (at your option) any later version.          *
 *                                                                           *
 * This library is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public License *
 * along with this library; see the file COPYING.LIB.  If not, write to      *
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
 * Boston, MA 02110-1301, USA.                                               *
 *****************************************************************************/
20

21
/*
22
23
24
 *    Future Plans:
 *	Build a proper exception handling framework
 */
Joseph Kerian's avatar
Joseph Kerian committed
25
26

#include "radselectview.h"
27
28

#include "buttongrid.h"
29
#include "radselectconfig.h"
Joseph Kerian's avatar
Joseph Kerian committed
30

31
#include <KMessageBox>
32
#include <KLocalizedString>
33
34
35
36
37
38
39
40
41

#include <QApplication>
#include <QClipboard>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPushButton>
#include <QString>

RadSelectView::RadSelectView( QWidget *parent )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
42
: QWidget( parent )
Joseph Kerian's avatar
Joseph Kerian committed
43
{
44
45
  //Setup the ui from the .ui file
  setupUi( this );
Laurent Montel's avatar
Laurent Montel committed
46
  m_radicalInfo = nullptr;
47

Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
48
  //Load the radical information
Laurent Montel's avatar
Laurent Montel committed
49
  QString radkfilename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kiten/radkfile"));
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
50
51
  if ( radkfilename.isNull() )
  {
Laurent Montel's avatar
Laurent Montel committed
52
    KMessageBox::error( nullptr, i18n( "Kanji radical information does not seem to "
53
54
                                 "be installed (file kiten/radkfile), this "
                                 "file is required for this app to function." ) );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
55
56
57
  }
  else
  {
58
59
60
61
62
63
64
65
66
67
68
    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 );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
69
70
71
  }

  //Configure the scrolling area
72
  m_buttongrid = new ButtonGrid( radical_box, m_radicalInfo );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
73
74
75
76
77
78
79
80
81
  radical_box->setWidget( m_buttongrid );
  radical_box->setWidgetResizable( true );

  //Configure the stroke selectors
  strokes_low->setSpecialValueText(  i18nc( "Minimum number of strokes for a kanji", "Min" ) );
  strokes_high->setSpecialValueText( i18nc( "Maximum number of strokes for a kanji", "Max") );

  //== Now we connect all our signals ==
  //Connect our radical grid to our adding method
Laurent Montel's avatar
Laurent Montel committed
82
83
  connect( m_buttongrid, &ButtonGrid::possibleKanji,
                   this,   &RadSelectView::listPossibleKanji );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
84
  //Connect the results selection to our logic
Laurent Montel's avatar
Laurent Montel committed
85
86
87
88
  connect( selected_radicals, &QListWidget::itemClicked,
                        this,   &RadSelectView::kanjiClicked );
  connect( selected_radicals, &QListWidget::itemDoubleClicked,
                        this,   &RadSelectView::kanjiDoubleClicked );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
89
  //Connect our stroke limit actions
90
91
92
93
  connect( strokes_low, SIGNAL(valueChanged(int)),
                  this,   SLOT(strokeLimitChanged(int)) );
  connect( strokes_high, SIGNAL(valueChanged(int)),
                   this,   SLOT(strokeLimitChanged(int)) );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
94
  //Connect statusbar updates
Laurent Montel's avatar
Laurent Montel committed
95
96
  connect( m_buttongrid, &ButtonGrid::signalChangeStatusbar,
                   this, &RadSelectView::signalChangeStatusbar );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
97
98

  //Connect our clear button
Laurent Montel's avatar
Laurent Montel committed
99
100
  connect( clear_button, &QAbstractButton::clicked,
                   this,   &RadSelectView::clearSearch );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
101
102

  // copy text from copied_line (QLineEdit) to clipboard
Laurent Montel's avatar
Laurent Montel committed
103
104
  connect( copy_button, &QAbstractButton::clicked,
                  this,   &RadSelectView::toClipboard );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
105
106

  loadSettings();
Joseph Kerian's avatar
Joseph Kerian committed
107
108
}

109
RadSelectView::~RadSelectView()
Joseph Kerian's avatar
Joseph Kerian committed
110
{
111
  delete m_radicalInfo;
Joseph Kerian's avatar
Joseph Kerian committed
112
113
}

114
void RadSelectView::changedSearch()
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
115
{
116
  emit searchModified();
117
118
}

119
120
void RadSelectView::clearSearch()
{
121
  m_possibleKanji.clear();
122
123
124
125
126
127
128
  m_buttongrid->clearSelections();
  selected_radicals->clear();
  strokes_low->setValue( 0 );
  strokes_high->setValue( 0 );
}

void RadSelectView::kanjiClicked( QListWidgetItem *item )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
  QString allText = i18nc( "@item:inlist all matches should be found", "(ALL)" );
  QString finalText;
  if( item->text() == allText )
  {
    foreach(  QListWidgetItem *listItem
            , selected_radicals->findItems( "*", Qt::MatchWildcard ) )
    {
      if( listItem->text() != allText )
      {
        finalText += listItem->text();
      }
    }
  }
  else
  {
    finalText = item->text();
  }

  QApplication::clipboard()->setText( finalText, QClipboard::Selection );
149
150
}

151
void RadSelectView::kanjiDoubleClicked( QListWidgetItem *item )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
152
153
154
155
156
157
{
  QString str = copied_line->text();
  int pos = copied_line->cursorPosition();
  str.insert( pos, item->text() );
  copied_line->setText( str );
  copied_line->setCursorPosition( pos + 1 );
158
159
}

160
void RadSelectView::listPossibleKanji( const QList<Kanji>& list )
161
{
162
163
  unsigned int low  = strokes_low->value();
  unsigned int high = strokes_high->value();
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

  //Modification of the stroke boxes
  //We want to move the max value to something reasonable...
  //for example, 5 above the current max value so that rollover
  //works nicely. We want to reset to all if the list is empty.
  //And we also don't limit if the current value is higher than
  //max value in the list
  int newMax = 20;
  if( list.count() < 1
    || list.last().strokes() < low
    || list.last().strokes() + 5 < high )
  {
    newMax = 99;
  }
  else
  {
    newMax = list.last().strokes() + 5;
  }

  strokes_low->setMaximum( newMax );
  strokes_high->setMaximum( newMax );
  if( high == 0 )
  {
    high = 99;
  }

  selected_radicals->clear();
  foreach( const Kanji &it, list )
  {
    if( low <= it.strokes() && it.strokes() <= high )
    {
      new QListWidgetItem( (QString)it, selected_radicals );
    }
  }

  m_possibleKanji = list;

  emit searchModified();
Joseph Kerian's avatar
Joseph Kerian committed
202
203
}

204
void RadSelectView::loadKanji( QString &kanji )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
205
206
{
  //TODO: loadKanji method
Joseph Kerian's avatar
Joseph Kerian committed
207
208
}

209
210
211
void RadSelectView::loadRadicals(  const QString &radicals
                                 , int strokeMin
                                 , int strokeMax )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
212
{
213
214
  //TODO: loadRadicals method
  emit searchModified();
215
216
}

217
void RadSelectView::loadSettings()
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
218
{
219
220
  //TODO: Add preferences for what to do on single/double click
  //Suggested options: Lookup in Kiten, Add to Search Bar, Copy to Clipboard
221
  selected_radicals->setFont( RadSelectConfigSkeleton::self()->resultListFont() );
222
  m_buttongrid->setFont( RadSelectConfigSkeleton::self()->font() );
Joseph Kerian's avatar
Joseph Kerian committed
223
224
}

225
void RadSelectView::strokeLimitChanged( int newvalue )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
  int low  = strokes_low->value();
  int high = strokes_high->value();
  if( low > high )
  {
    if( low == newvalue )
    {
      strokes_high->setValue( newvalue );
    }
    else
    {
      strokes_low->setValue( newvalue );
    }
  }

  //This will force reevaluation of the list if it's needed
  QList<Kanji> newList = m_possibleKanji;
  listPossibleKanji( newList );
244
245
}

246
247
248
249
250
251
252
void RadSelectView::toClipboard()
{
  QClipboard *cb = QApplication::clipboard();
  cb->setText( copied_line->text(), QClipboard::Clipboard );
  cb->setText( copied_line->text(), QClipboard::Selection );
}

253