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