changeset 479:044b5393f8bc

Translator: added ClosingProjectDialog.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Mon, 12 Nov 2007 17:18:30 +0100
parents 2949146f2781
children f25985f6b222
files trunk/src/translator/closing_project.ui trunk/src/translator/translator.py trunk/src/translator/ui_closing_project.py
diffstat 3 files changed, 271 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/src/translator/closing_project.ui	Mon Nov 12 17:18:30 2007 +0100
@@ -0,0 +1,125 @@
+<ui version="4.0" >
+ <class>ClosingProjectDialog</class>
+ <widget class="QDialog" name="ClosingProjectDialog" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>300</height>
+   </rect>
+  </property>
+  <property name="windowTitle" >
+   <string>Closing Project</string>
+  </property>
+  <layout class="QVBoxLayout" >
+   <item>
+    <widget class="QLabel" name="label" >
+     <property name="text" >
+      <string>The following documents have been modified:</string>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QTreeWidget" name="treeWidget" >
+     <column>
+      <property name="text" >
+       <string>Title</string>
+      </property>
+     </column>
+     <column>
+      <property name="text" >
+       <string>Full Path</string>
+      </property>
+     </column>
+    </widget>
+   </item>
+   <item>
+    <spacer>
+     <property name="orientation" >
+      <enum>Qt::Vertical</enum>
+     </property>
+     <property name="sizeHint" >
+      <size>
+       <width>382</width>
+       <height>33</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
+   <item>
+    <layout class="QHBoxLayout" >
+     <item>
+      <spacer>
+       <property name="orientation" >
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" >
+        <size>
+         <width>40</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item>
+      <widget class="QPushButton" name="button_Save_Selected" >
+       <property name="text" >
+        <string>&amp;Save Selected</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="button_Discard_All" >
+       <property name="text" >
+        <string>&amp;Discard All Changes</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="button_Cancel" >
+       <property name="text" >
+        <string>&amp;Cancel</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>button_Save_Selected</sender>
+   <signal>clicked()</signal>
+   <receiver>ClosingProjectDialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>144</x>
+     <y>269</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>179</x>
+     <y>238</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>button_Cancel</sender>
+   <signal>clicked()</signal>
+   <receiver>ClosingProjectDialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>337</x>
+     <y>268</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>328</x>
+     <y>248</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
--- a/trunk/src/translator/translator.py	Sun Nov 11 16:43:37 2007 +0200
+++ b/trunk/src/translator/translator.py	Mon Nov 12 17:18:30 2007 +0100
@@ -12,6 +12,7 @@
 from ui_new_project import Ui_NewProjectDialog
 from ui_project_properties import Ui_ProjectProperties
 from ui_msg_form import Ui_MsgForm
+from ui_closing_project import Ui_ClosingProjectDialog
 
 from project import Project, newProjectData
 
@@ -29,9 +30,9 @@
 Qt.SLOT = QtCore.SLOT
 
 def QTabWidgetCloseAll(self):
- for i in range(0, self.count()):
-   widget = self.widget(0)
-   self.removeTab(0)
+ for i in range(self.count()-1,-1,-1):
+   widget = self.widget(i)
+   self.removeTab(i)
    widget.close()
 QtGui.QTabWidget.closeAll = QTabWidgetCloseAll
 
@@ -50,17 +51,18 @@
     self.projectDock.setWidget(self.projectTree)
     self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.projectDock)
     # Custom connections
