Article dialog formatting controls implemented
[biaweb_qt.git] / article_dialog.py
index 3494138..02c0771 100644 (file)
@@ -9,23 +9,33 @@ class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
                PyQt4.QtGui.QDialog.__init__ (self, parent)
                self.setupUi (self)
 
+       # when rejected, confirm first
+       def reject (self):
+               ans = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
+                                       "Are you sure you wish to cancel all changes?",
+                                       PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
+               if ans == PyQt4.QtGui.QMessageBox.Yes:
+                       PyQt4.QtGui.QDialog.reject (self)
+
+
        # when bold is clicked
        def onBold (self):
                textcur = self.content.textCursor ()
                sel = textcur.selectedText ()
-               boldsel = "<b>" + str (sel) + "</b>"
+               boldsel = "<b>" + sel + "</b>"
                textcur.insertText (boldsel)
 
        # when block quote is clicked
        def onBQuote (self):
                textcur = self.content.textCursor ()
                sel = textcur.selectedText ()
-               quotesel = "<blockquote>\n" + str (sel) + "\n</blockquote>"
+               quotesel = "<blockquote>\n" + sel + "\n</blockquote>"
                textcur.insertText (quotesel)
 
        # when bullet is clicked
        def onBullet (self):
-               pass
+               # insert bulleted list
+               self.insert_list_items ()
 
        # when code is clicked
        def onCode (self):
@@ -36,52 +46,166 @@ class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog):
 
        # when horiz rule is clicked
        def onHRule (self):
-               pass
+               textcur = self.content.textCursor ()
+               hrule = "\n<hr />\n"
+               textcur.clearSelection ()
+               textcur.insertText (hrule)
 
        # when image is clicked
        def onImage (self):
-               pass
+               img_url, ok = PyQt4.QtGui.QInputDialog.getText (self, "Image URL", "URL of the image to insert")
+               if not ok or img_url.isEmpty ():
+                       return
+               alt_txt, ok = PyQt4.QtGui.QInputDialog.getText (self, "Alt text", "Alternate text of the image")
+               if not ok or alt_txt.isEmpty ():
+                       return
+
+               textcur = self.content.textCursor ()
+               imgtag = '<img src="' + img_url + '" alt="' + alt_txt + '" />'
+               textcur.clearSelection ()
+               textcur.insertText (imgtag)
 
        # when italic is clicked
        def onItalic (self):
-               pass
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               italsel = "<i>" + sel + "</i>"
+               textcur.insertText (italsel)
 
        # when link is clicked
        def onLink (self):
-               pass
+               linkhref, ok = PyQt4.QtGui.QInputDialog.getText (self, "Link URL", "URL of the link to insert")
+               if not ok or linkhref.isEmpty ():
+                       return
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               linksel = '<a href="' + linkhref + '">' + sel + "</a>"
+               textcur.insertText (linksel)
 
        # when numbered is clicked
        def onNumber (self):
-               pass
+               # numbered list
+               self.insert_list_items (True)
+
+       # get a list of items in numbered or bulleted list
+       def insert_list_items (self, numbered = False):
+               if numbered == True:
+                       title = "Numbered list"
+                       otag = "<ol>\n"
+                       ctag = "</ol>\n"
+               else:
+                       title = "Bulleted list"
+                       otag = "<ul>\n"
+                       ctag = "</ul>\n"
+
+               itemlist = [ otag ]
+               while True:
+                       num_item, ok = PyQt4.QtGui.QInputDialog.getText (self, title,
+                                                       "List item content (leave blank to finish the list)")
+                       # if cancelled quit
+                       if not ok:
+                               return
+                       # if item is empty then close the list
+                       if num_item.isEmpty ():
+                               break
+                       itemlist.append ("   <li>" + num_item + "</li>\n")
+               itemlist.append (ctag)
+
+               textcur = self.content.textCursor ()
+               textcur.clearSelection ()
+               for item in itemlist:
+                       textcur.insertText (item)
+
 
        # when center is clicked
        def onCenter (self):
-               pass
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               paracentersel = '<p style="text-align:center;">' + sel + "</p>"
+               textcur.insertText (paracentersel)
 
        # when justify is clicked
        def onJustify (self):
-               pass
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               parajustsel = '<p style="text-align:justify;">' + sel + "</p>"
+               textcur.insertText (parajustsel)
 
        # when left is clicked
        def onLeft (self):
-               pass
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               paraleftsel = '<p style="text-align:left;">' + sel + "</p>"
+               textcur.insertText (paraleftsel)
 
        # when right is clicked
        def onRight (self):
-               pass
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               pararightsel = '<p style="text-align:right;">' + sel + "</p>"
+               textcur.insertText (pararightsel)
 
        # when pre is clicked
        def onPre (self):
-               pass
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               presel = "<pre>" + sel + "</pre>"
+               textcur.insertText (presel)
 
        # when para is clicked
        def onPara (self):
-               pass
+               textcur = self.content.textCursor ()
+               sel = textcur.selectedText ()
+               parasel = "<p>" + sel + "</p>"
+               textcur.insertText (parasel)
 
        # when table is clicked
        def onTable (self):
-               pass
+               # get the number of rows
+               rows, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table rows")
+               if not ok:
+                       return
+               # get the number of columns
+               cols, ok = PyQt4.QtGui.QInputDialog.getInt (self, "Table", "Number of table columns")
+               if not ok:
+                       return
+               # should there be a header row?
+               headerflag = PyQt4.QtGui.QMessageBox.question (self, "Header", "Do you want additional header row?",
+                                                                                                       PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No,
+                                                                                                       PyQt4.QtGui.QMessageBox.Cancel)
+               if headerflag == PyQt4.QtGui.QMessageBox.Cancel:
+                       return
+
+               # build the table tag
+               tablelist = ["\n<table>\n"]
+               if headerflag == PyQt4.QtGui.QMessageBox.Yes:
+                       tablelist.append (" <thead>\n")
+                       tablelist.append ("  <tr>\n")
+                       for i in range (cols):
+                               tablelist.append ("   <th></th>\n")
+                       tablelist.append ("  </tr>\n")
+                       tablelist.append (" </thead>\n")
+
+               tablelist.append (" <tbody>\n")
+               for j in range (rows):
+                       tablelist.append ("  <tr>\n")
+                       for i in range (cols):
+                               tablelist.append ("   <td></td>\n")
+                       tablelist.append ("  </tr>\n")
+               tablelist.append (" </tbody>\n")
+               tablelist.append ("</table>\n")
+
+               # insert the table
+               textcur = self.content.textCursor ()
+               textcur.clearSelection ()
+               for item in tablelist:
+                       textcur.insertText (item)
+
+
 
        # when break is clicked
        def onBreak (self):
-               pass
+               textcur = self.content.textCursor ()
+               breaktxt = "\n<br />\n"
+               textcur.clearSelection ()
+               textcur.insertText (breaktxt)