1
/* This file is part of the KDE project
2
Copyright (C) 2006-2012 JarosÅaw Staniek <staniek@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
22
#include <KLocalizedString>
25
#include <kexi_global.h>
27
#include "kexitabledesignercommands.h"
29
using namespace KexiTableDesignerCommands;
32
Command::Command(const KUndo2MagicString &text, Command *parent, KexiTableDesignerView* view)
33
: KUndo2Command(text, parent)
35
, m_blockRedoOnce(false)
39
Command::Command(Command* parent, KexiTableDesignerView* view)
40
: KUndo2Command(KUndo2MagicString(), parent)
42
, m_blockRedoOnce(false)
52
if (m_blockRedoOnce) {
53
m_blockRedoOnce = false;
64
void Command::redoInternal()
68
void Command::undoInternal()
72
void Command::blockRedoOnce()
74
m_blockRedoOnce = true;
77
//--------------------------------------------------------
79
ChangeFieldPropertyCommand::ChangeFieldPropertyCommand(
80
Command* parent, KexiTableDesignerView* view,
81
const KPropertySet& set, const QByteArray& propertyName,
82
const QVariant& oldValue, const QVariant& newValue,
83
KPropertyListData* const oldListData,
84
KPropertyListData* const newListData)
85
: Command(parent, view)
87
propertyName == "name" ? oldValue.toString() : set.property("name").value().toString(),
88
propertyName, newValue, set["uid"].value().toInt())
89
, m_oldValue(oldValue)
90
, m_oldListData(oldListData ? new KPropertyListData(*oldListData) : 0)
91
, m_listData(newListData ? new KPropertyListData(*newListData) : 0)
93
setText(kundo2_i18n("Change <resource>%1</resource> property for table field from "
94
"<resource>%2</resource> to <resource>%3</resource>",
95
m_alterTableAction.propertyName(),
96
m_oldValue.toString(),
97
m_alterTableAction.newValue().toString()));
99
qDebug() << debugString();
102
ChangeFieldPropertyCommand::~ChangeFieldPropertyCommand()
104
delete m_oldListData;
108
QString ChangeFieldPropertyCommand::debugString() const
110
QString s(text().toString());
111
if (m_oldListData || m_listData)
112
s += QString("\nAnd list data from [%1]\n to [%2]")
115
.arg(m_oldListData->keysAsStringList().join(",")).arg(m_oldListData->names.join(","))
119
.arg(m_listData->keysAsStringList().join(",")).arg(m_listData->names.join(","))
120
: QString("<NONE>"));
121
return s + QString(" (UID=%1)").arg(m_alterTableAction.uid());
124
void ChangeFieldPropertyCommand::redoInternal()
126
m_view->changeFieldProperty(
127
m_alterTableAction.uid(),
128
m_alterTableAction.propertyName().toLatin1(),
129
m_alterTableAction.newValue(), m_listData);
132
void ChangeFieldPropertyCommand::undoInternal()
134
m_view->changeFieldProperty(
135
m_alterTableAction.uid(),
136
m_alterTableAction.propertyName().toLatin1(),
137
m_oldValue, m_oldListData);
140
KDbAlterTableHandler::ActionBase* ChangeFieldPropertyCommand::createAction() const
142
if (m_alterTableAction.propertyName() == "subType") {//skip these properties
145
return new KDbAlterTableHandler::ChangeFieldPropertyAction(m_alterTableAction);
148
//--------------------------------------------------------
150
RemoveFieldCommand::RemoveFieldCommand(Command* parent, KexiTableDesignerView* view, int fieldIndex,
151
const KPropertySet* set)
152
: Command(parent, view)
153
, m_alterTableAction(set ? (*set)["name"].value().toString() : QString(),
154
set ? (*set)["uid"].value().toInt() : -1)
155
, m_set(set ? new KPropertySet(*set /*deep copy*/) : 0)
156
, m_fieldIndex(fieldIndex)
159
setText(kundo2_i18n("Remove table field <resource>%1</resource>", m_alterTableAction.fieldName()));
161
setText(kundo2_i18n("Remove empty row at position %1", m_fieldIndex));
164
RemoveFieldCommand::~RemoveFieldCommand()
169
void RemoveFieldCommand::redoInternal()
171
// m_view->deleteField( m_fieldIndex );
172
m_view->deleteRecord(m_fieldIndex);
175
void RemoveFieldCommand::undoInternal()
177
m_view->insertEmptyRecord(m_fieldIndex);
179
m_view->insertField(m_fieldIndex, *m_set);
182
QString RemoveFieldCommand::debugString() const
185
return text().toString();
187
return text().toString() + "\nAT ROW " + QString::number(m_fieldIndex)
188
+ ", FIELD: " + (*m_set)["caption"].value().toString()
189
+ QString(" (UID=%1)").arg(m_alterTableAction.uid());
192
KDbAlterTableHandler::ActionBase* RemoveFieldCommand::createAction() const
194
return new KDbAlterTableHandler::RemoveFieldAction(m_alterTableAction);
197
//--------------------------------------------------------
199
InsertFieldCommand::InsertFieldCommand(Command* parent, KexiTableDesignerView* view,
200
int fieldIndex/*, const KDbField& field*/, const KPropertySet& set)
201
: Command(parent, view)
202
, m_set(set) //? new KPropertySet(*set) : 0 )
204
KDbField *f = view->buildField(m_set);
206
m_alterTableAction = new KDbAlterTableHandler::InsertFieldAction(
207
fieldIndex, f, set["uid"].value().toInt());
209
m_alterTableAction = new KDbAlterTableHandler::InsertFieldAction;
211
setText(kundo2_i18n("Insert table field \"%1\"", m_set["caption"].value().toString()));
214
InsertFieldCommand::~InsertFieldCommand()
216
delete m_alterTableAction;
219
void InsertFieldCommand::redoInternal()
221
m_view->insertField(m_alterTableAction->index(), /*m_alterTableAction.field(),*/ m_set);
224
void InsertFieldCommand::undoInternal()
226
m_view->clearRecord(m_alterTableAction->index()); //m_alterTableAction.index() );
229
KDbAlterTableHandler::ActionBase* InsertFieldCommand::createAction() const
231
return new KDbAlterTableHandler::InsertFieldAction(*m_alterTableAction);
234
QString InsertFieldCommand::debugString() const
236
return text().toString() + "\nAT ROW " + QString::number(m_alterTableAction->index())
237
+ ", FIELD: " + m_set["caption"].value().toString();
240
//--------------------------------------------------------
242
ChangePropertyVisibilityCommand::ChangePropertyVisibilityCommand(Command* parent, KexiTableDesignerView* view,
243
const KPropertySet& set, const QByteArray& propertyName, bool visible)
244
: Command(parent, view)
245
, m_alterTableAction(set.property("name").value().toString(), propertyName, visible, set["uid"].value().toInt())
246
, m_oldVisibility(set.property(propertyName).isVisible())
248
setText(kundo2_noi18n("[internal] Change <resource>%1</resource> visibility from "
249
"<resource>%2</resource> to <resource>%3</resource>",
250
m_alterTableAction.propertyName(),
251
m_oldVisibility ? "true" : "false",
252
m_alterTableAction.newValue().toBool() ? "true" : "false"));
254
qDebug() << debugString();
257
ChangePropertyVisibilityCommand::~ChangePropertyVisibilityCommand()
261
void ChangePropertyVisibilityCommand::redoInternal()
263
m_view->changePropertyVisibility(
264
m_alterTableAction.uid(),
265
m_alterTableAction.propertyName().toLatin1(),
266
m_alterTableAction.newValue().toBool());
269
void ChangePropertyVisibilityCommand::undoInternal()
271
m_view->changePropertyVisibility(
272
m_alterTableAction.uid(),
273
m_alterTableAction.propertyName().toLatin1(),
277
//--------------------------------------------------------
279
InsertEmptyRecordCommand::InsertEmptyRecordCommand(Command* parent, KexiTableDesignerView* view, int row)
280
: Command(parent, view)
283
setText(kundo2_noi18n("Insert empty row at position %1", m_row));
286
InsertEmptyRecordCommand::~InsertEmptyRecordCommand()
290
void InsertEmptyRecordCommand::redoInternal()
292
m_view->insertEmptyRecord(m_row);
295
void InsertEmptyRecordCommand::undoInternal()
297
// let's assume the row is empty...
298
m_view->deleteRecord(m_row);