1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
|
//
// File: PhyView.cpp
// Created by: Julien Dutheil
// Created on: Tue Aug 05 14:59 2009
//
/*
Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
This software is a computer program whose purpose is to provide
graphic components to develop bioinformatics applications.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/
#include "PhyView.h"
#include "TreeSubWindow.h"
#include "TreeDocument.h"
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QButtonGroup>
#include <QDockWidget>
#include <QUndoView>
#include <QLineEdit>
#include <QAction>
#include <QMenuBar>
#include <QInputDialog>
#include <QGraphicsTextItem>
#include <Bpp/Qt/QtGraphicDevice.h>
#include <Bpp/Numeric/DataTable.h>
#include <Bpp/Phyl/Tree.h>
#include <Bpp/Phyl/Io/Nhx.h>
#include <Bpp/Phyl/Graphics/PhylogramPlot.h>
#include <fstream>
using namespace std;
using namespace bpp;
MouseActionListener::MouseActionListener(PhyView* phyview):
phyview_(phyview),
treeChooser_(new QDialog()),
treeList_(new QListWidget(treeChooser_))
{
treeChooser_->setParent(phyview_);
treeChooser_->setModal(true);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(treeList_);
layout->addStretch(1);
treeChooser_->setLayout(layout);
treeChooser_->connect(treeList_, SIGNAL(itemClicked(QListWidgetItem*)), treeChooser_, SLOT(accept()));
}
TranslateNameChooser::TranslateNameChooser(PhyView* phyview) :
QDialog(phyview), phyview_(phyview), fileDialog_(new QFileDialog(this))
{
fileFilters_ << "Coma separated columns (*.txt *.csv)"
<< "Tab separated columns (*.txt *.csv)";
fileDialog_->setNameFilters(fileFilters_);
fileDialog_->setOptions(QFileDialog::DontUseNativeDialog);
hasHeader_ = new QCheckBox(tr("File has header line"));
QGridLayout *dlayout = dynamic_cast<QGridLayout*>(fileDialog_->layout()); //Check that here!!
dlayout->addWidget(hasHeader_, 4, 0);
QFormLayout* layout = new QFormLayout;
fromList_ = new QComboBox;
toList_ = new QComboBox;
ok_ = new QPushButton(tr("Ok"));
cancel_ = new QPushButton(tr("Cancel"));
layout->addRow(tr("From"), fromList_);
layout->addRow(tr("To") , toList_);
layout->addRow(cancel_, ok_);
connect(ok_, SIGNAL(clicked(bool)), this, SLOT(accept()));
connect(cancel_, SIGNAL(clicked(bool)), this, SLOT(reject()));
setLayout(layout);
}
void TranslateNameChooser::translateTree(TreeTemplate<Node>& tree)
{
fileDialog_->setAcceptMode(QFileDialog::AcceptOpen);
if (fileDialog_->exec() == QDialog::Accepted) {
QStringList path = fileDialog_->selectedFiles();
string sep = ",";
if (fileDialog_->selectedNameFilter() == fileFilters_[1])
sep = "\t";
ifstream file(path[0].toStdString().c_str(), ios::in);
try {
DataTable* table = DataTable::read(file, sep, hasHeader_->isChecked());
//Clean button groups:
fromList_->clear();
toList_->clear();
//Now add the new ones:
if (!hasHeader_->isChecked()) {
vector<string> names;
for (unsigned int i = 0; i < table->getNumberOfColumns(); ++i) {
names.push_back("Col" + TextTools::toString(i + 1));
}
table->setColumnNames(names);
}
for (unsigned int i = 0; i < table->getNumberOfColumns(); ++i) {
fromList_->addItem(QtTools::toQt(table->getColumnName(i)));
toList_->addItem(QtTools::toQt(table->getColumnName(i)));
}
if (exec() == QDialog::Accepted)
phyview_->submitCommand(new TranslateNodeNamesCommand(phyview_->getActiveDocument(), *table, fromList_->currentIndex(), toList_->currentIndex()));
} catch (Exception& e) {
QMessageBox::critical(this, tr("Ouch..."), tr("Error when reading table:\n") + tr(e.what()));
}
}
}
DataLoader::DataLoader(PhyView* phyview) :
QDialog(phyview), phyview_(phyview)
{
QFormLayout* layout = new QFormLayout;
idIndex_ = new QRadioButton(tr("Index from id"));
idIndex_->setChecked(true);
nameIndex_ = new QRadioButton(tr("Index from name"));
indexCol_ = new QComboBox;
QButtonGroup* bg = new QButtonGroup();
bg->addButton(idIndex_);
bg->addButton(nameIndex_);
ok_ = new QPushButton(tr("Ok"));
cancel_ = new QPushButton(tr("Cancel"));
layout->addRow(idIndex_, nameIndex_);
layout->addRow(tr("Column"), indexCol_);
layout->addRow(cancel_, ok_);
connect(ok_, SIGNAL(clicked(bool)), this, SLOT(accept()));
connect(cancel_, SIGNAL(clicked(bool)), this, SLOT(reject()));
setLayout(layout);
}
void DataLoader::load(const DataTable* data)
{
indexCol_->clear();
for (unsigned int i = 0; i < data->getNumberOfColumns(); ++i)
indexCol_->addItem(QtTools::toQt(data->getColumnName(i)));
if (exec() == QDialog::Accepted) {
unsigned int index = static_cast<unsigned int>(indexCol_->currentIndex());
phyview_->submitCommand(new AttachDataCommand(phyview_->getActiveDocument(), *data, index, nameIndex_->isChecked()));
}
}
ImageExportDialog::ImageExportDialog(PhyView* phyview):
QDialog(phyview)
{
QGridLayout* layout = new QGridLayout;
path_ = new QLabel;
path_->setText("(none selected)");
layout->addWidget(path_, 1, 1);
browse_ = new QPushButton(tr("&Browse"));
connect(browse_, SIGNAL(clicked(bool)), this, SLOT(chosePath()));
layout->addWidget(browse_, 1, 2);
height_ = new QSpinBox;
height_->setRange(100, 10000);
layout->addWidget(new QLabel(tr("Height:")), 2, 1);
layout->addWidget(height_, 2, 2);
width_ = new QSpinBox;
width_->setRange(100, 10000);
layout->addWidget(new QLabel(tr("Width:")), 3, 1);
layout->addWidget(width_, 3, 2);
transparent_ = new QCheckBox(tr("Transparent"));
layout->addWidget(transparent_, 4, 1, 1, 2);
keepAspectRatio_ = new QCheckBox(tr("Keep aspect ratio"));
layout->addWidget(keepAspectRatio_, 5, 1, 1, 2);
ok_ = new QPushButton(tr("Ok"));
ok_->setDisabled(true);
connect(ok_, SIGNAL(clicked(bool)), this, SLOT(accept()));
layout->addWidget(ok_, 6, 2);
cancel_ = new QPushButton(tr("Cancel"));
connect(cancel_, SIGNAL(clicked(bool)), this, SLOT(reject()));
layout->addWidget(cancel_, 6, 1);
setLayout(layout);
imageFileDialog_ = new QFileDialog(this, "Image File");
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
for (int i = 0; i < formats.size(); ++i)
imageFileFilters_ << QString(formats[i]) + QString(" (*.*)");
imageFileDialog_->setNameFilters(imageFileFilters_);
}
void ImageExportDialog::chosePath()
{
if (imageFileDialog_->exec() == QDialog::Accepted) {
QStringList path = imageFileDialog_->selectedFiles();
int i = imageFileFilters_.indexOf(imageFileDialog_->selectedNameFilter());
path_->setText(path[0] + " (" + QString(QImageWriter::supportedImageFormats()[i]) + ")");
ok_->setEnabled(true);
}
}
void ImageExportDialog::process(QGraphicsScene* scene)
{
if (ok_->isEnabled()) {
QStringList path = imageFileDialog_->selectedFiles();
int i = imageFileFilters_.indexOf(imageFileDialog_->selectedNameFilter());
//Chose the correct format according to options:
QImage::Format format = QImage::Format_RGB32;
QBrush bckBrush = scene->backgroundBrush();
if (transparent_->isChecked()) {
format = QImage::Format_ARGB32_Premultiplied;
scene->setBackgroundBrush(Qt::NoBrush);
} else {
if (bckBrush == Qt::NoBrush)
scene->setBackgroundBrush(Qt::white);
}
QImage image(width_->value(), height_->value(), format);
QPainter painter;
painter.begin(&image);
if (keepAspectRatio_->isChecked())
scene->render(&painter);
else
scene->render(&painter, QRectF(), QRectF(), Qt::IgnoreAspectRatio);
painter.end();
scene->setBackgroundBrush(bckBrush);
image.save(path[0], QImageWriter::supportedImageFormats()[i]);
} else {
throw Exception("Can't process image as no file has been selected.");
}
}
TypeNumberDialog::TypeNumberDialog(PhyView* phyview, const string& what, unsigned int min, unsigned int max) :
QDialog(phyview)
{
QFormLayout* layout = new QFormLayout;
spinBox_ = new QSpinBox;
spinBox_->setRange(min, max);
ok_ = new QPushButton(tr("Ok"));
cancel_ = new QPushButton(tr("Cancel"));
layout->addRow(QtTools::toQt(what), spinBox_);
layout->addRow(cancel_, ok_);
connect(ok_, SIGNAL(clicked(bool)), this, SLOT(accept()));
connect(cancel_, SIGNAL(clicked(bool)), this, SLOT(reject()));
setLayout(layout);
}
void MouseActionListener::mousePressEvent(QMouseEvent *event)
{
if (dynamic_cast<NodeMouseEvent*>(event)->hasNodeId())
{
int nodeId = dynamic_cast<NodeMouseEvent*>(event)->getNodeId();
QString action;
if (event->button() == Qt::LeftButton)
action = phyview_->getMouseLeftButtonActionType();
else if (event->button() == Qt::MidButton)
action = phyview_->getMouseMiddleButtonActionType();
else if (event->button() == Qt::RightButton)
action = phyview_->getMouseRightButtonActionType();
else
action = "None";
if (action == "Swap")
{
if (!phyview_->getActiveDocument()->getTree()->isRoot(nodeId))
{
int fatherId = phyview_->getActiveDocument()->getTree()->getFatherId(nodeId);
vector<int> sonsId = phyview_->getActiveDocument()->getTree()->getSonsId(fatherId);
unsigned int i1 = 0, i2 = 0;
if (sonsId[0] == nodeId) {
i1 = 0;
i2 = sonsId.size() - 1;
} else {
for (unsigned int i = 1; i < sonsId.size(); ++i)
if (sonsId[i] == nodeId) {
i1 = i;
i2 = i - 1;
}
}
phyview_->submitCommand(new SwapCommand(phyview_->getActiveDocument(), fatherId, i1, i2 , nodeId, sonsId[i2]));
}
}
else if (action == "Order down")
{
phyview_->submitCommand(new OrderCommand(phyview_->getActiveDocument(), nodeId, true));
}
else if (action == "Order up")
{
phyview_->submitCommand(new OrderCommand(phyview_->getActiveDocument(), nodeId, false));
}
else if (action == "Root on node")
{
if (phyview_->getActiveDocument()->getTree()->getNode(nodeId)->isLeaf()) {
QMessageBox::warning(phyview_, "PhyView", "Cannot root on a leaf.", QMessageBox::Cancel);
} else {
phyview_->submitCommand(new RerootCommand(phyview_->getActiveDocument(), nodeId));
}
}
else if (action == "Root on branch")
phyview_->submitCommand(new OutgroupCommand(phyview_->getActiveDocument(), nodeId));
else if (action == "Collapse") {
TreeCanvas& tc = phyview_->getActiveSubWindow()->getTreeCanvas();
tc.collapseNode(nodeId, !tc.isNodeCollapsed(nodeId));
tc.redraw();
}
else if (action == "Sample subtree") {
Node* n = phyview_->getActiveDocument()->getTree()->getNode(nodeId);
TypeNumberDialog dial(phyview_, "Sample size", 1u, TreeTemplateTools::getNumberOfLeaves(*n));
if (dial.exec() == QDialog::Accepted) {
unsigned int size = dial.getValue();
phyview_->submitCommand(new SampleSubtreeCommand(phyview_->getActiveDocument(), nodeId, size));
}
} else if (action == "Delete subtree") {
phyview_->submitCommand(new DeleteSubtreeCommand(phyview_->getActiveDocument(), nodeId));
}
else if (action == "Copy subtree") {
Node* subtree = TreeTemplateTools::cloneSubtree<Node>(*phyview_->getActiveDocument()->getTree()->getNode(nodeId));
unique_ptr< TreeTemplate<Node> > tt(new TreeTemplate<Node>(subtree));
phyview_->createNewDocument(tt.get());
}
else if (action == "Cut subtree") {
Node* subtree = TreeTemplateTools::cloneSubtree<Node>(*phyview_->getActiveDocument()->getTree()->getNode(nodeId));
unique_ptr< TreeTemplate<Node> > tt(new TreeTemplate<Node>(subtree));
phyview_->submitCommand(new DeleteSubtreeCommand(phyview_->getActiveDocument(), nodeId));
phyview_->createNewDocument(tt.get());
}
else if (action == "Insert on node") {
TreeTemplate<Node>* tree = phyview_->pickTree();
if (tree) {
Node* subtree = TreeTemplateTools::cloneSubtree<Node>(*tree->getRootNode());
phyview_->submitCommand(new InsertSubtreeAtNodeCommand(phyview_->getActiveDocument(), nodeId, subtree));
}
}
else if (action == "Insert on branch") {
TreeTemplate<Node>* tree = phyview_->pickTree();
if (tree) {
Node* subtree = TreeTemplateTools::cloneSubtree<Node>(*tree->getRootNode());
phyview_->submitCommand(new InsertSubtreeOnBranchCommand(phyview_->getActiveDocument(), nodeId, subtree));
}
}
}
}
PhyView::PhyView():
manager_(),
collapsedNodesListener_(true)
{
setAttribute(Qt::WA_DeleteOnClose);
setAttribute(Qt::WA_QuitOnClose);
initGui_();
createActions_();
createMenus_();
createStatusBar_();
resize(1000, 600);
}
void PhyView::initGui_()
{
mdiArea_ = new QMdiArea;
connect(mdiArea_, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(setCurrentSubWindow(QMdiSubWindow*)));
setCentralWidget(mdiArea_);
//Trees panel:
createTreesPanel_();
treesDockWidget_ = new QDockWidget(tr("Trees"));
treesDockWidget_->setWidget(treesPanel_);
treesDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, treesDockWidget_);
//Stats panel:
createStatsPanel_();
statsDockWidget_ = new QDockWidget(tr("Statistics"));
statsDockWidget_->setWidget(statsPanel_);
statsDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, statsDockWidget_);
//Display panel:
createDisplayPanel_();
displayDockWidget_ = new QDockWidget(tr("Display"));
displayDockWidget_->setWidget(displayPanel_);
displayDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, displayDockWidget_);
//Search panel:
createSearchPanel_();
searchDockWidget_ = new QDockWidget(tr("Search in tree"));
searchDockWidget_->setWidget(searchPanel_);
searchDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, searchDockWidget_);
//Undo panel:
QUndoView* undoView = new QUndoView;
undoView->setGroup(&manager_);
undoDockWidget_ = new QDockWidget(tr("Undo list"));
undoDockWidget_->setWidget(undoView);
undoDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, undoDockWidget_);
//Branch lengths panel:
createBrlenPanel_();
brlenDockWidget_ = new QDockWidget(tr("Branch lengths"));
brlenDockWidget_->setWidget(brlenPanel_);
brlenDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, brlenDockWidget_);
brlenDockWidget_->setVisible(false);
//Mouse control panel:
createMouseControlPanel_();
mouseControlDockWidget_ = new QDockWidget(tr("Mouse control"));
mouseControlDockWidget_->setWidget(mouseControlPanel_);
mouseControlDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, mouseControlDockWidget_);
//Names operations panel:
createDataPanel_();
dataDockWidget_ = new QDockWidget(tr("Associated Data"));
dataDockWidget_->setWidget(dataPanel_);
dataDockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, dataDockWidget_);
dataDockWidget_->setVisible(false);
//Other stuff...
treeFileDialog_ = new QFileDialog(this, "Tree File");
treeFileFilters_ << "Newick files (*.dnd *.tre *.tree *.nwk *.newick *.phy *.txt)"
<< "Nexus files (*.nx *.nex *.nexus)"
<< "Nhx files (*.nhx)";
treeFileDialog_->setNameFilters(treeFileFilters_);
treeFileDialog_->setConfirmOverwrite(true);
dataFileDialog_ = new QFileDialog(this, "Data File");
dataFileFilters_ << "Coma separated columns (*.txt *.csv)"
<< "Tab separated columns (*.txt *.csv)";
dataFileDialog_->setNameFilters(dataFileFilters_);
imageExportDialog_ = new ImageExportDialog(this);
printer_ = new QPrinter(QPrinter::HighResolution);
printDialog_ = new QPrintDialog(printer_, this);
translateNameChooser_ = new TranslateNameChooser(this);
dataLoader_ = new DataLoader(this);
}
void PhyView::createDisplayPanel_()
{
displayPanel_ = new QWidget(this);
treeControlers_ = new TreeCanvasControlers();
treeControlers_->addActionListener(this);
for (unsigned int i = 0; i < treeControlers_->getNumberOfTreeDrawings(); ++i)
treeControlers_->getTreeDrawing(i)->addTreeDrawingListener(&collapsedNodesListener_);
QGroupBox* drawingOptions = new QGroupBox(tr("Drawing"));
QFormLayout* drawingLayout = new QFormLayout;
drawingLayout->addRow(tr("&Type:"), treeControlers_->getControlerById(TreeCanvasControlers::ID_DRAWING_CTRL));
drawingLayout->addRow(tr("&Orientation:"), treeControlers_->getControlerById(TreeCanvasControlers::ID_ORIENTATION_CTRL));
drawingLayout->addRow(tr("Width (px):"), treeControlers_->getControlerById(TreeCanvasControlers::ID_WIDTH_CTRL));
drawingLayout->addRow(tr("&Height (px):"), treeControlers_->getControlerById(TreeCanvasControlers::ID_HEIGHT_CTRL));
drawingOptions->setLayout(drawingLayout);
QGroupBox* displayOptions = new QGroupBox(tr("Display"));
QVBoxLayout* displayLayout = new QVBoxLayout;
displayLayout->addWidget(treeControlers_->getControlerById(TreeCanvasControlers::ID_DRAW_NODE_IDS_CTRL));
displayLayout->addWidget(treeControlers_->getControlerById(TreeCanvasControlers::ID_DRAW_LEAF_NAMES_CTRL));
displayLayout->addWidget(treeControlers_->getControlerById(TreeCanvasControlers::ID_DRAW_BRANCH_LENGTHS_CTRL));
displayLayout->addWidget(treeControlers_->getControlerById(TreeCanvasControlers::ID_DRAW_BOOTSTRAP_VALUES_CTRL));
displayLayout->addWidget(treeControlers_->getControlerById(TreeCanvasControlers::ID_DRAW_CLICKABLE_AREAS_CTRL));
displayOptions->setLayout(displayLayout);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(drawingOptions);
layout->addWidget(displayOptions);
layout->addStretch(1);
displayPanel_->setLayout(layout);
}
void PhyView::createTreesPanel_()
{
treesPanel_ = new QWidget(this);
QVBoxLayout* treesLayout = new QVBoxLayout;
treesTable_ = new QTableWidget;
treesTable_->setColumnCount(2);
treesTable_->setHorizontalHeaderLabels(QString("Tree;Size").split(";"));
treesTable_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
treesTable_->setEditTriggers(QAbstractItemView::NoEditTriggers);
treesTable_->setSelectionBehavior(QAbstractItemView::SelectRows);
treesTable_->setSelectionMode(QAbstractItemView::SingleSelection);
connect(treesTable_, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(activateSelectedDocument()));
treesLayout->addWidget(treesTable_);
treesLayout->addStretch(1);
treesPanel_->setLayout(treesLayout);
}
void PhyView::createStatsPanel_()
{
statsPanel_ = new QWidget(this);
QVBoxLayout* statsLayout = new QVBoxLayout;
statsBox_ = new TreeStatisticsBox;
statsLayout->addWidget(statsBox_);
QPushButton* update = new QPushButton(tr("Update"));
connect(update, SIGNAL(clicked(bool)), this, SLOT(updateStatistics()));
statsLayout->addWidget(update);
statsLayout->addStretch(1);
statsPanel_->setLayout(statsLayout);
}
void PhyView::createBrlenPanel_()
{
brlenPanel_ = new QWidget(this);
QVBoxLayout* brlenLayout = new QVBoxLayout;
//Set all lengths:
brlenSetLengths_ = new QDoubleSpinBox;
brlenSetLengths_->setDecimals(6);
brlenSetLengths_->setSingleStep(0.01);
QPushButton* brlenSetLengthsGo = new QPushButton(tr("Go!"));
connect(brlenSetLengthsGo, SIGNAL(clicked(bool)), this, SLOT(setLengths()));
QGroupBox* brlenSetLengthsBox = new QGroupBox(tr("Set all lengths"));
QHBoxLayout* brlenSetLengthsBoxLayout = new QHBoxLayout;
brlenSetLengthsBoxLayout->addWidget(brlenSetLengths_);
brlenSetLengthsBoxLayout->addWidget(brlenSetLengthsGo);
brlenSetLengthsBoxLayout->addStretch(1);
brlenSetLengthsBox->setLayout(brlenSetLengthsBoxLayout);
brlenLayout->addWidget(brlenSetLengthsBox);
//Remove all branch lengths:
QPushButton* brlenRemoveAll = new QPushButton(tr("Remove all lengths"));
connect(brlenRemoveAll, SIGNAL(clicked(bool)), this, SLOT(deleteAllLengths()));
brlenLayout->addWidget(brlenRemoveAll);
//Grafen method:
QPushButton* brlenInitGrafen = new QPushButton(tr("Init"));
connect(brlenInitGrafen, SIGNAL(clicked(bool)), this, SLOT(initLengthsGrafen()));
brlenComputeGrafen_ = new QDoubleSpinBox;
brlenComputeGrafen_->setValue(1.);
brlenComputeGrafen_->setDecimals(2);
brlenComputeGrafen_->setSingleStep(0.1);
QPushButton* brlenComputeGrafenGo = new QPushButton(tr("Go!"));
connect(brlenComputeGrafenGo, SIGNAL(clicked(bool)), this, SLOT(computeLengthsGrafen()));
QGroupBox* brlenGrafenBox = new QGroupBox(tr("Grafen"));
QHBoxLayout* brlenGrafenBoxLayout = new QHBoxLayout;
brlenGrafenBoxLayout->addWidget(brlenInitGrafen);
brlenGrafenBoxLayout->addWidget(brlenComputeGrafen_);
brlenGrafenBoxLayout->addWidget(brlenComputeGrafenGo);
brlenGrafenBoxLayout->addStretch(1);
brlenGrafenBox->setLayout(brlenGrafenBoxLayout);
brlenLayout->addWidget(brlenGrafenBox);
//To clock tree:
QPushButton* brlenToClockTree = new QPushButton(tr("Convert to clock"));
connect(brlenToClockTree, SIGNAL(clicked(bool)), this, SLOT(convertToClockTree()));
brlenLayout->addWidget(brlenToClockTree);
//Midpoint rooting:
brlenMidpointRootingCriteria_ = new QComboBox;
brlenMidpointRootingCriteria_->addItem("Sum of squares");
brlenMidpointRootingCriteria_->addItem("Variance");
brlenMidpointRootingCriteria_->setEditable(false);
QPushButton* brlenMidpointRootingGo = new QPushButton(tr("Go!"));
connect(brlenMidpointRootingGo, SIGNAL(clicked(bool)), this, SLOT(midpointRooting()));
QGroupBox* brlenMidpointRootingBox = new QGroupBox(tr("Midpoint rooting"));
QHBoxLayout* brlenMidpointRootingLayout = new QHBoxLayout;
brlenMidpointRootingLayout->addWidget(brlenMidpointRootingCriteria_);
brlenMidpointRootingLayout->addWidget(brlenMidpointRootingGo);
brlenMidpointRootingLayout->addStretch(1);
brlenMidpointRootingBox->setLayout(brlenMidpointRootingLayout);
brlenLayout->addWidget(brlenMidpointRootingBox);
//Unresolved uncertain trees:
bootstrapThreshold_ = new QDoubleSpinBox;
bootstrapThreshold_->setValue(60);
bootstrapThreshold_->setDecimals(2);
bootstrapThreshold_->setSingleStep(0.1);
QPushButton* unresolveUncertainNodesGo = new QPushButton(tr("Go!"));
connect(unresolveUncertainNodesGo, SIGNAL(clicked(bool)), this, SLOT(unresolveUncertainNodes()));
QGroupBox* unresolveUncertainNodesBox = new QGroupBox(tr("Unresolve uncertain nodes"));
QHBoxLayout* unresolveUncertainNodesLayout = new QHBoxLayout;
unresolveUncertainNodesLayout->addWidget(bootstrapThreshold_);
unresolveUncertainNodesLayout->addWidget(unresolveUncertainNodesGo);
unresolveUncertainNodesLayout->addStretch(1);
unresolveUncertainNodesBox->setLayout(unresolveUncertainNodesLayout);
brlenLayout->addWidget(unresolveUncertainNodesBox);
////
brlenLayout->addStretch(1);
brlenPanel_->setLayout(brlenLayout);
}
void PhyView::createMouseControlPanel_()
{
mouseControlPanel_ = new QWidget;
QStringList mouseActions;
mouseActions.append(tr("None"));
mouseActions.append(tr("Swap"));
mouseActions.append(tr("Order down"));
mouseActions.append(tr("Order up"));
mouseActions.append(tr("Root on node"));
mouseActions.append(tr("Root on branch"));
mouseActions.append(tr("Sample subtree"));
mouseActions.append(tr("Collapse"));
mouseActions.append(tr("Delete subtree"));
mouseActions.append(tr("Copy subtree"));
mouseActions.append(tr("Cut subtree"));
mouseActions.append(tr("Insert on node"));
mouseActions.append(tr("Insert on branch"));
leftButton_ = new QComboBox;
leftButton_->addItems(mouseActions);
middleButton_ = new QComboBox;
middleButton_->addItems(mouseActions);
rightButton_ = new QComboBox;
rightButton_->addItems(mouseActions);
QFormLayout* formLayout = new QFormLayout;
formLayout->addRow(tr("Left:"), leftButton_);
formLayout->addRow(tr("Middle:"), middleButton_);
formLayout->addRow(tr("Right:"), rightButton_);
mouseControlPanel_->setLayout(formLayout);
}
void PhyView::createDataPanel_()
{
dataPanel_ = new QWidget;
QVBoxLayout* dataLayout = new QVBoxLayout;
loadData_ = new QPushButton(tr("Load Data"));
connect(loadData_, SIGNAL(clicked(bool)), this, SLOT(attachData()));
dataLayout->addWidget(loadData_);
saveData_ = new QPushButton(tr("Save Data"));
connect(saveData_, SIGNAL(clicked(bool)), this, SLOT(saveData()));
dataLayout->addWidget(saveData_);
addData_ = new QPushButton(tr("Add Data"));
connect(addData_, SIGNAL(clicked(bool)), this, SLOT(addData()));
dataLayout->addWidget(addData_);
removeData_ = new QPushButton(tr("Remove Data"));
connect(removeData_, SIGNAL(clicked(bool)), this, SLOT(removeData()));
dataLayout->addWidget(removeData_);
renameData_ = new QPushButton(tr("Rename Data"));
connect(renameData_, SIGNAL(clicked(bool)), this, SLOT(renameData()));
dataLayout->addWidget(renameData_);
translateNames_ = new QPushButton(tr("Translate"));
connect(translateNames_, SIGNAL(clicked(bool)), this, SLOT(translateNames()));
dataLayout->addWidget(translateNames_);
duplicateDownSelection_ = new QPushButton(tr("Duplicate down"));
connect(duplicateDownSelection_, SIGNAL(clicked(bool)), this, SLOT(duplicateDownSelection()));
dataLayout->addWidget(duplicateDownSelection_);
snapData_ = new QPushButton(tr("Snap shot"));
connect(snapData_, SIGNAL(clicked(bool)), this, SLOT(snapData()));
dataLayout->addWidget(snapData_);
dataPanel_->setLayout(dataLayout);
}
void PhyView::createSearchPanel_()
{
searchPanel_ = new QWidget;
QVBoxLayout* searchLayout = new QVBoxLayout;
searchText_ = new QLineEdit();
connect(searchText_, SIGNAL(returnPressed()), this, SLOT(searchText()));
searchLayout->addWidget(searchText_);
searchResults_ = new QListWidget();
searchResults_->setSelectionMode(QAbstractItemView::SingleSelection);
connect(searchResults_, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(searchResultSelected()));
searchLayout->addWidget(searchResults_);
searchPanel_->setLayout(searchLayout);
}
void PhyView::createActions_()
{
openAction_ = new QAction(tr("&Open"), this);
openAction_->setShortcut(tr("Ctrl+O"));
openAction_->setStatusTip(tr("Open a new tree file"));
connect(openAction_, SIGNAL(triggered()), this, SLOT(openTree()));
saveAction_ = new QAction(tr("&Save"), this);
saveAction_->setShortcut(tr("Ctrl+S"));
saveAction_->setStatusTip(tr("Save the current tree to file"));
saveAction_->setDisabled(true);
connect(saveAction_, SIGNAL(triggered()), this, SLOT(saveTree()));
saveAsAction_ = new QAction(tr("Save &as"), this);
saveAsAction_->setShortcut(tr("Ctrl+Shift+S"));
saveAsAction_->setStatusTip(tr("Save the current tree to a file"));
saveAsAction_->setDisabled(true);
connect(saveAsAction_, SIGNAL(triggered()), this, SLOT(saveTreeAs()));
closeAction_ = new QAction(tr("&Close"), this);
closeAction_->setShortcut(tr("Ctrl+W"));
closeAction_->setStatusTip(tr("Close the current tree plot."));
closeAction_->setDisabled(true);
connect(closeAction_, SIGNAL(triggered()), this, SLOT(closeTree()));
exportAction_ = new QAction(tr("Export as &Image"), this);
exportAction_->setShortcut(tr("Ctrl+I"));
exportAction_->setStatusTip(tr("Print the current tree plot."));
exportAction_->setDisabled(true);
connect(exportAction_, SIGNAL(triggered()), this, SLOT(exportTree()));
printAction_ = new QAction(tr("&Print"), this);
printAction_->setShortcut(tr("Ctrl+P"));
printAction_->setStatusTip(tr("Print the current tree plot."));
printAction_->setDisabled(true);
connect(printAction_, SIGNAL(triggered()), this, SLOT(printTree()));
exitAction_ = new QAction(tr("&Quit"), this);
exitAction_->setShortcut(tr("Ctrl+Q"));
exitAction_->setStatusTip(tr("Quit PhyView"));
connect(exitAction_, SIGNAL(triggered()), this, SLOT(exit()));
cascadeWinAction_ = new QAction(tr("&Cascade windows"), this);
connect(cascadeWinAction_, SIGNAL(triggered()), mdiArea_, SLOT(cascadeSubWindows()));
tileWinAction_ = new QAction(tr("&Tile windows"), this);
connect(tileWinAction_, SIGNAL(triggered()), mdiArea_, SLOT(tileSubWindows()));
aboutAction_ = new QAction(tr("About"), this);
connect(aboutAction_, SIGNAL(triggered()), this, SLOT(about()));
aboutBppAction_ = new QAction(tr("About Bio++"), this);
connect(aboutBppAction_, SIGNAL(triggered()), this, SLOT(aboutBpp()));
aboutQtAction_ = new QAction(tr("About Qt"), this);
connect(aboutQtAction_, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
undoAction_ = manager_.createUndoAction(this);
redoAction_ = manager_.createRedoAction(this);
undoAction_->setShortcut(QKeySequence("Ctrl+Z"));
redoAction_->setShortcut(QKeySequence("Shift+Ctrl+Z"));
}
void PhyView::createMenus_()
{
fileMenu_ = menuBar()->addMenu(tr("&File"));
fileMenu_->addAction(openAction_);
fileMenu_->addAction(saveAction_);
fileMenu_->addAction(saveAsAction_);
fileMenu_->addAction(closeAction_);
fileMenu_->addAction(exportAction_);
fileMenu_->addAction(printAction_);
fileMenu_->addAction(exitAction_);
editMenu_ = menuBar()->addMenu(tr("&Edit"));
editMenu_->addAction(undoAction_);
editMenu_->addAction(redoAction_);
viewMenu_ = menuBar()->addMenu(tr("&View"));
viewMenu_->addAction(statsDockWidget_->toggleViewAction());
viewMenu_->addAction(displayDockWidget_->toggleViewAction());
viewMenu_->addAction(brlenDockWidget_->toggleViewAction());
viewMenu_->addAction(undoDockWidget_->toggleViewAction());
viewMenu_->addAction(mouseControlDockWidget_->toggleViewAction());
viewMenu_->addAction(dataDockWidget_->toggleViewAction());
viewMenu_->addAction(searchDockWidget_->toggleViewAction());
viewMenu_->addAction(cascadeWinAction_);
viewMenu_->addAction(tileWinAction_);
helpMenu_ = menuBar()->addMenu(tr("&Help"));
helpMenu_->addAction(aboutAction_);
helpMenu_->addAction(aboutBppAction_);
helpMenu_->addAction(aboutQtAction_);
}
void PhyView::createStatusBar_()
{
updateStatusBar();
}
void PhyView::closeEvent(QCloseEvent* event)
{
}
TreeDocument* PhyView::createNewDocument(Tree* tree)
{
TreeDocument* doc = new TreeDocument();
doc->setTree(*tree);
manager_.addStack(&doc->getUndoStack());
TreeSubWindow *subWindow = new TreeSubWindow(this, doc, treeControlers_->getSelectedTreeDrawing());
mdiArea_->addSubWindow(subWindow);
treeControlers_->applyOptions(&subWindow->getTreeCanvas());
subWindow->show();
setCurrentSubWindow(subWindow);
updateTreesTable();
return doc;
}
QList<TreeDocument*> PhyView::getNonActiveDocuments()
{
QList<TreeDocument*> documents;
QList<QMdiSubWindow *> lst = mdiArea_->subWindowList();
for (int i = 0; i < lst.size(); ++i) {
if (lst[i] != mdiArea_->currentSubWindow())
documents.push_back(dynamic_cast<TreeSubWindow*>(lst[i])->getDocument());
}
return documents;
}
QList<TreeDocument*> PhyView::getDocuments()
{
QList<TreeDocument*> documents;
QList<QMdiSubWindow *> lst = mdiArea_->subWindowList();
for (int i = 0; i < lst.size(); ++i) {
documents.push_back(dynamic_cast<TreeSubWindow*>(lst[i])->getDocument());
}
return documents;
}
void PhyView::readTree(const QString& path, const string& format)
{
unique_ptr<ITree> treeReader(ioTreeFactory_.createReader(format));
try {
unique_ptr<Tree> tree(treeReader->read(path.toStdString()));
TreeDocument* doc = createNewDocument(tree.get());
doc->setFile(path.toStdString(), format);
saveAction_->setEnabled(true);
saveAsAction_->setEnabled(true);
closeAction_->setEnabled(true);
exportAction_->setEnabled(true);
printAction_->setEnabled(true);
//We need to remove and add action again for menu to be updated :s
fileMenu_->removeAction(saveAction_);
fileMenu_->removeAction(saveAsAction_);
fileMenu_->removeAction(closeAction_);
fileMenu_->removeAction(exportAction_);
fileMenu_->removeAction(printAction_);
fileMenu_->insertAction(exitAction_, saveAction_);
fileMenu_->insertAction(exitAction_, saveAsAction_);
fileMenu_->insertAction(exitAction_, closeAction_);
fileMenu_->insertAction(exitAction_, exportAction_);
fileMenu_->insertAction(exitAction_, printAction_);
updateTreesTable();
} catch (Exception& e) {
QMessageBox::critical(this, tr("Ouch..."), tr("Error when reading file:\n") + tr(e.what()));
}
}
void PhyView::openTree()
{
treeFileDialog_->setAcceptMode(QFileDialog::AcceptOpen);
if (treeFileDialog_->exec() == QDialog::Accepted) {
QStringList path = treeFileDialog_->selectedFiles();
string format = IOTreeFactory::NEWICK_FORMAT;
if (treeFileDialog_->selectedNameFilter() == treeFileFilters_[1])
format = IOTreeFactory::NEXUS_FORMAT;
else if (treeFileDialog_->selectedNameFilter() == treeFileFilters_[2])
format = IOTreeFactory::NHX_FORMAT;
readTree(path[0], format);
}
}
void PhyView::setCurrentSubWindow(TreeSubWindow* tsw)
{
clearSearchResults();
if (tsw)
{
statsBox_->updateTree(tsw->getTree());
treeControlers_->setTreeCanvas(&tsw->getTreeCanvas());
treeControlers_->actualizeOptions();
manager_.setActiveStack(&tsw->getDocument()->getUndoStack());
}
//Update selection in tree table:
updateTreesTable(); //We need this here as some windows may have been closed.
QList<QMdiSubWindow *> lst = mdiArea_->subWindowList();
for (int i = 0; i < lst.size(); ++i) {
if (lst[i] == mdiArea_->activeSubWindow()) {
treesTable_->setRangeSelected(QTableWidgetSelectionRange(i, 0, i, 1), true);
} else {
treesTable_->setRangeSelected(QTableWidgetSelectionRange(i, 0, i, 1), false);
}
}
}
bool PhyView::saveTree()
{
TreeDocument* doc = getActiveDocument();
if (doc->getFilePath() == "")
return saveTreeAs();
string format = doc->getFileFormat();
unique_ptr<OTree> treeWriter(ioTreeFactory_.createWriter(format));
Nhx* nhx = dynamic_cast<Nhx*>(treeWriter.get());
if (nhx) {
TreeTemplate<Node> treeCopy(*doc->getTree());
nhx->changeNamesToTags(*treeCopy.getRootNode());
treeWriter->write(treeCopy, doc->getFilePath(), true);
} else {
treeWriter->write(*doc->getTree(), doc->getFilePath(), true);
}
return true;
}
bool PhyView::saveTreeAs()
{
treeFileDialog_->setAcceptMode(QFileDialog::AcceptSave);
if (treeFileDialog_->exec() == QDialog::Accepted) {
QStringList path = treeFileDialog_->selectedFiles();
TreeDocument* doc = getActiveDocument();
string format = IOTreeFactory::NEWICK_FORMAT;
if (treeFileDialog_->selectedNameFilter() == treeFileFilters_[1])
format = IOTreeFactory::NEXUS_FORMAT;
else if (treeFileDialog_->selectedNameFilter() == treeFileFilters_[2])
format = IOTreeFactory::NHX_FORMAT;
doc->setFile(path[0].toStdString(), format);
return saveTree();
}
return false;
}
void PhyView::exportTree()
{
if (imageExportDialog_->exec() == QDialog::Accepted) {
imageExportDialog_->process(getActiveSubWindow()->getTreeCanvas().scene());
}
}
void PhyView::printTree()
{
if (printDialog_->exec() == QDialog::Accepted) {
QPainter painter(printer_);
getActiveSubWindow()->getTreeCanvas().scene()->render(&painter);
painter.end();
}
}
void PhyView::closeTree()
{
if (mdiArea_->currentSubWindow())
mdiArea_->currentSubWindow()->close();
if (mdiArea_->subWindowList().size() == 0) {
saveAction_->setDisabled(true);
saveAsAction_->setDisabled(true);
closeAction_->setDisabled(true);
exportAction_->setDisabled(true);
saveAction_->setDisabled(true);
}
updateTreesTable();
}
void PhyView::updateTreesTable()
{
//Update tree list:
treesTable_->clearSelection();
treesTable_->clearContents();
QList<QMdiSubWindow *> lst = mdiArea_->subWindowList();
treesTable_->setRowCount(lst.size());
for (int i = 0; i < lst.size(); ++i) {
TreeDocument* doc = dynamic_cast<TreeSubWindow*>(lst[i])->getDocument();
string docName = doc->getName();
if (docName == "")
docName = "Tree#" + TextTools::toString(i + 1);
treesTable_->setItem(i, 0, new QTableWidgetItem(QtTools::toQt(docName)));
treesTable_->setItem(i, 1, new QTableWidgetItem(QtTools::toQt(TextTools::toString<unsigned int>(doc->getTree()->getNumberOfLeaves()))));
}
}
void PhyView::exit()
{
close();
}
void PhyView::aboutBpp()
{
QMessageBox msgBox;
msgBox.setText("Bio++ 2.4.1.");
msgBox.setInformativeText("bpp-core 2.4.1\nbpp-seq 2.4.1.\nbpp-phyl 2.4.1.\nbpp-qt 2.4.1");
msgBox.exec();
}
void PhyView::about()
{
QMessageBox msgBox;
msgBox.setText("This is Bio++ Phylogenetic Viewer version 0.6.1.");
msgBox.setInformativeText("Julien Dutheil <dutheil@evolbio.mpg.de>.");
msgBox.exec();
}
void PhyView::updateStatusBar()
{
}
void PhyView::setLengths()
{
if (hasActiveDocument())
submitCommand(new SetLengthCommand(getActiveDocument(), brlenSetLengths_->value()));
}
void PhyView::initLengthsGrafen()
{
if (hasActiveDocument())
submitCommand(new InitGrafenCommand(getActiveDocument()));
}
void PhyView::computeLengthsGrafen()
{
if (hasActiveDocument())
submitCommand(new ComputeGrafenCommand(getActiveDocument(), brlenComputeGrafen_->value()));
}
void PhyView::convertToClockTree()
{
if (hasActiveDocument())
submitCommand(new ConvertToClockTreeCommand(getActiveDocument()));
}
void PhyView::midpointRooting()
{
if (hasActiveDocument())
try {
submitCommand(new MidpointRootingCommand(getActiveDocument(), brlenMidpointRootingCriteria_->currentText().toStdString()));
} catch (NodeException& ex) {
QMessageBox::critical(this, tr("Oups..."), tr("Some branch do not have lengths."));
}
}
void PhyView::deleteAllLengths()
{
if (hasActiveDocument())
submitCommand(new DeleteLengthCommand(getActiveDocument()));
}
void PhyView::unresolveUncertainNodes()
{
if (hasActiveDocument()) {
try {
submitCommand(new UnresolveUnsupportedNodesCommand(getActiveDocument(), bootstrapThreshold_->value()));
} catch (NodeException& ex) {
QMessageBox::critical(this, tr("Oups..."), tr("An exception occurred while unresolving your tree!"));
}
}
}
void PhyView::translateNames()
{
if (hasActiveDocument())
{
translateNameChooser_->translateTree(*getActiveDocument()->getTree());
}
}
void PhyView::controlerTakesAction()
{
QList<QMdiSubWindow *> lst = mdiArea_->subWindowList();
for (int i = 0; i < lst.size(); ++i) {
dynamic_cast<TreeSubWindow*>(lst[i])->getTreeCanvas().redraw();
}
}
void PhyView::attachData()
{
dataFileDialog_->setAcceptMode(QFileDialog::AcceptOpen);
if (dataFileDialog_->exec() == QDialog::Accepted) {
QStringList path = dataFileDialog_->selectedFiles();
string sep = ",";
if (dataFileDialog_->selectedNameFilter() == dataFileFilters_[1])
sep = "\t";
ifstream file(path[0].toStdString().c_str(), ios::in);
DataTable* table = DataTable::read(file, sep);
dataLoader_->load(table);
}
}
void PhyView::saveData()
{
if (hasActiveDocument())
{
dataFileDialog_->setAcceptMode(QFileDialog::AcceptSave);
if (dataFileDialog_->exec() == QDialog::Accepted) {
QStringList path = dataFileDialog_->selectedFiles();
string sep = ",";
if (dataFileDialog_->selectedNameFilter() == dataFileFilters_[1])
sep = "\t";
getActiveSubWindow()->writeTableToFile(path[0].toStdString(), sep);
}
}
}
void PhyView::addData()
{
if (hasActiveDocument())
{
bool ok;
QString name = QInputDialog::getText(this, tr("Set property name"), tr("Property name"), QLineEdit::Normal, tr("New property"), &ok);
if (ok)
submitCommand(new AddDataCommand(getActiveDocument(), name));
}
}
void PhyView::removeData()
{
if (hasActiveDocument())
{
vector<string> tmp;
TreeTemplateTools::getNodePropertyNames(*getActiveDocument()->getTree()->getRootNode(), tmp);
if (tmp.size() == 0) {
QMessageBox::information(this, tr("Warning"), tr("No removable data is attached to this tree."), QMessageBox::Cancel);
return;
}
QStringList properties;
for (size_t i = 0; i < tmp.size(); ++i) {
properties.append(QtTools::toQt(tmp[i]));
}
bool ok;
QString name = QInputDialog::getItem(this, tr("Get property name"), tr("Property name"), properties, 0, false, &ok);
if (ok)
submitCommand(new RemoveDataCommand(getActiveDocument(), name));
}
}
void PhyView::renameData()
{
if (hasActiveDocument())
{
vector<string> tmp;
TreeTemplateTools::getNodePropertyNames(*getActiveDocument()->getTree()->getRootNode(), tmp);
if (tmp.size() == 0) {
QMessageBox::information(this, tr("Warning"), tr("No data which can be renaded is attached to this tree."), QMessageBox::Cancel);
return;
}
QStringList properties;
for (size_t i = 0; i < tmp.size(); ++i) {
properties.append(QtTools::toQt(tmp[i]));
}
bool ok;
QString fromName = QInputDialog::getItem(this, tr("Get property name"), tr("Property name"), properties, 0, false, &ok);
if (ok) {
QString toName = QInputDialog::getText(this, tr("Set property name"), tr("Property name"), QLineEdit::Normal, tr("New property"), &ok);
if (ok) {
submitCommand(new RenameDataCommand(getActiveDocument(), fromName, toName));
}
}
}
}
void PhyView::duplicateDownSelection()
{
if (hasActiveDocument())
{
getActiveSubWindow()->duplicateDownSelection(1);
}
}
void PhyView::snapData()
{
if (hasActiveDocument())
{
submitCommand(new SnapCommand(getActiveDocument()));
}
}
void PhyView::searchText()
{
if (!getActiveSubWindow())
return;
getActiveSubWindow()->getTreeCanvas().redraw();
clearSearchResults();
QList<QGraphicsTextItem*> results = getActiveSubWindow()->getTreeCanvas().searchText(searchText_->text());
for (int i = 0; i < results.size(); ++i) {
searchResults_->addItem(results[i]->toPlainText());
searchResultsItems_.append(results[i]);
results[i]->setDefaultTextColor(Qt::red);
}
}
void PhyView::searchResultSelected()
{
getActiveSubWindow()->getTreeCanvas().ensureVisible(searchResultsItems_[searchResults_->currentRow()]);
}
void PhyView::activateSelectedDocument() {
if (treesTable_->selectedItems().size() > 0) {
int index = treesTable_->selectedItems()[0]->row();
mdiArea_->setActiveSubWindow(mdiArea_->subWindowList()[index]);
mdiArea_->activeSubWindow()->showNormal();
mdiArea_->activeSubWindow()->raise();
}
}
TreeTemplate<Node>* PhyView::pickTree()
{
QList<TreeDocument*> documents = getDocuments();
//treeList_->clear();
QStringList items;
for (int i = 0; i < documents.size(); ++i) {
QString text = QtTools::toQt(documents[i]->getName());
if (text == "") text = "(unknown)";
vector<string> leaves = documents[i]->getTree()->getLeavesNames();
text += QtTools::toQt(" " + TextTools::toString(leaves.size()) + " leaves ");
for (unsigned int j = 0; j < min(static_cast<unsigned int>(leaves.size()), 5u); ++j) {
text += QtTools::toQt(", " + leaves[j]);
}
if (leaves.size() >= 5) text += "...";
//treeList_->addItem(text);
items << text;
}
//treeChooser_->exec();
//int index = treeList_->currentRow();
//return index > 0 ? documents[index]->getTree() : 0;
bool ok;
QString item = QInputDialog::getItem(this, "Pick a tree", "Tree to insert:", items, 0, false, &ok);
if (ok && !item.isEmpty())
return documents[items.indexOf(item)]->getTree();
else
return 0;
}
// This class is necessary to reimplement the notify method, in order to catch any foreign exception.
class PhyViewApplication:
public QApplication
{
public:
PhyViewApplication(int &argc, char *argv[]):
QApplication(argc, argv) {}
public:
bool notify(QObject *receiver_, QEvent *event_)
{
try
{
return QApplication::notify(receiver_, event_);
}
catch (std::exception &ex)
{
std::cerr << "std::exception was caught" << std::endl;
std::cerr << ex.what() << endl;
QMessageBox msgBox;
msgBox.setText(ex.what());
msgBox.exec();
}
return false;
}
};
int main(int argc, char *argv[])
{
PhyViewApplication app(argc, argv);
PhyView* phyview = new PhyView();
phyview->show();
//Parse command line arguments:
QStringList args = app.arguments();
string format = IOTreeFactory::NEWICK_FORMAT;
//QTextCodec* codec = QTextCodec::codecForLocale(); Not supported in Qt5...
for (int i = 1; i < args.size(); ++i) {
if (args[i] == "--nhx") {
format = IOTreeFactory::NHX_FORMAT;
} else if (args[i] == "--nexus") {
format = IOTreeFactory::NEWICK_FORMAT;
} else if (args[i] == "--newick") {
format = IOTreeFactory::NEWICK_FORMAT;
//} else if (args[i] == "--enc") {
// if (i == args.size() - 1) {
// cerr << "You must specify a text encoding after --enc tag." << endl;
// exit(1);
// }
// ++i;
// codec = QTextCodec::codecForName(args[i].toStdString().c_str());
} else {
phyview->readTree(args[i], format);
}
}
return app.exec();
}
|