Qt地铁智慧换乘系统浅学(四 )实现添加线路,添加站点,添加边 并且存储到本地txt文件

添加前的构思

假设 现要添加一个线路 : 9号线

如果你将它存在初始化的txt文件中,类似以下的形式
在这里插入图片描述
这个时候 站点都是未知的,每次插入站点维护都很麻烦!

于是我们新建一个txt文件 用来存储新添加的站点,并且添加之后维护到程序运行的存储结构中
文本内容如下

线路名称 线路颜色

在维护的时候 之间

LineColor[线路名称] = QColor( 线路颜色) 即可

注意 我们需要判断线路是否存在,以及颜色是否存在 进行特判

假如 我们要添加站点呢?
我们添加的信息是

站点名称 站点所属线路 东经 北纬

同样的 如果将这个信息存储到初始化里面,我们并不知道他所在的顺序,因为初始化文件里都是顺序存储,建边需要!
所以 我们需要单独存储到addstation.txt 文件里,格式如上
我们在初始化完成之后 ,再额外初始化咱们添加的这些站点即可 ,将他们画在地图上

添加边的话 ,我们输入的信息是 起始站,终点站,所属线路
同样的 也不好添加到初始化文件中

我们也是新建一个文本文件 addedge.txt
格式如输入的那样

然后初始化之后 我们额外初始化咱们的边即可

界面设计

tabWidget

添加线路界面

在这里插入图片描述

添加站点界面

在这里插入图片描述

添加边界面

在这里插入图片描述

代码实现

添加线路

思路

输入线路名称,和选择颜色。
判断线路是否存在,
判断颜色是否存在
合理 更新容器和txt

连接槽函数

connect(ui->pushButtonAddLine,&QPushButton::clicked,this,[=]{addLine();}); //添加线路的槽
connect(ui->pushbuttongetcolor,&QPushButton::clicked,this,[=]{getcolor();}); //用来选择线路颜色

槽函数

void addMenu::getcolor(){
    this->color=QColorDialog::getColor(Qt::white,this);
    ui->pushbuttongetcolor->setStyleSheet(QString("background-color:rgb(%1,%2,%3)").arg(color.red()).arg(color.green()).arg(color.blue()));
    qDebug()<<this->color;
    this->flag = 1;
}

void addMenu::addLine(){
    ui->textBrowser->clear();
    QString name = ui->lineEdit->text();
    if(name.size()==0){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请输入线路名称.",QMessageBox::Close);
        MyBox.exec();
        this->flag=0;
        return ;
    }

    if(!this->flag){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请选择颜色.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return ;
    }

    QMap<QString, QColor>::iterator it =LineColor.find(name);
//    for(auto i:LineColor.keys()){
//        qDebug()<<i<<"\n";
//    }
    if (it != LineColor.end()) {
        qDebug()<<name<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","请不要输入相同线路.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }

    while(!this->flag){};

    bool exists = false;
    for (const auto pair : LineColor.values()) {
        if (pair == this->color) {
            exists = true;
            break;
        }
    }
    if(exists){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请不要选择已经存在的颜色.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }

    QFile linefie("../testgitee/test/addline.txt");
    /*  addline.txt
    linename linecolor
*/
    linefie.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
    if(!linefie.isOpen()){
        qDebug()<<"error open"<<linefie.errorString();
        this->flag = 0;
        return ;
    }
    QTextStream stream(&linefie);
    stream.setCodec("UTF-8");
    if (linefie.size() > 0) {
        linefie.seek(linefie.size() - 1); // 移动到倒数第二个字符,忽略换行符
        while (!stream.atEnd()) {
            QString line = stream.readLine();
        }
    }
    stream<<name<<" "<<this->color.name()<<endl;
    linefie.close();
    LineColor[name] = this->color;
    initcombox1();
    ui->textBrowser->append("线路"+name+"插入成功"+" 颜色"+this->color.name());
}

添加站点

思路

我们输入站点的名称,站点所属线路,以及站点的经纬度

判断站点是否存在,判断线路是否存在
合理 添加到txt文件 并且更新容器

连接槽函数

connect(ui->pushButtonaddsta,&QPushButton::clicked,this,[=]{addstation();});

初始化combox

这里放置了添加tabwidget的所有combox

