kexi

kexitabledesignerview_p.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexitabledesignerview_p.h"
00021 #include "kexitabledesignerview.h"
00022 
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <qsplitter.h>
00026 
00027 #include <kiconloader.h>
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 #include <kaction.h>
00031 #include <kpopupmenu.h>
00032 #include <kmessagebox.h>
00033 
00034 #include <koproperty/set.h>
00035 
00036 #include <kexidb/cursor.h>
00037 #include <kexidb/tableschema.h>
00038 #include <kexidb/connection.h>
00039 #include <kexidb/utils.h>
00040 #include <kexidb/roweditbuffer.h>
00041 #include <kexidb/error.h>
00042 #include <kexiutils/identifier.h>
00043 #include <kexiproject.h>
00044 #include <keximainwindow.h>
00045 #include <widget/tableview/kexidataawarepropertyset.h>
00046 #include <widget/kexicustompropertyfactory.h>
00047 #include <kexiutils/utils.h>
00048 #include <kexidialogbase.h>
00049 #include <kexitableview.h>
00050 #include "kexitabledesignercommands.h"
00051 
00052 using namespace KexiTableDesignerCommands;
00053 
00054 //----------------------------------------------
00055 
00056 CommandHistory::CommandHistory(KActionCollection *actionCollection, bool withMenus)
00057  : KCommandHistory(actionCollection, withMenus)
00058 {
00059     // We need ALL the commands because we'll collect reuse their 
00060     // data before performing alter table, so set that to the maximum, 
00061     // as KCommandHistory has default = 50.
00062     setUndoLimit(INT_MAX);
00063     setRedoLimit(INT_MAX);
00064 }
00065 
00066 void CommandHistory::addCommand(KCommand *command, bool execute)
00067 {
00068     KCommandHistory::addCommand(command, execute);
00069     m_commandsToUndo.append(command);
00070 }
00071 
00072 void CommandHistory::undo()
00073 {
00074     if (!m_commandsToUndo.isEmpty()) {
00075         KCommand * cmd = m_commandsToUndo.take( m_commandsToUndo.count()-1 );
00076         m_commandsToRedo.append( cmd );
00077     }
00078     KCommandHistory::undo();
00079 }
00080 
00081 void CommandHistory::redo()
00082 {
00083     if (!m_commandsToRedo.isEmpty()) {
00084         KCommand * cmd = m_commandsToRedo.take( m_commandsToRedo.count()-1 );
00085         m_commandsToUndo.append( cmd );
00086     }
00087     KCommandHistory::redo();
00088 }
00089 
00090 //----------------------------------------------
00091 
00092 KexiTableDesignerViewPrivate::KexiTableDesignerViewPrivate(KexiTableDesignerView* aDesignerView)
00093  : designerView(aDesignerView)
00094  , sets(0)
00095  , uniqueIdCounter(0)
00096  , dontAskOnStoreData(false)
00097  , slotTogglePrimaryKeyCalled(false)
00098  , primaryKeyExists(false)
00099  , slotPropertyChanged_primaryKey_enabled(true)
00100  , slotPropertyChanged_subType_enabled(true)
00101  , addHistoryCommand_in_slotPropertyChanged_enabled(true)
00102  , addHistoryCommand_in_slotRowUpdated_enabled(true)
00103  , addHistoryCommand_in_slotAboutToDeleteRow_enabled(true)
00104  , addHistoryCommand_in_slotRowInserted_enabled(true)
00105  , slotBeforeCellChanged_enabled(true)
00106  , tempStoreDataUsingRealAlterTable(false)
00107 {
00108     historyActionCollection = new KActionCollection((QWidget*)0,"");
00109     history = new CommandHistory(historyActionCollection, true);
00110 
00111     internalPropertyNames.insert("subType",(char*)1);
00112     internalPropertyNames.insert("uid",(char*)1);
00113     internalPropertyNames.insert("newrow",(char*)1);
00114     internalPropertyNames.insert("rowSource",(char*)1);
00115     internalPropertyNames.insert("rowSourceType",(char*)1);
00116     internalPropertyNames.insert("boundColumn",(char*)1);
00117     internalPropertyNames.insert("visibleColumn",(char*)1);
00118 }
00119 
00120 KexiTableDesignerViewPrivate::~KexiTableDesignerViewPrivate() {
00121     delete sets;
00122     delete historyActionCollection;
00123     delete history;
00124 }
00125 
00126 int KexiTableDesignerViewPrivate::generateUniqueId()
00127 {
00128     return ++uniqueIdCounter;
00129 }
00130 
00131 void KexiTableDesignerViewPrivate::setPropertyValueIfNeeded( 
00132     const KoProperty::Set& set, const QCString& propertyName, 
00133     const QVariant& newValue, const QVariant& oldValue, CommandGroup* commandGroup, 
00134     bool forceAddCommand, bool rememberOldValue,
00135     QStringList* const slist, QStringList* const nlist)
00136 {
00137     KoProperty::Property& property = set[propertyName];
00138 
00139     KoProperty::Property::ListData *oldListData = property.listData() ? 
00140         new KoProperty::Property::ListData(*property.listData()) : 0; //remember because we'll change list data soon
00141     if (slist && nlist) {
00142         if (slist->isEmpty() || nlist->isEmpty()) {
00143             property.setListData(0);
00144         }
00145         else {
00146             property.setListData(*slist, *nlist);
00147         }
00148     }
00149     if (oldValue.type() == newValue.type() && oldValue == newValue && !forceAddCommand)
00150         return;
00151 
00152     const bool prev_addHistoryCommand_in_slotPropertyChanged_enabled 
00153         = addHistoryCommand_in_slotPropertyChanged_enabled; //remember
00154     addHistoryCommand_in_slotPropertyChanged_enabled = false;
00155     if (property.value() != newValue)
00156         property.setValue( newValue, rememberOldValue );
00157     if (commandGroup) {
00158         commandGroup->addCommand(
00159             new ChangeFieldPropertyCommand( designerView, set, propertyName, oldValue, newValue,
00160                 oldListData, property.listData()) );
00161     }
00162     delete oldListData;
00163     addHistoryCommand_in_slotPropertyChanged_enabled 
00164         = prev_addHistoryCommand_in_slotPropertyChanged_enabled; //restore
00165 }
00166 
00167 void KexiTableDesignerViewPrivate::setPropertyValueIfNeeded( 
00168     const KoProperty::Set& set, const QCString& propertyName, 
00169     const QVariant& newValue, CommandGroup* commandGroup, 
00170     bool forceAddCommand, bool rememberOldValue,
00171     QStringList* const slist, QStringList* const nlist)
00172 {
00173     KoProperty::Property& property = set[propertyName];
00174     QVariant oldValue( property.value() );
00175     setPropertyValueIfNeeded( set, propertyName, newValue, property.value(), 
00176         commandGroup, forceAddCommand, rememberOldValue, slist, nlist);
00177 }
00178 
00179 void KexiTableDesignerViewPrivate::setVisibilityIfNeeded( const KoProperty::Set& set, KoProperty::Property* prop, 
00180     bool visible, bool &changed, CommandGroup *commandGroup )
00181 {
00182     if (prop->isVisible() != visible) {
00183         if (commandGroup) {
00184             commandGroup->addCommand( 
00185                 new ChangePropertyVisibilityCommand( designerView, set, prop->name(), visible ) );
00186         }
00187         prop->setVisible( visible );
00188         changed = true;
00189     }
00190 }
00191 
00192 bool KexiTableDesignerViewPrivate::updatePropertiesVisibility(KexiDB::Field::Type fieldType, KoProperty::Set &set,
00193     CommandGroup *commandGroup)
00194 {
00195     bool changed = false;
00196     KoProperty::Property *prop;
00197     bool visible;
00198     
00199     prop = &set["subType"];
00200     kexipluginsdbg << "subType=" << prop->value().toInt() << " type=" << set["type"].value().toInt()<< endl;
00201 
00202     //if there is no more than 1 subType name or it's a PK: hide the property
00203     visible = (prop->listData() && prop->listData()->keys.count() > 1 /*disabled || isObjectTypeGroup*/)
00204         && set["primaryKey"].value().toBool()==false;
00205     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00206 
00207     prop = &set["objectType"];
00208     const bool isObjectTypeGroup = set["type"].value().toInt() == (int)KexiDB::Field::BLOB; // used only for BLOBs
00209     visible = isObjectTypeGroup;
00210     setVisibilityIfNeeded( set, prop,  visible, changed, commandGroup );
00211 
00212     prop = &set["unsigned"];
00213     visible = KexiDB::Field::isNumericType(fieldType);
00214     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00215 
00216     prop = &set["length"];
00217     visible = (fieldType == KexiDB::Field::Text);
00218     if (prop->isVisible()!=visible) {
00219 //              prop->setVisible( visible );
00220         //update the length when it makes sense
00221         const int lengthToSet = visible ? KexiDB::Field::defaultTextLength() : 0;
00222         setPropertyValueIfNeeded( set, "length", lengthToSet, 
00223             commandGroup, false, false  );
00224 //      if (lengthToSet != prop->value().toInt())
00225 //          prop->setValue( lengthToSet, false );
00226 //              changed = true;
00227     }
00228     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00229 #ifndef KEXI_NO_UNFINISHED
00230     prop = &set["precision"];
00231     visible = KexiDB::Field::isFPNumericType(fieldType);
00232     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00233 #endif
00234     prop = &set["visibleDecimalPlaces"];
00235     visible = KexiDB::supportsVisibleDecimalPlacesProperty(fieldType);
00236     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00237 
00238     prop = &set["unique"];
00239     visible = fieldType != KexiDB::Field::BLOB;
00240     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00241 
00242     prop = &set["indexed"];
00243     visible = fieldType != KexiDB::Field::BLOB;
00244     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00245 
00246     prop = &set["allowEmpty"];
00247     visible = KexiDB::Field::hasEmptyProperty(fieldType);
00248     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00249 
00250     prop = &set["autoIncrement"];
00251     visible = KexiDB::Field::isAutoIncrementAllowed(fieldType);
00252     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00253 
00255 #ifdef KEXI_NO_UNFINISHED
00256     prop = &set["defaultValue"];
00257     visible = !isObjectTypeGroup;
00258     setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00259 #endif
00260 
00261     return changed;
00262 }
00263 
00264 QString KexiTableDesignerViewPrivate::messageForSavingChanges(bool &emptyTable)
00265 {
00266     KexiDB::Connection *conn = designerView->mainWin()->project()->dbConnection();
00267     bool ok;
00268     emptyTable = conn->isEmpty( *designerView->tempData()->table, ok ) && ok;
00269     return i18n("Do you want to save the design now?")
00270     + ( emptyTable ? QString::null :
00271         (QString("\n\n") + designerView->part()->i18nMessage(":additional message before saving design", 
00272         designerView->parentDialog())) );
00273 }
00274 
00275 void KexiTableDesignerViewPrivate::updateIconForItem(KexiTableItem &item, KoProperty::Set& set)
00276 {
00277     QVariant icon;
00278     if (!set["rowSource"].value().toString().isEmpty() && !set["rowSourceType"].value().toString().isEmpty())
00279         icon = "combo";
00280     //show/hide icon in the table
00281     view->data()->clearRowEditBuffer();
00282     view->data()->updateRowEditBuffer(&item, COLUMN_ID_ICON, icon);
00283     view->data()->saveRowChanges(item, true);
00284 }
00285 
00286 #include "kexitabledesignerview_p.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys