File indexing completed on 2024-04-28 05:40:48
0001 /* 0002 SPDX-FileCopyrightText: 2011 Vishesh Yadav <vishesh3y@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "pathconfig.h" 0008 #include "hgconfig.h" 0009 0010 #include <QHBoxLayout> 0011 #include <QVBoxLayout> 0012 #include <QTableWidget> 0013 #include <QHeaderView> 0014 #include <QEvent> 0015 #include <QPushButton> 0016 #include <QAction> 0017 #include <QMenu> 0018 #include <KLocalizedString> 0019 #include <KMessageBox> 0020 #include <QDebug> 0021 0022 HgPathConfigWidget::HgPathConfigWidget(QWidget *parent): 0023 QWidget(parent), 0024 m_loadingCell(false), 0025 m_allValidData(true), 0026 m_newAdd(false) 0027 { 0028 setupUI(); 0029 loadConfig(); 0030 } 0031 0032 void HgPathConfigWidget::setupContextMenu() 0033 { 0034 m_addAction = new QAction(this); 0035 m_addAction->setIcon(QIcon::fromTheme(QStringLiteral("add"))); 0036 m_addAction->setText(xi18nc("@action:inmenu", 0037 "Add")); 0038 connect(m_addAction, SIGNAL(triggered()), this, SLOT(slotAddPath())); 0039 0040 m_modifyAction = new QAction(this); 0041 m_modifyAction->setIcon(QIcon::fromTheme(QStringLiteral("edit"))); 0042 m_modifyAction->setText(xi18nc("@action:inmenu", 0043 "Edit")); 0044 connect(m_modifyAction, SIGNAL(triggered()), this, SLOT(slotModifyPath())); 0045 0046 m_deleteAction = new QAction(this); 0047 m_deleteAction->setIcon(QIcon::fromTheme(QStringLiteral("remove"))); 0048 m_deleteAction->setText(xi18nc("@action:inmenu", 0049 "Remove")); 0050 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotDeletePath())); 0051 0052 m_contextMenu = new QMenu(this); 0053 m_contextMenu->addAction(m_addAction); 0054 m_contextMenu->addAction(m_modifyAction); 0055 m_contextMenu->addAction(m_deleteAction); 0056 0057 connect(m_pathsListWidget, &QTableWidget::cellChanged, 0058 this, &HgPathConfigWidget::slotCellChanged); 0059 connect(m_pathsListWidget, SIGNAL(itemSelectionChanged()), 0060 this, SLOT(slotSelectionChanged())); 0061 connect(m_pathsListWidget, &QTableWidget::customContextMenuRequested, 0062 this, &HgPathConfigWidget::slotContextMenuRequested); 0063 } 0064 0065 void HgPathConfigWidget::setupUI() 0066 { 0067 // add, remove, modify buttons goes here 0068 QHBoxLayout *actionsLayout = new QHBoxLayout; 0069 m_addPathButton = new QPushButton(xi18nc("@label:button", "Add")); 0070 m_modifyPathButton = new QPushButton(xi18nc("@label:button", "Edit")); 0071 m_deletePathButton = new QPushButton(xi18nc("@label:button", "Remove")); 0072 0073 actionsLayout->addWidget(m_addPathButton); 0074 actionsLayout->addWidget(m_modifyPathButton); 0075 actionsLayout->addWidget(m_deletePathButton); 0076 0077 connect(m_addPathButton, SIGNAL(clicked()), this, SLOT(slotAddPath())); 0078 connect(m_modifyPathButton, SIGNAL(clicked()), this, SLOT(slotModifyPath())); 0079 connect(m_deletePathButton, SIGNAL(clicked()), this, SLOT(slotDeletePath())); 0080 0081 /// create and setup the Table Widget 0082 m_pathsListWidget = new QTableWidget; 0083 setupContextMenu(); // make context menu 0084 0085 m_pathsListWidget->setColumnCount(2); 0086 m_pathsListWidget->verticalHeader()->hide(); 0087 m_pathsListWidget->horizontalHeader()->hide(); 0088 m_pathsListWidget->setSelectionBehavior(QAbstractItemView::SelectRows); 0089 m_pathsListWidget->setSelectionMode(QAbstractItemView::SingleSelection); 0090 m_pathsListWidget->setEditTriggers(QAbstractItemView::DoubleClicked); 0091 m_pathsListWidget->horizontalHeader()->setStretchLastSection(true); 0092 m_pathsListWidget->setContextMenuPolicy(Qt::CustomContextMenu); 0093 0094 // setup main layout 0095 QVBoxLayout *mainLayout = new QVBoxLayout; 0096 mainLayout->addLayout(actionsLayout); 0097 mainLayout->addWidget(m_pathsListWidget); 0098 setLayout(mainLayout); 0099 } 0100 0101 void HgPathConfigWidget::loadConfig() 0102 { 0103 HgConfig hgc(HgConfig::RepoConfig); 0104 m_remotePathMap = hgc.repoRemotePathList(); 0105 m_loadingCell = true; 0106 0107 m_pathsListWidget->clearContents(); 0108 m_removeList.clear(); 0109 0110 QMutableMapIterator<QString, QString> it(m_remotePathMap); 0111 int count = 0; 0112 while (it.hasNext()) { 0113 it.next(); 0114 0115 QTableWidgetItem *alias = new QTableWidgetItem; 0116 QTableWidgetItem *path = new QTableWidgetItem; 0117 0118 alias->setText(it.key()); 0119 path->setText(it.value()); 0120 0121 m_pathsListWidget->insertRow(count); 0122 m_pathsListWidget->setItem(count, 0, alias); 0123 m_pathsListWidget->setItem(count, 1, path); 0124 } 0125 0126 m_pathsListWidget->resizeRowsToContents(); 0127 m_loadingCell = false; 0128 } 0129 0130 void HgPathConfigWidget::saveConfig() 0131 { 0132 HgConfig hgc(HgConfig::RepoConfig); 0133 0134 if (!m_allValidData) { 0135 return; 0136 } 0137 0138 // first delete the alias in remove list from hgrc 0139 for (const QString &alias : std::as_const(m_removeList)) { 0140 hgc.deleteRepoRemotePath(alias); 0141 } 0142 0143 // now save the new map in table to hgrc 0144 QMutableMapIterator<QString, QString> it(m_remotePathMap); 0145 while (it.hasNext()) { 0146 it.next(); 0147 QString alias = it.key(); 0148 QString url = it.value(); 0149 hgc.setRepoRemotePath(alias, url); 0150 } 0151 } 0152 0153 void HgPathConfigWidget::slotContextMenuRequested(const QPoint &pos) 0154 { 0155 if (m_pathsListWidget->indexAt(pos).isValid()) { 0156 m_deleteAction->setEnabled(true); 0157 m_modifyAction->setEnabled(true); 0158 } else { 0159 m_deleteAction->setEnabled(false); 0160 m_modifyAction->setEnabled(false); 0161 } 0162 m_addAction->setEnabled(true); 0163 m_contextMenu->exec(m_pathsListWidget->mapToGlobal(pos)); 0164 } 0165 0166 void HgPathConfigWidget::slotAddPath() 0167 { 0168 QTableWidgetItem *alias = new QTableWidgetItem; 0169 QTableWidgetItem *path = new QTableWidgetItem; 0170 0171 int count = m_pathsListWidget->rowCount(); 0172 m_loadingCell = true; 0173 m_pathsListWidget->insertRow(count); 0174 m_pathsListWidget->setItem(count, 0, alias); 0175 m_pathsListWidget->setItem(count, 1, path); 0176 m_pathsListWidget->resizeRowsToContents(); 0177 m_pathsListWidget->setCurrentItem(alias); 0178 m_pathsListWidget->editItem(m_pathsListWidget->item(count, 0)); 0179 m_loadingCell = false; 0180 m_newAdd = true; 0181 } 0182 0183 void HgPathConfigWidget::slotDeletePath() 0184 { 0185 int currentRow = m_pathsListWidget->currentRow(); 0186 m_removeList << m_pathsListWidget->item(currentRow, 0)->text(); 0187 m_remotePathMap.remove(m_pathsListWidget->item(currentRow, 0)->text()); 0188 m_pathsListWidget->removeRow(currentRow); 0189 } 0190 0191 void HgPathConfigWidget::slotModifyPath() 0192 { 0193 m_pathsListWidget->editItem(m_pathsListWidget->currentItem()); 0194 } 0195 0196 void HgPathConfigWidget::slotCellChanged(int row, int col) 0197 { 0198 if (m_loadingCell || 0199 m_oldSelValue == m_pathsListWidget->currentItem()->text()) { 0200 return; 0201 } 0202 0203 QTableWidgetItem *alias = m_pathsListWidget->item(row, 0); 0204 QTableWidgetItem *url = m_pathsListWidget->item(row, 1); 0205 if (alias->text().isEmpty() || url->text().isEmpty()) { 0206 alias->setBackground(Qt::red); 0207 url->setBackground(Qt::red); 0208 m_allValidData = false; 0209 return; 0210 } 0211 else if (m_remotePathMap.contains(alias->text()) && m_newAdd) { 0212 m_oldSelValue = m_pathsListWidget->currentItem()->text(); 0213 alias->setBackground(Qt::red); 0214 url->setBackground(Qt::red); 0215 m_allValidData = false; 0216 return; 0217 } 0218 else if (m_remotePathMap.contains(alias->text()) && col == 0) { 0219 m_oldSelValue = m_pathsListWidget->currentItem()->text(); 0220 alias->setBackground(Qt::red); 0221 url->setBackground(Qt::red); 0222 m_allValidData = false; 0223 return; 0224 } 0225 else { 0226 qDebug() << "bingo"; 0227 if (!m_newAdd && col == 0) { 0228 m_remotePathMap.remove(m_oldSelValue); 0229 m_removeList << m_oldSelValue; 0230 } 0231 m_remotePathMap.insert(alias->text(), url->text()); 0232 m_oldSelValue = m_pathsListWidget->currentItem()->text(); 0233 alias->setBackground(Qt::NoBrush); 0234 url->setBackground(Qt::NoBrush); 0235 m_allValidData = true; 0236 } 0237 0238 m_newAdd = false; 0239 } 0240 0241 void HgPathConfigWidget::slotSelectionChanged() 0242 { 0243 m_oldSelValue = m_pathsListWidget->currentItem()->text(); 0244 } 0245 0246 0247 0248 #include "moc_pathconfig.cpp"