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
| #include "mainwindow.h" #include "ui_mainwindow.h" #include <dlt.h> #include <QPainter> #include <QPen> #include <inputdialog.h> #include <QDebug> #include <QMessageBox> #include <QScrollArea> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this);
tree = DLT();
s = new QScrollArea(this); s->setGeometry(0, 0, 600, 600); w = new QWidget(s); s->setWidget(w); w->setGeometry(0, 0, 3000, 3000); w->installEventFilter(this);
connect(ui->btn1, &QPushButton::clicked, [=](){ inputDialog *dlg = new inputDialog;
connect(dlg, &inputDialog::express, [=](){ string str = dlg->edit.toStdString(); tree.InsertDLTree(tree.T, str); tree.countTotalChildMaxNum(tree.T->Ptr.first); w->update(); });
dlg->setModal(true); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->exec(); });
connect(ui->btn2, &QPushButton::clicked, [=](){ inputDialog *dlg = new inputDialog; connect(dlg, &inputDialog::express, [=](){ string str = dlg->edit.toStdString(); Record *res = NULL; res = tree.SearchDLTree(tree.T, str); if(res == NULL) QMessageBox::information(this, "Result", "Not exist");
}); dlg->setModal(true); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->exec(); });
connect(ui->btn3, &QPushButton::clicked, [=](){ tree.DestroyTree(tree.T->Ptr.first); tree.T->Ptr.first = NULL; w->update(); });
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if(obj == w && event->type() == QEvent::Paint) { QPainter *painter = new QPainter(w); paintTree(painter, MainWindow::tree.T, 0, 0); painter->end(); } return QWidget::eventFilter(obj,event); }
void MainWindow::paintTree(QPainter *painter, DLTree T, int breadth, int depth) { if(T->pathStatus == 1) { T->pathStatus = 0; QPen pen = painter->pen(); painter->setPen(Qt::blue); painter->drawEllipse(QPoint(startX + breadth * interval, startY + depth * interval), radius, radius); painter->setPen(pen); } else painter->drawEllipse(QPoint(startX + breadth * interval, startY + depth * interval), radius, radius); char *s = new char[2]; s[0] = T->symbol;s[1] = '\0'; painter->drawText(QRect(startX - radius + breadth * interval, startY - radius + depth * interval, radius * 2, radius * 2), Qt::AlignCenter, tr(s)); delete[] s; if(T->symbol != '$' && T->Ptr.first) { arrowDown(painter, breadth, depth); paintTree(painter, T->Ptr.first, breadth, depth + 1); } if(T->next) { arrowRight(painter, breadth, depth, T->childMaxNum); paintTree(painter, T->next, breadth + T->childMaxNum, depth); }
}
void MainWindow::arrowDown(QPainter *painter, int breadth, int depth) { painter->drawLine(startX + breadth * interval, startY + radius + depth * interval, startX + breadth * interval, startY - radius + (depth + 1) * interval); painter->drawLine(startX + breadth * interval, startY - radius + (depth + 1) * interval, startX + breadth * interval + 3, startY - radius + (depth + 1) * interval - 8); painter->drawLine(startX + breadth * interval, startY - radius + (depth + 1) * interval, startX + breadth * interval - 3, startY - radius + (depth + 1) * interval - 8); }
void MainWindow::arrowRight(QPainter *painter, int breadth, int depth, int space) { painter->drawLine(startX + radius + breadth * interval, startY + depth * interval, startX - radius + (breadth + space) * interval, startY + depth * interval); painter->drawLine(startX - radius + (breadth + space) * interval, startY + depth * interval, startX - radius + (breadth + space) * interval - 8, startY + depth * interval - 3); painter->drawLine(startX - radius + (breadth + space) * interval, startY + depth * interval, startX - radius + (breadth + space) * interval - 8, startY + depth * interval + 3); }
MainWindow::~MainWindow() { delete ui; }
|