Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added char-count with settings to enable/disable it. #746

Merged
merged 4 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions manuskript/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def wordCount(text):
t = [l for l in t if l]
return len(t)

def charCount(text, use_spaces = True):
t = text.strip()

if not use_spaces:
t = t.replace(" ", "")

return len(t)

validate_ok = lambda *args, **kwargs: True
def uiParse(input, default, converter, validator=validate_ok):
"""
Expand Down
13 changes: 9 additions & 4 deletions manuskript/models/outlineItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def __str__(self):
)

__repr__ = __str__

def charCount(self):
return self._data.get(self.enum.charCount, 0)

#######################################################################
# Data
Expand Down Expand Up @@ -122,7 +125,7 @@ def data(self, column, role=Qt.DisplayRole):

elif role == Qt.FontRole:
f = QFont()
if column == E.wordCount and self.isFolder():
if (column == E.wordCount or column == E.charCount) and self.isFolder():
f.setItalic(True)
elif column == E.goal and self.isFolder() and not self.data(E.setGoal):
f.setItalic(True)
Expand All @@ -143,7 +146,7 @@ def setData(self, column, data, role=Qt.DisplayRole):

# Checking if we will have to recount words
updateWordCount = False
if column in [E.wordCount, E.goal, E.setGoal]:
if column in [E.wordCount, E.charCount, E.goal, E.setGoal]:
updateWordCount = not column in self._data or self._data[column] != data

# Stuff to do before
Expand All @@ -156,8 +159,9 @@ def setData(self, column, data, role=Qt.DisplayRole):
# Stuff to do afterwards
if column == E.text:
wc = F.wordCount(data)
cc = F.charCount(data, settings.countSpaces)
self.setData(E.wordCount, wc)
self.setData(E.charCount, len(data))
self.setData(E.charCount, cc)

if column == E.compile:
# Title changes when compile changes
Expand Down Expand Up @@ -225,7 +229,8 @@ def updateWordCount(self):
self.setData(self.enum.goalPercentage, "")

self.emitDataChanged([self.enum.goal, self.enum.setGoal,
self.enum.wordCount, self.enum.goalPercentage])
self.enum.wordCount, self.enum.charCount,
self.enum.goalPercentage])

if self.parent():
self.parent().updateWordCount()
Expand Down
14 changes: 13 additions & 1 deletion manuskript/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
folderView = "cork"
lastTab = 0
openIndexes = [""]
progressChars = False
countSpaces = True
autoSave = False
autoSaveDelay = 5
autoSaveNoChanges = True
Expand Down Expand Up @@ -123,7 +125,7 @@ def initDefaultValues():
def save(filename=None, protocol=None):

global spellcheck, dict, corkSliderFactor, viewSettings, corkSizeFactor, folderView, lastTab, openIndexes, \
autoSave, autoSaveDelay, saveOnQuit, autoSaveNoChanges, autoSaveNoChangesDelay, outlineViewColumns, \
progressChars, autoSave, autoSaveDelay, saveOnQuit, autoSaveNoChanges, autoSaveNoChangesDelay, outlineViewColumns, \
corkBackground, corkStyle, fullScreenTheme, defaultTextType, textEditor, revisions, frequencyAnalyzer, viewMode, \
saveToZip, dontShowDeleteWarning, fullscreenSettings

Expand All @@ -136,6 +138,8 @@ def save(filename=None, protocol=None):
"folderView": folderView,
"lastTab": lastTab,
"openIndexes": openIndexes,
"progressChars": progressChars,
"countSpaces": countSpaces,
"autoSave":autoSave,
"autoSaveDelay":autoSaveDelay,
# TODO: Settings Cleanup Task -- Rename saveOnQuit to saveOnProjectClose -- see PR #615
Expand Down Expand Up @@ -235,6 +239,14 @@ def load(string, fromString=False, protocol=None):
global openIndexes
openIndexes = allSettings["openIndexes"]

if "progressChars" in allSettings:
global progressChars
progressChars = allSettings["progressChars"]

if "countSpaces" in allSettings:
global countSpaces
countSpaces = allSettings["countSpaces"]

if "autoSave" in allSettings:
global autoSave
autoSave = allSettings["autoSave"]
Expand Down
20 changes: 20 additions & 0 deletions manuskript/settingsWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def __init__(self, mainWindow):
self.spnGeneralFontSize.setValue(f.pointSize())
self.spnGeneralFontSize.valueChanged.connect(self.setAppFontSize)

self.chkProgressChars.setChecked(settings.progressChars);
self.chkProgressChars.stateChanged.connect(self.charSettingsChanged)

self.txtAutoSave.setValidator(QIntValidator(0, 999, self))
self.txtAutoSaveNoChanges.setValidator(QIntValidator(0, 999, self))
self.chkAutoSave.setChecked(settings.autoSave)
Expand Down Expand Up @@ -164,10 +167,12 @@ def __init__(self, mainWindow):
for item, what, value in [
(self.rdoTreeItemCount, "InfoFolder", "Count"),
(self.rdoTreeWC, "InfoFolder", "WC"),
(self.rdoTreeCC, "InfoFolder", "CC"),
(self.rdoTreeProgress, "InfoFolder", "Progress"),
(self.rdoTreeSummary, "InfoFolder", "Summary"),
(self.rdoTreeNothing, "InfoFolder", "Nothing"),
(self.rdoTreeTextWC, "InfoText", "WC"),
(self.rdoTreeTextCC, "InfoText", "CC"),
(self.rdoTreeTextProgress, "InfoText", "Progress"),
(self.rdoTreeTextSummary, "InfoText", "Summary"),
(self.rdoTreeTextNothing, "InfoText", "Nothing"),
Expand All @@ -180,6 +185,9 @@ def __init__(self, mainWindow):
lambda v: self.lblTreeIconSize.setText("{}x{}".format(v, v)))
self.sldTreeIconSize.setValue(settings.viewSettings["Tree"]["iconSize"])

self.chkCountSpaces.setChecked(settings.countSpaces);
self.chkCountSpaces.stateChanged.connect(self.countSpacesChanged)

self.rdoCorkOldStyle.setChecked(settings.corkStyle == "old")
self.rdoCorkNewStyle.setChecked(settings.corkStyle == "new")
self.rdoCorkNewStyle.toggled.connect(self.setCorkStyle)
Expand Down Expand Up @@ -338,6 +346,11 @@ def setAppFontSize(self, val):
sttgs = QSettings(qApp.organizationName(), qApp.applicationName())
sttgs.setValue("appFontSize", val)

def charSettingsChanged(self):
settings.progressChars = True if self.chkProgressChars.checkState() else False

self.mw.mainEditor.updateStats()

def saveSettingsChanged(self):
if self.txtAutoSave.text() in ["", "0"]:
self.txtAutoSave.setText("1")
Expand Down Expand Up @@ -427,10 +440,12 @@ def treeViewSettignsChanged(self):
for item, what, value in [
(self.rdoTreeItemCount, "InfoFolder", "Count"),
(self.rdoTreeWC, "InfoFolder", "WC"),
(self.rdoTreeCC, "InfoFolder", "CC"),
(self.rdoTreeProgress, "InfoFolder", "Progress"),
(self.rdoTreeSummary, "InfoFolder", "Summary"),
(self.rdoTreeNothing, "InfoFolder", "Nothing"),
(self.rdoTreeTextWC, "InfoText", "WC"),
(self.rdoTreeTextCC, "InfoText", "CC"),
(self.rdoTreeTextProgress, "InfoText", "Progress"),
(self.rdoTreeTextSummary, "InfoText", "Summary"),
(self.rdoTreeTextNothing, "InfoText", "Nothing"),
Expand All @@ -445,6 +460,11 @@ def treeViewSettignsChanged(self):

self.mw.treeRedacOutline.viewport().update()

def countSpacesChanged(self):
settings.countSpaces = True if self.chkCountSpaces.checkState() else False

self.mw.mainEditor.updateStats()

def setCorkColor(self):
color = QColor(settings.corkBackground["color"])
self.colorDialog = QColorDialog(color, self)
Expand Down
38 changes: 29 additions & 9 deletions manuskript/ui/editors/mainEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ def updateStats(self):
return

index = self.currentEditor().currentIndex

if index.isValid():
item = index.internalPointer()
else:
Expand All @@ -300,6 +301,7 @@ def updateStats(self):
if not item:
item = self.mw.mdlOutline.rootItem

cc = item.data(Outline.charCount)
wc = item.data(Outline.wordCount)
goal = item.data(Outline.goal)
chars = item.data(Outline.charCount) # len(item.data(Outline.text))
Expand All @@ -308,10 +310,12 @@ def updateStats(self):
goal = uiParse(goal, None, int, lambda x: x>=0)
progress = uiParse(progress, 0.0, float)

if not chars:
chars = 0
if not cc:
cc = 0

if not wc:
wc = 0

if goal:
self.lblRedacProgress.show()
rect = self.lblRedacProgress.geometry()
Expand All @@ -322,15 +326,31 @@ def updateStats(self):
drawProgress(p, rect, progress, 2)
del p
self.lblRedacProgress.setPixmap(self.px)
self.lblRedacWC.setText(self.tr("({} chars) {} words / {} ").format(
locale.format("%d", chars, grouping=True),
locale.format("%d", wc, grouping=True),
locale.format("%d", goal, grouping=True)))

if settings.progressChars:
self.lblRedacWC.setText(self.tr("({} chars) {} words / {} ").format(
locale.format("%d", cc, grouping=True),
locale.format("%d", wc, grouping=True),
locale.format("%d", goal, grouping=True)))
self.lblRedacWC.setToolTip("")
else:
self.lblRedacWC.setText(self.tr("{} words / {} ").format(
locale.format("%d", wc, grouping=True),
locale.format("%d", goal, grouping=True)))
self.lblRedacWC.setToolTip(self.tr("{} chars").format(
locale.format("%d", cc, grouping=True)))
else:
self.lblRedacProgress.hide()
self.lblRedacWC.setText(self.tr("({} chars) {} words ").format(
locale.format("%d", chars, grouping=True),
locale.format("%d", wc, grouping=True)))

if settings.progressChars:
self.lblRedacWC.setText(self.tr("{} chars ").format(
locale.format("%d", cc, grouping=True)))
self.lblRedacWC.setToolTip("")
else:
self.lblRedacWC.setText(self.tr("{} words ").format(
locale.format("%d", wc, grouping=True)))
self.lblRedacWC.setToolTip(self.tr("{} chars").format(
locale.format("%d", cc, grouping=True)))

###############################################################################
# VIEWS
Expand Down