-    Qt.connect(self.action_About, Qt.SIGNAL("triggered()"), self.showAboutDialog)
-    Qt.connect(self.action_New_Project, Qt.SIGNAL("triggered()"), self.createNewProject)
-    Qt.connect(self.action_Open_Project, Qt.SIGNAL("triggered()"), self.openProjectAction)
-    Qt.connect(self.action_Close_Project, Qt.SIGNAL("triggered()"), self.closeProject)
-    Qt.connect(self.action_Save, Qt.SIGNAL("triggered()"), self.saveForm)
-    Qt.connect(self.action_Save_All, Qt.SIGNAL("triggered()"), self.saveAllForms)
-    Qt.connect(self.action_Close, Qt.SIGNAL("triggered()"), self.closeForm)
-    Qt.connect(self.action_Close_All, Qt.SIGNAL("triggered()"), self.closeAllForms)
-    Qt.connect(self.action_Properties, Qt.SIGNAL("triggered()"), self.showProjectProperties)
-    Qt.connect(self.action_Add_Catalogue, Qt.SIGNAL("triggered()"), self.addCatalogue)
-    Qt.connect(self.action_Add_New_Catalogue, QtCore.SIGNAL("triggered()"), self.addNewCatalogue)
+    triggered = Qt.SIGNAL("triggered()")
+    Qt.connect(self.action_About, triggered, self.showAboutDialog)
+    Qt.connect(self.action_New_Project, triggered, self.createNewProject)
+    Qt.connect(self.action_Open_Project, triggered, self.openProjectAction)
+    Qt.connect(self.action_Close_Project, triggered, self.closeProjectAction)
+    Qt.connect(self.action_Save, triggered, self.saveForm)
+    Qt.connect(self.action_Save_All, triggered, self.saveAllForms)
+    Qt.connect(self.action_Close, triggered, self.closeForm)
+    Qt.connect(self.action_Close_All, triggered, self.closeAllForms)
+    Qt.connect(self.action_Properties, triggered, self.showProjectProperties)
+    Qt.connect(self.action_Add_Catalogue, triggered, self.addCatalogue)
+    Qt.connect(self.action_Add_New_Catalogue, triggered, self.addNewCatalogue)
     Qt.connect(self.projectTree, Qt.SIGNAL("itemDoubleClicked(QTreeWidgetItem*,int)"), self.projectTreeItemDblClicked)
     Qt.connect(self.projectTree, Qt.SIGNAL("onKeyEnter"), self.projectTreeItemActivated)
     Qt.connect(self.projectTree, Qt.SIGNAL("onKeyDelete"), self.projectTreeItemDeleted)
@@ -78,19 +80,21 @@
     dialog.exec_()
 
   def createNewProject(self):
-    if self.cantCloseProjectIfOpen():
+    if self.rejectClosingProject():
       return
     dialog = NewProjectDialog()
     code = dialog.exec_()
     if code == QtGui.QDialog.Accepted:
+      self.closeProject()
       self.openProject(str(dialog.projectFilePath.text()))
 
   def openProjectAction(self):
-    if self.cantCloseProjectIfOpen():
+    if self.rejectClosingProject():
       return
     filePath = QtGui.QFileDialog.getOpenFileName(self, "Select Project File", g_CWD, "Translator Project (*%s)" % g_projectExt);
     filePath = str(filePath)
     if filePath:
+      self.closeProject()
       self.openProject(filePath)
 
   def openProject(self, filePath):
@@ -103,6 +107,10 @@
     self.enableMenuItems()
     self.projectTree.setProject(self.project)
 
+  def closeProjectAction(self):
+    if not self.rejectClosingProject():
+      self.closeProject()
+
   def addCatalogue(self):
     filePath = QtGui.QFileDialog.getOpenFileName(self, "Select Project File", g_CWD, "Catalogue (*%s)" % g_catExt);
     filePath = str(filePath)
@@ -110,25 +118,37 @@
   def addNewCatalogue(self):
     pass
 
-  def cantCloseProjectIfOpen(self):
+  def rejectClosingProject(self):
     if self.project == None:
       return False
-    return self.closeProject()
+
+    modifiedDocs = []
+    # Check if any open document is modified.
+    for i in range(0, self.pages.count()):
+      if self.pages.widget(i).isModified:
+        modifiedDocs += [self.pages.widget(i)]
+    # Display dialog if so.
+    if len(modifiedDocs):
+      dialog = ClosingProjectDialog(modifiedDocs)
+      code = dialog.exec_()
+      if code == dialog.Accepted:
+        for doc in dialog.getSelectedDocs():
+          self.saveDocument(doc)
+      elif code == dialog.Rejected:
+        return True
+      elif code == dialog.DiscardAll:
+        pass
+
+    return False
 
   def closeProject(self):
     if self.project == None:
-      return True
-    MB = QtGui.QMessageBox
-    button = MB.question(self, "Closing", "Close the current project?", MB.Ok | MB.Cancel, MB.Cancel)
-    if button == MB.Cancel:
-      return False
-
+      return
     del self.project
     self.project = None
     self.disableMenuItems()
     self.projectTree.clear()
     self.pages.closeAll()
-    return True
 
   def enableMenuItems(self):
     #self.action_Close_Project.setEnabled(True)
@@ -184,9 +204,11 @@
 
   def saveDocument(self, form):
     if form.isModified:
+      # Reset tab text.
       index = self.pages.indexOf(form)
       text = form.getDocumentTitle()
       self.pages.setTabText(index, text)
+
       form.save()
 
   def closeForm(self):
@@ -194,7 +216,7 @@
       self.closeDocument(self.pages.currentWidget())
 
   def closeAllForms(self):
-    for i in range(0, self.pages.count()):
+    for i in range(self.pages.count()-1, -1, -1):
       self.closeDocument(self.pages.widget(i))
 
   def closeDocument(self, form):
@@ -211,7 +233,7 @@
     return True
 
   def closeEvent(self, event):
-    if self.closeProject() == False:
+    if self.rejectClosingProject():
       event.ignore()
       return
     self.writeSettings()
@@ -439,7 +461,6 @@
     return self.msgForm
 
   def docClosed(self):
-    print "docClosed()"
     self.msgForm = None
 
 class ProjectItem(QtGui.QTreeWidgetItem):
@@ -502,6 +523,34 @@
     self.msgIDsItem = None
     QtGui.QTreeWidget.clear(self)
 
+
+class ClosingProjectDialog(QtGui.QDialog, Ui_ClosingProjectDialog):
+  DiscardAll = 2
+  def __init__(self, docs):
+    QtGui.QDialog.__init__(self)
+    self.setupUi(self)
+    Qt.connect(self.button_Discard_All, Qt.SIGNAL("clicked()"),   self.discardAll)
+
+    self.items = []
+    for doc in docs:
+      title = doc.getDocumentTitle()
+      path = doc.getDocumentFullPath()
+      item = QtGui.QTreeWidgetItem([title, path])
+      item.doc = doc
+      item.setFlags(item.flags()|Qt.ItemIsUserCheckable);
+      item.setCheckState(0, Qt.Checked)
+      self.items += [item]
+    self.treeWidget.addTopLevelItems(self.items)
+
+    self.button_Cancel.setFocus()
+
+  def getSelectedDocs(self):
+    return [item.doc for item in self.items if item.checkState(0)]
+
+  def discardAll(self):
+    self.done(self.DiscardAll)
+
+
 class NewProjectDialog(QtGui.QDialog, Ui_NewProjectDialog):
   def __init__(self):
     QtGui.QDialog.__init__(self)
@@ -522,11 +571,12 @@
     projectName = str(self.projectName.text())
     filePath = str(self.projectFilePath.text())
 
+    MB = QtGui.QMessageBox
     if projectName == "":
-      QtGui.QMessageBox.warning(self, "Warning", "Please, enter a name for the project.")
+      MB.warning(self, "Warning", "Please, enter a name for the project.")
       return
     if filePath == "":
-      QtGui.QMessageBox.warning(self, "Warning", "Please, choose or enter a path for the project file.")
+      MB.warning(self, "Warning", "Please, choose or enter a path for the project file.")
       return
 
     projectData = newProjectData(projectName)
@@ -537,7 +587,7 @@
     try:
       yaml.dump(projectData, open(filePath, "w"), default_flow_style=False)
     except Exception, e:
