<commit_before>#    Copyright 2012, SIL International
#    All rights reserved.
#
#    This library is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published
#    by the Free Software Foundation; either version 2.1 of License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should also have received a copy of the GNU Lesser General Public
#    License along with this library in the file named "LICENSE".
#    If not, write to the Free Software Foundation, 51 Franklin Street,
#    suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
#    internet at http://www.fsf.org/licenses/lgpl.html.

from qtpy import QtCore, QtGui, QtWidgets
from graide.utils import configval, configintval
import os


class FileEntry(QtWidgets.QWidget) :

    textChanged = QtCore.Signal(str)

    def __init__(self, parent, val, pattern) :
        super(FileEntry, self).__init__(parent)
        self.pattern = pattern
        hboxLayout = QtWidgets.QHBoxLayout(self)
        hboxLayout.setContentsMargins(0, 0, 0, 0)
        hboxLayout.setSpacing(0)
        self.le = QtWidgets.QLineEdit(self)
        self.le.textChanged.connect(self.txtChanged)
        if val :
            self.le.setText(val)
        hboxLayout.addWidget(self.le)
        self.b = QtWidgets.QToolButton(self)
        self.b.setIcon(QtGui.QIcon.fromTheme("document-open", QtGui.QIcon(":/images/document-open.png")))
        hboxLayout.addWidget(self.b)
        self.b.clicked.connect(self.bClicked)

    def bClicked(self) :
        (fname, filt) = QtWidgets.QFileDialog.getSaveFileName(self,
                dir=os.path.dirname(self.le.text()), filter=self.pattern,
                options=QtWidgets.QFileDialog.DontConfirmOverwrite)
        if fname :
            self.le.setText(os.path.relpath(fname))
        #else Cancel was hit

    def text(self) :
        return self.le.text()

    def setText(self, txt) :
        self.le.setText(txt)

    def txtChanged(self, txt) :
        self.textChanged.emit(txt)
        
#end of class FileEntry


class PassSpin(QtWidgets.QSpinBox) :

    def __init__(self, parent = None) :
        super(PassSpin, self).__init__(parent)
        self.setMinimum(0)
        self.setSpecialValueText('None')
        self.setValue(-1)
        self.setMaximumWidth(50)

# end of class PassSpin


class ConfigDialog(QtWidgets.QDialog) :

    def __init__(self, config, currTab = None, parent = None) :
        super(ConfigDialog, self).__init__(parent)
        self.config = config
        
        self.setWindowTitle("Configure project")

        vboxLayout = QtWidgets.QVBoxLayout(self)
        self.tb = QtWidgets.QToolBox(self)
        vboxLayout.addWidget(self.tb)
        self.ok = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
        self.ok.accepted.connect(self.accept)
        self.ok.rejected.connect(self.reject)
        vboxLayout.addWidget(self.ok)

        # General section
        self.general = QtWidgets.QWidget(self.tb)
        genGridLay = QtWidgets.QGridLayout(self.general)
