Article dialog formatting controls implemented
[biaweb_qt.git] / article_dialog.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Category dialog class
3
4 import PyQt4
5 import ui_article_dialog
6
7 class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
8 def __init__ (self, parent):
9 PyQt4.QtGui.QDialog.__init__ (self, parent)
10 self.setupUi (self)
11
12 # when rejected, confirm first
13 def reject (self):
14 ans = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
15 "Are you sure you wish to cancel all changes?",
16 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
17 if ans == PyQt4.QtGui.QMessageBox.Yes:
18 PyQt4.QtGui.QDialog.reject (self)
19
20
21 # when bold is clicked
22 def onBold (self):
23 textcur = self.content.textCursor ()
24 sel = textcur.selectedText ()
25 boldsel = "<b>" + sel + "</b>"
26 textcur.insertText (boldsel)
27
28 # when block quote is clicked
29 def onBQuote (self):
30 textcur = self.content.textCursor ()
31 sel = textcur.selectedText ()
32 quotesel = "<blockquote>\n" + sel + "\n</blockquote>"
33 textcur.insertText (quotesel)
34
35 # when bullet is clicked
36 def onBullet (self):
37 # insert bulleted list
38 self.insert_list_items ()
39
40 # when code is clicked
41 def onCode (self):
42 textcur = self.content.textCursor ()
43 sel = textcur.selectedText ()
44 codesel = "<code>" + str (sel) + "</code>"
45 textcur.insertText (codesel)
46
47 # when horiz rule is clicked
48 def onHRule (self):
49 textcur = self.content.textCursor ()
50 hrule = "\n<hr />\n"
51 textcur.clearSelection ()
52 textcur.insertText (hrule)
53
54 # when image is clicked
55 def onImage (self):
56 img_url, ok = PyQt4.QtGui.QInputDialog.getText (self, "Image URL", "URL of the image to insert")
57 if not ok or img_url.isEmpty ():
58 return
59 alt_txt, ok = PyQt4.QtGui.QInputDialog.getText (self, "Alt text", "Alternate text of the image")
60 if not ok or alt_txt.isEmpty ():
61 return
62
63 textcur = self.content.textCursor ()
64 imgtag = '<img src="' + img_url + '" alt="' + alt_txt + '" />'
65 textcur.clearSelection ()
66 textcur.insertText (imgtag)
67
68 # when italic is clicked
69 def onItalic (self):
70 textcur = self.content.textCursor ()
71 sel = textcur.selectedText ()
72 italsel = "<i>" + sel + "</i>"
73 textcur.insertText (italsel)
74
75 # when link is clicked
76 def onLink (self):
77 linkhref, ok = PyQt4.QtGui.QInputDialog.getText (self, "Link URL", "URL of the link to insert")
78 if not ok or linkhref.isEmpty ():
79 return
80 textcur = self.content.textCursor ()
81 sel = textcur.selectedText ()
82 linksel = '<a href="' + linkhref + '">' + sel + "</a>"
83 textcur.insertText (linksel)
84
85 # when numbered is clicked
86 def onNumber (self):
87 # numbered list
88 self.insert_list_items (True)
89
90 # get a list of items in numbered or bulleted list
91 def insert_list_items (self, numbered = False):
92 if numbered == True:
93 title = "Numbered list"
94 otag = "<ol>\n"
95 ctag = "</ol>\n"
96 else:
97 title = "Bulleted list"
98 otag = "<ul>\n"
99 ctag = "</ul>\n"
100
101 itemlist = [ otag ]
102 while True:
103 num_item, ok = PyQt4.QtGui.QInputDialog.getText (self, title,
104 "List item content (leave blank to finish the list)")
105 # if cancelled quit
106 if not ok:
107 return
108 # if item is empty then close the list
109 if num_item.isEmpty ():
110 break
111 itemlist.append (" <li>" + num_item + "</li>\n")
112 itemlist.append (ctag)
113
114 textcur = self.content.textCursor ()
115 textcur.clearSelection ()
116 for item in itemlist:
117 textcur.insertText (item)
118
119
120 # when center is clicked
121 def onCenter (self):
122 textcur = self.content.textCursor ()
123 sel = textcur.selectedText ()
124 paracentersel = '<p style="text-align:center;">' + sel + "</p>"
125 textcur.insertText (paracentersel)
126
127 # when justify is clicked
128 def onJustify (self):
129 textcur = self.content.textCursor ()
130 sel = textcur.selectedText ()
131 parajustsel = '<p style="text-align:justify;">' + sel + "</p>"
132 textcur.insertText (parajustsel)
133
134 # when left is clicked
135 def onLeft (self):
136 textcur = self.content.textCursor ()
137 sel = textcur.selectedText ()
138 paraleftsel = '<p style="text-align:left;">' + sel + "</p>"
139 textcur.insertText (paraleftsel)
140
141 # when right is clicked
142 def onRight (self):
143 textcur = self.content.textCursor ()
144 sel = textcur.selectedText ()
145 pararightsel = '<p style="text-align:right;">' + sel + "</p>"
146 textcur.insertText (pararightsel)
147
148 # when pre is clicked
149 def onPre (self):
150 textcur = self.content.textCursor ()
151 sel = textcur.selectedText ()
152 presel = "<pre>" + sel + "</pre>"
153 textcur.insertText (presel)
154
155 # when para is clicked
156 def onPara (self):
157 textcur = self.content.textCursor ()
158 sel = textcur.selectedText ()
159 parasel = "<p>" + sel + "</p>"
160 textcur.insertText (parasel)
161
162 # when table is clicked
163 def onTable (self):
164 # get the number of rows
165 rows, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table rows")
166 if not ok:
167 return
168 # get the number of columns
169 cols, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table columns")
170 if not ok:
171 return
172 # should there be a header row?
173 headerflag = PyQt4.QtGui.QMessageBox.question (self, "Header", "Do you want additional header row?",
174 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No,
175 PyQt4.QtGui.QMessageBox.Cancel)
176 if headerflag == PyQt4.QtGui.QMessageBox.Cancel:
177 return
178
179 # build the table tag
180 tablelist = ["\n<table>\n"]
181 if headerflag == PyQt4.QtGui.QMessageBox.Yes:
182 tablelist.append (" <thead>\n")
183 tablelist.append (" <tr>\n")
184 for i in range (cols):
185 tablelist.append (" <th></th>\n")
186 tablelist.append (" </tr>\n")
187 tablelist.append (" </thead>\n")
188
189 tablelist.append (" <tbody>\n")
190 for j in range (rows):
191 tablelist.append (" <tr>\n")
192 for i in range (cols):
193 tablelist.append (" <td></td>\n")
194 tablelist.append (" </tr>\n")
195 tablelist.append (" </tbody>\n")
196 tablelist.append ("</table>\n")
197
198 # insert the table
199 textcur = self.content.textCursor ()
200 textcur.clearSelection ()
201 for item in tablelist:
202 textcur.insertText (item)
203
204
205
206 # when break is clicked
207 def onBreak (self):
208 textcur = self.content.textCursor ()
209 breaktxt = "\n<br />\n"
210 textcur.clearSelection ()
211 textcur.insertText (breaktxt)