Replaced QLabel by a QTextEdit; no more clipping out

All's working! (Except maybe some layout shenaningans)
This commit is contained in:
Nicolas Mailloux 2021-06-20 17:00:42 -04:00
parent e68900babb
commit 40262b0506
4 changed files with 303 additions and 208 deletions

View file

@ -14,6 +14,7 @@
#include <QScreen>
#include <QFontDatabase>
#include <QDirIterator>
#include <QTextCursor>
#include <QDebug>
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("<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");
@ -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);
}

View file

@ -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;

253
reader.ui
View file

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>490</width>
<height>693</height>
<height>676</height>
</rect>
</property>
<property name="windowTitle">
@ -16,127 +16,6 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="10" column="0">
<widget class="QLabel" name="text">
<property name="font">
<font>
<family>Ubuntu</family>
<italic>false</italic>
</font>
</property>
<property name="text">
<string>Text</string>
</property>
<property name="alignment">
<set>Qt::AlignJustify|Qt::AlignVCenter</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="11" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="17" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="4">
<widget class="Line" name="line_3">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="previousBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Previous</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="Line" name="line">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="optionsBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Options</string>
</property>
</widget>
</item>
<item row="0" column="5">
<widget class="QPushButton" name="nextBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Next</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QPushButton" name="hideOptionsBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Hide</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="16" column="0">
<widget class="Line" name="line_2">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="12" column="0">
<widget class="QStackedWidget" name="wordWidget">
<widget class="QWidget" name="page_8">
@ -1026,6 +905,136 @@
<widget class="QWidget" name="page_21"/>
</widget>
</item>
<item row="17" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="4">
<widget class="Line" name="line_3">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="previousBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Previous</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="Line" name="line">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="optionsBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Options</string>
</property>
</widget>
</item>
<item row="0" column="5">
<widget class="QPushButton" name="nextBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Next</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QPushButton" name="hideOptionsBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Hide</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="11" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="10" column="0">
<widget class="QTextEdit" name="text">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>50</horstretch>
<verstretch>50</verstretch>
</sizepolicy>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
</item>
<item row="16" column="0">
<widget class="Line" name="line_2">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="9" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">

View file

@ -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