#        self.general_vb.setVerticalSpacing(0)
        self.general_font = FileEntry(self.general, configval(config, 'main', 'font'), 'Font Files (*.ttf)')
        genGridLay.addWidget(QtWidgets.QLabel('Font File:'), 0, 0)
        genGridLay.addWidget(self.general_font, 0, 1, 1, 2)
        self.general_gdl = FileEntry(self.general, configval(config, 'build', 'gdlfile'), 'GDL Files (*.gdl)')
        genGridLay.addWidget(QtWidgets.QLabel('GDL File:'), 1, 0)
        genGridLay.addWidget(self.general_gdl, 1, 1, 1, 2)
        self.general_tests = FileEntry(self.general, configval(config, 'main', 'testsfile'), 'Tests Lists (*.xml)')
        genGridLay.addWidget(QtWidgets.QLabel('Tests File:'), 2, 0)
        genGridLay.addWidget(self.general_tests, 2, 1, 1, 2)
        self.general_rtl = QtWidgets.QCheckBox()
        self.general_rtl.setChecked(configintval(config, 'main', 'defaultrtl'))
        genGridLay.addWidget(QtWidgets.QLabel('Default RTL'), 3, 0)
        genGridLay.addWidget(self.general_rtl, 3, 1)
        genGridLay.setRowStretch(4, 1)  # 4 = total number of rows used
        self.tb.addItem(self.general, "General")

        # Build section
        # column 0 = main sub-section labels, column 1 - indented labels, column 2 = control for main sub-section,
        # column 3 = controls for indented stuff
        self.build = QtWidgets.QWidget(self.tb)
        buildGridLo = QtWidgets.QGridLayout(self.build)
        
        # makegdl / attachment point generation
        #self.build_apxml = FileEntry(self.general, configval(config, 'main', 'ap'), 'AP Files (*.xml)')
        #self.build_apxml.textChanged.connect(self.apChanged)
        #buildGridLo.addWidget(QtWidgets.QLabel('Attachment point database:'), 0, 0, 1, 2) # cols 0-1
        #buildGridLo.addWidget(self.build_apxml, 0, 2)

        self.build_grcexec = QtWidgets.QLineEdit(self.build)
        self.build_grcexec.setText(configval(config, 'build', 'grcexecutable'))
        self.build_grcexec.setToolTip('Graphite compiler executable; may be a separate batch file or shell script')
        buildGridLo.addWidget(QtWidgets.QLabel('Compiler executable:'), 0, 0, 1, 2) # cols 0-1
        buildGridLo.addWidget(self.build_grcexec, 0, 2)

        self.build_gdlinc = FileEntry(self.general, configval(config, 'build', 'makegdlfile'), 'GDL Files (*.gdl *.gdh)')
        self.build_gdlinc.textChanged.connect(self.apChanged)
        buildGridLo.addWidget(QtWidgets.QLabel('Autogenerated GDL file:'), 1, 0, 1, 2) # cols 0-1
        buildGridLo.addWidget(self.build_gdlinc, 1, 2)
        
        # sub-controls for AP generation, enabled or disabled as a unit
        self.build_apctrls = QtWidgets.QWidget(self.build)
        buildGridLo.addWidget(self.build_apctrls, 2, 0, 4, 3)  # 4 rows
        apGridLo = QtWidgets.QGridLayout(self.build_apctrls)
        apGridLo.setColumnMinimumWidth(0, 125) # Make the columns in this control match tweakGridLo !!!
        apGridLo.setColumnMinimumWidth(1, 20)
        
        self.build_makegdlcmd = QtWidgets.QLineEdit(self.build_apctrls)
        self.build_makegdlcmd.setMinimumWidth(250)
        self.build_makegdlcmd.setText(configval(config, 'build', 'makegdlcmd'))
        self.build_makegdlcmd.setToolTip('External make-gdl command: %a=AP Database, %f=Font File, %g=Generated GDL File,\n    %i=included GDL file %p=positioning pass number')
        apGridLo.addWidget(QtWidgets.QLabel('Make GDL Command:'), 1, 0)
        apGridLo.addWidget(self.build_makegdlcmd, 1, 2, 1, 3) # cols 1-3
        
        #self.build_gdlinc = FileEntry(self.build_apctrls, configval(config, 'build', 'makegdlfile'), 'GDL Files (*.gdl *.gdh)')
        #self.build_gdlinc.setMinimumWidth(250)
        #apGridLo.addWidget(QtWidgets.QLabel('Autogenerated GDL file:'), 2, 0, 1, 2) # cols 0-1
        #apGridLo.addWidget(self.build_gdlinc, 2, 2, 1, 2) # cols 2-3
        
        self.build_apxml = FileEntry(self.build_apctrls, configval(config, 'main', 'ap'), 'AP Files (*.xml)')
        #self.build_apxml.setMinimumWidth(250) - make the dialog scroll horizontally
        apGridLo.addWidget(QtWidgets.QLabel('Attachment point database:'), 2, 0, 1, 2) # cols 0-1
        apGridLo.addWidget(self.build_apxml, 2, 2, 1, 2) # cols 2-3
        
        self.build_apronly = QtWidgets.QCheckBox()
        self.build_apronly.setChecked(configintval(config, 'build', 'apronly'))
        self.build_apronly.setDisabled(True)  # mechanism to update AP file from Graide is not reliable
        apGridLo.addWidget(QtWidgets.QLabel('AP Database is read-only:'), 3, 0, 1, 2) # cols 0-1
        apGridLo.addWidget(self.build_apronly, 3, 2)
        
        self.build_att = PassSpin(self.build_apctrls)
        attpass = configval(config, 'build', 'attpass')
        if attpass :
            self.build_att.setValue(int(attpass))
        apGridLo.addWidget(QtWidgets.QLabel('Attachment positioning pass:'), 4, 0, 1, 2) # cols 0-1
        apGridLo.addWidget(self.build_att, 4, 2)
        
        #if not self.build_apxml.text() :
        if not self.build_gdlinc.text() :
            self.build_apctrls.setEnabled(False)

        # Ignore warnings
        self.build_noWarning = QtWidgets.QLineEdit(self.build_apctrls)
        self.build_noWarning.setMinimumWidth(300)
        if config.has_option('build', 'ignorewarnings') :
            self.build_noWarning.setText(configval(config, 'build', 'ignorewarnings'))
        else :
            self.build_noWarning.setText("510,3521")  # warnings to ignore by default
        self.build_noWarning.setToolTip('List warning numbers to be ignored, or \'none\'')
        buildGridLo.addWidget(QtWidgets.QLabel('Ignore warnings:'), 6, 0)
        buildGridLo.addWidget(self.build_noWarning, 6, 2, 1, 3) # cols 1-3
             
        # Tweaking controls<commit_msg>Move compiler executable control lower in the dialog and make it be able to choose a file.<commit_after>#    Copyright 2012, SIL International
