# Path: dunyadesktop_app/widgets/table.py
# class TableView(QTableView):
#     open_dunya_triggered = pyqtSignal(object)
#     add_to_collection = pyqtSignal(str, object)
# 
#     def __init__(self, parent=None):
#         QTableView.__init__(self, parent=parent)
# 
#         # setting the table for no edit and row selection
#         self.setEditTriggers(QAbstractItemView.NoEditTriggers)
#         self.setSelectionBehavior(QAbstractItemView.SelectRows)
#         # self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
#         self.setMouseTracking(True)
#         self.horizontalHeader().setStretchLastSection(True)
#         font = QFont()
#         font.setPointSize(13)
#         self.horizontalHeader().setFont(font)
# 
#         # hiding the vertical headers
#         self.verticalHeader().hide()
# 
#         # arranging the artist column for being multi-line
#         self.setWordWrap(True)
#         self.setTextElideMode(Qt.ElideMiddle)
# 
#         self._last_index = QPersistentModelIndex()
#         self.viewport().installEventFilter(self)
# 
#         self._set_font()
# 
#     def _set_font(self):
#         """Sets the font of the table"""
#         font = QFont()
#         font.setPointSize(FONT_SIZE)
#         self.setFont(font)
# 
#     def contextMenuEvent(self, event):
#         """Pops up the context menu when the right button is clicked."""
#         try:
#             index = self._get_current_index
#             menu = RCMenu(self)
#             menu.popup(QCursor.pos())
# 
#             self.selected_indexes = menu.return_selected_row_indexes()
#             menu.overall_hist_action.triggered.connect(
#                 self._compute_overall_histograms)
#         except UnboundLocalError:
#             pass
# 
#     @property
#     def _get_current_index(self):
#         """
#         Returns the index of clicked item.
# 
#         :return: (int) Index of clicked item.
#         """
#         if self.selectionModel().selection().indexes():
#             for index in self.selectionModel().selection().indexes():
#                 row, column = index.row(), index.column()
#         self.index = index
#         return index
# 
#     def _compute_overall_histograms(self):
#         """
#         Computes the overall histograms of selected items in a table.
#         """
#         coll_widget =  self.parent().parent().listView_collections
#         coll = str(coll_widget.currentItem().text())
# 
#         conn, c = database.connect()
#         histograms = {}
#         for row in self.selected_indexes:
#             mbid = str(database.get_nth_row(c, coll, row)[0])
#             pd_path = os.path.join(DOCS_PATH, mbid,
#                                    'audioanalysis--pitch_distribution.json')
#             tnc_path = os.path.join(DOCS_PATH, mbid,
#                                    'audioanalysis--tonic.json')
#             vals, bins = load_pd(pd_path)
#             tonic = load_tonic(tnc_path)
#             histograms[mbid] = [[vals, bins], tonic]
#         corpusbasestatistics.compute_overall_histogram(histograms)
# 
#     def send_rec(self):
#         """Emits 'open_dunya_triggered' signal and the current index to open
#         the player"""
#         self.index = self._get_current_index
#         if self.index:
#             self.open_dunya_triggered.emit(self.index)
#             self.index = None
# 
#     def get_selected_rows(self):
#         """Returns the current selected rows in the table."""
#         selected_rows = []
#         for item in self.selectionModel().selectedRows():
#             if item.row() not in selected_rows:
#                 selected_rows.append(item)
#         return selected_rows
# 
#     def send_to_db(self, coll):
#         """Emits 'add_to_collection' signal to add the selected rows to the
#         database"""
#         if self.index:
#             self.add_to_collection.emit(coll, self.index)
#             self.index = None
# 
# Path: dunyadesktop_app/widgets/models/filteringmodel.py
# class FilteringModel(QStandardItemModel):
#     """This model is contains the attributes"""
#     def __init__(self, parent=None):
#         QStandardItemModel.__init__(self, parent)
# 
#     def add_items(self, attribute):
#         self.setRowCount(len(attribute))
# 
#         for row, item in enumerate(attribute):
#             name = QStandardItem(item['name'])
#             self.setItem(row, 0, name)
# 
# Path: dunyadesktop_app/widgets/models/proxymodel.py
# class SortFilterProxyModel(QSortFilterProxyModel):
#     """Sort filter model is for filtering the table of query results."""
#     def __init__(self, QObject_parent=None):
#         QSortFilterProxyModel.__init__(self, QObject_parent)
# 
#     def filter_table(self, text):
#         reg_exp = QRegExp(text, Qt.CaseInsensitive)
#         self.setFilterRegExp(reg_exp)


from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLineEdit, QDialogButtonBox
from PyQt5.QtCore import pyqtSignal, Qt
from .table import TableView
from .models.filteringmodel import FilteringModel
from .models.proxymodel import SortFilterProxyModel



class FilteringDialog(QDialog):
    """The dialog which pops up when the user clicks the combobox"""
    ok_button_clicked = pyqtSignal()

    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.attribute = None
        self.setFixedSize(200, 300)

        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Popup)

        v_layout = QVBoxLayout(self)
        self.filtering_edit = QLineEdit()
        self.table_attribute = TableView()

        self.button_box = QDialogButtonBox(self)
        self.button_box.addButton('OK', QDialogButtonBox.AcceptRole)
        self.button_box.addButton('Cancel', QDialogButtonBox.RejectRole)

        v_layout.addWidget(self.filtering_edit)
        v_layout.addWidget(self.table_attribute)
        v_layout.addWidget(self.button_box)

        self.setLayout(v_layout)

        self.filtering_model = FilteringModel(self)


