~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/plugins/tables/kexitabledesignercommands.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <QDebug>
 
21
 
 
22
#include <KLocalizedString>
 
23
 
 
24
#include <KProperty>
 
25
#include <kexi_global.h>
 
26
 
 
27
#include "kexitabledesignercommands.h"
 
28
 
 
29
using namespace KexiTableDesignerCommands;
 
30
 
 
31
 
 
32
Command::Command(const KUndo2MagicString &text, Command *parent, KexiTableDesignerView* view)
 
33
        : KUndo2Command(text, parent)
 
34
        , m_view(view)
 
35
        , m_blockRedoOnce(false)
 
36
{
 
37
}
 
38
 
 
39
Command::Command(Command* parent, KexiTableDesignerView* view)
 
40
        : KUndo2Command(KUndo2MagicString(), parent)
 
41
        , m_view(view)
 
42
        , m_blockRedoOnce(false)
 
43
{
 
44
}
 
45
 
 
46
Command::~Command()
 
47
{
 
48
}
 
49
 
 
50
void Command::redo()
 
51
{
 
52
    if (m_blockRedoOnce) {
 
53
        m_blockRedoOnce = false;
 
54
        return;
 
55
    }
 
56
    redoInternal();
 
57
}
 
58
 
 
59
void Command::undo()
 
60
{
 
61
    undoInternal();
 
62
}
 
63
 
 
64
void Command::redoInternal()
 
65
{
 
66
}
 
67
 
 
68
void Command::undoInternal()
 
69
{
 
70
}
 
71
 
 
72
void Command::blockRedoOnce()
 
73
{
 
74
    m_blockRedoOnce = true;
 
75
}
 
76
 
 
77
//--------------------------------------------------------
 
78
 
 
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)
 
86
        , m_alterTableAction(
 
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)
 
92
{
 
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()));
 
98
 
 
99
    qDebug() << debugString();
 
100
}
 
101
 
 
102
ChangeFieldPropertyCommand::~ChangeFieldPropertyCommand()
 
103
{
 
104
    delete m_oldListData;
 
105
    delete m_listData;
 
106
}
 
107
 
 
108
QString ChangeFieldPropertyCommand::debugString() const
 
109
{
 
110
    QString s(text().toString());
 
111
    if (m_oldListData || m_listData)
 
112
        s += QString("\nAnd list data from [%1]\n  to [%2]")
 
113
             .arg(m_oldListData ?
 
114
                  QString("%1 -> %2")
 
115
                  .arg(m_oldListData->keysAsStringList().join(",")).arg(m_oldListData->names.join(","))
 
116
                  : QString("<NONE>"))
 
117
             .arg(m_listData ?
 
118
                  QString("%1 -> %2")
 
119
                  .arg(m_listData->keysAsStringList().join(",")).arg(m_listData->names.join(","))
 
120
                  : QString("<NONE>"));
 
121
    return s + QString(" (UID=%1)").arg(m_alterTableAction.uid());
 
122
}
 
123
 
 
124
void ChangeFieldPropertyCommand::redoInternal()
 
125
{
 
126
    m_view->changeFieldProperty(
 
127
        m_alterTableAction.uid(),
 
128
        m_alterTableAction.propertyName().toLatin1(),
 
129
        m_alterTableAction.newValue(), m_listData);
 
130
}
 
131
 
 
132
void ChangeFieldPropertyCommand::undoInternal()
 
133
{
 
134
    m_view->changeFieldProperty(
 
135
        m_alterTableAction.uid(),
 
136
        m_alterTableAction.propertyName().toLatin1(),
 
137
        m_oldValue, m_oldListData);
 
138
}
 
139
 
 
140
KDbAlterTableHandler::ActionBase* ChangeFieldPropertyCommand::createAction() const
 
141
{
 
142
    if (m_alterTableAction.propertyName() == "subType") {//skip these properties
 
143
        return 0;
 
144
    }
 
145
    return new KDbAlterTableHandler::ChangeFieldPropertyAction(m_alterTableAction);
 
146
}
 
147
 
 
148
//--------------------------------------------------------
 
149
 
 
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)
 
157
{
 
158
    if (m_set)
 
159
        setText(kundo2_i18n("Remove table field <resource>%1</resource>", m_alterTableAction.fieldName()));
 
160
    else
 
161
        setText(kundo2_i18n("Remove empty row at position %1", m_fieldIndex));
 
162
}
 
163
 
 
164
RemoveFieldCommand::~RemoveFieldCommand()
 
165
{
 
166
    delete m_set;
 
167
}
 
