Commits

Atsushi Odagiri committed ae845d1

polish as application.

Comments (0)

Files changed (2)

notepad/mainwindow.ui

    <property name="text">
     <string>Quit</string>
    </property>
+   <property name="shortcut">
+    <string>Ctrl+Q</string>
+   </property>
   </action>
  </widget>
  <resources/>
     def __init__(self):
         super(NotePad, self).__init__()
         self.setupUi(self)
-        self.filename = None
+        self._filename = None
+        self.textEdit.document().modificationChanged.connect(self.changed)
+        global windows
+        windows.append(self)
+
+    def changed(self):
+        self.updateTitle()
+
+    def filename(self):
+        return self._filename
+
+    def set_filename(self, filename):
+        self._filename = filename
+        self.updateTitle()
+
+    def updateTitle(self):
+        if self.textEdit.document().isModified():
+            self.setWindowTitle("NotePad - %s *" % self._filename)
+        else:
+            self.setWindowTitle("NotePad - %s" % self._filename)
+    def closeEvent(self, evt):
+        super(NotePad, self).closeEvent(evt)
+        global windows
+        windows.remove(self)
+
 
     @QtCore.pyqtSlot()
     def on_actionClose_triggered(self):
         f = open(filename)
         self.textEdit.setPlainText(unicode(f.read(), self.encoding))
         f.close()
-        self.filename = filename
+        self.textEdit.document().setModified(False)
+        self.set_filename(filename)
 
     @QtCore.pyqtSlot()
     def on_actionSave_triggered(self):
         if self.filename is None:
+            return self.on_actionSave_As_triggered()
+        self.save(self.filename())
+
+    @QtCore.pyqtSlot()
+    def on_actionSave_As_triggered(self):
+        filename = QtGui.QFileDialog.getSaveFileName(self)
+        if filename.isNull() or filename.isEmpty():
             return
+        self.save(filename)
+
+    def save(self, filename):
         text = unicode(self.textEdit.toPlainText())
-        f = open(self.filename, "w")
+        f = open(filename, "w")
         f.write(text.encode(self.encoding))
         f.close()
+        self.textEdit.document().setModified(False)
+        self.set_filename(filename)
 
 
+    @QtCore.pyqtSlot()
+    def on_actionQuit_triggered(self):
+        QtGui.qApp.quit()
+
+    @QtCore.pyqtSlot()
+    def on_actionNew_triggered(self):
+        win = NotePad()
+        win.show()
+
+windows = []
 import sys
 def main():
     app = QtGui.QApplication(sys.argv)