changeset 95:d1be9712818a

* Update actions appropriately when selections are changed
author Chris Cannam
date Wed, 24 Nov 2010 16:29:05 +0000
parents 44ed7766d55a
children 87ef2fa9ee8b
files filestatuswidget.cpp filestatuswidget.h hgexpwidget.cpp hgexpwidget.h mainwindow.cpp mainwindow.h
diffstat 6 files changed, 214 insertions(+), 233 deletions(-) [+]
line wrap: on
line diff
--- a/filestatuswidget.cpp	Wed Nov 24 14:41:52 2010 +0000
+++ b/filestatuswidget.cpp	Wed Nov 24 16:29:05 2010 +0000
@@ -73,6 +73,9 @@
         w->setSelectionMode(QListWidget::ExtendedSelection);
         boxlayout->addWidget(w, 1, 0);
 
+        connect(w, SIGNAL(itemSelectionChanged()),
+                this, SLOT(itemSelectionChanged()));
+
         layout->addWidget(box, ++row, 0, 1, 2);
         box->hide();
     }
@@ -85,13 +88,96 @@
     delete m_dateReference;
 }
 
+void FileStatusWidget::itemSelectionChanged()
+{
+    m_selectedFiles.clear();
+
+    DEBUG << "FileStatusWidget::itemSelectionChanged" << endl;
+
+    foreach (QListWidget *w, m_stateListMap) {
+        QList<QListWidgetItem *> sel = w->selectedItems();
+        foreach (QListWidgetItem *i, sel) {
+            m_selectedFiles.push_back(i->text());
+            DEBUG << "file " << i->text() << " is selected" << endl;
+        }
+    }
+
+    emit selectionChanged();
+}
+
 void FileStatusWidget::clearSelections()
 {
+    m_selectedFiles.clear();
     foreach (QListWidget *w, m_stateListMap) {
         w->clearSelection();
     }
 }
 
+bool FileStatusWidget::haveChangesToCommit() const
+{
+    return !m_fileStates.added().empty() ||
+           !m_fileStates.removed().empty() ||
+           !m_fileStates.modified().empty();
+}
+
+bool FileStatusWidget::haveSelection() const
+{
+    return !m_selectedFiles.empty();
+}
+
+QStringList FileStatusWidget::getAllSelectedFiles() const
+{
+    return m_selectedFiles;
+}
+
+QStringList FileStatusWidget::getSelectedCommittableFiles() const
+{
+    QStringList files;
+    foreach (QString f, m_selectedFiles) {
+        switch (m_fileStates.getStateOfFile(f)) {
+        case FileStates::Added:
+        case FileStates::Modified:
+        case FileStates::Removed:
+            files.push_back(f);
+            break;
+        default: break;
+        }
+    }
+    return files;
+}
+
+QStringList FileStatusWidget::getSelectedAddableFiles() const
+{
+    QStringList files;
+    foreach (QString f, m_selectedFiles) {
+        switch (m_fileStates.getStateOfFile(f)) {
+        case FileStates::Unknown:
+        case FileStates::Removed:
+            files.push_back(f);
+            break;
+        default: break;
+        }
+    }
+    return files;
+}
+
+QStringList FileStatusWidget::getSelectedRemovableFiles() const
+{
+    QStringList files;
+    foreach (QString f, m_selectedFiles) {
+        switch (m_fileStates.getStateOfFile(f)) {
+        case FileStates::Clean:
+        case FileStates::Added:
+        case FileStates::Modified:
+        case FileStates::Missing:
+            files.push_back(f);
+            break;
+        default: break;
+        }
+    }
+    return files;
+}
+
 void
 FileStatusWidget::setLocalPath(QString p)
 {
@@ -126,41 +212,63 @@
 }
 
 void
