Skip to content
reader.cpp 57.1 KiB
Newer Older
    }
    if(arg1 == "Source Serif Pro") {
        QFont sourceserif("Source Serif Pro");
        ui->text->setFont(sourceserif);
        string_writeconfig(".config/04-book/font", "Source Serif Pro");
    }
    if(arg1 == "Libre Baskerville") {
        QFont librebaskerville("Libre Baskerville");
        ui->text->setFont(librebaskerville);
        string_writeconfig(".config/04-book/font", "Libre Baskerville");
    }
    if(arg1 == "Noto Mono") {
        QFont notomono("Noto Mono");
        ui->text->setFont(notomono);
        string_writeconfig(".config/04-book/font", "Noto Mono");
    }
    if(arg1 == "Libertinus Serif") {
        QFont libertinus("Libertinus Serif");
        ui->text->setFont(libertinus);
        string_writeconfig(".config/04-book/font", "Libertinus Serif");
    }
    if(arg1 == "Crimson Pro") {
        // As adding Crimson Pro to the default fonts bundled along with the Qt libs breaks the general Inter homogeneity, it is incorporated on-demand here.
Nicolas Mailloux's avatar
Nicolas Mailloux committed
        setCrimsonProFont();
        string_writeconfig(".config/04-book/font", "Crimson Pro");
    }
    if(arg1 == "Bitter") {
Nicolas Mailloux's avatar
Nicolas Mailloux committed
        setBitterFont();
        string_writeconfig(".config/04-book/font", "Bitter");
    }
Nicolas Mailloux's avatar
Nicolas Mailloux committed
    if(arg1 == "Ibarra Real Nova") {
        setIbarraFont();
        string_writeconfig(".config/04-book/font", "Ibarra Real Nova");
    }
}

void reader::on_alignLeftBtn_clicked()
{
    if(is_epub != true) {
        ui->text->setAlignment(Qt::AlignLeft);
    }
    else {
        alignText(0);
    }
    string_writeconfig(".config/04-book/alignment", "Left");
}

void reader::on_alignCenterBtn_clicked()
{
    if(is_epub != true) {
        ui->text->setAlignment(Qt::AlignHCenter);
    }
    else {
        alignText(1);
    }
    string_writeconfig(".config/04-book/alignment", "Center");
}

void reader::on_alignRightBtn_clicked()
{
    if(is_epub != true) {
        ui->text->setAlignment(Qt::AlignRight);
    }
    else {
        alignText(2);
    }
    string_writeconfig(".config/04-book/alignment", "Right");
}

void reader::on_alignJustifyBtn_clicked()
{
    if(is_epub != true) {
        ui->text->setAlignment(Qt::AlignJustify);
    }
    else {
        alignText(3);
    }
    string_writeconfig(".config/04-book/alignment", "Justify");
}

void reader::alignText(int alignment) {
    /*
     * 0 - Left
     * 1 - Center
     * 2 - Right
     * 3 - Justify
    */
    textAlignment = alignment;
    if(is_epub == true) {
        if(alignment == 0) {
            QString epubPageContent_alignChange = epubPageContent;
            epubPageContent_alignChange.prepend("<div align='left'>");
            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);
    ui->pageWidget->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);
    ui->pageWidget->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);
    if(mupdf::epubPageNumber <= 0) {
        mupdf::epubPageNumber = 1;
    }
    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;
    }
}
Nicolas Mailloux's avatar
Nicolas Mailloux committed

