File indexing completed on 2024-05-05 17:15:08

0001 /**************************************************************************************
0002     begin                : Feb 24 2007
0003     copyright            : 2007 by Holger Danielsson (holger.danielsson@versanet.de)
0004                            2015 by Andreas Cord-Landwerh (cordlandwehr@kde.org)
0005 ***************************************************************************************/
0006 
0007 /***************************************************************************
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or modify  *
0010  *   it under the terms of the GNU General Public License as published by  *
0011  *   the Free Software Foundation; either version 2 of the License, or     *
0012  *   (at your option) any later version.                                   *
0013  *                                                                         *
0014  ***************************************************************************/
0015 
0016 #include "dialogs/abbreviationinputdialog.h"
0017 
0018 #include "kiledebug.h"
0019 
0020 #include <KLocalizedString>
0021 #include <QLabel>
0022 #include <QLayout>
0023 #include <QLineEdit>
0024 #include <QRegularExpression>
0025 #include <QFormLayout>
0026 #include <QDialogButtonBox>
0027 #include <QPushButton>
0028 
0029 namespace KileDialog {
0030 
0031 //////////////////// add/edit abbreviation ////////////////////
0032 
0033 //TODO dialog has non-standard logic
0034 // calling code is reacting on the result (i.e., if dialog accpepts or not) and then uses the
0035 // changed data accordingling; this should be changed
0036 
0037 AbbreviationInputDialog::AbbreviationInputDialog(KileWidget::AbbreviationView *listview, QTreeWidgetItem *item, int mode, const char *name)
0038     : QDialog(listview)
0039     , m_listview(listview)
0040     , m_buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel))
0041     , m_abbrevItem(item)
0042     , m_mode(mode)
0043 {
0044     setWindowTitle(i18n("Add Abbreviation"));
0045     setModal(true);
0046     setObjectName(name);
0047     setMinimumWidth(350);
0048 
0049     QFormLayout *mainLayout = new QFormLayout;
0050     setLayout(mainLayout);
0051 
0052     if(m_mode == KileWidget::AbbreviationView::ALVedit) {
0053         setWindowTitle( i18n("Edit Abbreviation") );
0054         m_abbrev = m_abbrevItem->text(KileWidget::AbbreviationView::ALVabbrev);
0055         m_expansion = m_abbrevItem->text(KileWidget::AbbreviationView::ALVexpansion);
0056     }
0057 
0058     m_leAbbrev = new QLineEdit(m_abbrev, this);
0059     m_leExpansion = new QLineEdit(m_expansion, this);
0060     QLabel *labelAbbreviation = new QLabel(i18n("&Abbreviation:"), this);
0061     labelAbbreviation->setBuddy(m_leAbbrev);
0062     QLabel *labelExpanded = new QLabel(i18n("&Expanded Text:"), this);
0063     labelExpanded->setBuddy(m_leExpansion);
0064 
0065     mainLayout->addRow(labelAbbreviation, m_leAbbrev);
0066     mainLayout->addRow(labelExpanded, m_leExpansion);
0067 
0068     static QRegularExpression reg("[a-zA-Z0-9]+");
0069     QRegularExpressionValidator *abbrevValidator = new QRegularExpressionValidator(reg, this);
0070     m_leAbbrev->setValidator(abbrevValidator);
0071 
0072     connect(m_leAbbrev, &QLineEdit::textChanged, this, &AbbreviationInputDialog::onTextChanged);
0073     connect(m_leExpansion, &QLineEdit::textChanged, this, &AbbreviationInputDialog::onTextChanged);
0074 
0075     onTextChanged(QString());
0076     m_leAbbrev->setFocus();
0077 
0078     QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
0079     okButton->setDefault(true);
0080     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0081     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0082     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0083     mainLayout->addWidget(m_buttonBox);
0084     okButton->setDefault(true);
0085 }
0086 
0087 AbbreviationInputDialog::~AbbreviationInputDialog()
0088 {
0089 }
0090 
0091 void AbbreviationInputDialog::abbreviation(QString &abbrev, QString &expansion)
0092 {
0093     abbrev = m_leAbbrev->text();
0094     expansion = m_leExpansion->text().trimmed();
0095 }
0096 
0097 void AbbreviationInputDialog::onTextChanged(const QString &)
0098 {
0099     bool state = (m_mode == KileWidget::AbbreviationView::ALVadd)
0100                  ? !m_listview->findAbbreviation( m_leAbbrev->text()) : true;
0101     state = state && !m_leAbbrev->text().isEmpty() && !m_leExpansion->text().isEmpty();
0102     m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
0103 }
0104 }
0105