//单击“添加旅客”按钮,开始运行
void Widget::addTravelerButtonClicked()
{
    //添加旅客,初始化旅客信息
    qDebug() << "Add new traveler, reset all elements in widget";
    std::vector<bool> temp(12, false);
    throughcity = temp;
    qDebug() << "throughcity creat success.";
    travelers.push_back(Traveler(addtravelertimes-1, getStartTime(), getDeadline(),
                                 getStrategy(), getStart(), getDestination(),
                                 ui->ThroughCityCheckBox->isChecked(), throughcity));
    qDebug() << "travelers.pushback...";
    startclicked.push_back(false);
    addtravelertimes += 1;
    startclickedtimes = 0;

    //将界面右侧各栏初始化显示
    ui->TravelerComboBox->addItem(QString::number(addtravelertimes));
    ui->TravelerComboBox->setCurrentText(QString::number(addtravelertimes));

    ui->StartButton->setText(QString::fromWCharArray(L"开始"));
    ui->TravelerComboBox->setEnabled(true);
    ui->StartComboBox->setEnabled(true);
    ui->StrategyComboBox->setEnabled(true);
    ui->DestinationComboBox->setEnabled(true);
    ui->StrategyComboBox->setCurrentIndex(0);
    ui->StartComboBox->setCurrentIndex(0);
    ui->DestinationComboBox->setCurrentIndex(1);
    ui->ThroughCityCheckBox->setEnabled(true);
    ui->DeadlineDateTimeEdit->setEnabled(false);
    ui->StartDateTimeEdit->setEnabled(true);
    ui->StartButton->setEnabled(true);

    ui->StartDateTimeEdit->setDateTime(QDateTime::currentDateTime());
    int deaDay = QDateTime::currentDateTime().date().day();
    deaDay += 1;
    QDateTime deadlineDateTime;
    deadlineDateTime.setDate(QDate(QDateTime::currentDateTime().date().year(), QDateTime::currentDateTime().date().month(), deaDay));
    deadlineDateTime.setTime(QTime(QDateTime::currentDateTime().time()));
    ui->DeadlineDateTimeEdit->setDateTime(deadlineDateTime);
}
Example #2
0
bool Plan_File::Read (long offset)
{
	//---- check the file status ----

	if (!Check_File ()) return (false);
	if (plan == NULL) return (Status (RECORD_SIZE));
	if (File_Access () != READ) return (Status (ERROR));

	//---- move to a specified location in the file ----

	if (offset >= 0) {
		if (!Offset (offset)) return (false);
	}

	//---- allocate space ----

	if (allocate_memory) {
		if (!Setup_Record ()) return (false);
	}

	//---- read the next plan ----

	if (Record_Format () == BINARY) {
		int num_token;

		if (!Db_File::Read (plan, (sizeof (Plan_Data) - sizeof (int)))) return (false);

		if (time_sort) {
			int temp = plan->key1;
			plan->key1 = plan->key2;
			plan->key2 = temp;
		}
		num_record++;
		num_plan++;

		if (Leg () == 2) {
			num_trip++;
		} else if (Leg () == 1 && Trip () == 1) {
			num_traveler++;
		}
		num_token = Tokens ();
		if (num_token > 0) {
			if (!Check_Size (num_token)) return (false);
			if (!Db_File::Read (&(plan->data [0]), num_token * sizeof (int))) return (Status (PLAN_FIELDS));

			num_record++;
		}
		return (true);
	} else {

		int field, max_field, value;
		char buffer [40], *ptr;

		field = max_field = 0;

		while (Db_File::Read ()) {
			num_record++;

			ptr = Clean_Record ();

			//---- check for a blank record ----

			if (ptr == NULL || *ptr == '\0') continue;

			//---- process the plan record ----

			while (ptr != NULL) {
				ptr = Get_Token (ptr, buffer, sizeof (buffer));

				if (buffer [0] == '\0') break;
				field++;
				value = atol (buffer);

				switch (field) {
					case 1:		//---- traveler id ----
						Traveler (value);
						num_plan++;
						break;
					case 2:		//---- user field ----
						break;
					case 3:		//---- trip id ----
						Trip (value);
						break;
					case 4:		//---- leg id ----
						Leg (value);
						if (value == 2) {
							num_trip++;
						} else if (value == 1 && Trip () == 1) {
							num_traveler++;
						}
						break;
					case 5:		//---- time ----
						Time (value);
						break;
					case 6:		//---- start id ----
						Start_ID (value);
						break;
					case 7:		//---- start type ----
						Start_Type (value);
						break;
					case 8:		//---- end id ----
						End_ID (value);
						break;
					case 9:		//---- end type ----
						End_Type (value);
						break;
					case 10:	//---- duration ----
						Duration (value);
						break;
					case 11:	//---- stop time ----
						Stop_Time (value);
						break;
					case 12:	//---- max time flag ----
						break;
					case 13:	//---- cost ----
						Cost (value);
						break;
					case 14:	//---- gcf ----
						GCF (value);
						break;
					case 15:	//---- driver flag ----
						Driver_Flag (value);
						break;
					case 16:	//---- mode ----
						Mode (value);
						break;
					case 17:	//---- number of tokens ----
						if (value < 0) {
							Status (PLAN_FIELDS);
							return (false);
						}
						Tokens (value);
						max_field = value + 17;

						if (value == 0) return (true);
						if (!Check_Size (value)) return (false);
						break;

					default:	//---- token value ----

						if (field > max_field) {
							Status (PLAN_FIELDS);
							return (false);
						}
						plan->data [field - 18] = value;

						if (field == max_field) return (true);
						break;
				}
			}
		}
		if (field != 0) {
			return (Status (PLAN_FIELDS));
		}
		return (false);
	}
}
Example #3
0
bool Plan_File::Write (Plan_Data *data)
{
	int num_token;
	FILE *file;

	//---- check the file status ----

	if (!Check_File ()) return (false);
	if (File_Access () == READ) return (Status (ERROR));

	Plan_Data *backup = NULL;

	if (data != NULL) {
		backup = plan;
		plan = data;
	} else {
		if (plan == NULL) return (Status (RECORD_SIZE));
	}

	//---- write the plan data ----

	file = File ();
	num_token = Tokens ();

	if (Record_Format () == BINARY) {
		if (time_sort) {
			int size, temp;
			size = sizeof (Plan_Data) - sizeof (int);

			memcpy (backup, plan, size);
			
			temp = backup->key1;
			backup->key1 = backup->key2;
			backup->key2 = temp;

			if (!Db_File::Write (backup, size)) goto reset;
		} else {
			if (!Db_File::Write (plan, (sizeof (Plan_Data) - sizeof (int)))) goto reset;
		}

		num_record++;
		num_plan++;

		if (Leg () == 2) {
			num_trip++;
		} else if (Leg () == 1 && Trip () == 1) {
			num_traveler++;
		}
		if (num_token > 0) {
			if (!Db_File::Write (&(plan->data [0]), num_token * sizeof (int))) goto reset;
			num_record++;
		}

	} else {

		if (fprintf (file, "%d 0 %d %d\n%d %d %d %d %d\n%d %d %d %d %d\n%d %d\n%d\n",
			Traveler (), Trip (), Leg (), 
			Time (), Start_ID (), Start_Type (), End_ID (), End_Type (),
			Duration (), Stop_Time (), 1, Cost (), GCF (), 
			Driver_Flag (), Mode (), 
			num_token) < 0) goto reset;

		num_record += 5;
		num_plan++;

		if (Leg () == 2) {
			num_trip++;
		} else if (Leg () == 1 && Trip () == 1) {
			num_traveler++;
		}

		//---- write tokens ----

		if (num_token > 0) {
			int field;
			int i = 0;

			switch (Mode ()) {
				case AUTO_MODE:		//---- auto ----
					if (Driver_Flag ()) {

						//---- vehicle ID and number of passengers ----

						i = 2;
						if (fprintf (file, "%d %d\n", plan->data [0], plan->data [1]) < 0) goto reset;
						num_record++;
					}
					break;
				case TRANSIT_MODE:		//---- transit ----
					if (Driver_Flag ()) {

						//---- schedule pairs, vehicle ID, and route ID ----

						i = 3;
						if (fprintf (file, "%d %d %d\n", plan->data [0], plan->data [1], plan->data [2]) < 0) goto reset;
						num_record++;
					}
					break;
				default:
					break;
			}

			//---- print the rest of the fields in groups of 10 ----

			for (field=0; i < num_token; i++, field++) {
				if (!field) {
					if (fprintf (file, "%d", plan->data [i]) < 0) goto reset;
					num_record++;
				} else if (!(field % 10)) {
					if (fprintf (file, "\n%d", plan->data [i]) < 0) goto reset;
					num_record++;
				} else {
					if (fprintf (file, " %d", plan->data [i]) < 0) goto reset;
				}
			}
			if (field) {
				if (fprintf (file, "\n") < 0) goto reset;
			}
		}

		//---- add a blank line at the end of the plan ----

		if (fprintf (file, "\n") < 0) goto reset;
		num_record++;

		Flush ();
	}
	if (data != NULL) {
		plan = backup;
	}
	return (true);

reset:
	if (data != NULL) {
		plan = backup;
	}
	return (false);
}
//单击“开始”按钮,获取用户输入信息
void Widget::startButtonClicked()
{
    QDateTime startDateTime;
    //对于当前旅客,初次点击开始按钮
    if (startclicked[ui->TravelerComboBox->currentIndex()] == false)
    {
        qDebug() << "StartButton clicked 1st time for CurrentTraveler";
        strategy = getStrategy();
        start = getStart();
        destination = getDestination();
        //始发地和目的地相同则弹框报错,不作操作
        if (start == destination)
        {
            qDebug() << "Start and Dedtination is the same one, wait for  another command";
            QMessageBox::information(this, "Error", QString::fromWCharArray(L"出发地和目的地相同"));
            return;
        }
        //(策略三的情况下)截止时间早于当前时间报错,不作操作
        if (!(ui->StartDateTimeEdit->dateTime() < ui->DeadlineDateTimeEdit->dateTime()))
        {
            qDebug() << "Deadline ahead of StratTime, wait for another command";
            QMessageBox::information(this, "Error", QString::fromWCharArray(L"截止时间早于当前时间"));
            return;
        }

        startDateTime = getStartTime();

        travelers[ui->TravelerComboBox->currentIndex()] = (Traveler(addtravelertimes-1, startDateTime,
                                                                    getDeadline(), strategy, start, destination, ui->ThroughCityCheckBox->isChecked(), throughcity));
        std::vector<Attribute> path = travelers[ui->TravelerComboBox->currentIndex()].getPlan();
        if (path.size() == 0)
        {
            qDebug() << "No legal path";
            QMessageBox::information(this, "Error", QString::fromWCharArray(L"无有效路径"));
            startclicked[ui->TravelerComboBox->currentIndex()] = false;
            return;
        }

        startclicked[ui->TravelerComboBox->currentIndex()] = true;
        currentTraveler = ui->TravelerComboBox->currentIndex();

        displayTotalTime();
        displayFare(path);
        displayPath(path);

        qDebug() << "StartButton rename as ChangePlan for CurrentTraveler";
        ui->StartButton->setText(QString::fromWCharArray(L"更改"));
        ui->StartComboBox->setEnabled(false);
        ui->StartDateTimeEdit->setEnabled(false);
        startclickedtimes += 1;
        startclicked[ui->TravelerComboBox->currentIndex()] = true;
        return;
    }
    //对于当前旅客,执行更改计划操作
    if (startclicked[ui->TravelerComboBox->currentIndex()] == true)
    {
        qDebug() << "StartButton clicked for CurrentTraveler";
        strategy = getStrategy();
        destination = getDestination();

        if (!(ui->StartDateTimeEdit->dateTime() < ui->DeadlineDateTimeEdit->dateTime()))
        {
            qDebug() << "Deadline ahead of StartTime, reset the DeadlineDateTimeEdit,wait for another command";
            QMessageBox::information(this, "Error", QString::fromWCharArray(L"截止时间早于当前时间"));

            int deaDay = ui->StartDateTimeEdit->dateTime().date().day();
            deaDay += 1;
            QDateTime deadlineDateTime;
            deadlineDateTime.setDate(QDate(ui->StartDateTimeEdit->dateTime().date().year(), ui->StartDateTimeEdit->dateTime().date().month(), deaDay));
            deadlineDateTime.setTime(QTime(ui->StartDateTimeEdit->dateTime().time()));
            ui->DeadlineDateTimeEdit->setDateTime(deadlineDateTime);

            return;
        }

        //获得新计划的始发地,即原计划的当前停留地/运行途中即将到达地
        int nextCity2Arrive = ui->LeftWidget->nextCity();
        if (nextCity2Arrive != -1)
        {
            std::vector<Attribute> path = travelers[ui->TravelerComboBox->currentIndex()].changePlan(nextCity2Arrive, strategy, destination, getDeadline(),
                                                                                                     ui->ThroughCityCheckBox->isChecked(),throughcity);
            if (path.size() == 0)
            {
                qDebug() << "No legal path";
                QMessageBox::information(this, "Error", QString::fromWCharArray(L"无有效路径"));
                return;
            }
            qDebug() << "Change plan success.";
            currentTraveler = ui->TravelerComboBox->currentIndex();
            displayTotalTime();
            displayFare(path);
            displayPath(path);
        }
    }
}