#    All rights reserved.
#
#    This library is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published
#    by the Free Software Foundation; either version 2.1 of License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should also have received a copy of the GNU Lesser General Public
#    License along with this library in the file named "LICENSE".
#    If not, write to the Free Software Foundation, 51 Franklin Street,
#    suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
#    internet at http://www.fsf.org/licenses/lgpl.html.

from qtpy import QtCore, QtGui, QtWidgets
from graide.utils import configval, configintval
import os


class FileEntry(QtWidgets.QWidget) :

    textChanged = QtCore.Signal(str)

    def __init__(self, parent, val, pattern) :
        super(FileEntry, self).__init__(parent)
        self.pattern = pattern
        hboxLayout = QtWidgets.QHBoxLayout(self)
        hboxLayout.setContentsMargins(0, 0, 0, 0)
        hboxLayout.setSpacing(0)
        self.le = QtWidgets.QLineEdit(self)
        self.le.textChanged.connect(self.txtChanged)
        if val :
            self.le.setText(val)
        hboxLayout.addWidget(self.le)
        self.b = QtWidgets.QToolButton(self)
        self.b.setIcon(QtGui.QIcon.fromTheme("document-open", QtGui.QIcon(":/images/document-open.png")))
        hboxLayout.addWidget(self.b)
        self.b.clicked.connect(self.bClicked)

    def bClicked(self) :
        (fname, filt) = QtWidgets.QFileDialog.getSaveFileName(self,
                dir=os.path.dirname(self.le.text()), filter=self.pattern,
                options=QtWidgets.QFileDialog.DontConfirmOverwrite)
        if fname :
            self.le.setText(os.path.relpath(fname))
        #else Cancel was hit

    def text(self) :
        return self.le.text()

    def setText(self, txt) :
        self.le.setText(txt)

    def txtChanged(self, txt) :
        self.textChanged.emit(txt)
        
#end of class FileEntry


class PassSpin(QtWidgets.QSpinBox) :

    def __init__(self, parent = None) :
        super(PassSpin, self).__init__(parent)
        self.setMinimum(0)
        self.setSpecialValueText('None')
        self.setValue(-1)
        self.setMaximumWidth(50)

