Skip to content

Commit

Permalink
Move author's dialog to plugin_model
Browse files Browse the repository at this point in the history
This is done in response to the finding that the delegates are executed outside of the built context of the python application.
  • Loading branch information
podestplatz committed Aug 3, 2019
1 parent 185fe50 commit d25fdd1
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 51 deletions.
51 changes: 6 additions & 45 deletions bcfplugin/gui/plugin_delegate.py
@@ -1,49 +1,15 @@
from copy import copy
from bcfplugin import TMPDIR
import bcfplugin.util as util

from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import (QModelIndex, Slot, QSize, QPoint, Signal, Qt, QRect)


emailRegex = "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)|(^\s*$)"
commentRegex = "[a-zA-Z0-9.,\-\/ ]* -- {}".format(emailRegex)
dueDateRegex = "\d{4}-[01]\d-[0-3]\d"

authorsDialog = None
authorsLineEdit = None
@Slot()
def setAuthor():

global authorsDialog
global authorsLineEdit

author = authorsLineEdit.text()
util.setAuthor(author)
authorsDialog.close()


def openAuthorsDialog(parent):

global authorsDialog
global authorsLineEdit

authorsDialog = QDialog(parent)
authorsDialog.setWindowTitle("Enter your e-Mail")

form = QFormLayout()
emailValidator = QRegExpValidator()
emailValidator.setRegExp(emailRegex)

authorsLineEdit = QLineEdit(parent)
authorsLineEdit.setValidator(emailValidator)
authorsLineEdit.editingFinished.connect(setAuthor)

form.addRow("E-Mail:", authorsLineEdit)
authorsDialog.setLayout(form)
authorsDialog.setModal(True)
authorsDialog.exec()

author = ""
""" Holds the entered email address of the author """

class CommentDelegate(QStyledItemDelegate):

Expand All @@ -56,7 +22,7 @@ def __init__(self, parent = None):
self.updateFonts(self.commentFont)
self.widgetWidth = 0

self._commentYOffset = 2 #TODO relate it to the actual screensize
self._commentYOffset = 2
self._commentYQOffset = None
self._commentXOffset = 2
self._commentXQOffset = None
Expand Down Expand Up @@ -122,14 +88,9 @@ def createEditor(self, parent, option, index):

""" Makes the comment and the author available in a QLineEdit """

modAuthor = ""
if util.isAuthorSet():
modAuthor = util.getAuthor()
else:
openAuthorsDialog(None)
modAuthor = util.getAuthor()
global author

util.debug("The email you entered is: {}".format(modAuthor))
author = index.model().getAuthor()

comment = index.model().data(index, Qt.EditRole)
editor = QLineEdit(comment[0], parent)
Expand Down
54 changes: 54 additions & 0 deletions bcfplugin/gui/plugin_model.py
Expand Up @@ -14,6 +14,45 @@
from bcfplugin.rdwr.markup import Comment
from bcfplugin.frontend.viewController import CamType

emailRegex = "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)|(^\s*$)"

authorsDialog = None
authorsLineEdit = None
@Slot()
def setAuthor():

global authorsDialog
global authorsLineEdit

author = authorsLineEdit.text()
authorsDialog.author = author
authorsDialog.done(0)


def openAuthorsDialog(parent):

global authorsDialog
global authorsLineEdit

authorsDialog = QDialog(parent)
authorsDialog.setWindowTitle("Enter your e-Mail")

form = QFormLayout()
emailValidator = QRegExpValidator()
emailValidator.setRegExp(emailRegex)

authorsLineEdit = QLineEdit(parent)
authorsLineEdit.setValidator(emailValidator)
authorsLineEdit.editingFinished.connect(setAuthor)

form.addRow("E-Mail:", authorsLineEdit)
authorsDialog.setLayout(form)
authorsDialog.exec()

author = authorsDialog.author
util.debug("We got something very nice {}".format(author))
util.setAuthor(author)


def openProjectBtnHandler(file):

Expand Down Expand Up @@ -283,6 +322,21 @@ def referencedViewpoint(self, index):
return self.items[index.row()].viewpoint


def getAuthor(self):

util.debug("This is the current temp directory:"\
" {}".format(util.getSystemTmp()))

modAuthor = None
if util.isAuthorSet():
modAuthor = util.getAuthor()
else:
openAuthorsDialog(None)
modAuthor = util.getAuthor()

return modAuthor


