Skip to content
reader.cpp 47.4 KiB
Newer Older
            epubPageContent_alignChange.append("</div>");
            ui->text->setText(epubPageContent_alignChange);
        }
        if(alignment == 1) {
            QString epubPageContent_alignChange = epubPageContent;
            epubPageContent_alignChange.prepend("<div align='center'>");
            epubPageContent_alignChange.append("</div>");
            ui->text->setText(epubPageContent_alignChange);
        }
        if(alignment == 2) {
            QString epubPageContent_alignChange = epubPageContent;
            epubPageContent_alignChange.prepend("<div align='right'>");
            epubPageContent_alignChange.append("</div>");
            ui->text->setText(epubPageContent_alignChange);
        }
        if(alignment == 3) {
            QString epubPageContent_alignChange = epubPageContent;
            epubPageContent_alignChange.prepend("<div align='justify'>");
            epubPageContent_alignChange.append("</div>");
            ui->text->setText(epubPageContent_alignChange);
        }
    }
    else {
        if(alignment == 0) {
            QString ittext_alignChange = ittext;
            ittext_alignChange.prepend("<div align='left'>");
            ittext_alignChange.append("</div>");
            ui->text->setText(ittext_alignChange);
        }
        if(alignment == 1) {
            QString ittext_alignChange = ittext;
            ittext_alignChange.prepend("<div align='center'>");
            ittext_alignChange.append("</div>");
            ui->text->setText(ittext_alignChange);
        }
        if(alignment == 2) {
            QString ittext_alignChange = ittext;
            ittext_alignChange.prepend("<div align='right'>");
            ittext_alignChange.append("</div>");
            ui->text->setText(ittext_alignChange);
        }
        if(alignment == 3) {
            QString ittext_alignChange = ittext;
            ittext_alignChange.prepend("<div align='justify'>");
            ittext_alignChange.append("</div>");
            ui->text->setText(ittext_alignChange);
        }
    }
}

