# -*- coding: utf-8 -*-
from PyQt5.QtCore import (
    QRectF,
    Qt
)
from PyQt5.QtGui import (
    QBrush,
    QFont,
    QColor,
    QPainter
)
from PyQt5.QtWidgets import (
    QGraphicsItem,
    QColorDialog,
    QGraphicsTextItem,
    QGraphicsSceneMouseEvent,
    QWidget,
    QStyleOptionGraphicsItem
)

from cadnano.gui.palette import getColorObj
from . import pathstyles as styles


_FONT = QFont(styles.THE_FONT, 12, QFont.Bold)


class ColorPanel(QGraphicsItem):
    """
    Attributes:
        colordialog (QColorDialog): Description
        rect (QRectF): Description
    """
    _shift_colors = [getColorObj(x) for x in styles.SCAF_COLORS]
    _colors = [getColorObj(x) for x in styles.STAP_COLORS]
    _PEN = Qt.NoPen

    def __init__(self, parent: QGraphicsItem = None):
        """
        Args:
            parent: Default is ``None``
        """
        super(ColorPanel, self).__init__(parent)
        self.rect = QRectF(0, 0, 30, 30)
        self.setFlag(QGraphicsItem.ItemIgnoresTransformations)
        # print("A bug in Qt is about to cause a warning 'Layout still needs update after calling...'")
        # print("Please vote to fix here: https://bugreports.qt.io/browse/QTBUG-58699")
        self.colordialog = QColorDialog()
        self._shift_color_index = -1  # init on -1, painttool will cycle to 0
        self._color_index = -1  # init on -1, painttool will cycle to 0
        self._shift_color = self._shift_colors[self._shift_color_index]
        self._color = self._colors[self._color_index]
        self._shift_brush = QBrush(self._shift_color)
        self._brush = QBrush(self._color)
        self._initLabel()
        self.hide()

    def _initLabel(self):
        """
        """
        self._label = label = QGraphicsTextItem("⇧", parent=self)
        label.setPos(28, -4)
        label.setFont(_FONT)

    def boundingRect(self) -> QRectF:
        """
        Returns:
            :class:`QRectF`
        """
        return self.rect

    def paint(self, painter: QPainter,
                    option: QStyleOptionGraphicsItem,
                    widget: QWidget):
        """
        Args:
            painter: Description
            option: Description
            widget: Description
        """
        painter.setPen(self._PEN)
        painter.setBrush(self._shift_brush)
        painter.drawRect(0, 0, 30, 15)
        painter.setBrush(self._brush)
        painter.drawRect(0, 15, 30, 15)

    def nextColor(self):
        """Increment ``self._color_index``
        """
        self._color_index += 1
        if self._color_index == len(self._colors):
            self._color_index = 0
        self._color = self._colors[self._color_index]
        self._brush.setColor(self._color)
        self.update()

    def prevColor(self):
        """Decrement ``self._color_index``
        """
        self._color_index -= 1

    def color(self) -> QColor:
        """
        Returns:
            :class:`QColor`
        """
        return self._color

    def shiftColorName(self) -> str:
        """
        Returns:
            string name of the color
        """
        return self._shift_color.name()

    def colorName(self) -> str:
        """
        Returns:
            string name of the color
        """
        return self._color.name()

    def mousePressEvent(self, event: QGraphicsSceneMouseEvent):
        """
        Args:
            event: Description
        """
        if event.pos().y() < 10:
            new_color = self.colordialog.getColor(self._shift_color)
            if new_color.isValid() and new_color.name() != self._shift_color.name():
                self._shift_color = new_color
                self._shift_brush = QBrush(new_color)
                if new_color not in self._shift_colors:
                    self._shift_colors.insert(self._shift_color_index, new_color)
                self.update()
        else:
            new_color = self.colordialog.getColor(self._color)
            if new_color.isValid() and new_color.name() != self._color.name():
                self._color = new_color
                self._brush = QBrush(new_color)
                if new_color not in self._colors:
                    self._colors.insert(self._color_index, new_color)
                self.update()

# -*- coding: utf-8 -*-
from PyQt5.QtCore import (
    QRectF,
    Qt
)
from PyQt5.QtGui import (
    QBrush,
    QFont,
    QColor,
    QPainter
)
from PyQt5.QtWidgets import (
    QGraphicsItem,
    QColorDialog,
    QGraphicsTextItem,
    QGraphicsSceneMouseEvent,
    QWidget,
    QStyleOptionGraphicsItem
)

