Partially implemented template editor
authorHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 12:15:18 +0000 (17:45 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 29 Nov 2010 12:15:18 +0000 (17:45 +0530)
Partially implemented template editor dialog. To complete the
template updating functionality on changing.

biaweb_db.py
editor_dialog.py [new file with mode: 0644]
editor_dialog.ui [new file with mode: 0644]
highlighter.py
templates_dialog.py
templates_dialog.ui
ui_editor_dialog.py [new file with mode: 0644]
ui_templates_dialog.py

index 80e3915..c2761f5 100644 (file)
@@ -6,6 +6,20 @@ import os
 import os.path
 import time
 
+# function to get the template text for a particular template
+def get_template_text (dbname, template_name):
+       try:
+               conn = sqlite3.connect (dbname)
+               c = conn.cursor ()
+               c.execute ("SELECT template_content FROM templates WHERE template_name=?",
+                                               (template_name,))
+               conn.commit ()
+               row = c.fetchone ()
+               conn.close ()
+               return row[0]
+       except sqlite3.Error:
+               return False
+
 # function to retrieve the templates from the database
 def get_templates (dbname):
        try:
diff --git a/editor_dialog.py b/editor_dialog.py
new file mode 100644 (file)
index 0000000..9c35a22
--- /dev/null
@@ -0,0 +1,41 @@
+# BiaWeb Website content manager (c) 2010 V.Harishankar
+# Editor dialog class
+
+import PyQt4
+import ui_editor_dialog
+import highlighter
+
+class EditorDialog (PyQt4.QtGui.QDialog, ui_editor_dialog.Ui_EditorDialog):
+       def __init__ (self, master, title, init_text):
+               PyQt4.QtGui.QDialog.__init__ (self, master)
+               self.setupUi (self)
+
+               # set the flags to a normal maximizable window
+               self.setWindowFlags (PyQt4.QtCore.Qt.Window)
+
+               # set the title
+               self.setWindowTitle ("Editor - " + title)
+
+               # set text highlighting
+               highlighter.SimpleHtmlHighlighter (self.template_text.document ())
+
+               self.template_text.setPlainText (init_text)
+               # set modified flag to false
+               self.modified = False
+
+       # when cancelled check if modified and confirm first
+       def reject (self):
+               # if modified ask confirmation first
+               if self.modified == True:
+                       ans = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
+                                               "Changes will be lost. Are you sure you wish to cancel editing?",
+                                               PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
+                       # if confirmed
+                       if ans == PyQt4.QtGui.QMessageBox.Yes:
+                               PyQt4.QtGui.QDialog.reject (self)
+               # not modified
+               else:
+                       PyQt4.QtGui.QDialog.reject (self)
+
+       def onTextChanged (self):
+               self.modified = True
diff --git a/editor_dialog.ui b/editor_dialog.ui
new file mode 100644 (file)
index 0000000..8a86b7c
--- /dev/null
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>EditorDialog</class>
+ <widget class="QDialog" name="EditorDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>663</width>
+    <height>463</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Editor</string>
+  </property>
+  <layout class="QGridLayout" name="gridLayout">
+   <item row="0" column="0">
+    <widget class="QPlainTextEdit" name="template_text">
+     <property name="font">
+      <font>
+       <family>Monospace</family>
+      </font>
+     </property>
+     <property name="verticalScrollBarPolicy">
+      <enum>Qt::ScrollBarAlwaysOn</enum>
+     </property>
+     <property name="tabChangesFocus">
+      <bool>true</bool>
+     </property>
+     <property name="lineWrapMode">
+      <enum>QPlainTextEdit::NoWrap</enum>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="0">
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>EditorDialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>227</x>
+     <y>441</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>EditorDialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>295</x>
+     <y>447</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>template_text</sender>
+   <signal>textChanged()</signal>
+   <receiver>EditorDialog</receiver>
+   <slot>onTextChanged()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>408</x>
+     <y>298</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>270</x>
+     <y>457</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+ <slots>
+  <slot>onTextChanged()</slot>
+ </slots>
+</ui>
index 3213755..e963a7f 100644 (file)
@@ -23,10 +23,16 @@ class SimpleHtmlHighlighter (PyQt4.QtGui.QSyntaxHighlighter):
                charfmt3 = PyQt4.QtGui.QTextCharFormat ()
                charfmt3.setForeground (PyQt4.QtCore.Qt.magenta)
 
+               # for template variables - ${temp_var}
+               charfmt4 = PyQt4.QtGui.QTextCharFormat ()
+               charfmt4.setForeground (PyQt4.QtCore.Qt.darkGray)
+               charfmt4.setFontItalic (True)
+
                # matching regular expressions
                htmltagexps = [ (PyQt4.QtCore.QRegExp ("<[^<>]+>"), charfmt1),
                                                (PyQt4.QtCore.QRegExp ("\"[^\"]+\""), charfmt2),
-                                               (PyQt4.QtCore.QRegExp ("&[^;]+;"), charfmt3)
+                                               (PyQt4.QtCore.QRegExp ("&[^;]+;"), charfmt3),
+                                               (PyQt4.QtCore.QRegExp ("\$\{[^\}]+\}"), charfmt4)
                                        ]
 
                # run through the list of regular expressions to highlight
index 8a57482..efd5bd7 100644 (file)
@@ -3,6 +3,7 @@
 
 import PyQt4
 import ui_templates_dialog
+import editor_dialog
 import biaweb_db
 
 class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDialog):