# end of class PassSpin


class ConfigDialog(QtWidgets.QDialog) :

    def __init__(self, config, currTab = None, parent = None) :
        super(ConfigDialog, self).__init__(parent)
        self.config = config
        
        self.setWindowTitle("Configure project")

        vboxLayout = QtWidgets.QVBoxLayout(self)
        self.tb = QtWidgets.QToolBox(self)
        vboxLayout.addWidget(self.tb)
        self.ok = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
        self.ok.accepted.connect(self.accept)
        self.ok.rejected.connect(self.reject)
        vboxLayout.addWidget(self.ok)

        # General section
        self.general = QtWidgets.QWidget(self.tb)
        genGridLay = QtWidgets.QGridLayout(self.general)
#        self.general_vb.setVerticalSpacing(0)
        self.general_font = FileEntry(self.general, configval(config, 'main', 'font'), 'Font Files (*.ttf)')
        genGridLay.addWidget(QtWidgets.QLabel('Font File:'), 0, 0)
        genGridLay.addWidget(self.general_font, 0, 1, 1, 2)
        self.general_gdl = FileEntry(self.general, configval(config, 'build', 'gdlfile'), 'GDL Files (*.gdl)')
        genGridLay.addWidget(QtWidgets.QLabel('GDL File:'), 1, 0)
        genGridLay.addWidget(self.general_gdl, 1, 1, 1, 2)
        self.general_tests = FileEntry(self.general, configval(config, 'main', 'testsfile'), 'Tests Lists (*.xml)')
        genGridLay.addWidget(QtWidgets.QLabel('Tests File:'), 2, 0)
        genGridLay.addWidget(self.general_tests, 2, 1, 1, 2)
        self.general_rtl = QtWidgets.QCheckBox()
        self.general_rtl.setChecked(configintval(config, 'main', 'defaultrtl'))
        genGridLay.addWidget(QtWidgets.QLabel('Default RTL'), 3, 0)
        genGridLay.addWidget(self.general_rtl, 3, 1)
        genGridLay.setRowStretch(4, 1)  # 4 = total number of rows used
        self.tb.addItem(self.general, "General")

        # Build section
        # column 0 = main sub-section labels, column 1 - indented labels, column 2 = control for main sub-section,
        # column 3 = controls for indented stuff
        self.build = QtWidgets.QWidget(self.tb)
        buildGridLo = QtWidgets.QGridLayout(self.build)
        
        # makegdl / attachment point generation
        #self.build_apxml = FileEntry(self.general, configval(config, 'main', 'ap'), 'AP Files (*.xml)')
        #self.build_apxml.textChanged.connect(self.apChanged)
        #buildGridLo.addWidget(QtWidgets.QLabel('Attachment point database:'), 0, 0, 1, 2) # cols 0-1
        #buildGridLo.addWidget(self.build_apxml, 0, 2)

        self.build_gdlinc = FileEntry(self.general, configval(config, 'build', 'makegdlfile'), 'GDL Files (*.gdl *.gdh)')
        self.build_gdlinc.textChanged.connect(self.apChanged)
        buildGridLo.addWidget(QtWidgets.QLabel('Autogenerated GDL file:'), 0, 0, 1, 2) # cols 0-1
        buildGridLo.addWidget(self.build_gdlinc, 0, 2)
        
        # sub-controls for AP generation, enabled or disabled as a unit
        self.build_apctrls = QtWidgets.QWidget(self.build)
        buildGridLo.addWidget(self.build_apctrls, 1, 0, 4, 3)  # 4 rows
        apGridLo = QtWidgets.QGridLayout(self.build_apctrls)
        apGridLo.setColumnMinimumWidth(0, 125) # Make the columns in this control match tweakGridLo !!!
        apGridLo.setColumnMinimumWidth(1, 20)
        
        self.build_makegdlcmd = QtWidgets.QLineEdit(self.build_apctrls)
        self.build_makegdlcmd.setMinimumWidth(250)
        self.build_makegdlcmd.setText(configval(config, 'build', 'makegdlcmd'))
        self.build_makegdlcmd.setToolTip('External make-gdl command: %a=AP Database, %f=Font File, %g=Generated GDL File,\n    %i=included GDL file %p=positioning pass number')
        apGridLo.addWidget(QtWidgets.QLabel('Make GDL Command:'), 1, 0)
        apGridLo.addWidget(self.build_makegdlcmd, 1, 2, 1, 3) # cols 1-3
        
        #self.build_gdlinc = FileEntry(self.build_apctrls, configval(config, 'build', 'makegdlfile'), 'GDL Files (*.gdl *.gdh)')
        #self.build_gdlinc.setMinimumWidth(250)
        #apGridLo.addWidget(QtWidgets.QLabel('Autogenerated GDL file:'), 2, 0, 1, 2) # cols 0-1
        #apGridLo.addWidget(self.build_gdlinc, 2, 2, 1, 2) # cols 2-3
        
        self.build_apxml = FileEntry(self.build_apctrls, configval(config, 'main', 'ap'), 'AP Files (*.xml)')
        #self.build_apxml.setMinimumWidth(250) - make the dialog scroll horizontally
        apGridLo.addWidget(QtWidgets.QLabel('Attachment point database:'), 2, 0, 1, 2) # cols 0-1
        apGridLo.addWidget(self.build_apxml, 2, 2, 1, 2) # cols 2-3
        
        self.build_apronly = QtWidgets.QCheckBox()
        self.build_apronly.setChecked(configintval(config, 'build', 'apronly'))
        self.build_apronly.setDisabled(True)  # mechanism to update AP file from Graide is not reliable
        apGridLo.addWidget(QtWidgets.QLabel('AP Database is read-only:'), 3, 0, 1, 2) # cols 0-1
        apGridLo.addWidget(self.build_apronly, 3, 2)
        
        self.build_att = PassSpin(self.build_apctrls)
        attpass = configval(config, 'build', 'attpass')
        if attpass :
            self.build_att.setValue(int(attpass))
        apGridLo.addWidget(QtWidgets.QLabel('Attachment positioning pass:'), 4, 0, 1, 2) # cols 0-1
        apGridLo.addWidget(self.build_att, 4, 2)
        
        #if not self.build_apxml.text() :
        if not self.build_gdlinc.text() :
            self.build_apctrls.setEnabled(False)

        # Compiler executable
        #self.build_grcexec = QtWidgets.QLineEdit(self.build)
        #self.build_grcexec.setText(configval(config, 'build', 'grcexecutable'))
        self.build_grcexec = FileEntry(self.general, configval(config, 'build', 'grcexecutable'), 'All files (*.*)')
        self.build_grcexec.setToolTip('Graphite compiler executable; may be a separate batch file or shell script')
        buildGridLo.addWidget(QtWidgets.QLabel('Compiler executable:'), 5, 0, 1, 2) # cols 0-1
        buildGridLo.addWidget(self.build_grcexec, 5, 2)

        # Ignore warnings
        self.build_noWarning = QtWidgets.QLineEdit(self.build_apctrls)
        self.build_noWarning.setMinimumWidth(300)
        if config.has_option('build', 'ignorewarnings') :
            self.build_noWarning.setText(configval(config, 'build', 'ignorewarnings'))
        else :
            self.build_noWarning.setText("510,3521")  # warnings to ignore by default
        self.build_noWarning.setToolTip('List warning numbers to be ignored, or \'none\'')
        buildGridLo.addWidget(QtWidgets.QLabel('Ignore warnings:'), 6, 0)
        buildGridLo.addWidget(self.build_noWarning, 6, 2, 1, 3) # cols 1-3
             
        # Tweaking controls
{'fraction_non_alphanumeric': 0.11395398838959364, 'fraction_numerical': 0.018275639647387657, 'mean_word_length': 3.066666666666667, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, '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': '7420410', 'n_tokens_mistral': 6405, 'n_tokens_neox': 6036, 'n_words': 1340}