Skip to content

Commit

Permalink
Settings (#3)
Browse files Browse the repository at this point in the history
* Add settings window

* Add tests
  • Loading branch information
yakimka authored Dec 14, 2019
1 parent 116e83e commit e1e61a6
Show file tree
Hide file tree
Showing 17 changed files with 672 additions and 113 deletions.
4 changes: 4 additions & 0 deletions CherryTomato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_DIR = os.path.join(BASE_DIR, 'media')
APP_ICON = os.path.join(MEDIA_DIR, 'icon.png')

VERSION = '0.2.0'
ORGANIZATION_NAME = 'CherryTomato'
APPLICATION_NAME = 'CherryTomato'
50 changes: 25 additions & 25 deletions CherryTomato/about_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,48 @@ def setupUi(self, About):
About.resize(402, 222)
self.verticalLayout = QtWidgets.QVBoxLayout(About)
self.verticalLayout.setObjectName("verticalLayout")
self.label_title = QtWidgets.QLabel(About)
self.labelTitle = QtWidgets.QLabel(About)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_title.sizePolicy().hasHeightForWidth())
self.label_title.setSizePolicy(sizePolicy)
sizePolicy.setHeightForWidth(self.labelTitle.sizePolicy().hasHeightForWidth())
self.labelTitle.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.label_title.setFont(font)
self.label_title.setText("CherryTomato")
self.label_title.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
self.label_title.setObjectName("label_title")
self.verticalLayout.addWidget(self.label_title)
self.label_text = QtWidgets.QLabel(About)
self.labelTitle.setFont(font)
self.labelTitle.setText("CherryTomato")
self.labelTitle.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
self.labelTitle.setObjectName("labelTitle")
self.verticalLayout.addWidget(self.labelTitle)
self.labelText = QtWidgets.QLabel(About)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.label_text.sizePolicy().hasHeightForWidth())
self.label_text.setSizePolicy(sizePolicy)
self.label_text.setTextFormat(QtCore.Qt.RichText)
self.label_text.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.label_text.setWordWrap(True)
self.label_text.setOpenExternalLinks(True)
self.label_text.setObjectName("label_text")
self.verticalLayout.addWidget(self.label_text)
self.label_copyright = QtWidgets.QLabel(About)
sizePolicy.setHeightForWidth(self.labelText.sizePolicy().hasHeightForWidth())
self.labelText.setSizePolicy(sizePolicy)
self.labelText.setTextFormat(QtCore.Qt.RichText)
self.labelText.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.labelText.setWordWrap(True)
self.labelText.setOpenExternalLinks(True)
self.labelText.setObjectName("labelText")
self.verticalLayout.addWidget(self.labelText)
self.labelCopyright = QtWidgets.QLabel(About)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_copyright.sizePolicy().hasHeightForWidth())
self.label_copyright.setSizePolicy(sizePolicy)
self.label_copyright.setText("Copyright © 2019 yakimka")
self.label_copyright.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft)
self.label_copyright.setObjectName("label_copyright")
self.verticalLayout.addWidget(self.label_copyright)
sizePolicy.setHeightForWidth(self.labelCopyright.sizePolicy().hasHeightForWidth())
self.labelCopyright.setSizePolicy(sizePolicy)
self.labelCopyright.setText("Copyright © 2019 yakimka")
self.labelCopyright.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft)
self.labelCopyright.setObjectName("labelCopyright")
self.verticalLayout.addWidget(self.labelCopyright)

self.retranslateUi(About)
QtCore.QMetaObject.connectSlotsByName(About)

