kspread

AddNamedAreaDialog.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright 2002-2003 Norbert Andres <nandres@web.de>
00003    Copyright 2002 Philipp Mueller <philipp.mueller@gmx.de>
00004    Copyright 2002 John Dailey <dailey@vt.edu>
00005    Copyright 1999-2001 Laurent Montel <montel@kde.org>
00006    Copyright 1998-1999 Torben Weis <weis@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021    Boston, MA 02110-1301, USA.
00022 */
00023 
00024 // Local
00025 #include "AddNamedAreaDialog.h"
00026 
00027 #include <QLabel>
00028 #include <QVBoxLayout>
00029 
00030 #include <klineedit.h>
00031 #include <kmessagebox.h>
00032 
00033 #include "Localization.h"
00034 #include "Map.h"
00035 #include "NamedAreaManager.h"
00036 #include "Selection.h"
00037 #include "Sheet.h"
00038 
00039 #include "commands/NamedAreaCommand.h"
00040 
00041 using namespace KSpread;
00042 
00043 AddNamedAreaDialog::AddNamedAreaDialog(QWidget* parent, Selection* selection)
00044     : KDialog(parent)
00045     , m_selection(selection)
00046 {
00047     setButtons(Ok | Cancel);
00048     setCaption(i18n("Add Named Area"));
00049     setModal(true);
00050     setObjectName("AddNamedAreaDialog");
00051 
00052     QWidget* widget = new QWidget();
00053     setMainWidget(widget);
00054 
00055     QVBoxLayout* layout = new QVBoxLayout(widget);
00056 
00057     QLabel* label = new QLabel(i18n("Enter the area name:"), widget);
00058     layout->addWidget(label);
00059 
00060     m_areaName = new KLineEdit(widget);
00061     m_areaName->setFocus();
00062     m_areaName->setMinimumWidth(m_areaName->sizeHint().width()* 3);
00063     layout->addWidget(m_areaName);
00064 
00065     enableButtonOk(!m_areaName->text().isEmpty());
00066 
00067     connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
00068     connect(m_areaName, SIGNAL(textChanged(const QString&)),
00069             this, SLOT(slotAreaNameChanged(const QString&)));
00070 }
00071 
00072 void AddNamedAreaDialog::slotAreaNameChanged(const QString& name)
00073 {
00074     enableButtonOk(!name.isEmpty());
00075 }
00076 
00077 void AddNamedAreaDialog::slotOk()
00078 {
00079     if (m_areaName->text().isEmpty())
00080         return;
00081 
00082     const QString name = m_areaName->text();
00083     const Region region(m_selection->lastRange(), m_selection->lastSheet());
00084     if (m_selection->activeSheet()->map()->namedAreaManager()->namedArea(name) == region)
00085         return; // nothing to do
00086 
00087     NamedAreaCommand* command = 0;
00088     if (m_selection->activeSheet()->map()->namedAreaManager()->contains(name))
00089     {
00090         const QString question = i18n("The named area '%1' already exists.\n"
00091                                       "Do you want to replace it?", name);
00092         int result = KMessageBox::warningContinueCancel(this, question, i18n("Replace Named Area"),
00093                                                         KStandardGuiItem::overwrite());
00094         if (result == KMessageBox::Cancel)
00095             return;
00096 
00097         command = new NamedAreaCommand();
00098         command->setText(i18n("Replace Named Area"));
00099     }
00100     else
00101         command = new NamedAreaCommand();
00102     command->setSheet(m_selection->activeSheet());
00103     command->setAreaName(name);
00104     command->add(region);
00105     command->execute(m_selection->canvas());
00106 }
00107 
00108 #include "AddNamedAreaDialog.moc"