<commit_before>'''
Created on 23.06.2011

@author: michi
'''
import sys

from PyQt4.QtCore import QObject, QVariant, SIGNAL, SLOT, pyqtSignal, pyqtSlot, \
    Qt, QString
from PyQt4.QtGui import QWidget, QComboBox, QCheckBox, QLineEdit, QGridLayout, \
    QPushButton, QTreeWidgetItem, QStackedWidget

from ems.qt4.gui.widgets.treecombo import TreeComboBox #@UnresolvedImport
from ems.qt4.gui.widgets.bigcombo import BigComboBox #@UnresolvedImport
from ems.qt4.util import variant_to_pyobject #@UnresolvedImport

class BuilderBackend(QObject):
    def setSearchWidget(self, widget):
        self._searchWidget = widget
    
    def populateFieldInput(self, fieldInput):
        pass
        
    
    def rowAdded(self, row):
        pass
    
    def onFieldInputCurrentItemChanged(self, searchRow, item):
        pass
    
    def buildQuery(self, criterias):
        raise NotImplementedError("Please create a buildQuery Method")

class SearchRow(QObject):
    
    rowEnabled = pyqtSignal(bool)
    rowDisabled = pyqtSignal(bool)
    
    def __init__(self, builder, parent=None):
        super(SearchRow, self).__init__(parent)
        
        self._builder = builder
        self._enabled = True
        self.removeButton = None
        self.conjunctionButton = None
                    
        self.fieldInput = TreeComboBox()
        self.fieldInput.setMinimumWidth(200)
        self.operatorInput = QComboBox()
        self.operatorInput.setMinimumContentsLength(12)
        self.valueStack = QStackedWidget()
        self.valueStack.setMaximumHeight(50)
        self.valueStack.setMinimumHeight(25)
        self.valueInput = QLineEdit()
        self.valueStack.addWidget(self.valueInput)
        self.matchesInput = QCheckBox()
        
        
        self.connect(self.fieldInput, SIGNAL("currentIndexChanged(int)"),
                     self, SLOT("onFieldInputCurrentIndexChanged(int)"))
        self.setupUi()
        
#        self.connect(self.fieldInput, SIGNAL("currentIndexChanged(QString)"),
#                     self, SLOT("onFieldInputCurrentTextChanged(QString)"))
#        
        #self.onFieldInputCurrentIndexChanged(0)
    
    def replaceValueInput(self, widget):
        self.valueStack.removeWidget(self.valueInput)
        i = self.valueStack.addWidget(widget)
        self.valueStack.setCurrentIndex(i)
        self.valueInput = widget
    
    @pyqtSlot(QString)
    def onFieldInputCurrentTextChanged(self, text):
        print "onTextChanged: %s" % text
        
    
    @pyqtSlot(int)
    def onFieldInputCurrentIndexChanged(self, index):
        currentItem = self.fieldInput.itemView.currentItem()
        if currentItem is None:
            comboText = self.fieldInput.itemData(index, Qt.DisplayRole).toString()
            foundedItems = self.fieldInput.itemView.findItems(comboText,Qt.MatchExactly,0)
            if len(foundedItems):
                currentItem = foundedItems[0]
                self._builder.onFieldInputCurrentItemChanged(self, currentItem)
        else:
            self._builder.onFieldInputCurrentItemChanged(self, currentItem)
        
    def reset(self):
        self.fieldInput.setCurrentIndex(0)
        #self.valueInput.setText("")
    
    def setEnabled(self, enabled=True):
        for widget in (self.fieldInput, self.operatorInput,
                       self.valueInput, self.matchesInput):
            widget.setEnabled(enabled)
        self._enabled = enabled
        self.rowEnabled.emit(self._enabled)
        self.rowDisabled.emit(not self._enabled)
            
    def setDisabled(self, enabled=False):
        return self.setEnabled(enabled)
         
    def setupUi(self):
        #self.fieldInput.addItem("Name")
        self._builder.populateFieldInput(self.fieldInput)
        self.matchesInput.setChecked(True)
        self.matchesInput.setText(self.tr("Trifft zu"))
    
    def __del__(self):
        print "Destructor called"
    
    def delete(self):
        print "delete()"
    

