6f033d6af2cb88fd19500cc268dc41f5b3aa6f6b
[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 article_dialog as artd
9 import biaweb_db
10 import sys
11
12 class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
13 def __init__ (self):
14 PyQt4.QtGui.QMainWindow.__init__ (self)
15 self.setupUi (self)
16 self.current_db = None
17
18 # refresh the category list
19 def repopulate_categories (self):
20 recs = biaweb_db.get_categories (self.current_db)
21 if not recs:
22 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
23 return
24
25 self.categories.clear ()
26 for (id, name, desc, stub) in recs:
27 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)])
28 self.categories.addTopLevelItem (qrow)
29
30 # refresh the articles list
31 def repopulate_articles (self, catid=None):
32 recs = biaweb_db.get_articles (self.current_db, catid)
33 if not recs:
34 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
35 return
36
37 self.articles.clear ()
38 for item in recs:
39 qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])])
40 self.articles.addTopLevelItem (qrow)
41
42 # when the view is refreshed
43 def onViewRefresh (self):
44 if self.current_db is not None:
45 self.repopulate_categories ()
46 self.repopulate_articles ()
47
48 # when add article is triggered
49 def onArticleAdd (self):
50 if self.current_db is None:
51 PyQt4.QtGui.QMessageBox.critical (self, "Error",
52 "Cannot create article. You need to create or open a website first")
53 else:
54 catid = self.get_selected_item_id (self.categories)
55 # if no category is selected
56 if catid is None:
57 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected for article")
58 return
59 artdlg = artd.ArticleDialog (self)
60 cats = biaweb_db.get_categories (self.current_db)
61
62 artdlg.populate_categories (cats, catid)
63
64 # if OK is pressed
65 if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
66 title = str (artdlg.article_title.text ()).strip ()
67 keywords = str (artdlg.keywords.text ()).strip ()
68 summary = str (artdlg.summary.toPlainText ()).strip ()
69 content = str (artdlg.content.toPlainText ()).strip ()
70 cid, ok = artdlg.category.itemData (artdlg.category.currentIndex ()).toInt ()
71 # if catid is not an integer then abort
72 if not ok:
73 return
74 rating = artdlg.rating.value ()
75 stub = str (artdlg.stub.text ()).strip ()
76
77 ret = biaweb_db.create_article (self.current_db, title, summary, keywords, content, cid,
78 stub, rating)
79 if ret:
80 PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully created")
81 self.repopulate_categories ()
82 self.repopulate_articles (catid)
83 else:
84 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating article")
85
86 # when edit article is triggered
87 def onArticleEdit (self):
88 if self.current_db is None:
89 PyQt4.QtGui.QMessageBox.critical (self, "Error",
90 "Cannot edit article. You need to create or open a website first")
91 else:
92 # get the selected article
93 artid = self.get_selected_item_id (self.articles)
94 catid = self.get_selected_item_id (self.categories)
95
96 # no article is selected
97 if artid is None:
98 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
99 return
100 article = biaweb_db.get_article (self.current_db, artid)
101 # if article cannot be read
102 if not article:
103 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading article")
104 return
105 artdlg = artd.ArticleDialog (self)
106
107 artdlg.article_title.setText (article[1])
108 artdlg.summary.setPlainText (article[2])
109 artdlg.keywords.setText (article[3])
110 artdlg.content.setPlainText (article[4])
111 # populate category combo box and set active category to the article's category
112 cats = biaweb_db.get_categories (self.current_db)
113 artdlg.populate_categories (cats, article[7])
114 artdlg.stub.setText (article[8])
115 artdlg.rating.setValue (article[9])
116
117 # ok is pressed
118 if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
119 title = str (artdlg.article_title.text ()).strip ()
120 summary = str (artdlg.summary.toPlainText ()).strip ()
121 keywords = str (artdlg.keywords.text ()).strip ()
122 content = str (artdlg.content.toPlainText ()).strip ()
123 cid, ok = artdlg.category.itemData (artdlg.category.currentIndex()).toInt ()
124 # if cat id is not an integer
125 if not ok:
126 return
127 rating = artdlg.rating.value ()
128 stub = str (artdlg.stub.text ()).strip ()
129 ret = biaweb_db.update_article (self.current_db, artid, title,
130 summary, keywords, content, cid, stub, rating)
131 if ret:
132 PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully updated")
133 self.repopulate_articles (catid)
134 else:
135 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating article")
136
137 # when delete article is triggered
138 def onArticleDelete (self):
139 if self.current_db is None:
140 PyQt4.QtGui.QMessageBox.critical (self, "Error",
141 "Cannot delete article. You need to create or open a website first")
142 else:
143 # get the selected article
144 artid = self.get_selected_item_id (self.articles)
145 catid = self.get_selected_item_id (self.categories)
146 if artid is None:
147 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
148 return
149 # get confirmation on delete
150 flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
151 "Are you sure you wish to delete the selected article?",
152 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
153 # confirmed
154 if flag == PyQt4.QtGui.QMessageBox.Yes:
155 ret = biaweb_db.delete_article (self.current_db, artid)
156 if not ret:
157 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting article")
158 else:
159 self.repopulate_articles (catid)
160
161 # when category item is activated
162 def onCategoryItemActivated (self):
163 self.onCategoryEdit ()
164
165 # when article item is activated
166 def onArticleItemActivated (self):
167 self.onArticleEdit ()
168
169 # when item selection is changed in categories tree widget
170 def onCategorySelectionChanged (self):
171 if self.current_db is not None:
172 catid = self.get_selected_item_id (self.categories)
173 self.repopulate_articles (catid)
174
175 # when configuration menu is activated
176 def onConfiguration (self):
177 if self.current_db is None:
178 PyQt4.QtGui.QMessageBox.critical (self, "Error",
179 "Cannot edit configuration. You need to create or open a website first")
180 else:
181 dlg = scd.SiteConfigDialog (self)
182 configs = biaweb_db.get_configuration (self.current_db)
183
184 if not configs:
185 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
186 return
187
188 # set the data
189 dlg.site_url.setText (configs[0])
190 dlg.site_title.setText (configs[1])
191 dlg.keywords.setText (configs[2])
192 dlg.description.setPlainText (configs[3])
193 dlg.num_rss_items.setValue (configs[4])
194 dlg.destination.setText (configs[5])
195 dlg.copyright.setText (configs[6])
196
197 if (dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted):
198 # if accepted, get the data and store in database
199 site_title = str (dlg.site_title.text ()).strip ()
200 site_url = str (dlg.site_url.text ()).strip ()
201 keywords = str (dlg.keywords.text ()).strip ()
202 destination = str (dlg.destination.text ()).strip ()
203 description = str (dlg.description.toPlainText ()).strip ()
204 num_rss = dlg.num_rss_items.value ()
205 copyright = str (dlg.copyright.text ()).strip ()
206 # database update
207 flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
208 copyright, num_rss, destination)
209 if not flag:
210 PyQt4.QtGui.QMessageBox.critical (self, "Error",
211 "SQLite 3 error in updating configuration")
212
213 # function to get the category or article ID from current selected item in a tree widget
214 def get_selected_item_id (self, twidget):
215 selitems = twidget.selectedItems ()
216 if selitems:
217 # get the first column data as integer
218 selindex = selitems[0].data(0, 0).toInt ()[0]
219 return selindex
220 else:
221 return None
222
223 # category edit action
224 def onCategoryEdit (self):
225 # if no database is open
226 if self.current_db is None:
227 PyQt4.QtGui.QMessageBox.critical (self, "Error",
228 "Cannot edit category. You need to create or open a website first")
229 # database is open
230 else:
231 catid = self.get_selected_item_id (self.categories)
232 # if no category is selected, display an error
233 if catid is None:
234 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
235 # category is selected
236 else:
237 cat = biaweb_db.get_category (self.current_db, catid)
238 # if the category cannot be retrieved from database
239 if not cat:
240 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading category")
241 return
242
243 # set the data in the dialog
244 dlg = catd.CategoryDialog (self)
245 dlg.category_name.setText (cat[1])
246 dlg.category_desc.setText (cat[2])
247 dlg.category_stub.setText (cat[3])
248
249 # if accepted
250 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
251 category_name = str (dlg.category_name.text ()).strip ()
252 category_desc = str (dlg.category_desc.text ()).strip ()
253 category_stub = str (dlg.category_stub.text ()).strip ()
254
255 ret = biaweb_db.update_category (self.current_db,
256 catid, category_name, category_desc, category_stub)
257 if ret:
258 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
259 self.repopulate_categories ()
260 else:
261 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
262
263
264 # category delete action
265 def onCategoryDelete (self):
266 # if there is no database
267 if self.current_db is None:
268 PyQt4.QtGui.QMessageBox.critical (self, "Error",
269 "Cannot delete category. You need to create or open a website first")
270 else:
271 # get the selected category
272 catid = self.get_selected_item_id (self.categories)
273 if catid is None:
274 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
275 # category is selected
276 else:
277 # get confirmation first
278 flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
279 "This will delete the category and all associated articles. Are you sure you wish to continue?",
280 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
281 # if confirmed
282 if flag == PyQt4.QtGui.QMessageBox.Yes:
283 ret = biaweb_db.remove_category (self.current_db, catid)
284 if not ret:
285 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
286 else:
287 self.repopulate_categories ()
288 self.repopulate_articles ()
289
290 # category add action
291 def onCategoryAdd (self):
292 # if there is no database
293 if self.current_db is None:
294 PyQt4.QtGui.QMessageBox.critical (self, "Error",
295 "Cannot add category. You need to create or open a website first")
296 else:
297 # show the category add dialog
298 dlg = catd.CategoryDialog (self)
299 # if OK button is pressed
300 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
301 cat_name = str (dlg.category_name.text ()).strip ()
302 cat_desc = str (dlg.category_desc.text ()).strip ()
303 cat_stub = str (dlg.category_stub.text ()).strip ()
304 ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
305 if ret:
306 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
307 self.repopulate_categories ()
308 self.repopulate_articles ()
309 else:
310 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
311
312 # file open menu is clicked
313 def onFileOpen (self):
314 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
315 if filename:
316 self.current_db = str (filename)
317 self.setWindowTitle ("BiaWeb - " + self.current_db)
318 self.repopulate_categories ()
319 self.repopulate_articles ()
320
321 # file new menu is clicked
322 def onFileNew (self):
323 # show the site configuration dialog to get the new site details
324 dlg = scd.SiteConfigDialog (self)
325 # if OK button is pressed
326 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
327 site_title = str (dlg.site_title.text ()).strip ()
328 site_url = str (dlg.site_url.text ()).strip ()
329 keywords = str (dlg.keywords.text ()).strip ()
330 destination = str (dlg.destination.text ()).strip ()
331 description = str (dlg.description.toPlainText ()).strip ()
332 num_rss = dlg.num_rss_items.value ()
333 copyright = str (dlg.copyright.text ()).strip ()
334
335 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
336
337 if savefilename:
338 self.current_db = str (savefilename)
339 self.setWindowTitle ("BiaWeb - " + self.current_db)
340 flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
341 copyright, num_rss, destination)
342 if flag:
343 PyQt4.QtGui.QMessageBox.information (self, "Success",
344 "New site db successfully created")
345 self.articles.clear ()
346 self.categories.clear ()
347 else:
348 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
349
350 # file quit is clicked
351 def onFileQuit (self):
352 sys.exit (0)