Repository: spyder-ide/spyder

Issue:
Project Explorer is sometimes not set correctly when switching projects
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports

## Problem Description

This bug is easier to reproduce and understand with two different drive with different letters, but it is also possible to reproduce it with only one drive. In fact this problem will occur when the root path of the project we are switching to is not contained in the root path of the project we are switching from.

So lets say that you create one project at `C:/Users/User/cdisk_project/project` and another one on a usb stick at `E:/usb_projects/project`, then switching from one project to the other cause the project explorer to set its root path incorrectly.

### What steps reproduce the problem?

1. Create a project on a given Drive
2. Create another project on another Drive (with a different name)
3.  Switch from one project to the other

### What is the expected output? What do you see instead?

I have already explained this above, but here is a screenshot.

![image](https://user-images.githubusercontent.com/10170372/50162362-2b900500-02ac-11e9-8e26-747446c07974.png)

### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

There is no traceback

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: Spyder 4.0.0.dev0 https://github.com/spyder-ide/spyder/commit/8be3fd8d042425005cfeeeef3e8e21d63149731d
* Python version: Python 3.6.6 64-bit
* Qt version: Qt 5.9.4
* PyQt version: PyQt5 5.9.2
* Operating System name/version: Windows 10 

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```pygments >=2.0    :  2.2.0 (OK)
qdarkstyle >=2.6.4:  2.6.4 (OK)
sphinx >=0.6.6    :  1.8.1 (OK)
pyls >=0.19.0     :  0.19.0 (OK)
nbconvert >=4.0   :  5.3.1 (OK)
pandas >=0.13.1   :  0.23.4 (OK)
numpy >=1.7       :  1.15.4 (OK)
sympy >=0.7.3     :  None (NOK)
cython >=0.21     :  None (NOK)
qtconsole >=4.2.0 :  4.5.0.dev0 (OK)
IPython >=4.0     :  7.0.1 (OK)
matplotlib >=2.0.0:  3.0.1 (OK)
pylint >=0.25     :  2.1.1 (OK)

```



Discussion:
This bug was hard to track down, but easy to solve. So I'm going to submit a PR to fix this soon.
Thanks @jnsebgosselin!

Resolving patch:
diff --git a/spyder/plugins/explorer/widgets.py b/spyder/plugins/explorer/widgets.py
--- a/spyder/plugins/explorer/widgets.py
+++ b/spyder/plugins/explorer/widgets.py
@@ -1019,6 +1019,7 @@ def set_root_path(self, root_path):
         self.root_path = root_path
         self.install_model()
         index = self.fsmodel.setRootPath(root_path)
+        self.proxymodel.setup_filter(self.root_path, [])
         self.setRootIndex(self.proxymodel.mapFromSource(index))
         
     def get_index(self, filename):
diff --git a/spyder/plugins/projects/widgets/explorer.py b/spyder/plugins/projects/widgets/explorer.py
--- a/spyder/plugins/projects/widgets/explorer.py
+++ b/spyder/plugins/projects/widgets/explorer.py
@@ -208,9 +208,8 @@ def closing_widget(self):
     def set_project_dir(self, directory):
         """Set the project directory"""
         if directory is not None:
-            project = directory.split(osp.sep)[-1]
             self.treewidget.set_root_path(osp.dirname(directory))
-            self.treewidget.set_folder_names([project])
+            self.treewidget.set_folder_names([osp.basename(directory)])
         self.treewidget.setup_project_view()
         try:
             self.treewidget.setExpanded(self.treewidget.get_index(directory),


Test patch:
diff --git a/spyder/plugins/projects/tests/test_plugin.py b/spyder/plugins/projects/tests/test_plugin.py
--- a/spyder/plugins/projects/tests/test_plugin.py
+++ b/spyder/plugins/projects/tests/test_plugin.py
@@ -11,6 +11,7 @@
 """
 
 # Standard library imports
+import os.path as osp
 try:
     from unittest.mock import Mock
 except ImportError:
@@ -231,5 +232,37 @@ def test_recent_projects_menu_action(projects, tmpdir):
     assert projects.get_active_project().root_path == path0
 
 
+def test_project_explorer_tree_root(projects, tmpdir, qtbot):
+    """
+    Test that the root item of the project explorer tree widget is set
+    correctly when switching projects.
+
+    Regression test for Issue #8455
+    """
+    qtbot.addWidget(projects.explorer)
+    projects.show_explorer()
+
+    ppath1 = to_text_string(tmpdir.mkdir(u'測試'))
+    ppath2 = to_text_string(tmpdir.mkdir(u'ïèô éàñ').mkdir(u'اختبار'))
+
+    # Open the projects.
+    for ppath in [ppath1, ppath2]:
+        projects.open_project(path=ppath)
+        projects.update_explorer()
+
+        # Check that the root path of the project explorer tree widget is
+        # set correctly.
+        assert projects.get_active_project_path() == ppath
+        assert projects.explorer.treewidget.root_path == osp.dirname(ppath)
+        assert (projects.explorer.treewidget.rootIndex().data() ==
+                osp.basename(osp.dirname(ppath)))
+
+        # Check that the first visible item in the project explorer
+        # tree widget is the folder of the project.
+        topleft_index = (projects.explorer.treewidget.indexAt(
+            projects.explorer.treewidget.rect().topLeft()))
+        assert topleft_index.data() == osp.basename(ppath)
+
+
 if __name__ == "__main__":
     pytest.main()