00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "keximacrodesignview.h"
00019 #include "keximacroproperty.h"
00020
00021 #include <qtimer.h>
00022 #include <qdom.h>
00023 #include <kdebug.h>
00024
00025 #include <kexidialogbase.h>
00026 #include <kexidb/connection.h>
00027 #include <kexidb/error.h>
00028
00029 #include <core/kexi.h>
00030 #include <core/kexiproject.h>
00031 #include <core/kexipartmanager.h>
00032 #include <core/kexipartinfo.h>
00033
00034 #include <widget/kexidatatable.h>
00035 #include <widget/tableview/kexitableview.h>
00036 #include <widget/tableview/kexitableviewdata.h>
00037 #include <widget/tableview/kexitableitem.h>
00038 #include <widget/tableview/kexidataawarepropertyset.h>
00039
00040 #include <koproperty/set.h>
00041 #include <koproperty/property.h>
00042
00043 #include "../lib/macro.h"
00044 #include "../lib/macroitem.h"
00045 #include "../lib/xmlhandler.h"
00046
00048 #define COLUMN_ID_ACTION 0
00049 #define COLUMN_ID_COMMENT 1
00050
00055 class KexiMacroDesignView::Private
00056 {
00057 public:
00058
00063 KexiDataTable* datatable;
00064
00068 KexiTableView* tableview;
00069
00074 KexiTableViewData* tabledata;
00075
00080 KexiDataAwarePropertySet* propertyset;
00081
00083 bool reloadsProperties;
00085 bool updatesProperties;
00086
00093 Private()
00094 : propertyset(0)
00095 , reloadsProperties(false)
00096 , updatesProperties(false)
00097 {
00098 }
00099
00103 ~Private()
00104 {
00105 delete propertyset;
00106 }
00107
00108 };
00109
00110 KexiMacroDesignView::KexiMacroDesignView(KexiMainWindow *mainwin, QWidget *parent, ::KoMacro::Macro* const macro)
00111 : KexiMacroView(mainwin, parent, macro, "KexiMacroDesignView")
00112 , d( new Private() )
00113 {
00114
00115 d->tabledata = new KexiTableViewData();
00116 d->tabledata->setSorting(-1);
00117
00118
00119 KexiTableViewColumn* actioncol = new KexiTableViewColumn(
00120 "action",
00121 KexiDB::Field::Enum,
00122 KexiDB::Field::NoConstraints,
00123 KexiDB::Field::NoOptions,
00124 0,
00125 0,
00126 QVariant(),
00127 i18n("Action"),
00128 QString::null,
00129 0
00130 );
00131 d->tabledata->addColumn(actioncol);
00132
00133 QValueVector<QString> items;
00134 items.append("");
00135
00136
00137 QStringList actionnames = KoMacro::Manager::self()->actionNames();
00138 QStringList::ConstIterator it, end( actionnames.constEnd() );
00139 for( it = actionnames.constBegin(); it != end; ++it) {
00140 KSharedPtr<KoMacro::Action> action = KoMacro::Manager::self()->action(*it);
00141 items.append( action->text() );
00142 }
00143
00144 actioncol->field()->setEnumHints(items);
00145
00146
00147 d->tabledata->addColumn( new KexiTableViewColumn(
00148 "comment",
00149 KexiDB::Field::Text,
00150 KexiDB::Field::NoConstraints,
00151 KexiDB::Field::NoOptions,
00152 0,
00153 0,
00154 QVariant(),
00155 i18n("Comment"),
00156 QString::null,
00157 0
00158 ) );
00159
00160
00161 QHBoxLayout* layout = new QHBoxLayout(this);
00162 d->datatable = new KexiDataTable(mainWin(), this, "Macro KexiDataTable", false );
00163 layout->addWidget(d->datatable);
00164 d->tableview = d->datatable->tableView();
00165 d->tableview->setSpreadSheetMode();
00166 d->tableview->setColumnStretchEnabled( true, COLUMN_ID_COMMENT );
00167
00168
00169
00170 KexiMacroPropertyFactory::initFactory();
00171
00172
00173 d->propertyset = new KexiDataAwarePropertySet(this, d->tableview);
00174
00175
00176 connect(d->tabledata, SIGNAL(aboutToChangeCell(KexiTableItem*,int,QVariant&,KexiDB::ResultInfo*)),
00177 this, SLOT(beforeCellChanged(KexiTableItem*,int,QVariant&,KexiDB::ResultInfo*)));
00178 connect(d->tabledata, SIGNAL(rowUpdated(KexiTableItem*)),
00179 this, SLOT(rowUpdated(KexiTableItem*)));
00180 connect(d->tabledata, SIGNAL(rowInserted(KexiTableItem*,uint,bool)),
00181 this, SLOT(rowInserted(KexiTableItem*,uint,bool)));
00182 connect(d->tabledata, SIGNAL(rowDeleted()),
00183 this, SLOT(rowDeleted()));
00184
00185
00186 updateData();
00187 setDirty(false);
00188 }
00189
00190 KexiMacroDesignView::~KexiMacroDesignView()
00191 {
00192 delete d;
00193 }
00194
00195 void KexiMacroDesignView::updateData()
00196 {
00197 kdDebug() << "KexiMacroDesignView::updateData()" << endl;
00198
00199
00200 d->tabledata->deleteAllRows();
00201
00202 d->propertyset->clear();
00203
00204
00205 for (int i=0; i<50; i++) {
00206 d->tabledata->append( d->tabledata->createItem() );
00207 }
00208
00209
00210 QStringList actionnames = KoMacro::Manager::self()->actionNames();
00211 KoMacro::MacroItem::List macroitems = macro()->items();
00212 KoMacro::MacroItem::List::ConstIterator it(macroitems.constBegin()), end(macroitems.constEnd());
00213 for(uint idx = 0; it != end; ++it, idx++) {
00214 KexiTableItem* tableitem = d->tabledata->at(idx);
00215 if(! tableitem) {
00216
00217 tableitem = d->tabledata->createItem();
00218 d->tabledata->append(tableitem);
00219 }
00220
00221 KSharedPtr<KoMacro::Action> action = (*it)->action();
00222 if(action.data()) {
00223 int i = actionnames.findIndex( action->name() );
00224 if(i >= 0) {
00225 tableitem->at(COLUMN_ID_ACTION) = i + 1;
00226
00227 }
00228 }
00229
00230 tableitem->at(COLUMN_ID_COMMENT) = (*it)->comment();
00231 }
00232
00233
00234 d->tableview->setData(d->tabledata);
00235
00236
00237 it = macroitems.constBegin();
00238 for(uint idx = 0; it != end; ++it, idx++) {
00239 updateProperties(idx, 0, *it);
00240 }
00241
00242
00243 d->tableview->setColumnStretchEnabled( true, COLUMN_ID_COMMENT );
00244
00245 propertySetReloaded(true);
00246 }
00247
00248 bool KexiMacroDesignView::loadData()
00249 {
00250 if(! KexiMacroView::loadData()) {
00251 return false;
00252 }
00253 updateData();
00254 return true;
00255 }
00256
00257 KoProperty::Set* KexiMacroDesignView::propertySet()
00258 {
00259 return d->propertyset->currentPropertySet();
00260 }
00261
00262 void KexiMacroDesignView::beforeCellChanged(KexiTableItem* item, int colnum, QVariant& newvalue, KexiDB::ResultInfo* result)
00263 {
00264 Q_UNUSED(result);
00265 kdDebug() << "KexiMacroDesignView::beforeCellChanged() colnum=" << colnum << " newvalue=" << newvalue.toString() << endl;
00266
00267 int rowindex = d->tabledata->findRef(item);
00268 if(rowindex < 0) {
00269 kdWarning() << "KexiMacroDesignView::beforeCellChanged() No such item" << endl;
00270 return;
00271 }
00272
00273
00274
00275 for(int i = macro()->items().count(); i <= rowindex; i++) {
00276 macro()->addItem( KSharedPtr<KoMacro::MacroItem>( new KoMacro::MacroItem() ) );
00277 }
00278
00279
00280 KSharedPtr<KoMacro::MacroItem> macroitem = macro()->items()[rowindex];
00281 if(! macroitem.data()) {
00282 kdWarning() << "KexiMacroDesignView::beforeCellChanged() Invalid item for rowindex=" << rowindex << endl;
00283 return;
00284 }
00285
00286
00287 switch(colnum) {
00288 case COLUMN_ID_ACTION: {
00289 QString actionname;
00290 bool ok;
00291 int selectedindex = newvalue.toInt(&ok);
00292 if(ok && selectedindex > 0) {
00293 QStringList actionnames = KoMacro::Manager::self()->actionNames();
00294 actionname = actionnames[ selectedindex - 1 ];
00295 }
00296 KSharedPtr<KoMacro::Action> action = KoMacro::Manager::self()->action(actionname);
00297 macroitem->setAction(action);
00298 updateProperties(d->propertyset->currentRow(), d->propertyset->currentPropertySet(), macroitem);
00299 propertySetReloaded(true);
00300 } break;
00301 case COLUMN_ID_COMMENT: {
00302 macroitem->setComment( newvalue.toString() );
00303 } break;
00304 default:
00305 kdWarning() << "KexiMacroDesignView::beforeCellChanged() No such column number " << colnum << endl;
00306 return;
00307 }
00308
00309 setDirty();
00310 }
00311
00312 void KexiMacroDesignView::rowUpdated(KexiTableItem* item)
00313 {
00314 int rowindex = d->tabledata->findRef(item);
00315 kdDebug() << "KexiMacroDesignView::rowUpdated() rowindex=" << rowindex << endl;
00316
00317
00318
00319 }
00320
00321 void KexiMacroDesignView::rowInserted(KexiTableItem*, uint row, bool)
00322 {
00323 kdDebug() << "KexiMacroDesignView::rowInserted() rowindex=" << row << endl;
00324 KoMacro::MacroItem::List& macroitems = macro()->items();
00325
00326 if(row < macroitems.count()) {
00327
00328
00329
00330
00331 kdDebug() << "KexiMacroDesignView::rowInserted() Inserting new MacroItem" << endl;
00332 KSharedPtr<KoMacro::MacroItem> macroitem = KSharedPtr<KoMacro::MacroItem>( new KoMacro::MacroItem() );
00333 KoMacro::MacroItem::List::Iterator it = macroitems.at(row);
00334 macroitems.insert(it, macroitem);
00335 }
00336 }
00337
00338 void KexiMacroDesignView::rowDeleted()
00339 {
00340 int rowindex = d->propertyset->currentRow();
00341 if(rowindex < 0) {
00342 kdWarning() << "KexiMacroDesignView::rowDeleted() No such item" << endl;
00343 return;
00344 }
00345 kdDebug() << "KexiMacroDesignView::rowDeleted() rowindex=" << rowindex << endl;
00346 KoMacro::MacroItem::List& macroitems = macro()->items();
00347 macroitems.remove( macroitems.at(rowindex) );
00348 }
00349
00350 bool KexiMacroDesignView::updateSet(KoProperty::Set* set, KSharedPtr<KoMacro::MacroItem> macroitem, const QString& variablename)
00351 {
00352 kdDebug() << "KexiMacroDesignView::updateSet() variablename=" << variablename << endl;
00353 KoProperty::Property* property = KexiMacroProperty::createProperty(macroitem, variablename);
00354 if(! property)
00355 return false;
00356 set->addProperty(property);
00357 return true;
00358 }
00359
00360 void KexiMacroDesignView::updateProperties(int row, KoProperty::Set* set, KSharedPtr<KoMacro::MacroItem> macroitem)
00361 {
00362 kdDebug() << "KexiMacroDesignView::updateProperties() row=" << row << endl;
00363
00364 if(row < 0 || d->updatesProperties) {
00365 return;
00366 }
00367
00368 KSharedPtr<KoMacro::Action> action = macroitem->action();
00369 if(! action.data()) {
00370
00371 d->propertyset->remove(row);
00372 return;
00373 }
00374
00375 d->updatesProperties = true;
00376
00377 if(set) {
00378
00379 set->clear();
00380 }
00381 else {
00382
00383 set = new KoProperty::Set(d->propertyset, action->name());
00384 d->propertyset->insert(row, set, true);
00385 connect(set, SIGNAL(propertyChanged(KoProperty::Set&, KoProperty::Property&)),
00386 this, SLOT(propertyChanged(KoProperty::Set&, KoProperty::Property&)));
00387 }
00388
00389
00390 KoProperty::Property* prop = new KoProperty::Property("this:classString", action->text());
00391 prop->setVisible(false);
00392 set->addProperty(prop);
00393
00394
00395 QStringList varnames = action->variableNames();
00396 for(QStringList::Iterator it = varnames.begin(); it != varnames.end(); ++it) {
00397 if(updateSet(set, macroitem, *it)) {
00398 KSharedPtr<KoMacro::Variable> variable = macroitem->variable(*it, true);
00399 kdDebug()<<"KexiMacroDesignView::updateProperties() name=" << *it << " variable=" << variable->variant().toString() << endl;
00400 #if 0
00401 macroitem->setVariable(*it, variable);
00402 #endif
00403 }
00404 }
00405
00406 d->updatesProperties = false;
00407 }
00408
00409 void KexiMacroDesignView::propertyChanged(KoProperty::Set& set, KoProperty::Property& property)
00410 {
00411 Q_UNUSED(set);
00412 kdDebug() << "!!!!! KexiMacroDesignView::propertyChanged() propertyname=" << property.name() << endl;
00413 setDirty();
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 }
00490
00491 void KexiMacroDesignView::reloadPropertyLater()
00492 {
00493 propertySetReloaded(true);
00494 }
00495
00496 #include "keximacrodesignview.moc"
00497