# @Time    : 2016/8/22 13:46
# @Author  : lixintong
import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import *

from uitester.ui.case_manager.case_editor import EditorWidget


class TableWidget(QWidget):
    def __init__(self, refresh_signal, tester, case_list, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.refresh_signal = refresh_signal
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.dataTableWidget = DataTableWidget(self.refresh_signal, tester, case_list)  # init ui table
        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.dataTableWidget)
        self.setLayout(layout)
        self.checked_cases_message = []

    def get_checked_data(self):
        del self.checked_cases_message[:]
        for row_num in range(0, self.dataTableWidget.rowCount()):
            checkbox_item = self.dataTableWidget.item(row_num, 0)
            if checkbox_item.checkState() == Qt.Checked:
                case_id = self.dataTableWidget.item(row_num, 1).text()
                self.checked_cases_message.append({"case_id": case_id, "row_num": row_num})


class DataTableWidget(QTableWidget):
    header_text_list = ['', 'id', '名称', '最后修改时间', '标识']
    column_count = len(header_text_list)

    def __init__(self, refresh_signal, tester, case_list, *args):
        super().__init__(*args)
        self.refresh_signal = refresh_signal
        self.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tester = tester
        self.setObjectName("data_table_widget")
        self.case_list = case_list
        self.setRowCount(len(self.case_list))
        self.setColumnCount(self.column_count)
        self.set_table_data()

    def item_clicked(self, item):
        if item.column() == 2:
            self.edit_case(item)

    def cell_clicked(self, row, column):
        if column == 2:
            id_item = self.item(row, 1)
            self.case_edit_window = EditorWidget(self.refresh_signal, self.tester, id_item.text())
            self.case_edit_window.show()

    def edit_case(self, item):
        id_item = self.item(item.row(), 1)
        self.case_edit_window = EditorWidget(self.refresh_signal,self.tester, id_item.text())
        self.case_edit_window.show()

    def set_checkbox_item(self, row, column):
        item = QTableWidgetItem()
        item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
        item.setCheckState(Qt.Unchecked)
        item.setTextAlignment(Qt.AlignRight)
        self.setItem(row, column, item)

    def set_table_header(self):
        for column in range(0, self.columnCount()):
            table_header_item = QTableWidgetItem(self.header_text_list[column])
            table_header_item.setFont(QFont("Roman times", 12, QFont.Bold))
            self.setHorizontalHeaderItem(column, table_header_item)
        self.horizontalHeader().setStyleSheet("QHeaderView::section{background:	#ECF5FF;}")

    def set_table_data(self):
        # self.itemClicked.connect(self.item_clicked)
        self.cellDoubleClicked.connect(self.cell_clicked)
        self.set_table_header()
        for row in range(0, self.rowCount()):
            self.set_checkbox_item(row, 0)
            case = self.case_list[row]
            self.setItem(row, 1, QTableWidgetItem(str(case.id)))
            case_name_label = QLabel()
            case_name_label.setText("<font color=#0072E3><u>{}</u></font>".format(case.name))
            self.setCellWidget(row, 2, case_name_label)
            self.setItem(row, 3, QTableWidgetItem(case.last_modify_time.strftime("%Y-%m-%d %H:%M:%S")))
            tag_names = ''
            for tag in case.tags:
                tag_names = tag_names + ',' + tag.name
            self.setItem(row, 4, QTableWidgetItem(tag_names[1:]))
        self.resizeColumnsToContents()
        self.horizontalHeader().setStretchLastSection(True)

    def label_clicked(self):
        print('label clicked')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    table_layout = TableWidget('我的')
    window = QMainWindow()
    window.resize(500, 500)
    window.setCentralWidget(table_layout)
    window.show()
    app.exec_()

@@ -252,39 +252,48 @@
  __init__(self, 
-refresh
+show_case_editor
 _signal, tester,
@@ -381,32 +381,50 @@
 elf.
-refresh_signal = refresh
+show_case_editor_signal = show_case_editor
 _sig
@@ -544,39 +544,48 @@
 ableWidget(self.
-refresh
+show_case_editor
 _signal, tester,
@@ -1395,23 +1395,32 @@
 _(self, 
-refresh
+show_case_editor
 _signal,
@@ -1496,32 +1496,50 @@
 elf.
-refresh_signal = refresh
+show_case_editor_signal = show_case_editor
 _sig
@@ -1844,106 +1844,61 @@
 a()%0A
-%0A
     
-def item_clicked(self, item):%0A        if item.column() == 2:%0A            self.edit_case(item)
+    self.cellClicked.connect(self.cell_clicked)%0A%0A
 %0A%0A  
@@ -1958,12 +1958,12 @@
 umn 
-== 2
+!= 0
 :%0A  
@@ -2009,32 +2009,37 @@
            self.
+show_
 case_edit_window
@@ -2035,326 +2035,44 @@
 edit
-_window = EditorWidget(self.refresh_signal, self.tester, id_item.text())%0A            self.case_edit_window.show()%0A%0A    def edit_case(self, item):%0A        id_item = self.item(item.row(), 1)%0A        self.case_edit_window = EditorWidget(self.refresh_signal,self.tester, id_item.text())%0A        self.case_edit_window.show(
+or_signal.emit(1,int(id_item.text())
 )%0A%0A 
@@ -2777,120 +2777,8 @@
 f):%0A
-        # self.itemClicked.connect(self.item_clicked)%0A        self.cellDoubleClicked.connect(self.cell_clicked)%0A
     
@@ -3014,183 +3014,56 @@
     
-case_name_label = QLabel()%0A            case_name_label.setText(%22%3Cfont color=#0072E3%3E%3Cu%3E%7B%7D%3C/u%3E%3C/font%3E%22.format(case.name))%0A            self.setCellWidget(row, 2, 
+self.setItem(row, 2, QTableWidgetItem(
 case
-_
+.
 name
-_label
+)
 )%0A  
@@ -3450,70 +3450,8 @@
 e)%0A%0A
-    def label_clicked(self):%0A        print('label clicked')%0A%0A%0A
 if _

fix table data style