# Path: src/plugins/util/custom_qt_items.py
# class CheckableComboBox(QtGui.QComboBox):
#     def __init__(self):
#         super(CheckableComboBox, self).__init__()
#         self.view().pressed.connect(self.handleItemPressed)
#         self.setModel(QtGui.QStandardItemModel(self))
# 
#     def handleItemPressed(self, index):
#         item = self.model().itemFromIndex(index)
#         if item.checkState() == QtCore.Qt.Checked:
#             item.setCheckState(QtCore.Qt.Unchecked)
#         else:
#             item.setCheckState(QtCore.Qt.Checked)
# 
# class PlayerDialog(QDialog):
#   def __init__(self, project, filename, parent=None, scaling=True):
#     super(PlayerDialog, self).__init__(parent)
#     self.project = project
#     self.setup_ui()
#     self.setWindowTitle(os.path.basename(filename))
# 
#     self.scale = scaling
#     self.fp = np.load(filename, mmap_mode='r')
#     if isinstance(scaling, bool) and scaling:
#         self.global_min = np.min(self.fp)
#         self.global_max = np.max(self.fp)
#     if isinstance(scaling, tuple):
#         self.global_min = scaling[0]
#         self.global_max = scaling[1]
#     self.slider.setMaximum(len(self.fp)-1)
#     self.show_frame(0)
# 
#   def show_frame(self, frame_num):
#     frame = self.fp[frame_num]
#     self.label_frame.setText(str(frame_num) + ' / ' + str(len(self.fp)-1))
#     if hasattr(self, 'global_min') and hasattr(self, 'global_max'):
#         self.view.show(frame, self.global_min, self.global_max)
#     else:
#         self.view.show(frame)
# 
#   def setup_ui(self):
#     vbox = QVBoxLayout()
#     self.view = MyGraphicsView(self.project)
#     vbox.addWidget(self.view)
#     hbox = QHBoxLayout()
#     self.slider = QSlider(Qt.Horizontal)
#     self.slider.valueChanged.connect(self.slider_moved)
#     hbox.addWidget(self.slider)
#     self.label_frame = QLabel('- / -')
#     hbox.addWidget(self.label_frame)
#     vbox.addLayout(hbox)
#     self.setLayout(vbox)
# 
#   def slider_moved(self, value):
#     self.show_frame(value)
# 
# Path: src/plugins/util/file_io.py
# def load_reference_frame(filename, offset=0):
#   if filename.endswith('.npy'):
#     frame = load_reference_frame_npy(filename, offset)
#   else:
#     raise UnknownFileFormatError()
#   return frame


import ast
import functools
import os
import pickle
import uuid
import qtutil
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from . import constants
from . import file_io
from .custom_qt_items import CheckableComboBox, PlayerDialog
from .file_io import load_reference_frame
            for y in flatten(x):
                yield y
        else:
            yield x

#todo: fix toolbutton...
def get_list_of_project_manips(project):
    vid_files = [f for f in project.files if f['type'] in constants.TOOLBUTTON_TYPES]
    list_of_manips = [x['manipulations'] for x in vid_files if x['manipulations'] != []]
    list_of_manips = [ast.literal_eval(l) for l in list_of_manips]
    list_of_manips = list(flatten(list_of_manips))
    return list(set(list_of_manips))


def selected_video_changed_multi(widget, selected, deselected):
    if not widget.video_list.selectedIndexes() or not widget.video_list.currentIndex().data(Qt.DisplayRole):
        return
    # for index in deselected.indexes():
    #     vidpath = str(os.path.join(widget.project.path,
    #                                index.data(Qt.DisplayRole))
    #                   + '.npy')
    #     widget.selected_videos = [x for x in widget.selected_videos if x != vidpath]
    widget.selected_videos = []
    for index in widget.video_list.selectedIndexes():
        vidpath = str(os.path.normpath(os.path.join(widget.project.path, index.data(Qt.DisplayRole)) + '.npy'))
        if vidpath not in widget.selected_videos and vidpath != 'None':
            widget.selected_videos = widget.selected_videos + [vidpath]
            widget.shown_video_path = str(os.path.normpath(os.path.join(widget.project.path,
                                                       widget.video_list.currentIndex().data(Qt.DisplayRole))
                                    + '.npy'))