-FileStatusWidget::highlightFile(QListWidget *w, int i)
-{
-    DEBUG << "FileStatusWidget: highlighting file at " << i << endl;
-    QListWidgetItem *item = w->item(i);
-    item->setForeground(Qt::red);
-    //!!! and a nice gold star
-}
-
-void
 FileStatusWidget::updateWidgets()
 {
+    QDateTime lastInteractionTime;
+    if (m_dateReference) {
+        lastInteractionTime = m_dateReference->lastModified();
+        DEBUG << "reference time: " << lastInteractionTime << endl;
+    }
+
+    QSet<QString> selectedFiles;
+    foreach (QString f, m_selectedFiles) selectedFiles.insert(f);
+
     foreach (FileStates::State s, m_stateListMap.keys()) {
+
         QListWidget *w = m_stateListMap[s];
         w->clear();
-        QStringList sl = m_fileStates.getFilesInState(s);
-        w->addItems(sl);
-        w->parentWidget()->setVisible(!sl.empty());
-    }
+        QStringList files = m_fileStates.getFilesInState(s);
 
-    if (m_dateReference) {
-        // Highlight untracked files that have appeared since the
-        // last interaction with the repo
-        QDateTime refTime = m_dateReference->lastModified();
-        DEBUG << "reference time: " << refTime << endl;
-        QListWidget *ul = m_stateListMap[FileStates::Unknown];
-        for (int i = 0; i < ul->count(); ++i) {
-            QString fn(m_localPath + "/" + ul->item(i)->text());
-            DEBUG << "comparing with " << fn << endl;
-            QFileInfo fi(fn);
-            if (fi.exists() && fi.lastModified() > refTime) {
-                DEBUG << "file " << fn << " is newer (" << fi.lastModified()
-                        << ") than reference" << endl;
-                highlightFile(ul, i);
+        QStringList highPriority, lowPriority;
+
+        foreach (QString file, files) {
+
+            bool highlighted = false;
+
+            if (s == FileStates::Unknown) {
+                // We want to highlight untracked files that have appeared
+                // since the last interaction with the repo
+                QString fn(m_localPath + "/" + file);
+                DEBUG << "comparing with " << fn << endl;
+                QFileInfo fi(fn);
+                if (fi.exists() && fi.lastModified() > lastInteractionTime) {
+                    DEBUG << "file " << fn << " is newer (" << fi.lastModified()
+                            << ") than reference" << endl;
+                    highlighted = true;
+                }
+            }
+
+            if (highlighted) {
+                highPriority.push_back(file);
+            } else {
+                lowPriority.push_back(file);
             }
         }
+
+        foreach (QString file, highPriority) {
+            QListWidgetItem *item = new QListWidgetItem(file);
+            w->addItem(item);
+            item->setForeground(Qt::red); //!!! and a nice gold star
+            item->setSelected(selectedFiles.contains(file));
+        }
+
+        foreach (QString file, lowPriority) {
+            QListWidgetItem *item = new QListWidgetItem(file);
+            w->addItem(item);
+            item->setSelected(selectedFiles.contains(file));
+        }
+
+        w->parentWidget()->setVisible(!files.empty());
     }
 }
 
--- a/filestatuswidget.h	Wed Nov 24 14:41:52 2010 +0000
+++ b/filestatuswidget.h	Wed Nov 24 16:29:05 2010 +0000
@@ -43,19 +43,23 @@
     FileStates fileStates() const { return m_fileStates; }
     void setFileStates(FileStates sp);
 
-    bool haveChangesToCommit() const {
-        return !m_fileStates.added().empty() ||
-               !m_fileStates.removed().empty() ||
-               !m_fileStates.modified().empty();
-    }
+    bool haveChangesToCommit() const;
+    bool haveSelection() const;
 
-    QStringList getSelectedCommittableFiles();
-    QStringList getSelectedAddableFiles();
-    QStringList getSelectedRemoveableFiles();
+    QStringList getAllSelectedFiles() const;
+    QStringList getSelectedCommittableFiles() const;
+    QStringList getSelectedAddableFiles() const;
+    QStringList getSelectedRemovableFiles() const;
+
+signals:
+    void selectionChanged();
 
 public slots:
     void clearSelections();
 
+private slots:
+    void itemSelectionChanged();
+
 private:
     QString m_localPath;
     QLabel *m_localPathLabel;
@@ -67,9 +71,9 @@
     QMap<FileStates::State, QListWidget *> m_stateListMap;
 
     QFileInfo *m_dateReference;
+    QStringList m_selectedFiles;
 
     void updateWidgets();
-    void highlightFile(QListWidget *, int);
 };
 
 #endif
--- a/hgexpwidget.cpp	Wed Nov 24 14:41:52 2010 +0000
+++ b/hgexpwidget.cpp	Wed Nov 24 16:29:05 2010 +0000
@@ -31,15 +31,17 @@
 
 #include <iostream>
 
-HgExpWidget::HgExpWidget(QWidget *parent, QString remoteRepo,
-                         QString workFolderPath,
-                         unsigned char viewFileTypesBits) :
+HgExpWidget::HgExpWidget(QWidget *parent,
+                         QString remoteRepo,
+                         QString workFolderPath) :
     QTabWidget(parent)
 {
     // Work page
     fileStatusWidget = new FileStatusWidget;
     fileStatusWidget->setLocalPath(workFolderPath);
     fileStatusWidget->setRemoteURL(remoteRepo);
+    connect(fileStatusWidget, SIGNAL(selectionChanged()),
+            this, SIGNAL(selectionChanged()));
     addTab(fileStatusWidget, tr("My work"));
 
     // History graph page
@@ -67,6 +69,46 @@
     return fileStatusWidget->haveChangesToCommit();
 }
 
+bool HgExpWidget::canAdd() const
+{
+    if (fileStatusWidget->getSelectedAddableFiles().empty()) return false;
+    if (!fileStatusWidget->getSelectedCommittableFiles().empty()) return false;
+    if (!fileStatusWidget->getSelectedRemovableFiles().empty()) return false;
+    return true;
+}
+
+bool HgExpWidget::canRemove() const
+{
+    if (fileStatusWidget->getSelectedRemovableFiles().empty()) return false;
+    if (!fileStatusWidget->getSelectedAddableFiles().empty()) return false;
+    return true;
+}
+
+bool HgExpWidget::canDoDiff() const
+{
+    return canCommit();
+}
+
+QStringList HgExpWidget::getAllSelectedFiles() const
+{
+    return fileStatusWidget->getAllSelectedFiles();
+}
+
+QStringList HgExpWidget::getSelectedCommittableFiles() const
+{
+    return fileStatusWidget->getSelectedCommittableFiles();
+}
+
+QStringList HgExpWidget::getSelectedAddableFiles() const
+{
+    return fileStatusWidget->getSelectedAddableFiles();
+}
+
+QStringList HgExpWidget::getSelectedRemovableFiles() const
+{
+    return fileStatusWidget->getSelectedRemovableFiles();
+}
+
 void HgExpWidget::updateWorkFolderFileList(QString fileList)
 {
     fileStates.parseStates(fileList);
--- a/hgexpwidget.h	Wed Nov 24 14:41:52 2010 +0000
+++ b/hgexpwidget.h	Wed Nov 24 16:29:05 2010 +0000
@@ -30,8 +30,6 @@
 #include <QLabel>
 #include <QTabWidget>
 
-#define NUM_STAT_FILE_TYPES 7
-
 class FileStatusWidget;
 
 
@@ -40,8 +38,7 @@
     Q_OBJECT
 
 public:
-    HgExpWidget(QWidget *parent, QString remoteRepo, QString workFolderPath,
-                unsigned char viewFileTypesBits = DEFAULT_HG_STAT_BITS);
+    HgExpWidget(QWidget *parent, QString remoteRepo, QString workFolderPath);
 
     void updateWorkFolderFileList(QString fileList);
     void updateLocalRepoHgLogList(QString hgLogList);
@@ -52,7 +49,15 @@
     bool canCommit() const;
     bool canAdd() const;
     bool canRemove() const;
-    bool canDoFolderDiff() const;
+    bool canDoDiff() const;
+
+    QStringList getAllSelectedFiles() const;
+    QStringList getSelectedCommittableFiles() const;
+    QStringList getSelectedAddableFiles() const;
+    QStringList getSelectedRemovableFiles() const;
+
+signals:
+    void selectionChanged();
 
 public slots:
     void clearSelections();
--- a/mainwindow.cpp	Wed Nov 24 14:41:52 2010 +0000
+++ b/mainwindow.cpp	Wed Nov 24 16:29:05 2010 +0000
@@ -67,9 +67,12 @@
 
     tabPage = 0;
     justMerged = false;
-    hgExp = new HgExpWidget((QWidget *) this, remoteRepoPath, workFolderPath, initialFileTypesBits);
+    hgExp = new HgExpWidget((QWidget *) this, remoteRepoPath, workFolderPath);
     setCentralWidget(hgExp);
 
+    connect(hgExp, SIGNAL(selectionChanged()),
+            this, SLOT(enableDisableActions()));
+
     setUnifiedTitleAndToolBarOnMac(true);
     connectActions();
     enableDisableActions();
@@ -1062,184 +1065,6 @@
     }
 }
 
