File indexing completed on 2024-04-21 05:41:03

0001 /*
0002     SPDX-FileCopyrightText: 2011 Vishesh Yadav <vishesh3y@gmail.com>
0003     SPDX-FileCopyrightText: 2015 Tomasz Bojczuk <seelook@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "statuslist.h"
0009 #include "hgwrapper.h"
0010 
0011 #include <QHash>
0012 #include <QTextCodec>
0013 #include <QVBoxLayout>
0014 #include <QHeaderView>
0015 #include <QTableWidget>
0016 #include <KLocalizedString>
0017 
0018 HgStatusList::HgStatusList(QWidget *parent):
0019     QGroupBox(parent),
0020     m_allWhereChecked(true),
0021     m_sortIndex(false)
0022 {
0023     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0024     m_statusTable = new QTableWidget(this);
0025 
0026     m_statusTable->setColumnCount(3);
0027     const QStringList headers{
0028         QStringLiteral("*"),
0029         QStringLiteral("S"),
0030         i18n("Filename"),
0031     };
0032     m_statusTable->setHorizontalHeaderLabels(headers);
0033     m_statusTable->verticalHeader()->hide();
0034     m_statusTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
0035     m_statusTable->setSelectionBehavior(QAbstractItemView::SelectRows);
0036     m_statusTable->setSelectionMode(QAbstractItemView::SingleSelection);
0037 
0038     mainLayout->addWidget(m_statusTable);
0039 
0040     setTitle(i18nc("@title:group", "File Status"));
0041     setLayout(mainLayout);
0042 
0043     reloadStatusTable();
0044 
0045     connect(m_statusTable, &QTableWidget::currentItemChanged,
0046             this, &HgStatusList::currentItemChangedSlot);
0047     connect(m_statusTable->horizontalHeader(), &QHeaderView::sectionClicked,
0048             this, &HgStatusList::headerClickedSlot);
0049 }
0050 
0051 void HgStatusList::currentItemChangedSlot()
0052 {
0053     Q_EMIT itemSelectionChanged(
0054         m_statusTable->item(m_statusTable->currentRow(), 1)->text()[0].toLatin1(),
0055         m_statusTable->item(m_statusTable->currentRow(), 2)->text());
0056 }
0057 
0058 void HgStatusList::reloadStatusTable()
0059 {
0060     m_statusTable->clearContents();
0061     m_statusTable->resizeRowsToContents();
0062     m_statusTable->resizeColumnsToContents();
0063     m_statusTable->horizontalHeader()->setStretchLastSection(true);
0064 
0065     HgWrapper *hgWrapper = HgWrapper::instance();
0066     QHash<QString, KVersionControlPlugin::ItemVersion> hgVsState;
0067     hgWrapper->getItemVersions(hgVsState);
0068     QMutableHashIterator<QString, KVersionControlPlugin::ItemVersion> it(hgVsState);
0069     int rowCount = 0;
0070     while (it.hasNext()) {
0071         it.next();
0072         KVersionControlPlugin::ItemVersion currentStatus = it.value();
0073         // Get path relative to root directory of repository
0074         // FIXME: preferred method, but not working :| bad hack below
0075         // QString currentFile 
0076         //    = KUrl::relativeUrl(hgWrapper->getBaseDir(), it.key()); 
0077         QString currentFile = it.key().mid(hgWrapper->getBaseDir().length()+1);
0078         QString currentStatusString; //one character status indicator
0079 
0080         // Temporarily ignoring
0081         // TODO: Ask to add file if this is checked by user
0082         if (currentStatus == KVersionControlPlugin::UnversionedVersion ||
0083                 currentStatus == KVersionControlPlugin::IgnoredVersion) {
0084             continue;
0085         }
0086 
0087         QTableWidgetItem *check = new QTableWidgetItem;
0088         QTableWidgetItem *status = new QTableWidgetItem;
0089         QTableWidgetItem *fileName = new QTableWidgetItem;
0090 
0091         switch (currentStatus) {
0092             case KVersionControlPlugin::AddedVersion:
0093                 status->setForeground(Qt::darkCyan);
0094                 fileName->setForeground(Qt::darkCyan);
0095                 check->setCheckState(Qt::Checked);
0096                 currentStatusString = QLatin1String("A");
0097                 break;
0098             case KVersionControlPlugin::LocallyModifiedVersion:
0099                 status->setForeground(Qt::blue);
0100                 fileName->setForeground(Qt::blue);
0101                 check->setCheckState(Qt::Checked);
0102                 currentStatusString = QLatin1String("M");
0103                 break;
0104             case KVersionControlPlugin::RemovedVersion:
0105                 status->setForeground(Qt::red);
0106                 fileName->setForeground(Qt::red);
0107                 check->setCheckState(Qt::Checked);
0108                 currentStatusString = QLatin1String("R");
0109                 break;
0110             case KVersionControlPlugin::UnversionedVersion:
0111                 status->setForeground(Qt::darkMagenta);
0112                 fileName->setForeground(Qt::darkMagenta);
0113                 currentStatusString = QLatin1String("?");
0114                 break;
0115             case KVersionControlPlugin::IgnoredVersion:
0116                 status->setForeground(Qt::black);
0117                 fileName->setForeground(Qt::black);
0118                 currentStatusString = QLatin1String("I");
0119                 break;
0120             case KVersionControlPlugin::MissingVersion:
0121                 status->setForeground(Qt::black);
0122                 fileName->setForeground(Qt::black);
0123                 currentStatusString = QLatin1String("!");
0124                 break;
0125             default:
0126                 break;
0127         }
0128 
0129         status->setText(QString(currentStatusString));
0130         fileName->setText(currentFile);
0131 
0132         m_statusTable->insertRow(rowCount);
0133         check->setCheckState(Qt::Checked); //Change. except untracked, ignored
0134         m_statusTable->setItem(rowCount, 0, check);
0135         m_statusTable->setItem(rowCount, 1, status);
0136         m_statusTable->setItem(rowCount, 2, fileName);
0137 
0138         ++rowCount;
0139     }
0140 }
0141 
0142 bool HgStatusList::getSelectionForCommit(QStringList &files)
0143 {
0144     int nChecked = 0;
0145     int nRowCount = m_statusTable->rowCount();
0146     for (int row = 0; row < nRowCount; row++) {
0147         QTableWidgetItem *item = m_statusTable->item(row, 0);
0148         if (item->checkState() == Qt::Checked) {
0149             nChecked++;
0150             files << m_statusTable->item(row, 2)->text();
0151         }
0152     }
0153     // if all files are selected, clear the list
0154     if (nChecked == nRowCount) {
0155         files.clear();
0156     }
0157     // at least one file is checked
0158     if (nChecked > 0) {
0159         return true;
0160     }
0161     //nothing is selected
0162     return false;
0163 }
0164 
0165 void HgStatusList::headerClickedSlot(int index)
0166 {
0167     if (index == 0) { // first column with check boxes
0168         m_allWhereChecked = !m_allWhereChecked;
0169         Qt::CheckState state = m_allWhereChecked ? Qt::Checked : Qt::Unchecked;
0170         for (int row = 0; row < m_statusTable->rowCount(); row++) {
0171             m_statusTable->item(row, 0)->setCheckState(state);
0172         }
0173         m_statusTable->horizontalHeader()->setSortIndicatorShown(false); // it might be set by 2-nd column
0174     } else if (index == 2) { // column with file names
0175         m_sortIndex = !m_sortIndex;;
0176         if (m_sortIndex) {
0177             m_statusTable->horizontalHeader()->setSortIndicator(index, Qt::AscendingOrder);
0178         }
0179         else {
0180             m_statusTable->horizontalHeader()->setSortIndicator(index, Qt::DescendingOrder);
0181         }
0182         m_statusTable->horizontalHeader()->setSortIndicatorShown(true);
0183         m_statusTable->sortByColumn(index, Qt::AscendingOrder);
0184     }
0185 }
0186 
0187 
0188 
0189 
0190 #include "moc_statuslist.cpp"