File indexing completed on 2024-05-12 05:09:49

0001 /***************************************************************************
0002     Copyright (C) 2015 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 "stringmapwidget.h"
0026 
0027 #include <KLocalizedString>
0028 #include <KGuiItem>
0029 
0030 #include <QTreeWidget>
0031 #include <QHeaderView>
0032 #include <QVBoxLayout>
0033 #include <QHBoxLayout>
0034 #include <QLineEdit>
0035 #include <QDialogButtonBox>
0036 #include <QPushButton>
0037 
0038 using Tellico::GUI::StringMapWidget;
0039 
0040 StringMapWidget::StringMapWidget(const QMap<QString, QString>& map_, QWidget* parent_)
0041     : QWidget(parent_) {
0042   QBoxLayout* l = new QVBoxLayout(this);
0043 
0044   m_treeWidget = new QTreeWidget(this);
0045   m_treeWidget->setColumnCount(2);
0046   m_treeWidget->setRootIsDecorated(false);
0047   m_treeWidget->setAllColumnsShowFocus(true);
0048   m_treeWidget->header()->setSortIndicatorShown(true);
0049   m_treeWidget->setHeaderHidden(true); // hide header since neither column has a label initially
0050   connect(m_treeWidget, &QTreeWidget::currentItemChanged, this, &StringMapWidget::slotUpdate);
0051   connect(m_treeWidget, &QTreeWidget::itemClicked, this, &StringMapWidget::slotUpdate);
0052   l->addWidget(m_treeWidget);
0053 
0054   QWidget* box = new QWidget(this);
0055   QHBoxLayout* boxHBoxLayout = new QHBoxLayout(box);
0056   boxHBoxLayout->setMargin(0);
0057   boxHBoxLayout->setSpacing(4);
0058   l->addWidget(box);
0059 
0060   m_edit1 = new QLineEdit(box);
0061   boxHBoxLayout->addWidget(m_edit1);
0062   m_edit1->setFocus();
0063   m_edit2 = new QLineEdit(box);
0064   boxHBoxLayout->addWidget(m_edit2);
0065 
0066   QDialogButtonBox* bb = new QDialogButtonBox(box);
0067   boxHBoxLayout->addWidget(bb);
0068 
0069   QPushButton* pb1 = new QPushButton(bb);
0070   KGuiItem::assign(pb1, KGuiItem(i18nc("set a value", "&Set"), QIcon::fromTheme(QStringLiteral("document-new"))));
0071   connect(pb1, &QAbstractButton::clicked, this, &StringMapWidget::slotAdd);
0072   bb->addButton(pb1, QDialogButtonBox::ActionRole);
0073 
0074   QPushButton* pb2 = new QPushButton(bb);
0075   KGuiItem::assign(pb2, KGuiItem(i18nc("delete a value", "&Delete"), QIcon::fromTheme(QStringLiteral("edit-delete"))));
0076   connect(pb2, &QAbstractButton::clicked, this, &StringMapWidget::slotDelete);
0077   bb->addButton(pb2, QDialogButtonBox::ActionRole);
0078 
0079   l->addWidget(box);
0080 
0081   setContentsMargins(0, 0, 0, 0);
0082   setStringMap(map_);
0083 }
0084 
0085 void StringMapWidget::slotAdd() {
0086   QString s1 = m_edit1->text();
0087   QString s2 = m_edit2->text();
0088   if(s1.isEmpty() && s2.isEmpty()) {
0089     return;
0090   }
0091   QTreeWidgetItem* item = m_treeWidget->currentItem();
0092   if(item && s1 == item->data(0, Qt::DisplayRole).toString()) { // only update values if same key
0093     item->setData(1, Qt::DisplayRole, s2);
0094   } else {
0095     item = new QTreeWidgetItem(m_treeWidget, QStringList() << s1 << s2);
0096   }
0097   m_treeWidget->resizeColumnToContents(0);
0098   m_treeWidget->scrollToItem(item);
0099   m_treeWidget->setCurrentItem(item);
0100 }
0101 
0102 void StringMapWidget::slotDelete() {
0103   delete m_treeWidget->currentItem();
0104   slotUpdate(m_treeWidget->currentItem());
0105 }
0106 
0107 void StringMapWidget::slotUpdate(QTreeWidgetItem* item_) {
0108   if(item_) {
0109     m_edit1->setText(item_->data(0, Qt::DisplayRole).toString());
0110     m_edit2->setText(item_->data(1, Qt::DisplayRole).toString());
0111   } else {
0112     m_edit1->clear();
0113     m_edit2->clear();
0114   }
0115 }
0116 
0117 void StringMapWidget::setLabels(const QString& label1_, const QString& label2_) {
0118   m_treeWidget->headerItem()->setData(0, Qt::DisplayRole, label1_);
0119   m_treeWidget->headerItem()->setData(1, Qt::DisplayRole, label2_);
0120   m_treeWidget->setHeaderHidden(false);
0121 }
0122 
0123 QMap<QString, QString> StringMapWidget::stringMap() {
0124   QMap<QString, QString> map;
0125   for(int i = 0; i < m_treeWidget->topLevelItemCount(); ++i) {
0126     QTreeWidgetItem* item = m_treeWidget->topLevelItem(i);
0127     map.insert(item->data(0, Qt::DisplayRole).toString(),
0128                item->data(1, Qt::DisplayRole).toString());
0129   }
0130   return map;
0131 }
0132 
0133 void StringMapWidget::setStringMap(const QMap<QString, QString>& map_) {
0134   for(QMap<QString, QString>::ConstIterator it = map_.begin(); it != map_.end(); ++it) {
0135     new QTreeWidgetItem(m_treeWidget, QStringList() << it.key() << it.value());
0136   }
0137   // m_treeWidget->resizeColumnToContents(0);
0138 }