from cadnano.gui.palette import getColorObj
from . import pathstyles as styles


_FONT = QFont(styles.THE_FONT, 12, QFont.Bold)


class ColorPanel(QGraphicsItem):
    """
    Attributes:
        colordialog (QColorDialog): Description
        rect (QRectF): Description
    """
    _shift_colors = [getColorObj(x) for x in styles.SCAF_COLORS]
    _colors = [getColorObj(x) for x in styles.STAP_COLORS]
    _PEN = Qt.NoPen

    def __init__(self, parent: QGraphicsItem = None):
        """
        Args:
            parent: Default is ``None``
        """
        super(ColorPanel, self).__init__(parent)
        self.rect = QRectF(0, 0, 30, 30)
        self.setFlag(QGraphicsItem.ItemIgnoresTransformations)
        # print("A bug in Qt is about to cause a warning 'Layout still needs update after calling...'")
        # print("Please vote to fix here: https://bugreports.qt.io/browse/QTBUG-58699")
        self.colordialog = QColorDialog()
        self._shift_color_index = -1  # init on -1, painttool will cycle to 0
        self._color_index = -1  # init on -1, painttool will cycle to 0
        self._shift_color = self._shift_colors[self._shift_color_index]
        self._color = self._colors[self._color_index]
        self._shift_brush = QBrush(self._shift_color)
        self._brush = QBrush(self._color)
        self._initLabel()
        self.hide()

    def _initLabel(self):
        """
        """
        self._label = label = QGraphicsTextItem("⇧", parent=self)
        label.setPos(28, -4)
        label.setFont(_FONT)

    def boundingRect(self) -> QRectF:
        """
        Returns:
            :class:`QRectF`
        """
        return self.rect

    def paint(self, painter: QPainter,
                    option: QStyleOptionGraphicsItem,
                    widget: QWidget):
        """
        Args:
            painter: Description
            option: Description
            widget: Description
        """
        painter.setPen(self._PEN)
        painter.setBrush(self._shift_brush)
        painter.drawRect(0, 0, 30, 15)
        painter.setBrush(self._brush)
        painter.drawRect(0, 15, 30, 15)

    def nextColor(self):
        """Increment ``self._color_index``
        """
        self._color_index += 1
        if self._color_index == len(self._colors):
            self._color_index = 0
        self._color = self._colors[self._color_index]
        self._brush.setColor(self._color)
        self.update()

    def prevColor(self):
        """Decrement ``self._color_index``
        """
        self._color_index -= 1

    def color(self) -> QColor:
        """
        Returns:
            :class:`QColor`
        """
        return self._color

    def shiftColorName(self) -> str:
        """
        Returns:
            string name of the color
        """
        return self._shift_color.name()

    def colorName(self) -> str:
        """
        Returns:
            string name of the color
        """
        return self._color.name()

    def mousePressEvent(self, event: QGraphicsSceneMouseEvent):
        """
        Args:
            event: Description
        """
        if event.pos().y() < 10:
            new_color = self.colordialog.getColor(self._shift_color)
            if new_color.isValid() and new_color.name() != self._shift_color.name():
                self._shift_color = new_color
                self._shift_brush = QBrush(new_color)
                if new_color not in self._shift_colors:
                    self._shift_colors.insert(self._shift_color_index, new_color)
                self.update()
        else:
            new_color = self.colordialog.getColor(self._color)
            if new_color.isValid() and new_color.name() != self._color.name():
                self._color = new_color
                self._brush = QBrush(new_color)
                if new_color not in self._colors:
                    self._colors.insert(self._color_index, new_color)
                self.update()

# -*- coding: utf-8 -*- Attributes:         colordialog (QColorDialog): Description         rect (QRectF): Description Args:             parent: Default is ``None`` # print("A bug in Qt is about to cause a warning 'Layout still needs update after calling...'") # print("Please vote to fix here: https://bugreports.qt.io/browse/QTBUG-58699") # init on -1, painttool will cycle to 0 # init on -1, painttool will cycle to 0 Returns:             :class:`QRectF` Args:             painter: Description             option: Description             widget: Description Increment ``self._color_index`` Decrement ``self._color_index`` Returns:             :class:`QColor` Returns:             string name of the color Returns:             string name of the color Args:             event: Description