from PyQt5 import QtWidgets, uic

from Factory import Factory
from Dialogs.DialogMacros import turn_into_free_point, free_point_checkbox
from Fill.ListWidget import fill_listWidget_with_data, set_selected_id_in_listWidget
import Constant as c


class RegularPolygonDialog(QtWidgets.QDialog):
    def __init__(self, scene, data):
        """Construct RegularPolygonDialog."""
        super(RegularPolygonDialog, self).__init__()
        self.ui = uic.loadUi('regularpolygon.ui', self)
        self.scene = scene
        self.sides = 3
        self.free_point = False
        self.data = data
        self.ui.buttonBox.accepted.connect(self.accepted)
        self.ui.buttonBox.rejected.connect(self.rejected)

        self.ui.sides_slider.valueChanged.connect(self.hslider_sides_func)

        self.ui.checkBox.stateChanged.connect(lambda x: free_point_checkbox(self, x))

    def hslider_sides_func(self, value):
        """Be slider callback function to set sides."""
        self.sides = value
        self.ui.sides_spin.setValue(value)

    def accepted(self):
        """Create new regular polygon with settings."""
        A, B = self.data
        angle = -(self.sides - 2) * 180 / self.sides
        polygon = [A, B]

        for _ in range(self.sides - 2):
            item = Factory.create_empty_item('point', c.Point.Definition.ROTATION)
            definition = {'A': A, 'B': B, 'angle': angle}
            id_ = Factory.next_id(item, definition, self.scene.project_data.items)
            item.item["id"] = id_
            item.item["definition"] = definition
            if self.free_point:
                item = turn_into_free_point(item, self.scene)
            self.scene.project_data.add(item)
            A = B
            B = item.item["id"]
            polygon.append(item.item["id"])

        item = Factory.create_empty_item('polygon', None)
        definition = polygon
        item.item["id"] = Factory.next_id(item, definition, self.scene.project_data.items)
        item.item["definition"] = definition
        self.scene.project_data.add(item)

        self.scene.project_data.recompute_canvas(*self.scene.init_canvas_dims)
        current_row_old = self.scene.ui.listWidget.currentRow()
        fill_listWidget_with_data(self.scene.project_data, self.scene.ui.listWidget, self.scene.current_tab_idx)
        set_selected_id_in_listWidget(self.scene, current_row_old)
        self.scene.edit.add_undo_item(self.scene)

    def rejected(self):
        """Add no new regular polygon."""
        pass

[((13, 18, 13, 55), 'PyQt5.uic.loadUi', 'uic.loadUi', ({(13, 29, 13, 48): '"""regularpolygon.ui"""', (13, 50, 13, 54): 'self'}, {}), "('regularpolygon.ui', self)", False, 'from PyQt5 import QtWidgets, uic\n'), ((49, 15, 49, 57), 'Factory.Factory.create_empty_item', 'Factory.create_empty_item', ({(49, 41, 49, 50): '"""polygon"""', (49, 52, 49, 56): 'None'}, {}), "('polygon', None)", False, 'from Factory import Factory\n'), ((51, 26, 51, 90), 'Factory.Factory.next_id', 'Factory.next_id', ({(51, 42, 51, 46): 'item', (51, 48, 51, 58): 'definition', (51, 60, 51, 89): 'self.scene.project_data.items'}, {}), '(item, definition, self.scene.project_data.items)', False, 'from Factory import Factory\n'), ((57, 8, 57, 112), 'Fill.ListWidget.fill_listWidget_with_data', 'fill_listWidget_with_data', ({(57, 34, 57, 57): 'self.scene.project_data', (57, 59, 57, 83): 'self.scene.ui.listWidget', (57, 85, 57, 111): 'self.scene.current_tab_idx'}, {}), '(self.scene.project_data, self.scene.ui.listWidget,\n    self.scene.current_tab_idx)', False, 'from Fill.ListWidget import fill_listWidget_with_data, set_selected_id_in_listWidget\n'), ((58, 8, 58, 66), 'Fill.ListWidget.set_selected_id_in_listWidget', 'set_selected_id_in_listWidget', ({(58, 38, 58, 48): 'self.scene', (58, 50, 58, 65): 'current_row_old'}, {}), '(self.scene, current_row_old)', False, 'from Fill.ListWidget import fill_listWidget_with_data, set_selected_id_in_listWidget\n'), ((37, 19, 37, 82), 'Factory.Factory.create_empty_item', 'Factory.create_empty_item', ({(37, 45, 37, 52): '"""point"""', (37, 54, 37, 81): 'c.Point.Definition.ROTATION'}, {}), "('point', c.Point.Definition.ROTATION)", False, 'from Factory import Factory\n'), ((39, 18, 39, 82), 'Factory.Factory.next_id', 'Factory.next_id', ({(39, 34, 39, 38): 'item', (39, 40, 39, 50): 'definition', (39, 52, 39, 81): 'self.scene.project_data.items'}, {}), '(item, definition, self.scene.project_data.items)', False, 'from Factory import Factory\n'), ((23, 56, 23, 84), 'Dialogs.DialogMacros.free_point_checkbox', 'free_point_checkbox', ({(23, 76, 23, 80): 'self', (23, 82, 23, 83): 'x'}, {}), '(self, x)', False, 'from Dialogs.DialogMacros import turn_into_free_point, free_point_checkbox\n'), ((43, 23, 43, 61), 'Dialogs.DialogMacros.turn_into_free_point', 'turn_into_free_point', ({(43, 44, 43, 48): 'item', (43, 50, 43, 60): 'self.scene'}, {}), '(item, self.scene)', False, 'from Dialogs.DialogMacros import turn_into_free_point, free_point_checkbox\n')]