Skip to content

Commit

Permalink
0.5.1 forms as groups
Browse files Browse the repository at this point in the history
  • Loading branch information
danbradham committed Aug 15, 2017
1 parent 61b4760 commit 4bc489f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion psforms/__init__.py
Expand Up @@ -4,7 +4,7 @@
__author__ = 'Dan Bradham'
__email__ = 'danielbradham@gmail.com'
__url__ = 'http://github.com/danbradham/psforms.git'
__version__ = '0.5.0'
__version__ = '0.5.1'
__license__ = 'MIT'
__description__ = 'Hassle free PySide forms.'

Expand Down
14 changes: 12 additions & 2 deletions psforms/form.py
Expand Up @@ -6,7 +6,7 @@
from Qt import QtWidgets, QtCore, QtGui

from .fields import FieldType
from .widgets import FormDialog, FormWidget
from .widgets import FormDialog, FormWidget, FormGroup
from .utils import Ordered, itemattrgetter


Expand All @@ -21,6 +21,7 @@ class FormMetaData(object):
labeled=True,
labels_on_top=True,
layout_horizontal=False,
subforms_as_groups=False,
)

def __init__(self, **kwargs):
Expand Down Expand Up @@ -101,10 +102,19 @@ def as_widget(cls, parent=None):
form_widget.add_control(name, control)

for name, form in cls.forms():
form_widget.add_form(name, form.as_widget(form_widget))
if cls.meta.subforms_as_groups:
form_widget.add_form(name, form.as_group(form_widget))
else:
form_widget.add_form(name, form.as_widget(form_widget))

return form_widget

@classmethod
def as_group(cls, parent=None):

group = FormGroup(cls.as_widget(), parent=parent)
return group

@classmethod
def as_dialog(cls, frameless=False, dim=False, parent=None):
'''Get this form as a dialog'''
Expand Down
22 changes: 22 additions & 0 deletions psforms/style.css
Expand Up @@ -50,6 +50,7 @@ QPushButton[browse='true']{
border-bottom-left-radius: 0;
}


QPushButton:hover{
background: rgb(75, 229, 160);
}
Expand All @@ -64,6 +65,27 @@ QPushButton:pressed{
margin-top: 3;
}

QPushButton[grouptitle='true']{
background: rgb(235, 235, 235);
color: rgb(45, 45, 45);
border: 0;
margin: 0;
padding: 0;
padding-left: 10px;
text-align: left;
}

QPushButton[grouptitle='true']:hover{
background: rgb(215, 215, 215);
border-radius: 0;
}

QPushButton[grouptitle='true']:pressed{}

QWidget[groupwidget='true']{
background: rgb(215, 215, 215);
}

QComboBox {
background: rgb(255, 255, 255);
border-radius: 3;
Expand Down
50 changes: 47 additions & 3 deletions psforms/widgets.py
Expand Up @@ -185,7 +185,7 @@ def __init__(self, widget, *args, **kwargs):

self.layout = QtWidgets.QGridLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setRowStretch(0, 1)
self.layout.setRowStretch(1, 1)
self.setLayout(self.layout)

self.button_layout = QtWidgets.QHBoxLayout()
Expand All @@ -194,7 +194,7 @@ def __init__(self, widget, *args, **kwargs):
self.button_layout.addWidget(self.accept_button)

self.layout.addWidget(self.widget, 0, 0)
self.layout.addLayout(self.button_layout, 1, 0)
self.layout.addLayout(self.button_layout, 2, 0)

def __getattr__(self, attr):
try:
Expand All @@ -208,6 +208,50 @@ def on_accept(self):
return


class FormGroup(QtWidgets.QWidget):

def __init__(self, widget, *args, **kwargs):
super(FormGroup, self).__init__(*args, **kwargs)

self.widget = widget
self.widget.setProperty('groupwidget', True)

self.layout = QtWidgets.QVBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setStretch(1, 0)
self.layout.setSpacing(0)
self.setLayout(self.layout)

self.title = QtWidgets.QPushButton(self.widget.name)
icon = QtGui.QIcon()
icon.addPixmap(
QtGui.QPixmap(':/icons/plus'),
QtGui.QIcon.Normal,
QtGui.QIcon.Off
)
icon.addPixmap(
QtGui.QPixmap(':/icons/minus'),
QtGui.QIcon.Normal,
QtGui.QIcon.On
)
self.title.setIcon(icon)
self.title.setProperty('grouptitle', True)
self.title.setCheckable(True)
self.title.setChecked(True)
self.title.toggled.connect(self.toggle_collapsed)
self.layout.addWidget(self.title)
self.layout.addWidget(self.widget)

def toggle_collapsed(self, collapsed):
self.widget.setVisible(collapsed)

def __getattr__(self, attr):
try:
return getattr(self.widget, attr)
except AttributeError:
raise AttributeError('FormDialog has no attr: {}'.format(attr))


class Header(QtWidgets.QWidget):

def __init__(self, title, description=None, icon=None, parent=None):
Expand Down Expand Up @@ -312,7 +356,7 @@ def paintEvent(self, event):

offsetX = -((self.pixmap.width() - self.width())*0.5)
offsetY = -((self.pixmap.height() - self.height())*0.5)
painter = QtWidgets.QPainter(self)
painter = QtGui.QPainter(self)
painter.drawPixmap(offsetX, offsetY, self.pixmap)


Expand Down
3 changes: 2 additions & 1 deletion pslive.py
Expand Up @@ -70,7 +70,8 @@ def css_changed(self, path):

widgets = self.path_mapping[path]
with open(path) as f:
style = f.read()
for widget in widgets:
widget.setStyleSheet(f.read())
widget.setStyleSheet(style)
widget.style().unpolish(widget)
widget.style().polish(widget)
1 change: 1 addition & 0 deletions test.py
Expand Up @@ -77,6 +77,7 @@ class VisualTestForm(Form):
title='Visual Test Form',
description='Stylsheet + widget tests',
header=True,
subforms_as_groups=True
)

image_form = ImageTestForm()
Expand Down

0 comments on commit 4bc489f

Please sign in to comment.