File indexing completed on 2024-04-21 03:41:46

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by Joshua Keel <joshuakeel@gmail.com>              *
0003  *             (C) 2007 by Jeremy Whiting <jpwhiting@kde.org>              *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #include "vocabsettings.h"
0022 
0023 #include <KConfigDialog>
0024 #include <KNSWidgets/Button>
0025 #include <sharedkvtmlfiles.h>
0026 
0027 #include <QIcon>
0028 #include <QFileInfo>
0029 #include <QPointer>
0030 
0031 
0032 #include "kanagramsettings.h"
0033 #include "vocabedit.h"
0034 
0035 VocabSettings::VocabSettings(QWidget *parent)
0036     : QWidget(parent)
0037 {
0038     setupUi(this);
0039 
0040     connect(lviewVocab, &QTreeWidget::currentItemChanged, this, &VocabSettings::slotSelectionChanged);
0041     connect(KanagramSettings::self(), &KCoreConfigSkeleton::configChanged,
0042             this, &VocabSettings::refreshView);
0043 
0044     connect(btnDownloadNew, &KNSWidgets::Button::dialogFinished, this, [this] (const QList<KNSCore::Entry> &changedEntries) {
0045         if (!changedEntries.isEmpty()) {
0046             refreshView();
0047         }
0048     });
0049     btnDownloadNew->setConfigFile("kanagram.knsrc");
0050 
0051     refreshView();
0052 }
0053 
0054 VocabSettings::~VocabSettings()
0055 {
0056 }
0057 
0058 void VocabSettings::loadView()
0059 {
0060     lviewVocab->clear();
0061 
0062     SharedKvtmlFiles::sortDownloadedFiles();
0063     QString language = KanagramSettings::dataLanguage();
0064     m_fileList = SharedKvtmlFiles::fileNames(language);
0065     m_titleList = SharedKvtmlFiles::titles(language);
0066     m_commentList = SharedKvtmlFiles::comments(language);
0067 
0068     for (int i = 0; i < m_fileList.size(); i++)
0069     {
0070         QTreeWidgetItem *item = new QTreeWidgetItem(lviewVocab, 0);
0071         item->setText( 0, m_titleList[i] );
0072         item->setText( 1, m_commentList[i] );
0073         m_itemMap[item] = i;
0074     }
0075 
0076     lviewVocab->resizeColumnToContents(0);
0077 }
0078 
0079 void VocabSettings::refreshView()
0080 {
0081     loadView();
0082     Q_EMIT widgetModified();
0083 }
0084 
0085 void VocabSettings::on_btnEdit_clicked()
0086 {
0087     if (lviewVocab->currentItem())
0088     {
0089         int index = m_itemMap[lviewVocab->currentItem()];
0090         VocabEdit *vocabEdit = new VocabEdit(this, m_fileList[index]);
0091         connect(vocabEdit, &VocabEdit::finished, this, &VocabSettings::refreshView);
0092         vocabEdit->show();
0093     }
0094 }
0095 
0096 void VocabSettings::on_btnCreateNew_clicked()
0097 {
0098     VocabEdit *vocabEdit = new VocabEdit(this, QLatin1String(""));
0099     connect(vocabEdit, &VocabEdit::finished, this, &VocabSettings::refreshView);
0100     vocabEdit->show();
0101 }
0102 
0103 void VocabSettings::slotSelectionChanged(QTreeWidgetItem *item)
0104 {
0105     int index = m_itemMap.value(item);
0106     bool writeable = QFileInfo(m_fileList[index]).isWritable();
0107     btnEdit->setEnabled(writeable);
0108 }
0109 
0110 #include "moc_vocabsettings.cpp"