File indexing completed on 2024-05-19 16:08:11
0001 /* This file is part of the KDE project 0002 Copyright (C) 2002-2003 Ariya Hidayat <ariya@kde.org> 0003 (C) 2001-2003 Laurent Montel <montel@kde.org> 0004 (C) 1998, 1999 Torben Weis <weis@kde.org> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 Boston, MA 02110-1301, USA. 0020 */ 0021 0022 // Local 0023 #include "ListDialog.h" 0024 0025 #include <QGridLayout> 0026 #include <QLabel> 0027 #include <QListWidget> 0028 #include <QPushButton> 0029 0030 #include <KSharedConfig> 0031 #include <kmessagebox.h> 0032 #include <ktextedit.h> 0033 0034 #include "commands/AutoFillCommand.h" 0035 #include "Localization.h" 0036 0037 using namespace Calligra::Sheets; 0038 0039 class ListDialog::Private 0040 { 0041 public: 0042 KSharedConfigPtr config; 0043 0044 QListWidget* list; 0045 KTextEdit* textEdit; 0046 QPushButton* addButton; 0047 QPushButton* cancelButton; 0048 QPushButton* removeButton; 0049 QPushButton* newButton; 0050 QPushButton* modifyButton; 0051 QPushButton* copyButton; 0052 bool changed; 0053 }; 0054 0055 static const int numBuiltinLists = 4; 0056 0057 ListDialog::ListDialog(QWidget* parent) 0058 : KoDialog(parent) 0059 , d(new Private) 0060 { 0061 setCaption(i18n("Custom Lists")); 0062 setButtons(Ok | Cancel); 0063 setModal(true); 0064 0065 QWidget* page = new QWidget(this); 0066 setMainWidget(page); 0067 0068 QGridLayout *grid1 = new QGridLayout(page); 0069 0070 QLabel *lab = new QLabel(page); 0071 lab->setText(i18n("List:")); 0072 grid1->addWidget(lab, 0, 0); 0073 0074 d->list = new QListWidget(page); 0075 grid1->addWidget(d->list, 1, 0, 7, 1); 0076 0077 lab = new QLabel(page); 0078 lab->setText(i18n("Entry:")); 0079 grid1->addWidget(lab, 0, 1); 0080 0081 d->textEdit = new KTextEdit(page); 0082 grid1->addWidget(d->textEdit, 1, 1, 7, 1); 0083 0084 d->addButton = new QPushButton(i18n("Add"), page); 0085 d->addButton->setEnabled(false); 0086 grid1->addWidget(d->addButton, 1, 2); 0087 0088 d->cancelButton = new QPushButton(i18n("Cancel"), page); 0089 d->cancelButton->setEnabled(false); 0090 grid1->addWidget(d->cancelButton, 2, 2); 0091 0092 d->newButton = new QPushButton(i18n("New"), page); 0093 grid1->addWidget(d->newButton, 3, 2); 0094 0095 d->removeButton = new QPushButton(i18n("Remove"), page); 0096 grid1->addWidget(d->removeButton, 4, 2); 0097 0098 d->modifyButton = new QPushButton(i18n("Modify"), page); 0099 grid1->addWidget(d->modifyButton, 5, 2); 0100 0101 d->copyButton = new QPushButton(i18n("Copy"), page); 0102 grid1->addWidget(d->copyButton, 6, 2); 0103 0104 connect(d->addButton, SIGNAL(clicked()), this, SLOT(slotAdd())); 0105 connect(d->cancelButton, SIGNAL(clicked()), this, SLOT(slotCancel())); 0106 connect(d->newButton, SIGNAL(clicked()), this, SLOT(slotNew())); 0107 connect(d->removeButton, SIGNAL(clicked()), this, SLOT(slotRemove())); 0108 connect(d->modifyButton, SIGNAL(clicked()), this, SLOT(slotModify())); 0109 connect(d->copyButton, SIGNAL(clicked()), this, SLOT(slotCopy())); 0110 connect(d->list, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slotDoubleClicked())); 0111 connect(d->list, SIGNAL(currentRowChanged(int)), this, SLOT(slotCurrentRowChanged(int))); 0112 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk())); 0113 0114 init(); 0115 d->textEdit->setEnabled(false); 0116 d->modifyButton->setEnabled(false); 0117 slotCurrentRowChanged(0); 0118 resize(600, 250); 0119 d->changed = false; 0120 } 0121 0122 ListDialog::~ListDialog() 0123 { 0124 delete d; 0125 } 0126 0127 void ListDialog::slotCurrentRowChanged(int row) 0128 { 0129 //we can't remove the first built-in items 0130 const bool state = row >= numBuiltinLists; 0131 d->removeButton->setEnabled(state); 0132 d->copyButton->setEnabled(row >= 0); 0133 d->textEdit->setEnabled(false); 0134 d->textEdit->clear(); 0135 } 0136 0137 void ListDialog::init() 0138 { 0139 QString month = 0140 i18n("January") + ", " + 0141 i18n("February") + ", " + 0142 i18n("March") + ", " + 0143 i18n("April") + ", " + 0144 i18n("May") + ", " + 0145 i18n("June") + ", " + 0146 i18n("July") + ", " + 0147 i18n("August") + ", " + 0148 i18n("September") + ", " + 0149 i18n("October") + ", " + 0150 i18n("November") + ", " + 0151 i18n("December"); 0152 QStringList lst; 0153 lst.append(month); 0154 0155 QString smonth = 0156 i18n("Jan") + ", " + 0157 i18n("Feb") + ", " + 0158 i18n("Mar") + ", " + 0159 i18n("Apr") + ", " + 0160 i18n("May") + ", " + 0161 i18n("Jun") + ", " + 0162 i18n("Jul") + ", " + 0163 i18n("Aug") + ", " + 0164 i18n("Sep") + ", " + 0165 i18n("Oct") + ", " + 0166 i18n("Nov") + ", "; 0167 i18n("Dec"); 0168 lst.append(smonth); 0169 0170 QString day = 0171 i18n("Monday") + ", " + 0172 i18n("Tuesday") + ", " + 0173 i18n("Wednesday") + ", " + 0174 i18n("Thursday") + ", " + 0175 i18n("Friday") + ", " + 0176 i18n("Saturday") + ", "+ 0177 i18n("Sunday"); 0178 lst.append(day); 0179 0180 QString sday = 0181 i18n("Mon") + ", " + 0182 i18n("Tue") + ", " + 0183 i18n("Wed") + ", " + 0184 i18n("Thu") + ", " + 0185 i18n("Fri") + ", " + 0186 i18n("Sat") + ", " + 0187 i18n("Sun"); 0188 lst.append(sday); 0189 0190 d->config = KSharedConfig::openConfig(); 0191 const QStringList other = d->config->group("Parameters").readEntry("Other list", QStringList()); 0192 QString tmp; 0193 for (QStringList::ConstIterator it = other.begin(); it != other.end(); ++it) { 0194 if ((*it) != "\\") { 0195 tmp += (*it) + ", "; 0196 } else if (it != other.begin()) { 0197 tmp = tmp.left(tmp.length() - 2); 0198 lst.append(tmp); 0199 tmp.clear(); 0200 } 0201 } 0202 d->list->addItems(lst); 0203 } 0204 0205 void ListDialog::slotDoubleClicked() 0206 { 0207 //we can't modify the first built-in items 0208 if (d->list->currentRow() < numBuiltinLists) { 0209 return; 0210 } 0211 const QStringList result = d->list->currentItem()->text().split(", ", QString::SkipEmptyParts); 0212 d->textEdit->setText(result.join(QChar('\n'))); 0213 d->textEdit->setEnabled(true); 0214 d->modifyButton->setEnabled(true); 0215 } 0216 0217 void ListDialog::slotAdd() 0218 { 0219 d->addButton->setEnabled(false); 0220 d->cancelButton->setEnabled(false); 0221 d->newButton->setEnabled(true); 0222 d->list->setEnabled(true); 0223 const QStringList tmp = d->textEdit->toPlainText().split(QChar('\n'), QString::SkipEmptyParts); 0224 if (!tmp.isEmpty()) { 0225 d->list->addItem(tmp.join(", ")); 0226 } 0227 0228 d->textEdit->setText(""); 0229 d->textEdit->setEnabled(false); 0230 d->textEdit->setFocus(); 0231 slotCurrentRowChanged(0); 0232 d->changed = true; 0233 } 0234 0235 void ListDialog::slotCancel() 0236 { 0237 d->textEdit->setText(""); 0238 slotAdd(); 0239 } 0240 0241 void ListDialog::slotNew() 0242 { 0243 d->addButton->setEnabled(true); 0244 d->cancelButton->setEnabled(true); 0245 d->newButton->setEnabled(false); 0246 d->removeButton->setEnabled(false); 0247 d->modifyButton->setEnabled(false); 0248 d->copyButton->setEnabled(false); 0249 d->list->setEnabled(false); 0250 d->textEdit->setText(""); 0251 d->textEdit->setEnabled(true); 0252 d->textEdit->setFocus(); 0253 } 0254 0255 void ListDialog::slotRemove() 0256 { 0257 if (!d->list->isEnabled() || d->list->currentRow() == -1) { 0258 return; 0259 } 0260 //don't remove the first built-in items 0261 if (d->list->currentRow() < numBuiltinLists) { 0262 return; 0263 } 0264 int ret = KMessageBox::warningContinueCancel(this, 0265 i18n("Do you really want to remove this list?"), 0266 i18n("Remove List"), KStandardGuiItem::del()); 0267 if (ret == Cancel) { // response = No 0268 return; 0269 } 0270 delete d->list->takeItem(d->list->currentRow()); 0271 d->textEdit->setEnabled(false); 0272 d->textEdit->setText(""); 0273 if (d->list->count() <= numBuiltinLists) { 0274 d->removeButton->setEnabled(false); 0275 } 0276 d->changed = true; 0277 } 0278 0279 void ListDialog::slotOk() 0280 { 0281 if (!d->textEdit->toPlainText().isEmpty()) { 0282 int ret = KMessageBox::warningYesNo(this, i18n("Entry area is not empty.\nDo you want to continue?")); 0283 if (ret == 4) { // response = No 0284 return; 0285 } 0286 } 0287 if (d->changed) { 0288 QStringList result; 0289 result.append("\\"); 0290 0291 //don't save the first built-in lines 0292 for (int i = numBuiltinLists - 1; i < d->list->count(); ++i) { 0293 QStringList tmp = d->list->item(i)->text().split(", ", QString::SkipEmptyParts); 0294 if (!tmp.isEmpty()) { 0295 result += tmp; 0296 result += "\\"; 0297 } 0298 } 0299 d->config->group("Parameters").writeEntry("Other list", result); 0300 //todo refresh AutoFillCommand::other 0301 // I don't know how to do for the moment 0302 delete(AutoFillCommand::other); 0303 AutoFillCommand::other = 0; 0304 } 0305 accept(); 0306 } 0307 0308 void ListDialog::slotModify() 0309 { 0310 //you can modify list but not the first built-in items 0311 if (d->list->currentRow() >= numBuiltinLists && !d->textEdit->toPlainText().isEmpty()) { 0312 const QString tmp = d->textEdit->toPlainText().split(QChar('\n'), QString::SkipEmptyParts).join(", "); 0313 d->list->insertItem(d->list->currentRow(), tmp); 0314 delete d->list->takeItem(d->list->currentRow()); 0315 0316 d->textEdit->setText(""); 0317 d->changed = true; 0318 } 0319 d->textEdit->setEnabled(false); 0320 d->modifyButton->setEnabled(false); 0321 } 0322 0323 void ListDialog::slotCopy() 0324 { 0325 if (d->list->currentRow() != -1) { 0326 d->list->addItem(d->list->currentItem()->text()); 0327 } 0328 }