src/RecipesTree.cpp

changeset 153
e71642121fb6
parent 98
1425bf3e18ed
child 336
e97f9e87d94b
equal deleted inserted replaced
152:58e4ce7dd217 153:e71642121fb6
249 249
250 record = -2; 250 record = -2;
251 connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromRecipesTree())); 251 connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromRecipesTree()));
252 connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked())); 252 connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked()));
253 connect(openButton, SIGNAL(clicked()), this, SLOT(on_openButton_clicked())); 253 connect(openButton, SIGNAL(clicked()), this, SLOT(on_openButton_clicked()));
254
255 connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString))); 254 connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
256 255
257 // treeWidget->setAutoExpandDelay(1);
258 // treeWidget->setUpdatesEnabled(true);
259 // treeWidget->collapseAll();
260 // treeWidget->setRootIsDecorated(false);
261 treeWidget->setColumnCount(4); 256 treeWidget->setColumnCount(4);
262 treeWidget->setHeaderLabels({ tr("Guide"), tr("Group"), tr("Style"), tr("Recipe") }); 257 treeWidget->setHeaderLabels({ tr("Guide"), tr("Group"), tr("Style"), tr("Recipe") });
263 treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents); 258 treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
264 // treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); 259 treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
265 260
266 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_clicked(QTreeWidgetItem*, int))); 261 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_clicked(QTreeWidgetItem*, int)));
267 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_doubleclicked(QTreeWidgetItem*, int))); 262 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_doubleclicked(QTreeWidgetItem*, int)));
268 263
269 emit refreshTable(); 264 emit refreshTable();
272 267
273 void RecipesTree::refreshTable() 268 void RecipesTree::refreshTable()
274 { 269 {
275 QTreeWidgetItem *st_guide, *st_group, *st_name, *name; 270 QTreeWidgetItem *st_guide, *st_group, *st_name, *name;
276 271
277 qDebug() << "RecipesTree reload" << treeWidget->autoExpandDelay(); 272 qDebug() << "RecipesTree reload";
278 273
279 treeWidget->clear(); 274 treeWidget->clear();
280 275
281 QSqlQuery query0; 276 QSqlQuery query0;
282 query0.prepare("SELECT DISTINCT st_guide FROM recipes ORDER BY st_guide"); 277 query0.prepare("SELECT DISTINCT st_guide FROM recipes ORDER BY st_guide");
283 query0.exec(); 278 query0.exec();
284 query0.first(); 279 while (query0.next()) {
285 for (int i = 0; i < query0.size() ; i++) { 280 st_guide = new QTreeWidgetItem( QStringList( { query0.value(0).toString() } ));
286 st_guide = new QTreeWidgetItem(treeWidget);
287 st_guide->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled );
288 st_guide->setText(0, query0.value(0).toString());
289 treeWidget->addTopLevelItem( st_guide ); 281 treeWidget->addTopLevelItem( st_guide );
290 st_guide->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator); 282 st_guide->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
291 283
292 QSqlQuery query1; 284 QSqlQuery query1;
293 query1.prepare("SELECT DISTINCT st_letter FROM recipes WHERE st_guide=:guide ORDER BY st_letter"); 285 query1.prepare("SELECT DISTINCT st_letter FROM recipes WHERE st_guide=:guide ORDER BY st_letter");
294 query1.bindValue(":guide", query0.value(0).toString()); 286 query1.bindValue(":guide", query0.value(0).toString());
295 query1.exec(); 287 query1.exec();
296 query1.first(); 288 while (query1.next()) {
297 for (int j = 0; j < query1.size(); j++) { 289 st_group = new QTreeWidgetItem(QStringList({ "", query1.value(0).toString() }));
298 st_group = new QTreeWidgetItem(treeWidget);
299 st_group->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled );
300 st_group->setText(1, query1.value(0).toString());
301 st_group->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator); 290 st_group->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
302 st_guide->addChild( st_group ); 291 st_guide->addChild( st_group );
303 292
304 QSqlQuery query2; 293 QSqlQuery query2;
305 query2.prepare("SELECT DISTINCT st_name FROM recipes WHERE st_guide=:guide AND st_letter=:group ORDER BY st_name"); 294 query2.prepare("SELECT DISTINCT st_name FROM recipes WHERE st_guide=:guide AND st_letter=:group ORDER BY st_name");
306 query2.bindValue(":guide", query0.value(0).toString()); 295 query2.bindValue(":guide", query0.value(0).toString());
307 query2.bindValue(":group", query1.value(0).toString()); 296 query2.bindValue(":group", query1.value(0).toString());
308 query2.exec(); 297 query2.exec();
309 query2.first(); 298 while (query2.next()) {
310 for (int k = 0; k < query2.size(); k++) { 299 st_name = new QTreeWidgetItem(QStringList({ "", "", query2.value(0).toString() }));
311 st_name = new QTreeWidgetItem(treeWidget);
312 st_name->setText(2, query2.value(0).toString());
313 st_name->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator); 300 st_name->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
314 st_group->addChild( st_name ); 301 st_group->addChild( st_name );
315 302
316 QSqlQuery query3; 303 QSqlQuery query3;
317 query3.prepare("SELECT name,record FROM recipes WHERE st_guide=:guide AND st_letter=:group AND st_name=:name ORDER BY name"); 304 query3.prepare("SELECT name,record FROM recipes WHERE st_guide=:guide AND st_letter=:group AND st_name=:name ORDER BY name");
318 query3.bindValue(":guide", query0.value(0).toString()); 305 query3.bindValue(":guide", query0.value(0).toString());
319 query3.bindValue(":group", query1.value(0).toString()); 306 query3.bindValue(":group", query1.value(0).toString());
320 query3.bindValue(":name", query2.value(0).toString()); 307 query3.bindValue(":name", query2.value(0).toString());
321 query3.exec(); 308 query3.exec();
322 query3.first(); 309 while (query3.next()) {
323 for (int l = 0; l < query3.size(); l++) { 310 name = new QTreeWidgetItem(QStringList({ "", "", "", query3.value(0).toString(), query3.value(1).toString() }));
324 name = new QTreeWidgetItem(treeWidget);
325 name->setText(3, query3.value(0).toString());
326 name->setText(4, query3.value(1).toString());
327 st_name->addChild( name ); 311 st_name->addChild( name );
328
329 query3.next();
330 } 312 }
331
332 query2.next();
333 } 313 }
334 query1.next(); 314 treeWidget->expandItem(st_group);
335 } 315 }
336 query0.next(); 316 treeWidget->expandItem(st_guide);
337 } 317 }
338 318
339 QSqlQuery query("SELECT * FROM recipes ORDER BY st_guide,st_letter,st_name,name"); 319 QSqlQuery query("SELECT record FROM recipes");
340 emit setStatus(QString(tr("Total items: %1")).arg(query.size())); 320 emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
341 321
342 showRecipe(); 322 showRecipe();
343 } 323 }
344 324
387 recipeBox->setTitle(query.value(13).toString()); 367 recipeBox->setTitle(query.value(13).toString());
388 recipeBox->setEnabled(true); 368 recipeBox->setEnabled(true);
389 } else { 369 } else {
390 qDebug() << "Error getting recipe record" << record; 370 qDebug() << "Error getting recipe record" << record;
391 } 371 }
392 qDebug() << "showRecipe done";
393 } 372 }
394 373
395 374
396 RecipesTree::~RecipesTree() {} 375 RecipesTree::~RecipesTree() {}
397 376
398 377
399 void RecipesTree::edit(int recno) 378 void RecipesTree::edit(int recno)
400 { 379 {
401 qDebug() << "edit" << recno;
402 EditRecipe dialog(recno, this); 380 EditRecipe dialog(recno, this);
403 /* Signal from editor if a refresh is needed */ 381 /* Signal from editor if a refresh is needed */
404 connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable())); 382 connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
405 dialog.setModal(true); 383 dialog.setModal(true);
406 int rc = dialog.exec(); 384 dialog.exec();
407 qDebug() << "edit is ready rc=" << rc;
408 } 385 }
409 386
410 387
411 void RecipesTree::on_openButton_clicked() 388 void RecipesTree::on_openButton_clicked()
412 { 389 {
421 } 398 }
422 399
423 400
424 void RecipesTree::on_item_clicked(QTreeWidgetItem *item, int col) 401 void RecipesTree::on_item_clicked(QTreeWidgetItem *item, int col)
425 { 402 {
426 // qDebug() << Q_FUNC_INFO << item->text(col) << "col:" << col << item->text(4);
427
428 record = -2; // Invalid 403 record = -2; // Invalid
429 404
430 if (col == 0) { 405 if (col == 0) {
431 qDebug() << item->text(0) << item->text(0).length() << item->isExpanded();
432 if (item->isExpanded()) { 406 if (item->isExpanded()) {
433 treeWidget->collapseItem(item); 407 treeWidget->collapseItem(item);
434 } else { 408 } else {
435 treeWidget->expandItem(item); 409 treeWidget->expandItem(item);
436 } 410 }
437 } else if (col == 1) { 411 } else if (col == 1) {
438 qDebug() << item->text(1) << item->text(1).length() << item->isExpanded();
439 if (item->isExpanded()) { 412 if (item->isExpanded()) {
440 treeWidget->collapseItem(item); 413 treeWidget->collapseItem(item);
441 } else { 414 } else {
442 treeWidget->expandItem(item); 415 treeWidget->expandItem(item);
443 } 416 }
444 } else if (col == 2 && item->text(2).length()) { 417 } else if (col == 2 && item->text(2).length()) {
445 qDebug() << item->text(2) << item->text(2).length() << item->isExpanded();
446 if (item->isExpanded()) { 418 if (item->isExpanded()) {
447 item->setExpanded(false); 419 item->setExpanded(false);
448 treeWidget->collapseItem(item);
449 } else { 420 } else {
450 item->setExpanded(true); 421 item->setExpanded(true);
451 treeWidget->expandItem(item);
452 } 422 }
453 } else if (col == 3) { 423 } else if (col == 3) {
454 /* 424 /*
455 * if a recipe name is selected then: 425 * if a recipe name is selected then:
456 * item column 3 contains the recipe name, 426 * item column 3 contains the recipe name,
457 * item column 4 contains the recipe record number. 427 * item column 4 contains the recipe record number.
458 */ 428 */
459 if (item->text(4).toInt() > 0) { 429 if (item->text(4).toInt() > 0) {
460 qDebug() << "save for openButton" << item->text(4);
461 if (record != item->text(4).toInt()) { 430 if (record != item->text(4).toInt()) {
462 record = item->text(4).toInt(); 431 record = item->text(4).toInt();
463 showRecipe(); 432 showRecipe();
464 } 433 }
465 } 434 }
467 } 436 }
468 437
469 438
470 void RecipesTree::on_item_doubleclicked(QTreeWidgetItem *item, int col) 439 void RecipesTree::on_item_doubleclicked(QTreeWidgetItem *item, int col)
471 { 440 {
472 // qDebug() << Q_FUNC_INFO << item->text(col) << "col:" << col;
473
474 if ((col == 3) && (item->text(4).toInt() > 0)) { 441 if ((col == 3) && (item->text(4).toInt() > 0)) {
475 qDebug() << "shorcut open record" << item->text(4);
476 edit(item->text(4).toInt()); 442 edit(item->text(4).toInt());
477 } else if (item->text(col).length() > 0) {
478 qDebug() << "doubleclick on" << item->text(col);
479 } 443 }
480 } 444 }
481 445
482 446

mercurial