Article dialog internals 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 # when accepted check the data
24 def accept (self):
25 title = str (self.article_title.text ()).strip ()
26 content = str (self.content.toPlainText ()).strip ()
27 stub = str (self.stub.text ()).strip ()
28
29 if title <> "" and content <> "" and stub <> "":
30 PyQt4.QtGui.QDialog.accept (self)
31 else:
32 PyQt4.QtGui.QMessageBox.critical (self, "Missing fields", "Some required fields are missing")
33
34 # populate categories in combo box
35 def populate_categories (self, category_list, selected_cat = None):
36 for catid, catname, catdesc, stub in category_list:
37 self.category.addItem (catname, int (catid))
38
39 # set the index to the selected category item
40 if selected_cat is not None:
41 self.category.setCurrentIndex (self.category.findData (selected_cat))
42
43 # when bold is clicked
44 def onBold (self):
45 textcur = self.content.textCursor ()
46 sel = textcur.selectedText ()
47 boldsel = "<b>" + sel + "</b>"
48 textcur.insertText (boldsel)
49
50 # when block quote is clicked
51 def onBQuote (self):
52 textcur = self.content.textCursor ()
53 sel = textcur.selectedText ()
54 quotesel = "<blockquote>\n" + sel + "\n</blockquote>"
55 textcur.insertText (quotesel)
56
57 # when bullet is clicked
58 def onBullet (self):
59 # insert bulleted list
60 self.insert_list_items ()
61
62 # when code is clicked
63 def onCode (self):
64 textcur = self.content.textCursor ()
65 sel = textcur.selectedText ()
66 codesel = "<code>" + str (sel) + "</code>"
67 textcur.insertText (codesel)
68
69 # when horiz rule is clicked
70 def onHRule (self):
71 textcur = self.content.textCursor ()
72 hrule = "\n<hr />\n"
73 textcur.clearSelection ()
74 textcur.insertText (hrule)
75
76 # when image is clicked
77 def onImage (self):
78 img_url, ok = PyQt4.QtGui.QInputDialog.getText (self, "Image URL", "URL of the image to insert")
79 if not ok or img_url.isEmpty ():
80 return
81 alt_txt, ok = PyQt4.QtGui.QInputDialog.getText (self, "Alt text", "Alternate text of the image")
82 if not ok or alt_txt.isEmpty ():
83 return
84
85 textcur = self.content.textCursor ()
86 imgtag = '<img src="' + img_url + '" alt="' + alt_txt + '" />'
87 textcur.clearSelection ()
88 textcur.insertText (imgtag)
89
90 # when italic is clicked
91 def onItalic (self):
92 textcur = self.content.textCursor ()
93 sel = textcur.selectedText ()
94 italsel = "<i>" + sel + "</i>"
95 textcur.insertText (italsel)
96
97 # when link is clicked
98 def onLink (self):
99 linkhref, ok = PyQt4.QtGui.QInputDialog.getText (self, "Link URL", "URL of the link to insert")
100 if not ok or linkhref.isEmpty ():
101 return
102 textcur = self.content.textCursor ()
103 sel = textcur.selectedText ()
104 linksel = '<a href="' + linkhref + '">' + sel + "</a>"
105 textcur.insertText (linksel)
106
107 # when numbered is clicked
108 def onNumber (self):
109 # numbered list
110 self.insert_list_items (True)
111
112 # get a list of items in numbered or bulleted list
113 def insert_list_items (self, numbered = False):
114 if numbered == True:
115 title = "Numbered list"
116 otag = "<ol>\n"
117 ctag = "</ol>\n"
118 else:
119 title = "Bulleted list"
120 otag = "<ul>\n"
121 ctag = "</ul>\n"
122
123 itemlist = [ otag ]
124 while True:
125 num_item, ok = PyQt4.QtGui.QInputDialog.getText (self, title,
126 "List item content (leave blank to finish the list)")
127 # if cancelled quit
128 if not ok:
129 return
130 # if item is empty then close the list
131 if num_item.isEmpty ():
132 break
133 itemlist.append (" <li>" + num_item + "</li>\n")
134 itemlist.append (ctag)
135
136 textcur = self.content.textCursor ()
137 textcur.clearSelection ()
138 for item in itemlist:
139 textcur.insertText (item)
140
141
142 # when center is clicked
143 def onCenter (self):
144 textcur = self.content.textCursor ()
145 sel = textcur.selectedText ()
146 paracentersel = '<p style="text-align:center;">' + sel + "</p>"
147 textcur.insertText (paracentersel)
148
149 # when justify is clicked
150 def onJustify (self):
151 textcur = self.content.textCursor ()
152 sel = textcur.selectedText ()
153 parajustsel = '<p style="text-align:justify;">' + sel + "</p>"
154 textcur.insertText (parajustsel)
155
156 # when left is clicked
157 def onLeft (self):
158 textcur = self.content.textCursor ()
159 sel = textcur.selectedText ()
160 paraleftsel = '<p style="text-align:left;">' + sel + "</p>"
161 textcur.insertText (paraleftsel)
162
163 # when right is clicked
164 def onRight (self):
165 textcur = self.content.textCursor ()
166 sel = textcur.selectedText ()
167 pararightsel = '<p style="text-align:right;">' + sel + "</p>"
168 textcur.insertText (pararightsel)
169
170 # when pre is clicked
171 def onPre (self):
172 textcur = self.content.textCursor ()
173 sel = textcur.selectedText ()
174 presel = "<pre>" + sel + "</pre>"
175 textcur.insertText (presel)
176
177 # when para is clicked
178 def onPara (self):
179 textcur = self.content.textCursor ()
180 sel = textcur.selectedText ()
181 parasel = "<p>" + sel + "</p>"
182 textcur.insertText (parasel)
183
184 # when table is clicked
185 def onTable (self):
186 # get the number of rows
187 rows, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table rows")
188 if not ok:
189 return
190 # get the number of columns
191 cols, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table columns")
192 if not ok:
193 return
194 # should there be a header row?
195 headerflag = PyQt4.QtGui.QMessageBox.question (self, "Header", "Do you want additional header row?",
196 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No,
197 PyQt4.QtGui.QMessageBox.Cancel)
198 if headerflag == PyQt4.QtGui.QMessageBox.Cancel:
199 return
200
201 # build the table tag
202 tablelist = ["\n<table>\n"]
203 if headerflag == PyQt4.QtGui.QMessageBox.Yes:
204 tablelist.append (" <thead>\n")
205 tablelist.append (" <tr>\n")
206 for i in range (cols):
207 tablelist.append (" <th></th>\n")
208 tablelist.append (" </tr>\n")
209 tablelist.append (" </thead>\n")
210
211 tablelist.append (" <tbody>\n")
212 for j in range (rows):
213 tablelist.append (" <tr>\n")
214 for i in range (cols):
215 tablelist.append (" <td></td>\n")
216 tablelist.append (" </tr>\n")
217 tablelist.append (" </tbody>\n")
218 tablelist.append ("</table>\n")
219
220 # insert the table
221 textcur = self.content.textCursor ()
222 textcur.clearSelection ()
223 for item in tablelist:
224 textcur.insertText (item)
225
226
227
228 # when break is clicked
229 def onBreak (self):
230 textcur = self.content.textCursor ()
231 breaktxt = "\n<br />\n"
232 textcur.clearSelection ()
233 textcur.insertText (breaktxt)