Menu

[64b5f9]: / SubtitleTab.py  Maximize  Restore  History

Download this file

127 lines (103 with data), 4.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import pysrt
from PyQt4 import QtGui
from SubMergeUI import _fromUtf8, _translate
from SubtitleTableModel import SubtitleTableModel
class SubtitleTabHelper(object):
"""
"""
def __init__(self, tab_widget):
"""
:return:
"""
self.tab_widget = tab_widget
self.tabs = []
def _add_tab(self, tab):
"""
:param tab:
:return:
"""
self.tab_widget.addTab(tab, _fromUtf8(""))
def remove_tab(self, tabIndex):
"""
Remove tab from tab list.
:param tabIndex:
:return:
"""
self.tabs.remove(str(self.tab_widget.tabText(tabIndex)))
self.tab_widget.removeTab(tabIndex)
def create_tab_from_file(self, sub_filename):
"""
Create a subtitle tab using srt file
:param sub_filename:
:return:
"""
# Get name of subtitle for tab title
name = os.path.basename(os.path.splitext(unicode(sub_filename))[0])
try:
subs = pysrt.open(unicode(sub_filename))
except Exception as e:
print e.message
subs = pysrt.open(unicode(sub_filename), encoding='latin')
return self.create_tab(name, subs)
def create_tab(self, name, sub_data):
"""
Create a widget and add it in tab container
:param name: name of subtitle file
:param sub_data:
:return:
"""
tab = QtGui.QWidget()
tab.setObjectName(_fromUtf8(name))
verticalLayout = QtGui.QVBoxLayout(tab)
verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
tableSubtitle = QtGui.QTableView(tab)
tableSubtitle.setObjectName(_fromUtf8("tableWidget"))
tableSubtitle.horizontalHeader().setStretchLastSection(True)
tableSubtitle.verticalHeader().setVisible(False)
tableSubtitle.horizontalHeader().setCascadingSectionResizes(True)
tableSubtitle.horizontalHeader().setDefaultSectionSize(200)
tableSubtitle.setSelectionMode(QtGui.QAbstractItemView.ContiguousSelection)
tableSubtitle.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
tableSubtitle.setStyleSheet(_fromUtf8("background-color: rgb(61, 61, 61);\n"
"selection-color: rgb(245, 245, 245);\n"
"selection-background-color: rgb(135, 167, 82);\n"
"font: 10pt Saab;\n"
"color: rgb(245, 245, 245);"))
tableSubtitle.horizontalHeader().setStyleSheet("::section{background-color: rgb(121, 121, 121);}")
tableSubtitle.horizontalHeader().setResizeMode(QtGui.QHeaderView.ResizeToContents)
verticalLayout.addWidget(tableSubtitle)
self._add_tab(tab)
# If a tab name already exist then append the name string with (0), (1), (2) ... tab (1), tab (2), tab (3)
_name = name
if _name in self.tabs:
_format = '%s ({})' % _name
index = 1
while _format.format(index) in self.tabs:
index += 1
_name = _format.format(index)
self.tabs.append(_name)
self.tab_widget.setTabText(self.tab_widget.indexOf(tab), _translate("MainWindow", _name, None))
table_model = SubtitleTableModel(sub_data, headers=['idx', 'Time codes', 'Subtitle Text'])
tableSubtitle.setModel(table_model)
return tab
def get_selected_table(self):
"""
:return:
"""
current_tab = self.tab_widget.currentWidget()
if not current_tab:
return None
return current_tab.findChildren(QtGui.QTableView)[0]
def get_tabs(self, _filter=None):
"""
Return all tabs if filter is none, otherwise return tabs whose name are in filter tuple
:param _filter:
:return:
"""
count = self.tab_widget.count()
# Filter tabs model with w.r.t. name otherwise return all tabs
if _filter:
return [self.tab_widget.widget(idx).findChildren(QtGui.QTableView)[0] for idx in xrange(0, count)
if self.tab_widget.tabText(idx) in _filter]
return [self.tab_widget.widget(idx).findChildren(QtGui.QTableView)[0] for idx in xrange(0, count)]