4d4afa35dd6e95cb04832aeb7e00212fce277856
[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 # configuration dialog
42 def onConfiguration (self):
43 if self.current_db == None:
44 PyQt4.QtGui.QMessageBox.critical (self, "Error",
45 "Cannot edit configuration. You need to create or open a website first")
46 else:
47 dlg = scd.SiteConfigDialog (self)
48 configs = biaweb_db.get_configuration (self.current_db)
49
50 if configs == False:
51 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
52 return
53
54 # set the data
55 dlg.site_url.setText (configs[0])
56 dlg.site_title.setText (configs[1])
57 dlg.keywords.setText (configs[2])
58 dlg.description.setPlainText (configs[3])
59 dlg.num_rss_items.setValue (configs[4])
60 dlg.destination.setText (configs[5])
61 dlg.copyright.setText (configs[6])
62
63 if (dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted):
64 # if accepted, get the data and store in database
65 site_title = str (dlg.site_title.text ()).strip ()
66 site_url = str (dlg.site_url.text ()).strip ()
67 keywords = str (dlg.keywords.text ()).strip ()
68 destination = str (dlg.destination.text ()).strip ()
69 description = str (dlg.description.toPlainText ()).strip ()
70 num_rss = dlg.num_rss_items.value ()
71 copyright = str (dlg.copyright.text ()).strip ()
72 # database update
73 flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
74 copyright, num_rss, destination)
75 if flag == False:
76 PyQt4.QtGui.QMessageBox.critical (self, "Error",
77 "SQLite 3 error in updating configuration")
78
79 # function to get the category or article ID from current selected item in a tree widget
80 def get_selected_item_id (self, twidget):
81 selitems = twidget.selectedItems ()
82 if selitems:
83 # get the first column data as integer
84 selindex = selitems[0].data(0, 0).toInt ()[0]
85 return selindex
86 else:
87 return None
88
89 # category edit action
90 def onCategoryEdit (self):
91 # if no database is open
92 if self.current_db == None:
93 PyQt4.QtGui.QMessageBox.critical (self, "Error",
94 "Cannot edit category. You need to create or open a website first")
95 # database is open
96 else:
97 catid = self.get_selected_item_id (self.categories)
98 # if no category is selected, display an error
99 if catid is None:
100 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
101 # category is selected
102 else:
103 cat = biaweb_db.get_category (self.current_db, catid)
104 # if the category cannot be retrieved from database
105 if cat == False:
106 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
107 return
108
109 # set the data in the dialog
110 dlg = catd.CategoryDialog (self)
111 dlg.category_name.setText (cat[1])
112 dlg.category_desc.setText (cat[2])
113 dlg.category_stub.setText (cat[3])
114
115 # if accepted
116 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
117 category_name = str (dlg.category_name.text ()).strip ()
118 category_desc = str (dlg.category_desc.text ()).strip ()
119 category_stub = str (dlg.category_stub.text ()).strip ()
120
121 ret = biaweb_db.update_category (self.current_db,
122 catid, category_name, category_desc, category_stub)
123 if ret == True:
124 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
125 else:
126 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
127
128
129 # category add action
130 def onCategoryAdd (self):
131 # if there is no database
132 if self.current_db == None:
133 PyQt4.QtGui.QMessageBox.critical (self, "Error",
134 "Cannot add category. You need to create or open a website first")
135 else:
136 # show the category add dialog
137 dlg = catd.CategoryDialog (self)
138 # if OK button is pressed
139 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
140 cat_name = str (dlg.category_name.text ()).strip ()
141 cat_desc = str (dlg.category_desc.text ()).strip ()
142 cat_stub = str (dlg.category_stub.text ()).strip ()
143 ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
144 if ret == True:
145 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
146 self.repopulate_categories ()
147 self.repopulate_articles ()
148 else:
149 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
150
151 # file open menu is clicked
152 def onFileOpen (self):
153 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
154 if filename:
155 self.current_db = str (filename)
156 self.setWindowTitle ("BiaWeb - " + self.current_db)
157 self.repopulate_categories ()
158 self.repopulate_articles ()
159
160 # file new menu is clicked
161 def onFileNew (self):
162 # show the site configuration dialog to get the new site details
163 dlg = scd.SiteConfigDialog (self)
164 # if OK button is pressed
165 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
166 site_title = str (dlg.site_title.text ()).strip ()
167 site_url = str (dlg.site_url.text ()).strip ()
168 keywords = str (dlg.keywords.text ()).strip ()
169 destination = str (dlg.destination.text ()).strip ()
170 description = str (dlg.description.toPlainText ()).strip ()
171 num_rss = dlg.num_rss_items.value ()
172 copyright = str (dlg.copyright.text ()).strip ()
173
174 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
175
176 if savefilename:
177 self.current_db = str (savefilename)
178 self.setWindowTitle ("BiaWeb - " + self.current_db)
179 flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
180 copyright, num_rss, destination)
181 if flag == True:
182 PyQt4.QtGui.QMessageBox.information (self, "Success",
183 "New site db successfully created")
184 self.articles.clear ()
185 self.categories.clear ()
186 else:
187 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
188
189 # file quit is clicked
190 def onFileQuit (self):
191 sys.exit (0)