@@ -17,3 +18,18 @@ class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDial
                tpls = biaweb_db.get_templates (self.current_db)
                for tpl_name, tpl_content in tpls:
                        self.templates.addItem (str (tpl_name))
+
+       def onEdit (self):
+               # get the currently selected list item
+               tpl_name = self.templates.currentItem ().data (0).toString ()
+
+               # get the template content from the database
+               tpl_str = biaweb_db.get_template_text (self.current_db, str (tpl_name))
+
+               # if template string cannot be obtained for some reason
+               if tpl_str == False:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in retrieving template")
+               else:
+                       # open edit dialog with title "template name" and text as the template string
+                       edlg = editor_dialog.EditorDialog (self, tpl_name, tpl_str)
+                       edlg.exec_ ()
index e3f77aa..d8e5a04 100644 (file)
@@ -51,6 +51,7 @@ selected template</string>
   </layout>
  </widget>
  <tabstops>
+  <tabstop>templates</tabstop>
   <tabstop>edit</tabstop>
   <tabstop>close</tabstop>
  </tabstops>
@@ -63,8 +64,8 @@ selected template</string>
    <slot>accept()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>741</x>
-     <y>525</y>
+     <x>340</x>
+     <y>246</y>
     </hint>
     <hint type="destinationlabel">
      <x>288</x>
@@ -72,6 +73,22 @@ selected template</string>
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>edit</sender>
+   <signal>clicked()</signal>
+   <receiver>TemplatesDialog</receiver>
+   <slot>onEdit()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>163</x>
+     <y>235</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>83</x>
+     <y>224</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
  <slots>
   <slot>onEdit()</slot>
diff --git a/ui_editor_dialog.py b/ui_editor_dialog.py
new file mode 100644 (file)
index 0000000..62b5e44
--- /dev/null
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+
+# Form implementation generated from reading ui file 'editor_dialog.ui'
+#
+# Created: Mon Nov 29 17:43:54 2010
+#      by: PyQt4 UI code generator 4.7.4
+#
+# WARNING! All changes made in this file will be lost!
+
+from PyQt4 import QtCore, QtGui
+
+class Ui_EditorDialog(object):
+    def setupUi(self, EditorDialog):
+        EditorDialog.setObjectName("EditorDialog")
+        EditorDialog.resize(663, 463)
+        self.gridLayout = QtGui.QGridLayout(EditorDialog)
+        self.gridLayout.setObjectName("gridLayout")
+        self.template_text = QtGui.QPlainTextEdit(EditorDialog)
+        font = QtGui.QFont()
+        font.setFamily("Monospace")
+        self.template_text.setFont(font)
+        self.template_text.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
+        self.template_text.setTabChangesFocus(True)
+        self.template_text.setLineWrapMode(QtGui.QPlainTextEdit.NoWrap)
+        self.template_text.setObjectName("template_text")
+        self.gridLayout.addWidget(self.template_text, 0, 0, 1, 1)
+        self.buttonBox = QtGui.QDialogButtonBox(EditorDialog)
+        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
+        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
+        self.buttonBox.setObjectName("buttonBox")
+        self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)
+
+        self.retranslateUi(EditorDialog)
+        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), EditorDialog.accept)
+        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), EditorDialog.reject)
+        QtCore.QObject.connect(self.template_text, QtCore.SIGNAL("textChanged()"), EditorDialog.onTextChanged)
+        QtCore.QMetaObject.connectSlotsByName(EditorDialog)
+
+    def retranslateUi(self, EditorDialog):
+        EditorDialog.setWindowTitle(QtGui.QApplication.translate("EditorDialog", "Editor", None, QtGui.QApplication.UnicodeUTF8))
+
index 895e2be..7c2fce4 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'templates_dialog.ui'
 #
-# Created: Mon Nov 29 15:30:13 2010
+# Created: Mon Nov 29 16:38:12 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -32,7 +32,9 @@ class Ui_TemplatesDialog(object):
 
         self.retranslateUi(TemplatesDialog)
         QtCore.QObject.connect(self.close, QtCore.SIGNAL("clicked()"), TemplatesDialog.accept)
+        QtCore.QObject.connect(self.edit, QtCore.SIGNAL("clicked()"), TemplatesDialog.onEdit)
         QtCore.QMetaObject.connectSlotsByName(TemplatesDialog)
+        TemplatesDialog.setTabOrder(self.templates, self.edit)
         TemplatesDialog.setTabOrder(self.edit, self.close)
 
     def retranslateUi(self, TemplatesDialog):