Code highlighting implemented
authorHarishankar <v.harishankar@gmail.com>
Sun, 28 Nov 2010 12:08:47 +0000 (17:38 +0530)
committerHarishankar <v.harishankar@gmail.com>
Sun, 28 Nov 2010 12:08:47 +0000 (17:38 +0530)
Syntax highlighting implemented for article editing

article_dialog.py
article_dialog.ui
highlighter.py [new file with mode: 0644]
main_window.ui
ui_article_dialog.py
ui_main_window.py

index 02c0771..ddd9d18 100644 (file)
@@ -1,13 +1,16 @@
 # BiaWeb Website content manager (c) 2010 V.Harishankar
-# Category dialog class
+# Article dialog class
 
 import PyQt4
 import ui_article_dialog
+import highlighter
 
 class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
        def __init__ (self, parent):
                PyQt4.QtGui.QDialog.__init__ (self, parent)
                self.setupUi (self)
+               # set the code highlighter to the document
+               self.hltext = highlighter.SimpleHtmlHighlighter (self.content.document ())
 
        # when rejected, confirm first
        def reject (self):
index a4259ef..3d3b726 100644 (file)
@@ -414,19 +414,6 @@ p, li { white-space: pre-wrap; }
      </property>
     </widget>
    </item>
-   <item row="5" column="0" colspan="22">
-    <widget class="QPlainTextEdit" name="content">
-     <property name="minimumSize">
-      <size>
-       <width>0</width>
-       <height>210</height>
-      </size>
-     </property>
-     <property name="verticalScrollBarPolicy">
-      <enum>Qt::ScrollBarAlwaysOn</enum>
-     </property>
-    </widget>
-   </item>
    <item row="6" column="0" colspan="2">
     <widget class="QLabel" name="label_4">
      <property name="text">
@@ -543,7 +530,7 @@ p, li { white-space: pre-wrap; }
 p, li { white-space: pre-wrap; }
 &lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Sans'; font-size:10pt; font-weight:400; font-style:normal;&quot;&gt;
 &lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Center para&lt;/p&gt;
-&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-style:italic;&quot;&gt;Shortcut: Ctrl+C&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-style:italic;&quot;&gt;Shortcut: Ctrl+O&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
      </property>
      <property name="text">
       <string/>
@@ -553,7 +540,7 @@ p, li { white-space: pre-wrap; }
        <normaloff>:/bia/resources/center.gif</normaloff>:/bia/resources/center.gif</iconset>
      </property>
      <property name="shortcut">
-      <string>Ctrl+C</string>
+      <string>Ctrl+O</string>
      </property>
      <property name="autoRaise">
       <bool>false</bool>
@@ -616,6 +603,24 @@ p, li { white-space: pre-wrap; }
      </property>
     </widget>
    </item>
+   <item row="5" column="0" colspan="22">
+    <widget class="QPlainTextEdit" name="content">
+     <property name="minimumSize">
+      <size>
+       <width>0</width>
+       <height>210</height>
+      </size>
+     </property>
+     <property name="font">
+      <font>
+       <family>Monospace</family>
+      </font>
+     </property>
+     <property name="verticalScrollBarPolicy">
+      <enum>Qt::ScrollBarAlwaysOn</enum>
+     </property>
+    </widget>
+   </item>
   </layout>
  </widget>
  <tabstops>
@@ -816,8 +821,8 @@ p, li { white-space: pre-wrap; }
    <slot>onJustify()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>302</x>
-     <y>211</y>
+     <x>332</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>98</x>
@@ -832,8 +837,8 @@ p, li { white-space: pre-wrap; }
    <slot>onBullet()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>355</x>
-     <y>199</y>
+     <x>386</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>336</x>
@@ -848,8 +853,8 @@ p, li { white-space: pre-wrap; }
    <slot>onNumber()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>387</x>
-     <y>195</y>
+     <x>422</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>372</x>
@@ -864,8 +869,8 @@ p, li { white-space: pre-wrap; }
    <slot>onBQuote()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>423</x>
