# -*- coding: utf-8 -*-

# Copyright (c) 2005 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the Watchpoint viewer widget.
"""

from qt import QListView, QListViewItem, QPopupMenu, SIGNAL, QDialog, Qt, QString

from EditWatchpointDialog import EditWatchpointDialog
from KdeQt import KQMessageBox

class WatchPointViewer(QListView):
    """
    Class implementing the Watchpoint viewer widget.
    
    Watchpoints will be shown with all their details. They can be modified through
    the context menu of this widget.
    """
    def __init__(self, dbs, parent = None):
        """
        Constructor
        
        @param reference to the debug server object
        @param parent the parent (QWidget)
        """
        QListView.__init__(self,parent)
        
        self.dbs = dbs
        
        self.addColumn(self.trUtf8('Condition'))
        
        col = self.addColumn(self.trUtf8('Temporary'))
        self.setColumnAlignment(col, Qt.AlignHCenter)
        
        col = self.addColumn(self.trUtf8('Enabled'))
        self.setColumnAlignment(col, Qt.AlignHCenter)
        
        col = self.addColumn(self.trUtf8('Ignore Count'))
        self.setColumnAlignment(col, Qt.AlignRight)
        
        self.setSorting(0)
        self.setSelectionMode(QListView.Extended)
        self.setAllColumnsShowFocus(1)
        
        self.setCaption(self.trUtf8("Watchpoints"))
        
        self.connect(self,SIGNAL('contextMenuRequested(QListViewItem *, const QPoint &, int)'),
                     self.handleContextMenu)
        self.connect(self,SIGNAL('doubleClicked(QListViewItem *, const QPoint &, int)'),
                     self.handleDoubleClicked)
        
        self.createPopupMenus()
        
    def createPopupMenus(self):
        """
        Private method to generate the popup menus.
        """
        self.menu = QPopupMenu()
        self.menu.insertItem(self.trUtf8("Add"), self.addWatch)
        self.menu.insertItem(self.trUtf8("Edit..."), self.editWatch)
        self.menu.insertSeparator()
        self.menu.insertItem(self.trUtf8("Enable"), self.enableWatch)
        self.menu.insertItem(self.trUtf8("Enable all"), self.enableAllWatches)
        self.menu.insertSeparator()
        self.menu.insertItem(self.trUtf8("Disable"), self.disableWatch)
        self.menu.insertItem(self.trUtf8("Disable all"), self.disableAllWatches)
        self.menu.insertSeparator()
        self.menu.insertItem(self.trUtf8("Delete"), self.deleteWatch)
        self.menu.insertItem(self.trUtf8("Delete all"), self.deleteAllWatches)

        self.backMenuIds = {}
        self.backMenu = QPopupMenu()
        self.backMenu.insertItem(self.trUtf8("Add"), self.addWatch)
        self.backMenuIds["EnableAll"] = \
            self.backMenu.insertItem(self.trUtf8("Enable all"), self.enableAllWatches)
        self.backMenuIds["DisableAll"] = \
            self.backMenu.insertItem(self.trUtf8("Disable all"), self.disableAllWatches)
        self.backMenuIds["DeleteAll"] = \
            self.backMenu.insertItem(self.trUtf8("Delete all"), self.deleteAllWatches)
        self.connect(self.backMenu, SIGNAL('aboutToShow()'), self.handleShowBackMenu)

        self.multiMenu = QPopupMenu()
        self.multiMenu.insertItem(self.trUtf8("Add"), self.addWatch)
        self.multiMenu.insertSeparator()
        self.multiMenu.insertItem(self.trUtf8("Enable selected"), self.enableSelectedWatches)
        self.multiMenu.insertItem(self.trUtf8("Enable all"), self.enableAllWatches)
        self.multiMenu.insertSeparator()
        self.multiMenu.insertItem(self.trUtf8("Disable selected"), self.disableSelectedWatches)
        self.multiMenu.insertItem(self.trUtf8("Disable all"), self.disableAllWatches)
        self.multiMenu.insertSeparator()
        self.multiMenu.insertItem(self.trUtf8("Delete selected"), self.deleteSelectedWatches)
        self.multiMenu.insertItem(self.trUtf8("Delete all"), self.deleteAllWatches)

    def findDuplicates(self, cond, showMessage=0):
        """
        Private method to check, if an entry already exists.
        
        @param cond condition to check (QString)
        @param showMessage flag indicating a message should be shown,
            if a duplicate entry is found (boolean)
        @return flag indicating a duplicate entry (boolean)
        """
        duplicate = self.findItem(cond, 0) is not None
        if showMessage and duplicate:
            KQMessageBox.warning(None,
                self.trUtf8("Watchpoint already exists"),
                self.trUtf8("""<p>A watchpoint with the condition '<b>%1</b>' already exists.</p>""")\
                .arg(cond),
                self.trUtf8("&OK"),
                None,
                None,
                0, -1)

        return duplicate
    
    def addWatch(self):
        """
        Private slot to handle the add watchpoint context menu entry.
        """
        dlg = EditWatchpointDialog((None, 0, 1, 0), self)
        if dlg.exec_loop() == QDialog.Accepted:
            cond, temp, enabled, ignorecount = dlg.getData()
            if not self.findDuplicates(cond, 1):
                QListViewItem(self, 
                              cond, 
                              temp and self.trUtf8("Yes") or self.trUtf8("No"),
                              enabled and self.trUtf8("Yes") or self.trUtf8("No"),
                              "%d" % ignorecount)
                self.dbs.remoteWatchpoint(cond, 1, temp)
                if not enabled:
                    self.dbs.remoteWatchpointEnable(cond, 0)
                if ignorecount:
                    self.dbs.remoteWatchpointIgnore(cond, ignorecount)
    
    def editWatch(self):
        """
        Private slot to handle the edit watchpoint context menu entry.
        """
        itm = self.currentItem()
        if itm is not None:
            self.editWatchpoint(itm)
        
    def editWatchpoint(self, itm):
        """
        Private slot to edit a watchpoint.
        
        @param itm watchpoint to be edited (QListViewItem)
        """
        wpProperties = self.extractData(itm)
        dlg = EditWatchpointDialog(wpProperties, self)
        if dlg.exec_loop() == QDialog.Accepted:
            cond, temp, enabled, ignorecount = dlg.getData()
            
            itm.setText(0, cond)
            itm.setText(1, temp and self.trUtf8("Yes") or self.trUtf8("No"))
            itm.setText(2, enabled and self.trUtf8("Yes") or self.trUtf8("No"))
            itm.setText(3, "%d" % ignorecount)
            
            # step 1: delete the watchpoint
            self.dbs.remoteWatchpoint(wpProperties[0], 0)
            
            # step 2: add a new watchpoint with the new properties
            self.dbs.remoteWatchpoint(cond, 1, temp)
            if not enabled:
                self.dbs.remoteWatchpointEnable(cond, 0)
            if ignorecount:
                self.dbs.remoteWatchpointIgnore(cond, ignorecount)
    
    def deleteWatch(self):
        """
        Private slot to handle the delete watchpoint context menu entry.
        """
        itm = self.currentItem()
        cond = self.extractData(itm)[0]
        self.dbs.remoteWatchpoint(cond, 0)
        self.takeItem(itm)
        del itm
    
    def deleteAllWatches(self):
        """
        Private slot to handle the delete all watchpoints context menu entry.
        """
        itm = self.firstChild()
        while itm:
            cond = self.extractData(itm)[0]
            self.dbs.remoteWatchpoint(cond, 0)
            itm = itm.itemBelow()
        self.clear()
    
    def deleteSelectedWatches(self):
        """
        Private slot to handle the delete selected watchpoints context menu entry.
        """
        for itm in self.getSelectedItems():
            cond = self.extractData(itm)[0]
            self.dbs.remoteWatchpoint(cond, 0)
            self.takeItem(itm)
            del itm
    
    def enableWatch(self):
        """
        Private slot to handle the enable watchpoint context menu entry.
        """
        itm = self.currentItem()
        cond = self.extractData(itm)[0]
        self.dbs.remoteWatchpointEnable(cond, 1)
        itm.setText(2, self.trUtf8("Yes"))
    
    def enableAllWatches(self):
        """
        Private slot to handle the enable all watchpoints context menu entry.
        """
        itm = self.firstChild()
        while itm:
            cond = self.extractData(itm)[0]
            self.dbs.remoteWatchpointEnable(cond, 1)
            itm.setText(2, self.trUtf8("Yes"))
            itm = itm.itemBelow()
    
    def enableSelectedWatches(self):
        """
        Private slot to handle the enable selected watchpoints context menu entry.
        """
        for itm in self.getSelectedItems():
            cond = self.extractData(itm)[0]
            self.dbs.remoteWatchpointEnable(cond, 1)
            itm.setText(2, self.trUtf8("Yes"))
   
    def disableWatch(self):
        """
        Private slot to handle the disable watchpoint context menu entry.
        """
        itm = self.currentItem()
        cond = self.extractData(itm)[0]
        self.dbs.remoteWatchpointEnable(cond, 0)
        itm.setText(2, self.trUtf8("No"))

    def disableAllWatches(self):
        """
        Private slot to handle the disable all watchpoints context menu entry.
        """
        itm = self.firstChild()
        while itm:
            cond = self.extractData(itm)[0]
            self.dbs.remoteWatchpointEnable(cond, 0)
            itm.setText(2, self.trUtf8("No"))
            itm = itm.itemBelow()
    
    def disableSelectedWatches(self):
        """
        Private slot to handle the disable selected watchpoints context menu entry.
        """
        for itm in self.getSelectedItems():
            cond = self.extractData(itm)[0]
            self.dbs.remoteWatchpointEnable(cond, 0)
            itm.setText(2, self.trUtf8("No"))
    
    def clearWatchpoint(self, cond):
        """
        Public slot to clear a watchpoint.
        
        @param cond condition of the watchpoint (string or QString)
        """
        itm = self.findItem(cond, 0)
        if itm is not None:
            self.takeItem(itm)
            del itm
    
    def handleContextMenu(self,itm,coord,col):
        """
        Private slot to show the context menu of the listview.
        
        @param itm the selected listview item (QListViewItem)
        @param coord the position of the mouse pointer (QPoint)
        @param col the column of the mouse pointer (int)
        """
        cnt = self.getSelectedItemsCount()
        if cnt > 1:
            self.multiMenu.popup(coord)
        elif cnt == 1:
            self.menu.popup(coord)
        else:
            self.backMenu.popup(coord)
    
    def handleDoubleClicked(self,itm,coord,col):
        """
        Private slot to handle the double clicked signal.
        
        @param itm the selected listview item (QListViewItem)
        @param coord the position of the mouse pointer (QPoint)
        @param col the column of the mouse pointer (int)
        """
        if itm is not None:
            self.editWatchpoint(itm)
    
    def handleShowBackMenu(self):
        """
        Private slot to handle the aboutToShow signal of the background menu.
        """
        if self.childCount() == 0:
            self.backMenu.setItemEnabled(self.backMenuIds["EnableAll"], 0)
            self.backMenu.setItemEnabled(self.backMenuIds["DisableAll"], 0)
            self.backMenu.setItemEnabled(self.backMenuIds["DeleteAll"], 0)
        else:
            self.backMenu.setItemEnabled(self.backMenuIds["EnableAll"], 1)
            self.backMenu.setItemEnabled(self.backMenuIds["DisableAll"], 1)
            self.backMenu.setItemEnabled(self.backMenuIds["DeleteAll"], 1)
    
    def getSelectedItems(self):
        """
        Public method to get the selected items.
        
        @return list of selected items (list of QListViewItem)
        """
        selectedItems = []
        itm = self.firstChild()
        while itm:
            if itm.isSelected():
                selectedItems.append(itm)
            itm = itm.itemBelow()
        return selectedItems
    
    def getSelectedItemsCount(self):
        """
        Public method to get the count of items selcted.
        
        @return count of items selected (integer)
        """
        count = 0
        itm = self.firstChild()
        while itm:
            if itm.isSelected():
                count += 1
            itm = itm.itemBelow()
        return count

    def getWatchpoints(self):
        """
        Public mathod to retrieve all watchpoints
        
        @return list of tuples with condition, temp, enabled, ignoreCount
            (QString, boolean, boolean, integer)
        """
        wpList = []
        itm = self.firstChild()
        while itm:
            wpList.append(self.extractData(itm))
            itm = itm.itemBelow()
        return wpList
        
    def setWatchpoint(self, properties):
        """
        Public method to set a watchpoint with its properties.
        
        @param properties tuple of condition, temp, enabled, ignoreCount
            (QString or string, boolean, boolean, integer)
        """
        cond, temp, enabled, ignorecount = properties
        cond = QString(cond)
        if not self.findDuplicates(cond):
            QListViewItem(self, 
                          cond,
                          temp and self.trUtf8("Yes") or self.trUtf8("No"),
                          enabled and self.trUtf8("Yes") or self.trUtf8("No"),
                          "%d" % ignorecount)
            self.dbs.remoteWatchpoint(cond, 1, temp)
            if not enabled:
                self.dbs.remoteWatchpointEnable(cond, 0)
            if ignorecount:
                self.dbs.remoteWatchpointIgnore(cond, ignorecount)
        
    def extractData(self, itm):
        """
        Private method to extract the data of a watchpoint item.
        
        @param itm the watchpoint item (QListViewItem)
        @return tuple with condition, temp, enabled, ignoreCount
            (QString, boolean, boolean, integer)
        """
        temp = itm.text(1).compare(self.trUtf8("Yes")) == 0 and 1 or 0
        enabled = itm.text(2).compare(self.trUtf8("Yes")) == 0 and 1 or 0
        count, ok = itm.text(3).toUInt()
        return (itm.text(0), temp, enabled, count)
