File indexing completed on 2024-05-12 16:35:23
0001 /* This file is part of the KDE project 0002 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net> 0003 Copyright 2002-2003 Norbert Andres <nandres@web.de> 0004 Copyright 2002 Ariya Hidayat <ariya@kde.org> 0005 Copyright 2002 Harri Porten <porten@kde.org> 0006 Copyright 2002 John Dailey <dailey@vt.edu> 0007 Copyright 1999-2002 Laurent Montel <montel@kde.org> 0008 Copyright 2001-2002 Philipp Mueller <philipp.mueller@gmx.de> 0009 Copyright 1998-2000 Torben Weis <weis@kde.org> 0010 0011 This library is free software; you can redistribute it and/or 0012 modify it under the terms of the GNU Library General Public 0013 License as published by the Free Software Foundation; either 0014 version 2 of the License, or (at your option) any later version. 0015 0016 This library is distributed in the hope that it will be useful, 0017 but WITHOUT ANY WARRANTY; without even the implied warranty of 0018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0019 Library General Public License for more details. 0020 0021 You should have received a copy of the GNU Library General Public License 0022 along with this library; see the file COPYING.LIB. If not, write to 0023 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0024 Boston, MA 02110-1301, USA. 0025 */ 0026 0027 // Local 0028 #include "NamedAreaDialog.h" 0029 0030 // Qt 0031 #include <QGridLayout> 0032 #include <QLabel> 0033 #include <QList> 0034 #include <QHBoxLayout> 0035 #include <QVBoxLayout> 0036 #include <QListWidget> 0037 #include <QPushButton> 0038 0039 // KF5 0040 #include <kcombobox.h> 0041 #include <klineedit.h> 0042 #include <kmessagebox.h> 0043 #include <kstandardguiitem.h> 0044 0045 #include <KoCanvasBase.h> 0046 0047 // Sheets 0048 #include "Localization.h" 0049 #include "Map.h" 0050 #include "NamedAreaManager.h" 0051 #include "ui/Selection.h" 0052 #include "Sheet.h" 0053 #include "Util.h" 0054 0055 #include "commands/NamedAreaCommand.h" 0056 0057 using namespace Calligra::Sheets; 0058 0059 NamedAreaDialog::NamedAreaDialog(QWidget* parent, Selection* selection) 0060 : KoDialog(parent) 0061 , m_selection(selection) 0062 { 0063 setButtons(KoDialog::Ok | KoDialog::Close); 0064 setButtonText(KoDialog::Ok, i18n("&Select")); 0065 setCaption(i18n("Named Areas")); 0066 setModal(true); 0067 setObjectName(QLatin1String("NamedAreaDialog")); 0068 0069 QWidget* widget = new QWidget(this); 0070 setMainWidget(widget); 0071 0072 QHBoxLayout *hboxLayout = new QHBoxLayout(widget); 0073 hboxLayout->setMargin(0); 0074 0075 QVBoxLayout *vboxLayout = new QVBoxLayout(); 0076 0077 m_list = new QListWidget(this); 0078 m_list->setSortingEnabled(true); 0079 vboxLayout->addWidget(m_list); 0080 0081 m_rangeName = new QLabel(this); 0082 m_rangeName->setText(i18n("Area: %1", QString())); 0083 vboxLayout->addWidget(m_rangeName); 0084 0085 hboxLayout->addLayout(vboxLayout); 0086 0087 // list buttons 0088 QVBoxLayout *listButtonLayout = new QVBoxLayout(); 0089 0090 m_newButton = new QPushButton(i18n("&New..."), widget); 0091 listButtonLayout->addWidget(m_newButton); 0092 m_editButton = new QPushButton(i18n("&Edit..."), widget); 0093 listButtonLayout->addWidget(m_editButton); 0094 m_removeButton = new QPushButton(i18n("&Remove"), widget); 0095 listButtonLayout->addWidget(m_removeButton); 0096 listButtonLayout->addStretch(1); 0097 0098 hboxLayout->addLayout(listButtonLayout); 0099 0100 const QList<QString> namedAreas = m_selection->activeSheet()->map()->namedAreaManager()->areaNames(); 0101 for (int i = 0; i < namedAreas.count(); ++i) 0102 m_list->addItem(namedAreas[i]); 0103 0104 if (m_list->count() == 0) { 0105 enableButtonOk(false); 0106 m_removeButton->setEnabled(false); 0107 m_editButton->setEnabled(false); 0108 m_list->setCurrentRow(0); 0109 } 0110 0111 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk())); 0112 connect(this, SIGNAL(cancelClicked()), this, SLOT(slotClose())); 0113 connect(m_newButton, SIGNAL(clicked(bool)), this, SLOT(slotNew())); 0114 connect(m_editButton, SIGNAL(clicked(bool)), this, SLOT(slotEdit())); 0115 connect(m_removeButton, SIGNAL(clicked(bool)), this, SLOT(slotRemove())); 0116 connect(m_list, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(slotOk())); 0117 connect(m_list, SIGNAL(currentTextChanged(QString)), 0118 this, SLOT(displayAreaValues(QString))); 0119 0120 if (m_list->count() > 0) 0121 m_list->setCurrentItem(m_list->item(0)); 0122 0123 m_list->setFocus(); 0124 } 0125 0126 void NamedAreaDialog::displayAreaValues(QString const & areaName) 0127 { 0128 const QString regionName = m_selection->activeSheet()->map()->namedAreaManager()->namedArea(areaName).name(); 0129 m_rangeName->setText(i18n("Area: %1", regionName)); 0130 } 0131 0132 void NamedAreaDialog::slotOk() 0133 { 0134 if (m_list->count() > 0) { 0135 QListWidgetItem* item = m_list->currentItem(); 0136 Region region = m_selection->activeSheet()->map()->namedAreaManager()->namedArea(item->text()); 0137 Sheet* sheet = m_selection->activeSheet()->map()->namedAreaManager()->sheet(item->text()); 0138 if (!sheet || !region.isValid()) { 0139 return; 0140 } 0141 0142 if (sheet && sheet != m_selection->activeSheet()) 0143 m_selection->emitVisibleSheetRequested(sheet); 0144 m_selection->initialize(region); 0145 } 0146 0147 m_selection->emitModified(); 0148 accept(); 0149 } 0150 0151 void NamedAreaDialog::slotClose() 0152 { 0153 reject(); 0154 } 0155 0156 void NamedAreaDialog::slotNew() 0157 { 0158 QPointer<EditNamedAreaDialog> dialog = new EditNamedAreaDialog(this, m_selection); 0159 dialog->setCaption(i18n("New Named Area")); 0160 dialog->setRegion(*m_selection); 0161 dialog->exec(); 0162 if (dialog->result() == Rejected) 0163 return; 0164 if (dialog->areaName().isEmpty()) 0165 return; 0166 0167 m_list->addItem(dialog->areaName()); 0168 QList<QListWidgetItem*> items = m_list->findItems(dialog->areaName(), 0169 Qt::MatchExactly | Qt::MatchCaseSensitive); 0170 m_list->setCurrentItem(items.first()); 0171 displayAreaValues(dialog->areaName()); 0172 delete dialog; 0173 0174 enableButtonOk(true); 0175 m_removeButton->setEnabled(true); 0176 m_editButton->setEnabled(true); 0177 } 0178 0179 void NamedAreaDialog::slotEdit() 0180 { 0181 QListWidgetItem* item = m_list->currentItem(); 0182 if (item->text().isEmpty()) 0183 return; 0184 0185 QPointer<EditNamedAreaDialog> dialog = new EditNamedAreaDialog(this, m_selection); 0186 dialog->setCaption(i18n("Edit Named Area")); 0187 dialog->setAreaName(item->text()); 0188 dialog->exec(); 0189 if (dialog->result() == Rejected) 0190 return; 0191 0192 item->setText(dialog->areaName()); 0193 displayAreaValues(dialog->areaName()); 0194 delete dialog; 0195 } 0196 0197 void NamedAreaDialog::slotRemove() 0198 { 0199 const QString question = i18n("Do you really want to remove this named area?"); 0200 int result = KMessageBox::warningContinueCancel(this, question, i18n("Remove Named Area"), 0201 KStandardGuiItem::del()); 0202 if (result == KMessageBox::Cancel) 0203 return; 0204 0205 QListWidgetItem* item = m_list->currentItem(); 0206 0207 NamedAreaCommand* command = new NamedAreaCommand(); 0208 command->setAreaName(item->text()); 0209 command->setReverse(true); 0210 command->setSheet(m_selection->activeSheet()); 0211 if (!command->execute(m_selection->canvas())) { 0212 delete command; 0213 return; 0214 } 0215 m_list->takeItem(m_list->row(item)); 0216 0217 if (m_list->count() == 0) { 0218 enableButtonOk(false); 0219 m_removeButton->setEnabled(false); 0220 m_editButton->setEnabled(false); 0221 displayAreaValues(QString()); 0222 } else 0223 displayAreaValues(m_list->currentItem()->text()); 0224 } 0225 0226 0227 0228 EditNamedAreaDialog::EditNamedAreaDialog(QWidget* parent, Selection* selection) 0229 : KoDialog(parent) 0230 , m_selection(selection) 0231 { 0232 setButtons(Ok | Cancel); 0233 setModal(true); 0234 setObjectName(QLatin1String("EditNamedAreaDialog")); 0235 enableButtonOk(false); 0236 0237 QWidget *page = new QWidget(); 0238 setMainWidget(page); 0239 0240 QGridLayout * gridLayout = new QGridLayout(page); 0241 0242 QLabel * textLabel4 = new QLabel(page); 0243 textLabel4->setText(i18n("Cells:")); 0244 gridLayout->addWidget(textLabel4, 2, 0); 0245 0246 m_cellRange = new KLineEdit(page); 0247 gridLayout->addWidget(m_cellRange, 2, 1); 0248 0249 QLabel * textLabel1 = new QLabel(page); 0250 textLabel1->setText(i18n("Sheet:")); 0251 gridLayout->addWidget(textLabel1, 1, 0); 0252 0253 m_sheets = new KComboBox(page); 0254 gridLayout->addWidget(m_sheets, 1, 1); 0255 0256 QLabel * textLabel2 = new QLabel(page); 0257 textLabel2->setText(i18n("Area name:")); 0258 gridLayout->addWidget(textLabel2, 0, 0); 0259 0260 m_areaNameEdit = new KLineEdit(page); 0261 gridLayout->addWidget(m_areaNameEdit, 0, 1); 0262 0263 const QList<Sheet*> sheetList = m_selection->activeSheet()->map()->sheetList(); 0264 for (int i = 0; i < sheetList.count(); ++i) { 0265 Sheet* sheet = sheetList.at(i); 0266 if (!sheet) 0267 continue; 0268 m_sheets->insertItem(i, sheet->sheetName()); 0269 } 0270 0271 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk())); 0272 connect(m_areaNameEdit, SIGNAL(textChanged(QString)), 0273 this, SLOT(slotAreaNameModified(QString))); 0274 } 0275 0276 EditNamedAreaDialog::~EditNamedAreaDialog() 0277 { 0278 } 0279 0280 QString EditNamedAreaDialog::areaName() const 0281 { 0282 return m_areaNameEdit->text(); 0283 } 0284 0285 void EditNamedAreaDialog::setAreaName(const QString& name) 0286 { 0287 m_initialAreaName = name; 0288 m_areaNameEdit->setText(name); 0289 Sheet* sheet = m_selection->activeSheet()->map()->namedAreaManager()->sheet(name); 0290 const QString tmpName = m_selection->activeSheet()->map()->namedAreaManager()->namedArea(name).name(sheet); 0291 m_cellRange->setText(tmpName); 0292 } 0293 0294 void EditNamedAreaDialog::setRegion(const Region& region) 0295 { 0296 Sheet* sheet = region.firstSheet(); 0297 m_sheets->setCurrentIndex(m_sheets->findText(sheet->sheetName())); 0298 m_cellRange->setText(region.name(sheet)); 0299 } 0300 0301 void EditNamedAreaDialog::slotOk() 0302 { 0303 if (m_areaNameEdit->text().isEmpty()) 0304 return; 0305 Sheet* sheet = m_selection->activeSheet()->map()->sheet(m_sheets->currentIndex()); 0306 Region region(m_cellRange->text(), m_selection->activeSheet()->map(), sheet); 0307 if (!region.isValid()) 0308 return; 0309 0310 KUndo2Command* macroCommand = 0; 0311 if (!m_initialAreaName.isEmpty() && m_initialAreaName != m_areaNameEdit->text()) { 0312 macroCommand = new KUndo2Command(kundo2_i18n("Replace Named Area")); 0313 // remove the old named area 0314 NamedAreaCommand* command = new NamedAreaCommand(macroCommand); 0315 command->setAreaName(m_initialAreaName); 0316 command->setReverse(true); 0317 command->setSheet(sheet); 0318 command->add(region); 0319 } 0320 0321 // insert the new named area 0322 NamedAreaCommand* command = new NamedAreaCommand(macroCommand); 0323 command->setAreaName(m_areaNameEdit->text()); 0324 command->setSheet(sheet); 0325 command->add(region); 0326 0327 m_selection->canvas()->addCommand(macroCommand ? macroCommand : command); 0328 0329 accept(); 0330 } 0331 0332 void EditNamedAreaDialog::slotAreaNameModified(const QString& name) 0333 { 0334 enableButtonOk(!name.isEmpty()); 0335 }