summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/tables/kexitabledesignerview_p.cpp
blob: cf98b6837b2a6de6d75db36c63d13be17bfa89f0 (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2004-2007 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexitabledesignerview_p.h"
#include "kexitabledesignerview.h"

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqsplitter.h>

#include <kiconloader.h>
#include <kdebug.h>
#include <klocale.h>
#include <kaction.h>
#include <kpopupmenu.h>
#include <kmessagebox.h>

#include <koproperty/set.h>

#include <kexidb/cursor.h>
#include <kexidb/tableschema.h>
#include <kexidb/connection.h>
#include <kexidb/utils.h>
#include <kexidb/roweditbuffer.h>
#include <kexidb/error.h>
#include <kexiutils/identifier.h>
#include <kexiproject.h>
#include <keximainwindow.h>
#include <widget/tableview/kexidataawarepropertyset.h>
#include <widget/kexicustompropertyfactory.h>
#include <kexiutils/utils.h>
#include <kexidialogbase.h>
#include <kexitableview.h>
#include "kexitabledesignercommands.h"

using namespace KexiTableDesignerCommands;

//----------------------------------------------

CommandHistory::CommandHistory(KActionCollection *actionCollection, bool withMenus)
 : KCommandHistory(actionCollection, withMenus)
{
	// We need ALL the commands because we'll collect reuse their 
	// data before performing alter table, so set that to the maximum, 
	// as KCommandHistory has default = 50.
	setUndoLimit(INT_MAX);
	setRedoLimit(INT_MAX);
}

void CommandHistory::addCommand(KCommand *command, bool execute)
{
	KCommandHistory::addCommand(command, execute);
	m_commandsToUndo.append(command);
}

void CommandHistory::undo()
{
	if (!m_commandsToUndo.isEmpty()) {
		KCommand * cmd = m_commandsToUndo.take( m_commandsToUndo.count()-1 );
		m_commandsToRedo.append( cmd );
	}
	KCommandHistory::undo();
}

void CommandHistory::redo()
{
	if (!m_commandsToRedo.isEmpty()) {
		KCommand * cmd = m_commandsToRedo.take( m_commandsToRedo.count()-1 );
		m_commandsToUndo.append( cmd );
	}
	KCommandHistory::redo();
}

void CommandHistory::clear() {
	KCommandHistory::clear(); m_commandsToUndo.clear();
}

//----------------------------------------------

KexiTableDesignerViewPrivate::KexiTableDesignerViewPrivate(KexiTableDesignerView* aDesignerView)
 : designerView(aDesignerView)
 , sets(0)
 , uniqueIdCounter(0)
 , dontAskOnStoreData(false)
 , slotTogglePrimaryKeyCalled(false)
 , primaryKeyExists(false)
 , slotPropertyChanged_primaryKey_enabled(true)
 , slotPropertyChanged_subType_enabled(true)
 , addHistoryCommand_in_slotPropertyChanged_enabled(true)
 , addHistoryCommand_in_slotRowUpdated_enabled(true)
 , addHistoryCommand_in_slotAboutToDeleteRow_enabled(true)
 , addHistoryCommand_in_slotRowInserted_enabled(true)
 , slotBeforeCellChanged_enabled(true)
 , tempStoreDataUsingRealAlterTable(false)
{
	historyActionCollection = new KActionCollection((TQWidget*)0,"");
	history = new CommandHistory(historyActionCollection, true);

	internalPropertyNames.insert("subType",(char*)1);
	internalPropertyNames.insert("uid",(char*)1);
	internalPropertyNames.insert("newrow",(char*)1);
	internalPropertyNames.insert("rowSource",(char*)1);
	internalPropertyNames.insert("rowSourceType",(char*)1);
	internalPropertyNames.insert("boundColumn",(char*)1);
	internalPropertyNames.insert("visibleColumn",(char*)1);
}

KexiTableDesignerViewPrivate::~KexiTableDesignerViewPrivate() {
	delete sets;
	delete historyActionCollection;
	delete history;
}

int KexiTableDesignerViewPrivate::generateUniqueId()
{
	return ++uniqueIdCounter;
}

void KexiTableDesignerViewPrivate::setPropertyValueIfNeeded( 
	const KoProperty::Set& set, const TQCString& propertyName, 
	const TQVariant& newValue, const TQVariant& oldValue, CommandGroup* commandGroup, 
	bool forceAddCommand, bool rememberOldValue,
	TQStringList* const slist, TQStringList* const nlist)
{
	KoProperty::Property& property = set[propertyName];

	KoProperty::Property::ListData *oldListData = property.listData() ? 
		new KoProperty::Property::ListData(*property.listData()) : 0; //remember because we'll change list data soon
	if (slist && nlist) {
		if (slist->isEmpty() || nlist->isEmpty()) {
			property.setListData(0);
		}
		else {
			property.setListData(*slist, *nlist);
		}
	}
	if (oldValue.type() == newValue.type() 
		&& (oldValue == newValue || (!oldValue.isValid() && !newValue.isValid()))
		&& !forceAddCommand)
	{
		return;
	}

	const bool prev_addHistoryCommand_in_slotPropertyChanged_enabled 
		= addHistoryCommand_in_slotPropertyChanged_enabled; //remember
	addHistoryCommand_in_slotPropertyChanged_enabled = false;
	if (property.value() != newValue)
		property.setValue( newValue, rememberOldValue );
	if (commandGroup) {
		commandGroup->addCommand(
			new ChangeFieldPropertyCommand( designerView, set, propertyName, oldValue, newValue,
				oldListData, property.listData()) );
	}
	delete oldListData;
	addHistoryCommand_in_slotPropertyChanged_enabled 
		= prev_addHistoryCommand_in_slotPropertyChanged_enabled; //restore
}

void KexiTableDesignerViewPrivate::setPropertyValueIfNeeded( 
	const KoProperty::Set& set, const TQCString& propertyName, 
	const TQVariant& newValue, CommandGroup* commandGroup, 
	bool forceAddCommand, bool rememberOldValue,
	TQStringList* const slist, TQStringList* const nlist)
{
	KoProperty::Property& property = set[propertyName];
	TQVariant oldValue( property.value() );
	setPropertyValueIfNeeded( set, propertyName, newValue, property.value(), 
		commandGroup, forceAddCommand, rememberOldValue, slist, nlist);
}

void KexiTableDesignerViewPrivate::setVisibilityIfNeeded( const KoProperty::Set& set, KoProperty::Property* prop, 
	bool visible, bool &changed, CommandGroup *commandGroup )
{
	if (prop->isVisible() != visible) {
		if (commandGroup) {
			commandGroup->addCommand( 
				new ChangePropertyVisibilityCommand( designerView, set, prop->name(), visible ) );
		}
		prop->setVisible( visible );
		changed = true;
	}
}

bool KexiTableDesignerViewPrivate::updatePropertiesVisibility(KexiDB::Field::Type fieldType, KoProperty::Set &set,
	CommandGroup *commandGroup)
{
	bool changed = false;
	KoProperty::Property *prop;
	bool visible;
	
	prop = &set["subType"];
	kexipluginsdbg << "subType=" << prop->value().toInt() << " type=" << set["type"].value().toInt()<< endl;

	//if there is no more than 1 subType name or it's a PK: hide the property
	visible = (prop->listData() && prop->listData()->keys.count() > 1 /*disabled || isObjectTypeGroup*/)
		&& set["primaryKey"].value().toBool()==false;
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );

	prop = &set["objectType"];
	const bool isObjectTypeGroup = set["type"].value().toInt() == (int)KexiDB::Field::BLOB; // used only for BLOBs
	visible = isObjectTypeGroup;
	setVisibilityIfNeeded( set, prop,  visible, changed, commandGroup );

	prop = &set["unsigned"];
	visible = KexiDB::Field::isNumericType(fieldType);
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );

	prop = &set["length"];
	visible = (fieldType == KexiDB::Field::Text);
	if (prop->isVisible()!=visible) {
//				prop->setVisible( visible );
		//update the length when it makes sense
		const int lengthToSet = visible ? KexiDB::Field::defaultTextLength() : 0;
		setPropertyValueIfNeeded( set, "length", lengthToSet, 
			commandGroup, false, false /*!rememberOldValue*/ );
//		if (lengthToSet != prop->value().toInt())
//			prop->setValue( lengthToSet, false );
//				changed = true;
	}
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
#ifndef KEXI_NO_UNFINISHED
	prop = &set["precision"];
	visible = KexiDB::Field::isFPNumericType(fieldType);
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
#endif
	prop = &set["visibleDecimalPlaces"];
	visible = KexiDB::supportsVisibleDecimalPlacesProperty(fieldType);
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );

	prop = &set["unique"];
	visible = fieldType != KexiDB::Field::BLOB;
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );

	prop = &set["indexed"];
	visible = fieldType != KexiDB::Field::BLOB;
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );

	prop = &set["allowEmpty"];
	visible = KexiDB::Field::hasEmptyProperty(fieldType);
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );

	prop = &set["autoIncrement"];
	visible = KexiDB::Field::isAutoIncrementAllowed(fieldType);
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );

