Skip to content

Commit

Permalink
feat: Optimized UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
bookfere committed Apr 13, 2024
1 parent 33ed918 commit 6fc7c97
Show file tree
Hide file tree
Showing 18 changed files with 341 additions and 320 deletions.
4 changes: 2 additions & 2 deletions about.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from calibre.utils.localization import get_lang

from . import EbookTranslator
from .components import layout_info
from .components import Footer


try:
Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self, plugin, parent, icon):

layout.addWidget(brand, 1)
layout.addWidget(description, 2)
layout.addWidget(layout_info())
layout.addWidget(Footer())

def get_readme(self):
default = 'README.md'
Expand Down
27 changes: 13 additions & 14 deletions advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from . import EbookTranslator
from .components import (
EngineList, layout_info, SourceLang, TargetLang, InputFormat, OutputFormat,
EngineList, Footer, SourceLang, TargetLang, InputFormat, OutputFormat,
AlertMessage, AdvancedTranslationTable, StatusColor, TranslationStatus)


Expand Down Expand Up @@ -315,6 +315,7 @@ def __init__(self, parent, icon, worker, ebook):
self.ebook = ebook
self.config = get_config()
self.alert = AlertMessage(self)
self.footer = Footer()
# self.error = JobError(self)
self.current_engine = get_engine_class()
self.cache = None
Expand Down Expand Up @@ -349,7 +350,7 @@ def __init__(self, parent, icon, worker, ebook):
self.stack = QStackedWidget()
self.stack.addWidget(self.waiting)
layout.addWidget(self.stack)
layout.addWidget(layout_info())
layout.addWidget(self.footer)

def working_status():
self.logging_text.clear()
Expand Down Expand Up @@ -562,6 +563,7 @@ def write_progress():
counter_layout.addWidget(paragraph_count)
counter_layout.addWidget(non_aligned_paragraph_count)
counter_layout.addStretch(1)
self.footer.layout().insertWidget(0, counter)

def get_paragraph_count(select_all=True):
item_count = char_count = 0
Expand Down Expand Up @@ -597,7 +599,6 @@ def show_none_aligned_count():
layout.addWidget(filter_widget)
layout.addWidget(self.table, 1)
layout.addWidget(progress_bar)
layout.addWidget(counter)
layout.addWidget(self.layout_table_control())

def working_start():
Expand Down Expand Up @@ -893,6 +894,7 @@ def auto_open_close_splitter():

save_status = QLabel()
save_button = QPushButton(_('Save'))
save_button.setDisabled(True)

status_indicator = TranslationStatus()

Expand All @@ -915,7 +917,8 @@ def update_translation_status(row):
else:
status_indicator.set_color(StatusColor('gray'))
elif not paragraph.aligned and self.merge_enabled:
status_indicator.set_color(StatusColor('yellow'))
status_indicator.set_color(
StatusColor('yellow'), )
else:
status_indicator.set_color(StatusColor('green'))
self.table.row.connect(update_translation_status)
Expand Down Expand Up @@ -952,11 +955,9 @@ def modify_translation():
if self.trans_worker.on_working and \
self.table.selected_count() > 1:
return
save_button.setDisabled(False)
# paragraph = self.table.current_paragraph()
# translation = translation_text.toPlainText()
# control.setVisible(
# bool(translation) and translation != paragraph.translation)
paragraph = self.table.current_paragraph()
translation = translation_text.toPlainText()
save_button.setDisabled(translation == paragraph.translation)
translation_text.textChanged.connect(modify_translation)

def save_translation():
Expand All @@ -967,11 +968,9 @@ def save_translation():
paragraph.target_lang = self.ebook.target_lang
self.table.row.emit(paragraph.row)
self.cache.update_paragraph(paragraph)
# self.editor_worker.start[str, object].emit(
# _('Your changes have been saved.'),
# lambda: control.setVisible(False))
self.editor_worker.start[str].emit(
_('Your changes have been saved.'))
self.editor_worker.start[str, object].emit(
_('Your changes have been saved.'),
lambda: save_button.setDisabled(True))
translation_text.setFocus(Qt.OtherFocusReason)
self.editor_worker.show.connect(save_status.setText)
save_button.clicked.connect(save_translation)
Expand Down
40 changes: 28 additions & 12 deletions batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
from .lib.encodings import encoding_list
from .engines.custom import CustomTranslate
from .components import (
layout_info, AlertMessage, SourceLang, TargetLang, InputFormat,
Footer, AlertMessage, SourceLang, TargetLang, InputFormat,
OutputFormat)


