00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00060
00061
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;
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()
00150 && (oldValue == newValue || (!oldValue.isValid() && !newValue.isValid()))
00151 && !forceAddCommand)
00152 {
00153 return;
00154 }
00155
00156 const bool prev_addHistoryCommand_in_slotPropertyChanged_enabled
00157 = addHistoryCommand_in_slotPropertyChanged_enabled;
00158 addHistoryCommand_in_slotPropertyChanged_enabled = false;
00159 if (property.value() != newValue)
00160 property.setValue( newValue, rememberOldValue );
00161 if (commandGroup) {
00162 commandGroup->addCommand(
00163 new ChangeFieldPropertyCommand( designerView, set, propertyName, oldValue, newValue,
00164 oldListData, property.listData()) );
00165 }
00166 delete oldListData;
00167 addHistoryCommand_in_slotPropertyChanged_enabled
00168 = prev_addHistoryCommand_in_slotPropertyChanged_enabled;
00169 }
00170
00171 void KexiTableDesignerViewPrivate::setPropertyValueIfNeeded(
00172 const KoProperty::Set& set, const QCString& propertyName,
00173 const QVariant& newValue, CommandGroup* commandGroup,
00174 bool forceAddCommand, bool rememberOldValue,
00175 QStringList* const slist, QStringList* const nlist)
00176 {
00177 KoProperty::Property& property = set[propertyName];
00178 QVariant oldValue( property.value() );
00179 setPropertyValueIfNeeded( set, propertyName, newValue, property.value(),
00180 commandGroup, forceAddCommand, rememberOldValue, slist, nlist);
00181 }
00182
00183 void KexiTableDesignerViewPrivate::setVisibilityIfNeeded( const KoProperty::Set& set, KoProperty::Property* prop,
00184 bool visible, bool &changed, CommandGroup *commandGroup )
00185 {
00186 if (prop->isVisible() != visible) {
00187 if (commandGroup) {
00188 commandGroup->addCommand(
00189 new ChangePropertyVisibilityCommand( designerView, set, prop->name(), visible ) );
00190 }
00191 prop->setVisible( visible );
00192 changed = true;
00193 }
00194 }
00195
00196 bool KexiTableDesignerViewPrivate::updatePropertiesVisibility(KexiDB::Field::Type fieldType, KoProperty::Set &set,
00197 CommandGroup *commandGroup)
00198 {
00199 bool changed = false;
00200 KoProperty::Property *prop;
00201 bool visible;
00202
00203 prop = &set["subType"];
00204 kexipluginsdbg << "subType=" << prop->value().toInt() << " type=" << set["type"].value().toInt()<< endl;
00205
00206
00207 visible = (prop->listData() && prop->listData()->keys.count() > 1 )
00208 && set["primaryKey"].value().toBool()==false;
00209 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00210
00211 prop = &set["objectType"];
00212 const bool isObjectTypeGroup = set["type"].value().toInt() == (int)KexiDB::Field::BLOB;
00213 visible = isObjectTypeGroup;
00214 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00215
00216 prop = &set["unsigned"];
00217 visible = KexiDB::Field::isNumericType(fieldType);
00218 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00219
00220 prop = &set["length"];
00221 visible = (fieldType == KexiDB::Field::Text);
00222 if (prop->isVisible()!=visible) {
00223
00224
00225 const int lengthToSet = visible ? KexiDB::Field::defaultTextLength() : 0;
00226 setPropertyValueIfNeeded( set, "length", lengthToSet,
00227 commandGroup, false, false );
00228
00229
00230
00231 }
00232 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00233 #ifndef KEXI_NO_UNFINISHED
00234 prop = &set["precision"];
00235 visible = KexiDB::Field::isFPNumericType(fieldType);
00236 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00237 #endif
00238 prop = &set["visibleDecimalPlaces"];
00239 visible = KexiDB::supportsVisibleDecimalPlacesProperty(fieldType);
00240 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00241
00242 prop = &set["unique"];
00243 visible = fieldType != KexiDB::Field::BLOB;
00244 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00245
00246 prop = &set["indexed"];
00247 visible = fieldType != KexiDB::Field::BLOB;
00248 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00249
00250 prop = &set["allowEmpty"];
00251 visible = KexiDB::Field::hasEmptyProperty(fieldType);
00252 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00253
00254 prop = &set["autoIncrement"];
00255 visible = KexiDB::Field::isAutoIncrementAllowed(fieldType);
00256 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00257
00259 #ifdef KEXI_NO_UNFINISHED
00260 prop = &set["defaultValue"];
00261 visible = !isObjectTypeGroup;
00262 setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
00263 #endif
00264
00265 return changed;
00266 }
00267
00268 QString KexiTableDesignerViewPrivate::messageForSavingChanges(bool &emptyTable)
00269 {
00270 KexiDB::Connection *conn = designerView->mainWin()->project()->dbConnection();
00271 bool ok;
00272 emptyTable = conn->isEmpty( *designerView->tempData()->table, ok ) && ok;
00273 return i18n("Do you want to save the design now?")
00274 + ( emptyTable ? QString::null :
00275 (QString("\n\n") + designerView->part()->i18nMessage(":additional message before saving design",
00276 designerView->parentDialog())) );
00277 }
00278
00279 void KexiTableDesignerViewPrivate::updateIconForItem(KexiTableItem &item, KoProperty::Set& set)
00280 {
00281 QVariant icon;
00282 if (!set["rowSource"].value().toString().isEmpty() && !set["rowSourceType"].value().toString().isEmpty())
00283 icon = "combo";
00284
00285 view->data()->clearRowEditBuffer();
00286 view->data()->updateRowEditBuffer(&item, COLUMN_ID_ICON, icon);
00287 view->data()->saveRowChanges(item, true);
00288 }
00289
00290 #include "kexitabledesignerview_p.moc"