File indexing completed on 2022-11-29 13:00:36

0001 /*
0002     options to enable/disable columns for the languages
0003     SPDX-FileCopyrightText: 2008 Javier goday <jgoday@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "vocabularycolumnsdialog.h"
0008 
0009 // parley imports
0010 #include "documentsettings.h"
0011 #include "vocabularymodel.h"
0012 
0013 // KDE imports
0014 #include <KEduVocDocument>
0015 #include <KLocalizedString>
0016 #include <KTitleWidget>
0017 
0018 // Qt imports
0019 #include <QDialogButtonBox>
0020 #include <QGridLayout>
0021 #include <QGroupBox>
0022 #include <QHeaderView>
0023 #include <QStandardItem>
0024 #include <QStandardItemModel>
0025 #include <QTreeView>
0026 #include <QVBoxLayout>
0027 
0028 const int COLUMNS_LIMIT = 1; // columns for row
0029 
0030 using namespace Editor;
0031 
0032 VocabularyColumnsDialog::VocabularyColumnsDialog(KEduVocDocument *doc, QWidget *parent)
0033     : QDialog(parent)
0034     , m_doc{doc}
0035     , m_models()
0036 {
0037     QDialogButtonBox *button_dialog = new QDialogButtonBox;
0038     button_dialog->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0039 
0040     m_box = new QGridLayout();
0041     m_settings = new DocumentSettings(m_doc->url().url());
0042 
0043     setWindowTitle(i18nc("@title:window", "Vocabulary Columns"));
0044 
0045     QWidget *main_widget = new QWidget(this);
0046     main_widget->setLayout(m_box);
0047 
0048     QVBoxLayout *layout = new QVBoxLayout;
0049     layout->addWidget(main_widget);
0050     layout->addWidget(button_dialog);
0051 
0052     setLayout(layout);
0053 
0054     connect(button_dialog, &QDialogButtonBox::accepted, this, &VocabularyColumnsDialog::accept);
0055     connect(button_dialog, &QDialogButtonBox::rejected, this, &VocabularyColumnsDialog::reject);
0056 
0057     KTitleWidget *titleWidget = new KTitleWidget(this);
0058     titleWidget->setText(i18n("Enable/Disable the columns for each language"));
0059     titleWidget->setIcon(QIcon::fromTheme(QStringLiteral("view-file-columns")), KTitleWidget::ImageRight);
0060     m_box->addWidget(titleWidget, 0, 0, 1, 2);
0061 
0062     createLanguagesLayout();
0063 }
0064 
0065 VocabularyColumnsDialog::~VocabularyColumnsDialog()
0066 {
0067     delete m_settings;
0068 }
0069 
0070 void VocabularyColumnsDialog::accept()
0071 {
0072     saveVisibleColumns();
0073     QDialog::accept();
0074 }
0075 
0076 void VocabularyColumnsDialog::createLanguagesLayout()
0077 {
0078     int row = 1;
0079     int column = 0;
0080     for (int i = 0; i < m_doc->identifierCount(); i++) {
0081         m_models[i] = new QStandardItemModel(this);
0082 
0083         QGroupBox *box = new QGroupBox(m_doc->identifier(i).name(), 0);
0084         QVBoxLayout *box_layout = new QVBoxLayout(0);
0085 
0086         m_box->addWidget(box, row, column);
0087 
0088         if (column < COLUMNS_LIMIT) {
0089             column++;
0090         } else {
0091             row++;
0092             column = 0;
0093         }
0094 
0095         box->setLayout(box_layout);
0096 
0097         addLanguage(i, box_layout);
0098     }
0099 }
0100 
0101 void VocabularyColumnsDialog::addLanguage(int i, QVBoxLayout *parent)
0102 {
0103     for (int c = 1; c < VocabularyModel::EntryColumnsMAX; c++) {
0104         QStandardItem *item_data = new QStandardItem(VocabularyModel::columnTitle(m_doc, i, c, /*addLocaleSuffix*/ false));
0105         item_data->setData(QVariant(c - 1), VocabularyColumnsDialog::ColumnRole);
0106         item_data->setData(QVariant(i), VocabularyColumnsDialog::LanguageRole);
0107         item_data->setCheckable(true);
0108         item_data->setEditable(false);
0109 
0110         // check if the column is active
0111         if (m_settings->visibleColumns().size() >= (i * 8 + c)) {
0112             item_data->setCheckState(m_settings->visibleColumns().at(i * 8 + c) == 1 ? Qt::Checked : Qt::Unchecked);
0113         }
0114 
0115         m_models[i]->insertRow(c - 1, item_data);
0116     }
0117 
0118     QTreeView *treeView = new QTreeView(this);
0119     treeView->header()->hide();
0120     treeView->setModel(m_models[i]);
0121 
0122     parent->addWidget(treeView);
0123 }
0124 
0125 void VocabularyColumnsDialog::saveVisibleColumns()
0126 {
0127     QList<int> columns;
0128 
0129     for (int i = 0; i < m_doc->identifierCount(); i++) { // each model for every language
0130         columns << 1;
0131         for (int row = 0; row < m_models[i]->rowCount(); row++) { // the columns for one language
0132             int enabled = (m_models[i]->item(row)->checkState() == Qt::Checked) ? 1 : 0;
0133             columns << enabled;
0134         }
0135     }
0136 
0137     // i think that this is not the place for this
0138     m_settings->setVisibleColumns(columns);
0139     m_settings->save();
0140 }