void reader::menubar_show() {
    // Checking battery level and status, then displaying the relevant icon on batteryIconLabel
    string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status");
    if(checkconfig_str_val == "Charging\n") {
        ui->batteryIconLabel->setPixmap(scaledChargingPixmap);
    }
    else {
        get_battery_level();
        if(batt_level_int >= 75 && batt_level_int <= 100) {
            ui->batteryIconLabel->setPixmap(scaledFullPixmap);
        }
        if(batt_level_int >= 25 && batt_level_int <= 74) {
            ui->batteryIconLabel->setPixmap(scaledHalfPixmap);
        }
        if(batt_level_int >= 0 && batt_level_int <= 24) {
            ui->batteryIconLabel->setPixmap(scaledEmptyPixmap);
        }
    }

    ui->hideOptionsBtn->show();
    ui->optionsBtn->hide();
    ui->menuBarWidget->setVisible(true);
    ui->statusBarWidget->setVisible(true);
    string_checkconfig_ro("/opt/inkbox_device");
    if(checkconfig_str_val == "n705\n" or checkconfig_str_val == "n905\n") {
        ui->brightnessWidget->setVisible(true);
    menubar_shown = true;
}

void reader::menubar_hide() {
    string_checkconfig_ro("/opt/inkbox_device");
    if(checkconfig_str_val == "n705\n" or checkconfig_str_val == "n905\n") {
        ui->brightnessWidget->setVisible(false);
        ui->brightnessWidget->setVisible(false);
    ui->hideOptionsBtn->hide();
    ui->optionsBtn->show();
    ui->menuBarWidget->setVisible(false);
    if(checkconfig(".config/11-menubar/sticky") == true) {
        ui->statusBarWidget->setVisible(true);
        ui->statusBarWidget->setVisible(false);
        ui->menuWidget->setVisible(false);
    menubar_shown = false;
}

void reader::wordwidget_show() {
    if(menubar_shown == true) {
        menubar_hide();
        ui->hideOptionsBtn->hide();
        ui->optionsBtn->hide();
        ui->line->hide();
        ui->wordWidget->setVisible(true);
    }
    else {
        ui->hideOptionsBtn->hide();
        ui->optionsBtn->hide();
        ui->line->hide();
        ui->wordWidget->setVisible(true);
    }
}

void reader::wordwidget_hide() {
    ui->wordWidget->setVisible(false);
    ui->hideOptionsBtn->hide();
    ui->optionsBtn->show();
    ui->line->show();
}

void reader::on_infoCloseBtn_clicked()
{
    wordwidget_hide();
    dictionary_position = 1;
    QString dictionary_position_str = QString::number(dictionary_position);
    ui->definitionStatusLabel->setText(dictionary_position_str);

    QTextCursor cursor = ui->text->textCursor();
    cursor.clearSelection();
    ui->text->setTextCursor(cursor);
}

void reader::on_previousDefinitionBtn_clicked()
{
    dictionary_position = dictionary_position - 1;
    if(dictionary_position <= 0) {
        dictionary_position = 1;
    }
    else {
        dictionary_lookup(selected_text_str, letter, dictionary_position);
        ui->definitionLabel->setText(definition);
        QString dictionary_position_str = QString::number(dictionary_position);
        ui->definitionStatusLabel->setText(dictionary_position_str);
    }
}

void reader::on_nextDefinitionBtn_clicked()
{
    dictionary_position = dictionary_position + 1;
    dictionary_lookup(selected_text_str, letter, dictionary_position);
    if(nextdefinition_lock == true) {
        dictionary_position = dictionary_position - 1;
    }
    else {
        ui->definitionLabel->setText(definition);
        QString dictionary_position_str = QString::number(dictionary_position);
        ui->definitionStatusLabel->setText(dictionary_position_str);
    }
}

void reader::on_saveWordBtn_clicked()
{
    if(checkconfig_match(".config/06-words/config", selected_text_str) == true) {
        checkwords();
        word = word.append("\n");
        words = words.replace(word, "");
        string words_std_string = words.toStdString();
        save_word(words_std_string, true);
        ui->saveWordBtn->setText("");
        ui->saveWordBtn->setIcon(QIcon(":/resources/star.png"));
    }
    else {
        save_word(selected_text_str, false);
        ui->saveWordBtn->setText("");
        ui->saveWordBtn->setIcon(QIcon(":/resources/starred_star.png"));
    }
}

void reader::on_sizeSlider_valueChanged(int value)
{
    // Font size
    string value_str = to_string(value);
    string_writeconfig(".config/04-book/size", value_str);

    // Future improvement?
    if(checkconfig_match("/opt/inkbox_device", "n705\n") == true) {
        if(value == 0) {
            ui->text->setStyleSheet("font-size: 6pt");
            ui->sizeValueLabel->setText("1");
        }
        if(value == 1) {
            ui->text->setStyleSheet("font-size: 10pt");
            ui->sizeValueLabel->setText("2");
        }
        if(value == 2) {
            ui->text->setStyleSheet("font-size: 14pt");
            ui->sizeValueLabel->setText("3");
        }
        if(value == 3) {
            ui->text->setStyleSheet("font-size: 18pt");
            ui->sizeValueLabel->setText("4");
        }
        if(value == 4) {
            ui->text->setStyleSheet("font-size: 22pt");
            ui->sizeValueLabel->setText("5");
        }
    }
    if(checkconfig_match("/opt/inkbox_device", "n905\n") == true) {
        if(value == 0) {
            ui->text->setStyleSheet("font-size: 6pt");
            ui->sizeValueLabel->setText("1");
        }
        if(value == 1) {
            ui->text->setStyleSheet("font-size: 10pt");
            ui->sizeValueLabel->setText("2");
        }
        if(value == 2) {
            ui->text->setStyleSheet("font-size: 14pt");
            ui->sizeValueLabel->setText("3");
        }
        if(value == 3) {
            ui->text->setStyleSheet("font-size: 18pt");
            ui->sizeValueLabel->setText("4");
        }
        if(value == 4) {
            ui->text->setStyleSheet("font-size: 22pt");
            ui->sizeValueLabel->setText("5");
        }
    }
    else {
        if(value == 0) {
            ui->text->setStyleSheet("font-size: 6pt");
            ui->sizeValueLabel->setText("1");
        }
        if(value == 1) {
            ui->text->setStyleSheet("font-size: 10pt");
            ui->sizeValueLabel->setText("2");
        }
        if(value == 2) {
            ui->text->setStyleSheet("font-size: 14pt");
            ui->sizeValueLabel->setText("3");
        }
        if(value == 3) {
            ui->text->setStyleSheet("font-size: 18pt");
            ui->sizeValueLabel->setText("4");
        }
        if(value == 4) {
            ui->text->setStyleSheet("font-size: 22pt");
            ui->sizeValueLabel->setText("5");
        }
    }
}

void reader::writeconfig_pagenumber() {
    // Saving the page number in tmpfs
    if(is_epub != true) {
        std::string split_total_str = std::to_string(split_total);
        string_writeconfig("/tmp/inkboxPageNumber", split_total_str);
    }
    else {
        std::string epubPageNumber_str = std::to_string(mupdf::epubPageNumber);
        string_writeconfig("/tmp/inkboxPageNumber", epubPageNumber_str);
    }

void reader::quit_restart() {
    // Restarting InkBox
    QProcess process;
    process.startDetached("inkbox", QStringList());
    qApp->quit();
}

void reader::openLowBatteryDialog() {
    global::mainwindow::lowBatteryDialog = true;
    global::battery::batteryAlertLock = true;

    generalDialogWindow = new generalDialog(this);
    generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
    generalDialogWindow->show();
    QApplication::processEvents();
}

void reader::openCriticalBatteryAlertWindow() {
    global::battery::showCriticalBatteryAlert = true;
    global::battery::showLowBatteryDialog = false;

    alertWindow = new alert();
    alertWindow->setAttribute(Qt::WA_DeleteOnClose);
    alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size()));
    alertWindow->show();
void reader::convertMuPdfVars() {
Nicolas Mailloux's avatar
Nicolas Mailloux committed
    setPageStyle();
    mupdf::fontSize = 12;
    mupdf::fontSize_qstr = QString::number(mupdf::fontSize);
    mupdf::width_qstr = QString::number(mupdf::width);
    mupdf::height_qstr = QString::number(mupdf::height);
    mupdf::epubPageNumber_qstr = QString::number(mupdf::epubPageNumber);
}

void reader::setPageStyle() {
    // General page size
Nicolas Mailloux's avatar
Nicolas Mailloux committed
    defineDefaultPageSize();

    string_checkconfig_ro(".config/13-epub_page_size/width");
    if(checkconfig_str_val != "") {
        ;
Nicolas Mailloux's avatar
Nicolas Mailloux committed
    else {
        std::string pageWidth = std::to_string(defaultEpubPageWidth);
        string_writeconfig(".config/13-epub_page_size/width", pageWidth);
        string_writeconfig(".config/13-epub_page_size/set", "true");
        string_checkconfig_ro(".config/13-epub_page_size/width");
Nicolas Mailloux's avatar
Nicolas Mailloux committed
    mupdf::width = checkconfig_str_val.toInt();

    string_checkconfig_ro(".config/13-epub_page_size/height");
    if(checkconfig_str_val != "") {
        ;
    }
    else {
        std::string pageHeight = std::to_string(defaultEpubPageHeight);
        string_writeconfig(".config/13-epub_page_size/height", pageHeight);
        string_writeconfig(".config/13-epub_page_size/set", "true");
        string_checkconfig_ro(".config/13-epub_page_size/height");
    }
    mupdf::height = checkconfig_str_val.toInt();
}

void reader::delay(int seconds) {
    // https://stackoverflow.com/questions/3752742/how-do-i-create-a-pause-wait-function-using-qt
    QTime dieTime= QTime::currentTime().addSecs(seconds);
    while (QTime::currentTime() < dieTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);

void reader::on_text_selectionChanged() {
    delay(0.1);
    if(wordwidgetLock != true) {
        QTextCursor cursor = ui->text->textCursor();
        selected_text = cursor.selectedText();
        if(selected_text != "") {
            QString dictionary_position_str = QString::number(dictionary_position);
            ui->definitionStatusLabel->setText(dictionary_position_str);

            selected_text = selected_text.toLower();
            QStringList parts = selected_text.split(' ', QString::SkipEmptyParts);
            for (int i = 0; i < parts.size(); ++i)
                parts[i].replace(0, 1, parts[i][0].toUpper());
            word = parts.join(" ");
            letter = word.left(1);
            selected_text_str = word.toStdString();
            dictionary_lookup(selected_text_str, letter, dictionary_position);
            ui->wordLabel->setText(word);
            ui->definitionLabel->setText(definition);
            if(checkconfig_match(".config/06-words/config", selected_text_str) == true) {
                ui->saveWordBtn->setText("");
                ui->saveWordBtn->setIcon(QIcon(":/resources/starred_star.png"));
            }
            else {
                ui->saveWordBtn->setText("");
                ui->saveWordBtn->setIcon(QIcon(":/resources/star.png"));
            }
            wordwidgetLock = true;
            wordwidget_show();
        }
        else {
            ;
        }
    }
}

void reader::on_nightModeBtn_clicked()
{
    if(isNightModeActive == true) {
        // Disabling night/dark mode
        string_writeconfig("/tmp/invertScreen", "n");
        string_writeconfig(".config/10-dark_mode/config", "false");
        ui->nightModeBtn->setIcon(QIcon(":/resources/nightmode-empty.png"));
        isNightModeActive = false;
    }
    else {
        // Enabling night/dark mode
        string_writeconfig("/tmp/invertScreen", "y");
        string_writeconfig(".config/10-dark_mode/config", "true");
        ui->nightModeBtn->setIcon(QIcon(":/resources/nightmode-full.png"));
        isNightModeActive = true;
    }
}

void reader::pre_set_brightness(int brightnessValue) {
    string_checkconfig_ro("/opt/inkbox_device");
    if(checkconfig_str_val == "n705\n" or checkconfig_str_val == "n905\n") {
        set_brightness(brightnessValue);
    }
    else if(checkconfig_str_val == "n613\n") {
        set_brightness_ntxio(brightnessValue);
    }
    else {
        set_brightness(brightnessValue);
    }