-     <y>203</y>
+     <x>458</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>311</x>
@@ -880,8 +885,8 @@ p, li { white-space: pre-wrap; }
    <slot>onCode()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>457</x>
-     <y>203</y>
+     <x>494</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>311</x>
@@ -896,8 +901,8 @@ p, li { white-space: pre-wrap; }
    <slot>onTable()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>492</x>
-     <y>203</y>
+     <x>530</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>311</x>
@@ -912,8 +917,8 @@ p, li { white-space: pre-wrap; }
    <slot>onHRule()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>532</x>
-     <y>203</y>
+     <x>572</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>311</x>
@@ -928,8 +933,8 @@ p, li { white-space: pre-wrap; }
    <slot>onPara()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>566</x>
-     <y>203</y>
+     <x>608</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>311</x>
@@ -944,8 +949,8 @@ p, li { white-space: pre-wrap; }
    <slot>onBreak()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>601</x>
-     <y>203</y>
+     <x>644</x>
+     <y>219</y>
     </hint>
     <hint type="destinationlabel">
      <x>311</x>
@@ -972,5 +977,6 @@ p, li { white-space: pre-wrap; }
   <slot>onHRule()</slot>
   <slot>onPara()</slot>
   <slot>onBreak()</slot>
+  <slot>onTextChanged()</slot>
  </slots>
 </ui>
diff --git a/highlighter.py b/highlighter.py
new file mode 100644 (file)
index 0000000..95cfa90
--- /dev/null
@@ -0,0 +1,31 @@
+# BiaWeb Website content manager (c) 2010 V.Harishankar
+# HTML highlighter class
+
+import PyQt4
+
+class SimpleHtmlHighlighter (PyQt4.QtGui.QSyntaxHighlighter):
+       def __init__ (self, document):
+               PyQt4.QtGui.QSyntaxHighlighter.__init__ (self, document)
+
+       def highlightBlock (self, text):
+               # define the character highlight formats
+               charfmt1 = PyQt4.QtGui.QTextCharFormat ()
+               charfmt1.setFontWeight (PyQt4.QtGui.QFont.Bold)
+               charfmt1.setForeground (PyQt4.QtCore.Qt.blue)
+
+               charfmt2 = PyQt4.QtGui.QTextCharFormat ()
+               charfmt2.setForeground (PyQt4.QtCore.Qt.darkGreen)
+
+               # matching regular expressions
+               htmltagexps = [ (PyQt4.QtCore.QRegExp ("<[^<>]+>"), charfmt1),
+                                               (PyQt4.QtCore.QRegExp ("\"[^\"]+\""), charfmt2)
+                                       ]
+
+               # run through the list of regular expressions to highlight
+               for htmltagexp, charfmt in htmltagexps:
+                       index = htmltagexp.indexIn (text)
+                       while index >= 0:
+                               length = htmltagexp.matchedLength ()
+                               # self.setFormat is actual code highlighting
+                               self.setFormat (index, length, charfmt)
+                               index = htmltagexp.indexIn (text, index + length)
index 3808477..ab98a30 100644 (file)
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>633</width>
-    <height>458</height>
+    <width>798</width>
+    <height>517</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -88,7 +88,7 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>633</width>
+     <width>798</width>
      <height>25</height>
     </rect>
    </property>
    </property>
   </action>
  </widget>
+ <tabstops>
+  <tabstop>categories</tabstop>
+  <tabstop>articles</tabstop>
+ </tabstops>
  <resources/>
  <connections>
   <connection>
index 7e42cc7..d9dc575 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'article_dialog.ui'
 #
-# Created: Sun Nov 28 13:59:41 2010
+# Created: Sun Nov 28 17:32:45 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -156,11 +156,6 @@ class Ui_ArticleDialog(object):
         self.linebreak.setAutoRaise(False)
         self.linebreak.setObjectName("linebreak")
         self.gridLayout.addWidget(self.linebreak, 4, 21, 1, 1)
