File indexing completed on 2024-04-28 05:08:19

0001 /***************************************************************************
0002     Copyright (C) 2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "entrymatchdialog.h"
0026 #include "entryview.h"
0027 #include "entry.h"
0028 #include "fetch/fetchmanager.h"
0029 
0030 #include <KLocalizedString>
0031 #include <KTextEdit>
0032 #include <KIconLoader>
0033 
0034 #include <QSplitter>
0035 #include <QLabel>
0036 #include <QVBoxLayout>
0037 #include <QHBoxLayout>
0038 #include <QTreeWidget>
0039 #include <QDialogButtonBox>
0040 #include <QPushButton>
0041 
0042 namespace {
0043   static const int DIALOG_MIN_WIDTH = 600;
0044 }
0045 
0046 using namespace Tellico;
0047 using Tellico::EntryMatchDialog;
0048 
0049 EntryMatchDialog::EntryMatchDialog(QWidget* parent_, Data::EntryPtr entryToUpdate_,
0050                                    Fetch::Fetcher* fetcher_, const EntryUpdater::ResultList& matchResults_)
0051     : QDialog(parent_) {
0052   Q_ASSERT(entryToUpdate_);
0053   Q_ASSERT(fetcher_);
0054 
0055   setModal(true);
0056   setWindowTitle(i18n("Select Match"));
0057 
0058   QVBoxLayout* mainLayout = new QVBoxLayout(this);
0059   setLayout(mainLayout);
0060 
0061   QWidget* mainWidget = new QWidget(this);
0062   mainLayout->addWidget(mainWidget);
0063 
0064   QWidget* hbox = new QWidget(mainWidget);
0065   mainLayout->addWidget(hbox);
0066   QHBoxLayout* hboxHBoxLayout = new QHBoxLayout(hbox);
0067   hboxHBoxLayout->setMargin(0);
0068   hboxHBoxLayout->setSpacing(10);
0069 
0070   QLabel* icon = new QLabel(hbox);
0071   hboxHBoxLayout->addWidget(icon);
0072   icon->setPixmap(Fetch::Manager::fetcherIcon(fetcher_, KIconLoader::Panel, 48));
0073   icon->setAlignment(Qt::Alignment(Qt::AlignLeft) | Qt::AlignTop);
0074 
0075   QString s = i18n("<qt><b>%1</b> returned multiple results which could match <b>%2</b>, "
0076                    "the entry currently in the collection. Please select the correct match.</qt>",
0077                    fetcher_->source(),
0078                    entryToUpdate_->title());
0079 
0080   KTextEdit* l = new KTextEdit(hbox);
0081   hboxHBoxLayout->addWidget(l);
0082   l->setHtml(s);
0083   l->setReadOnly(true);
0084   l->setMaximumHeight(48);
0085   l->setFrameStyle(0);
0086 
0087   QSplitter* split = new QSplitter(Qt::Vertical, mainWidget);
0088   mainLayout->addWidget(split);
0089   split->setMinimumHeight(400);
0090 
0091   m_treeWidget = new QTreeWidget(split);
0092   m_treeWidget->setAllColumnsShowFocus(true);
0093   m_treeWidget->setSortingEnabled(true);
0094   m_treeWidget->setHeaderLabels(QStringList() << i18n("Title") << i18n("Description"));
0095   connect(m_treeWidget, &QTreeWidget::itemSelectionChanged, this, &EntryMatchDialog::slotShowEntry);
0096 
0097   foreach(const EntryUpdater::UpdateResult& res, matchResults_) {
0098     Data::EntryPtr matchingEntry = res.result->fetchEntry();
0099     QTreeWidgetItem* item = new QTreeWidgetItem(m_treeWidget, QStringList() << matchingEntry->title() << res.result->desc);
0100     m_itemResults.insert(item, res);
0101   }
0102 
0103   m_entryView = new EntryView(split);
0104   // don't bother creating funky gradient images for compact view
0105   m_entryView->setUseGradientImages(false);
0106   // set the xslt file AFTER setting the gradient image option
0107   m_entryView->setXSLTFile(QStringLiteral("Compact.xsl"));
0108   m_entryView->addXSLTStringParam("skip-fields", "id,mdate,cdate");
0109 
0110   QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0111   QPushButton* okButton = buttonBox->button(QDialogButtonBox::Ok);
0112   okButton->setDefault(true);
0113   okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0114   connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0115   connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0116   mainLayout->addWidget(buttonBox);
0117 
0118   setMinimumWidth(qMax(minimumWidth(), DIALOG_MIN_WIDTH));
0119   // have the entry view be taller than the tree widget
0120   split->setStretchFactor(1, 10);
0121 }
0122 
0123 void EntryMatchDialog::slotShowEntry() {
0124   QTreeWidgetItem* item = m_treeWidget->currentItem();
0125   if(!item) {
0126     return;
0127   }
0128 
0129   m_entryView->showEntry(m_itemResults[item].result->fetchEntry());
0130 }
0131 
0132 Tellico::EntryUpdater::UpdateResult EntryMatchDialog::updateResult() const {
0133   QTreeWidgetItem* item = m_treeWidget->currentItem();
0134   if(!item) {
0135     return EntryUpdater::UpdateResult();
0136   }
0137   return m_itemResults[item];
0138 }