File indexing completed on 2024-05-05 04:22:02

0001 /* SPDX-FileCopyrightText: 2003-2020 The KPhotoAlbum Development Team
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "ImportMatcher.h"
0007 
0008 #include "ImportSettings.h"
0009 
0010 #include <KColorScheme>
0011 #include <KLocalizedString>
0012 #include <QGridLayout>
0013 #include <QVBoxLayout>
0014 #include <qcheckbox.h>
0015 #include <qcombobox.h>
0016 #include <qlabel.h>
0017 using namespace ImportExport;
0018 
0019 ImportMatcher::ImportMatcher(const QString &otherCategory, const QString &myCategory,
0020                              const QStringList &otherItems, const QStringList &myItems,
0021                              bool allowNew, QWidget *parent)
0022     : QScrollArea(parent)
0023     , m_otherCategory(otherCategory)
0024     , m_myCategory(myCategory)
0025 {
0026     setWidgetResizable(true);
0027     QWidget *top = new QWidget(viewport());
0028     QVBoxLayout *layout = new QVBoxLayout(top);
0029     QWidget *grid = new QWidget;
0030     layout->addWidget(grid);
0031     layout->addStretch(1);
0032 
0033     QGridLayout *gridLay = new QGridLayout(grid);
0034     gridLay->setColumnStretch(1, 1);
0035     setWidget(top);
0036 
0037     QLabel *label = new QLabel(i18n("Key in file"), grid);
0038     label->setAutoFillBackground(true);
0039     label->setForegroundRole(QPalette::Dark);
0040     label->setBackgroundRole(QPalette::BrightText);
0041     label->setAlignment(Qt::AlignCenter);
0042     gridLay->addWidget(label, 0, 0);
0043 
0044     label = new QLabel(i18n("Key in your database"), grid);
0045     label->setAutoFillBackground(true);
0046     label->setForegroundRole(QPalette::Dark);
0047     label->setBackgroundRole(QPalette::BrightText);
0048     label->setAlignment(Qt::AlignCenter);
0049     gridLay->addWidget(label, 0, 1);
0050 
0051     int row = 1;
0052     for (QStringList::ConstIterator it = otherItems.begin(); it != otherItems.end(); ++it) {
0053         CategoryMatch *match = new CategoryMatch(allowNew, *it, myItems, grid, gridLay, row++);
0054         m_matchers.append(match);
0055     }
0056 }
0057 
0058 CategoryMatch::CategoryMatch(bool allowNew, const QString &kimFileItem, QStringList myItems, QWidget *parent, QGridLayout *grid, int row)
0059 {
0060     m_checkbox = new QCheckBox(kimFileItem, parent);
0061     m_text = kimFileItem; // We can't just use QCheckBox::text() as Qt adds accelerators.
0062     m_checkbox->setChecked(true);
0063     grid->addWidget(m_checkbox, row, 0);
0064 
0065     m_combobox = new QComboBox;
0066     m_combobox->setEditable(allowNew);
0067 
0068     myItems.sort();
0069     m_combobox->addItems(myItems);
0070     QObject::connect(m_checkbox, &QCheckBox::toggled, m_combobox, &QComboBox::setEnabled);
0071     grid->addWidget(m_combobox, row, 1);
0072 
0073     if (myItems.contains(kimFileItem)) {
0074         m_combobox->setCurrentIndex(myItems.indexOf(kimFileItem));
0075     } else {
0076         // This item was not in my database
0077         QString match;
0078         for (QStringList::ConstIterator it = myItems.constBegin(); it != myItems.constEnd(); ++it) {
0079             if ((*it).contains(kimFileItem) || kimFileItem.contains(*it)) {
0080                 // Either my item was a substring of the kim item or the other way around (Jesper is a substring of Jesper Pedersen)
0081                 if (match.isEmpty())
0082                     match = *it;
0083                 else {
0084                     match.clear();
0085                     break;
0086                 }
0087             }
0088         }
0089         if (!match.isEmpty()) {
0090             // there was a single substring match
0091             m_combobox->setCurrentIndex(myItems.indexOf(match));
0092         } else {
0093             // Either none or multiple items matches
0094             if (allowNew) {
0095                 m_combobox->addItem(kimFileItem);
0096                 m_combobox->setCurrentIndex(m_combobox->count() - 1);
0097             } else
0098                 m_checkbox->setChecked(false);
0099         }
0100     }
0101 }
0102 
0103 ImportExport::CategoryMatchSetting ImportExport::ImportMatcher::settings()
0104 {
0105     CategoryMatchSetting res(m_myCategory, m_otherCategory);
0106     for (CategoryMatch *match : m_matchers) {
0107         if (match->m_checkbox->isChecked())
0108             res.add(match->m_combobox->currentText(), match->m_text);
0109     }
0110     return res;
0111 }
0112 
0113 // vi:expandtab:tabstop=4 shiftwidth=4:
0114 
0115 #include "moc_ImportMatcher.cpp"