def retranslateUi(self, About):
_translate = QtCore.QCoreApplication.translate
About.setWindowTitle(_translate("About", "About CherryTomato"))
self.label_text.setText(_translate("About", "<html><head/><body><p>CherryTomato is a simple tomato timer app.</p><p>It\'s written in Python 3 using PyQt5.</p><p>Project page: <a href=\"https://github.com/yakimka/CherryTomato\"><span style=\" text-decoration: underline; color:#4877b1;\">https://github.com/yakimka/CherryTomato</span></a></p></body></html>"))
self.labelText.setText(_translate("About", "<html><head/><body><p>CherryTomato is a simple tomato timer app.</p><p>It\'s written in Python 3 using PyQt5.</p><p>Project page: <a href=\"https://github.com/yakimka/CherryTomato\"><span style=\" text-decoration: underline; color:#4877b1;\">https://github.com/yakimka/CherryTomato</span></a></p></body></html>"))
6 changes: 3 additions & 3 deletions CherryTomato/about_ui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_title">
<widget class="QLabel" name="labelTitle">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
Expand All @@ -38,7 +38,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="label_text">
<widget class="QLabel" name="labelText">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
Expand All @@ -63,7 +63,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="label_copyright">
<widget class="QLabel" name="labelCopyright">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
Expand Down
5 changes: 4 additions & 1 deletion CherryTomato/about.py → CherryTomato/about_window.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon

from CherryTomato import APP_ICON
from CherryTomato import APP_ICON, VERSION
from .about_ui import Ui_About


Expand All @@ -11,4 +11,7 @@ def __init__(self):

self.setupUi(self)

title = f'{self.labelTitle.text()} {VERSION}'
self.labelTitle.setText(title)

self.setWindowIcon(QIcon(APP_ICON))
9 changes: 7 additions & 2 deletions CherryTomato/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import sys

from PyQt5 import Qt
from PyQt5.QtCore import QCoreApplication

from CherryTomato.main_window import TomatoTimerWindow
from CherryTomato import ORGANIZATION_NAME, APPLICATION_NAME
from CherryTomato.main_window import CherryTomatoMainWindow

QCoreApplication.setApplicationName(ORGANIZATION_NAME)
QCoreApplication.setApplicationName(APPLICATION_NAME)

app = Qt.QApplication(sys.argv)

watch = TomatoTimerWindow()
watch = CherryTomatoMainWindow()
watch.show()

app.exec_()
4 changes: 4 additions & 0 deletions CherryTomato/main_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def setupUi(self, MainWindow):
MainWindow.setMenuBar(self.menuBar)
self.actionAbout = QtWidgets.QAction(MainWindow)
self.actionAbout.setObjectName("actionAbout")
self.actionSettings = QtWidgets.QAction(MainWindow)
self.actionSettings.setObjectName("actionSettings")
self.menuFile.addAction(self.actionSettings)
self.menuFile.addAction(self.actionAbout)
self.menuBar.addAction(self.menuFile.menuAction())

Expand All @@ -64,4 +67,5 @@ def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "CherryTomato"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionAbout.setText(_translate("MainWindow", "About"))
self.actionSettings.setText(_translate("MainWindow", "Settings"))
from CherryTomato.widget import QRoundProgressBar, QRoundPushbutton
6 changes: 6 additions & 0 deletions CherryTomato/main_ui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ border-radius: 20px;</string>
<property name="title">
<string>File</string>
</property>
<addaction name="actionSettings"/>
<addaction name="actionAbout"/>
</widget>
<addaction name="menuFile"/>
Expand All @@ -108,6 +109,11 @@ border-radius: 20px;</string>
<string>About</string>
</property>
</action>
<action name="actionSettings">
<property name="text">
<string>Settings</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
78 changes: 47 additions & 31 deletions CherryTomato/main_window.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import os

from PyQt5 import Qt, QtCore
from PyQt5.QtCore import QSettings, QSize, QPoint
from PyQt5.QtGui import QBrush, QColor, QPalette, QIcon
from PyQt5.QtMultimedia import QSound

from CherryTomato import about, APP_ICON, MEDIA_DIR
from CherryTomato import about_window, APP_ICON, MEDIA_DIR, settings_window
from CherryTomato.main_ui import Ui_MainWindow
from CherryTomato.settings import CherryTomatoSettings
from CherryTomato.tomato_timer import TomatoTimer


