From 7db1961b4f7513f9ad5b6b357a655e16903e7f83 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sat, 27 Nov 2010 14:08:37 +0530 Subject: [PATCH] UI functionality added to main view Some UI functionality added to main window: * Open site database now works * Populated the category and articles view --- biaweb_db.py | 28 +++++++++++++++++++++++++++ main_window.py | 37 +++++++++++++++++++++++++++++++++++ main_window.ui | 49 +++++++++++++++++++++++++++++++++++++++++++++-- ui_main_window.py | 21 +++++++++++++------- 4 files changed, 126 insertions(+), 9 deletions(-) diff --git a/biaweb_db.py b/biaweb_db.py index 5496453..576f284 100644 --- a/biaweb_db.py +++ b/biaweb_db.py @@ -4,6 +4,7 @@ import sqlite3 import os import os.path +import pprint # function to create a category def create_category (dbname, category_name, category_desc, category_stub): @@ -18,6 +19,33 @@ def create_category (dbname, category_name, category_desc, category_stub): except sqlite3.Error: return False +# Function to get list of articles (either full list or just in a category +def get_articles (dbname, category_id=None): + try: + conn = sqlite3.connect (dbname) + c = conn.cursor () + if category_id == None: + c.execute ("SELECT * FROM articles;") + else: + c.execute ("SELECT * FROM articles WHERE cid=?", (category_id,)) + conn.commit () + rows = c.fetchall () + return rows + except sqlite3.Error: + return False + +# Function to get list of categories and return a (category_id, category_name) array +def get_categories (dbname): + try: + conn = sqlite3.connect (dbname) + c = conn.cursor () + c.execute ("SELECT * FROM categories;") + conn.commit () + recs = c.fetchall () + conn.close () + return recs + except sqlite3.Error: + return False # function to create a new site database def create_db (dbname, site_title, site_url, keywords, description, copyright, diff --git a/main_window.py b/main_window.py index 5902fbc..b6cb96d 100644 --- a/main_window.py +++ b/main_window.py @@ -14,6 +14,30 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): self.setupUi (self) self.current_db = None + # refresh the category list + def repopulate_categories (self): + recs = biaweb_db.get_categories (self.current_db) + if recs == False: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories") + return + + self.categories.clear () + for (id, name, desc, stub) in recs: + qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)]) + self.categories.addTopLevelItem (qrow) + + # refresh the articles list + def repopulate_articles (self, catid=None): + recs = biaweb_db.get_articles (self.current_db, catid) + if recs == False: + PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles") + return + + self.articles.clear () + for item in recs: + qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])]) + self.articles.addTopLevelItem (qrow) + # category add action def onCategoryAdd (self): # if there is no database @@ -31,9 +55,20 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub) if ret == True: PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created") + self.repopulate_categories () + self.repopulate_articles () else: PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating category") + # file open menu is clicked + def onFileOpen (self): + filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database") + if filename: + self.current_db = str (filename) + self.setWindowTitle ("BiaWeb - " + self.current_db) + self.repopulate_categories () + self.repopulate_articles () + # file new menu is clicked def onFileNew (self): # show the site configuration dialog to get the new site details @@ -58,6 +93,8 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow): if flag == True: PyQt4.QtGui.QMessageBox.information (self, "Success", "New site db successfully created") + self.articles.clear () + self.categories.clear () else: PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database") diff --git a/main_window.ui b/main_window.ui index 61423a3..dc14412 100644 --- a/main_window.ui +++ b/main_window.ui @@ -23,7 +23,18 @@ - + + + + Article ID + + + + + Article Title + + + @@ -44,7 +55,24 @@ - + + + true + + + true + + + + Category ID + + + + + Category Name + + + @@ -285,10 +313,27 @@ + + action_Open_site + triggered() + MainWindow + onFileOpen() + + + -1 + -1 + + + 316 + 228 + + + onFileNew() onFileQuit() onCategoryAdd() + onFileOpen() diff --git a/ui_main_window.py b/ui_main_window.py index 5d613ca..bce3eb7 100644 --- a/ui_main_window.py +++ b/ui_main_window.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'main_window.ui' # -# Created: Fri Nov 26 17:19:44 2010 +# Created: Sat Nov 27 14:06:22 2010 # by: PyQt4 UI code generator 4.7.4 # # WARNING! All changes made in this file will be lost! @@ -20,9 +20,9 @@ class Ui_MainWindow(object): self.label_2 = QtGui.QLabel(self.centralwidget) self.label_2.setObjectName("label_2") self.gridLayout.addWidget(self.label_2, 0, 0, 1, 1) - self.articles_list = QtGui.QListWidget(self.centralwidget) - self.articles_list.setObjectName("articles_list") - self.gridLayout.addWidget(self.articles_list, 1, 0, 1, 1) + self.articles = QtGui.QTreeWidget(self.centralwidget) + self.articles.setObjectName("articles") + self.gridLayout.addWidget(self.articles, 1, 0, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") @@ -35,9 +35,11 @@ class Ui_MainWindow(object): self.dockWidgetContents_2.setObjectName("dockWidgetContents_2") self.gridLayout_2 = QtGui.QGridLayout(self.dockWidgetContents_2) self.gridLayout_2.setObjectName("gridLayout_2") - self.categories_list = QtGui.QListWidget(self.dockWidgetContents_2) - self.categories_list.setObjectName("categories_list") - self.gridLayout_2.addWidget(self.categories_list, 0, 0, 1, 1) + self.categories = QtGui.QTreeWidget(self.dockWidgetContents_2) + self.categories.setAlternatingRowColors(True) + self.categories.setAllColumnsShowFocus(True) + self.categories.setObjectName("categories") + self.gridLayout_2.addWidget(self.categories, 0, 0, 1, 1) self.dockWidget_2.setWidget(self.dockWidgetContents_2) MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget_2) self.menubar = QtGui.QMenuBar(MainWindow) @@ -116,12 +118,17 @@ class Ui_MainWindow(object): QtCore.QObject.connect(self.actionNew_site, QtCore.SIGNAL("triggered()"), MainWindow.onFileNew) QtCore.QObject.connect(self.action_Quit, QtCore.SIGNAL("triggered()"), MainWindow.onFileQuit) QtCore.QObject.connect(self.action_AddCategory, QtCore.SIGNAL("triggered()"), MainWindow.onCategoryAdd) + QtCore.QObject.connect(self.action_Open_site, QtCore.SIGNAL("triggered()"), MainWindow.onFileOpen) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "BiaWeb - Static Website Content Manager", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Articles in category", None, QtGui.QApplication.UnicodeUTF8)) + self.articles.headerItem().setText(0, QtGui.QApplication.translate("MainWindow", "Article ID", None, QtGui.QApplication.UnicodeUTF8)) + self.articles.headerItem().setText(1, QtGui.QApplication.translate("MainWindow", "Article Title", None, QtGui.QApplication.UnicodeUTF8)) self.dockWidget_2.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Categories", None, QtGui.QApplication.UnicodeUTF8)) + self.categories.headerItem().setText(0, QtGui.QApplication.translate("MainWindow", "Category ID", None, QtGui.QApplication.UnicodeUTF8)) + self.categories.headerItem().setText(1, QtGui.QApplication.translate("MainWindow", "Category Name", None, QtGui.QApplication.UnicodeUTF8)) self.menu_About.setTitle(QtGui.QApplication.translate("MainWindow", "&Help", None, QtGui.QApplication.UnicodeUTF8)) self.menu_Settings.setTitle(QtGui.QApplication.translate("MainWindow", "&Settings", None, QtGui.QApplication.UnicodeUTF8)) self.menu_Content.setTitle(QtGui.QApplication.translate("MainWindow", "&Content", None, QtGui.QApplication.UnicodeUTF8)) -- 2.20.1