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

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

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from KdeQt.KQApplication import e4App

from Ui_MatchesDialog import Ui_MatchesDialog

"""
Module implementing a dialog to show matching references/definitions.
"""

class MatchesDialog(QDialog, Ui_MatchesDialog):
    """
    Class implementing a dialog to show matching references/definitions.
    """
    def __init__(self, ui, parent = None, name = None):
        """
        Constructor
        
        @param ui reference to the UI object
        @param parent parent of this dialog (QWidget)
        @param name name of this dialog (string or QString)
        """
        QDialog.__init__(self, parent)
        if name:
            self.setObjectName(name)
        self.setupUi(self)
        
        self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
        
        self.ui = ui
        self.matchesList.header().setSortIndicator(0, Qt.AscendingOrder)
        
    def __resort(self):
        """
        Private method to resort the tree.
        """
        self.matchesList.sortItems(self.matchesList.sortColumn(), 
                                   self.matchesList.header().sortIndicatorOrder())
        
    def __resizeColumns(self):
        """
        Private method to resize the list columns.
        """
        self.matchesList.header().resizeSections(QHeaderView.ResizeToContents)
        self.matchesList.header().setStretchLastSection(True)
        
    def on_matchesList_itemActivated(self, itm, column):
        """
        Private slot to handle the itemActivated signal of the list.
        """
        lineno = int(str(itm.text(1)))
        fn = unicode(itm.text(0))
        e4App().getObject("ViewManager").openSourceFile(fn, lineno)
        
    def addEntry(self, ref):
        """
        Public slot to add a reference to the list.
        """
        itm = QTreeWidgetItem(self.matchesList, QStringList() \
            << ref.filename << " %5d" % ref.lineno << " %5d" % ref.confidence)
        itm.setTextAlignment(1, Qt.AlignRight)
        itm.setTextAlignment(2, Qt.AlignRight)
        self.__resort()
        self.__resizeColumns()
