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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
/* This file is part of the KDE project
Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
(C) 2002-2003 Philipp Mueller <philipp.mueller@gmx.de>
(C) 2002 John Dailey <dailey@vt.edu>
(C) 1999-2001 Laurent Montel <montel@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.
*/
#include "ConditionalDialog.h"
#include "Condition.h"
#include "Cell.h"
#include "Map.h"
#include "ui/Selection.h"
#include "Sheet.h"
#include "StyleManager.h"
#include "ValueConverter.h"
#include "ValueParser.h"
// commands
#include "commands/ConditionCommand.h"
#include <kcombobox.h>
#include <kdebug.h>
#include <klineedit.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <KoGlobal.h>
#include <QGroupBox>
#include <QLabel>
//Added by qt3to4:
#include <QGridLayout>
using namespace Calligra::Sheets;
ConditionalWidget::ConditionalWidget(QWidget* parent, const char* /*name*/, Qt::WFlags fl)
: QWidget(parent, fl)
{
QGridLayout * Form1Layout = new QGridLayout(this);
QGroupBox * groupBox1_3 = new QGroupBox(this);
QGridLayout * groupBox1_3Layout = new QGridLayout(groupBox1_3);
groupBox1_3Layout->setAlignment(Qt::AlignTop);
QLabel * textLabel1_3 = new QLabel(groupBox1_3);
groupBox1_3Layout->addWidget(textLabel1_3, 0, 0);
m_condition_3 = new KComboBox(groupBox1_3);
groupBox1_3Layout->addWidget(m_condition_3, 0, 1);
m_firstValue_3 = new KLineEdit(groupBox1_3);
m_firstValue_3->setEnabled(false);
groupBox1_3Layout->addWidget(m_firstValue_3, 0, 2);
m_secondValue_3 = new KLineEdit(groupBox1_3);
m_secondValue_3->setEnabled(false);
groupBox1_3Layout->addWidget(m_secondValue_3, 0, 3);
m_style_3 = new KComboBox(groupBox1_3);
m_style_3->setEnabled(false);
groupBox1_3Layout->addWidget(m_style_3, 1, 1);
QLabel * textLabel2_3 = new QLabel(groupBox1_3);
groupBox1_3Layout->addWidget(textLabel2_3, 1, 0);
QSpacerItem * spacer = new QSpacerItem(41, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
groupBox1_3Layout->addItem(spacer, 1, 2);
QSpacerItem * spacer_2 = new QSpacerItem(61, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
groupBox1_3Layout->addItem(spacer_2, 1, 3);
Form1Layout->addWidget(groupBox1_3, 2, 0);
QGroupBox * groupBox1_2 = new QGroupBox(this);
QGridLayout * groupBox1_2Layout = new QGridLayout(groupBox1_2);
groupBox1_2Layout->setAlignment(Qt::AlignTop);
QLabel * textLabel1_2 = new QLabel(groupBox1_2);
groupBox1_2Layout->addWidget(textLabel1_2, 0, 0);
QLabel * textLabel2_2 = new QLabel(groupBox1_2);
groupBox1_2Layout->addWidget(textLabel2_2, 1, 0);
m_condition_2 = new KComboBox(groupBox1_2);
groupBox1_2Layout->addWidget(m_condition_2, 0, 1);
m_style_2 = new KComboBox(groupBox1_2);
m_style_2->setEnabled(false);
groupBox1_2Layout->addWidget(m_style_2, 1, 1);
m_firstValue_2 = new KLineEdit(groupBox1_2);
m_firstValue_2->setEnabled(false);
groupBox1_2Layout->addWidget(m_firstValue_2, 0, 2);
m_secondValue_2 = new KLineEdit(groupBox1_2);
m_secondValue_2->setEnabled(false);
groupBox1_2Layout->addWidget(m_secondValue_2, 0, 3);
QSpacerItem * spacer_3 = new QSpacerItem(41, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
groupBox1_2Layout->addItem(spacer_3, 1, 2);
QSpacerItem * spacer_4 = new QSpacerItem(61, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
groupBox1_2Layout->addItem(spacer_4, 1, 3);
Form1Layout->addWidget(groupBox1_2, 1, 0);
QGroupBox * groupBox1_1 = new QGroupBox(this);
QGridLayout * groupBox1_1Layout = new QGridLayout(groupBox1_1);
groupBox1_1Layout->setAlignment(Qt::AlignTop);
QLabel * textLabel1_1 = new QLabel(groupBox1_1);
groupBox1_1Layout->addWidget(textLabel1_1, 0, 0);
QLabel * textLabel2_1 = new QLabel(groupBox1_1);
groupBox1_1Layout->addWidget(textLabel2_1, 1, 0);
m_condition_1 = new KComboBox(groupBox1_1);
groupBox1_1Layout->addWidget(m_condition_1, 0, 1);
m_style_1 = new KComboBox(groupBox1_1);
m_style_1->setEnabled(false);
groupBox1_1Layout->addWidget(m_style_1, 1, 1);
m_firstValue_1 = new KLineEdit(groupBox1_1);
m_firstValue_1->setEnabled(false);
groupBox1_1Layout->addWidget(m_firstValue_1, 0, 2);
m_secondValue_1 = new KLineEdit(groupBox1_1);
m_secondValue_1->setEnabled(false);
groupBox1_1Layout->addWidget(m_secondValue_1, 0, 3);
QSpacerItem * spacer_5 = new QSpacerItem(41, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
groupBox1_1Layout->addItem(spacer_5, 1, 2);
QSpacerItem * spacer_6 = new QSpacerItem(61, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
groupBox1_1Layout->addItem(spacer_6, 1, 3);
Form1Layout->addWidget(groupBox1_1, 0, 0);
resize(QSize(702, 380).expandedTo(minimumSizeHint()));
QStringList list;
list += i18n("<none>"); // krazy:exclude=i18ncheckarg
list += i18n("equal to");
list += i18n("greater than");
list += i18n("less than");
list += i18n("equal to or greater than");
list += i18n("equal to or less than");
list += i18n("between");
list += i18n("outside range");
list += i18n("different to");
m_condition_1->clear();
m_condition_2->clear();
m_condition_3->clear();
m_condition_1->insertItems(0, list);
m_condition_2->insertItems(0, list);
m_condition_3->insertItems(0, list);
groupBox1_1->setTitle(i18n("First Condition"));
groupBox1_2->setTitle(i18n("Second Condition"));
groupBox1_3->setTitle(i18n("Third Condition"));
textLabel1_1->setText(i18n("Cell is"));
textLabel1_2->setText(i18n("Cell is"));
textLabel1_3->setText(i18n("Cell is"));
textLabel2_1->setText(i18n("Cell style"));
textLabel2_2->setText(i18n("Cell style"));
textLabel2_3->setText(i18n("Cell style"));
connect(m_condition_1, SIGNAL(highlighted(const QString &)), this, SLOT(slotTextChanged1(const QString &)));
connect(m_condition_2, SIGNAL(highlighted(const QString &)), this, SLOT(slotTextChanged2(const QString &)));
connect(m_condition_3, SIGNAL(highlighted(const QString &)), this, SLOT(slotTextChanged3(const QString &)));
}
ConditionalWidget::~ConditionalWidget()
{
}
void ConditionalWidget::slotTextChanged1(const QString & text)
{
if (text == i18n("<none>")) { // krazy:exclude=i18ncheckarg
m_firstValue_1->setEnabled(false);
m_secondValue_1->setEnabled(false);
m_style_1->setEnabled(false);
} else {
m_condition_2->setEnabled(true);
m_style_1->setEnabled(true);
if ((text == i18n("between")) || (text == i18n("different from"))) {
m_firstValue_1->setEnabled(true);
m_secondValue_1->setEnabled(true);
} else {
m_firstValue_1->setEnabled(true);
m_secondValue_1->setEnabled(false);
}
}
}
void ConditionalWidget::slotTextChanged2(const QString & text)
{
if (text == i18n("<none>")) { // krazy:exclude=i18ncheckarg
m_firstValue_2->setEnabled(false);
m_secondValue_2->setEnabled(false);
m_style_2->setEnabled(false);
} else {
m_condition_3->setEnabled(true);
m_style_2->setEnabled(true);
if ((text == i18n("between")) || (text == i18n("different from"))) {
m_firstValue_2->setEnabled(true);
m_secondValue_2->setEnabled(true);
} else {
m_firstValue_2->setEnabled(true);
m_secondValue_2->setEnabled(false);
}
}
}
void ConditionalWidget::slotTextChanged3(const QString & text)
{
if (text == i18n("<none>")) { // krazy:exclude=i18ncheckarg
m_firstValue_3->setEnabled(false);
m_secondValue_3->setEnabled(false);
m_style_3->setEnabled(false);
} else {
m_style_3->setEnabled(true);
if ((text == i18n("between")) || (text == i18n("different from"))) {
m_firstValue_3->setEnabled(true);
m_secondValue_3->setEnabled(true);
} else {
m_firstValue_3->setEnabled(true);
m_secondValue_3->setEnabled(false);
}
}
}
/**
* ConditionalDialog
* Sets conditional cell formattings.
*/
ConditionalDialog::ConditionalDialog(QWidget* parent, Selection* selection)
: KDialog(parent),
m_selection(selection),
m_dlg(new ConditionalWidget(this))
{
setButtons(KDialog::Ok | KDialog::Cancel);
setCaption(i18n("Conditional Styles"));
QStringList list(m_selection->activeSheet()->map()->styleManager()->styleNames());
m_dlg->m_style_1->insertItems(0, list);
m_dlg->m_style_2->insertItems(0, list);
m_dlg->m_style_3->insertItems(0, list);
setButtonsOrientation(Qt::Vertical);
setMainWidget(m_dlg);
connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
init();
}
void ConditionalDialog::init()
{
QLinkedList<Conditional> conditionList;
QLinkedList<Conditional> otherList;
bool found;
int numCondition;
QLinkedList<Conditional>::iterator it1;
QLinkedList<Conditional>::iterator it2;
Sheet* sheet = m_selection->activeSheet();
conditionList = Cell(sheet, m_selection->marker()).conditions().conditionList();
/* this is the list, but only display the conditions common to all selected
cells*/
for (int x = m_selection->lastRange().left(); x <= m_selection->lastRange().right(); x++) {
for (int y = m_selection->lastRange().top(); y <= m_selection->lastRange().bottom(); y++) {
otherList = Cell(sheet, x, y).conditions().conditionList();
it1 = conditionList.begin();
while (it1 != conditionList.end()) {
kDebug() << "Here";
found = false;
for (it2 = otherList.begin(); !found && it2 != otherList.end(); ++it2) {
kDebug() << "Found:" << found;
found = ((*it1).value1 == (*it2).value1 &&
(*it1).value2 == (*it2).value2 &&
(*it1).cond == (*it2).cond);
if (!found)
continue;
if ((*it1).styleName != (*it2).styleName)
found = false;
}
if (!found) { /* if it's not here, don't display this condition */
it1 = conditionList.erase(it1);
} else {
++it1;
}
}
}
}
kDebug() << "Conditions:" << conditionList.size();
m_dlg->m_condition_2->setEnabled(false);
m_dlg->m_condition_3->setEnabled(false);
m_dlg->m_style_1->setEnabled(false);
m_dlg->m_style_2->setEnabled(false);
m_dlg->m_style_3->setEnabled(false);
numCondition = 0;
for (it1 = conditionList.begin(); numCondition < 3 && it1 != conditionList.end(); ++it1) {
init(*it1, numCondition);
++numCondition;
}
}
void ConditionalDialog::init(Conditional const & tmp, int numCondition)
{
kDebug() << "Adding" << numCondition;
KComboBox * cb = 0;
KComboBox * sb = 0;
KLineEdit * kl1 = 0;
KLineEdit * kl2 = 0;
QString value;
Map *const map = m_selection->activeSheet()->map();
ValueConverter *const converter = map->converter();
switch (numCondition) {
case 0:
cb = m_dlg->m_condition_1;
sb = m_dlg->m_style_1;
kl1 = m_dlg->m_firstValue_1;
kl2 = m_dlg->m_secondValue_1;
break;
case 1:
cb = m_dlg->m_condition_2;
sb = m_dlg->m_style_2;
kl1 = m_dlg->m_firstValue_2;
kl2 = m_dlg->m_secondValue_2;
break;
case 2:
cb = m_dlg->m_condition_3;
sb = m_dlg->m_style_3;
kl1 = m_dlg->m_firstValue_3;
kl2 = m_dlg->m_secondValue_3;
break;
default:
return;
}
if (!tmp.styleName.isEmpty()) {
sb->setCurrentIndex(sb->findText(tmp.styleName));
sb->setEnabled(true);
}
switch (tmp.cond) {
case Conditional::None :
case Conditional::IsTrueFormula: // was unhandled
break;
case Conditional::Equal :
cb->setCurrentIndex(1);
break;
case Conditional::Superior :
cb->setCurrentIndex(2);
break;
case Conditional::Inferior :
cb->setCurrentIndex(3);
break;
case Conditional::SuperiorEqual :
cb->setCurrentIndex(4);
break;
case Conditional::InferiorEqual :
cb->setCurrentIndex(5);
break;
case Conditional::Between :
cb->setCurrentIndex(6);
kl2->setText(converter->asString(tmp.value2).asString());
break;
case Conditional::Different :
cb->setCurrentIndex(7);
kl2->setText(converter->asString(tmp.value2).asString());
break;
case Conditional::DifferentTo :
cb->setCurrentIndex(8);
break;
}
if (tmp.cond != Conditional::None) {
kl1->setEnabled(true);
kl1->setText(converter->asString(tmp.value1).asString());
}
}
Conditional::Type ConditionalDialog::typeOfCondition(KComboBox const * const cb) const
{
Conditional::Type result = Conditional::None;
switch (cb->currentIndex()) {
case 0 :
result = Conditional::None;
break;
case 1 :
result = Conditional::Equal;
break;
case 2 :
result = Conditional::Superior;
break;
case 3 :
result = Conditional::Inferior;
break;
case 4 :
result = Conditional::SuperiorEqual;
break;
case 5 :
result = Conditional::InferiorEqual;
break;
case 6 :
result = Conditional::Between;
break;
case 7 :
result = Conditional::Different;
break;
case 8 :
result = Conditional::DifferentTo;
break;
default:
kDebug(36001) << "Error in list";
break;
}
return result;
}
bool ConditionalDialog::checkInputData(KLineEdit const * const edit1,
KLineEdit const * const edit2)
{
bool b1 = false;
bool b2 = false;
if (!edit2->isEnabled())
return true;
edit1->text().toDouble(&b1);
edit2->text().toDouble(&b2);
if (b1 != b2) {
if (b1)
KMessageBox::sorry(0, i18n("If the first value is a number, the second value also has to be a number."));
else
KMessageBox::sorry(0, i18n("If the first value is a string, the second value also has to be a string."));
return false;
}
return true;
}
bool ConditionalDialog::checkInputData()
{
if (m_dlg->m_firstValue_1->isEnabled() && !checkInputData(m_dlg->m_firstValue_1, m_dlg->m_secondValue_1))
return false;
if (m_dlg->m_firstValue_2->isEnabled() && !checkInputData(m_dlg->m_firstValue_2, m_dlg->m_secondValue_2))
return false;
if (m_dlg->m_firstValue_3->isEnabled() && !checkInputData(m_dlg->m_firstValue_3, m_dlg->m_secondValue_3))
return false;
return true;
}
bool ConditionalDialog::getCondition(Conditional & newCondition, const KComboBox * cb,
const KLineEdit * edit1, const KLineEdit * edit2,
const KComboBox * sb)
{
if (!cb->isEnabled())
return false;
newCondition.cond = typeOfCondition(cb);
if (newCondition.cond == Conditional::None)
return false;
Map *const map = m_selection->activeSheet()->map();
ValueParser *const parser = map->parser();
newCondition.value1 = parser->parse(edit1->text());
newCondition.value2 = parser->parse(edit2->text());
newCondition.styleName = sb->currentText();
return true;
}
void ConditionalDialog::slotOk()
{
kDebug() << "slotOk";
if (!checkInputData())
return;
kDebug() << "Input data is valid";
QLinkedList<Conditional> newList;
Conditional newCondition;
if (getCondition(newCondition, m_dlg->m_condition_1, m_dlg->m_firstValue_1,
m_dlg->m_secondValue_1, m_dlg->m_style_1))
newList.append(newCondition);
if (getCondition(newCondition, m_dlg->m_condition_2, m_dlg->m_firstValue_2,
m_dlg->m_secondValue_2, m_dlg->m_style_2))
newList.append(newCondition);
if (getCondition(newCondition, m_dlg->m_condition_3, m_dlg->m_firstValue_3,
m_dlg->m_secondValue_3, m_dlg->m_style_3))
newList.append(newCondition);
kDebug() << "Setting conditional list";
CondtionCommand* manipulator = new CondtionCommand();
manipulator->setSheet(m_selection->activeSheet());
manipulator->setConditionList(newList);
manipulator->add(*m_selection);
manipulator->execute(m_selection->canvas());
accept();
}
#include "ConditionalDialog.moc"
|