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

« back to all changes in this revision

Viewing changes to src/plugins/tables/kexitablepart.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) 2003 Lucijan Busch <lucijan@kde.org>
 
3
   Copyright (C) 2002, 2003 Joseph Wenninger <jowenn@kde.org>
 
4
   Copyright (C) 2004-2010 Jarosław Staniek <staniek@kde.org>
 
5
 
 
6
   This library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2 of the License, or (at your option) any later version.
 
10
 
 
11
   This library is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public License
 
17
   along with this library; see the file COPYING.LIB.  If not, write to
 
18
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 * Boston, MA 02110-1301, USA.
 
20
*/
 
21
 
 
22
#include "kexitablepart.h"
 
23
#include <KexiIcon.h>
 
24
#include <core/KexiMainWindowIface.h>
 
25
#include <core/kexiproject.h>
 
26
#include <core/kexipartinfo.h>
 
27
#include <widget/tableview/KexiDataTableView.h>
 
28
#include <widget/tableview/KexiDataTableScrollArea.h>
 
29
#include "kexitabledesignerview.h"
 
30
#include "kexitabledesigner_dataview.h"
 
31
#include "kexilookupcolumnpage.h"
 
32
#include <KexiWindow.h>
 
33
 
 
34
#include <KDbConnection>
 
35
 
 
36
#include <KMessageBox>
 
37
 
 
38
#include <QDebug>
 
39
#include <QTabWidget>
 
40
 
 
41
KEXI_PLUGIN_FACTORY(KexiTablePart, "kexi_tableplugin.json")
 
42
 
 
43
//! @internal
 
44
class KexiTablePart::Private
 
45
{
 
46
public:
 
47
    Private() {
 
48
    }
 
49
    ~Private() {
 
50
        delete static_cast<KexiLookupColumnPage*>(lookupColumnPage);
 
51
    }
 
52
    QPointer<KexiLookupColumnPage> lookupColumnPage;
 
53
};
 
54
 
 
55
KexiTablePart::KexiTablePart(QObject *parent, const QVariantList& l)
 
56
  : KexiPart::Part(parent,
 
57
        xi18nc("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
 
58
              "Use '_' character instead of spaces. First character should be a..z character. "
 
59
              "If you cannot use latin characters in your language, use english word.",
 
60
              "table"),
 
61
        xi18nc("tooltip", "Create new table"),
 
62
        xi18nc("what's this", "Creates new table."),
 
63
        l)
 
64
  , d(new Private)
 
65
{
 
66
//! @todo js: also add Kexi::TextViewMode when we'll have SQL ALTER TABLE EDITOR!!!
 
67
}
 
68
 
 
69
KexiTablePart::~KexiTablePart()
 
70
{
 
71
    delete d;
 
72
}
 
73
 
 
74
void KexiTablePart::initPartActions()
 
75
{
 
76
}
 
77
 
 
78
void KexiTablePart::initInstanceActions()
 
79
{
 
80
}
 
81
 
 
82
KexiWindowData* KexiTablePart::createWindowData(KexiWindow* window)
 
83
{
 
84
    return new KexiTablePartTempData(window);
 
85
}
 
86
 
 
87
KexiView* KexiTablePart::createView(QWidget *parent, KexiWindow* window,
 
88
                                    KexiPart::Item *item, Kexi::ViewMode viewMode, QMap<QString, QVariant>*)
 
89
{
 
90
    Q_ASSERT(item);
 
91
    KexiMainWindowIface *win = KexiMainWindowIface::global();
 
92
    if (!win || !win->project() || !win->project()->dbConnection())
 
93
        return 0;
 
94
 
 
95
 
 
96
    KexiTablePartTempData *temp
 
97
        = static_cast<KexiTablePartTempData*>(window->data());
 
98
    if (!temp->table) {
 
99
        temp->table = win->project()->dbConnection()->tableSchema(item->name());
 
100
        qDebug() << "schema is " << temp->table;
 
101
    }
 
102
 
 
103
    if (viewMode == Kexi::DesignViewMode) {
 
104
        KexiTableDesignerView *t = new KexiTableDesignerView(parent);
 
105
        return t;
 
106
    } else if (viewMode == Kexi::DataViewMode) {
 
107
        if (!temp->table) {
 
108
            return 0; //!< @todo message
 
109
        }
 
110
        //we're not setting table schema here -it will be forced to set
 
111
        // in KexiTableDesigner_DataView::afterSwitchFrom()
 
112
        KexiTableDesigner_DataView *t = new KexiTableDesigner_DataView(parent);
 
113
        return t;
 
114
    }
 
115
    return 0;
 
116
}
 