168
 
 
169
void RemoveFieldCommand::redoInternal()
 
170
{
 
171
// m_view->deleteField( m_fieldIndex );
 
172
    m_view->deleteRecord(m_fieldIndex);
 
173
}
 
174
 
 
175
void RemoveFieldCommand::undoInternal()
 
176
{
 
177
    m_view->insertEmptyRecord(m_fieldIndex);
 
178
    if (m_set)
 
179
        m_view->insertField(m_fieldIndex, *m_set);
 
180
}
 
181
 
 
182
QString RemoveFieldCommand::debugString() const
 
183
{
 
184
    if (!m_set)
 
185
        return text().toString();
 
186
 
 
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());
 
190
}
 
191
 
 
192
KDbAlterTableHandler::ActionBase* RemoveFieldCommand::createAction() const
 
193
{
 
194
    return new KDbAlterTableHandler::RemoveFieldAction(m_alterTableAction);
 
195
}
 
196
 
 
197
//--------------------------------------------------------
 
198
 
 
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 )
 
203
{
 
204
    KDbField *f = view->buildField(m_set);
 
205
    if (f)
 
206
        m_alterTableAction = new KDbAlterTableHandler::InsertFieldAction(
 
207
            fieldIndex, f, set["uid"].value().toInt());
 
208
    else //null action
 
209
        m_alterTableAction = new KDbAlterTableHandler::InsertFieldAction;
 
210
 
 
211
    setText(kundo2_i18n("Insert table field \"%1\"", m_set["caption"].value().toString()));
 
212
}
 
213
 
 
214
InsertFieldCommand::~InsertFieldCommand()
 
215
{
 
216
    delete m_alterTableAction;
 
217
}
 
218
 
 
219
void InsertFieldCommand::redoInternal()
 
220
{
 
221
    m_view->insertField(m_alterTableAction->index(), /*m_alterTableAction.field(),*/ m_set);
 
222
}
 
223
 
 
224
void InsertFieldCommand::undoInternal()
 
225
{
 
226
    m_view->clearRecord(m_alterTableAction->index());  //m_alterTableAction.index() );
 
227
}
 
228
 
 
229
KDbAlterTableHandler::ActionBase* InsertFieldCommand::createAction() const
 
230
{
 
231
    return new KDbAlterTableHandler::InsertFieldAction(*m_alterTableAction);
 
232
}
 
233
 
 
234
QString InsertFieldCommand::debugString() const
 
235
{
 
236
    return text().toString() + "\nAT ROW " + QString::number(m_alterTableAction->index())
 
237
           + ", FIELD: " + m_set["caption"].value().toString();
 
238
}
 
239
 
 
240
//--------------------------------------------------------
 
241
 
 
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())
 
247
{
 
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"));
 
253
 
 
254
    qDebug() << debugString();
 
255
}
 
256
 
 
257
ChangePropertyVisibilityCommand::~ChangePropertyVisibilityCommand()
 
258
{
 
259
}
 
260
 
 
261
void ChangePropertyVisibilityCommand::redoInternal()
 
262
{
 
263
    m_view->changePropertyVisibility(
 
264
        m_alterTableAction.uid(),
 
265
        m_alterTableAction.propertyName().toLatin1(),
 
266
        m_alterTableAction.newValue().toBool());
 
267
}
 
268
 
 
269
void ChangePropertyVisibilityCommand::undoInternal()
 
270
{
 
271
    m_view->changePropertyVisibility(
 
272
        m_alterTableAction.uid(),
 
273
        m_alterTableAction.propertyName().toLatin1(),
 
274
        m_oldVisibility);
 
275
}
 
276
 
 
277
//--------------------------------------------------------
 
278
 
 
279
InsertEmptyRecordCommand::InsertEmptyRecordCommand(Command* parent, KexiTableDesignerView* view, int row)
 
280
        : Command(parent, view)
 
281
        , m_row(row)
 
282
{
 
283
    setText(kundo2_noi18n("Insert empty row at position %1", m_row));
 
284
}
 
285
 
 
286
InsertEmptyRecordCommand::~InsertEmptyRecordCommand()
 
287
{
 
288
}
 
289
 
 
290
void InsertEmptyRecordCommand::redoInternal()
 
291
{
 
292
    m_view->insertEmptyRecord(m_row);
 
293
}
 
294
 
 
295
void InsertEmptyRecordCommand::undoInternal()
 
296
{
 
297
    // let's assume the row is empty...
 
298
    m_view->deleteRecord(m_row);
 
299
}
 
300