File indexing completed on 2024-05-05 03:40:58

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015 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 "initialphrasebookwidget.h"
0022 
0023 // include files for Qt
0024 #include <QDir>
0025 #include <QLabel>
0026 #include <QStack>
0027 #include <QStandardItemModel>
0028 #include <QStandardPaths>
0029 #include <QTreeView>
0030 #include <QVBoxLayout>
0031 
0032 // include files for KDE
0033 #include <KLocalizedString>
0034 #include <QDialog>
0035 #include <QUrl>
0036 
0037 #include <QDebug>
0038 
0039 InitialPhraseBookWidget::InitialPhraseBookWidget(QWidget *parent, const char *name)
0040     : QWizardPage(parent)
0041 {
0042     setObjectName(QLatin1String(name));
0043     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0044     // TODO PORT QT5     mainLayout->setSpacing(QDialog::spacingHint());
0045     QLabel *label = new QLabel(i18n("Please decide which phrase books you need:"), this);
0046     label->setObjectName(QStringLiteral("booksTitle"));
0047     mainLayout->addWidget(label);
0048 
0049     m_model = new QStandardItemModel(0, 1, this);
0050     m_model->setHeaderData(0, Qt::Horizontal, i18n("Book"));
0051     QTreeView *view = new QTreeView(this);
0052     view->setSortingEnabled(false);
0053     // books->setItemsMovable (false);
0054     view->setDragEnabled(false);
0055     view->setRootIsDecorated(true);
0056     view->setSelectionMode(QAbstractItemView::MultiSelection);
0057     view->setModel(m_model);
0058     mainLayout->addWidget(view);
0059 
0060     initStandardPhraseBooks();
0061     connect(m_model, &QStandardItemModel::itemChanged, this, &InitialPhraseBookWidget::slotItemChanged);
0062 }
0063 
0064 InitialPhraseBookWidget::~InitialPhraseBookWidget()
0065 {
0066 }
0067 
0068 void InitialPhraseBookWidget::slotItemChanged(QStandardItem *item)
0069 {
0070     if (item->hasChildren()) {
0071         for (int i = 0; i < item->rowCount(); ++i) {
0072             QStandardItem *child = item->child(i);
0073             child->setCheckState(item->checkState());
0074         }
0075     }
0076 }
0077 
0078 void InitialPhraseBookWidget::initStandardPhraseBooks()
0079 {
0080     StandardBookList bookPaths = PhraseBook::standardPhraseBooks();
0081 
0082     QStandardItem *parent = m_model->invisibleRootItem();
0083     QStringList currentNamePath;
0084     currentNamePath << QLatin1String("");
0085     QStack<QStandardItem *> stack;
0086     StandardBookList::iterator it;
0087     for (it = bookPaths.begin(); it != bookPaths.end(); ++it) {
0088         QString namePath = (*it).path;
0089         QStringList dirs = namePath.split(QLatin1Char('/'));
0090 
0091         QStringList::iterator it1 = currentNamePath.begin();
0092         QStringList::iterator it2 = dirs.begin();
0093         for (; (it1 != currentNamePath.end()) && (it2 != dirs.end()) && (*it1 == *it2); ++it1, ++it2)
0094             ;
0095 
0096         for (; it1 != currentNamePath.end(); ++it1) {
0097             parent = stack.pop();
0098         }
0099         for (; it2 != dirs.end(); ++it2) {
0100             stack.push(parent);
0101             QStandardItem *newParent = new QStandardItem(*it2);
0102             newParent->setCheckable(true);
0103             parent->appendRow(newParent);
0104             parent = newParent;
0105         }
0106         currentNamePath = dirs;
0107 
0108         QStandardItem *book;
0109         book = new QStandardItem((*it).name);
0110         book->setData((*it).filename);
0111         book->setCheckable(true);
0112         parent->appendRow(book);
0113     }
0114 }
0115 
0116 void InitialPhraseBookWidget::createBook()
0117 {
0118     PhraseBook book;
0119     QStandardItem *item = m_model->invisibleRootItem();
0120     addChildrenToBook(book, item);
0121 
0122     QString bookLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1Char('/');
0123     if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
0124         QDir().mkpath(bookLocation);
0125         qDebug() << "creating book at location " << bookLocation;
0126         book.save(QUrl::fromLocalFile(bookLocation + QStringLiteral("standard.phrasebook")));
0127     }
0128 }
0129 
0130 void InitialPhraseBookWidget::addChildrenToBook(PhraseBook &book, QStandardItem *item)
0131 {
0132     for (int i = 0; i < item->rowCount(); ++i) {
0133         QStandardItem *child = item->child(i);
0134         if (child->checkState() != Qt::Unchecked) {
0135             PhraseBook localBook;
0136             if (localBook.open(QUrl::fromLocalFile(child->data().toString()))) {
0137                 book += localBook;
0138             }
0139         }
0140         if (child->hasChildren())
0141             addChildrenToBook(book, child);
0142     }
0143 }