void reader::openUsbmsDialog() {
    global::usbms::showUsbmsDialog = false;
    global::usbms::usbmsDialog = true;

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

QString reader::setPageNumberLabelContent() {
    QString content;
    if(is_epub == true) {
        QString pageNumber;
        QString totalPages;
        pageNumberInt = mupdf::epubPageNumber;
        pageNumber = QString::number(pageNumberInt);
        totalPages = QString::number(totalPagesInt);
        content.append(pageNumber);
        content.append(" <i>of</i> ");
        content.append(totalPages);
    }
    else {
        QString pageNumber;
        QString totalPages;
        pageNumberInt = split_files_number - split_total;
        totalPagesInt = split_files_number - 2;
        pageNumber = QString::number(pageNumberInt);
        totalPages = QString::number(totalPagesInt);
        content.append(pageNumber);
        content.append(" <i>of</i> ");
        content.append(totalPages);
    }
    return content;
}

void reader::setupPageWidget() {
    QString pageNumberInfoLabelContent = setPageNumberLabelContent();
    ui->pageNumberLabel->setText(pageNumberInfoLabelContent);
    ui->pageProgressBar->setMaximum(totalPagesInt);
    ui->pageProgressBar->setMinimum(1);
    ui->pageProgressBar->setValue(pageNumberInt);
}

void reader::getTotalEpubPagesNumber() {
    QString epubProg ("sh");
    QStringList epubArgs;
    convertMuPdfVars();
    epubArgs << "/mnt/onboard/.adds/inkbox/epub.sh" << mupdf::fontSize_qstr << mupdf::width_qstr << mupdf::height_qstr << mupdf::epubPageNumber_qstr << "get_pages_number";
    QProcess *epubProc = new QProcess();
    epubProc->start(epubProg, epubArgs);
    epubProc->waitForFinished();

    string_checkconfig_ro("/run/epub_total_pages_number");
    totalPagesInt = checkconfig_str_val.toInt();
}

void reader::on_gotoBtn_clicked()
{
    global::keyboard::keypadDialog = true;
    generalDialogWindow = new generalDialog();
    generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
    connect(generalDialogWindow, SIGNAL(gotoPageSelected(int)), SLOT(gotoPage(int)));
    generalDialogWindow->show();
}

void reader::gotoPage(int pageNumber) {
    if(is_epub == true) {
        if(pageNumber > totalPagesInt or pageNumber < 1) {
            QMessageBox::critical(this, tr("Invalid argument"), tr("Request is beyond the page range."));
        }
        else {
            mupdf::epubPageNumber = pageNumber;
            setup_book(book_file, mupdf::epubPageNumber, true);
            ui->text->setText("");
            ui->text->setText(epubPageContent);

            pagesTurned = 0;
            writeconfig_pagenumber();
        }
    }
    else {
        if(split_files_number - pageNumber < 2 or split_files_number - pageNumber > split_files_number - 1) {
            QMessageBox::critical(this, tr("Invalid argument"), tr("You've reached the end of the document."));
        }
        else {
            split_total = split_files_number - pageNumber;

            setup_book(book_file, split_total, false);
            ui->text->setText("");
            ui->text->setText(ittext);

            pagesTurned = 0;
            writeconfig_pagenumber();
        }
    }
    alignText(textAlignment);
    setupPageWidget();
    refreshScreen();
}

void reader::on_searchBtn_clicked()
{
    global::forbidOpenSearchDialog = false;
    setupSearchDialog();
}

void reader::setupSearchDialog() {
    if(global::forbidOpenSearchDialog == false) {
        global::keyboard::keyboardDialog = true;
        global::keyboard::searchDialog = true;
        global::keyboard::keyboardText = "";
        generalDialogWindow = new generalDialog();
        generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
        connect(generalDialogWindow, SIGNAL(refreshScreen()), SLOT(searchRefreshScreen()));
        connect(generalDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(setupSearchDialog()));
        generalDialogWindow->show();
    }
    else {
        ;
    }
}

Nicolas Mailloux's avatar
Nicolas Mailloux committed
void reader::setBitterFont() {
    QString family;
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-Medium.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-MediumItalic.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-Bold.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/Bitter-BoldItalic.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    QFont bitter(family);
    ui->text->setFont(bitter);
    ui->fontChooser->setCurrentText("Bitter");
}

void reader::setCrimsonProFont() {
    QString family;
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Regular.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Italic.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Bold.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-BoldItalic.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    QFont crimson(family);
    ui->text->setFont(crimson);
    ui->fontChooser->setCurrentText("Crimson Pro");
}

void reader::setIbarraFont() {
    QString family;
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-Medium.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-MediumItalic.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-Bold.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    {
        int id = QFontDatabase::addApplicationFont(":/resources/fonts/IbarraRealNova-BoldItalic.ttf");
        family = QFontDatabase::applicationFontFamilies(id).at(0);
    }
    QFont ibarra(family);
    ui->text->setFont(ibarra);
    ui->fontChooser->setCurrentText("Ibarra Real Nova");
}

void reader::searchRefreshScreen() {
    this->repaint();
}