From 40262b05068814b4d390053b21aa1354ae0b7f15 Mon Sep 17 00:00:00 2001 From: Nicolas Mailloux Date: Sun, 20 Jun 2021 17:00:42 -0400 Subject: [PATCH] Replaced QLabel by a QTextEdit; no more clipping out All's working! (Except maybe some layout shenaningans) --- reader.cpp | 248 +++++++++++++++++++++++++++++--------------- reader.h | 6 +- reader.ui | 253 +++++++++++++++++++++++---------------------- resources/eink.qss | 4 - 4 files changed, 303 insertions(+), 208 deletions(-) diff --git a/reader.cpp b/reader.cpp index 6a0b3b3..c4e33c4 100644 --- a/reader.cpp +++ b/reader.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include using namespace std; @@ -29,6 +30,7 @@ reader::reader(QWidget *parent) : is_epub = true; } mupdf::epubPageNumber = 22; + wordwidgetLock = false; ui->setupUi(this); ui->previousBtn->setProperty("type", "borderless"); @@ -104,26 +106,6 @@ reader::reader(QWidget *parent) : } } - // Alignment - string_checkconfig(".config/04-book/alignment"); - if (checkconfig_str_val == "") { - ; - } - else { - if(checkconfig_str_val == "Left") { - ui->text->setAlignment(Qt::AlignLeft); - } - if(checkconfig_str_val == "Center") { - ui->text->setAlignment(Qt::AlignHCenter); - } - if(checkconfig_str_val == "Right") { - ui->text->setAlignment(Qt::AlignRight); - } - if(checkconfig_str_val == "Justify") { - ui->text->setAlignment(Qt::AlignJustify); - } - } - // Stylesheet + misc. QFile stylesheetFile(":/resources/eink.qss"); stylesheetFile.open(QFile::ReadOnly); @@ -153,15 +135,9 @@ reader::reader(QWidget *parent) : } // Topbar widget / book info - if(checkconfig(".config/13-topbarinfo/config") == true) { - ui->topbarStackedWidget->setVisible(true); - showTopbarWidget = true; - ui->bookInfoLabel->setFont(crimson); - } - else { - ui->topbarStackedWidget->setVisible(false); - showTopbarWidget = false; - } + ui->topbarStackedWidget->setVisible(true); + showTopbarWidget = true; + ui->bookInfoLabel->setFont(crimson); // Getting brightness level int brightness_value = get_brightness(); @@ -257,46 +233,6 @@ reader::reader(QWidget *parent) : t->start(); } - // Word selection & dictionary lookup feature - QString dictionary_position_str = QString::number(dictionary_position); - ui->definitionStatusLabel->setText(dictionary_position_str); - QTimer *select_t = new QTimer(this); - select_t->setInterval(100); - connect(select_t, &QTimer::timeout, [&]() { - selected_text = ui->text->selectedText(); - if(ui->text->hasSelectedText() == true) { - if(selected_text_lock == false) { - selected_text_lock = true; - 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")); - } - wordwidget_show(); - } - else { - ; - } - } - else { - ; - } - } ); - select_t->start(); - // We have to get the file's path if(global::reader::skipOpenDialog == true) { if(checkconfig("/tmp/suspendBook") == true) { @@ -435,6 +371,30 @@ reader::reader(QWidget *parent) : ui->text->setText(epubPageContent); } + // Alignment + string_checkconfig(".config/04-book/alignment"); + if (checkconfig_str_val == "") { + ; + } + else { + if(checkconfig_str_val == "Left") { + textAlignment = 0; + alignText(0); + } + if(checkconfig_str_val == "Center") { + textAlignment = 1; + alignText(1); + } + if(checkconfig_str_val == "Right") { + textAlignment = 2; + alignText(2); + } + if(checkconfig_str_val == "Justify") { + textAlignment = 3; + alignText(3); + } + } + // Topbar info widget if(is_epub == true) { QString bookCreator = findEpubMetadata(book_file, "creator"); @@ -756,6 +716,7 @@ void reader::on_nextBtn_clicked() pagesTurned = pagesTurned + 1; writeconfig_pagenumber(); } + alignText(textAlignment); refreshScreen(); } @@ -770,16 +731,10 @@ void reader::on_previousBtn_clicked() split_total = split_total + 1; setup_book(book_file, split_total, false); ui->text->setText(""); - if(is_epub != true) { - ui->text->setText(ittext); - } - else { - ui->text->setText(epubPageContent); - } + ui->text->setText(ittext); // We always increment pagesTurned regardless whether we press the Previous or Next button pagesTurned = pagesTurned + 1; - refreshScreen(); writeconfig_pagenumber(); } } @@ -793,6 +748,8 @@ void reader::on_previousBtn_clicked() pagesTurned = pagesTurned + 1; writeconfig_pagenumber(); } + alignText(textAlignment); + refreshScreen(); } void reader::refreshScreen() { @@ -911,28 +868,110 @@ void reader::on_fontChooser_currentIndexChanged(const QString &arg1) void reader::on_alignLeftBtn_clicked() { - ui->text->setAlignment(Qt::AlignLeft); + if(is_epub != true) { + ui->text->setAlignment(Qt::AlignLeft); + } + else { + alignText(0); + } string_writeconfig(".config/04-book/alignment", "Left"); } void reader::on_alignCenterBtn_clicked() { - ui->text->setAlignment(Qt::AlignHCenter); + if(is_epub != true) { + ui->text->setAlignment(Qt::AlignHCenter); + } + else { + alignText(1); + } string_writeconfig(".config/04-book/alignment", "Center"); } void reader::on_alignRightBtn_clicked() { - ui->text->setAlignment(Qt::AlignRight); + if(is_epub != true) { + ui->text->setAlignment(Qt::AlignRight); + } + else { + alignText(2); + } string_writeconfig(".config/04-book/alignment", "Right"); } void reader::on_alignJustifyBtn_clicked() { - ui->text->setAlignment(Qt::AlignJustify); + 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("
"); + epubPageContent_alignChange.append("
"); + ui->text->setText(epubPageContent_alignChange); + } + if(alignment == 1) { + QString epubPageContent_alignChange = epubPageContent; + epubPageContent_alignChange.prepend("
"); + epubPageContent_alignChange.append("
"); + ui->text->setText(epubPageContent_alignChange); + } + if(alignment == 2) { + QString epubPageContent_alignChange = epubPageContent; + epubPageContent_alignChange.prepend("
"); + epubPageContent_alignChange.append("
"); + ui->text->setText(epubPageContent_alignChange); + } + if(alignment == 3) { + QString epubPageContent_alignChange = epubPageContent; + epubPageContent_alignChange.prepend("
"); + epubPageContent_alignChange.append("
"); + ui->text->setText(epubPageContent_alignChange); + } + } + else { + if(alignment == 0) { + QString ittext_alignChange = ittext; + ittext_alignChange.prepend("
"); + ittext_alignChange.append("
"); + ui->text->setText(ittext_alignChange); + } + if(alignment == 1) { + QString ittext_alignChange = ittext; + ittext_alignChange.prepend("
"); + ittext_alignChange.append("
"); + ui->text->setText(ittext_alignChange); + } + if(alignment == 2) { + QString ittext_alignChange = ittext; + ittext_alignChange.prepend("
"); + ittext_alignChange.append("
"); + ui->text->setText(ittext_alignChange); + } + if(alignment == 3) { + QString ittext_alignChange = ittext; + ittext_alignChange.prepend("
"); + ittext_alignChange.append("
"); + 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"); @@ -1021,7 +1060,7 @@ void reader::wordwidget_hide() { ui->hideOptionsBtn->hide(); ui->optionsBtn->show(); ui->line->show(); - selected_text_lock = false; + wordwidgetLock = false; } void reader::on_infoCloseBtn_clicked() @@ -1030,6 +1069,10 @@ void reader::on_infoCloseBtn_clicked() 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() @@ -1228,3 +1271,46 @@ void reader::setPageStyle() { } } } + +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::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); +} diff --git a/reader.h b/reader.h index 8033302..b74a1c6 100644 --- a/reader.h +++ b/reader.h @@ -38,13 +38,13 @@ public: int page_number; int dictionary_position = 1; int pagesTurned = 0; + int textAlignment; // -1 : Never refresh | 0 : Refresh every page | 1 : Refresh every 1 page. And so on... // Refresh every 3 pages is the default int pageRefreshSetting = 3; bool menubar_shown = false; - bool selected_text_lock = false; bool nextdefinition_lock = false; bool is_epub = false; bool parser_ran = false; @@ -53,6 +53,7 @@ public: bool wakeFromSleep = false; bool remount = true; bool showTopbarWidget; + bool wordwidgetLock; QString book_1; QString book_2; QString book_3; @@ -87,6 +88,8 @@ public: void convertMuPdfVars(); void refreshScreen(); void setPageStyle(); + void alignText(int alignment); + void delay(int seconds); private slots: void on_nextBtn_clicked(); @@ -109,6 +112,7 @@ private slots: void on_sizeSlider_valueChanged(int value); void writeconfig_pagenumber(); void quit_restart(); + void on_text_selectionChanged(); private: Ui::reader *ui; diff --git a/reader.ui b/reader.ui index b26608b..0f64ecd 100644 --- a/reader.ui +++ b/reader.ui @@ -7,7 +7,7 @@ 0 0 490 - 693 + 676 @@ -16,127 +16,6 @@ - - - - - Ubuntu - false - - - - Text - - - Qt::AlignJustify|Qt::AlignVCenter - - - true - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - QFrame::Plain - - - Qt::Vertical - - - - - - - - 75 - true - - - - Previous - - - - - - - QFrame::Plain - - - Qt::Vertical - - - - - - - - 75 - true - - - - Options - - - - - - - - 75 - true - - - - Next - - - - - - - - 75 - true - - - - Hide - - - - - - - - - QFrame::Plain - - - 2 - - - Qt::Horizontal - - - @@ -1026,6 +905,136 @@ + + + + + + QFrame::Plain + + + Qt::Vertical + + + + + + + + 75 + true + + + + Previous + + + + + + + QFrame::Plain + + + Qt::Vertical + + + + + + + + 75 + true + + + + Options + + + + + + + + 75 + true + + + + Next + + + + + + + + 75 + true + + + + Hide + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + 50 + 50 + + + + + + + QFrame::NoFrame + + + QFrame::Plain + + + 0 + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + + + + + QFrame::Plain + + + 2 + + + Qt::Horizontal + + + diff --git a/resources/eink.qss b/resources/eink.qss index c86c0a2..3fd5e1f 100644 --- a/resources/eink.qss +++ b/resources/eink.qss @@ -192,19 +192,15 @@ QLineEdit:edit-focus, QSpinBox:edit-focus, QDoubleSpinBox:edit-focus QTextEdit { - background-color:transparent; border: none; - padding: 1px; } QTextEdit:focus { border: none; - padding: 1px; } QTextEdit:edit-focus { border: none; - padding: 0px; } QComboBox