00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kexitablepart.h"
00023
00024 #include <kdebug.h>
00025 #include <kgenericfactory.h>
00026 #include <kmessagebox.h>
00027 #include <ktabwidget.h>
00028 #include <kiconloader.h>
00029
00030 #include "keximainwindow.h"
00031 #include "kexiproject.h"
00032 #include "kexipartinfo.h"
00033 #include "widget/kexidatatable.h"
00034 #include "widget/tableview/kexidatatableview.h"
00035 #include "kexitabledesignerview.h"
00036 #include "kexitabledesigner_dataview.h"
00037 #include "kexilookupcolumnpage.h"
00038
00039 #include <kexidb/connection.h>
00040 #include <kexidb/cursor.h>
00041 #include <kexidialogbase.h>
00042
00044 class KexiTablePart::Private
00045 {
00046 public:
00047 Private()
00048 {
00049 }
00050 ~Private()
00051 {
00052 delete static_cast<KexiLookupColumnPage*>(lookupColumnPage);
00053 }
00054 QGuardedPtr<KexiLookupColumnPage> lookupColumnPage;
00055 };
00056
00057 KexiTablePart::KexiTablePart(QObject *parent, const char *name, const QStringList &l)
00058 : KexiPart::Part(parent, name, l)
00059 , d(new Private())
00060 {
00061
00062 m_registeredPartID = (int)KexiPart::TableObjectType;
00063
00064 kdDebug() << "KexiTablePart::KexiTablePart()" << endl;
00065 m_names["instanceName"]
00066 = i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
00067 "Use '_' character instead of spaces. First character should be a..z character. "
00068 "If you cannot use latin characters in your language, use english word.",
00069 "table");
00070 m_names["instanceCaption"] = i18n("Table");
00071 m_supportedViewModes = Kexi::DataViewMode | Kexi::DesignViewMode;
00072
00073 }
00074
00075 KexiTablePart::~KexiTablePart()
00076 {
00077 delete d;
00078 }
00079
00080 void KexiTablePart::initPartActions()
00081 {
00082 }
00083
00084 void KexiTablePart::initInstanceActions()
00085 {
00086
00087
00088 KAction *a = createSharedToggleAction(
00089 Kexi::DesignViewMode, i18n("Primary Key"), "key", 0, "tablepart_toggle_pkey");
00090
00091 a->setWhatsThis(i18n("Sets or removes primary key for currently selected field."));
00092 }
00093
00094 KexiDialogTempData* KexiTablePart::createTempData(KexiDialogBase* dialog)
00095 {
00096 return new KexiTablePart::TempData(dialog);
00097 }
00098
00099 KexiViewBase* KexiTablePart::createView(QWidget *parent, KexiDialogBase* dialog,
00100 KexiPart::Item &item, int viewMode, QMap<QString,QString>*)
00101 {
00102 KexiMainWindow *win = dialog->mainWin();
00103 if (!win || !win->project() || !win->project()->dbConnection())
00104 return 0;
00105
00106
00107 KexiTablePart::TempData *temp = static_cast<KexiTablePart::TempData*>(dialog->tempData());
00108 if (!temp->table) {
00109 temp->table = win->project()->dbConnection()->tableSchema(item.name());
00110 kdDebug() << "KexiTablePart::execute(): schema is " << temp->table << endl;
00111 }
00112
00113 if (viewMode == Kexi::DesignViewMode) {
00114 KexiTableDesignerView *t = new KexiTableDesignerView(win, parent);
00115 return t;
00116 }
00117 else if (viewMode == Kexi::DataViewMode) {
00118 if(!temp->table)
00119 return 0;
00120
00121
00122 KexiTableDesigner_DataView *t = new KexiTableDesigner_DataView(win, parent);
00123 return t;
00124 }
00125 return 0;
00126 }
00127
00128 bool KexiTablePart::remove(KexiMainWindow *win, KexiPart::Item &item)
00129 {
00130 if (!win || !win->project() || !win->project()->dbConnection())
00131 return false;
00132
00133 KexiDB::Connection *conn = win->project()->dbConnection();
00134 KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());
00135
00136 if (sch) {
00137 tristate res = KexiTablePart::askForClosingObjectsUsingTableSchema(
00138 win, *conn, *sch,
00139 i18n("You are about to remove table \"%1\" but following objects using this table are opened:")
00140 .arg(sch->name()));
00141 return true == conn->dropTable( sch );
00142 }
00143
00144 return conn->removeObject( item.identifier() );
00145 }
00146
00147 tristate KexiTablePart::rename(KexiMainWindow *win, KexiPart::Item & item,
00148 const QString& newName)
00149 {
00150
00151 KexiDB::Connection *conn = win->project()->dbConnection();
00152 KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());
00153 if (!sch)
00154 return false;
00155 return conn->alterTableName(*sch, newName);
00156 }
00157
00158 KexiDB::SchemaData*
00159 KexiTablePart::loadSchemaData(KexiDialogBase *dlg, const KexiDB::SchemaData& sdata, int viewMode)
00160 {
00161 Q_UNUSED( viewMode );
00162
00163 return dlg->mainWin()->project()->dbConnection()->tableSchema( sdata.name() );
00164 }
00165
00166 #if 0
00167 KexiPart::DataSource *
00168 KexiTablePart::dataSource()
00169 {
00170 return new KexiTableDataSource(this);
00171 }
00172 #endif
00173
00174 tristate KexiTablePart::askForClosingObjectsUsingTableSchema(QWidget *parent, KexiDB::Connection& conn,
00175 KexiDB::TableSchema& table, const QString& msg)
00176 {
00177 QPtrList<KexiDB::Connection::TableSchemaChangeListenerInterface>* listeners
00178 = conn.tableSchemaChangeListeners(table);
00179 if (!listeners || listeners->isEmpty())
00180 return true;
00181
00182 QString openedObjectsStr = "<ul>";
00183 for (QPtrListIterator<KexiDB::Connection::TableSchemaChangeListenerInterface> it(*listeners);
00184 it.current(); ++it) {
00185 openedObjectsStr += QString("<li>%1</li>").arg(it.current()->listenerInfoString);
00186 }
00187 openedObjectsStr += "</ul>";
00188 int r = KMessageBox::questionYesNo(parent,
00189 "<p>"+msg+"</p><p>"+openedObjectsStr+"</p><p>"
00190 +i18n("Do you want to close all windows for these objects?"),
00191 QString::null, KGuiItem(i18n("Close windows"),"fileclose"), KStdGuiItem::cancel());
00192 tristate res;
00193 if (r == KMessageBox::Yes) {
00194
00195 res = conn.closeAllTableSchemaChangeListeners(table);
00196 if (res!=true)
00197 res = cancelled;
00198 }
00199 else
00200 res = cancelled;
00201
00202 return res;
00203 }
00204
00205 QString
00206 KexiTablePart::i18nMessage(const QCString& englishMessage, KexiDialogBase* dlg) const
00207 {
00208 if (englishMessage=="Design of object \"%1\" has been modified.")
00209 return i18n("Design of table \"%1\" has been modified.");
00210
00211 if (englishMessage=="Object \"%1\" already exists.")
00212 return i18n("Table \"%1\" already exists.");
00213
00214 if (dlg->currentViewMode()==Kexi::DesignViewMode && !dlg->neverSaved()
00215 && englishMessage==":additional message before saving design")
00216 return i18n("Warning! Any data in this table will be removed upon design's saving!");
00217
00218 return englishMessage;
00219 }
00220
00221 void KexiTablePart::setupCustomPropertyPanelTabs(KTabWidget *tab, KexiMainWindow* mainWin)
00222 {
00223 if (!d->lookupColumnPage) {
00224 d->lookupColumnPage = new KexiLookupColumnPage(0);
00225 connect(d->lookupColumnPage, SIGNAL(jumpToObjectRequested(const QCString&, const QCString&)),
00226 mainWin, SLOT(highlightObject(const QCString&, const QCString&)));
00227
00229
00230
00231
00232
00233
00234
00235
00236
00237 }
00238
00239 KexiProject *prj = mainWin->project();
00240 d->lookupColumnPage->setProject(prj);
00241
00243 tab->addTab( d->lookupColumnPage, SmallIconSet("combo"), "");
00244 tab->setTabToolTip( d->lookupColumnPage, i18n("Lookup column"));
00245 }
00246
00247 KexiLookupColumnPage* KexiTablePart::lookupColumnPage() const
00248 {
00249 return d->lookupColumnPage;
00250 }
00251
00252
00253
00254 #if 0
00255 KexiTableDataSource::KexiTableDataSource(KexiPart::Part *part)
00256 : KexiPart::DataSource(part)
00257 {
00258 }
00259
00260 KexiTableDataSource::~KexiTableDataSource()
00261 {
00262 }
00263
00264 KexiDB::FieldList *
00265 KexiTableDataSource::fields(KexiProject *project, const KexiPart::Item &it)
00266 {
00267 kdDebug() << "KexiTableDataSource::fields(): " << it.name() << endl;
00268 return project->dbConnection()->tableSchema(it.name());
00269 }
00270
00271 KexiDB::Cursor *
00272 KexiTableDataSource::cursor(KexiProject * ,
00273 const KexiPart::Item &, bool )
00274 {
00275 return 0;
00276 }
00277 #endif
00278
00279
00280
00281 KexiTablePart::TempData::TempData(QObject* parent)
00282 : KexiDialogTempData(parent)
00283 , table(0)
00284 , tableSchemaChangedInPreviousView(true )
00285 {
00286 }
00287
00288
00289
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 K_EXPORT_COMPONENT_FACTORY( kexihandler_table, KGenericFactory<KexiTablePart>("kexihandler_table") )
00311
00312 #include "kexitablepart.moc"
00313