class RowAddSearch(QWidget):
    
    rowAdded = pyqtSignal(int)
    queryChanged = pyqtSignal(object)
    
    def __init__(self, builderBackend=None, parent=None):
        QWidget.__init__(self, parent)
        if builderBackend is None:
            builderBackend = BuilderBackend()
        self._builder = builderBackend
        self._builder.setSearchWidget(self)
        
        self._rows = []
        self._addRowButton = None
        self.setupUi()
        #self.setStyleSheet("background-color: #fff")
        
    
    def setupUi(self):
        self.setLayout(QGridLayout(self))
        
        self.addRow()
    
    @property
    def addRowButton(self):
        return self.getAddRowButton()
    
    def getAddRowButton(self):
        if self._addRowButton is None:
            self._addRowButton = QPushButton("+",self)
            self.connect(self._addRowButton, SIGNAL("clicked()"),
                         self, SLOT('addRow()'))
        return self._addRowButton
    
    @property
    def conjunctionButton(self):
        return self.getConjunctionButton()
    
    def getConjunctionButton(self):
        widget = QComboBox()
        widget.setMinimumWidth(60)
        widget.setMaximumWidth(60)
        widget.addItem("Und", QVariant("AND"))
        widget.addItem("Oder", QVariant("OR"))
        return widget
    
    def getConjunctionPlaceHolder(self):
        widget = QWidget()
        widget.setMinimumWidth(60)
        widget.setMaximumWidth(60)
        return widget
    
    def getRemoveButton(self):
        return QPushButton("-")

        
    @pyqtSlot()
    def _onDeleteClicked(self):
        searchRow = self.sender().row
        i = 0
        for row in self._rows:
            if row is searchRow:
                self.removeRow(i)
                break
            i += 1 
            
    
    @pyqtSlot()
    def addRow(self):
        #print "addRow"
        currentRow = len(self._rows)
        if currentRow < 0:
            currentRow = 0
            
        row = SearchRow(self._builder, self)
        
        layout = self.layout()<commit_msg>* Möglichkeit eingebaut, die +/- Buttons zu übergeben<commit_after>'''
Created on 23.06.2011

@author: michi
'''
import sys

from PyQt4.QtCore import QObject, QVariant, SIGNAL, SLOT, pyqtSignal, pyqtSlot, \
    Qt, QString
from PyQt4.QtGui import QWidget, QComboBox, QCheckBox, QLineEdit, QGridLayout, \
    QPushButton, QTreeWidgetItem, QStackedWidget, QIcon

from ems.qt4.gui.widgets.treecombo import TreeComboBox #@UnresolvedImport
from ems.qt4.gui.widgets.bigcombo import BigComboBox #@UnresolvedImport
from ems.qt4.util import variant_to_pyobject #@UnresolvedImport

class BuilderBackend(QObject):
    def setSearchWidget(self, widget):
        self._searchWidget = widget
    
    def populateFieldInput(self, fieldInput):
        pass
        
    
    def rowAdded(self, row):
        pass
    
    def onFieldInputCurrentItemChanged(self, searchRow, item):
        pass
    
    def buildQuery(self, criterias):
        raise NotImplementedError("Please create a buildQuery Method")

class SearchRow(QObject):
    
    rowEnabled = pyqtSignal(bool)
    rowDisabled = pyqtSignal(bool)
    
    def __init__(self, builder, parent=None):
        super(SearchRow, self).__init__(parent)
        
        self._builder = builder
        self._enabled = True
        self.removeButton = None
        self.conjunctionButton = None
                    
        self.fieldInput = TreeComboBox()
        self.fieldInput.setMinimumWidth(200)
        self.operatorInput = QComboBox()
        self.operatorInput.setMinimumContentsLength(12)
        self.valueStack = QStackedWidget()
        self.valueStack.setMaximumHeight(50)
        self.valueStack.setMinimumHeight(25)
        self.valueInput = QLineEdit()
        self.valueStack.addWidget(self.valueInput)
        self.matchesInput = QCheckBox()
        
        
        self.connect(self.fieldInput, SIGNAL("currentIndexChanged(int)"),
                     self, SLOT("onFieldInputCurrentIndexChanged(int)"))
        self.setupUi()
        
#        self.connect(self.fieldInput, SIGNAL("currentIndexChanged(QString)"),
#                     self, SLOT("onFieldInputCurrentTextChanged(QString)"))
#        
        #self.onFieldInputCurrentIndexChanged(0)
    
    def replaceValueInput(self, widget):
        self.valueStack.removeWidget(self.valueInput)
        i = self.valueStack.addWidget(widget)
        self.valueStack.setCurrentIndex(i)
        self.valueInput = widget
    
    @pyqtSlot(QString)
    def onFieldInputCurrentTextChanged(self, text):
        print "onTextChanged: %s" % text
        
    
    @pyqtSlot(int)
    def onFieldInputCurrentIndexChanged(self, index):
        currentItem = self.fieldInput.itemView.currentItem()
        if currentItem is None:
            comboText = self.fieldInput.itemData(index, Qt.DisplayRole).toString()
            foundedItems = self.fieldInput.itemView.findItems(comboText,Qt.MatchExactly,0)
            if len(foundedItems):
                currentItem = foundedItems[0]
                self._builder.onFieldInputCurrentItemChanged(self, currentItem)
        else:
            self._builder.onFieldInputCurrentItemChanged(self, currentItem)
        
    def reset(self):
        self.fieldInput.setCurrentIndex(0)
        #self.valueInput.setText("")
    
    def setEnabled(self, enabled=True):
        for widget in (self.fieldInput, self.operatorInput,
                       self.valueInput, self.matchesInput):
            widget.setEnabled(enabled)
        self._enabled = enabled
        self.rowEnabled.emit(self._enabled)
        self.rowDisabled.emit(not self._enabled)
            
    def setDisabled(self, enabled=False):
        return self.setEnabled(enabled)
         
    def setupUi(self):
        #self.fieldInput.addItem("Name")
        self._builder.populateFieldInput(self.fieldInput)
        self.matchesInput.setChecked(True)
        self.matchesInput.setText(self.tr("Trifft zu"))
    
    def __del__(self):
        print "Destructor called"
    
    def delete(self):
        pass
    

class RowAddSearch(QWidget):
    
    rowAdded = pyqtSignal(int)
    queryChanged = pyqtSignal(object)
    
    def __init__(self, builderBackend=None, parent=None,
                 buttonFactory=None):
        QWidget.__init__(self, parent)
        if builderBackend is None:
            builderBackend = BuilderBackend()
        self._builder = builderBackend
        self._builder.setSearchWidget(self)
        
        self._rows = []
        self._addRowButton = None
        self._buttonFactory = buttonFactory
        self.setupUi()
        
        #self.setStyleSheet("background-color: #fff")
        
    
    def setupUi(self):
        self.setLayout(QGridLayout(self))
        
        self.addRow()
    
    @property
    def addRowButton(self):
        return self.getAddRowButton()
    
    def getAddRowButton(self):
        if self._addRowButton is None:
            if self._buttonFactory is None:
                self._addRowButton = QPushButton("+",self)
            else:
                self._addRowButton = self._buttonFactory('add')
                
            self.connect(self._addRowButton, SIGNAL("clicked()"),
                         self, SLOT('addRow()'))
        return self._addRowButton
    
    @property
    def conjunctionButton(self):
        return self.getConjunctionButton()
    
    def getConjunctionButton(self):
        widget = QComboBox()
        widget.setMinimumWidth(60)
        widget.setMaximumWidth(60)
        widget.addItem("Und", QVariant("AND"))
        widget.addItem("Oder", QVariant("OR"))
        return widget
    
    def getConjunctionPlaceHolder(self):
        widget = QWidget()
        widget.setMinimumWidth(60)
        widget.setMaximumWidth(60)
        return widget
    
    def getRemoveButton(self):
        if self._buttonFactory is None:
            button = QPushButton("-",self)
        else:
            button = self._buttonFactory('remove')
            
        return button
    

        
    @pyqtSlot()
    def _onDeleteClicked(self):
        searchRow = self.sender().row
        i = 0
        for row in self._rows:
            if row is searchRow:
                self.removeRow(i)
                break
            i += 1 
            
    
    @pyqtSlot()
    def addRow(self):
        #print "addRow"
        currentRow = len(self._rows)
        if currentRow < 0:
            currentRow = 0
            
        row = SearchRow(self._builder, self)
        
        layout = self.layout()
{'fraction_non_alphanumeric': 0.09295126210509605, 'fraction_numerical': 0.006032703603746627, 'mean_word_length': 2.237153134635149, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4102657', 'n_tokens_mistral': 3640, 'n_tokens_neox': 3544, 'n_words': 656}