1 /* This file is part of the KDE project
2    Copyright (C) 2006-2012 Jarosław Staniek <staniek@kde.org>
3 
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.
8 
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.
13 
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.
18 */
19 
20 #include "kexitabledesignercommands.h"
21 #include <kexi_global.h>
22 
23 #include <KLocalizedString>
24 
25 #include <KProperty>
26 #include <KPropertyListData>
27 
28 #include <QDebug>
29 
30 using namespace KexiTableDesignerCommands;
31 
32 
Command(const KUndo2MagicString & text,Command * parent,KexiTableDesignerView * view)33 Command::Command(const KUndo2MagicString &text, Command *parent, KexiTableDesignerView* view)
34         : KUndo2Command(text, parent)
35         , m_view(view)
36         , m_blockRedoOnce(false)
37 {
38 }
39 
Command(Command * parent,KexiTableDesignerView * view)40 Command::Command(Command* parent, KexiTableDesignerView* view)
41         : KUndo2Command(KUndo2MagicString(), parent)
42         , m_view(view)
43         , m_blockRedoOnce(false)
44 {
45 }
46 
~Command()47 Command::~Command()
48 {
49 }
50 
redo()51 void Command::redo()
52 {
53     if (m_blockRedoOnce) {
54         m_blockRedoOnce = false;
55         return;
56     }
57     redoInternal();
58 }
59 
undo()60 void Command::undo()
61 {
62     undoInternal();
63 }
64 
redoInternal()65 void Command::redoInternal()
66 {
67 }
68 
undoInternal()69 void Command::undoInternal()
70 {
71 }
72 
blockRedoOnce()73 void Command::blockRedoOnce()
74 {
75     m_blockRedoOnce = true;
76 }
77 
78 //--------------------------------------------------------
79 
ChangeFieldPropertyCommand(Command * parent,KexiTableDesignerView * view,const KPropertySet & set,const QByteArray & propertyName,const QVariant & oldValue,const QVariant & newValue,KPropertyListData * const oldListData,KPropertyListData * const newListData)80 ChangeFieldPropertyCommand::ChangeFieldPropertyCommand(
81     Command* parent, KexiTableDesignerView* view,
82     const KPropertySet& set, const QByteArray& propertyName,
83     const QVariant& oldValue, const QVariant& newValue,
84     KPropertyListData* const oldListData,
85     KPropertyListData* const newListData)
86         : Command(parent, view)
87         , m_alterTableAction(
88             propertyName == "name" ? oldValue.toString() : set.property("name").value().toString(),
89             propertyName, newValue, set["uid"].value().toInt())
90         , m_oldValue(oldValue)
91         , m_oldListData(oldListData ? new KPropertyListData(*oldListData) : 0)
92         , m_listData(newListData ? new KPropertyListData(*newListData) : 0)
93 {
94     setText(kundo2_i18n("Change <resource>%1</resource> property for table field from "
95                         "<resource>%2</resource> to <resource>%3</resource>",
96                         m_alterTableAction.propertyName(),
97                         m_oldValue.toString(),
98                         m_alterTableAction.newValue().toString()));
99 
100     qDebug() << debugString();
101 }
102 
~ChangeFieldPropertyCommand()103 ChangeFieldPropertyCommand::~ChangeFieldPropertyCommand()
104 {
105     delete m_oldListData;
106     delete m_listData;
107 }
108 
debugString() const109 QString ChangeFieldPropertyCommand::debugString() const
110 {
111     QString s(text().toString());
112     if (m_oldListData || m_listData)
113         s += QString("\nAnd list data from [%1]\n  to [%2]")
114              .arg(m_oldListData ?
115                   QString("%1 -> %2")
116                   .arg(m_oldListData->keysAsStringList().join(",")).arg(m_oldListData->namesAsStringList().join(","))
117                   : QString("<NONE>"))
118              .arg(m_listData ?
119                   QString("%1 -> %2")
120                   .arg(m_listData->keysAsStringList().join(",")).arg(m_listData->namesAsStringList().join(","))
121                   : QString("<NONE>"));
122     return s + QString(" (UID=%1)").arg(m_alterTableAction.uid());
123 }
124 
redoInternal()125 void ChangeFieldPropertyCommand::redoInternal()
126 {
127     m_view->changeFieldProperty(
128         m_alterTableAction.uid(),
129         m_alterTableAction.propertyName().toLatin1(),
130         m_alterTableAction.newValue(), m_listData);
131 }
132 
undoInternal()133 void ChangeFieldPropertyCommand::undoInternal()
134 {
135     m_view->changeFieldProperty(
136         m_alterTableAction.uid(),
137         m_alterTableAction.propertyName().toLatin1(),
138         m_oldValue, m_oldListData);
139 }
140 
createAction() const141 KDbAlterTableHandler::ActionBase* ChangeFieldPropertyCommand::createAction() const
142 {
143     if (m_alterTableAction.propertyName() == "subType") {//skip these properties
144         return 0;
145     }
146     return new KDbAlterTableHandler::ChangeFieldPropertyAction(m_alterTableAction);
147 }
148 
149 //--------------------------------------------------------
150 
RemoveFieldCommand(Command * parent,KexiTableDesignerView * view,int fieldIndex,const KPropertySet * set)151 RemoveFieldCommand::RemoveFieldCommand(Command* parent, KexiTableDesignerView* view, int fieldIndex,
152                                        const KPropertySet* set)
153         : Command(parent, view)
154         , m_alterTableAction(set ? (*set)["name"].value().toString() : QString(),
155                              set ? (*set)["uid"].value().toInt() : -1)
156         , m_set(set ? new KPropertySet(*set /*deep copy*/) : 0)
157         , m_fieldIndex(fieldIndex)
158 {
159     if (m_set)
160         setText(kundo2_i18n("Delete table field <resource>%1</resource>", m_alterTableAction.fieldName()));
161     else
162         setText(kundo2_i18n("Delete empty row at position %1", m_fieldIndex));
163 }
164 
~RemoveFieldCommand()165 RemoveFieldCommand::~RemoveFieldCommand()
166 {
167     delete m_set;
168 }
169 
redoInternal()170 void RemoveFieldCommand::redoInternal()
171 {
172 // m_view->deleteField( m_fieldIndex );
173     m_view->deleteRecord(m_fieldIndex);
174 }
175 
undoInternal()176 void RemoveFieldCommand::undoInternal()
177 {
178     m_view->insertEmptyRecord(m_fieldIndex);
179     if (m_set)
180         m_view->insertField(m_fieldIndex, *m_set);
181 }
182 
debugString() const183 QString RemoveFieldCommand::debugString() const
184 {
185     if (!m_set)
186         return text().toString();
187 
188     return text().toString() + "\nAT ROW " + QString::number(m_fieldIndex)
189            + ", FIELD: " + (*m_set)["caption"].value().toString()
190            + QString(" (UID=%1)").arg(m_alterTableAction.uid());
191 }
192 
createAction() const193 KDbAlterTableHandler::ActionBase* RemoveFieldCommand::createAction() const
194 {
195     return new KDbAlterTableHandler::RemoveFieldAction(m_alterTableAction);
196 }
197 
198 //--------------------------------------------------------
199 
InsertFieldCommand(Command * parent,KexiTableDesignerView * view,int fieldIndex,const KPropertySet & set)200 InsertFieldCommand::InsertFieldCommand(Command* parent, KexiTableDesignerView* view,
201                                        int fieldIndex/*, const KDbField& field*/, const KPropertySet& set)
202         : Command(parent, view)
203         , m_set(set)   //? new KPropertySet(*set) : 0 )
204 {
205     KDbField *f = view->buildField(m_set);
206     if (f)
207         m_alterTableAction = new KDbAlterTableHandler::InsertFieldAction(
208             fieldIndex, f, set["uid"].value().toInt());
209     else //null action
210         m_alterTableAction = new KDbAlterTableHandler::InsertFieldAction;
211 
212     setText(kundo2_i18n("Insert table field \"%1\"", m_set["caption"].value().toString()));
213 }
214 
~InsertFieldCommand()215 InsertFieldCommand::~InsertFieldCommand()
216 {
217     delete m_alterTableAction;
218 }
219 
redoInternal()220 void InsertFieldCommand::redoInternal()
221 {
222     m_view->insertField(m_alterTableAction->index(), /*m_alterTableAction.field(),*/ m_set);
223 }
224 
undoInternal()225 void InsertFieldCommand::undoInternal()
226 {
227     m_view->clearRecord(m_alterTableAction->index());  //m_alterTableAction.index() );
228 }
229 
createAction() const230 KDbAlterTableHandler::ActionBase* InsertFieldCommand::createAction() const
231 {
232     return new KDbAlterTableHandler::InsertFieldAction(*m_alterTableAction);
233 }
234 
debugString() const235 QString InsertFieldCommand::debugString() const
236 {
237     return text().toString() + "\nAT ROW " + QString::number(m_alterTableAction->index())
238            + ", FIELD: " + m_set["caption"].value().toString();
239 }
240 
241 //--------------------------------------------------------
242 
ChangePropertyVisibilityCommand(Command * parent,KexiTableDesignerView * view,const KPropertySet & set,const QByteArray & propertyName,bool visible)243 ChangePropertyVisibilityCommand::ChangePropertyVisibilityCommand(Command* parent, KexiTableDesignerView* view,
244         const KPropertySet& set, const QByteArray& propertyName, bool visible)
245         : Command(parent, view)
246         , m_alterTableAction(set.property("name").value().toString(), propertyName, visible, set["uid"].value().toInt())
247         , m_oldVisibility(set.property(propertyName).isVisible())
248 {
249     setText(kundo2_noi18n("[internal] Change <resource>%1</resource> visibility from "
250                           "<resource>%2</resource> to <resource>%3</resource>",
251                           m_alterTableAction.propertyName(),
252                           m_oldVisibility ? "true" : "false",
253                           m_alterTableAction.newValue().toBool() ? "true" : "false"));
254 
255     qDebug() << debugString();
256 }
257 
~ChangePropertyVisibilityCommand()258 ChangePropertyVisibilityCommand::~ChangePropertyVisibilityCommand()
259 {
260 }
261 
redoInternal()262 void ChangePropertyVisibilityCommand::redoInternal()
263 {
264     m_view->changePropertyVisibility(
265         m_alterTableAction.uid(),
266         m_alterTableAction.propertyName().toLatin1(),
267         m_alterTableAction.newValue().toBool());
268 }
269 
undoInternal()270 void ChangePropertyVisibilityCommand::undoInternal()
271 {
272     m_view->changePropertyVisibility(
273         m_alterTableAction.uid(),
274         m_alterTableAction.propertyName().toLatin1(),
275         m_oldVisibility);
276 }
277 
278 //--------------------------------------------------------
279 
InsertEmptyRecordCommand(Command * parent,KexiTableDesignerView * view,int row)280 InsertEmptyRecordCommand::InsertEmptyRecordCommand(Command* parent, KexiTableDesignerView* view, int row)
281         : Command(parent, view)
282         , m_row(row)
283 {
284     setText(kundo2_noi18n("Insert empty row at position %1", m_row));
285 }
286 
~InsertEmptyRecordCommand()287 InsertEmptyRecordCommand::~InsertEmptyRecordCommand()
288 {
289 }
290 
redoInternal()291 void InsertEmptyRecordCommand::redoInternal()
292 {
293     m_view->insertEmptyRecord(m_row);
294 }
295 
undoInternal()296 void InsertEmptyRecordCommand::undoInternal()
297 {
298     // let's assume the row is empty...
299     m_view->deleteRecord(m_row);
300 }
301 
302