UI functionality added to main view
[biaweb_qt.git] / main_window.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Main Window class
3
4 import PyQt4
5 import ui_main_window
6 import site_configuration_dialog as scd
7 import category_dialog as catd
8 import biaweb_db
9 import sys
10
11 class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
12 def __init__ (self):
13 PyQt4.QtGui.QMainWindow.__init__ (self)
14 self.setupUi (self)
15 self.current_db = None
16
17 # refresh the category list
18 def repopulate_categories (self):
19 recs = biaweb_db.get_categories (self.current_db)
20 if recs == False:
21 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
22 return
23
24 self.categories.clear ()
25 for (id, name, desc, stub) in recs:
26 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)])
27 self.categories.addTopLevelItem (qrow)
28
29 # refresh the articles list
30 def repopulate_articles (self, catid=None):
31 recs = biaweb_db.get_articles (self.current_db, catid)
32 if recs == False:
33 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
34 return
35
36 self.articles.clear ()
37 for item in recs:
38 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])])
39 self.articles.addTopLevelItem (qrow)
40
41 # category add action
42 def onCategoryAdd (self):
43 # if there is no database
44 if self.current_db == None:
45 PyQt4.QtGui.QMessageBox.critical (self, "Error",
46 "Cannot add category. You need to create or open a website first.")
47 else:
48 # show the category add dialog
49 dlg = catd.CategoryDialog (self)
50 # if OK button is pressed
51 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
52 cat_name = str (dlg.category_name.text ()).strip ()
53 cat_desc = str (dlg.category_desc.text ()).strip ()
54 cat_stub = str (dlg.category_stub.text ()).strip ()
55 ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
56 if ret == True:
57 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
58 self.repopulate_categories ()
59 self.repopulate_articles ()
60 else:
61 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
62
63 # file open menu is clicked
64 def onFileOpen (self):
65 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
66 if filename:
67 self.current_db = str (filename)
68 self.setWindowTitle ("BiaWeb - " + self.current_db)
69 self.repopulate_categories ()
70 self.repopulate_articles ()
71
72 # file new menu is clicked
73 def onFileNew (self):
74 # show the site configuration dialog to get the new site details
75 dlg = scd.SiteConfigDialog (self)
76 # if OK button is pressed
77 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
78 site_title = str (dlg.site_title.text ()).strip ()
79 site_url = str (dlg.site_url.text ()).strip ()
80 keywords = str (dlg.keywords.text ()).strip ()
81 destination = str (dlg.destination.text ()).strip ()
82 description = str (dlg.description.toPlainText ()).strip ()
83 num_rss = dlg.num_rss_items.value ()
84 copyright = str (dlg.copyright.text ()).strip ()
85
86 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
87
88 if savefilename:
89 self.current_db = str (savefilename)
90 self.setWindowTitle ("BiaWeb - " + self.current_db)
91 flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
92 copyright, num_rss, destination)
93 if flag == True:
94 PyQt4.QtGui.QMessageBox.information (self, "Success",
95 "New site db successfully created")
96 self.articles.clear ()
97 self.categories.clear ()
98 else:
99 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
100
101 # file quit is clicked
102 def onFileQuit (self):
103 sys.exit (0)