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 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 ())
17 # when rejected, confirm first
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
)
25 # when accepted check the data
27 title
= str (self
.article_title
.text ()).strip ()
28 content
= str (self
.content
.toPlainText ()).strip ()
29 stub
= str (self
.stub
.text ()).strip ()
31 if title
<> "" and content
<> "" and stub
<> "":
32 PyQt4
.QtGui
.QDialog
.accept (self
)
34 PyQt4
.QtGui
.QMessageBox
.critical (self
, "Missing fields", "Some required fields are missing")
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
))
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
))
45 # when bold is clicked
47 textcur
= self
.content
.textCursor ()
48 sel
= textcur
.selectedText ()
49 boldsel
= "<b>" + sel
+ "</b>"
50 textcur
.insertText (boldsel
)
52 # when block quote is clicked
54 textcur
= self
.content
.textCursor ()
55 sel
= textcur
.selectedText ()
56 quotesel
= "<blockquote>\n" + sel
+ "\n</blockquote>"
57 textcur
.insertText (quotesel
)
59 # when bullet is clicked
61 # insert bulleted list
62 self
.insert_list_items ()
64 # when code is clicked
66 textcur
= self
.content
.textCursor ()
67 sel
= textcur
.selectedText ()
68 codesel
= "<code>" + str (sel
) + "</code>"
69 textcur
.insertText (codesel
)
71 # when horiz rule is clicked
73 textcur
= self
.content
.textCursor ()
75 textcur
.clearSelection ()
76 textcur
.insertText (hrule
)
78 # when image is clicked
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 ():
83 alt_txt
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Alt text", "Alternate text of the image")
84 if not ok
or alt_txt
.isEmpty ():
87 textcur
= self
.content
.textCursor ()
88 imgtag
= '<img src="' + img_url
+ '" alt="' + alt_txt
+ '" />'
89 textcur
.clearSelection ()
90 textcur
.insertText (imgtag
)
92 # when italic is clicked
94 textcur
= self
.content
.textCursor ()
95 sel
= textcur
.selectedText ()
96 italsel
= "<i>" + sel
+ "</i>"
97 textcur
.insertText (italsel
)
99 # when link is clicked
101 linkhref
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Link URL", "URL of the link to insert")
102 if not ok
or linkhref
.isEmpty ():
104 textcur
= self
.content
.textCursor ()
105 sel
= textcur
.selectedText ()
106 linksel
= '<a href="' + linkhref
+ '">' + sel
+ "</a>"
107 textcur
.insertText (linksel
)
109 # when numbered is clicked
112 self
.insert_list_items (True)
114 # get a list of items in numbered or bulleted list
115 def insert_list_items (self
, numbered
= False):
117 title
= "Numbered list"
121 title
= "Bulleted list"
127 num_item
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, title
,
128 "List item content (leave blank to finish the list)")
132 # if item is empty then close the list
133 if num_item
.isEmpty ():
135 itemlist
.append (" <li>" + num_item
+ "</li>\n")
136 itemlist
.append (ctag
)
138 textcur
= self
.content
.textCursor ()
139 textcur
.clearSelection ()
140 for item
in itemlist
:
141 textcur
.insertText (item
)
144 # when center is clicked
146 textcur
= self
.content
.textCursor ()
147 sel
= textcur
.selectedText ()
148 paracentersel
= '<p style="text-align:center;">' + sel
+ "</p>"
149 textcur
.insertText (paracentersel
)
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
)
158 # when left is clicked
160 textcur
= self
.content
.textCursor ()
161 sel
= textcur
.selectedText ()
162 paraleftsel
= '<p style="text-align:left;">' + sel
+ "</p>"
163 textcur
.insertText (paraleftsel
)
165 # when right is clicked
167 textcur
= self
.content
.textCursor ()
168 sel
= textcur
.selectedText ()
169 pararightsel
= '<p style="text-align:right;">' + sel
+ "</p>"
170 textcur
.insertText (pararightsel
)
172 # when pre is clicked
174 textcur
= self
.content
.textCursor ()
175 sel
= textcur
.selectedText ()
176 presel
= "<pre>" + sel
+ "</pre>"
177 textcur
.insertText (presel
)
179 # when para is clicked
181 textcur
= self
.content
.textCursor ()
182 sel
= textcur
.selectedText ()
183 parasel
= "<p>" + sel
+ "</p>"
184 textcur
.insertText (parasel
)
186 # when table is clicked
188 # get the number of rows
189 rows
, ok
= PyQt4
.QtGui
.QInputDialog
.getInt (self
, "Table", "Number of table rows")
192 # get the number of columns
193 cols
, ok
= PyQt4
.QtGui
.QInputDialog
.getInt (self
, "Table", "Number of table columns")
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
:
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")
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")
223 textcur
= self
.content
.textCursor ()
224 textcur
.clearSelection ()
225 for item
in tablelist
:
226 textcur
.insertText (item
)
230 # when break is clicked
232 textcur
= self
.content
.textCursor ()
233 breaktxt
= "<br />\n"
234 textcur
.clearSelection ()
235 textcur
.insertText (breaktxt
)