-
-bool MainWindow::areAllSelectedCommitable(QListWidget *workList)
-{
-    QList<QListWidgetItem *> selList = workList -> selectedItems();
-    for (int i = 0; i < selList.size(); ++i)
-    {
-        QString tmp = selList.at(i) -> text().mid(0, 1);
-        if (tmp == "A")
-        {
-            //scheduled to be added, ok to commit
-        }
-        else if (tmp == "M")
-        {
-            //locally modified, ok to commit
-        }
-        else if (tmp == "R")
-        {
-            //user wants to remove from repo, ok to commit
-        }
-        else
-        {
-            return false;
-        }
-    }
-    return true;
-}
-
-bool MainWindow::isSelectedDeletable(QListWidget *workList)
-{
-    QList<QListWidgetItem *> selList = workList -> selectedItems();
-    if (selList.count() == 1)
-    {
-        QString tmp = selList.at(0)->text().mid(0, 1);
-        if (tmp == "A")
-        {
-            //scheduled to be added, ok to remove (won't go to repo)
-            return true;
-        }
-        else if (tmp == "C")
-        {
-            //Tracked but unchanged, ok to remove
-            return true;
-        }
-        else if (tmp == "M")
-        {
-            //locally modified, ok to remove from repo
-            return true;
-        }
-        else if (tmp == "!")
-        {
-            //locally deleted, ok to remove from repo
-            return true;
-        }
-    }
-    return false;
-}
-
-
-bool MainWindow::areAllSelectedUntracked(QListWidget *workList)
-{
-    QList<QListWidgetItem *> selList = workList -> selectedItems();
-    for (int i = 0; i < selList.size(); ++i)
-    {
-        QString tmp = selList.at(i) -> text();
-
-        if (tmp.mid(0,1) != "?")
-        {
-            return false;
-        }
-    }
-    return true;
-}
-
-
-bool MainWindow::isSelectedModified(QListWidget *workList)
-{
-    QList<QListWidgetItem *> selList = workList -> selectedItems();
-    if (selList.count() == 1)
-    {
-        if (selList.at(0)->text().mid(0, 1) == "M")
-        {
-            return true;
-        }
-    }
-    return false;
-}
-
-void MainWindow::countModifications(QListWidget *workList, int& added, int& modified, int& removed, int& notTracked,
-                                    int& selected,
-                                    int& selectedAdded, int& selectedModified, int& selectedRemoved, int& selectedNotTracked)
-{
-    int itemCount = workList -> count();
-    if (itemCount > 0)
-    {
-        int A= 0;
-        int M=0;
-        int R=0;
-        int N=0;
-        int S=0;
-        int SA=0;
-        int SM=0;
-        int SR=0;
-        int SN=0;
-
-        for (int i = 0; i < workList -> count(); i++)
-        {
-            QListWidgetItem *currItem = workList -> item(i);
-
-            QString tmp = currItem->text().mid(0, 1);
-            if (tmp == "M")
-            {
-                M++;
-            }
-            else if (tmp == "R")
-            {
-                R++;
-            }
-            else if (tmp == "A")
-            {
-                A++;
-            }
-            else if (tmp == "?")
-            {
-                N++;
-            }
-        }
-
-        added = A;
-        modified = M;
-        removed = R;
-        notTracked = N;
-
-        QList <QListWidgetItem *> selList = workList -> selectedItems();
-
-        S = selList.size();
-        for (int i = 0; i < selList.size(); ++i)
-        {
-            QString tmp = selList.at(i) -> text();
-
-            if (tmp.mid(0,1) == "A")
-            {
-                SA++;
-            }
-            else if (tmp.mid(0,1) == "M")
-            {
-                SM++;
-            }
-            else if (tmp.mid(0,1) == "R")
-            {
-                SR++;
-            }
-            else if (tmp.mid(0,1) == "?")
-            {
-                SN++;
-            }
-        }
-
-        selected = S;
-        selectedAdded = SA;
-        selectedModified = SM;
-        selectedRemoved = SR;
-        selectedNotTracked = SN;
-    }
-    else
-    {
-        added = 0;
-        modified = 0;
-        removed = 0;
-        notTracked = 0;
-        selected = 0;
-        selectedAdded = 0;
-        selectedModified = 0;
-        selectedRemoved = 0;
-        selectedNotTracked = 0;
-    }
-}
-
-
 void MainWindow::updateFileSystemWatcher()
 {
     //!!! this needs to be incremental when something changes
@@ -1581,8 +1406,11 @@
     DEBUG << "canCommit = " << hgExp->canCommit() << endl;
 
     //!!! new stuff:
+    hgAddAct->setEnabled(localRepoActionsEnabled && hgExp->canAdd());
+    hgRemoveAct->setEnabled(localRepoActionsEnabled && hgExp->canRemove());
     hgCommitAct->setEnabled(localRepoActionsEnabled && hgExp->canCommit());
     hgRevertAct->setEnabled(localRepoActionsEnabled && hgExp->canCommit());
+    hgFolderDiffAct->setEnabled(localRepoActionsEnabled && hgExp->canDoDiff());
 
 /*!!!
     int added, modified, removed, notTracked, selected, selectedAdded, selectedModified, selectedRemoved, selectedNotTracked;
@@ -1844,8 +1672,8 @@
     addToolBar(Qt::LeftToolBarArea, workFolderToolBar);
     workFolderToolBar -> setIconSize(QSize(MY_ICON_SIZE, MY_ICON_SIZE));
 //    workFolderToolBar->addSeparator();
-    workFolderToolBar->addAction(hgFileDiffAct);
-//    workFolderToolBar->addAction(hgFolderDiffAct);
+//    workFolderToolBar->addAction(hgFileDiffAct);
+    workFolderToolBar->addAction(hgFolderDiffAct);
     workFolderToolBar->addSeparator();
     workFolderToolBar->addAction(hgRevertAct);
     workFolderToolBar->addAction(hgUpdateAct);
--- a/mainwindow.h	Wed Nov 24 14:41:52 2010 +0000
+++ b/mainwindow.h	Wed Nov 24 16:29:05 2010 +0000
@@ -71,7 +71,6 @@
     MainWindow();
     HgExpWidget *hgExp;
     void writeSettings();
-    void enableDisableActions();
 
     //Paths to remote repo & workfolder
     //Local repo is directory "./hg/" under work folder
@@ -87,6 +86,7 @@
     void tabChanged(int currTab);
     void commandCompleted();
     void commandFailed();
+    void enableDisableActions();
 
 private slots:
     void about();
@@ -134,13 +134,7 @@
     void splitChangeSets(QStringList *list, QString hgLogOutput);
     bool getCommentOrTag(QString& commentOrTag, QString question, QString dlgTitle);
     void presentLongStdoutToUser(QString stdo);
-    void countModifications(QListWidget *workList, int& added, int& modified, int& removed, int& notTracked,
-        int& selected,
-        int& selectedAdded, int& selectedModified, int& selectedRemoved, int& selectedNotTracked);
-    bool isSelectedModified(QListWidget *workList);
-    bool areAllSelectedUntracked(QListWidget *workList);
-    bool isSelectedDeletable(QListWidget *workList);
-    bool areAllSelectedCommitable(QListWidget *workList);
+
     QString listAllUpIpV4Addresses();
     QString filterTag(QString tag);