ddd9d18051eaa1b920fc85640695149901cde9a0
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
)
24 # when bold is clicked
26 textcur
= self
.content
.textCursor ()
27 sel
= textcur
.selectedText ()
28 boldsel
= "<b>" + sel
+ "</b>"
29 textcur
.insertText (boldsel
)
31 # when block quote is clicked
33 textcur
= self
.content
.textCursor ()
34 sel
= textcur
.selectedText ()
35 quotesel
= "<blockquote>\n" + sel
+ "\n</blockquote>"
36 textcur
.insertText (quotesel
)
38 # when bullet is clicked
40 # insert bulleted list
41 self
.insert_list_items ()
43 # when code is clicked
45 textcur
= self
.content
.textCursor ()
46 sel
= textcur
.selectedText ()
47 codesel
= "<code>" + str (sel
) + "</code>"
48 textcur
.insertText (codesel
)
50 # when horiz rule is clicked
52 textcur
= self
.content
.textCursor ()
54 textcur
.clearSelection ()
55 textcur
.insertText (hrule
)
57 # when image is clicked
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 ():
62 alt_txt
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Alt text", "Alternate text of the image")
63 if not ok
or alt_txt
.isEmpty ():
66 textcur
= self
.content
.textCursor ()
67 imgtag
= '<img src="' + img_url
+ '" alt="' + alt_txt
+ '" />'
68 textcur
.clearSelection ()
69 textcur
.insertText (imgtag
)
71 # when italic is clicked
73 textcur
= self
.content
.textCursor ()
74 sel
= textcur
.selectedText ()
75 italsel
= "<i>" + sel
+ "</i>"
76 textcur
.insertText (italsel
)
78 # when link is clicked
80 linkhref
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, "Link URL", "URL of the link to insert")
81 if not ok
or linkhref
.isEmpty ():
83 textcur
= self
.content
.textCursor ()
84 sel
= textcur
.selectedText ()
85 linksel
= '<a href="' + linkhref
+ '">' + sel
+ "</a>"
86 textcur
.insertText (linksel
)
88 # when numbered is clicked
91 self
.insert_list_items (True)
93 # get a list of items in numbered or bulleted list
94 def insert_list_items (self
, numbered
= False):
96 title
= "Numbered list"
100 title
= "Bulleted list"
106 num_item
, ok
= PyQt4
.QtGui
.QInputDialog
.getText (self
, title
,
107 "List item content (leave blank to finish the list)")
111 # if item is empty then close the list
112 if num_item
.isEmpty ():
114 itemlist
.append (" <li>" + num_item
+ "</li>\n")
115 itemlist
.append (ctag
)
117 textcur
= self
.content
.textCursor ()
118 textcur
.clearSelection ()
119 for item
in itemlist
:
120 textcur
.insertText (item
)
123 # when center is clicked
125 textcur
= self
.content
.textCursor ()
126 sel
= textcur
.selectedText ()
127 paracentersel
= '<p style="text-align:center;">' + sel
+ "</p>"
128 textcur
.insertText (paracentersel
)
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
)
137 # when left is clicked
139 textcur
= self
.content
.textCursor ()
140 sel
= textcur
.selectedText ()
141 paraleftsel
= '<p style="text-align:left;">' + sel
+ "</p>"
142 textcur
.insertText (paraleftsel
)
144 # when right is clicked
146 textcur
= self
.content
.textCursor ()
147 sel
= textcur
.selectedText ()
148 pararightsel
= '<p style="text-align:right;">' + sel
+ "</p>"
149 textcur
.insertText (pararightsel
)
151 # when pre is clicked
153 textcur
= self
.content
.textCursor ()
154 sel
= textcur
.selectedText ()
155 presel
= "<pre>" + sel
+ "</pre>"
156 textcur
.insertText (presel
)
158 # when para is clicked
160 textcur
= self
.content
.textCursor ()
161 sel
= textcur
.selectedText ()
162 parasel
= "<p>" + sel
+ "</p>"
163 textcur
.insertText (parasel
)
165 # when table is clicked
167 # get the number of rows
168 rows
, ok
= PyQt4
.QtGui
.QInputDialog
.getInt (self
, "Table", "Number of table rows")
171 # get the number of columns
172 cols
, ok
= PyQt4
.QtGui
.QInputDialog
.getInt (self
, "Table", "Number of table columns")
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
:
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")
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")
202 textcur
= self
.content
.textCursor ()
203 textcur
.clearSelection ()
204 for item
in tablelist
:
205 textcur
.insertText (item
)
209 # when break is clicked
211 textcur
= self
.content
.textCursor ()
212 breaktxt
= "\n<br />\n"
213 textcur
.clearSelection ()
214 textcur
.insertText (breaktxt
)