manuskript/manuskript/functions.py

222 lines
5 KiB
Python
Raw Normal View History

2015-06-05 06:22:37 +12:00
#!/usr/bin/env python
#--!-- coding: utf8 --!--
2015-06-19 01:03:16 +12:00
import os
2016-02-07 00:34:22 +13:00
from random import *
2016-03-25 01:42:47 +13:00
from PyQt5.QtCore import Qt, QRect, QStandardPaths, QObject, QRegExp
# Used to detect multiple connections
2016-02-07 00:34:22 +13:00
from PyQt5.QtGui import QBrush, QIcon, QPainter
from PyQt5.QtGui import QColor
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import qApp
from manuskript.enums import Outline
AUC = Qt.AutoConnection | Qt.UniqueConnection
2015-07-03 23:59:41 +12:00
MW = None
2016-03-25 01:42:47 +13:00
2015-06-05 06:22:37 +12:00
def wordCount(text):
return len(text.strip().replace(" ", "\n").split("\n")) if text else 0
2015-06-05 21:37:01 +12:00
2016-03-25 01:42:47 +13:00
2015-06-05 21:37:01 +12:00
def toInt(text):
if text:
return int(text)
else:
return 0
2016-03-25 01:42:47 +13:00
2015-06-05 21:37:01 +12:00
def toFloat(text):
if text:
return float(text)
else:
return 0.
2016-03-25 01:42:47 +13:00
def toString(text):
2015-06-15 22:18:42 +12:00
if text in [None, "None"]:
return ""
else:
return str(text)
2016-03-25 01:42:47 +13:00
def drawProgress(painter, rect, progress, radius=0):
painter.setPen(Qt.NoPen)
painter.setBrush(QColor("#dddddd"))
painter.drawRoundedRect(rect, radius, radius)
2015-06-16 09:15:10 +12:00
painter.setBrush(QBrush(colorFromProgress(progress)))
r2 = QRect(rect)
r2.setWidth(r2.width() * min(progress, 1))
painter.drawRoundedRect(r2, radius, radius)
2016-03-25 01:42:47 +13:00
2015-06-16 09:15:10 +12:00
def colorFromProgress(progress):
progress = toFloat(progress)
c1 = QColor(Qt.red)
c2 = QColor(Qt.blue)
c3 = QColor(Qt.darkGreen)
c4 = QColor("#FFA500")
if progress < 0.3:
2015-06-16 09:15:10 +12:00
return c1
elif progress < 0.8:
2015-06-16 09:15:10 +12:00
return c2
elif progress > 1.2:
2015-06-16 09:15:10 +12:00
return c4
else:
2015-06-16 09:15:10 +12:00
return c3
2016-03-25 01:42:47 +13:00
def mainWindow():
2015-07-03 23:59:41 +12:00
global MW
if not MW:
for i in qApp.topLevelWidgets():
if i.objectName() == "MainWindow":
MW = i
return MW
return None
else:
return MW
2015-06-10 08:05:03 +12:00
2016-03-25 01:42:47 +13:00
2015-06-10 08:05:03 +12:00
def iconColor(icon):
2016-02-07 00:34:22 +13:00
"""Returns a QRgb from a QIcon, assuming its all the same color"""
2015-06-16 02:35:23 +12:00
px = icon.pixmap(5, 5)
if px.width() != 0:
return QColor(QImage(px).pixel(2, 2))
else:
2015-06-18 03:15:13 +12:00
return QColor(Qt.transparent)
2016-03-25 01:42:47 +13:00
def iconFromColor(color):
px = QPixmap(32, 32)
px.fill(color)
return QIcon(px)
2016-03-25 01:42:47 +13:00
def iconFromColorString(string):
2015-06-16 03:24:02 +12:00
return iconFromColor(QColor(string))
2016-03-25 01:42:47 +13:00
2015-06-16 03:24:02 +12:00
def randomColor(mix=None):
2016-02-07 00:34:22 +13:00
"""Generates a random color. If mix (QColor) is given, mixes the random color and mix."""
2015-06-16 03:24:02 +12:00
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
if mix:
r = (r + mix.red()) / 2
g = (g + mix.green()) / 2
b = (b + mix.blue()) / 2
2015-06-16 09:15:10 +12:00
return QColor(r, g, b)
2016-03-25 01:42:47 +13:00
2015-06-16 09:15:10 +12:00
def mixColors(col1, col2, f=.5):
f2 = 1-f
r = col1.red() * f + col2.red() * f2
g = col1.green() * f + col2.green() * f2
b = col1.blue() * f + col2.blue() * f2
return QColor(r, g, b)
2016-03-25 01:42:47 +13:00
2015-06-16 09:15:10 +12:00
def outlineItemColors(item):
2016-02-07 00:34:22 +13:00
"""Takes an OutlineItem and returns a dict of colors."""
colors = {}
mw = mainWindow()
# POV
colors["POV"] = QColor(Qt.transparent)
POV = item.data(Outline.POV.value)
for i in range(mw.mdlCharacter.rowCount()):
if mw.mdlCharacter.ID(i) == POV:
colors["POV"] = iconColor(mw.mdlCharacter.icon(i))
2016-02-07 00:34:22 +13:00
# Label
lbl = item.data(Outline.label.value)
col = iconColor(mw.mdlLabels.item(toInt(lbl)).icon())
if col == Qt.black:
# Don't know why, but transparent is rendered as black
col = QColor(Qt.transparent)
colors["Label"] = col
# Progress
pg = item.data(Outline.goalPercentage.value)
colors["Progress"] = colorFromProgress(pg)
# Compile
if item.compile() in [0, "0"]:
colors["Compile"] = QColor(Qt.gray)
else:
colors["Compile"] = QColor(Qt.black)
return colors
2016-03-25 01:42:47 +13:00
2015-06-16 09:15:10 +12:00
def colorifyPixmap(pixmap, color):
# FIXME: ugly
p = QPainter(pixmap)
p.setCompositionMode(p.CompositionMode_Overlay)
p.fillRect(pixmap.rect(), color)
2015-06-19 01:03:16 +12:00
return pixmap
2016-03-25 01:42:47 +13:00
2015-06-19 07:28:47 +12:00
def appPath(suffix=None):
p = os.path.realpath(os.path.join(os.path.split(__file__)[0], ".."))
if suffix:
p = os.path.join(p, suffix)
return p
2015-06-19 01:03:16 +12:00
2016-03-25 01:42:47 +13:00
2015-06-19 07:28:47 +12:00
def writablePath(suffix=None):
2015-06-21 02:45:54 +12:00
if hasattr(QStandardPaths, "AppLocalDataLocation"):
p = QStandardPaths.writableLocation(QStandardPaths.AppLocalDataLocation)
else:
# Qt < 5.4
p = QStandardPaths.writableLocation(QStandardPaths.DataLocation)
2015-06-19 07:28:47 +12:00
if suffix:
p = os.path.join(p, suffix)
2015-06-21 02:45:54 +12:00
if not os.path.exists(p):
os.makedirs(p)
2015-06-19 07:28:47 +12:00
return p
2015-06-19 01:03:16 +12:00
2016-03-25 01:42:47 +13:00
2015-06-19 01:03:16 +12:00
def allPaths(suffix=None):
paths = []
# src directory
2015-06-19 07:28:47 +12:00
paths.append(appPath(suffix))
2015-06-19 01:03:16 +12:00
# user writable directory
2015-06-19 07:28:47 +12:00
paths.append(writablePath(suffix))
return paths
2016-03-25 01:42:47 +13:00
def lightBlue():
"""
A light blue used in several places in manuskript.
@return: QColor
"""
2015-07-03 23:59:41 +12:00
return QColor(Qt.blue).lighter(190)
2016-03-25 01:42:47 +13:00
2015-07-03 23:59:41 +12:00
def totalObjects():
return len(mainWindow().findChildren(QObject))
2016-03-25 01:42:47 +13:00
2015-07-03 23:59:41 +12:00
def printObjects():
2016-03-25 01:42:47 +13:00
print("Objects:", str(totalObjects()))
def findWidgetsOfClass(cls):
"""
Returns all widgets, children of MainWindow, whose class is cls.
@param cls: a class
@return: list of QWidgets
"""
return mainWindow().findChildren(cls, QRegExp())