view MainWindow.py @ 46:03358c113170

Better preferences handling: the GUI's responsibility is to convert the input from the event into a boolean value
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 16 May 2010 07:54:24 +0200
parents 0c0d1760b737
children a8442c3487b5
line wrap: on
line source


from AddFeed import AddFeed
from DisplayModel import DisplayModel
from Feed import Feed
from FeedEntry import FeedEntry
from PreferencesDialog import PreferencesDialog
from PyQt4 import QtGui
from PyQt4.QtCore import QUrl
from PyQt4.QtGui import QFont
from Ui_MainWindow import Ui_MainWindow
import FeedUpdater
import subprocess

class MainWindow(QtGui.QMainWindow):
    def __init__(self, session=None):
        QtGui.QWidget.__init__(self, None)
        self.session = session
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.updateFeedList()
        self.initFeedEntryList()
        
    def updateFeedList(self):        
        self.allFeeds = Feed.all(self.session)
        feedModel = DisplayModel(self, self.allFeeds, Feed.userPresentableString)
        self.ui.feedList.setModel(feedModel)
        self.ui.feedList.update()

    def initFeedEntryList(self):
        self.ui.feedEntryList.setItemDelegate(FeedEntryItemDelegate())

    def feedSelected(self, index):
        self.selectedFeed = self.allFeeds[index.row()]
        self.sortedEntries = self.selectedFeed.entriesSortedByUpdateDate()
        model = DisplayModel(self, self.sortedEntries, FeedEntry.userPresentableString)
        self.ui.feedEntryList.setModel(model)
        self.ui.feedEntryList.itemDelegate().entries = self.sortedEntries
        self.ui.feedEntryList.update()
        
    def feedEntrySelected(self, index):
        self.ui.menuArticle.setEnabled(True)
        row = index.row()
        self.selectedEntry = self.sortedEntries[row]
        baseUrl = QUrl(self.selectedEntry.link) # TODO this is the wrong base url, figure out the correct one
        self.ui.webView.setHtml(self.selectedEntry.summary, baseUrl)
    
    def toggleReadOnSelectedEntry(self):
        self.selectedEntry.toggleRead()
        self.session.commit()
        self.ui.feedList.update()
    
    def addFeed(self):
        addFeed = AddFeed(self.session)
        success = addFeed.exec_()
        if success:
            url = addFeed.getUrl()
            FeedUpdater.createNewFeed(url, self.session)
            # TODO get status from feedUpdater and display in status area of the main window
            self.updateFeedList()

    def openSelectedEntryInBrowser(self):
        # TODO make browser configurable
        browser = "/usr/local/bin/opera"
        subprocess.Popen([browser, self.selectedEntry.link])
        
    def openLinkFromSelectedEntry(self):
        url = QUrl(self.selectedEntry.link)
        self.ui.webView.load(url)
        self.ui.webView.show()
        
    def showPreferences(self):
        preferences = PreferencesDialog(self.session)
        preferences.exec_()
        

class FeedEntryItemDelegate(QtGui.QStyledItemDelegate):
    def __init__(self):
        QtGui.QStyledItemDelegate.__init__(self, None)
    
    def paint(self, painter, style, index):
        row = index.row()
        entry = self.entries[row]
        if entry.read:
            style.font.setWeight(QFont.Normal)
        else:
            style.font.setWeight(QFont.Bold)
        QtGui.QStyledItemDelegate.paint(self, painter, style, index)