Ejemplo n.º 1
0
class MainPanel(wx.Panel):
    """Main Panel of the application"""

    # ----------------------------------------------------------------------
    def __init__(self, parent, theme):
        """
        Constructor
        Defines the ObjectListView for dropped items
        and the layout for its components
        """
        wx.Panel.__init__(self, parent=parent)
        self.videos_list = []

        self.primary_background_color = theme.get("primaryBackgroundColor")
        self.secondary_background_color = theme.get("secondaryBackgroundColor")
        self.foreground_color = theme.get("foregroundColor")
        self.list_font = wx.Font(pointSize=11,
                                 family=wx.FONTFAMILY_SWISS,
                                 style=wx.FONTSTYLE_NORMAL,
                                 weight=wx.FONTWEIGHT_LIGHT,
                                 underline=False,
                                 faceName='Roboto')
        self.regular_font = wx.Font(pointSize=12,
                                    family=wx.FONTFAMILY_SWISS,
                                    style=wx.FONTSTYLE_NORMAL,
                                    weight=wx.FONTWEIGHT_NORMAL,
                                    underline=False,
                                    faceName='Roboto')
        self.big_font = wx.Font(pointSize=32,
                                family=wx.FONTFAMILY_SWISS,
                                style=wx.FONTSTYLE_NORMAL,
                                weight=wx.FONTWEIGHT_BOLD,
                                underline=False,
                                faceName='Roboto')

        self.SetBackgroundColour(self.primary_background_color)
        self.SetForegroundColour(self.foreground_color)

        video_drop_target = VideoDropTarget(self)
        self.olv = FastObjectListView(self,
                                      style=wx.LC_REPORT | wx.BORDER_NONE)
        self.olv.SetEmptyListMsg(i18n.t('i18n.emptyList'))
        self.olv.SetDropTarget(video_drop_target)
        self.olv.cellEditMode = FastObjectListView.CELLEDIT_NONE
        self.olv.SetEmptyListMsgFont(self.big_font)
        self.olv.SetFont(self.list_font)
        self.set_files()

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.olv.Bind(wx.EVT_KEY_UP, self.on_delete)
        self.sizer.Add(self.olv, 1, wx.EXPAND)

        grid1 = wx.GridSizer(1, 3, 1, 10)
        grid2 = wx.GridSizer(1, 3, 1, 10)

        self.lbl_cut_init = wx.StaticText(self, -1,
                                          i18n.t('i18n.lblCutInitSec'))
        self.lbl_cut_init.SetFont(self.regular_font)

        self.txt_cut_init = wx.TextCtrl(self,
                                        -1,
                                        "0",
                                        size=(175, -1),
                                        style=wx.BORDER_STATIC)
        self.txt_cut_init.SetBackgroundColour(self.secondary_background_color)
        self.txt_cut_init.SetForegroundColour(self.foreground_color)
        self.txt_cut_init.SetFont(self.regular_font)

        self.lbl_cut_end = wx.StaticText(self, -1, i18n.t('i18n.lblCutEndSec'))
        self.lbl_cut_end.SetFont(self.regular_font)

        self.txt_cut_end = wx.TextCtrl(self,
                                       -1,
                                       "0",
                                       size=(175, -1),
                                       style=wx.BORDER_STATIC)
        self.txt_cut_end.SetBackgroundColour(self.secondary_background_color)
        self.txt_cut_end.SetForegroundColour(self.foreground_color)
        self.txt_cut_end.SetFont(self.regular_font)

        self.chk_delete = wx.CheckBox(self, -1, i18n.t('i18n.chkDelete'))
        self.chk_delete.SetFont(self.regular_font)

        grid1.AddMany([self.lbl_cut_init, self.lbl_cut_end])
        grid1.AddSpacer(0)
        grid2.AddMany([self.txt_cut_init, self.txt_cut_end, self.chk_delete])

        self.btn_cut = wx.Button(self,
                                 -1,
                                 i18n.t('i18n.btnCut'),
                                 size=(100, 50),
                                 style=wx.BORDER_NONE)
        self.btn_cut.Bind(wx.EVT_BUTTON, self.on_cut)
        self.btn_cut.SetBackgroundColour(self.secondary_background_color)
        self.btn_cut.SetForegroundColour(self.foreground_color)
        self.btn_cut.SetFont(self.big_font)

        self.sizer.AddSpacer(20)
        self.sizer.Add(grid1, 0, wx.EXPAND | wx.LEFT | wx.BOTTOM, 10)
        self.sizer.Add(grid2, 0, wx.EXPAND | wx.LEFT, 10)
        self.sizer.AddSpacer(20)
        self.sizer.Add(self.btn_cut, 0, wx.EXPAND)
        self.SetSizer(self.sizer)

    # ----------------------------------------------------------------------
    def update_display(self, videos_list):
        """
        Triggered when dropped items,
        get the item and transform it into VideoInfo with parameters
        to use it in ObjectListView
        """
        import nmi_video
        for path in videos_list:
            file_stats = os.stat(path)
            try:
                clip_duration = nmi_video.get_video_duration(path)
            except Exception as e:
                dialog = wx.RichMessageDialog(self,
                                              i18n.t('i18n.onlyVideosError'),
                                              "Error", wx.ICON_ERROR)
                dialog.ShowDetailedText("Error info:\n" + "\\" + str(e) + "\\")
                dialog.ShowModal()
                return
            minutes_lenght = str(datetime.timedelta(seconds=clip_duration))
            file_size = file_stats[stat.ST_SIZE]
            if (file_size > 1024):
                file_size = file_size / 1024.0
                if (file_size > 1024.0):
                    file_size = file_size / 1024.0
                    file_size = "%.2f MB" % file_size
                else:
                    file_size = "%.2f KB" % file_size

            self.videos_list.append(
                nmi_video.VideoInfo(path, minutes_lenght, file_size))

        self.olv.SetObjects(self.videos_list)

    # ----------------------------------------------------------------------
    def set_files(self):
        """Set the columns for items dropped"""
        self.olv.SetColumns([
            ColumnDefn(i18n.t('i18n.colName'),
                       "left",
                       250,
                       "name",
                       isSpaceFilling=True),
            ColumnDefn(i18n.t('i18n.colMinutes'), "right", 150,
                       "minutes_lenght"),
            ColumnDefn(i18n.t('i18n.colSize'), "right", 150, "size")
        ])
        self.olv.SetObjects(self.videos_list)

    # ----------------------------------------------------------------------
    def on_cut(self, event):
        """
        Trigger when click on CUT Button,
        Get parameters and in a for loop cut videos calling the function
        """
        import nmi_video
        try:
            beginning = int(self.txt_cut_init.GetLineText(1))
            end = int(self.txt_cut_end.GetLineText(1))
            delete = self.chk_delete.GetValue()
            delete_videos_list = list()
        except Exception as e:
            dialog = wx.RichMessageDialog(self, i18n.t('i18n.onlyNumberError'),
                                          "Error", wx.ICON_ERROR)
            dialog.ShowDetailedText("Error info:\n" + "\\" + str(e) + "\\")
            dialog.ShowModal()
            return

        if (len(self.videos_list) > 0):
            c = 0
            list_count = len(self.videos_list)
            dialog = wx.ProgressDialog(
                i18n.t('i18n.cutProgressTitle'),
                i18n.t('i18n.cutProgress') + str(c) + '/' + str(list_count),
                maximum=list_count,
                style=wx.PD_SMOOTH | wx.PD_CAN_ABORT | wx.PD_AUTO_HIDE)
            for file in self.videos_list:
                cut = nmi_video.cut_video(beginning, end, file)
                if (cut.get('state') is True):
                    delete_videos_list.append(file)
                    c += 1
                    if (dialog.WasCancelled() is True):
                        dialog = wx.RichMessageDialog(self,
                                                      i18n.t('i18n.cutCancel'),
                                                      "Error", wx.ICON_ERROR)
                        dialog.ShowModal()
                        break
                    dialog.Update(
                        c,
                        i18n.t('i18n.cutProgress') + str(c + 1) + '/' +
                        str(list_count))
            for file in delete_videos_list:
                self.videos_list.remove(file)
            self.olv.RemoveObjects(delete_videos_list)
            self.olv.SetObjects(self.videos_list)
            wx.MessageBox(
                str(c) + i18n.t('i18n.cutVideos'), "OK", wx.ICON_INFORMATION)
        else:
            dialog = wx.RichMessageDialog(self,
                                          i18n.t('i18n.noCutVideosError'),
                                          "Error", wx.ICON_ERROR)
            dialog.ShowModal()
            return

        if (delete is True):
            try:
                nmi_video.delete_videos(delete_videos_list)
                wx.MessageBox(i18n.t('i18n.deletedOld'), "OK",
                              wx.ICON_INFORMATION)
            except Exception as e:
                dialog = wx.RichMessageDialog(self,
                                              i18n.t('i18n.permisionError'),
                                              "Error", wx.ICON_ERROR)
                dialog.ShowDetailedText("Error info:\n" + "\\" + str(e) + "\\")
                dialog.ShowModal()

    # ----------------------------------------------------------------------
    def on_delete(self, event):
        """Trigger the event when user delete item from list with SUPR"""
        if (event.GetUnicodeKey() == 127
                and self.olv.GetSelectedItemCount() > 0):
            for i in self.olv.GetSelectedObjects():
                self.videos_list.remove(i)
            self.olv.RemoveObjects(self.olv.GetSelectedObjects())
            self.olv.SetObjects(self.videos_list)