void addMenu::initcombox1(){
   void addMenu::initcombox1(){
    ui->comboBox->clear();
    QStringList Line_list,Line_list1;
    for(auto &i:LineColor.keys()){
        Line_list.append(i);
        Line_list1.append(i);
    }
    std::sort(Line_list.begin(),Line_list.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    std::sort(Line_list1.begin(),Line_list1.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    ui->comboBox->addItems(Line_list);  //添加站点中 选择线路
    QLineEdit *line1 = new QLineEdit;
    ui->comboBox->setLineEdit(line1);
    ui->comboBox->lineEdit()->clear();

//qDebug()<<"1错误";
    ui->comboBox_4->clear();
    ui->comboBox_4->addItems(Line_list1);  //添加边时 所属线路
    QLineEdit *line2 = new QLineEdit;
    ui->comboBox_4->setLineEdit(line2);
    ui->comboBox_4->lineEdit()->clear();

//qDebug()<<"2错了";
    QStringList sta_name,sta_name1;
    for(auto &i:Station.keys()){
        sta_name.append(i);
        sta_name1.append(i);
    }
    std::sort(sta_name.begin(),sta_name.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    std::sort(sta_name1.begin(),sta_name1.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    ui->comboBox_2->clear();    // 添加边时 起始站点
    ui->comboBox_2->addItems(sta_name);
    QLineEdit *line3 = new QLineEdit;
    ui->comboBox_2->setLineEdit(line3);
    ui->comboBox_2->lineEdit()->clear();
//qDebug()<<"3错了";
    ui->comboBox_3->clear();
    ui->comboBox_3->addItems(sta_name1);  //添加边时  终点站
    QLineEdit *line4 = new QLineEdit;
    ui->comboBox_3->setLineEdit(line4);
    ui->comboBox_3->lineEdit()->clear();

}

槽函数

void addMenu::addstation(){
    QString staname = ui->lineEdit_2->text();
    if(staname.size()==0){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请输入站点.",QMessageBox::Close);
        MyBox.exec();
        this->flag=0;
        return ;
    }

    QMap<QString, node>::iterator its =Station.find(staname);
    //    for(auto i:LineColor.keys()){
    //        qDebug()<<i<<"\n";
    //    }
    if (its != Station.end()) {
        qDebug()<<"站点已经存在: "<<staname<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","站点已经存在.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }


    QString linename = ui->comboBox->lineEdit()->text();  //判断线路是否存在
    QMap<QString, QColor>::iterator it =LineColor.find(linename);
    //    for(auto i:LineColor.keys()){
    //        qDebug()<<i<<"\n";
    //    }
    if (it == LineColor.end()) {
        qDebug()<<"不存在线路: "<<linename<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","请选择存在的线路.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }

    bool exits = false;
    for(auto i:Station_Line[staname]){
        if(i==linename){
            exits = true;
        }
    }
    if(exits){
        qDebug()<<"该线路已经存在站点:"<<linename<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","该线路已经存在站点.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }
    QFile linefie("../testgitee/test/addstation.txt");
    /*  addstation.txt
    station   line   E  N
*/
    linefie.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
    if(!linefie.isOpen()){
        qDebug()<<"error open"<<linefie.errorString();
        this->flag = 0;
        return ;
    }
    QTextStream stream(&linefie);
    stream.setCodec("UTF-8");
    if (linefie.size() > 0) {
        linefie.seek(linefie.size() - 1); // 移动到倒数第二个字符,忽略换行符
        while (!stream.atEnd()) {
            QString line = stream.readLine();
        }
    }
    stream<<staname<<" "<<linename<<" "<<ui->doubleSpinBox->text()<<" "<<ui->doubleSpinBox_2->text()<<endl;
    linefie.close();

    ui->textBrowser_2->append("站点"+staname+"添加成功"+" 线路"+linename);
    infoaddstation();

}

更新容器函数


void infoaddstation(){
    QFile linefie("../testgitee/test/addstation.txt");
/*
 *  addstation.txt
 *  stationname linename e n
*/
    linefie.open(QIODevice::ReadOnly);
    if(!linefie.isOpen()){
        qDebug()<<"error open";
        return ;
    }
    QString stationname;
    QString linename;
    QTextStream linein(&linefie);
    linein.setCodec("UTF-8");
    node stal;
    while(true){
       linein>>stal.name>>linename>>stal.n>>stal.e;
        if(linein.atEnd()) break;
        updateBound(stal.e,stal.n);
        qDebug()<<stal.name<<stal.n<<stal.e<<"\n";
        qDebug()<<maxLati<<" "<<maxLongi<<" "<<minLati<<" "<<minLongi<<"\n";
        stal.coord.setY((maxLati-stal.e)/(maxLati-minLati)*500+100);
        stal.coord.setX((stal.n-minLongi)/(maxLongi-minLongi)*1000+100);
        Station[stal.name] = stal;
        Station_Line[stal.name].insert(linename);
    }
    linefie.close();
}

添加边

思路

起始站 终点站 所属线
分别判断是否存在

合理则更新txt和容器

槽函数 和代码

void addMenu::addedge(){
    QString sta1,sta2,linename;
    sta1 = ui->comboBox_2->lineEdit()->text();
    sta2 = ui->comboBox_3->lineEdit()->text();
    linename = ui->comboBox_4->lineEdit()->text();


    if(sta1.size()==0 || sta2.size()==0 ||linename.size()==0){
        QMessageBox MyBox(QMessageBox::Warning,"警告","请输入完整信息.",QMessageBox::Close);
        MyBox.exec();
    }
    QMap<QString, node>::iterator its =Station.find(sta1);
    if (its == Station.end()) {
        qDebug()<<"站点1不存在: "<<sta1<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","站点1不存在.",QMessageBox::Close);
        MyBox.exec();
        return;
    }

    its =Station.find(sta2);
    if (its == Station.end()) {
        qDebug()<<"站点2不存在: "<<sta1<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","站点2不存在.",QMessageBox::Close);
        MyBox.exec();
        return;
    }


    QMap<QString, QColor>::iterator it =LineColor.find(linename);
    //    for(auto i:LineColor.keys()){
    //        qDebug()<<i<<"\n";
    //    }
    if (it == LineColor.end()) {
        qDebug()<<"不存在线路: "<<linename<<"\n";
        QMessageBox MyBox(QMessageBox::Warning,"警告","请选择存在的线路.",QMessageBox::Close);
        MyBox.exec();this->flag=0;
        return;
    }

    // 将信息添加到txt文件

    QFile linefie("../testgitee/test/addedge.txt");
    /*  addedge.txt
   station1 station2  linename
*/
    linefie.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
    if(!linefie.isOpen()){
        qDebug()<<"error open"<<linefie.errorString();
        this->flag = 0;
        return ;
    }
    QTextStream stream(&linefie);
    stream.setCodec("UTF-8");
    if (linefie.size() > 0) {
        linefie.seek(linefie.size() - 1); // 移动到倒数第二个字符,忽略换行符
        while (!stream.atEnd()) {
            QString line = stream.readLine();
        }
    }
    stream<<sta1<<" "<<sta2<<" "<<linename<<endl;
    linefie.close();
    infoaddedge();
    QMessageBox MyBox(QMessageBox::Information,"信息","成功添加边:"+sta1+"<---"+linename+"--->"+sta2,QMessageBox::Close);
    MyBox.exec();
}
void infoaddedge(){
    QFile linefie("../testgitee/test/addedge.txt");

    /*
 *  addedge.txt
 *  sta1 sta2 linename
*/
    linefie.open(QIODevice::ReadOnly);
    if(!linefie.isOpen()){
        qDebug()<<"error open";
        return ;
    }
    QString sta1name,sta2name;
    QString Linename;
    QTextStream linein(&linefie);
    linein.setCodec("UTF-8");
    while(true){
        linein>>sta1name>>sta2name>>Linename;
        if(linein.atEnd()) break;
        edge[sta1name].append(sta2name);
        edge[sta2name].append(sta1name);
        dp[sta1name][sta2name] = dp[sta2name][sta1name] = getDistance(sta1name,sta2name);
        mp[sta1name][sta2name].insert(Linename);
        mp[sta2name][sta1name].insert(Linename);
    }
    linefie.close();
}

注意

需要注意的是,在初始化阶段infoaddline ,infoaddstation,infoaddedge也是要执行的,在Qt地铁智慧换乘系统浅学(二 )中没有体现

其实就是在infoinit执行完成后执行这三个函数即可

    //addline
    infoaddline();
    //addstation
    infoaddstation();
    //addedge
    infoaddedge();

在这里插入图片描述
左上角即添加的站点和线路

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛郎恋刘娘,刘娘念牛郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值