117
 
 
118
tristate KexiTablePart::remove(KexiPart::Item *item)
 
119
{
 
120
    KexiProject *project = KexiMainWindowIface::global()->project();
 
121
    if (!project || !project->dbConnection())
 
122
        return false;
 
123
 
 
124
    KDbConnection *conn = project->dbConnection();
 
125
    KDbTableSchema *sch = conn->tableSchema(item->identifier());
 
126
 
 
127
    if (sch) {
 
128
        tristate res = KexiTablePart::askForClosingObjectsUsingTableSchema(
 
129
            KexiMainWindowIface::global()->thisWidget(), conn, sch,
 
130
            xi18n("You are about to remove table <resource>%1</resource> but following objects using this table are opened:",
 
131
                 sch->name()));
 
132
        if (res != true) {
 
133
            return res;
 
134
        }
 
135
        return conn->dropTable(sch);
 
136
    }
 
137
    //last chance: just remove item
 
138
    return conn->removeObject(item->identifier());
 
139
}
 
140
 
 
141
tristate KexiTablePart::rename(KexiPart::Item *item, const QString& newName)
 
142
{
 
143
    Q_ASSERT(item);
 
144
    KDbConnection *conn = KexiMainWindowIface::global()->project()->dbConnection();
 
145
    KDbTableSchema *schema = conn->tableSchema(item->identifier());
 
146
    if (!schema)
 
147
        return false;
 
148
    tristate res = KexiTablePart::askForClosingObjectsUsingTableSchema(
 
149
        KexiMainWindowIface::global()->thisWidget(), conn, schema,
 
150
        xi18n("You are about to rename table <resource>%1</resource> but following objects using this table are opened:",
 
151
             schema->name()));
 
152
    if (res != true) {
 
153
        return res;
 
154
    }
 
155
    return conn->alterTableName(schema, newName);
 
156
}
 
157
 
 
158
KDbObject* KexiTablePart::loadSchemaObject(KexiWindow *window, const KDbObject& object,
 
159
                              Kexi::ViewMode viewMode, bool *ownedByWindow)
 
160
{
 
161
    Q_UNUSED(window);
 
162
    Q_UNUSED(viewMode);
 
163
    if (ownedByWindow)
 
164
        *ownedByWindow = false;
 
165
    return KexiMainWindowIface::global()->project()->dbConnection()->tableSchema(object.name());
 
166
}
 
167
 
 
168
//static
 
169
tristate KexiTablePart::askForClosingObjectsUsingTableSchema(
 
170
    QWidget *parent, KDbConnection *conn,
 
171
    KDbTableSchema *table, const QString& msg)
 
172
{
 
173
    Q_ASSERT(conn);
 
174
    Q_ASSERT(table);
 
175
    QSet<KDbConnection::TableSchemaChangeListenerInterface*>* listeners
 
176
        = conn->tableSchemaChangeListeners(table);
 
177
    if (!listeners || listeners->isEmpty())
 
178
        return true;
 
179
 
 
180
    QString openedObjectsStr = "<list>";
 
181
    foreach(KDbConnection::TableSchemaChangeListenerInterface* iface, *listeners) {
 
182
        openedObjectsStr += QString("<item>%1</item>").arg(iface->listenerInfoString);
 
183
    }
 
184
    openedObjectsStr += "</list>";
 
185
    int r = KMessageBox::questionYesNo(parent,
 
186
                                       i18nc("@info", "<para>%1</para><para>%2</para>", msg, openedObjectsStr)
 
187
                                       + "<para>"
 
188
                                       + xi18n("Do you want to close all windows for these objects?")
 
189
                                       + "</para>",
 
190
                                       QString(), KGuiItem(xi18nc("@action:button Close All Windows", "Close Windows"), koIconName("window-close")), KStandardGuiItem::cancel());
 
191
    tristate res;
 
192
    if (r == KMessageBox::Yes) {
 
193
        //try to close every window
 
194
        res = conn->closeAllTableSchemaChangeListeners(table);
 
195
        if (res != true) //do not expose closing errors twice; just cancel
 
196
            res = cancelled;
 
197
    } else
 
198
        res = cancelled;
 
199
 
 
200
    return res;
 
201
}
 