# Path: src/plugins/util/custom_qt_items.py
# class CheckableComboBox(QtGui.QComboBox):
#     def __init__(self):
#         super(CheckableComboBox, self).__init__()
#         self.view().pressed.connect(self.handleItemPressed)
#         self.setModel(QtGui.QStandardItemModel(self))
# 
#     def handleItemPressed(self, index):
#         item = self.model().itemFromIndex(index)
#         if item.checkState() == QtCore.Qt.Checked:
#             item.setCheckState(QtCore.Qt.Unchecked)
#         else:
#             item.setCheckState(QtCore.Qt.Checked)
# 
# class PlayerDialog(QDialog):
#   def __init__(self, project, filename, parent=None, scaling=True):
#     super(PlayerDialog, self).__init__(parent)
#     self.project = project
#     self.setup_ui()
#     self.setWindowTitle(os.path.basename(filename))
# 
#     self.scale = scaling
#     self.fp = np.load(filename, mmap_mode='r')
#     if isinstance(scaling, bool) and scaling:
#         self.global_min = np.min(self.fp)
#         self.global_max = np.max(self.fp)
#     if isinstance(scaling, tuple):
#         self.global_min = scaling[0]
#         self.global_max = scaling[1]
#     self.slider.setMaximum(len(self.fp)-1)
#     self.show_frame(0)
# 
#   def show_frame(self, frame_num):
#     frame = self.fp[frame_num]
#     self.label_frame.setText(str(frame_num) + ' / ' + str(len(self.fp)-1))
#     if hasattr(self, 'global_min') and hasattr(self, 'global_max'):
#         self.view.show(frame, self.global_min, self.global_max)
#     else:
#         self.view.show(frame)
# 
#   def setup_ui(self):
#     vbox = QVBoxLayout()
#     self.view = MyGraphicsView(self.project)
#     vbox.addWidget(self.view)
#     hbox = QHBoxLayout()
#     self.slider = QSlider(Qt.Horizontal)
#     self.slider.valueChanged.connect(self.slider_moved)
#     hbox.addWidget(self.slider)
#     self.label_frame = QLabel('- / -')
#     hbox.addWidget(self.label_frame)
#     vbox.addLayout(hbox)
#     self.setLayout(vbox)
# 
#   def slider_moved(self, value):
#     self.show_frame(value)
# 
# Path: src/plugins/util/file_io.py
# def load_reference_frame(filename, offset=0):
#   if filename.endswith('.npy'):
#     frame = load_reference_frame_npy(filename, offset)
#   else:
#     raise UnknownFileFormatError()
#   return frame


# Path: src/plugins/util/project_functions.py
import ast
import functools
import os
import pickle
import uuid
import qtutil
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from . import constants
from . import file_io
from .custom_qt_items import CheckableComboBox, PlayerDialog
from .file_io import load_reference_frame

            for y in flatten(x):
                yield y
        else:
            yield x

#todo: fix toolbutton...
def get_list_of_project_manips(project):
    vid_files = [f for f in project.files if f['type'] in constants.TOOLBUTTON_TYPES]
    list_of_manips = [x['manipulations'] for x in vid_files if x['manipulations'] != []]
    list_of_manips = [ast.literal_eval(l) for l in list_of_manips]
    list_of_manips = list(flatten(list_of_manips))
    return list(set(list_of_manips))


def selected_video_changed_multi(widget, selected, deselected):
    if not widget.video_list.selectedIndexes() or not widget.video_list.currentIndex().data(Qt.DisplayRole):
        return
    # for index in deselected.indexes():
    #     vidpath = str(os.path.join(widget.project.path,
    #                                index.data(Qt.DisplayRole))
    #                   + '.npy')
    #     widget.selected_videos = [x for x in widget.selected_videos if x != vidpath]
    widget.selected_videos = []
    for index in widget.video_list.selectedIndexes():
        vidpath = str(os.path.normpath(os.path.join(widget.project.path, index.data(Qt.DisplayRole)) + '.npy'))
        if vidpath not in widget.selected_videos and vidpath != 'None':
            widget.selected_videos = widget.selected_videos + [vidpath]
            widget.shown_video_path = str(os.path.normpath(os.path.join(widget.project.path,
                                                       widget.video_list.currentIndex().data(Qt.DisplayRole))
                                    + '.npy'))

    frame = load_reference_frame(widget.shown_video_path)