-      QtGui.QMessageBox.critical(self, "Error", str(e))
+      MB.critical(self, "Error", str(e))
       return
 
     # Accept and close dialog.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/src/translator/ui_closing_project.py	Mon Nov 12 17:18:30 2007 +0100
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+
+# Form implementation generated from reading ui file 'closing_project.ui'
+#
+# Created: Sun Nov 11 23:01:28 2007
+#      by: PyQt4 UI code generator 4.1
+#
+# WARNING! All changes made in this file will be lost!
+
+import sys
+from PyQt4 import QtCore, QtGui
+
+class Ui_ClosingProjectDialog(object):
+    def setupUi(self, ClosingProjectDialog):
+        ClosingProjectDialog.setObjectName("ClosingProjectDialog")
+        ClosingProjectDialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(ClosingProjectDialog.minimumSizeHint()))
+
+        self.vboxlayout = QtGui.QVBoxLayout(ClosingProjectDialog)
+        self.vboxlayout.setObjectName("vboxlayout")
+
+        self.label = QtGui.QLabel(ClosingProjectDialog)
+        self.label.setObjectName("label")
+        self.vboxlayout.addWidget(self.label)
+
+        self.treeWidget = QtGui.QTreeWidget(ClosingProjectDialog)
+        self.treeWidget.setObjectName("treeWidget")
+        self.vboxlayout.addWidget(self.treeWidget)
+
+        spacerItem = QtGui.QSpacerItem(382,33,QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
+        self.vboxlayout.addItem(spacerItem)
+
+        self.hboxlayout = QtGui.QHBoxLayout()
+        self.hboxlayout.setObjectName("hboxlayout")
+
+        spacerItem1 = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
+        self.hboxlayout.addItem(spacerItem1)
+
+        self.button_Save_Selected = QtGui.QPushButton(ClosingProjectDialog)
+        self.button_Save_Selected.setObjectName("button_Save_Selected")
+        self.hboxlayout.addWidget(self.button_Save_Selected)
+
+        self.button_Discard_All = QtGui.QPushButton(ClosingProjectDialog)
+        self.button_Discard_All.setObjectName("button_Discard_All")
+        self.hboxlayout.addWidget(self.button_Discard_All)
+
+        self.button_Cancel = QtGui.QPushButton(ClosingProjectDialog)
+        self.button_Cancel.setDefault(True)
+        self.button_Cancel.setObjectName("button_Cancel")
+        self.hboxlayout.addWidget(self.button_Cancel)
+        self.vboxlayout.addLayout(self.hboxlayout)
+
+        self.retranslateUi(ClosingProjectDialog)
+        QtCore.QObject.connect(self.button_Save_Selected,QtCore.SIGNAL("clicked()"),ClosingProjectDialog.accept)
+        QtCore.QObject.connect(self.button_Cancel,QtCore.SIGNAL("clicked()"),ClosingProjectDialog.reject)
+        QtCore.QMetaObject.connectSlotsByName(ClosingProjectDialog)
+
+    def retranslateUi(self, ClosingProjectDialog):
+        ClosingProjectDialog.setWindowTitle(QtGui.QApplication.translate("ClosingProjectDialog", "Closing Project", None, QtGui.QApplication.UnicodeUTF8))
+        self.label.setText(QtGui.QApplication.translate("ClosingProjectDialog", "The following documents have been modified:", None, QtGui.QApplication.UnicodeUTF8))
+        self.treeWidget.headerItem().setText(0,QtGui.QApplication.translate("ClosingProjectDialog", "Title", None, QtGui.QApplication.UnicodeUTF8))
+        self.treeWidget.headerItem().setText(1,QtGui.QApplication.translate("ClosingProjectDialog", "Full Path", None, QtGui.QApplication.UnicodeUTF8))
+        self.button_Save_Selected.setText(QtGui.QApplication.translate("ClosingProjectDialog", "&Save Selected", None, QtGui.QApplication.UnicodeUTF8))
+        self.button_Discard_All.setText(QtGui.QApplication.translate("ClosingProjectDialog", "&Discard All Changes", None, QtGui.QApplication.UnicodeUTF8))
+        self.button_Cancel.setText(QtGui.QApplication.translate("ClosingProjectDialog", "&Cancel", None, QtGui.QApplication.UnicodeUTF8))
+