Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
soitool
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bachelor-paa-bittet
soitool
Commits
8aa39a2e
Commit
8aa39a2e
authored
5 years ago
by
Anders H. Rebner
Browse files
Options
Downloads
Patches
Plain Diff
#23 Widgetstørrelse endrer seg nå dynamisk etter innhold
parent
786f6144
No related branches found
Branches containing commit
No related tags found
1 merge request
!8
ModuleTable og ModuleBase
Pipeline
#71228
failed
5 years ago
Stage: lint
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
soitool/modules/module_table.py
+53
-15
53 additions, 15 deletions
soitool/modules/module_table.py
with
53 additions
and
15 deletions
soitool/modules/module_table.py
+
53
−
15
View file @
8aa39a2e
"""
Module containing subclassed SOIModule (QTableWidget, ModuleBase).
"""
from
PySide2.QtWidgets
import
QTableWidget
,
QTableWidgetItem
,
QShortcut
from
PySide2
import
QtGui
from
PySide2
import
QtGui
,
QtCore
from
module_base
import
ModuleBase
HEADER_FONT
=
QtGui
.
QFont
()
...
...
@@ -8,6 +8,9 @@ HEADER_FONT.setFamily('Arial')
HEADER_FONT
.
setPointSize
(
12
)
HEADER_FONT
.
setWeight
(
100
)
START_COLUMNS
=
2
START_ROWS
=
2
class
Meta
(
type
(
ModuleBase
),
type
(
QTableWidget
)):
"""
Used as a metaclass to enable multiple inheritance.
"""
...
...
@@ -19,6 +22,9 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
Has shortcuts for adding and removing rows and columns.
Inherits from ModuleBase and QTableWidget.
Functions inherited from ModuleBase are overridden by this class.
Has header-styled first row.
Functionality for adding and removing columns and rows is implemented.
"""
def
__init__
(
self
):
...
...
@@ -26,19 +32,28 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
QTableWidget
.
__init__
(
self
)
super
(
QTableWidget
)
# Remove headers
self
.
horizontalHeader
().
hide
()
self
.
verticalHeader
().
hide
()
self
.
setColumnCount
(
2
)
self
.
setRowCount
(
2
)
# Set number of columns and rows
self
.
setColumnCount
(
START_COLUMNS
)
self
.
setRowCount
(
START_ROWS
)
self
.
set_header_item
(
0
,
""
)
self
.
set_header_item
(
1
,
""
)
# Resize width and height of columns and rows, and set size of window
self
.
resize
()
self
.
setFixedWidth
(
START_COLUMNS
*
self
.
columnWidth
(
0
)
+
2
)
self
.
setFixedHeight
(
START_ROWS
*
self
.
rowHeight
(
0
)
+
5
)
self
.
resizeRowsToContents
()
self
.
resizeColumnsToContents
()
# Remove scrollbars
self
.
setHorizontalScrollBarPolicy
(
QtCore
.
Qt
.
ScrollBarAlwaysOff
)
self
.
setVerticalScrollBarPolicy
(
QtCore
.
Qt
.
ScrollBarAlwaysOff
)
self
.
cellChanged
.
connect
(
self
.
cell_changed
)
# Set headers
for
i
in
range
(
self
.
columnCount
()):
self
.
set_header_item
(
i
,
""
)
self
.
cellChanged
.
connect
(
self
.
resize
)
self
.
set_shortcuts
()
...
...
@@ -61,11 +76,13 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
def
set_shortcuts
(
self
):
"""
Set shortcuts for adding and removing rows and columns.
"""
# Create shortcuts
shortcut_add_col
=
QShortcut
(
QtGui
.
QKeySequence
(
"
Shift++
"
),
self
)
shortcut_rem_col
=
QShortcut
(
QtGui
.
QKeySequence
(
"
Shift+-
"
),
self
)
shortcut_add_row
=
QShortcut
(
QtGui
.
QKeySequence
(
"
Ctrl++
"
),
self
)
shortcut_rem_row
=
QShortcut
(
QtGui
.
QKeySequence
(
"
Ctrl+-
"
),
self
)
# Connect shortcuts to functions
shortcut_add_col
.
activated
.
connect
(
self
.
add_column
)
shortcut_rem_col
.
activated
.
connect
(
self
.
remove_column
)
shortcut_add_row
.
activated
.
connect
(
self
.
add_row
)
...
...
@@ -75,28 +92,48 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
"""
Add column to the right of selected column.
"""
self
.
insertColumn
(
self
.
currentColumn
()
+
1
)
self
.
set_header_item
(
self
.
currentColumn
()
+
1
,
""
)
self
.
resize
ColumnsToContents
()
self
.
resize
()
def
remove_column
(
self
):
"""
Remove selected column if two or more columns exist.
"""
if
self
.
columnCount
()
>
1
:
self
.
removeColumn
(
self
.
currentColumn
())
self
.
resize
()
def
add_row
(
self
):
"""
Add row below selected row.
"""
self
.
insertRow
(
self
.
currentRow
()
+
1
)
self
.
resize
RowsToContents
()
self
.
resize
()
def
remove_row
(
self
):
"""
Remove selected row if two or more rows exist (including header).
"""
if
self
.
rowCount
()
>
2
:
if
self
.
rowCount
()
>
2
and
self
.
currentRow
()
!=
0
:
self
.
removeRow
(
self
.
currentRow
())
self
.
resize
()
def
cell_changed
(
self
):
"""
Resize rows and columns to contents when a cell changes.
"""
def
resize
(
self
):
"""
Resize widget, rows and columns.
Resize widget size to total width and height of rows and columns.
Resize rows and columns to contents.
"""
self
.
resizeColumnsToContents
()
self
.
resizeRowsToContents
()
# Calculate total width and height of columns and rows
width
=
0
height
=
0
for
x
in
range
(
self
.
columnCount
()):
width
+=
self
.
columnWidth
(
x
)
+
0.5
for
y
in
range
(
self
.
rowCount
()):
height
+=
self
.
rowHeight
(
y
)
+
0.5
# Set total width and height
self
.
setFixedWidth
(
width
)
self
.
setFixedHeight
(
height
)
def
get_size
(
self
):
"""
Get size of widget.
...
...
@@ -105,14 +142,15 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
Tuple
(width, height) (total)
"""
# Calculate total width and height of columns and rows
width
=
0
height
=
0
for
i
in
range
(
self
.
columnCount
()):
width
+=
self
.
columnWidth
(
i
)
width
+=
self
.
columnWidth
(
i
)
+
0.5
for
i
in
range
(
self
.
columnCount
()):
height
+=
self
.
rowHeight
(
i
)
height
+=
self
.
rowHeight
(
i
)
+
0.5
return
width
,
height
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment