From b517d55320f54925d862aca9dedb363b6d3b156d Mon Sep 17 00:00:00 2001 From: Harishankar Date: Mon, 29 Nov 2010 17:45:18 +0530 Subject: [PATCH] Partially implemented template editor Partially implemented template editor dialog. To complete the template updating functionality on changing. --- biaweb_db.py | 14 ++++++ editor_dialog.py | 41 +++++++++++++++++ editor_dialog.ui | 101 +++++++++++++++++++++++++++++++++++++++++ highlighter.py | 8 +++- templates_dialog.py | 16 +++++++ templates_dialog.ui | 21 ++++++++- ui_editor_dialog.py | 41 +++++++++++++++++ ui_templates_dialog.py | 4 +- 8 files changed, 242 insertions(+), 4 deletions(-) create mode 100644 editor_dialog.py create mode 100644 editor_dialog.ui create mode 100644 ui_editor_dialog.py diff --git a/biaweb_db.py b/biaweb_db.py index 80e3915..c2761f5 100644 --- a/biaweb_db.py +++ b/biaweb_db.py @@ -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 index 0000000..9c35a22 --- /dev/null +++ b/editor_dialog.py @@ -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 index 0000000..8a86b7c --- /dev/null +++ b/editor_dialog.ui @@ -0,0 +1,101 @@ + + + EditorDialog + + + + 0 + 0 + 663 + 463 + + + + Editor + + + + + + + Monospace + + + + Qt::ScrollBarAlwaysOn + + + true + + + QPlainTextEdit::NoWrap + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + EditorDialog + accept() + + + 227 + 441 + + + 157 + 274 + + + + + buttonBox + rejected() + EditorDialog + reject() + + + 295 + 447 + + + 286 + 274 + + + + + template_text + textChanged() + EditorDialog + onTextChanged() + + + 408 + 298 + + + 270 + 457 + + + + + + onTextChanged() + + diff --git a/highlighter.py b/highlighter.py index 3213755..e963a7f 100644 --- a/highlighter.py +++ b/highlighter.py @@ -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 diff --git a/templates_dialog.py b/templates_dialog.py index 8a57482..efd5bd7 100644 --- a/templates_dialog.py +++ b/templates_dialog.py @@ -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_ () diff --git a/templates_dialog.ui b/templates_dialog.ui index e3f77aa..d8e5a04 100644 --- a/templates_dialog.ui +++ b/templates_dialog.ui @@ -51,6 +51,7 @@ selected template + templates edit close @@ -63,8 +64,8 @@ selected template accept() - 741 - 525 + 340 + 246 288 @@ -72,6 +73,22 @@ selected template + + edit + clicked() + TemplatesDialog + onEdit() + + + 163 + 235 + + + 83 + 224 + + + onEdit() diff --git a/ui_editor_dialog.py b/ui_editor_dialog.py new file mode 100644 index 0000000..62b5e44 --- /dev/null +++ b/ui_editor_dialog.py @@ -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)) + diff --git a/ui_templates_dialog.py b/ui_templates_dialog.py index 895e2be..7c2fce4 100644 --- a/ui_templates_dialog.py +++ b/ui_templates_dialog.py @@ -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): -- 2.20.1