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