00001
00002 #include <QtGui/qabstractitemview.h>
00003
00004 #include "main.h"
00005 #include "treescreen.h"
00006 #include "recordtabledata.h"
00007
00008 extern appconfig mytetraconfig;
00009
00010
00011 treescreen::treescreen(QWidget *parent) : QWidget(parent)
00012 {
00013 setup_actions();
00014 setup_ui();
00015
00016 init_knowtree();
00017
00018 setup_signals();
00019 assembly();
00020
00021
00022
00023 knowtree->setContextMenuPolicy(Qt::CustomContextMenu);
00024 }
00025
00026
00027 treescreen::~treescreen()
00028 {
00029
00030 }
00031
00032
00033 void treescreen::setup_actions(void)
00034 {
00035
00036
00037 action_expand_all_subbranch = new QAction(tr("Expand all subbranches"), this);
00038 action_expand_all_subbranch->setStatusTip(tr("Expand all subbranches"));
00039 action_expand_all_subbranch->setIcon(QIcon("resource/pic/expand_all_subbranch.svg"));
00040 connect(action_expand_all_subbranch, SIGNAL(triggered()), this, SLOT(expand_all_subbranch()));
00041
00042
00043 action_collapse_all_subbranch = new QAction(tr("Collapse all subbranches"), this);
00044 action_collapse_all_subbranch->setStatusTip(tr("Collapse all subbranches"));
00045 action_collapse_all_subbranch->setIcon(QIcon("resource/pic/collapse_all_subbranch.svg"));
00046 connect(action_collapse_all_subbranch, SIGNAL(triggered()), this, SLOT(collapse_all_subbranch()));
00047
00048
00049 action_move_up_branch = new QAction(tr("Move up branch"), this);
00050 action_move_up_branch->setStatusTip(tr("Move up branch"));
00051 action_move_up_branch->setIcon(QIcon("resource/pic/move_up.svg"));
00052 connect(action_move_up_branch, SIGNAL(triggered()), this, SLOT(move_up_branch()));
00053
00054
00055 action_move_dn_branch = new QAction(tr("Move down branch"), this);
00056 action_move_dn_branch->setStatusTip(tr("Move down branch"));
00057 action_move_dn_branch->setIcon(QIcon("resource/pic/move_dn.svg"));
00058 connect(action_move_dn_branch, SIGNAL(triggered()), this, SLOT(move_dn_branch()));
00059
00060
00061 action_ins_subbranch = new QAction(tr("Insert new subbranch"), this);
00062 action_ins_subbranch->setStatusTip(tr("Insert new subbranch into selected branch"));
00063 action_ins_subbranch->setIcon(QIcon("resource/pic/add_subbranch.svg"));
00064 connect(action_ins_subbranch, SIGNAL(triggered()), this, SLOT(ins_subbranch()));
00065
00066
00067 action_ins_branch = new QAction(tr("Insert new branch"), this);
00068 action_ins_branch->setStatusTip(tr("Insert new branch after sibling selected branch"));
00069 action_ins_branch->setIcon(QIcon("resource/pic/add_branch.svg"));
00070 connect(action_ins_branch, SIGNAL(triggered()), this, SLOT(ins_branch()));
00071
00072
00073 action_edit_branch = new QAction(tr("Edit branch name"), this);
00074 action_edit_branch->setStatusTip(tr("Edit name of selected branch"));
00075 action_edit_branch->setIcon(QIcon("resource/pic/note_edit.svg"));
00076 connect(action_edit_branch, SIGNAL(triggered()), this, SLOT(edit_branch()));
00077
00078
00079 action_del_branch = new QAction(tr("Delete branch"), this);
00080 action_del_branch->setStatusTip(tr("Delete selected branch and all subbranches"));
00081 action_del_branch->setIcon(QIcon("resource/pic/note_delete.svg"));
00082 connect(action_del_branch, SIGNAL(triggered()), this, SLOT(del_branch()));
00083
00084 }
00085
00086
00087 void treescreen::setup_ui(void)
00088 {
00089 tools_line=new QToolBar(this);
00090 tools_line->addAction(action_ins_subbranch);
00091 tools_line->addAction(action_ins_branch);
00092 tools_line->addAction(action_edit_branch);
00093 tools_line->addAction(action_del_branch);
00094 tools_line->addSeparator();
00095 tools_line->addAction(action_expand_all_subbranch);
00096 tools_line->addAction(action_collapse_all_subbranch);
00097 tools_line->addSeparator();
00098 tools_line->addAction(action_move_up_branch);
00099 tools_line->addAction(action_move_dn_branch);
00100
00101 knowtree=new QTreeView(this);
00102 knowtree->setObjectName("knowtree");
00103 knowtree->setMinimumSize(150,400);
00104 knowtree->setWordWrap(true);
00105 knowtree->setSelectionMode(QAbstractItemView::ExtendedSelection);
00106 knowtree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
00107
00108
00109 }
00110
00111
00112
00113 void treescreen::on_customContextMenuRequested(const QPoint &pos)
00114 {
00115 qDebug() << "In treescreen on_customContextMenuRequested";
00116
00117
00118 QMenu menu(this);
00119 menu.addAction(action_ins_subbranch);
00120 menu.addAction(action_ins_branch);
00121 menu.addAction(action_edit_branch);
00122 menu.addAction(action_del_branch);
00123 menu.addSeparator();
00124 menu.addAction(action_expand_all_subbranch);
00125 menu.addAction(action_collapse_all_subbranch);
00126
00127
00128
00129 menu.exec(knowtree->viewport()->mapToGlobal(pos));
00130 }
00131
00132
00133 void treescreen::setup_signals(void)
00134 {
00135
00136 connect(knowtree,SIGNAL(customContextMenuRequested(const QPoint &)),
00137 this,SLOT(on_customContextMenuRequested(const QPoint &)));
00138
00139
00140 connect(knowtree->selectionModel(), SIGNAL(currentRowChanged (const QModelIndex&, const QModelIndex&)),
00141 this, SLOT(on_knowtree_clicked(const QModelIndex&)));
00142
00143
00144 connect(knowtree, SIGNAL(doubleClicked(const QModelIndex &)),
00145 this, SLOT(edit_branch(void)));
00146
00147
00148
00149
00150 }
00151
00152
00153 void treescreen::assembly(void)
00154 {
00155 treescreen_layout=new QVBoxLayout();
00156 treescreen_layout->setObjectName("treescreen_QVBoxLayout");
00157
00158 treescreen_layout->addWidget(tools_line);
00159 treescreen_layout->addWidget(knowtree);
00160
00161 setLayout(treescreen_layout);
00162
00163
00164 QLayout *lt;
00165 lt=layout();
00166 lt->setContentsMargins(0,2,0,0);
00167 }
00168
00169
00170 void treescreen::expand_all_subbranch(void)
00171 {
00172
00173 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00174
00175 for(int i = 0; i < selectitems.size(); ++i)
00176 expand_or_collapse_recurse(selectitems.at(i), true);
00177 }
00178
00179
00180 void treescreen::collapse_all_subbranch(void)
00181 {
00182
00183 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00184
00185 for(int i = 0; i < selectitems.size(); ++i)
00186 expand_or_collapse_recurse(selectitems.at(i), false);
00187 }
00188
00189
00190 void treescreen::expand_or_collapse_recurse(QModelIndex index,bool mode)
00191 {
00192 knowtree->setExpanded(index, mode);
00193
00194 int i=0;
00195 while( (index.child(i,0)).isValid() )
00196 {
00197 expand_or_collapse_recurse(index.child(i,0), mode);
00198 i++;
00199 }
00200
00201 }
00202
00203
00204 void treescreen::move_up_branch(void)
00205 {
00206 move_updn_branch(1);
00207 }
00208
00209
00210 void treescreen::move_dn_branch(void)
00211 {
00212 move_updn_branch(-1);
00213 }
00214
00215
00216 void treescreen::move_updn_branch(int direction)
00217 {
00218
00219 if(!move_check_enable()) return;
00220
00221
00222 QModelIndex index=get_current_item_index();
00223
00224
00225 QModelIndex index_after_move;
00226 if(direction==1) index_after_move=kntrmodel->move_up_branch(index);
00227 else index_after_move=kntrmodel->move_dn_branch(index);
00228
00229
00230 if(index_after_move.isValid())
00231 {
00232 knowtree->selectionModel()->setCurrentIndex(index_after_move,QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current);
00233 knowtree->selectionModel()->select(index_after_move,QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current);
00234 }
00235
00236
00237 find_object<treescreen>("treeview")->save_knowtree();
00238 }
00239
00240
00241 bool treescreen::move_check_enable(void)
00242 {
00243
00244 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00245
00246
00247 if(selectitems.size()>1)
00248 {
00249 QMessageBox messageBox(this);
00250 messageBox.setWindowTitle(tr("Unavailable action"));
00251 messageBox.setText(tr("You select ")+QString::number(selectitems.size())+tr(" branches.\nPlease select one branch for moving."));
00252 QAbstractButton *okButton =messageBox.addButton(tr("Ok"),QMessageBox::AcceptRole);
00253 messageBox.exec();
00254 return false;
00255 }
00256 else
00257 return true;
00258 }
00259
00260
00261 void treescreen::ins_subbranch(void)
00262 {
00263 qDebug() << "In ins_subbranch()";
00264
00265
00266 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00267
00268
00269 if(selectitems.size()>1)
00270 {
00271 QMessageBox messageBox(this);
00272 messageBox.setWindowTitle(tr("Unavailable action"));
00273 messageBox.setText(tr("You select ")+QString::number(selectitems.size())+tr(" branches.\nPlease select one branch for insert subbranch."));
00274 QAbstractButton *okButton =messageBox.addButton(tr("Ok"),QMessageBox::AcceptRole);
00275 messageBox.exec();
00276 return;
00277 }
00278
00279
00280
00281 QModelIndex index=get_current_item_index();
00282
00283
00284 TreeItem *item=kntrmodel->getItem(index);
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 bool ok;
00300 QString name = QInputDialog::getText(this,
00301 tr("Insert new subbranch"),
00302 tr("Subbranch name:"),
00303 QLineEdit::Normal,
00304 "",
00305 &ok);
00306
00307
00308 if (!( ok && !name.isEmpty() )) return;
00309
00310
00311
00312
00313 find_object<mainwindow>("mainwindow")->setDisabled(true);
00314
00315
00316 int idnum=mytetraconfig.get_lastidnum();
00317 mytetraconfig.inc_lastidnum();
00318 QString id;
00319 id.setNum(idnum);
00320
00321
00322 kntrmodel->add_new_child_branch(index,id,name);
00323
00324
00325 knowtree->selectionModel()->setCurrentIndex(kntrmodel->indexChildren(index,item->childCount()-1),
00326 QItemSelectionModel::ClearAndSelect);
00327
00328
00329
00330
00331
00332
00333 find_object<treescreen>("treeview")->save_knowtree();
00334
00335 find_object<mainwindow>("mainwindow")->setEnabled(true);
00336 }
00337
00338
00339 void treescreen::ins_branch(void)
00340 {
00341 qDebug() << "In ins_branch()";
00342
00343
00344 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00345
00346
00347 if(selectitems.size()>1)
00348 {
00349 QMessageBox messageBox(this);
00350 messageBox.setWindowTitle(tr("Unavailable action"));
00351 messageBox.setText(tr("You select ")+QString::number(selectitems.size())+tr(" branches.\nPlease select one branch for insert sibling branch."));
00352 QAbstractButton *okButton =messageBox.addButton(tr("Ok"),QMessageBox::AcceptRole);
00353 messageBox.exec();
00354 return;
00355 }
00356
00357
00358
00359 QModelIndex index=get_current_item_index();
00360
00361
00362 TreeItem *item=kntrmodel->getItem(index);
00363
00364
00365 bool ok;
00366 QString name = QInputDialog::getText(this,
00367 tr("Insert new branch"),
00368 tr("Branch name:"),
00369 QLineEdit::Normal,
00370 "",
00371 &ok);
00372
00373
00374 if (!( ok && !name.isEmpty() )) return;
00375
00376
00377
00378
00379 find_object<mainwindow>("mainwindow")->setDisabled(true);
00380
00381
00382 int idnum=mytetraconfig.get_lastidnum();
00383 mytetraconfig.inc_lastidnum();
00384 QString id;
00385 id.setNum(idnum);
00386
00387
00388 kntrmodel->add_new_sibling_branch(index,id,name);
00389
00390
00391
00392
00393
00394
00395
00396
00397 QModelIndex setto=kntrmodel->index(item->parent()->childCount()-1,0,index.parent());
00398 knowtree->selectionModel()->setCurrentIndex(setto,QItemSelectionModel::ClearAndSelect);
00399
00400
00401 find_object<treescreen>("treeview")->save_knowtree();
00402
00403 find_object<mainwindow>("mainwindow")->setEnabled(true);
00404 }
00405
00406
00407 void treescreen::insert_new_branch(void)
00408 {
00409
00410
00411 }
00412
00413
00414 void treescreen::edit_branch(void)
00415 {
00416 qDebug() << "In edit_branch()";
00417
00418
00419 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00420
00421
00422 if(selectitems.size()>1)
00423 {
00424 QMessageBox messageBox(this);
00425 messageBox.setWindowTitle(tr("Unavailable action"));
00426 messageBox.setText(tr("You select ")+QString::number(selectitems.size())+tr(" branches.\nPlease select one branch for edit name."));
00427 QAbstractButton *okButton =messageBox.addButton(tr("Ok"),QMessageBox::AcceptRole);
00428 messageBox.exec();
00429 return;
00430 }
00431
00432
00433 QModelIndex index=get_current_item_index();
00434
00435
00436 TreeItem *item=kntrmodel->getItem(index);
00437
00438
00439 QString name=(item->data("name")).toString();
00440
00441
00442 bool ok;
00443 QString newname = QInputDialog::getText(this,
00444 tr("Edit branch name"),
00445 tr("Branch name:"),
00446 QLineEdit::Normal,
00447 name,
00448 &ok);
00449
00450
00451 if (!( ok && !newname.isEmpty() )) return;
00452
00453 find_object<mainwindow>("mainwindow")->setDisabled(true);
00454
00455 item->setData("name",newname);
00456
00457
00458 find_object<treescreen>("treeview")->save_knowtree();
00459
00460 find_object<mainwindow>("mainwindow")->setEnabled(true);
00461
00462 }
00463
00464
00465 void treescreen::del_branch(void)
00466 {
00467 qDebug() << "In del_branch()";
00468
00469
00470 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00471
00472
00473 if(selectitems.size()>1)
00474 {
00475 QMessageBox messageBox(this);
00476 messageBox.setWindowTitle(tr("Unavailable action"));
00477 messageBox.setText(tr("Please select one branch for delete."));
00478 QAbstractButton *okButton =messageBox.addButton(tr("Ok"),QMessageBox::AcceptRole);
00479 messageBox.exec();
00480 return;
00481 }
00482
00483
00484
00485 QModelIndex index=get_current_item_index();
00486
00487
00488 TreeItem *item=kntrmodel->getItem(index);
00489
00490
00491
00492 QMessageBox messageBox(this);
00493 messageBox.setWindowTitle(tr("Delete branch"));
00494 messageBox.setText(tr("Are you sure to delete branch ") + (item->data("name")).toString() + tr(" and it subbranches?"));
00495 QAbstractButton *cancelButton =messageBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
00496 QAbstractButton *deleteButton =messageBox.addButton(tr("Delete"), QMessageBox::AcceptRole);
00497 messageBox.exec();
00498 if(messageBox.clickedButton() != deleteButton)return;
00499
00500 qDebug() << "Before delete item";
00501
00502 find_object<mainwindow>("mainwindow")->setDisabled(true);
00503 find_object<mainwindow>("mainwindow")->blockSignals(true);
00504
00505
00506
00507
00508
00509 kntrmodel->removeRow(index.row(), index.parent());
00510
00511 qDebug() << "Arter delete item";
00512
00513
00514 find_object<treescreen>("treeview")->save_knowtree();
00515
00516 find_object<mainwindow>("mainwindow")->setEnabled(true);
00517 find_object<mainwindow>("mainwindow")->blockSignals(false);
00518
00519 return;
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545 }
00546
00547
00548
00549 void treescreen::on_knowtree_clicked(const QModelIndex &index)
00550 {
00551
00552
00553
00554 find_object<mainwindow>("mainwindow")->save_current_record_text();
00555
00556
00557 TreeItem *item = kntrmodel->getItem(index);
00558
00559
00560 recordtabledata *rtdata=item->recordtable_gettabledata();
00561
00562
00563 find_object<recordtablescreen>("recordtableview")->set_tabledata(rtdata);
00564
00565
00566 knowtree->resizeColumnToContents(0);
00567 }
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579 void treescreen::init_knowtree(void)
00580 {
00581
00582 xmltree xmlt;
00583 if(!xmlt.load(mytetraconfig.get_tetradir()+"/mytetra.xml"))return;
00584
00585 QStringList headers;
00586 headers << tr("Group of info");
00587
00588
00589 kntrmodel = new knowtreemodel(headers, xmlt.dommodel);
00590
00591
00592 knowtree->setModel(kntrmodel);
00593
00594
00595 knowtree->setEditTriggers(QAbstractItemView::NoEditTriggers);
00596 }
00597
00598
00599
00600 void treescreen::save_knowtree(void)
00601 {
00602
00603 QDomDocument doc("mytetradoc");
00604
00605
00606 doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
00607
00608
00609 QDomElement rootelement=doc.createElement("root");
00610
00611
00612 QDomElement formvers=doc.createElement("format");
00613 formvers.setAttribute("version",CURRENT_FORMAT_VERSION);
00614 formvers.setAttribute("subversion",CURRENT_FORMAT_SUBVERSION);
00615 rootelement.appendChild(formvers);
00616
00617
00618 QDomElement elmdomtree=kntrmodel->export_fullmodeldata_to_dom(kntrmodel->rootItem);
00619
00620
00621 rootelement.appendChild(elmdomtree);
00622
00623
00624 doc.appendChild(rootelement);
00625
00626
00627
00628
00629
00630 QString filenamefrom=mytetraconfig.get_tetradir()+"/mytetra.xml";
00631 QString filenameto =mytetraconfig.get_trashdir()+"/"+mytetraconfig.get_lastprefixnum_as_line()+"_mytetra.xml";
00632 mytetraconfig.inc_lastprefixnum();
00633 qDebug() << "Move file from " << filenamefrom << " to " << filenameto;
00634 QFile::rename(filenamefrom,filenameto);
00635
00636
00637 QString filename=mytetraconfig.get_tetradir()+"/mytetra.xml";
00638 QFile wfile(filename);
00639 if (!wfile.open(QIODevice::WriteOnly | QIODevice::Text))
00640 {
00641 qDebug() << "Cant open file " << filename << " for write.";
00642 exit(1);
00643 }
00644 QTextStream out(&wfile);
00645 out.setCodec("UTF-8");
00646 out << doc.toString();
00647 }
00648
00649
00650 void treescreen::update_selected_branch(void)
00651 {
00652
00653 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00654
00655
00656 for (int i = 0; i < selectitems.size(); ++i)
00657 knowtree->update(selectitems.at(i));
00658 }
00659
00660
00661 QItemSelectionModel * treescreen::get_selection_model(void)
00662 {
00663 return knowtree->selectionModel();
00664 }
00665
00666
00667 void treescreen::set_cursor_to_index(QModelIndex index)
00668 {
00669 knowtree->selectionModel()->setCurrentIndex(index,QItemSelectionModel::ClearAndSelect);
00670 }
00671
00672
00673
00674 int treescreen::get_first_selected_item_index(void)
00675 {
00676
00677 QModelIndexList selectitems=knowtree->selectionModel()->selectedIndexes();
00678
00679 if(selectitems.isEmpty())
00680 return -1;
00681 else
00682 return (selectitems.at(0)).row();
00683 }
00684
00685
00686
00687 QModelIndex treescreen::get_current_item_index(void)
00688 {
00689 return knowtree->selectionModel()->currentIndex();
00690 }