Code highlighting implemented
[biaweb_qt.git] / article_dialog.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Article dialog class
3
4 import PyQt4
5 import ui_article_dialog
6 import highlighter
7
8 class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
9 def __init__ (self, parent):
10 PyQt4.QtGui.QDialog.__init__ (self, parent)
11 self.setupUi (self)
12 # set the code highlighter to the document
13 self.hltext = highlighter.SimpleHtmlHighlighter (self.content.document ())
14
15 # when rejected, confirm first
16 def reject (self):
17 ans = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
18 "Are you sure you wish to cancel all changes?",
19 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
20 if ans == PyQt4.QtGui.QMessageBox.Yes:
21 PyQt4.QtGui.QDialog.reject (self)
22
23
24 # when bold is clicked
25 def onBold (self):
26 textcur = self.content.textCursor ()
27 sel = textcur.selectedText ()
28 boldsel = "<b>" + sel + "</b>"
29 textcur.insertText (boldsel)
30
31 # when block quote is clicked
32 def onBQuote (self):
33 textcur = self.content.textCursor ()
34 sel = textcur.selectedText ()
35 quotesel = "<blockquote>\n" + sel + "\n</blockquote>"
36 textcur.insertText (quotesel)
37
38 # when bullet is clicked
39 def onBullet (self):
40 # insert bulleted list
41 self.insert_list_items ()
42
43 # when code is clicked
44 def onCode (self):
45 textcur = self.content.textCursor ()
46 sel = textcur.selectedText ()
47 codesel = "<code>" + str (sel) + "</code>"
48 textcur.insertText (codesel)
49
50 # when horiz rule is clicked
51 def onHRule (self):
52 textcur = self.content.textCursor ()
53 hrule = "\n<hr />\n"
54 textcur.clearSelection ()
55 textcur.insertText (hrule)
56
57 # when image is clicked
58 def onImage (self):
59 img_url, ok = PyQt4.QtGui.QInputDialog.getText (self, "Image URL", "URL of the image to insert")
60 if not ok or img_url.isEmpty ():
61 return
62 alt_txt, ok = PyQt4.QtGui.QInputDialog.getText (self, "Alt text", "Alternate text of the image")
63 if not ok or alt_txt.isEmpty ():
64 return
65
66 textcur = self.content.textCursor ()
67 imgtag = '<img src="' + img_url + '" alt="' + alt_txt + '" />'
68 textcur.clearSelection ()
69 textcur.insertText (imgtag)
70
71 # when italic is clicked
72 def onItalic (self):
73 textcur = self.content.textCursor ()
74 sel = textcur.selectedText ()
75 italsel = "<i>" + sel + "</i>"
76 textcur.insertText (italsel)
77
78 # when link is clicked
79 def onLink (self):
80 linkhref, ok = PyQt4.QtGui.QInputDialog.getText (self, "Link URL", "URL of the link to insert")
81 if not ok or linkhref.isEmpty ():
82 return
83 textcur = self.content.textCursor ()
84 sel = textcur.selectedText ()
85 linksel = '<a href="' + linkhref + '">' + sel + "</a>"
86 textcur.insertText (linksel)
87
88 # when numbered is clicked
89 def onNumber (self):
90 # numbered list
91 self.insert_list_items (True)
92
93 # get a list of items in numbered or bulleted list
94 def insert_list_items (self, numbered = False):
95 if numbered == True:
96 title = "Numbered list"
97 otag = "<ol>\n"
98 ctag = "</ol>\n"
99 else:
100 title = "Bulleted list"
101 otag = "<ul>\n"
102 ctag = "</ul>\n"
103
104 itemlist = [ otag ]
105 while True:
106 num_item, ok = PyQt4.QtGui.QInputDialog.getText (self, title,
107 "List item content (leave blank to finish the list)")
108 # if cancelled quit
109 if not ok:
110 return
111 # if item is empty then close the list
112 if num_item.isEmpty ():
113 break
114 itemlist.append (" <li>" + num_item + "</li>\n")
115 itemlist.append (ctag)
116
117 textcur = self.content.textCursor ()
118 textcur.clearSelection ()
119 for item in itemlist:
120 textcur.insertText (item)
121
122
123 # when center is clicked
124 def onCenter (self):
125 textcur = self.content.textCursor ()
126 sel = textcur.selectedText ()
127 paracentersel = '<p style="text-align:center;">' + sel + "</p>"
128 textcur.insertText (paracentersel)
129
130 # when justify is clicked
131 def onJustify (self):
132 textcur = self.content.textCursor ()
133 sel = textcur.selectedText ()
134 parajustsel = '<p style="text-align:justify;">' + sel + "</p>"
135 textcur.insertText (parajustsel)
136
137 # when left is clicked
138 def onLeft (self):
139 textcur = self.content.textCursor ()
140 sel = textcur.selectedText ()
141 paraleftsel = '<p style="text-align:left;">' + sel + "</p>"
142 textcur.insertText (paraleftsel)
143
144 # when right is clicked
145 def onRight (self):
146 textcur = self.content.textCursor ()
147 sel = textcur.selectedText ()
148 pararightsel = '<p style="text-align:right;">' + sel + "</p>"
149 textcur.insertText (pararightsel)
150
151 # when pre is clicked
152 def onPre (self):
153 textcur = self.content.textCursor ()
154 sel = textcur.selectedText ()
155 presel = "<pre>" + sel + "</pre>"
156 textcur.insertText (presel)
157
158 # when para is clicked
159 def onPara (self):
160 textcur = self.content.textCursor ()
161 sel = textcur.selectedText ()
162 parasel = "<p>" + sel + "</p>"
163 textcur.insertText (parasel)
164
165 # when table is clicked
166 def onTable (self):
167 # get the number of rows
168 rows, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table rows")
169 if not ok:
170 return
171 # get the number of columns
172 cols, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table columns")
173 if not ok:
174 return
175 # should there be a header row?
176 headerflag = PyQt4.QtGui.QMessageBox.question (self, "Header", "Do you want additional header row?",
177 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No,
178 PyQt4.QtGui.QMessageBox.Cancel)
179 if headerflag == PyQt4.QtGui.QMessageBox.Cancel:
180 return
181
182 # build the table tag
183 tablelist = ["\n<table>\n"]
184 if headerflag == PyQt4.QtGui.QMessageBox.Yes:
185 tablelist.append (" <thead>\n")
186 tablelist.append (" <tr>\n")
187 for i in range (cols):
188 tablelist.append (" <th></th>\n")
189 tablelist.append (" </tr>\n")
190 tablelist.append (" </thead>\n")
191
192 tablelist.append (" <tbody>\n")
193 for j in range (rows):
194 tablelist.append (" <tr>\n")
195 for i in range (cols):
196 tablelist.append (" <td></td>\n")
197 tablelist.append (" </tr>\n")
198 tablelist.append (" </tbody>\n")
199 tablelist.append ("</table>\n")
200
201 # insert the table
202 textcur = self.content.textCursor ()
203 textcur.clearSelection ()
204 for item in tablelist:
205 textcur.insertText (item)
206
207
208
209 # when break is clicked
210 def onBreak (self):
211 textcur = self.content.textCursor ()
212 breaktxt = "\n<br />\n"
213 textcur.clearSelection ()
214 textcur.insertText (breaktxt)