try:
from qt.core import (
QDialog, QWidget, QPushButton, QHeaderView, QVBoxLayout, QTableWidget,
QTableWidgetItem, Qt, QComboBox, QLabel)
QTableWidgetItem, Qt, QComboBox, QLabel, QSizePolicy, QHBoxLayout)
except ImportError:
from PyQt5.Qt import (
QDialog, QWidget, QPushButton, QHeaderView, QVBoxLayout, QTableWidget,
QTableWidgetItem, Qt, QComboBox, QLabel)
QTableWidgetItem, Qt, QComboBox, QLabel, QSizePolicy, QHBoxLayout)

load_translations()

Expand All @@ -42,15 +42,28 @@ def __init__(self, parent, worker, ebooks):
def main_layout(self):
layout = QVBoxLayout(self)
layout.addWidget(self.layout_translate())
layout.addWidget(layout_info())
layout.addWidget(Footer())

def _cell_widget(self, _widget):
widget = QWidget()
layout = QHBoxLayout(widget)
layout.setContentsMargins(5, 5, 5, 5)
# _widget.setFixedSize(_widget.sizeHint())
_widget.setFixedHeight(_widget.sizeHint().height())
layout.addWidget(_widget, 1)
# layout.setAlignment(_widget, Qt.AlignCenter)
return widget

def layout_translate(self):
widget = QWidget()
layout = QVBoxLayout(widget)
layout.setContentsMargins(0, 0, 0, 0)

table = QTableWidget()
table.setStyleSheet('QComboBox{border:0;}')
table.setAlternatingRowColors(True)
table.setFocusPolicy(Qt.NoFocus)
table.setSelectionMode(QTableWidget.NoSelection)
# table.SelectionBehavior(QTableWidget.SelectRows)
table.setRowCount(len(self.ebooks))
table.setColumnCount(5)
table.setHorizontalHeaderLabels([
Expand All @@ -61,13 +74,12 @@ def layout_translate(self):
stretch = getattr(QHeaderView.ResizeMode, 'Stretch', None) or \
QHeaderView.Stretch
header.setSectionResizeMode(0, stretch)
# table.verticalHeader().setMinimumSectionSize(100)

translation_engine = get_engine_class()

for row, ebook in enumerate(self.ebooks):
ebook_title = QTableWidgetItem(ebook.title)
ebook_title.setFlags(Qt.ItemIsEnabled)
ebook_title.setSizeHint(table.sizeHint())
table.setItem(row, 0, ebook_title)

if ebook.input_format in extra_formats.keys():
Expand All @@ -78,13 +90,15 @@ def layout_translate(self):
.set_encoding(encoding))
else:
input_encoding = QLabel(_('Default'))
table.setCellWidget(row, 1, input_encoding)
input_encoding.setDisabled(True)
input_encoding.setAlignment(Qt.AlignCenter)
table.setCellWidget(row, 1, self._cell_widget(input_encoding))

input_fmt = InputFormat(ebook.files.keys())
table.setCellWidget(row, 2, input_fmt)
table.setCellWidget(row, 2, self._cell_widget(input_fmt))

output_format = OutputFormat()
table.setCellWidget(row, 3, output_format)
table.setCellWidget(row, 3, self._cell_widget(output_format))

exist_format = output_format.findText(ebook.input_format)
if ebook.is_extra_format() and exist_format:
Expand All @@ -106,7 +120,7 @@ def change_output_format(format, row=row):
output_format.currentTextChanged.connect(change_output_format)

source_lang = SourceLang(book_lang=ebook.source_lang)
table.setCellWidget(row, 4, source_lang)
table.setCellWidget(row, 4, self._cell_widget(source_lang))
self.source_langs.append(source_lang)

def change_source_lang(lang, row=row):
Expand All @@ -120,7 +134,7 @@ def change_source_lang(lang, row=row):
not issubclass(translation_engine, CustomTranslate))

target_lang = TargetLang()
table.setCellWidget(row, 5, target_lang)
table.setCellWidget(row, 5, self._cell_widget(target_lang))
self.target_langs.append(target_lang)

def change_target_lang(lang, row=row):
Expand All @@ -135,6 +149,8 @@ def change_target_lang(lang, row=row):
translation_engine.lang_codes.get('target'),
translation_engine.config.get('target_lang'))

table.resizeRowsToContents()
# table.resizeColumnsToContents()
layout.addWidget(table)

start_button = QPushButton(_('Translate'))
Expand Down
13 changes: 8 additions & 5 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .lib.utils import open_path
from .lib.cache import default_cache_path, TranslationCache
from .lib.config import get_config
from .components import layout_info, AlertMessage
from .components import Footer, AlertMessage