class SnapshotModel(QAbstractListModel):


Expand Down
6 changes: 6 additions & 0 deletions bcfplugin/gui/plugin_panel.py
Expand Up @@ -25,6 +25,7 @@ def __init__(self):

self.running = True
self.form = view.MyMainWindow()
self.form.resize(self.form.geometry().width(), self.form.geometry().height())
self.form.setObjectName("BCFPlugin")


Expand Down Expand Up @@ -68,6 +69,11 @@ def getStandardButtons(self):
return int(QDialogButtonBox.Close)


def close(self):

self.form.closeEvent()


def reject(self):

""" Called when the 'Close' button of the task dialog is pressed,
Expand Down
36 changes: 30 additions & 6 deletions bcfplugin/gui/plugin_view.py
Expand Up @@ -59,9 +59,9 @@ def mouseEntered(self, index):

itemRect = self.rectForIndex(index)
x = itemRect.width() - deleteButton.geometry().width()
y = itemRect.y() + (itemRect.height() -
vOffset = self.verticalOffset() # scroll offset
y = itemRect.y() - vOffset + (itemRect.height() -
deleteButton.geometry().height()) / 2
util.debug("DeleteButton y start: {}".format(y))
deleteButton.move(x, y)

deleteButton.show()
Expand Down Expand Up @@ -265,6 +265,9 @@ def __init__(self):
self.viewpointList.activateViewpoint(x, self.viewpointResetBtn))
self.viewpointResetBtn.clicked.connect(self.viewpointsModel.resetView)
self.viewpointResetBtn.clicked.connect(self.viewpointResetBtn.hide)
# delete delete button if the view is scrolled
self.commentList.verticalScrollBar().valueChanged.connect(lambda x:
self.commentList.deleteDelBtn())

self.setLayout(self.mainLayout)

Expand Down Expand Up @@ -345,8 +348,6 @@ def createCommentGroup(self):
self.commentLayout.addWidget(self.commentList)

self.commentPlaceholder = self.tr("Enter a new comment here")
self.commentValidator = QRegExpValidator()
self.commentValidator.setRegExp(delegate.commentRegex)
self.newCommentEdit = QLineEdit()
self.newCommentEdit.returnPressed.connect(self.checkAndAddComment)
self.newCommentEdit.setPlaceholderText(self.commentPlaceholder)
Expand Down Expand Up @@ -415,7 +416,6 @@ def openProjectBtnHandler(self):
@Slot()
def checkAndAddComment(self):

util.debug("Pressed enter on the input")
editor = self.newCommentEdit
text = editor.text()

Expand Down Expand Up @@ -446,7 +446,7 @@ def saveProjectHandler(self):
dflPath = self.openFilePath
filename = QFileDialog.getSaveFileName(self, self.tr("Save BCF File"),
dflPath, self.tr("BCF Files (*.bcf *.bcfzip)"))
if filename != "":
if filename[0] != "":
util.debug("Got a file to write to: {}.".format(filename))
model.saveProject(filename[0])

Expand Down Expand Up @@ -487,6 +487,30 @@ def showTopicMetrics(self):
metricsWindow.show()


def closeEvent(self):

self.showExitSaveDialog()
util.debug("Deleting temporary directory {}".format(util.getSystemTmp()))
util.deleteTmp()


def showExitSaveDialog(self):

dialog = QDialog(self)
buttons = QDialogButtonBox(QDialogButtonBox.Save |
QDialogButtonBox.Close, dialog)
label = QLabel(self.tr("Save before closing?"))

layout = QVBoxLayout(dialog)
layout.addWidget(label)
layout.addWidget(buttons)
dialog.setLayout(layout)

buttons.accepted.connect(self.saveProjectHandler)
buttons.rejected.connect(lambda: dialog.done(0))
dialog.exec()


if __name__ == "__main__":
app = QApplication(sys.argv)

Expand Down
12 changes: 12 additions & 0 deletions bcfplugin/util.py
Expand Up @@ -101,6 +101,18 @@ def getSystemTmp(createNew: bool = False):
return tmpDir.name


def deleteTmp():

""" Delete the temporary directory with all its contents """

global tmpDir

if tmpDir is not None:
print("deleting temporary directory {}".format(tmpDir.name))
del tmpDir
tmpDir = None


def printErr(msg, toFile=False):

""" Print msg to stderr """
Expand Down

0 comments on commit d25fdd1

Please sign in to comment.