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 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
|
/*
* dbeditor.cpp
*
* (c) 2002-2004,2008-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
/** @file dbeditor.cpp
* Source file for DBEditor
*/
#include <QApplication>
#include <QLabel>
#include <QLayout>
#include <QMessageBox>
#include <QPushButton>
#include <QRegExp>
#include <QStackedWidget>
#include <QTreeWidget>
#include "calc/calcnode.h"
#include "columneditor.h"
#include "database.h"
#include "datatypes.h"
#include "dbeditor.h"
#include "factory.h"
/**
* Constructor.
*
* @param parent This dialog's parent widget.
*/
DBEditor::DBEditor(QWidget *parent)
: PBDialog(tr("Columns Editor"), parent), db(0), ceName("_cename"),
ceType("_cetype"), ceDefault("_cedefault"), ceOldIndex("_ceoldindex"),
ceNewIndex("_cenewindex"), resized(false)
{
stack = new QStackedWidget(this);
vbox->addWidget(stack, 1);
QString text("<center>%1<br><br>%2</center>");
text = text.arg(tr("No columns defined"));
text = text.arg(tr("Press the \"Add\" button to create one"));
noColumns = new QLabel(text, stack);
stack->addWidget(noColumns);
QStringList headers;
headers << ColumnEditor::tr("Name");
headers << ColumnEditor::tr("Type");
headers << ColumnEditor::tr("Default");
table = Factory::treeWidget(stack, headers);
stack->addWidget(table);
int colWidth = width() / 3 - 2;
table->setColumnWidth(0, colWidth);
table->setColumnWidth(1, colWidth);
table->setColumnWidth(2, colWidth);
stack->setCurrentWidget(noColumns);
addEditButtons();
connect(addButton, SIGNAL(clicked()), this, SLOT(addColumn()));
connect(editButton, SIGNAL(clicked()), this, SLOT(editColumn()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteColumn()));
connect(upButton, SIGNAL(clicked()), this, SLOT(moveUp()));
connect(downButton, SIGNAL(clicked()), this, SLOT(moveDown()));
finishLayout();
}
/**
* Destructor.
*/
DBEditor::~DBEditor()
{
NameCalcMap::Iterator iter;
for (iter = calcMap.begin(); iter != calcMap.end(); ++iter) {
CalcNode *calcRoot = iter.value();
if (calcRoot != 0) {
delete calcRoot;
}
}
}
/**
* Launch this dialog in order to edit the format of the specified database.
*
* @param subject The database whose format is to be edited
*/
int DBEditor::edit(Database *subject)
{
db = subject;
columnEditor = new ColumnEditor(db, this);
originalCols = db->listColumns();
renamedCols = db->listColumns();
int count = originalCols.count();
for (int i = 0; i < count; i++) {
QString name = originalCols[i];
int type = db->getType(name);
QString defaultVal = db->getDefault(name);
if (type == CALC) {
int decimals = 2;
CalcNode *root = db->loadCalc(name, &decimals);
calcMap.insert(name, root);
decimalsMap.insert(name, decimals);
if (root != 0) {
defaultVal = root->equation(db);
}
}
info.Add(ceName [name.toUtf8()] + ceType [type]
+ ceDefault [defaultVal.toUtf8()]
+ ceOldIndex [i] + ceNewIndex [i]);
}
updateTable();
return exec();
}
/**
* Add a new column to the database via the ColumnEditor dialog. Called when
* the "Add" button is pressed.
*/
void DBEditor::addColumn()
{
QString name = "";
int type = STRING;
QString defaultVal = "";
columnEditor->setName(name);
columnEditor->setType(type);
columnEditor->setTypeEditable(true);
columnEditor->setDefaultValue(defaultVal);
bool finished = false;
bool aborted = false;
while (!finished) {
if (!columnEditor->exec()) {
finished = true;
aborted = true;
}
else {
name = columnEditor->name();
type = columnEditor->type();
defaultVal = columnEditor->defaultValue();
finished = (isValidName(name) && isValidDefault(type, defaultVal));
}
}
int decimals = 2;
CalcNode *calcRoot = columnEditor->calculation(&decimals);
if (!aborted) {
int size = info.GetSize();
info.Add(ceName [name.toUtf8()] + ceType [type]
+ ceDefault[defaultVal.toUtf8()]
+ ceOldIndex [-1] + ceNewIndex[size]);
if (type == CALC) {
calcMap.insert(name, calcRoot);
decimalsMap.insert(name, decimals);
columnEditor->setCalculation(0, 2);
calcRoot = 0;
}
updateTable();
}
if (calcRoot != 0) {
delete calcRoot;
columnEditor->setCalculation(0, 2);
}
}
/**
* Use the ColumnEditor dialog to edit the currently selected column
* definition. Called when the "Edit" button is pressed.
*/
void DBEditor::editColumn()
{
QTreeWidgetItem *item = table->currentItem();
if (!item) {
return;
}
QString name = item->text(0);
QString oldName = name;
int index = info.Find(ceName [name.toUtf8()]);
int type = ceType (info[index]);
QString defaultVal = QString::fromUtf8(ceDefault (info[index]));
columnEditor->setName(name);
columnEditor->setType(type);
columnEditor->setTypeEditable(false);
columnEditor->setDefaultValue(defaultVal);
CalcNode *calcRoot = 0;
int decimals = 2;
if (type == CALC) {
calcRoot = calcMap[name];
decimals = decimalsMap[name];
// use a copy in case the edit is cancelled
if (calcRoot != 0) {
calcRoot = calcRoot->clone();
}
columnEditor->setCalculation(calcRoot, decimals);
}
bool finished = false;
bool aborted = false;
while (!finished) {
if (!columnEditor->exec()) {
finished = true;
aborted = true;
}
else {
name = columnEditor->name();
type = columnEditor->type();
defaultVal = columnEditor->defaultValue();
if (name != oldName) {
finished = (isValidName(name)
&& isValidDefault(type, defaultVal));
if (finished) {
int oldIndex = ceOldIndex (info[index]);
if (oldIndex != -1) {
renamedCols[oldIndex] = name;
}
renameColumnRefs(oldName, name);
}
}
else {
finished = isValidDefault(type, defaultVal);
}
}
}
calcRoot = columnEditor->calculation(&decimals);
if (!aborted) {
ceName (info[index]) = name.toUtf8();
ceDefault (info[index]) = defaultVal.toUtf8();
if (type == CALC) {
CalcNode *oldRoot = calcMap[oldName];
if (oldRoot != 0) {
delete oldRoot;
}
calcMap.remove(oldName);
decimalsMap.remove(oldName);
calcMap.insert(name, calcRoot);
decimalsMap.insert(name, decimals);
columnEditor->setCalculation(0, 2);
calcRoot = 0;
}
updateTable();
}
if (calcRoot != 0) {
delete calcRoot;
columnEditor->setCalculation(0, 2);
}
}
/**
* Determine if the provided text can be used as a valid column name.
*
* @param colName The candidate column name
* @return True if valid, false otherwise
*/
bool DBEditor::isValidName(const QString &colName)
{
return validateName(colName, "", columnNames());
}
/**
* Determine if the provided string represents a valid default value for a
* column of the specified type. Assumes that the proposed value was
* retrieved from the ColumnEditor dialog.
*
* @param type The type of column in question
* @param defaultVal The proposed default value
* @return True if valid, false otherwise
*/
bool DBEditor::isValidDefault(int type, const QString &defaultVal)
{
if (type == DATE || type == TIME || type >= FIRST_ENUM) {
// The column editor doesn't allow free entry for these
return true;
}
QString error = db->isValidValue(type, defaultVal);
if (!error.isEmpty()) {
QString message = ColumnEditor::tr("Default") + " " + error;
QMessageBox::warning(this, qApp->applicationName(), message);
return false;
}
else {
return true;
}
}
/**
* Delete the currently selected column definition.
*/
void DBEditor::deleteColumn()
{
QTreeWidgetItem *item = table->currentItem();
if (!item) {
return;
}
QString name = item->text(0);
int index = info.Find(ceName [name.toUtf8()]);
int oldIndex = ceOldIndex (info[index]);
info.RemoveAt(index);
if (oldIndex != -1) {
deletedCols.append(originalCols[oldIndex]);
}
int size = info.GetSize();
for (int i = index; i < size; i++) {
ceNewIndex (info[i]) = ceNewIndex (info[i]) - 1;
}
if (calcMap.contains(name)) {
CalcNode *calcRoot = calcMap[name];
if (calcRoot != 0) {
delete calcRoot;
}
calcMap.remove(name);
decimalsMap.remove(name);
}
deleteColumnRefs(name);
updateTable();
}
/**
* Move the currently selected column definition up by one (unless it is
* already the first one). This results in that column's position index
* being swapped with the one which had preceded it.
*/
void DBEditor::moveUp()
{
QTreeWidgetItem *item = table->currentItem();
if (!item) {
return;
}
QString name = item->text(0);
int row1 = info.Find(ceName [name.toUtf8()]);
int index = ceNewIndex (info[row1]);
if (index > 0) {
int row2 = info.Find(ceNewIndex [index - 1]);
ceNewIndex (info[row1]) = index - 1;
ceNewIndex (info[row2]) = index;
updateTable();
selectRow(name);
}
}
/**
* Move the currently selected column definition down by one (unless it is
* already the last one). This results in that column's position index being
* swapped with the one which had followed it.
*/
void DBEditor::moveDown()
{
QTreeWidgetItem *item = table->currentItem();
if (!item) {
return;
}
QString name = item->text(0);
int row1 = info.Find(ceName [name.toUtf8()]);
int index = ceNewIndex (info[row1]);
int size = info.GetSize();
if (index < size - 1) {
int row2 = info.Find(ceNewIndex [index + 1]);
ceNewIndex (info[row1]) = index + 1;
ceNewIndex (info[row2]) = index;
updateTable();
selectRow(name);
}
}
/**
* Select the column definition with the specified name.
*
* @param colName The column name of the entry to highlight
*/
void DBEditor::selectRow(const QString &colName)
{
int count = table->topLevelItemCount();
for (int i = 0; i < count; i++) {
QTreeWidgetItem *item = table->topLevelItem(i);
if (item && item->text(0) == colName) {
table->setCurrentItem(item);
break;
}
}
}
/**
* Update the displayed table of column definitions.
*/
void DBEditor::updateTable()
{
table->clear();
c4_View temp = info.SortOn(ceNewIndex);
int size = temp.GetSize();
QTreeWidgetItem *last = 0;
for (int i = 0; i < size; i++) {
QString name = QString::fromUtf8(ceName (temp[i]));
int type = ceType (temp[i]);
QString typeString = getTypeString(type);
QString defaultVal = QString::fromUtf8(ceDefault (temp[i]));
if (type == NOTE) {
defaultVal = defaultVal.replace(QRegExp("\n"), " ");
}
if (i == 0) {
last = new QTreeWidgetItem(table);
}
else {
last = new QTreeWidgetItem(table, last);
}
last->setText(0, name);
last->setText(1, typeString);
last->setText(2, defaultVal);
if (type == BOOLEAN) {
last->setText(2, "");
int checked = defaultVal.toInt();
last->setCheckState(2, checked ? Qt::Checked : Qt::Unchecked);
// remove the ability to toggle the checkbox
last->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
else if (type == NOTE) {
last->setIcon(2, QIcon(":/icons/note.png"));
}
else if (type == DATE) {
if (defaultVal == "0") {
last->setText(2, ColumnEditor::tr("Today"));
}
else {
last->setText(2, ColumnEditor::tr("None"));
}
}
else if (type == TIME) {
if (defaultVal == "0") {
last->setText(2, ColumnEditor::tr("Now"));
}
else {
last->setText(2, ColumnEditor::tr("None"));
}
}
}
if (size == 0) {
stack->setCurrentWidget(noColumns);
}
else {
stack->setCurrentWidget(table);
}
}
/**
* Get the display name of the specified column type.
*
* @param type A column type ID
* @return A human-readable column type name
*/
QString DBEditor::getTypeString(int type)
{
if (type == STRING) {
return ColumnEditor::tr("String");
}
else if (type == INTEGER) {
return ColumnEditor::tr("Integer");
}
else if (type == FLOAT) {
return ColumnEditor::tr("Decimal");
}
else if (type == BOOLEAN) {
return ColumnEditor::tr("Boolean");
}
else if (type == NOTE) {
return ColumnEditor::tr("Note");
}
else if (type == DATE) {
return ColumnEditor::tr("Date");
}
else if (type == TIME) {
return ColumnEditor::tr("Time");
}
else if (type == CALC) {
return ColumnEditor::tr("Calculation");
}
else if (type == SEQUENCE) {
return ColumnEditor::tr("Sequence");
}
else if (type == IMAGE) {
return ColumnEditor::tr("Image");
}
else {
return db->getEnumName(type);
}
}
/**
* Apply all of the changes that have been made in this dialog to the
* database being edited.
*/
void DBEditor::applyChanges()
{
// handle deletions of original columns
int deleteCount = deletedCols.count();
int i;
for (i = 0; i < deleteCount; i++) {
QString name = deletedCols[i];
db->deleteColumn(name);
}
// make minor changes to remaining original columns
int oldCount = originalCols.count();
for (int oldIndex = 0; oldIndex < oldCount; oldIndex++) {
QString oldName = originalCols[oldIndex];
int index = info.Find(ceOldIndex [oldIndex]);
if (index == -1) {
// deleted already, move on
continue;
}
int newIndex = ceNewIndex (info[index]);
if (newIndex != oldIndex) {
db->setIndex(oldName, newIndex);
}
int type = ceType (info[index]);
if (type != CALC) {
QString defaultVal = QString::fromUtf8(ceDefault (info[index]));
db->setDefault(oldName, defaultVal);
}
}
// handle renames of original columns
for (i = 0; i < oldCount; i++) {
QString oldName = originalCols[i];
QString newName = renamedCols[i];
if (oldName != newName) {
if (!deletedCols.contains(oldName)) {
// hasn't been deleted, go ahead and rename
db->renameColumn(oldName, newName);
}
}
}
// add new columns
c4_View temp = info.Select(ceOldIndex [-1]);
int newCount = temp.GetSize();
for (i = 0; i < newCount; i++) {
int index = ceNewIndex (temp[i]);
QString name = QString::fromUtf8(ceName (temp[i]));
int type = ceType (temp[i]);
QString defaultVal = QString::fromUtf8(ceDefault (temp[i]));
if (type == CALC) {
defaultVal = "";
}
db->addColumn(index, name, type, defaultVal);
}
db->updateDataFormat();
// add and update calculations
NameCalcMap::Iterator iter;
for (iter = calcMap.begin(); iter != calcMap.end(); ++iter) {
QString name = iter.key();
CalcNode *calcRoot = iter.value();
db->updateCalc(name, calcRoot, decimalsMap[name]);
if (calcRoot != 0) {
delete calcRoot;
}
}
calcMap.clear();
}
/**
* Override QDialog's base resizing behavior to set the display table's
* column widths appropriately when the dialog is first resized as part of
* initial setup.
*
* @param event The dialog resizing event
*/
void DBEditor::resizeEvent(QResizeEvent *event)
{
QDialog::resizeEvent(event);
if (!resized) {
int colWidth = width() / 3 - 2;
table->setColumnWidth(0, colWidth);
table->setColumnWidth(1, colWidth);
table->setColumnWidth(2, colWidth);
resized = true;
}
}
/**
* List all of the currently defined column names, ordered by position.
*
* @return The list of column names
*/
QStringList DBEditor::columnNames()
{
QStringList names;
c4_View temp = info.SortOn(ceNewIndex);
int size = temp.GetSize();
for (int i = 0; i < size; i++) {
names.append(QString::fromUtf8(ceName (temp[i])));
}
return names;
}
/**
* List the types of each currently defined column, ordered by position.
*
* @return An array of column type IDs
*/
int *DBEditor::columnTypes()
{
c4_View temp = info.SortOn(ceNewIndex);
int size = temp.GetSize();
int *types = new int[size];
for (int i = 0; i < size; i++) {
types[i] = ceType (temp[i]);
}
return types;
}
/**
* Update any defined calculations to reflect a column renaming.
*
* @param oldName The old column name
* @param newName The new column name
*/
void DBEditor::renameColumnRefs(const QString &oldName, const QString &newName)
{
NameCalcMap::Iterator iter;
for (iter = calcMap.begin(); iter != calcMap.end(); ++iter) {
QString calcName = iter.key();
CalcNode *calcRoot = iter.value();
if (calcRoot != 0) {
int index = info.Find(ceName [calcName.toUtf8()]);
calcRoot->renameColumn(oldName, newName);
ceDefault (info[index]) = calcRoot->equation().toUtf8();
}
}
}
/**
* Update any defined calculations to reflect the deletion of a column.
*
* @param name The name of the column that was deleted
*/
void DBEditor::deleteColumnRefs(const QString &name)
{
NameCalcMap::Iterator iter;
QStringList deletions;
for (iter = calcMap.begin(); iter != calcMap.end(); ++iter) {
QString calcName = iter.key();
CalcNode *calcRoot = iter.value();
if (calcRoot != 0) {
int index = info.Find(ceName [calcName.toUtf8()]);
if (calcRoot->deleteColumn(name)) {
delete calcRoot;
deletions.append(calcName);
ceDefault (info[index]) = "";
}
else {
ceDefault (info[index]) = calcRoot->equation().toUtf8();
}
}
}
int count = deletions.count();
for (int i = 0; i < count; i++) {
calcMap.remove(deletions[i]);
calcMap.insert(deletions[i], 0);
}
}
|