-        self.content = QtGui.QPlainTextEdit(ArticleDialog)
-        self.content.setMinimumSize(QtCore.QSize(0, 210))
-        self.content.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
-        self.content.setObjectName("content")
-        self.gridLayout.addWidget(self.content, 5, 0, 1, 22)
         self.label_4 = QtGui.QLabel(ArticleDialog)
         self.label_4.setObjectName("label_4")
         self.gridLayout.addWidget(self.label_4, 6, 0, 1, 2)
@@ -233,6 +228,14 @@ class Ui_ArticleDialog(object):
         self.blockquote.setAutoRaise(False)
         self.blockquote.setObjectName("blockquote")
         self.gridLayout.addWidget(self.blockquote, 4, 15, 1, 1)
+        self.content = QtGui.QPlainTextEdit(ArticleDialog)
+        self.content.setMinimumSize(QtCore.QSize(0, 210))
+        font = QtGui.QFont()
+        font.setFamily("Monospace")
+        self.content.setFont(font)
+        self.content.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
+        self.content.setObjectName("content")
+        self.gridLayout.addWidget(self.content, 5, 0, 1, 22)
 
         self.retranslateUi(ArticleDialog)
         QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), ArticleDialog.accept)
@@ -392,8 +395,8 @@ class Ui_ArticleDialog(object):
 "p, li { white-space: pre-wrap; }\n"
 "</style></head><body style=\" font-family:\'Sans\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Center para</p>\n"
-"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-style:italic;\">Shortcut: Ctrl+C</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
-        self.paracenter.setShortcut(QtGui.QApplication.translate("ArticleDialog", "Ctrl+C", None, QtGui.QApplication.UnicodeUTF8))
+"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-style:italic;\">Shortcut: Ctrl+O</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
+        self.paracenter.setShortcut(QtGui.QApplication.translate("ArticleDialog", "Ctrl+O", None, QtGui.QApplication.UnicodeUTF8))
         self.paraleft.setToolTip(QtGui.QApplication.translate("ArticleDialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
 "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
 "p, li { white-space: pre-wrap; }\n"
index 279ea23..e14e758 100644 (file)
@@ -2,7 +2,7 @@
 
 # Form implementation generated from reading ui file 'main_window.ui'
 #
-# Created: Sun Nov 28 09:35:09 2010
+# Created: Sun Nov 28 15:30:11 2010
 #      by: PyQt4 UI code generator 4.7.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -12,7 +12,7 @@ from PyQt4 import QtCore, QtGui
 class Ui_MainWindow(object):
     def setupUi(self, MainWindow):
         MainWindow.setObjectName("MainWindow")
-        MainWindow.resize(633, 458)
+        MainWindow.resize(798, 517)
         self.centralwidget = QtGui.QWidget(MainWindow)
         self.centralwidget.setObjectName("centralwidget")
         self.gridLayout = QtGui.QGridLayout(self.centralwidget)
@@ -45,7 +45,7 @@ class Ui_MainWindow(object):
         self.dockWidget_2.setWidget(self.dockWidgetContents_2)
         MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget_2)
         self.menubar = QtGui.QMenuBar(MainWindow)
-        self.menubar.setGeometry(QtCore.QRect(0, 0, 633, 25))
+        self.menubar.setGeometry(QtCore.QRect(0, 0, 798, 25))
         self.menubar.setObjectName("menubar")
         self.menu_About = QtGui.QMenu(self.menubar)
         self.menu_About.setObjectName("menu_About")
@@ -136,6 +136,7 @@ class Ui_MainWindow(object):
         QtCore.QObject.connect(self.action_EditArticle, QtCore.SIGNAL("triggered()"), MainWindow.onArticleEdit)
         QtCore.QObject.connect(self.action_DeleteArticle, QtCore.SIGNAL("triggered()"), MainWindow.onArticleDelete)
         QtCore.QMetaObject.connectSlotsByName(MainWindow)
+        MainWindow.setTabOrder(self.categories, self.articles)
 
     def retranslateUi(self, MainWindow):
         MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "BiaWeb - Static Website Content Manager", None, QtGui.QApplication.UnicodeUTF8))