try:
from qt.core import (
Expand All @@ -30,13 +30,17 @@ def __init__(self, plugin, parent):
self.gui = parent
self.config = get_config()
self.alert = AlertMessage(self)
self.footer = Footer()
self.default_path = default_cache_path()

self.cache_size = QLabel()
self.footer.layout().insertWidget(0, self.cache_size)

self.layout = QVBoxLayout(self)
self.layout.addWidget(self.control_widget())
self.layout.addWidget(self.table_widget())
self.layout.addWidget(self.enable_widget())
self.layout.addWidget(layout_info())
self.layout.addWidget(self.footer)

self.cache_list.selected_rows.connect(
lambda rows: self.delete_button.setDisabled(len(rows) < 1))
Expand Down Expand Up @@ -86,15 +90,14 @@ def enable_widget(self):
widget = QWidget()
layout = QHBoxLayout(widget)
layout.setContentsMargins(0, 0, 0, 0)
self.cache_size = QLabel()

self.clear_button = QPushButton(_('Clear All'))
self.clear_button.setDisabled(True)
self.delete_button = QPushButton(_('Delete'))
self.delete_button.setDisabled(True)

layout.addWidget(self.cache_size)
layout.addStretch(1)
layout.addWidget(self.clear_button)
layout.addStretch(1)
layout.addWidget(self.delete_button)

return widget
Expand Down
2 changes: 1 addition & 1 deletion components/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .engine import EngineList, EngineTester, ManageCustomEngine
from .info import layout_info
from .footer import Footer
from .lang import SourceLang, TargetLang
from .format import InputFormat, OutputFormat
from .alert import AlertMessage
Expand Down
40 changes: 40 additions & 0 deletions components/footer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from calibre.utils.localization import get_lang
from calibre_plugins.ebook_translator import EbookTranslator


try:
from qt.core import QWidget, QHBoxLayout, QLabel
except ImportError:
from PyQt5.Qt import QWidget, QHBoxLayout, QLabel

load_translations()


class Footer(QWidget):

def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.status = QLabel()

app_author = EbookTranslator.author
github = 'https://github.com/bookfere/Ebook-Translator-Calibre-Plugin'
if 'zh' in get_lang():
feedback = 'https://{}/post/1057.html'.format(app_author)
donate = 'https://{}/donate'.format(app_author)
else:
feedback = '{}/issues'.format(github)
donate = 'https://www.paypal.com/paypalme/bookfere'
link = QLabel(
'<span style="color:crimson;">♥</span> by <a href="https://{0}">'
'{0}</a> | <a href="{1}">GitHub</a> | <a href="{2}">{4}</a>'
' | <a href="{3}">{5}</a>'.format(
app_author, github, feedback, donate, _('Feedback'),
_('Donate')))
link.setStyleSheet('color:grey')
link.setOpenExternalLinks(True)

layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.status)
layout.addStretch(1)
layout.addWidget(link)
39 changes: 0 additions & 39 deletions components/info.py

This file was deleted.

4 changes: 2 additions & 2 deletions components/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


try:
from qt.core import pyqtSignal, pyqtSlot, QComboBox
from qt.core import pyqtSignal, pyqtSlot, QComboBox, QVBoxLayout
except ImportError:
from PyQt5.Qt import pyqtSignal, pyqtSlot, QComboBox
from PyQt5.Qt import pyqtSignal, pyqtSlot, QComboBox, QVBoxLayout

load_translations()

Expand Down
4 changes: 3 additions & 1 deletion components/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ def layout(self):
original.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
original.setData(Qt.UserRole, paragraph)
engine_name = QTableWidgetItem(paragraph.engine_name)
engine_name.setToolTip(paragraph.engine_name)
engine_name.setTextAlignment(Qt.AlignCenter)
traget_lang = QTableWidgetItem(paragraph.target_lang)
traget_lang.setToolTip(paragraph.target_lang)
traget_lang.setTextAlignment(Qt.AlignCenter)
status = QTableWidgetItem()
status.setTextAlignment(Qt.AlignCenter)
Expand Down Expand Up @@ -152,7 +154,7 @@ def set_row_background_color(
for column in range(self.columnCount()):
item = self.item(row, column)
item.setBackground(background)
item.setToolTip(tip)
item.setToolTip(tip)

def contextMenuEvent(self, event):
if self.parent.trans_worker.on_working:
Expand Down
Loading

0 comments on commit 6fc7c97

Please sign in to comment.