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:
--- /dev/null
+# 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
--- /dev/null
+<?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>
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
import PyQt4
import ui_templates_dialog
+import editor_dialog
import biaweb_db
class TemplatesDialog (PyQt4.QtGui.QDialog, ui_templates_dialog.Ui_TemplatesDialog):
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_ ()
</layout>
</widget>
<tabstops>
+ <tabstop>templates</tabstop>
<tabstop>edit</tabstop>
<tabstop>close</tabstop>
</tabstops>
<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>
</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>
--- /dev/null
+# -*- 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))
+
# 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!
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):