b2547b65899ed2131d0ebf1f626d8693969c52e6
[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 recs == False:
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 recs == False:
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 == 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 artdlg.exec_ ()
61
62
63 # when edit article is triggered
64 def onArticleEdit (self):
65 pass
66
67 # when delete article is triggered
68 def onArticleDelete (self):
69 pass
70
71 # when item selection is changed in categories tree widget
72 def onCategorySelectionChanged (self):
73 if self.current_db is not None:
74 catid = self.get_selected_item_id (self.categories)
75 self.repopulate_articles (catid)
76
77 # when configuration menu is activated
78 def onConfiguration (self):
79 if self.current_db == None:
80 PyQt4.QtGui.QMessageBox.critical (self, "Error",
81 "Cannot edit configuration. You need to create or open a website first")
82 else:
83 dlg = scd.SiteConfigDialog (self)
84 configs = biaweb_db.get_configuration (self.current_db)
85
86 if configs == False:
87 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
88 return
89
90 # set the data
91 dlg.site_url.setText (configs[0])
92 dlg.site_title.setText (configs[1])
93 dlg.keywords.setText (configs[2])
94 dlg.description.setPlainText (configs[3])
95 dlg.num_rss_items.setValue (configs[4])
96 dlg.destination.setText (configs[5])
97 dlg.copyright.setText (configs[6])
98
99 if (dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted):
100 # if accepted, get the data and store in database
101 site_title = str (dlg.site_title.text ()).strip ()
102 site_url = str (dlg.site_url.text ()).strip ()
103 keywords = str (dlg.keywords.text ()).strip ()
104 destination = str (dlg.destination.text ()).strip ()
105 description = str (dlg.description.toPlainText ()).strip ()
106 num_rss = dlg.num_rss_items.value ()
107 copyright = str (dlg.copyright.text ()).strip ()
108 # database update
109 flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
110 copyright, num_rss, destination)
111 if flag == False:
112 PyQt4.QtGui.QMessageBox.critical (self, "Error",
113 "SQLite 3 error in updating configuration")
114
115 # function to get the category or article ID from current selected item in a tree widget
116 def get_selected_item_id (self, twidget):
117 selitems = twidget.selectedItems ()
118 if selitems:
119 # get the first column data as integer
120 selindex = selitems[0].data(0, 0).toInt ()[0]
121 return selindex
122 else:
123 return None
124
125 # category edit action
126 def onCategoryEdit (self):
127 # if no database is open
128 if self.current_db == None:
129 PyQt4.QtGui.QMessageBox.critical (self, "Error",
130 "Cannot edit category. You need to create or open a website first")
131 # database is open
132 else:
133 catid = self.get_selected_item_id (self.categories)
134 # if no category is selected, display an error
135 if catid is None:
136 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
137 # category is selected
138 else:
139 cat = biaweb_db.get_category (self.current_db, catid)
140 # if the category cannot be retrieved from database
141 if cat == False:
142 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
143 return
144
145 # set the data in the dialog
146 dlg = catd.CategoryDialog (self)
147 dlg.category_name.setText (cat[1])
148 dlg.category_desc.setText (cat[2])
149 dlg.category_stub.setText (cat[3])
150
151 # if accepted
152 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
153 category_name = str (dlg.category_name.text ()).strip ()
154 category_desc = str (dlg.category_desc.text ()).strip ()
155 category_stub = str (dlg.category_stub.text ()).strip ()
156
157 ret = biaweb_db.update_category (self.current_db,
158 catid, category_name, category_desc, category_stub)
159 if ret == True:
160 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
161 else:
162 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
163
164
165 # category delete action
166 def onCategoryDelete (self):
167 # if there is no database
168 if self.current_db == None:
169 PyQt4.QtGui.QMessageBox.critical (self, "Error",
170 "Cannot edit category. You need to create or open a website first")
171 else:
172 # get the selected category
173 catid = self.get_selected_item_id (self.categories)
174 if catid is None:
175 PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected")
176 # category is selected
177 else:
178 # get confirmation first
179 flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
180 "This will delete the category and all associated articles. Are you sure you wish to continue?",
181 PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
182 # if confirmed
183 if flag == PyQt4.QtGui.QMessageBox.Yes:
184 ret = biaweb_db.remove_category (self.current_db, catid)
185 if ret == False:
186 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
187 else:
188 self.repopulate_categories ()
189 self.repopulate_articles ()
190
191 # category add action
192 def onCategoryAdd (self):
193 # if there is no database
194 if self.current_db == None:
195 PyQt4.QtGui.QMessageBox.critical (self, "Error",
196 "Cannot add category. You need to create or open a website first")
197 else:
198 # show the category add dialog
199 dlg = catd.CategoryDialog (self)
200 # if OK button is pressed
201 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
202 cat_name = str (dlg.category_name.text ()).strip ()
203 cat_desc = str (dlg.category_desc.text ()).strip ()
204 cat_stub = str (dlg.category_stub.text ()).strip ()
205 ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
206 if ret == True:
207 PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
208 self.repopulate_categories ()
209 self.repopulate_articles ()
210 else:
211 PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category")
212
213 # file open menu is clicked
214 def onFileOpen (self):
215 filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
216 if filename:
217 self.current_db = str (filename)
218 self.setWindowTitle ("BiaWeb - " + self.current_db)
219 self.repopulate_categories ()
220 self.repopulate_articles ()
221
222 # file new menu is clicked
223 def onFileNew (self):
224 # show the site configuration dialog to get the new site details
225 dlg = scd.SiteConfigDialog (self)
226 # if OK button is pressed
227 if dlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
228 site_title = str (dlg.site_title.text ()).strip ()
229 site_url = str (dlg.site_url.text ()).strip ()
230 keywords = str (dlg.keywords.text ()).strip ()
231 destination = str (dlg.destination.text ()).strip ()
232 description = str (dlg.description.toPlainText ()).strip ()
233 num_rss = dlg.num_rss_items.value ()
234 copyright = str (dlg.copyright.text ()).strip ()
235
236 savefilename = PyQt4.QtGui.QFileDialog.getSaveFileName (self, "Save site database to")
237
238 if savefilename:
239 self.current_db = str (savefilename)
240 self.setWindowTitle ("BiaWeb - " + self.current_db)
241 flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
242 copyright, num_rss, destination)
243 if flag == True:
244 PyQt4.QtGui.QMessageBox.information (self, "Success",
245 "New site db successfully created")
246 self.articles.clear ()
247 self.categories.clear ()
248 else:
249 PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
250
251 # file quit is clicked
252 def onFileQuit (self):
253 sys.exit (0)