1 # BiaWeb Website content manager (c) 2010 V.Harishankar
5 import ui_article_dialog
8 class ArticleDialog (PyQt4
.QtGui
.QDialog
, ui_article_dialog
.Ui_ArticleDialog
):
9 def __init__ (self
, parent
):
10 PyQt4
.QtGui
.QDialog
.__init
__ (self
, parent
)
12 # set the code highlighter to the document
13 self
.hltext
= highlighter
.SimpleHtmlHighlighter (self
.content
.document ())
15 # when rejected, confirm first
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
)
23 # when accepted check the data
25 title
= str (self
.article_title
.text ()).strip ()
26 content
= str (self
.content
.toPlainText ()).strip ()
27 stub
= str (self
.stub
.text ()).strip ()
29 if title
<> "" and content
<> "" and stub
<> "":
30 PyQt4
.QtGui
.QDialog
.accept (self
)
32 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Missing fields", "Some required fields are missing")
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
))
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
))
43 # when bold is clicked
45 textcur
= self
.content
.textCursor ()
46 sel
= textcur
.selectedText ()
47 boldsel
= "<b>" + sel
+ "</b>"
48 textcur
.insertText (boldsel
)
50 # when block quote is clicked
52 textcur
= self
.content
.textCursor ()
53 sel
= textcur
.selectedText ()
54 quotesel
= "<blockquote>\n" + sel
+ "\n</blockquote>"
55 textcur
.insertText (quotesel
)
57 # when bullet is clicked
59 # insert bulleted list
60 self
.insert_list_items ()
62 # when code is clicked
64 textcur
= self
.content
.textCursor ()
65 sel
= textcur
.selectedText ()
66 codesel
= "<code>" + str (sel
) + "</code>"
67 textcur
.insertText (codesel
)
69 # when horiz rule is clicked
71 textcur
= self
.content
.textCursor ()
73 textcur
.clearSelection ()
74 textcur
.insertText (hrule
)
76 # when image is clicked
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 ():
81 alt_txt
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Alt text", "Alternate text of the image")
82 if not ok
or alt_txt
.isEmpty ():
85 textcur
= self
.content
.textCursor ()
86 imgtag
= '<img src="' + img_url
+ '" alt="' + alt_txt
+ '" />'
87 textcur
.clearSelection ()
88 textcur
.insertText (imgtag
)
90 # when italic is clicked
92 textcur
= self
.content
.textCursor ()
93 sel
= textcur
.selectedText ()
94 italsel
= "<i>" + sel
+ "</i>"
95 textcur
.insertText (italsel
)
97 # when link is clicked
99 linkhref
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Link URL", "URL of the link to insert")
100 if not ok
or linkhref
.isEmpty ():
102 textcur
= self
.content
.textCursor ()
103 sel
= textcur
.selectedText ()
104 linksel
= '<a href="' + linkhref
+ '">' + sel
+ "</a>"
105 textcur
.insertText (linksel
)
107 # when numbered is clicked
110 self
.insert_list_items (True)
112 # get a list of items in numbered or bulleted list
113 def insert_list_items (self
, numbered
= False):
115 title
= "Numbered list"
119 title
= "Bulleted list"
125 num_item
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, title
,
126 "List item content (leave blank to finish the list)")
130 # if item is empty then close the list
131 if num_item
.isEmpty ():
133 itemlist
.append (" <li>" + num_item
+ "</li>\n")
134 itemlist
.append (ctag
)
136 textcur
= self
.content
.textCursor ()
137 textcur
.clearSelection ()
138 for item
in itemlist
:
139 textcur
.insertText (item
)
142 # when center is clicked
144 textcur
= self
.content
.textCursor ()
145 sel
= textcur
.selectedText ()
146 paracentersel
= '<p style="text-align:center;">' + sel
+ "</p>"
147 textcur
.insertText (paracentersel
)
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
)
156 # when left is clicked
158 textcur
= self
.content
.textCursor ()
159 sel
= textcur
.selectedText ()
160 paraleftsel
= '<p style="text-align:left;">' + sel
+ "</p>"
161 textcur
.insertText (paraleftsel
)
163 # when right is clicked
165 textcur
= self
.content
.textCursor ()
166 sel
= textcur
.selectedText ()
167 pararightsel
= '<p style="text-align:right;">' + sel
+ "</p>"
168 textcur
.insertText (pararightsel
)
170 # when pre is clicked
172 textcur
= self
.content
.textCursor ()
173 sel
= textcur
.selectedText ()
174 presel
= "<pre>" + sel
+ "</pre>"
175 textcur
.insertText (presel
)
177 # when para is clicked
179 textcur
= self
.content
.textCursor ()
180 sel
= textcur
.selectedText ()
181 parasel
= "<p>" + sel
+ "</p>"
182 textcur
.insertText (parasel
)
184 # when table is clicked
186 # get the number of rows
187 rows
, ok
= PyQt4
.QtGui
.QInputDialog
.getInt (self
, "Table", "Number of table rows")
190 # get the number of columns
191 cols
, ok
= PyQt4
.QtGui
.QInputDialog
.getInt (self
, "Table", "Number of table columns")
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
:
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")
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")
221 textcur
= self
.content
.textCursor ()
222 textcur
.clearSelection ()
223 for item
in tablelist
:
224 textcur
.insertText (item
)
228 # when break is clicked
230 textcur
= self
.content
.textCursor ()
231 breaktxt
= "\n<br />\n"
232 textcur
.clearSelection ()
233 textcur
.insertText (breaktxt
)