# Path: dunyadesktop_app/widgets/table.py
# class TableView(QTableView):
#     open_dunya_triggered = pyqtSignal(object)
#     add_to_collection = pyqtSignal(str, object)
# 
#     def __init__(self, parent=None):
#         QTableView.__init__(self, parent=parent)
# 
#         # setting the table for no edit and row selection
#         self.setEditTriggers(QAbstractItemView.NoEditTriggers)
#         self.setSelectionBehavior(QAbstractItemView.SelectRows)
#         # self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
#         self.setMouseTracking(True)
#         self.horizontalHeader().setStretchLastSection(True)
#         font = QFont()
#         font.setPointSize(13)
#         self.horizontalHeader().setFont(font)
# 
#         # hiding the vertical headers
#         self.verticalHeader().hide()
# 
#         # arranging the artist column for being multi-line
#         self.setWordWrap(True)
#         self.setTextElideMode(Qt.ElideMiddle)
# 
#         self._last_index = QPersistentModelIndex()
#         self.viewport().installEventFilter(self)
# 
#         self._set_font()
# 
#     def _set_font(self):
#         """Sets the font of the table"""
#         font = QFont()
#         font.setPointSize(FONT_SIZE)
#         self.setFont(font)
# 
#     def contextMenuEvent(self, event):
#         """Pops up the context menu when the right button is clicked."""
#         try:
#             index = self._get_current_index
#             menu = RCMenu(self)
#             menu.popup(QCursor.pos())
# 
#             self.selected_indexes = menu.return_selected_row_indexes()
#             menu.overall_hist_action.triggered.connect(
#                 self._compute_overall_histograms)
#         except UnboundLocalError:
#             pass
# 
#     @property
#     def _get_current_index(self):
#         """
#         Returns the index of clicked item.
# 
#         :return: (int) Index of clicked item.
#         """
#         if self.selectionModel().selection().indexes():
#             for index in self.selectionModel().selection().indexes():
#                 row, column = index.row(), index.column()
#         self.index = index
#         return index
# 
#     def _compute_overall_histograms(self):
#         """
#         Computes the overall histograms of selected items in a table.
#         """
#         coll_widget =  self.parent().parent().listView_collections
#         coll = str(coll_widget.currentItem().text())
# 
#         conn, c = database.connect()
#         histograms = {}
#         for row in self.selected_indexes:
#             mbid = str(database.get_nth_row(c, coll, row)[0])
#             pd_path = os.path.join(DOCS_PATH, mbid,
#                                    'audioanalysis--pitch_distribution.json')
#             tnc_path = os.path.join(DOCS_PATH, mbid,
#                                    'audioanalysis--tonic.json')
#             vals, bins = load_pd(pd_path)
#             tonic = load_tonic(tnc_path)
#             histograms[mbid] = [[vals, bins], tonic]
#         corpusbasestatistics.compute_overall_histogram(histograms)
# 
#     def send_rec(self):
#         """Emits 'open_dunya_triggered' signal and the current index to open
#         the player"""
#         self.index = self._get_current_index
#         if self.index:
#             self.open_dunya_triggered.emit(self.index)
#             self.index = None
# 
#     def get_selected_rows(self):
#         """Returns the current selected rows in the table."""
#         selected_rows = []
#         for item in self.selectionModel().selectedRows():
#             if item.row() not in selected_rows:
#                 selected_rows.append(item)
#         return selected_rows
# 
#     def send_to_db(self, coll):
#         """Emits 'add_to_collection' signal to add the selected rows to the
#         database"""
#         if self.index:
#             self.add_to_collection.emit(coll, self.index)
#             self.index = None
# 
# Path: dunyadesktop_app/widgets/models/filteringmodel.py
# class FilteringModel(QStandardItemModel):
#     """This model is contains the attributes"""
#     def __init__(self, parent=None):
#         QStandardItemModel.__init__(self, parent)
# 
#     def add_items(self, attribute):
#         self.setRowCount(len(attribute))
# 
#         for row, item in enumerate(attribute):
#             name = QStandardItem(item['name'])
#             self.setItem(row, 0, name)
# 
# Path: dunyadesktop_app/widgets/models/proxymodel.py
# class SortFilterProxyModel(QSortFilterProxyModel):
#     """Sort filter model is for filtering the table of query results."""
#     def __init__(self, QObject_parent=None):
#         QSortFilterProxyModel.__init__(self, QObject_parent)
# 
#     def filter_table(self, text):
#         reg_exp = QRegExp(text, Qt.CaseInsensitive)
#         self.setFilterRegExp(reg_exp)


# Path: dunyadesktop_app/widgets/filteringdialog.py
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLineEdit, QDialogButtonBox
from PyQt5.QtCore import pyqtSignal, Qt
from .table import TableView
from .models.filteringmodel import FilteringModel
from .models.proxymodel import SortFilterProxyModel




class FilteringDialog(QDialog):
    """The dialog which pops up when the user clicks the combobox"""
    ok_button_clicked = pyqtSignal()

    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.attribute = None
        self.setFixedSize(200, 300)

        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Popup)

        v_layout = QVBoxLayout(self)
        self.filtering_edit = QLineEdit()
        self.table_attribute = TableView()

        self.button_box = QDialogButtonBox(self)
        self.button_box.addButton('OK', QDialogButtonBox.AcceptRole)
        self.button_box.addButton('Cancel', QDialogButtonBox.RejectRole)

        v_layout.addWidget(self.filtering_edit)
        v_layout.addWidget(self.table_attribute)
        v_layout.addWidget(self.button_box)

        self.setLayout(v_layout)

        self.filtering_model = FilteringModel(self)


        self.proxy_model = SortFilterProxyModel(self)