File indexing completed on 2024-05-12 16:40:13

0001 /* This file is part of the KDE project
0002    Copyright (C) 2009 Adam Pigg <adam@piggz.co.uk>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License version 2 as published by the Free Software Foundation.
0007 
0008    This library is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    Library General Public License for more details.
0012 
0013    You should have received a copy of the GNU Library General Public License
0014    along with this library; see the file COPYING.LIB.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "AlterSchemaWidget.h"
0020 #include <KexiIcon.h>
0021 #include <KexiMainWindowIface.h>
0022 #include <kexiproject.h>
0023 #include <KexiWindow.h>
0024 #include <widget/KexiNameWidget.h>
0025 
0026 #include <KDbConnection>
0027 
0028 #include <KLocalizedString>
0029 
0030 #include <QGridLayout>
0031 #include <QTableView>
0032 #include <QComboBox>
0033 #include <QCheckBox>
0034 #include <QLabel>
0035 #include <QLineEdit>
0036 #include <QDebug>
0037 
0038 using namespace KexiMigration;
0039 
0040 AlterSchemaWidget::AlterSchemaWidget(QWidget *parent) : QWidget(parent)
0041 {
0042     m_schema = 0;
0043     m_selectedColumn = -1;
0044 
0045     m_layout = new QGridLayout();
0046     m_table = new QTableView(this);
0047     m_columnType = new QComboBox(this);
0048     m_columnPKey = new QCheckBox(this);
0049     m_tableNameWidget = new KexiNameWidget("",this);
0050 
0051     m_columnNumLabel = new QLabel(xi18n("Column %1", 1), this);
0052     m_columnTypeLabel = new QLabel(xi18n("Type"), this);
0053     m_columnPKeyLabel = new QLabel(xi18n("Primary Key"), this);
0054 
0055     m_types = KDbField::typeNames();
0056     m_types.removeFirst(); //Remove InvalidTypes
0057 
0058     for (unsigned int i = KDbField::FirstType; i <= KDbField::LastType; ++i) {
0059         m_columnType->addItem(KDbField::typeName(KDb::intToFieldType(i)), i);
0060     }
0061 
0062     m_layout->addWidget(m_tableNameWidget, 0, 0, 2, 3);
0063     m_layout->addWidget(m_columnNumLabel, 2, 0, 1, 3);
0064     m_layout->addWidget(m_columnTypeLabel, 3, 0, 1, 1);
0065     m_layout->addWidget(m_columnPKeyLabel, 3, 1, 1, 2);
0066     m_layout->addWidget(m_columnType, 4, 0, 1, 1);
0067     m_layout->addWidget(m_columnPKey, 4, 1, 1, 2);
0068     m_layout->addWidget(m_table, 5, 0, 1, 3);
0069 
0070     setLayout(m_layout);
0071 
0072     connect(m_table, SIGNAL(clicked(QModelIndex)), this, SLOT(tableClicked(QModelIndex)));
0073     connect(m_columnType, SIGNAL(activated(int)), this, SLOT(typeActivated(int)));
0074     connect(m_columnPKey, SIGNAL(clicked(bool)), this, SLOT(pkeyClicked(bool)));
0075 
0076     m_model = new AlterSchemaTableModel();
0077     m_table->setModel(m_model);
0078 }
0079 
0080 AlterSchemaWidget::~AlterSchemaWidget()
0081 {
0082     delete m_table;
0083     delete m_model;
0084     delete m_schema;
0085 }
0086 
0087 void AlterSchemaWidget::setTableSchema(KDbTableSchema* ts)
0088 {
0089     if (ts == m_schema) {
0090         return;
0091     }
0092     m_model->setSchema(ts);
0093     delete m_schema;
0094     m_schema = ts;
0095 
0096     m_tableNameWidget->setCaptionText(ts->captionOrName());
0097     m_tableNameWidget->captionLineEdit()->selectAll();
0098     m_tableNameWidget->captionLineEdit()->setFocus();
0099 
0100     m_model->setRowCount(3); // default
0101     tableClicked(m_model->index(0,0));
0102 }
0103 
0104 void AlterSchemaWidget::setData(QList<KDbRecordData*>* data)
0105 {
0106     m_model->setData(data);
0107 }
0108 
0109 void AlterSchemaWidget::tableClicked(const QModelIndex& idx)
0110 {
0111     m_selectedColumn = idx.column();
0112     m_columnNumLabel->setText(xi18n("Column %1", m_selectedColumn + 1));
0113     if (m_schema && m_selectedColumn < int(m_schema->fieldCount()) && m_schema->field(m_selectedColumn)) {
0114         qDebug() << m_schema->field(m_selectedColumn)->typeName() << m_types.indexOf(m_schema->field(m_selectedColumn)->typeName());
0115         m_columnType->setCurrentIndex(m_types.indexOf(m_schema->field(m_selectedColumn)->typeName()));
0116 
0117         //Only set the pkey check enabled if the field type is integer
0118         m_columnPKey->setEnabled(KDbField::isIntegerType(KDb::intToFieldType(m_columnType->itemData(m_types.indexOf(m_schema->field(m_selectedColumn)->typeName())).toInt())));
0119 
0120         m_columnPKey->setChecked(m_schema->field(m_selectedColumn)->isPrimaryKey());
0121     }
0122 }
0123 
0124 void AlterSchemaWidget::typeActivated(int typ)
0125 {
0126     if (!m_schema) {
0127         return;
0128     }
0129     m_schema->field(m_selectedColumn)->setType(KDb::intToFieldType(m_columnType->itemData(typ).toInt()));
0130 
0131     //Only set the pkey check enabled if the field type is integer
0132     const bool isInteger = KDbField::isIntegerType(
0133                                KDb::intToFieldType(m_columnType->itemData(typ).toInt()));
0134     m_columnPKey->setEnabled(isInteger);
0135 
0136     //If the field type is not integer, then the field cannot be a pkey
0137     if (!isInteger) {
0138         m_schema->field(m_selectedColumn)->setPrimaryKey(false);
0139     }
0140 }
0141 
0142 void AlterSchemaWidget::pkeyClicked(bool pkey)
0143 {
0144     if (!m_schema) {
0145         return;
0146     }
0147     m_schema->field(m_selectedColumn)->setAutoIncrement(pkey);
0148     m_schema->field(m_selectedColumn)->setPrimaryKey(pkey);
0149 }
0150 
0151 KDbTableSchema* AlterSchemaWidget::newSchema()
0152 {
0153     return m_schema;
0154 }
0155 
0156 KDbTableSchema* AlterSchemaWidget::takeTableSchema()
0157 {
0158     KDbTableSchema *schema = m_schema;
0159     m_schema = 0;
0160     return schema;
0161 }
0162 
0163 
0164 KexiNameWidget* AlterSchemaWidget::nameWidget()
0165 {
0166     return m_tableNameWidget;
0167 }
0168 
0169 AlterSchemaTableModel* AlterSchemaWidget::model()
0170 {
0171     return m_model;
0172 }
0173 
0174 QString AlterSchemaWidget::suggestedItemCaption(const QString& baseCaption)
0175 {
0176     unsigned int n = 0;
0177     QString newCaption;
0178     do {
0179         newCaption = baseCaption;
0180         if (n >= 1) {
0181             newCaption = baseCaption + QString::number(n);
0182         }
0183 
0184         if (nameExists(KDb::stringToIdentifier(newCaption))) {
0185             n++;
0186             continue; //stored exists!
0187         } else {
0188             break;
0189         }
0190     } while (n < 1000/*sanity*/);
0191 
0192     if (n == 1000) {
0193         newCaption = QString(""); //unable to find a usable name
0194     }
0195 
0196     return newCaption;
0197 }
0198 
0199 bool AlterSchemaWidget::nameExists(const QString & name) const
0200 {
0201     KexiPart::ItemDict* list
0202       = KexiMainWindowIface::global()->project()->itemsForPluginId("org.kexi-project.table");
0203     if (!list) {
0204         return false;
0205     }
0206 
0207     QHash<int, KexiPart::Item*>::const_iterator it = list->constBegin();
0208     while (it != list->constEnd()) {
0209         if (QString::compare(name, it.value()->name(), Qt::CaseInsensitive) == 0) {
0210             return true;
0211         }
0212         ++it;
0213     }
0214 
0215     return false;
0216 }