summaryrefslogtreecommitdiff
path: root/src/frontEnd/ProjectExplorer.py
blob: ca525f870b596d77795be4de685c2aa214cba8bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from PyQt4 import QtGui, QtCore
import os
import json
from configuration.Appconfig import Appconfig


# This is main class for Project Explorer Area.
class ProjectExplorer(QtGui.QWidget):
    """
    This class contains function:
        a)One work as a constructor(__init__).
        b)For saving data.
        c)for renaming project.
        d)for refreshing project.
        e)for removing project.
        f) for saving data.
    """

    def __init__(self):
        """
        This method is doing following tasks:
            a)Working as a constructor for class ProjectExplorer.
            b)view of project explorer area.
        """
        QtGui.QWidget.__init__(self)
        self.obj_appconfig = Appconfig()
        self.treewidget = QtGui.QTreeWidget()
        self.window = QtGui.QVBoxLayout()
        header = QtGui.QTreeWidgetItem(["Projects", "path"])
        self.treewidget.setHeaderItem(header)
        self.treewidget.setColumnHidden(1, True)

        # CSS
        self.treewidget.setStyleSheet(" \
            QTreeView::branch:has-siblings:!adjoins-item { \
            border-image: url(../../images/vline.png) 0;} \
            QTreeView::branch:has-siblings:adjoins-item { \
            border-image: url(../../images/branch-more.png) 0; } \
            QTreeView::branch:!has-children:!has-siblings:adjoins-item { \
            border-image: url(../../images/branch-end.png) 0; } \
            QTreeView::branch:has-children:!has-siblings:closed, \
            QTreeView::branch:closed:has-children:has-siblings { \
            border-image: none; \
            image: url(../../images/branch-closed.png); } \
            QTreeView::branch:open:has-children:!has-siblings, \
            QTreeView::branch:open:has-children:has-siblings  { \
            border-image: none; \
            image: url(../../images/branch-open.png); } \
        ")

        for parents, children in list(
                self.obj_appconfig.project_explorer.items()):
            os.path.join(parents)
            if os.path.exists(parents):
                pathlist = parents.split(os.sep)
                parentnode = QtGui.QTreeWidgetItem(
                    self.treewidget, [pathlist[-1], parents]
                    )
                for files in children:
                    QtGui.QTreeWidgetItem(
                        parentnode, [files, os.path.join(parents, files)])
        self.window.addWidget(self.treewidget)

        self.treewidget.doubleClicked.connect(self.openProject)
        self.treewidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.treewidget.customContextMenuRequested.connect(self.openMenu)
        self.setLayout(self.window)
        self.show()

    def addTreeNode(self, parents, children):
        os.path.join(parents)
        pathlist = parents.split(os.sep)
        parentnode = QtGui.QTreeWidgetItem(
            self.treewidget, [pathlist[-1], parents])
        for files in children:
            QtGui.QTreeWidgetItem(
                parentnode, [files, os.path.join(parents, files)]
            )

        (
            self.obj_appconfig.
            proc_dict[self.obj_appconfig.current_project['ProjectName']]
        ) = []
        (
            self.obj_appconfig.
            dock_dict[self.obj_appconfig.current_project['ProjectName']]
        ) = []

    def openMenu(self, position):

        indexes = self.treewidget.selectedIndexes()
        if len(indexes) > 0:
            level = 0
            index = indexes[0]
            while index.parent().isValid():
                index = index.parent()
                level += 1

        menu = QtGui.QMenu()
        if level == 0:
            renameProject = menu.addAction(self.tr("Rename Project"))
            renameProject.triggered.connect(self.renameProject)
            deleteproject = menu.addAction(self.tr("Remove Project"))
            deleteproject.triggered.connect(self.removeProject)
            refreshproject = menu.addAction(self.tr("Refresh"))
            refreshproject.triggered.connect(self.refreshProject)
        elif level == 1:
            openfile = menu.addAction(self.tr("Open"))
            openfile.triggered.connect(self.openProject)

        menu.exec_(self.treewidget.viewport().mapToGlobal(position))

    def openProject(self):
        self.indexItem = self.treewidget.currentIndex()
        filename = str(self.indexItem.data())
        self.filePath = str(
            self.indexItem.sibling(
                self.indexItem.row(),
                1).data())
        self.obj_appconfig.print_info(
            'The current project is ' + self.filePath)

        self.textwindow = QtGui.QWidget()
        self.textwindow.setMinimumSize(600, 500)
        self.textwindow.setGeometry(QtCore.QRect(400, 150, 400, 400))
        self.textwindow.setWindowTitle(filename)

        self.text = QtGui.QTextEdit()
        self.save = QtGui.QPushButton('Save and Exit')
        self.save.setDisabled(True)
        self.windowgrid = QtGui.QGridLayout()
        # if (os.path.isfile(str(self.filePath))) == True:
        if (os.path.isfile(str(self.filePath))):
            self.fopen = open(str(self.filePath), 'r')
            lines = self.fopen.read()
            self.text.setText(lines)

            QtCore.QObject.connect(
                self.text,
                QtCore.SIGNAL("textChanged()"),
                self.enable_save)

            vbox_main = QtGui.QVBoxLayout(self.textwindow)
            vbox_main.addWidget(self.text)
            vbox_main.addWidget(self.save)
            self.save.clicked.connect(self.save_data)
            # self.connect(exit,QtCore.SIGNAL('close()'), self.onQuit)

            self.textwindow.show()
        else:
            self.obj_appconfig.current_project["ProjectName"] = str(
                self.filePath)
            (
                self.obj_appconfig.
                proc_dict[self.obj_appconfig.current_project['ProjectName']]
            ) = []
            if (
                self.obj_appconfig.current_project['ProjectName'] not in
                self.obj_appconfig.dock_dict
            ):
                (
                    self.obj_appconfig.
                    dock_dict[
                        self.obj_appconfig.current_project['ProjectName']]
                ) = []

    # This function is enabling save button option.
    def enable_save(self):
        self.save.setEnabled(True)

    # This function is saving data before it closes the given file.
    def save_data(self):
        """
        This function first opens file in write-mode, when write
        operation is performed it closes that file and then window.
        """
        self.fopen = open(self.filePath, 'w')
        self.fopen.write(self.text.toPlainText())
        self.fopen.close()
        self.textwindow.close()

    # This function removes the project in explorer area by right
    # clicking on project and selecting remove option.
    def removeProject(self):
        """

        """
        self.indexItem = self.treewidget.currentIndex()
        self.filePath = str(
            self.indexItem.sibling(
                self.indexItem.row(),
                1).data())
        self.int = self.indexItem.row()
        self.treewidget.takeTopLevelItem(self.int)

        if self.obj_appconfig.current_project["ProjectName"] == self.filePath:
            self.obj_appconfig.current_project["ProjectName"] = None

        del self.obj_appconfig.project_explorer[str(self.filePath)]
        json.dump(self.obj_appconfig.project_explorer,
                  open(self.obj_appconfig.dictPath, 'w'))

    # This function refresh the project in explorer area by right
    # clicking on project and selecting refresh option.
    def refreshProject(self):
        """ """
        self.indexItem = self.treewidget.currentIndex()
        self.filePath = str(
            self.indexItem.sibling(
                self.indexItem.row(),
                1).data())
        filelistnew = os.listdir(os.path.join(self.filePath))
        parentnode = self.treewidget.currentItem()
        count = parentnode.childCount()
        for i in range(count):
            for items in self.treewidget.selectedItems():
                items.removeChild(items.child(0))
        for files in filelistnew:
            QtGui.QTreeWidgetItem(
                parentnode, [
                    files, os.path.join(
                        self.filePath, files)])

        self.obj_appconfig.project_explorer[self.filePath] = filelistnew
        json.dump(self.obj_appconfig.project_explorer,
                  open(self.obj_appconfig.dictPath, 'w'))

    # """
    def renameProject(self):
        indexItem = self.treewidget.currentIndex()
        baseFileName = str(indexItem.data())
        newBaseFileName, ok = QtGui.QInputDialog.getText(
            self, 'Rename Project', 'Project Name:',
            QtGui.QLineEdit.Normal, baseFileName
        )
        if ok and newBaseFileName:
            newBaseFileName = str(newBaseFileName)
            projectPath, projectFiles = list(
                self.obj_appconfig.project_explorer.items())[indexItem.row()]
            updatedProjectFiles = []

            # rename files matching project name
            for projectFile in projectFiles:
                if baseFileName in projectFile:
                    oldFilePath = os.path.join(projectPath, projectFile)
                    projectFile = projectFile.replace(
                        baseFileName, newBaseFileName, 1)
                    newFilePath = os.path.join(projectPath, projectFile)
                    print ("Renaming " + oldFilePath + " to " + newFilePath)
                    os.rename(oldFilePath, newFilePath)

                    updatedProjectFiles.append(projectFile)

            # rename project folder
            updatedProjectPath = newBaseFileName.join(
                projectPath.rsplit(baseFileName, 1))
            print ("Renaming " + projectPath + " to " + updatedProjectPath)
            os.rename(projectPath, updatedProjectPath)

            # update project_explorer dictionary
            del self.obj_appconfig.project_explorer[projectPath]
            self.obj_appconfig.project_explorer[updatedProjectPath] = (
                updatedProjectFiles
            )

            # save project_explorer dictionary on disk
            json.dump(self.obj_appconfig.project_explorer,
                      open(self.obj_appconfig.dictPath, 'w'))

            # recreate project explorer tree
            self.treewidget.clear()
            for parent, children in (
                self.obj_appconfig.project_explorer.items()
            ):
                self.addTreeNode(parent, children)
        # """