Пример #1
0
void MainWindow::exportCSV()
{
    QString file_name = QFileDialog::getSaveFileName(this,
                                                     tr("Export as CSV"),
                                                     QString("%1.csv").arg(currentDatabaseName()),
                                                     tr("CSV files (*.csv)"));
    if (file_name.isEmpty())
        return;

    QFile file(file_name);
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        QMessageBox::critical(this, tr("Export as CSV"), tr("Cannot write file %1:\n%2.").arg(file_name).arg(file.errorString()));
        this->setEnabled(true); return;
    }

    QTextStream out(&file);
    out.setCodec("UTF-8");

    const QString separator(',');

    QTextDocument doc;
    for (int i = 0; i < LQListWidget->count(); ++i) {
        QuestionItem *item = current_db_questions.value(LQListWidget->item(i));
        out << quoteForCSV(item->name()) << separator;
        out << QString::number(item->category()) << separator;
        out << quoteForCSV(item->group()) << separator;
        doc.setHtml(item->text());
        out << quoteForCSV(doc.toPlainText()) << separator;
        out << quoteForCSV(item->explanation()) << separator;
        out << QString::number(item->correctAnswers());
        foreach (const QString &answer, item->answers())
            out << separator << quoteForCSV(answer);
        out << endl;
    }

    file.close();
}
Пример #2
0
void Client::loadResults(QString input)
{
    QTextStream in(&input);
    QString buffer;
    QuestionItem *item;
    Question::Answer ans;
    c_score = 0;
    c_maxscore = 0;

    do {
        item = NULL;
        if (in.readLine() != "[Q_NAME]")
            return;
        buffer = in.readLine();
        QMapIterator<QListWidgetItem *, QuestionItem *> i(c_parent->current_db_questions);
        while (i.hasNext()) { i.next();
            if (i.value()->name() == buffer) {
                item = i.value();
                break;
            }
        }
        if (item == NULL) {
            in.readLine();
            in.readLine();
            continue;
        }
        if (in.readLine() != "[Q_ANSWERED]")
            return;
        ans = (Question::Answer)in.readLine().toInt();
        QuestionAnswer qans(item->correctAnswer(), ans, item->numAnswers(), item->category(), item->difficulty(), item->selectionType(), item->explanation());
        c_results->insert(item->name(), qans);
        c_score += qans.score(c_parent->current_db_scoringsystem);
        c_maxscore += qans.maximumScore(c_parent->current_db_scoringsystem);
        if (c_score > 0.0) {
            item->addCorrectAns();
        } else {
            item->addIncorrectAns();
        }
    } while (!in.atEnd());

    c_passed = c_parent->current_db_passmark.check(c_results, &c_parent->current_db_questions, c_parent->current_db_scoringsystem);
    c_ready = true;
    emit resultsLoaded(this);
}