Initial commit to git repository
[liteman.git] / src / tableeditordialog.cpp
blobd720eeff601b822327f60ec6ef0605a2646588c6
1 /*
2 * This file is part of LineMan.
4 * Copyright 2006 Igor Khanin
6 * LiteMan is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * LiteMan is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with LiteMan; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <QTableWidget>
22 #include <QHeaderView>
23 #include <QPushButton>
24 #include <QComboBox>
25 #include <QGroupBox>
26 #include <QLineEdit>
27 #include <QLabel>
28 #include <QHash>
30 #include <QGridLayout>
31 #include <QHBoxLayout>
32 #include <QVBoxLayout>
34 #include "tableeditordialog.h"
35 #include "database.h"
37 /*!
38 * @brief Creates the table editor.
40 * @param parent The parent widget for the dialog.
41 * @param mode The operation mode for the dialog (Creating new table or editing existing one)
42 * @param tableName If mode is TableEditorDialog::AlterTable then this parameter contains the name of the
43 * table to edit.
45 TableEditorDialog::TableEditorDialog(QWidget * parent, Database * dbase, Mode mode, const QString & tableName): QDialog(parent)
47 db = dbase;
48 editorMode = mode;
49 currentTable = tableName;
51 initUI();
53 if(mode == AlterTable)
55 // As the only editing possibily we currently offer is the rename a table
56 // the fields table can be disabled
57 fieldsTable->setEnabled(false);
58 addButton->setEnabled(false);
59 removeButton->setEnabled(false);
61 // Initialize fields
62 nameEdit->setText(tableName);
64 FieldList fields = db->tableFields(tableName);
66 fieldsTable->setRowCount(fields.size());
67 for(int i = 0; i < fields.size(); i++)
69 QTableWidgetItem * nameItem = new QTableWidgetItem(fields[i].name);
70 QTableWidgetItem * typeItem = new QTableWidgetItem(fields[i].type);
71 QTableWidgetItem * commentItem = new QTableWidgetItem(fields[i].comment);
73 fieldsTable->setItem(i, 0, nameItem);
74 fieldsTable->setItem(i, 1, typeItem);
75 fieldsTable->setItem(i, 2, commentItem);
78 setWindowTitle(tr("Table Editor"));
80 else
82 removeButton->setEnabled(false); // Disable row removal
83 addField(); // A table should have at least one field
85 setWindowTitle(tr("New Table"));
89 TableEditorDialog::~TableEditorDialog()
93 /**
94 * \brief A function to sync the table defined in the dialog to the database.
96 * @param db A pointer to a database object to update
98 void TableEditorDialog::doUpdate()
100 if(!db)
101 return;
103 // Handle a rename update
104 if(editorMode == AlterTable)
106 if(!nameEdit->text().isEmpty())
107 db->renameTable(currentTable, nameEdit->text());
109 else // Handle a creation update
111 QString name;
112 FieldList fields;
114 name = nameEdit->text();
116 for(int i = 0; i < fieldsTable->rowCount(); i++)
118 QTableWidgetItem * nameItem = fieldsTable->item(i, 0);
119 QComboBox * typeBox = qobject_cast<QComboBox *>(fieldsTable->cellWidget(i, 1));
121 // For user convinence reasons, the type "INTEGER PRIMARY KEY" is presented to the user
122 // as "Primary Key" alone. Therefor, untill there is a more robust solution (which will
123 // support translation of type names as well) the primary key type needs to be corrected
124 // at update time.
125 QString type = typeBox->currentText();
126 if(type == "Primary Key")
127 type = "Integer Primary Key";
129 DatabaseTableField field = {nameItem->text(), type.toUpper(), QString()};
130 fields.append(field);
133 if(!name.isEmpty() && !fields.isEmpty())
134 db->createTable(name, fields);
138 void TableEditorDialog::initUI()
140 QPushButton * okButton = new QPushButton(tr("OK"));
141 QPushButton * cancelButton = new QPushButton(tr("Cancel"));
143 connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
144 connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
146 QLabel * nameLabel = new QLabel(tr("Table Name: "));
147 nameEdit = new QLineEdit();
149 fieldsTable = new QTableWidget();
150 fieldsTable->setSortingEnabled(false);
151 fieldsTable->setColumnCount(3);
153 fieldsTable->setHorizontalHeaderLabels(QStringList() << tr("Name") << tr("Type") << tr("Comment"));
154 fieldsTable->horizontalHeader()->setStretchLastSection(true);
155 fieldsTable->verticalHeader()->hide();
157 connect(fieldsTable, SIGNAL(itemSelectionChanged()), this, SLOT(fieldSelected()));
159 addButton = new QPushButton(tr("+"));
160 removeButton = new QPushButton(tr("-"));
162 connect(addButton, SIGNAL(clicked()), this, SLOT(addField()));
163 connect(removeButton, SIGNAL(clicked()), this, SLOT(removeField()));
166 // Layout
168 QHBoxLayout * nameLayout = new QHBoxLayout();
169 nameLayout->addWidget(nameLabel);
170 nameLayout->addWidget(nameEdit);
172 QHBoxLayout * fieldsButtonLayout = new QHBoxLayout();
173 fieldsButtonLayout->addStretch(1);
174 fieldsButtonLayout->addWidget(addButton);
175 fieldsButtonLayout->addWidget(removeButton);
177 QVBoxLayout * fieldsLayout = new QVBoxLayout();
178 fieldsLayout->addWidget(fieldsTable);
179 fieldsLayout->addLayout(fieldsButtonLayout);
181 QGroupBox * fieldsBox = new QGroupBox(tr("Fields"));
182 fieldsBox->setLayout(fieldsLayout);
184 QHBoxLayout * buttonLayout = new QHBoxLayout();
185 buttonLayout->addStretch(1);
186 buttonLayout->addWidget(okButton);
187 buttonLayout->addWidget(cancelButton);
189 QVBoxLayout * mainLayout = new QVBoxLayout();
190 mainLayout->addLayout(nameLayout);
191 mainLayout->addWidget(fieldsBox, 1);
192 mainLayout->addLayout(buttonLayout);
194 setLayout(mainLayout);
197 QComboBox * TableEditorDialog::makeTypeBox()
199 QComboBox * box;
200 QStringList types;
202 types << "Text" << "Primary Key" << "Integer" << "Real" << "Blob" << "Null";
204 box = new QComboBox();
205 box->addItems(types);
207 return box;
210 void TableEditorDialog::addField()
212 fieldsTable->setRowCount(fieldsTable->rowCount() + 1);
213 fieldsTable->setCellWidget(fieldsTable->rowCount() - 1, 1, makeTypeBox());
216 void TableEditorDialog::removeField()
218 fieldsTable->removeRow(fieldsTable->currentRow());
221 void TableEditorDialog::fieldSelected()
223 int i, row;
224 bool sameRow;
225 QList<QTableWidgetItem *> selected = fieldsTable->selectedItems();
227 // The following is a bit of an hack to find out wheather the selected items
228 // are in the same row (that is, a row has been selected), but I couldn't find
229 // another way.
231 i = 0;
232 sameRow = true;
233 while(i < selected.size() && sameRow)
235 if(i == 0)
236 row = fieldsTable->row(selected[i]);
237 else
238 if(fieldsTable->row(selected[i]) != row)
239 sameRow = false;
241 i++;
244 if(selected.count() == 0 || !sameRow)
245 removeButton->setEnabled(false);
246 else
247 removeButton->setEnabled(true);