<commit_before>from PyQt4.QtCore import SIGNAL, Qt, QSize
from PyQt4.QtGui import QMainWindow, QLabel, QProgressBar, QMessageBox, QSpacerItem, QSizePolicy, QDialog, QTextEdit, QVBoxLayout
from PyQt4 import uic
from exceptions import RuntimeError
from datetime import datetime
import os.path

from src.showtabui import showtabui
from src.settings import settings
from src.storage import storage
from src.backends.thetvdbbackend import thetvdbbackend
from src.dataclasses import showmodel

class mainui(QMainWindow):
    ''' The main window '''
    
    def __init__(self, parent = None):
        '''
        @type parent: QWidget
        '''
        QMainWindow.__init__(self, parent)
        self.ui = uic.loadUi(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ui/main.ui'), self)
        
        self.__lastupdatedlabel = QLabel()
        
        self.ui.statusbar.addWidget(self.__lastupdatedlabel, 1)
                
        self.connect(self.ui.searchbutton, SIGNAL('pressed()'), self.search)
        self.connect(self.ui.addbutton, SIGNAL('pressed()'), self.add)
        self.connect(self.ui.searchtext, SIGNAL('textChanged(QString)'), self.enablesearch)
        self.connect(self.ui.showlist, SIGNAL('doubleClicked(QModelIndex)'), self.openshow)
        self.connect(self.ui.refreshshowsbutton, SIGNAL('pressed()'), self.updateshows)
        self.connect(self.ui.removeshowsbutton, SIGNAL('pressed()'), self.removeshows)
        self.connect(self.ui.getshowlistbutton, SIGNAL('pressed()'), self.getshowlist)
        
        self.connect(self.ui.settingsautoswitchshowtab, SIGNAL('stateChanged(int)'), self.settingsautoswitchshowtabvaluechanged)
        self.connect(self.ui.settingsautoswitchseasontab, SIGNAL('stateChanged(int)'), self.settingsautoswitchseasontabvaluechanged)
                     

    def load(self):
        ''' Load shows, setup the basics '''
        
        label = QLabel()
        self.ui.statusbar.addWidget(label)
        
        try:
            label.setText('Loading settings')
            self.settings = settings()
            
            if self.settings.get(settings.categories.application, settings.keys.firstrun, bool):
                self.ui.tabs.setCurrentIndex(4)
                self.settings.set(settings.categories.application, settings.keys.firstrun, False)
            
            lastupdated = self.settings.get(settings.categories.application, settings.keys.lastupdated, datetime)
            self.setlastupdatedstatus(lastupdated)
            
            self.ui.settingsautoswitchshowtab.setChecked(self.settings.get(settings.categories.application, settings.keys.autoswitchshowtab, bool))
            self.ui.settingsautoswitchseasontab.setChecked(self.settings.get(settings.categories.application, settings.keys.autoswitchseasontab, bool))

            label.setText('Loading storage')
            self.storage = storage(self.settings.path())
            
            label.setText('Loading backend')
            self.backend = thetvdbbackend(self.settings)

            label.setText('Displaying shows')
            self.displayshows()
            
            self.displayshowstatuses()
        except RuntimeError as (errnum, errstr):
            print 'An error has occurred (%d: %s)' % (errnum, errstr)
            
        self.ui.statusbar.removeWidget(label)                
            
    def search(self):
        ''' Search for a show and display a list of results '''
        
        label = QLabel('Searching for: %s' % self.ui.searchtext.text())
        self.ui.statusbar.addWidget(label)

        results = self.backend.searchshow(self.ui.searchtext.text())
        
        resultsmodel = showmodel(self.backend, results)
        self.ui.searchresultsview.setModel(resultsmodel)
        self.ui.addbutton.setEnabled(False)
        self.connect(self.ui.searchresultsview.selectionModel(), SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.enableadd)

        self.ui.statusbar.removeWidget(label)                
        
    def add(self):
        ''' Add a show that was selected in the search results '''
        
        row = self.ui.searchresultsview.selectedIndexes()[0].row()
        show = self.ui.searchresultsview.model().getshow(row)
        id = show.id

        label = QLabel('Adding: %s' % show.name)
        self.ui.statusbar.addWidget(label)
        
        self.backend.addshow(id)
        self.ui.showlist.model().addshow(show)

        self.ui.statusbar.removeWidget(label)                
        
    def enablesearch(self, text):
        ''' Enable search when text is valid
        @type text: QString
        '''
        
        self.ui.searchbutton.setEnabled(not text.isEmpty())
        
    def enableadd(self, selected, deselected):
        ''' Enable adding when the selected item is valid
        @type selected: QItemSelection
        @type deselected: QItemSelection
        '''
        
        self.ui.addbutton.setEnabled(selected != None)
        
    def displayshows(self):
        ''' Display local shows '''
        
        label = QLabel('Displaying shows')
        self.ui.statusbar.addWidget(label)

        shows = self.backend.getlocalshows()
        
        showsmodel = showmodel(self.backend, shows)
        self.ui.showlist.setModel(showsmodel)

        self.ui.statusbar.removeWidget(label)                
         
    def openshow(self, index):
        ''' Open a local show in a new tab
        @type index: QModelIndex
        '''
        
        row = index.row()
        show = self.ui.showlist.model().getshow(row)
        
        add = True

        # Check if a tab is already open for this show
        for tabnum in range(len(self.ui.tabs)):
            if hasattr(self.ui.tabs.widget(tabnum), 'showid') and self.ui.tabs.widget(tabnum).showid == show.id:
                add = False
                break
        
        if add:    
            newtab = showtabui()
            newtab.loadshow(show, self.backend, self.settings)
    
            self.connect(newtab, SIGNAL('episodestatuschanged(QString, QDateTime)'), self.ui.showlist.model().setshowdate)

            tabindex = self.ui.tabs.addTab(newtab, show.name)
            
            if self.settings.get(settings.categories.application, settings.keys.autoswitchshowtab, bool):
                self.ui.tabs.setCurrentIndex(tabindex)
        
    def updateshows(self):
        ''' Update local shows with remote content '''
        
        label = QLabel('Updating: ')
        bar = QProgressBar()
        bar.setMinimum(0)
        bar.setMaximum(self.ui.showlist.model().rowCount())
        bar.setValue(bar.minimum())
        
        self.ui.statusbar.addWidget(label)
        self.ui.statusbar.addWidget(bar)
        
        for row in range(self.ui.showlist.model().rowCount()):
            self.backend.updateshow(self.ui.showlist.model().getshow(row).id)
            bar.setValue(row)
            
        self.ui.statusbar.removeWidget(bar)
        self.ui.statusbar.removeWidget(label)<commit_msg>Swap the ampersand(&) with 'and' to prevent the underscore alt-shortcut from appearing in the tab strip<commit_after>from PyQt4.QtCore import SIGNAL, Qt, QSize
from PyQt4.QtGui import QMainWindow, QLabel, QProgressBar, QMessageBox, QSpacerItem, QSizePolicy, QDialog, QTextEdit, QVBoxLayout
from PyQt4 import uic
from exceptions import RuntimeError
from datetime import datetime
import os.path

from src.showtabui import showtabui
from src.settings import settings
from src.storage import storage
from src.backends.thetvdbbackend import thetvdbbackend
from src.dataclasses import showmodel

class mainui(QMainWindow):
    ''' The main window '''
    
    def __init__(self, parent = None):
        '''
        @type parent: QWidget
        '''
        QMainWindow.__init__(self, parent)
        self.ui = uic.loadUi(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ui/main.ui'), self)
        
        self.__lastupdatedlabel = QLabel()
        
        self.ui.statusbar.addWidget(self.__lastupdatedlabel, 1)
                
        self.connect(self.ui.searchbutton, SIGNAL('pressed()'), self.search)
        self.connect(self.ui.addbutton, SIGNAL('pressed()'), self.add)
        self.connect(self.ui.searchtext, SIGNAL('textChanged(QString)'), self.enablesearch)
        self.connect(self.ui.showlist, SIGNAL('doubleClicked(QModelIndex)'), self.openshow)
        self.connect(self.ui.refreshshowsbutton, SIGNAL('pressed()'), self.updateshows)
        self.connect(self.ui.removeshowsbutton, SIGNAL('pressed()'), self.removeshows)
        self.connect(self.ui.getshowlistbutton, SIGNAL('pressed()'), self.getshowlist)
        
        self.connect(self.ui.settingsautoswitchshowtab, SIGNAL('stateChanged(int)'), self.settingsautoswitchshowtabvaluechanged)
        self.connect(self.ui.settingsautoswitchseasontab, SIGNAL('stateChanged(int)'), self.settingsautoswitchseasontabvaluechanged)
                     

    def load(self):
        ''' Load shows, setup the basics '''
        
        label = QLabel()
        self.ui.statusbar.addWidget(label)
        
        try:
            label.setText('Loading settings')
            self.settings = settings()
            
            if self.settings.get(settings.categories.application, settings.keys.firstrun, bool):
                self.ui.tabs.setCurrentIndex(4)
                self.settings.set(settings.categories.application, settings.keys.firstrun, False)
            
            lastupdated = self.settings.get(settings.categories.application, settings.keys.lastupdated, datetime)
            self.setlastupdatedstatus(lastupdated)
            
            self.ui.settingsautoswitchshowtab.setChecked(self.settings.get(settings.categories.application, settings.keys.autoswitchshowtab, bool))
            self.ui.settingsautoswitchseasontab.setChecked(self.settings.get(settings.categories.application, settings.keys.autoswitchseasontab, bool))

            label.setText('Loading storage')
            self.storage = storage(self.settings.path())
            
            label.setText('Loading backend')
            self.backend = thetvdbbackend(self.settings)

            label.setText('Displaying shows')
            self.displayshows()
            
            self.displayshowstatuses()
        except RuntimeError as (errnum, errstr):
            print 'An error has occurred (%d: %s)' % (errnum, errstr)
            
        self.ui.statusbar.removeWidget(label)                
            
    def search(self):
        ''' Search for a show and display a list of results '''
        
        label = QLabel('Searching for: %s' % self.ui.searchtext.text())
        self.ui.statusbar.addWidget(label)

        results = self.backend.searchshow(self.ui.searchtext.text())
        
        resultsmodel = showmodel(self.backend, results)
        self.ui.searchresultsview.setModel(resultsmodel)
        self.ui.addbutton.setEnabled(False)
        self.connect(self.ui.searchresultsview.selectionModel(), SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.enableadd)

        self.ui.statusbar.removeWidget(label)                
        
    def add(self):
        ''' Add a show that was selected in the search results '''
        
        row = self.ui.searchresultsview.selectedIndexes()[0].row()
        show = self.ui.searchresultsview.model().getshow(row)
        id = show.id

        label = QLabel('Adding: %s' % show.name)
        self.ui.statusbar.addWidget(label)
        
        self.backend.addshow(id)
        self.ui.showlist.model().addshow(show)

        self.ui.statusbar.removeWidget(label)                
        
    def enablesearch(self, text):
        ''' Enable search when text is valid
        @type text: QString
        '''
        
        self.ui.searchbutton.setEnabled(not text.isEmpty())
        
    def enableadd(self, selected, deselected):
        ''' Enable adding when the selected item is valid
        @type selected: QItemSelection
        @type deselected: QItemSelection
        '''
        
        self.ui.addbutton.setEnabled(selected != None)
        
    def displayshows(self):
        ''' Display local shows '''
        
        label = QLabel('Displaying shows')
        self.ui.statusbar.addWidget(label)

        shows = self.backend.getlocalshows()
        
        showsmodel = showmodel(self.backend, shows)
        self.ui.showlist.setModel(showsmodel)

        self.ui.statusbar.removeWidget(label)                
         
    def openshow(self, index):
        ''' Open a local show in a new tab
        @type index: QModelIndex
        '''
        
        row = index.row()
        show = self.ui.showlist.model().getshow(row)
        
        add = True

        # Check if a tab is already open for this show
        for tabnum in range(len(self.ui.tabs)):
            if hasattr(self.ui.tabs.widget(tabnum), 'showid') and self.ui.tabs.widget(tabnum).showid == show.id:
                add = False
                break
        
        if add:    
            newtab = showtabui()
            newtab.loadshow(show, self.backend, self.settings)
    
            self.connect(newtab, SIGNAL('episodestatuschanged(QString, QDateTime)'), self.ui.showlist.model().setshowdate)

            tabindex = self.ui.tabs.addTab(newtab, show.name.replace('&', 'and'))
            
            if self.settings.get(settings.categories.application, settings.keys.autoswitchshowtab, bool):
                self.ui.tabs.setCurrentIndex(tabindex)
        
    def updateshows(self):
        ''' Update local shows with remote content '''
        
        label = QLabel('Updating: ')
        bar = QProgressBar()
        bar.setMinimum(0)
        bar.setMaximum(self.ui.showlist.model().rowCount())
        bar.setValue(bar.minimum())
        
        self.ui.statusbar.addWidget(label)
        self.ui.statusbar.addWidget(bar)
        
        for row in range(self.ui.showlist.model().rowCount()):
            self.backend.updateshow(self.ui.showlist.model().getshow(row).id)
            bar.setValue(row)
            
        self.ui.statusbar.removeWidget(bar)
        self.ui.statusbar.removeWidget(label)
{'fraction_non_alphanumeric': 0.1095553053757388, 'fraction_numerical': 0.0009850830284266817, 'mean_word_length': 2.309196740395809, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '174664', 'n_tokens_mistral': 4066, 'n_tokens_neox': 3994, 'n_words': 725}