class TomatoTimerWindow(Qt.QMainWindow, Ui_MainWindow):
APP_TITLE = 'CherryTomato'
NOTIFICATION_ON = True

class CherryTomatoMainWindow(Qt.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)

self.setupUi(self)

self.setWindowIcon(QIcon(APP_ICON))

self.settings = QSettings('yakimka', self.APP_TITLE)
# Initial window size/pos last saved. Use default values for first time
self.resize(self.settings.value('size', QSize(400, 520)))
self.move(self.settings.value('pos', QPoint(50, 50)))
self.settings = CherryTomatoSettings()
self.setWindowSizeAndPosition()

self.tomatoTimer = TomatoTimer()

Expand All @@ -34,8 +29,27 @@ def __init__(self, parent=None):

self.display()

self.aboutWindow = about.About()
self.aboutWindow = about_window.About()
self.actionAbout.triggered.connect(self.showAboutWindow)
self.settingsWindow = settings_window.Settings()
self.actionSettings.triggered.connect(self.showSettingsWindow)
self.settingsWindow.closing.connect(self.update)

def update(self):
self.tomatoTimer.updateState()

def setWindowSizeAndPosition(self):
# Initial window size/pos last saved. Use default values for first time
while True:
try:
self.resize(self.settings.size)
self.move(self.settings.position)
except TypeError:
del self.settings.size
del self.settings.position
continue
else:
break

def showAboutWindow(self):
centerX, centerY = self.getCenterPoint()
Expand All @@ -44,6 +58,13 @@ def showAboutWindow(self):
self.aboutWindow.move(x, y)
self.aboutWindow.show()

def showSettingsWindow(self):
centerX, centerY = self.getCenterPoint()
x = int(centerX - self.settingsWindow.width() / 2)
y = int(centerY - self.settingsWindow.height() / 2)
self.settingsWindow.move(x, y)
self.settingsWindow.show()

def getCenterPoint(self):
x = int(self.x() + self.width() / 2)
y = int(self.y() + self.height() / 2)
Expand All @@ -52,8 +73,8 @@ def getCenterPoint(self):

def closeEvent(self, e):
# Write window size and position to config file
self.settings.setValue('size', self.size())
self.settings.setValue('pos', self.pos())
self.settings.size = self.size()
self.settings.position = self.pos()

e.accept()

Expand All @@ -74,22 +95,16 @@ def display(self):
@Qt.pyqtSlot()
def do_start(self):
# trigger through proxy
self.tomatoTimer.start()

@Qt.pyqtSlot()
def do_stop(self):
# trigger through proxy
self.tomatoTimer.abort()
if not self.tomatoTimer.running:
self.tomatoTimer.start()
else:
self.tomatoTimer.abort()

def changeButtonState(self):
if not self.tomatoTimer.running:
self.button.setImage('play.png')
self.button.clicked.disconnect()
self.button.clicked.connect(self.do_start)
else:
self.button.setImage('stop.png')
self.button.clicked.disconnect()
self.button.clicked.connect(self.do_stop)

def setRed(self):
# https://coolors.co/eff0f1-d11f2a-8da1b9-95adb6-fad0cf
Expand Down Expand Up @@ -130,12 +145,13 @@ def keyPressEvent(self, event):

@Qt.pyqtSlot()
def setFocusOnWindowAndPlayNotification(self):
self.raise_()
self.show()
self.activateWindow()
if self.windowState() == QtCore.Qt.WindowMinimized:
# Window is minimised. Restore it.
self.setWindowState(QtCore.Qt.WindowNoState)

if self.NOTIFICATION_ON:
if self.settings.interrupt:
self.raise_()
self.show()
self.activateWindow()
if self.windowState() == QtCore.Qt.WindowMinimized:
# Window is minimised. Restore it.
self.setWindowState(QtCore.Qt.WindowNoState)

if self.settings.notification:
QSound.play(os.path.join(MEDIA_DIR, 'sound.wav'))
Loading

0 comments on commit e1e61a6

Please sign in to comment.