File indexing completed on 2024-05-19 12:55:53
0001 /*************************************************************************** 0002 Copyright (C) 2005 by Holger Danielsson (holger.danielsson@t-online.de) 0003 2010-2014 by Michel Ludwig (michel.ludwig@kdemail.net) 0004 ***************************************************************************/ 0005 0006 /*************************************************************************** 0007 * * 0008 * This program is free software; you can redistribute it and/or modify * 0009 * it under the terms of the GNU General Public License as published by * 0010 * the Free Software Foundation; either version 2 of the License, or * 0011 * (at your option) any later version. * 0012 * * 0013 ***************************************************************************/ 0014 0015 // kommandos mit weiteren Parametern 0016 0017 #include "dialogs/latexcommanddialog.h" 0018 0019 #include <QCheckBox> 0020 #include <QGroupBox> 0021 #include <QHBoxLayout> 0022 #include <QLabel> 0023 #include <QLayout> 0024 #include <QRegularExpression> 0025 #include <QTreeWidget> 0026 #include <QValidator> 0027 #include <QVBoxLayout> 0028 0029 #include <KComboBox> 0030 #include <KConfig> 0031 #include <QIcon> 0032 #include <QLineEdit> 0033 #include <KMessageBox> 0034 #include <QPushButton> 0035 #include <QTabWidget> 0036 #include <KConfigGroup> 0037 #include <QDialogButtonBox> 0038 0039 #include "kileconfig.h" 0040 #include "kiledebug.h" 0041 #include "latexcmd.h" 0042 0043 namespace KileDialog 0044 { 0045 0046 // BEGIN NewLatexCommand 0047 0048 NewLatexCommand::NewLatexCommand(QWidget *parent, const QString &caption, 0049 const QString &groupname, QTreeWidgetItem *lvitem, 0050 KileDocument::CmdAttribute cmdtype, 0051 QMap<QString, bool> *dict) 0052 : QDialog(parent), m_dict(dict) 0053 { 0054 setWindowTitle(caption); 0055 setModal(true); 0056 QWidget *mainWidget = new QWidget(this); 0057 QVBoxLayout *mainLayout = new QVBoxLayout; 0058 setLayout(mainLayout); 0059 mainLayout->addWidget(mainWidget); 0060 0061 // 'add' is only allowed, if the QTreeWidgetItem is defined 0062 m_addmode = (lvitem == 0); 0063 m_envmode = (cmdtype < KileDocument::CmdAttrLabel); 0064 m_cmdType = cmdtype; 0065 0066 // set modes for input dialog 0067 // AMS Math Tab List Verb Label Ref Cit Inc 0068 // MathOrTab + + + 0069 // Option + + + + + + + + 0070 // Parameter + + + + + + + + 0071 0072 m_useMathOrTab = false; 0073 m_useOption = m_useParameter = true; 0074 if (cmdtype == KileDocument::CmdAttrAmsmath || cmdtype == KileDocument::CmdAttrMath || cmdtype == KileDocument::CmdAttrTabular) { 0075 m_useMathOrTab = true; 0076 } 0077 else { 0078 if(cmdtype == KileDocument::CmdAttrVerbatim) { 0079 m_useParameter = false; 0080 } 0081 } 0082 0083 QWidget *page = new QWidget(this); 0084 mainLayout->addWidget(page); 0085 0086 // layout 0087 QVBoxLayout *vbox = new QVBoxLayout(); 0088 vbox->setContentsMargins(0, 0, 0, 0); 0089 page->setLayout(vbox); 0090 0091 QLabel *label1 = new QLabel(page); 0092 mainLayout->addWidget(label1); 0093 0094 QGroupBox* group = new QGroupBox(i18n("Attributes"), page); 0095 mainLayout->addWidget(group); 0096 QGridLayout *grid = new QGridLayout(); 0097 group->setLayout(grid); 0098 0099 QLabel *label2 = new QLabel(i18n("Group:"), group); 0100 QLabel *label3 = new QLabel(i18n("&Name:"), group); 0101 QLabel *grouplabel = new QLabel(groupname, group); 0102 QLabel *label4 = new QLabel(i18n("Include *-&version:"), group); 0103 m_edName = new QLineEdit(group); 0104 m_chStarred = new QCheckBox(group); 0105 0106 grid->addWidget(label2, 0, 0); 0107 grid->addWidget(grouplabel, 0, 2); 0108 grid->addWidget(label3, 1, 0); 0109 grid->addWidget(m_edName, 1, 2); 0110 grid->addWidget(label4, 2, 0); 0111 grid->addWidget(m_chStarred, 2, 2); 0112 0113 label3->setBuddy(m_edName); 0114 label4->setBuddy(m_chStarred); 0115 grouplabel->setWhatsThis(i18n("Name of the group, to which this environment or command belongs.")); 0116 if (m_addmode) { 0117 m_edName->setWhatsThis(i18n("Name of the new environment or command.")); 0118 } 0119 else { 0120 m_edName->setWhatsThis(i18n("Name of the environment or command to edit.")); 0121 } 0122 m_chStarred->setWhatsThis(i18n("Does this environment or command also exist in a starred version?")); 0123 0124 int currentRow = 3; 0125 if (m_useMathOrTab) { 0126 QLabel *label5 = new QLabel(i18n("\\\\ is end of &line:"), group); 0127 QLabel *label6 = new QLabel(i18n("Needs &math mode:"), group); 0128 QLabel *label7 = new QLabel(i18n("&Tabulator:"), group); 0129 m_chEndofline = new QCheckBox(group); 0130 m_chMath = new QCheckBox(group); 0131 m_coTab = new KComboBox(group); 0132 0133 grid->addWidget(label5, 3, 0); 0134 grid->addWidget(m_chEndofline, 3, 2); 0135 grid->addWidget(label6, 4, 0); 0136 grid->addWidget(m_chMath, 4, 2); 0137 grid->addWidget(label7, 5, 0); 0138 grid->addWidget(m_coTab, 5, 2); 0139 0140 label5->setBuddy(m_chEndofline); 0141 label6->setBuddy(m_chMath); 0142 label7->setBuddy(m_coTab); 0143 m_chEndofline->setWhatsThis(i18n("Shall 'Smart New Line' insert \\\\?")); 0144 m_chMath->setWhatsThis(i18n("Does this environment need math mode?")); 0145 m_coTab->setWhatsThis(i18n("Define the standard tabulator of this environment.")); 0146 0147 m_coTab->addItem(QString()); 0148 m_coTab->addItem("&"); 0149 m_coTab->addItem("&="); 0150 m_coTab->addItem("&=&"); 0151 0152 currentRow += 3; 0153 } 0154 0155 if (m_useOption) { 0156 QLabel *label8 = new QLabel(i18n("Opt&ion:"), group); 0157 m_coOption = new KComboBox(group); 0158 grid->addWidget(label8, currentRow, 0); 0159 grid->addWidget(m_coOption, currentRow, 2); 0160 0161 label8->setBuddy(m_coOption); 0162 0163 m_coOption->addItem(QString()); 0164 if (m_envmode) { 0165 m_coOption->addItem("[tcb]"); 0166 m_coOption->addItem("[lcr]"); 0167 m_coOption->setWhatsThis(i18n("Define an optional alignment parameter.")); 0168 } 0169 else { 0170 m_coOption->setWhatsThis(i18n("Does this command need an optional parameter?")); 0171 } 0172 m_coOption->addItem("[ ]"); 0173 0174 currentRow++; 0175 } 0176 0177 if(m_useParameter) { 0178 QLabel *label9 = new QLabel(i18n("&Parameter:"), group); 0179 m_coParameter = new KComboBox(group); 0180 grid->addWidget(label9, currentRow, 0); 0181 grid->addWidget(m_coParameter, currentRow, 2); 0182 0183 label9->setBuddy(m_coParameter); 0184 0185 if(m_envmode) { 0186 m_coParameter->addItem(QString()); 0187 m_coParameter->addItem("{n}"); 0188 m_coParameter->addItem("{w}"); 0189 m_coParameter->addItem("{ }"); 0190 m_coParameter->setWhatsThis(i18n("Does this environment need an additional parameter like {n} for an integer number, {w} for a width or { } for any other parameter?")); 0191 } 0192 else 0193 { 0194 m_coParameter->addItem("{ }"); 0195 // m_coParameter->addItem(QString()); 0196 m_coParameter->setWhatsThis(i18n("Does this command need an argument?")); 0197 } 0198 } 0199 0200 // stretch last row 0201 //grid->setRowStretch(maxrows-1,1); 0202 0203 // add or edit mode 0204 if(m_addmode) { // add mode 0205 QString pattern; 0206 if(m_envmode) { 0207 label1->setText(i18n("Define a new LaTeX environment:")); 0208 pattern = "[A-Za-z]+"; 0209 } 0210 else { 0211 label1->setText(i18n("Define a new LaTeX command:")); 0212 pattern = "\\\\?[A-Za-z]+"; 0213 } 0214 QRegularExpression reg(pattern); 0215 m_edName->setValidator(new QRegularExpressionValidator(reg, m_edName)); 0216 m_edName->setFocus(); 0217 } 0218 else { // edit mode 0219 // always insert name and starred attribute 0220 m_edName->setText(lvitem->text(0)); 0221 m_edName->setReadOnly(true); 0222 m_chStarred->setChecked(lvitem->text(1) == "*"); 0223 0224 int index; 0225 if (m_envmode) { // insert existing arguments for environments 0226 label1->setText(i18n("Edit a LaTeX Environment")); 0227 if(m_useMathOrTab) { 0228 m_chEndofline->setChecked(lvitem->text(2) == "\\\\"); 0229 m_chMath->setChecked(lvitem->text(3) == "$"); 0230 if ((index = m_coTab->findText(lvitem->text(4))) > -1) 0231 m_coTab->setCurrentIndex(index); 0232 } 0233 if(m_useOption) { 0234 if ((index = m_coOption->findText(lvitem->text(5))) > -1) 0235 m_coOption->setCurrentIndex(index); 0236 } 0237 if (m_useParameter) { 0238 if ((index = m_coParameter->findText(lvitem->text(6))) > -1) 0239 m_coParameter->setCurrentIndex(index); 0240 } 0241 } 0242 else { // insert existing arguments for commands 0243 label1->setText(i18n("Edit a LaTeX Command")); 0244 if (m_useOption) { 0245 if ((index = m_coOption->findText(lvitem->text(2))) > -1) { 0246 m_coOption->setCurrentIndex(index); 0247 } 0248 } 0249 if (m_useParameter) { 0250 if ((index = m_coParameter->findText(lvitem->text(3))) > -1) { 0251 m_coParameter->setCurrentIndex(index); 0252 } 0253 } 0254 } 0255 } 0256 0257 // fill vbox 0258 vbox->addWidget(label1, 0, Qt::AlignHCenter); 0259 vbox->addWidget(group); 0260 vbox->addStretch(); 0261 0262 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); 0263 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0264 okButton->setDefault(true); 0265 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0266 mainLayout->addWidget(buttonBox); 0267 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0268 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0269 connect(this, &QDialog::accepted, this, &NewLatexCommand::slotAccepted); 0270 } 0271 0272 // get all attributes of this command 0273 0274 void NewLatexCommand::getParameter(QString &name, KileDocument::LatexCmdAttributes &attr) 0275 { 0276 name = m_edName->text(); 0277 if(m_envmode == false && name.at(0) != '\\') { 0278 name.prepend('\\'); 0279 } 0280 0281 // set main attributes 0282 attr.standard = false; 0283 attr.type = m_cmdType; 0284 attr.starred = m_chStarred->isChecked(); 0285 0286 // read all atributes attributes 0287 if(m_useMathOrTab) { 0288 attr.cr = m_chEndofline->isChecked(); 0289 attr.mathmode = m_chMath->isChecked(); 0290 attr.displaymathmode = false; 0291 attr.tabulator = m_coTab->currentText(); 0292 } 0293 else { 0294 attr.cr = false; 0295 attr.mathmode = false; 0296 attr.displaymathmode = false; 0297 attr.tabulator.clear(); 0298 } 0299 0300 attr.option = (m_useOption) ? m_coOption->currentText() : QString(); 0301 attr.parameter = (m_useParameter) ? m_coParameter->currentText() : QString(); 0302 } 0303 0304 void NewLatexCommand::slotAccepted() 0305 { 0306 // check for an empty string 0307 if(m_edName->text().isEmpty()) { 0308 KMessageBox::error(this, i18n("An empty string is not allowed.")); 0309 return; 0310 } 0311 0312 QString name = m_edName->text(); 0313 if (m_envmode == false && name.at(0) != '\\') { 0314 name.prepend('\\'); 0315 } 0316 0317 if (m_addmode && m_dict->contains(name)) { 0318 QString msg = (m_envmode) ? i18n("This environment already exists.") 0319 : i18n("This command already exists."); 0320 KMessageBox::error(this, msg); 0321 return; 0322 } 0323 } 0324 //END NewLatexCommand 0325 0326 ////////////////////////////// LaTeX environments/commands dialog ////////////////////////////// 0327 0328 //BEGIN LatexCommandsDialog 0329 0330 LatexCommandsDialog::LatexCommandsDialog(KConfig *config, KileDocument::LatexCommands *commands, QWidget *parent) 0331 : QDialog(parent) 0332 , m_config(config) 0333 , m_commands(commands) 0334 , m_commandChanged(false) 0335 , m_buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::RestoreDefaults)) 0336 { 0337 setWindowTitle(i18n("LaTeX Configuration")); 0338 setLayout(new QVBoxLayout); 0339 setModal(true); 0340 0341 QWidget* widget = new QWidget(this); 0342 m_widget.setupUi(widget); 0343 0344 // read config and initialize changes (add, edit or delete an entry) 0345 readConfig(); 0346 // init listview 0347 resetListviews(); 0348 slotEnableButtons(); 0349 0350 connect(m_widget.tab, SIGNAL(currentChanged(int)), this, SLOT(slotEnableButtons())); 0351 connect(m_widget.environments, SIGNAL(itemSelectionChanged()), this, SLOT(slotEnableButtons())); 0352 connect(m_widget.commands, SIGNAL(itemSelectionChanged()), this, SLOT(slotEnableButtons())); 0353 connect(m_widget.addButton, SIGNAL(clicked()), this, SLOT(slotAddClicked())); 0354 connect(m_widget.deleteButton, SIGNAL(clicked()), this, SLOT(slotDeleteClicked())); 0355 connect(m_widget.editButton, SIGNAL(clicked()), this, SLOT(slotEditClicked())); 0356 connect(m_widget.showOnlyUserDefined, SIGNAL(clicked()), this, SLOT(slotUserDefinedClicked())); 0357 0358 for (int col = 0; col <= 6; ++col) { 0359 m_widget.environments->resizeColumnToContents(col); 0360 } 0361 for (int col = 0; col <= 3; ++col) { 0362 m_widget.commands->resizeColumnToContents(col); 0363 } 0364 0365 layout()->addWidget(widget); 0366 layout()->addWidget(m_buttonBox); 0367 0368 QPushButton* okButton = m_buttonBox->button(QDialogButtonBox::Ok); 0369 QPushButton* defaultsButton = m_buttonBox->button(QDialogButtonBox::RestoreDefaults); 0370 0371 okButton->setDefault(true); 0372 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0373 0374 connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0375 connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0376 connect(defaultsButton, &QPushButton::clicked, this, &LatexCommandsDialog::slotSetDefaults); 0377 connect(this, &QDialog::accepted, this, &LatexCommandsDialog::slotAccepted); 0378 } 0379 0380 ////////////////////////////// listview ////////////////////////////// 0381 0382 void LatexCommandsDialog::resetListviews() 0383 { 0384 m_dictCommands.clear(); 0385 m_widget.environments->clear(); 0386 m_widget.commands->clear(); 0387 0388 m_lviAmsmath = new QTreeWidgetItem(m_widget.environments, QStringList(i18n("AMS-Math"))); 0389 m_lviMath = new QTreeWidgetItem(m_widget.environments, QStringList(i18n("Math"))); 0390 m_lviList = new QTreeWidgetItem(m_widget.environments, QStringList(i18n("Lists"))); 0391 m_lviTabular = new QTreeWidgetItem(m_widget.environments, QStringList(i18n("Tabular"))); 0392 m_lviVerbatim = new QTreeWidgetItem(m_widget.environments, QStringList(i18n("Verbatim"))); 0393 0394 m_lviLabels = new QTreeWidgetItem(m_widget.commands, QStringList(i18n("Labels"))); 0395 m_lviReferences = new QTreeWidgetItem(m_widget.commands, QStringList(i18n("References"))); 0396 m_lviBibliographies = new QTreeWidgetItem(m_widget.commands, QStringList(i18n("Bibliographies"))); 0397 m_lviCitations = new QTreeWidgetItem(m_widget.commands, QStringList(i18n("Citations"))); 0398 m_lviInputs = new QTreeWidgetItem(m_widget.commands, QStringList(i18n("Includes"))); 0399 0400 QStringList list; 0401 QStringList::ConstIterator it; 0402 KileDocument::LatexCmdAttributes attr; 0403 0404 m_commands->commandList(list, KileDocument::CmdAttrNone, m_widget.showOnlyUserDefined->isChecked()); 0405 for (it = list.constBegin(); it != list.constEnd(); ++it) { 0406 if (m_commands->commandAttributes(*it, attr)) { 0407 QTreeWidgetItem *parent; 0408 switch (attr.type) { 0409 case KileDocument::CmdAttrAmsmath: 0410 parent = m_lviAmsmath; 0411 break; 0412 case KileDocument::CmdAttrMath: 0413 parent = m_lviMath; 0414 break; 0415 case KileDocument::CmdAttrList: 0416 parent = m_lviList; 0417 break; 0418 case KileDocument::CmdAttrTabular: 0419 parent = m_lviTabular; 0420 break; 0421 case KileDocument::CmdAttrVerbatim: 0422 parent = m_lviVerbatim; 0423 break; 0424 case KileDocument::CmdAttrLabel: 0425 parent = m_lviLabels; 0426 break; 0427 case KileDocument::CmdAttrReference: 0428 parent = m_lviReferences; 0429 break; 0430 case KileDocument::CmdAttrCitations: 0431 parent = m_lviCitations; 0432 break; 0433 case KileDocument::CmdAttrIncludes: 0434 parent = m_lviInputs; 0435 break; 0436 case KileDocument::CmdAttrBibliographies: 0437 parent = m_lviBibliographies; 0438 break; 0439 default: 0440 continue; 0441 } 0442 setEntry(parent, *it, attr); 0443 } 0444 } 0445 } 0446 0447 LatexCommandsDialog::LVmode LatexCommandsDialog::getListviewMode() 0448 { 0449 return (m_widget.tab->currentIndex() == 0) ? lvEnvMode : lvCmdMode; 0450 } 0451 0452 KileDocument::CmdAttribute LatexCommandsDialog::getCommandMode(QTreeWidgetItem *item) 0453 { 0454 if (item == m_lviAmsmath) { 0455 return KileDocument::CmdAttrAmsmath; 0456 } 0457 if (item == m_lviMath) { 0458 return KileDocument::CmdAttrMath; 0459 } 0460 if (item == m_lviList) { 0461 return KileDocument::CmdAttrList; 0462 } 0463 if (item == m_lviTabular) { 0464 return KileDocument::CmdAttrTabular; 0465 } 0466 if (item == m_lviVerbatim) { 0467 return KileDocument::CmdAttrVerbatim; 0468 } 0469 if (item == m_lviLabels) { 0470 return KileDocument::CmdAttrLabel; 0471 } 0472 if (item == m_lviReferences) { 0473 return KileDocument::CmdAttrReference; 0474 } 0475 if (item == m_lviCitations) { 0476 return KileDocument::CmdAttrCitations; 0477 } 0478 if (item == m_lviInputs) { 0479 return KileDocument::CmdAttrIncludes; 0480 } 0481 if (item == m_lviBibliographies) { 0482 return KileDocument::CmdAttrBibliographies; 0483 } 0484 0485 return KileDocument::CmdAttrNone; 0486 } 0487 0488 bool LatexCommandsDialog::isParentItem(QTreeWidgetItem *item) 0489 { 0490 return (item == m_lviMath || 0491 item == m_lviList || 0492 item == m_lviTabular || 0493 item == m_lviVerbatim || 0494 item == m_lviLabels || 0495 item == m_lviReferences || 0496 item == m_lviCitations || 0497 item == m_lviInputs || 0498 item == m_lviBibliographies 0499 ); 0500 } 0501 0502 ////////////////////////////// entries ////////////////////////////// 0503 0504 void LatexCommandsDialog::setEntry(QTreeWidgetItem *parent, const QString &name, 0505 KileDocument::LatexCmdAttributes &attr) 0506 { 0507 // set dictionary 0508 m_dictCommands[name] = attr.standard; 0509 0510 // create an item 0511 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(name)); 0512 0513 // always set the starred entry 0514 if (attr.starred) 0515 item->setText(1, "*"); 0516 0517 // environments have more attributes 0518 if (attr.type < KileDocument::CmdAttrLabel) // environments 0519 { 0520 if (attr.cr) 0521 item->setText(2, "\\\\"); 0522 if (attr.mathmode) 0523 item->setText(3, "$"); 0524 else if (attr.displaymathmode) 0525 item->setText(3, "$$"); 0526 item->setText(4, attr.tabulator); 0527 item->setText(5, attr.option); 0528 item->setText(6, attr.parameter); 0529 } 0530 else // commands 0531 { 0532 item->setText(2, attr.option); 0533 item->setText(3, attr.parameter); 0534 } 0535 0536 for(int i = 1; i < parent->treeWidget()->columnCount(); ++i) { 0537 item->setTextAlignment(i, Qt::AlignHCenter); 0538 } 0539 } 0540 0541 void LatexCommandsDialog::getEntry(QTreeWidgetItem *item, KileDocument::LatexCmdAttributes &attr) 0542 { 0543 // always set the starred entry 0544 attr.starred = (item->text(1) == "*"); 0545 0546 // get all attributes 0547 if (item->text(0).at(0) != '\\') { // environment 0548 attr.cr = (item->text(2) == "\\\\"); 0549 attr.mathmode = (item->text(3) == "$"); 0550 attr.displaymathmode = (item->text(3) == "$$"); 0551 attr.tabulator = item->text(4); 0552 attr.option = item->text(5); 0553 attr.parameter = item->text(6); 0554 } 0555 else { // commands 0556 attr.cr = false; 0557 attr.mathmode = false; 0558 attr.displaymathmode = false; 0559 attr.tabulator.clear(); 0560 attr.option = item->text(2); 0561 attr.parameter = item->text(3); 0562 } 0563 } 0564 0565 ////////////////////////////// type of environment or command ////////////////////////////// 0566 0567 bool LatexCommandsDialog::isUserDefined(const QString &name) 0568 { 0569 return (m_dictCommands.contains(name) && m_dictCommands[name] == false); 0570 } 0571 0572 // look for user-defined environment or commands in this listview 0573 0574 bool LatexCommandsDialog::hasUserDefined(QTreeWidget *listview) 0575 { 0576 for (int i = 0; i < listview->topLevelItemCount(); ++i) { 0577 QTreeWidgetItem *tli = listview->topLevelItem(i); 0578 for (int j = 0; j < tli->childCount(); ++j) { 0579 if (isUserDefined(tli->child(j)->text(0))) { 0580 return true; 0581 } 0582 } 0583 } 0584 return false; 0585 } 0586 0587 ////////////////////////////// slots ////////////////////////////// 0588 0589 void LatexCommandsDialog::slotEnableButtons() 0590 { 0591 bool addState = false; 0592 bool deleteState = false; 0593 bool editState = false; 0594 bool resetState = false; 0595 0596 QTreeWidget *listview = (getListviewMode() == lvEnvMode) ? m_widget.environments : m_widget.commands; 0597 resetState = (hasUserDefined(listview)); 0598 0599 QTreeWidgetItem *item = listview->currentItem(); 0600 0601 if (item && item != m_lviAmsmath) 0602 { 0603 addState = isParentItem(item); 0604 if (!addState && isUserDefined(item->text(0))) 0605 { 0606 deleteState = true; 0607 editState = true; 0608 } 0609 } 0610 0611 m_widget.addButton->setEnabled(addState); 0612 m_widget.deleteButton->setEnabled(deleteState); 0613 m_widget.editButton->setEnabled(editState); 0614 m_buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(resetState); 0615 } 0616 0617 void LatexCommandsDialog::slotAddClicked() 0618 { 0619 QTreeWidget *listview; 0620 QString caption; 0621 0622 if (getListviewMode() == lvEnvMode) { 0623 listview = m_widget.environments; 0624 caption = i18n("LaTeX Environments"); 0625 } 0626 else { 0627 listview = m_widget.commands; 0628 caption = i18n("LaTeX Commands"); 0629 } 0630 0631 QTreeWidgetItem *item = listview->currentItem(); 0632 if (item && isParentItem(item)) { 0633 // get current command type 0634 KileDocument::CmdAttribute type = getCommandMode(item); 0635 if (type == KileDocument::CmdAttrNone) { 0636 KILE_DEBUG_MAIN << "\tLatexCommandsDialog error: no item in slotAddClicked() (" << item->text(0) << ")" << Qt::endl; 0637 return; 0638 } 0639 0640 // add a new environment or command 0641 NewLatexCommand *dialog = new NewLatexCommand(this, caption, item->text(0), Q_NULLPTR, type, &m_dictCommands); 0642 if (dialog->exec() == QDialog::Accepted) { 0643 m_commandChanged = true; 0644 0645 // insert new item with attributes 0646 QString name; 0647 KileDocument::LatexCmdAttributes attr; 0648 dialog->getParameter(name, attr); 0649 setEntry(item, name, attr); 0650 // open this parent item 0651 if (!item->isExpanded()) { 0652 item->setExpanded(true); 0653 } 0654 slotEnableButtons(); 0655 } 0656 delete dialog; 0657 } 0658 } 0659 0660 void LatexCommandsDialog::slotDeleteClicked() 0661 { 0662 QTreeWidget *listview; 0663 QString message; 0664 0665 if (getListviewMode() == lvEnvMode) { 0666 listview = m_widget.environments; 0667 message = i18n("Do you want to delete this environment?"); 0668 } 0669 else { 0670 listview = m_widget.commands; 0671 message = i18n("Do you want to delete this command?"); 0672 } 0673 0674 QTreeWidgetItem *item = listview->currentItem(); 0675 if (item && !isParentItem(item)) { 0676 if (KMessageBox::warningContinueCancel(this, message, i18n("Delete")) == KMessageBox::Continue) { 0677 m_commandChanged = true; 0678 0679 if (isUserDefined(item->text(0))) 0680 m_dictCommands.remove(item->text(0)); 0681 delete item; 0682 slotEnableButtons(); 0683 } 0684 } 0685 } 0686 0687 void LatexCommandsDialog::slotEditClicked() 0688 { 0689 QTreeWidget *listview; 0690 QString caption; 0691 0692 if (getListviewMode() == lvEnvMode) 0693 { 0694 listview = m_widget.environments; 0695 caption = i18n("LaTeX Environment"); 0696 } 0697 else 0698 { 0699 listview = m_widget.commands; 0700 caption = i18n("LaTeX Commands"); 0701 } 0702 0703 QTreeWidgetItem *item = listview->currentItem(); 0704 if (item && !isParentItem(item)) { 0705 QTreeWidgetItem *parentitem = item->parent(); 0706 if (parentitem) { 0707 // get current command type 0708 KileDocument::CmdAttribute type = getCommandMode(parentitem); 0709 if (type == KileDocument::CmdAttrNone) { 0710 KILE_DEBUG_MAIN << "\tLatexCommandsDialog error: no item in slotAddClicked() (" << item->text(0) << ")" << Qt::endl; 0711 return; 0712 } 0713 0714 // edit a new environment or command 0715 NewLatexCommand *dialog = new NewLatexCommand(this, caption, parentitem->text(0), item, type, &m_dictCommands); 0716 if (dialog->exec() == QDialog::Accepted) { 0717 m_commandChanged = true; 0718 0719 // delete old item 0720 delete item; 0721 // insert new item with changed attributes 0722 QString name; 0723 KileDocument::LatexCmdAttributes attr; 0724 dialog->getParameter(name, attr); 0725 setEntry(parentitem, name, attr); 0726 } 0727 delete dialog; 0728 } 0729 } 0730 } 0731 0732 void LatexCommandsDialog::slotUserDefinedClicked() 0733 { 0734 bool states[9]; 0735 0736 getListviewStates(states); 0737 resetListviews(); 0738 setListviewStates(states); 0739 } 0740 0741 0742 void LatexCommandsDialog::slotAccepted() 0743 { 0744 // OK-Button clicked, we have to look for user-defined environments/commands 0745 // save checkbox for user-defined commands 0746 KileConfig::setShowUserCommands(m_widget.showOnlyUserDefined->isChecked()); 0747 0748 // write config entries for environments and commands 0749 writeConfig(m_widget.environments, m_commands->envGroupName(), true); 0750 writeConfig(m_widget.commands, m_commands->cmdGroupName(), false); 0751 m_config->sync(); 0752 0753 // reset known LaTeX environments and commands 0754 m_commands->resetCommands(); 0755 0756 // save if there is a change in user-defined commands and environments 0757 KileConfig::setCompleteChangedCommands(m_commandChanged); 0758 } 0759 0760 void LatexCommandsDialog::slotSetDefaults() 0761 { 0762 const QString text = (getListviewMode() == lvEnvMode) 0763 ? i18n("All your 'environment' settings will be overwritten with the default settings, are you sure you want to continue?") 0764 : i18n("All your 'command' settings will be overwritten with the default settings, are you sure you want to continue?"); 0765 if (KMessageBox::warningContinueCancel(this, text) == KMessageBox::Continue) { 0766 if (getListviewMode() == lvEnvMode) { 0767 resetEnvironments(); 0768 } 0769 else { 0770 resetCommands(); 0771 } 0772 slotEnableButtons(); 0773 } 0774 } 0775 0776 ////////////////////////////// read/write config ////////////////////////////// 0777 0778 void LatexCommandsDialog::readConfig() 0779 { 0780 // read checkbox for user-defined commands 0781 m_widget.showOnlyUserDefined->setChecked(KileConfig::showUserCommands()); 0782 } 0783 0784 void LatexCommandsDialog::writeConfig(QTreeWidget *listview, const QString &groupname, bool env) 0785 { 0786 // first delete old entries 0787 if (m_config->hasGroup(groupname)) { 0788 m_config->deleteGroup(groupname); 0789 } 0790 0791 // prepare for new entries 0792 KConfigGroup group = m_config->group(groupname); 0793 0794 // now get all attributes 0795 KileDocument::LatexCmdAttributes attr; 0796 attr.standard = false; 0797 0798 int nrOfdefinedCommands = 0; 0799 0800 // scan the listview for non standard entries 0801 for (int i = 0; i < listview->topLevelItemCount(); ++i) { 0802 QTreeWidgetItem *cur = listview->topLevelItem(i); 0803 // get the type of the parent entry 0804 attr.type = getCommandMode(cur); 0805 if (attr.type == KileDocument::CmdAttrNone) 0806 { 0807 KILE_DEBUG_MAIN << "\tLatexCommandsDialog error: no parent item (" << cur->text(0) << ")" << Qt::endl; 0808 continue; 0809 } 0810 0811 // look for children 0812 for (int j = 0; j < cur->childCount(); ++j) { 0813 QTreeWidgetItem *curchild = cur->child(j); 0814 QString key = curchild->text(0); 0815 if (isUserDefined(key)) 0816 { 0817 getEntry(curchild, attr); 0818 QString value = m_commands->configString(attr, env); 0819 KILE_DEBUG_MAIN << "\tLatexCommandsDialog write config: " << key << " --> " << value << Qt::endl; 0820 if (!value.isEmpty()) { 0821 group.writeEntry("Command" + QString::number(nrOfdefinedCommands), key); 0822 group.writeEntry("Parameters" + QString::number(nrOfdefinedCommands), value); 0823 ++nrOfdefinedCommands; 0824 } 0825 } 0826 } 0827 } 0828 if(nrOfdefinedCommands > 0) { 0829 group.writeEntry("Number of Commands", nrOfdefinedCommands); 0830 } 0831 } 0832 0833 ////////////////////////////// reset environments and commands ////////////////////////////// 0834 0835 // delete all user-defined environments 0836 0837 void LatexCommandsDialog::resetEnvironments() 0838 { 0839 // remember current states 0840 bool states[9]; 0841 getListviewStates(states); 0842 0843 // delete user-defined commands and re-read the list 0844 if (m_config->hasGroup(m_commands->envGroupName())) 0845 m_config->deleteGroup(m_commands->envGroupName()); 0846 m_commands->resetCommands(); 0847 0848 // reset Listview and set old states again (if possible) 0849 resetListviews(); 0850 setListviewStates(states); 0851 } 0852 0853 // delete all user-defined commands 0854 0855 void LatexCommandsDialog::resetCommands() 0856 { 0857 // remember current states 0858 bool states[9]; 0859 getListviewStates(states); 0860 0861 // delete user-defined commands and re-read the list 0862 if (m_config->hasGroup(m_commands->cmdGroupName())) 0863 m_config->deleteGroup(m_commands->cmdGroupName()); 0864 m_commands->resetCommands(); 0865 0866 // reset Listview and set old states again (if possible) 0867 resetListviews(); 0868 setListviewStates(states); 0869 } 0870 0871 // states of all parent items 0872 0873 void LatexCommandsDialog::getListviewStates(bool states[]) 0874 { 0875 states[0] = m_lviAmsmath->isExpanded(); 0876 states[1] = m_lviMath->isExpanded(); 0877 states[2] = m_lviList->isExpanded(); 0878 states[3] = m_lviTabular->isExpanded(); 0879 states[4] = m_lviVerbatim->isExpanded(); 0880 0881 states[5] = m_lviLabels->isExpanded(); 0882 states[6] = m_lviReferences->isExpanded(); 0883 states[7] = m_lviCitations->isExpanded(); 0884 states[8] = m_lviInputs->isExpanded(); 0885 } 0886 0887 void LatexCommandsDialog::setListviewStates(bool states[]) 0888 { 0889 m_lviAmsmath->setExpanded(states[0]); 0890 m_lviMath->setExpanded(states[1]); 0891 m_lviList->setExpanded(states[2]); 0892 m_lviTabular->setExpanded(states[3]); 0893 m_lviVerbatim->setExpanded(states[4]); 0894 0895 m_lviLabels->setExpanded(states[5]); 0896 m_lviReferences->setExpanded(states[6]); 0897 m_lviCitations->setExpanded(states[7]); 0898 m_lviInputs->setExpanded(states[8]); 0899 } 0900 0901 //END LatexCommandsDialog 0902 0903 0904 }