~neon/kiten/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
    This file is part of Kiten, a KDE Japanese Reference Tool...
    SPDX-FileCopyrightText: 2005 Paul Temple <paul.temple@gmx.net>
    SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
    SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "configdictionaryselector.h"

#include <KConfigSkeleton>

#include <QFileDialog>
#include <QStringList>

ConfigDictionarySelector::ConfigDictionarySelector( const QString &dictionaryName,
  QWidget *parent, KConfigSkeleton *config,Qt::WindowFlags f )
{
  setupUi( this );
  _dictName = dictionaryName;
  _config = config;

  connect(addButton, &QPushButton::clicked, this, &ConfigDictionarySelector::addDictSlot);
  connect(delButton, &QPushButton::clicked, this, &ConfigDictionarySelector::deleteDictSlot);
  __useGlobal->setObjectName( QString( "kcfg_" + _dictName + "__useGlobal" ) );
}

//Read from preferences to the active list
void ConfigDictionarySelector::updateWidgets()
{
  QStringList names;

  QString groupName = "dicts_" + _dictName;
  KConfigGroup group = _config->config()->group( groupName );
  KConfigSkeletonItem *item = _config->findItem( _dictName + "__NAMES" );
  if( item != nullptr )
  {
    names = item->property().toStringList();
  }

  fileList->clear();

  foreach( const QString &it, names )
  {
    QStringList newRow( it );
    newRow << group.readEntry( it, QString() );
    (void) new QTreeWidgetItem( fileList, newRow );
  }
}

void ConfigDictionarySelector::updateSettings()
{
  QStringList names;

  KConfigGroup group = _config->config()->group( "dicts_" + _dictName.toLower() );

  for( int i = 0; i < fileList->topLevelItemCount(); i++ )
  {
    QTreeWidgetItem *it = fileList->topLevelItem( i );
    QString dictionaryName = it->text( 0 );
    QString dictionaryPath = it->text( 1 );
    names.append( dictionaryName );

    group.writeEntry( dictionaryName, dictionaryPath );
  }

  //This feels distinctly hackish to me... :(
  _config->findItem( _dictName + "__NAMES" )->setProperty( names );
  _config->save();
}

void ConfigDictionarySelector::updateWidgetsDefault()
{
  // no default for custom edict list or
  // should we really delete all items in the list?
}

bool ConfigDictionarySelector::isDefault()
{
  // no default for custom edict list or
  // should we really delete all items in the list?
  return true;
}

bool ConfigDictionarySelector::hasChanged()
{
  return false;
}

void ConfigDictionarySelector::addDictSlot()
{
  QTreeWidgetItem *item = fileList->topLevelItem( 0 );

  QString filename = QFileDialog::getOpenFileName(nullptr, QString(),
                  item ? QFileInfo( item->text( 1 ) ).absolutePath().append( "/" )
                  : QString() );
  QString name = QFileInfo( filename ).fileName();
  if( filename.isNull() )
    return;

  QStringList newRow( name );
  newRow << filename;
  (void) new QTreeWidgetItem( fileList, newRow );

  updateSettings();
  emit widgetChanged();
}

void ConfigDictionarySelector::deleteDictSlot()
{
  foreach( QTreeWidgetItem *file, fileList->selectedItems() )
  {
    if ( ! file )
    {
      return;
    }

    delete file;

    updateSettings();
    emit widgetChanged();
  }
}