File indexing completed on 2024-05-12 16:35:15

0001 /* This file is part of the KDE project
0002    Copyright 2002-2003 Norbert Andres <nandres@web.de>
0003    Copyright 2002 Philipp Mueller <philipp.mueller@gmx.de>
0004    Copyright 2002 John Dailey <dailey@vt.edu>
0005    Copyright 1999-2001 Laurent Montel <montel@kde.org>
0006    Copyright 1998-1999 Torben Weis <weis@kde.org>
0007 
0008    This library is free software; you can redistribute it and/or
0009    modify it under the terms of the GNU Library General Public
0010    License as published by the Free Software Foundation; either
0011    version 2 of the License, or (at your option) any later version.
0012 
0013    This library is distributed in the hope that it will be useful,
0014    but WITHOUT ANY WARRANTY; without even the implied warranty of
0015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016    Library General Public License for more details.
0017 
0018    You should have received a copy of the GNU Library General Public License
0019    along with this library; see the file COPYING.LIB.  If not, write to
0020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021    Boston, MA 02110-1301, USA.
0022 */
0023 
0024 // Local
0025 #include "AddNamedAreaDialog.h"
0026 
0027 #include <QLabel>
0028 #include <QVBoxLayout>
0029 
0030 #include <klineedit.h>
0031 #include <kmessagebox.h>
0032 
0033 #include "Localization.h"
0034 #include "Map.h"
0035 #include "NamedAreaManager.h"
0036 #include "ui/Selection.h"
0037 #include "Sheet.h"
0038 
0039 #include "commands/NamedAreaCommand.h"
0040 
0041 using namespace Calligra::Sheets;
0042 
0043 AddNamedAreaDialog::AddNamedAreaDialog(QWidget* parent, Selection* selection)
0044         : KoDialog(parent)
0045         , m_selection(selection)
0046 {
0047     setButtons(Ok | Cancel);
0048     setCaption(i18n("Add Named Area"));
0049     setModal(true);
0050     setObjectName(QLatin1String("AddNamedAreaDialog"));
0051 
0052     QWidget* widget = new QWidget();
0053     setMainWidget(widget);
0054 
0055     QVBoxLayout* layout = new QVBoxLayout(widget);
0056 
0057     QLabel* label = new QLabel(i18n("Enter the area name:"), widget);
0058     layout->addWidget(label);
0059 
0060     m_areaName = new KLineEdit(widget);
0061     m_areaName->setFocus();
0062     m_areaName->setMinimumWidth(m_areaName->sizeHint().width()* 3);
0063     layout->addWidget(m_areaName);
0064 
0065     enableButtonOk(!m_areaName->text().isEmpty());
0066 
0067     connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
0068     connect(m_areaName, SIGNAL(textChanged(QString)),
0069             this, SLOT(slotAreaNameChanged(QString)));
0070 }
0071 
0072 void AddNamedAreaDialog::slotAreaNameChanged(const QString& name)
0073 {
0074     enableButtonOk(!name.isEmpty());
0075 }
0076 
0077 void AddNamedAreaDialog::slotOk()
0078 {
0079     if (m_areaName->text().isEmpty())
0080         return;
0081 
0082     const QString name = m_areaName->text();
0083     const Region region(m_selection->lastRange(), m_selection->lastSheet());
0084     if (m_selection->activeSheet()->map()->namedAreaManager()->namedArea(name) == region)
0085         return; // nothing to do
0086 
0087     NamedAreaCommand* command = 0;
0088     if (m_selection->activeSheet()->map()->namedAreaManager()->contains(name)) {
0089         const QString question = i18n("The named area '%1' already exists.\n"
0090                                       "Do you want to replace it?", name);
0091         int result = KMessageBox::warningContinueCancel(this, question, i18n("Replace Named Area"),
0092                      KStandardGuiItem::overwrite());
0093         if (result == KMessageBox::Cancel)
0094             return;
0095 
0096         command = new NamedAreaCommand();
0097         command->setText(kundo2_i18n("Replace Named Area"));
0098     } else
0099         command = new NamedAreaCommand();
0100     command->setSheet(m_selection->activeSheet());
0101     command->setAreaName(name);
0102     command->add(region);
0103     command->execute(m_selection->canvas());
0104 }