202
 
 
203
KLocalizedString KexiTablePart::i18nMessage(
 
204
    const QString& englishMessage, KexiWindow* window) const
 
205
{
 
206
    Q_UNUSED(window);
 
207
    if (englishMessage == "Design of object <resource>%1</resource> has been modified.")
 
208
        return kxi18nc(I18NC_NOOP("@info", "Design of table <resource>%1</resource> has been modified."));
 
209
 
 
210
    if (englishMessage == "Object <resource>%1</resource> already exists.")
 
211
        return kxi18nc(I18NC_NOOP("@info", "Table <resource>%1</resource> already exists."));
 
212
 
 
213
    if (window->currentViewMode() == Kexi::DesignViewMode && !window->neverSaved()
 
214
            && englishMessage == ":additional message before saving design")
 
215
        return kxi18nc(I18NC_NOOP("@info", "<warning>Any data in this table will be removed upon design's saving!</warning>"));
 
216
 
 
217
    return Part::i18nMessage(englishMessage, window);
 
218
}
 
219
 
 
220
void KexiTablePart::setupCustomPropertyPanelTabs(QTabWidget *tab)
 
221
{
 
222
    if (!d->lookupColumnPage) {
 
223
        d->lookupColumnPage = new KexiLookupColumnPage(0);
 
224
        connect(d->lookupColumnPage,
 
225
                SIGNAL(jumpToObjectRequested(QString,QString)),
 
226
                KexiMainWindowIface::global()->thisWidget(),
 
227
                SLOT(highlightObject(QString,QString)));
 
228
 
 
229
//! @todo add "Table" tab
 
230
        /*
 
231
          connect(d->dataSourcePage, SIGNAL(formDataSourceChanged(QCString,QCString)),
 
232
            KFormDesigner::FormManager::self(), SLOT(setFormDataSource(QCString,QCString)));
 
233
          connect(d->dataSourcePage, SIGNAL(dataSourceFieldOrExpressionChanged(QString,QString,KDbField::Type)),
 
234
            KFormDesigner::FormManager::self(), SLOT(setDataSourceFieldOrExpression(QString,QString,KDbField::Type)));
 
235
          connect(d->dataSourcePage, SIGNAL(insertAutoFields(QString,QString,QStringList)),
 
236
            KFormDesigner::FormManager::self(), SLOT(insertAutoFields(QString,QString,QStringList)));*/
 
237
    }
 
238
 
 
239
    KexiProject *prj = KexiMainWindowIface::global()->project();
 
240
    d->lookupColumnPage->setProject(prj);
 
241
 
 
242
//! @todo add lookup field icon
 
243
    tab->addTab(d->lookupColumnPage, KexiIcon("combobox"), QString());
 
244
    tab->setTabToolTip(tab->indexOf(d->lookupColumnPage), xi18n("Lookup column"));
 
245
}
 
246
 
 
247
KexiLookupColumnPage* KexiTablePart::lookupColumnPage() const
 
248
{
 
249
    return d->lookupColumnPage;
 
250
}
 
251
 
 
252
//----------------
 
253
 
 
254
KexiTablePartTempData::KexiTablePartTempData(QObject* parent)
 
255
        : KexiWindowData(parent)
 
256
        , table(0)
 
257
        , tableSchemaChangedInPreviousView(true /*to force reloading on startup*/)
 
258
{
 
259
}
 
260
 
 
261
#include "kexitablepart.moc"