//! @todo remove this when BLOB supports default value
#ifdef KEXI_NO_UNFINISHED
	prop = &set["defaultValue"];
	visible = !isObjectTypeGroup;
	setVisibilityIfNeeded( set, prop, visible, changed, commandGroup );
#endif

	return changed;
}

TQString KexiTableDesignerViewPrivate::messageForSavingChanges(bool &emptyTable, bool skipWarning)
{
	KexiDB::Connection *conn = designerView->mainWin()->project()->dbConnection();
	bool ok;
	emptyTable = conn->isEmpty( *designerView->tempData()->table, ok ) && ok;
	return i18n("Do you want to save the design now?")
	+ ( (emptyTable || skipWarning) ? TQString() :
		(TQString("\n\n") + designerView->part()->i18nMessage(":additional message before saving design", 
		designerView->tqparentDialog())) );
}

void KexiTableDesignerViewPrivate::updateIconForItem(KexiTableItem &item, KoProperty::Set& set)
{
	TQVariant icon;
	if (!set["rowSource"].value().toString().isEmpty() && !set["rowSourceType"].value().toString().isEmpty())
		icon = "combo";
	//show/hide icon in the table
	view->KexiDataAwareObjectInterface::data()->clearRowEditBuffer();
	view->KexiDataAwareObjectInterface::data()->updateRowEditBuffer(&item, COLUMN_ID_ICON, icon);
	view->KexiDataAwareObjectInterface::data()->saveRowChanges(item, true);
}

#include "kexitabledesignerview_p.moc"