1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/* This file is part of the KDE project
Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
Copyright 2002-2003 Norbert Andres <nandres@web.de>
Copyright 2002 Ariya Hidayat <ariya@kde.org>
Copyright 2002 Harri Porten <porten@kde.org>
Copyright 2002 John Dailey <dailey@vt.edu>
Copyright 1999-2002 Laurent Montel <montel@kde.org>
Copyright 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
Copyright 1998-2000 Torben Weis <weis@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// Local
#include "NamedAreaDialog.h"
// Qt
#include <QGridLayout>
#include <QLabel>
#include <QList>
#include <QPushButton>
#include <QVBoxLayout>
// KDE
#include <kcombobox.h>
#include <kdebug.h>
#include <klineedit.h>
#include <klistwidget.h>
#include <kmessagebox.h>
#include <KStandardGuiItem>
#include <KoCanvasBase.h>
// KSpread
#include "Localization.h"
#include "Map.h"
#include "NamedAreaManager.h"
#include "ui/Selection.h"
#include "Sheet.h"
#include "Util.h"
#include "commands/NamedAreaCommand.h"
using namespace Calligra::Sheets;
NamedAreaDialog::NamedAreaDialog(QWidget* parent, Selection* selection)
: KDialog(parent)
, m_selection(selection)
{
setButtons(KDialog::Ok | KDialog::Close | KDialog::User1 | KDialog::User2 | KDialog::User3);
setButtonsOrientation(Qt::Vertical);
setButtonText(KDialog::Ok, i18n("&Select"));
setButtonText(KDialog::User1, i18n("&Remove"));
setButtonText(KDialog::User2, i18n("&Edit..."));
setButtonText(KDialog::User3, i18n("&New..."));
setCaption(i18n("Named Areas"));
setModal(true);
setObjectName(QLatin1String("NamedAreaDialog"));
QWidget* widget = new QWidget(this);
setMainWidget(widget);
QVBoxLayout *vboxLayout = new QVBoxLayout(widget);
vboxLayout->setMargin(0);
vboxLayout->setSpacing(KDialog::spacingHint());
m_list = new KListWidget(this);
m_list->setSortingEnabled(true);
vboxLayout->addWidget(m_list);
m_rangeName = new QLabel(this);
m_rangeName->setText(i18n("Area: %1", QString()));
vboxLayout->addWidget(m_rangeName);
const QList<QString> namedAreas = m_selection->activeSheet()->map()->namedAreaManager()->areaNames();
for (int i = 0; i < namedAreas.count(); ++i)
m_list->addItem(namedAreas[i]);
if (m_list->count() == 0) {
enableButtonOk(false);
enableButton(KDialog::User1, false);
enableButton(KDialog::User2, false);
m_list->setCurrentRow(0);
}
connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
connect(this, SIGNAL(cancelClicked()), this, SLOT(slotClose()));
connect(this, SIGNAL(user1Clicked()), this, SLOT(slotRemove()));
connect(this, SIGNAL(user2Clicked()), this, SLOT(slotEdit()));
connect(this, SIGNAL(user3Clicked()), this, SLOT(slotNew()));
connect(m_list, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(slotOk()));
connect(m_list, SIGNAL(currentTextChanged(const QString&)),
this, SLOT(displayAreaValues(const QString&)));
if (m_list->count() > 0)
m_list->setCurrentItem(m_list->item(0));
m_list->setFocus();
}
void NamedAreaDialog::displayAreaValues(QString const & areaName)
{
const QString regionName = m_selection->activeSheet()->map()->namedAreaManager()->namedArea(areaName).name();
m_rangeName->setText(i18n("Area: %1", regionName));
}
void NamedAreaDialog::slotOk()
{
if (m_list->count() > 0) {
QListWidgetItem* item = m_list->currentItem();
Region region = m_selection->activeSheet()->map()->namedAreaManager()->namedArea(item->text());
Sheet* sheet = m_selection->activeSheet()->map()->namedAreaManager()->sheet(item->text());
if (!sheet || !region.isValid()) {
return;
}
if (sheet && sheet != m_selection->activeSheet())
m_selection->emitVisibleSheetRequested(sheet);
m_selection->initialize(region);
}
m_selection->emitModified();
accept();
}
void NamedAreaDialog::slotClose()
{
reject();
}
void NamedAreaDialog::slotNew()
{
QPointer<EditNamedAreaDialog> dialog = new EditNamedAreaDialog(this, m_selection);
dialog->setCaption(i18n("New Named Area"));
dialog->setRegion(*m_selection);
dialog->exec();
if (dialog->result() == Rejected)
return;
if (dialog->areaName().isEmpty())
return;
m_list->addItem(dialog->areaName());
QList<QListWidgetItem*> items = m_list->findItems(dialog->areaName(),
Qt::MatchExactly | Qt::MatchCaseSensitive);
m_list->setCurrentItem(items.first());
displayAreaValues(dialog->areaName());
delete dialog;
enableButtonOk(true);
enableButton(KDialog::User1, true);
enableButton(KDialog::User2, true);
}
void NamedAreaDialog::slotEdit()
{
QListWidgetItem* item = m_list->currentItem();
if (item->text().isEmpty())
return;
QPointer<EditNamedAreaDialog> dialog = new EditNamedAreaDialog(this, m_selection);
dialog->setCaption(i18n("Edit Named Area"));
dialog->setAreaName(item->text());
dialog->exec();
if (dialog->result() == Rejected)
return;
item->setText(dialog->areaName());
displayAreaValues(dialog->areaName());
delete dialog;
}
void NamedAreaDialog::slotRemove()
{
const QString question = i18n("Do you really want to remove this named area?");
int result = KMessageBox::warningContinueCancel(this, question, i18n("Remove Named Area"),
KStandardGuiItem::del());
if (result == KMessageBox::Cancel)
return;
QListWidgetItem* item = m_list->currentItem();
NamedAreaCommand* command = new NamedAreaCommand();
command->setAreaName(item->text());
command->setReverse(true);
command->setSheet(m_selection->activeSheet());
if (!command->execute(m_selection->canvas())) {
delete command;
return;
}
m_list->takeItem(m_list->row(item));
if (m_list->count() == 0) {
enableButtonOk(false);
enableButton(KDialog::User1, false);
enableButton(KDialog::User2, false);
displayAreaValues(QString());
} else
displayAreaValues(m_list->currentItem()->text());
}
EditNamedAreaDialog::EditNamedAreaDialog(QWidget* parent, Selection* selection)
: KDialog(parent)
, m_selection(selection)
{
setButtons(Ok | Cancel);
setModal(true);
setObjectName(QLatin1String("EditNamedAreaDialog"));
enableButtonOk(false);
QWidget *page = new QWidget();
setMainWidget(page);
QGridLayout * gridLayout = new QGridLayout(page);
gridLayout->setMargin(KDialog::marginHint());
gridLayout->setSpacing(KDialog::spacingHint());
QLabel * textLabel4 = new QLabel(page);
textLabel4->setText(i18n("Cells:"));
gridLayout->addWidget(textLabel4, 2, 0);
m_cellRange = new KLineEdit(page);
gridLayout->addWidget(m_cellRange, 2, 1);
QLabel * textLabel1 = new QLabel(page);
textLabel1->setText(i18n("Sheet:"));
gridLayout->addWidget(textLabel1, 1, 0);
m_sheets = new KComboBox(page);
gridLayout->addWidget(m_sheets, 1, 1);
QLabel * textLabel2 = new QLabel(page);
textLabel2->setText(i18n("Area name:"));
gridLayout->addWidget(textLabel2, 0, 0);
m_areaNameEdit = new KLineEdit(page);
gridLayout->addWidget(m_areaNameEdit, 0, 1);
const QList<Sheet*> sheetList = m_selection->activeSheet()->map()->sheetList();
for (int i = 0; i < sheetList.count(); ++i) {
Sheet* sheet = sheetList.at(i);
if (!sheet)
continue;
m_sheets->insertItem(i, sheet->sheetName());
}
connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
connect(m_areaNameEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(slotAreaNameModified(const QString&)));
}
EditNamedAreaDialog::~EditNamedAreaDialog()
{
}
QString EditNamedAreaDialog::areaName() const
{
return m_areaNameEdit->text();
}
void EditNamedAreaDialog::setAreaName(const QString& name)
{
m_initialAreaName = name;
m_areaNameEdit->setText(name);
Sheet* sheet = m_selection->activeSheet()->map()->namedAreaManager()->sheet(name);
const QString tmpName = m_selection->activeSheet()->map()->namedAreaManager()->namedArea(name).name(sheet);
m_cellRange->setText(tmpName);
}
void EditNamedAreaDialog::setRegion(const Region& region)
{
Sheet* sheet = region.firstSheet();
m_sheets->setCurrentIndex(m_sheets->findText(sheet->sheetName()));
m_cellRange->setText(region.name(sheet));
}
void EditNamedAreaDialog::slotOk()
{
if (m_areaNameEdit->text().isEmpty())
return;
Sheet* sheet = m_selection->activeSheet()->map()->sheet(m_sheets->currentIndex());
Region region(m_cellRange->text(), m_selection->activeSheet()->map(), sheet);
if (!region.isValid())
return;
KUndo2Command* macroCommand = 0;
if (!m_initialAreaName.isEmpty() && m_initialAreaName != m_areaNameEdit->text()) {
macroCommand = new KUndo2Command(i18nc("(qtundo-format)", "Replace Named Area"));
// remove the old named area
NamedAreaCommand* command = new NamedAreaCommand(macroCommand);
command->setAreaName(m_initialAreaName);
command->setReverse(true);
command->setSheet(sheet);
command->add(region);
}
// insert the new named area
NamedAreaCommand* command = new NamedAreaCommand(macroCommand);
command->setAreaName(m_areaNameEdit->text());
command->setSheet(sheet);
command->add(region);
m_selection->canvas()->addCommand(macroCommand ? macroCommand : command);
accept();
}
void EditNamedAreaDialog::slotAreaNameModified(const QString& name)
{
enableButtonOk(!name.isEmpty());
}
#include "NamedAreaDialog.moc"
|