Completed
Push — master ( ac0a27...6f0f8d )
by Jan
04:44
created

ModuleConfigModel.columnCount()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
Qudi is free software: you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation, either version 3 of the License, or
6
(at your option) any later version.
7
8
Qudi is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
GNU General Public License for more details.
12
13
You should have received a copy of the GNU General Public License
14
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
15
16
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
17
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
18
"""
19
20
from qtpy import QtCore
21
22
class ModuleConfigModel(QtCore.QAbstractTableModel):
23
24
    def __init__(self, module):
25
        super().__init__()
26
        self.headers = ['Option', 'Value']
27
        self.storage = module.options
28
29
    def getKeyByNumber(self, n):
30
        """ Get a dict key by index number
31
32
            @param n int: index number for element
33
34
            @return key: key at index
35
        """
36
        i = 0
37
        if not(0 <= n < len(self.storage)):
38
            raise IndexError
39
        it = iter(self.storage)
40
        key = next(it)
41
        while(i<n):
42
            key = next(it)
43
            i += 1
44
        return key
45
46
    def getNumberByKey(self, key):
47
        """ Get index number for dict key.
48
49
            @param key: dict key
50
51
            @return int: index numer for key
52
53
            Warning: index number for a key changes when keys with lower numbers are removed.
54
        """
55
        i = 0
56
        it = iter(self.storage)
57
        newkey = next(it)
58
        while(key != newkey):
59
            newkey = next(it)
60
            i += 1
61
        return i
62
63
    def rowCount(self, parent = QtCore.QModelIndex()):
0 ignored issues
show
Unused Code introduced by
The argument parent seems to be unused.
Loading history...
64
        """ Gives the number of stored items.
65
66
          @return int: number of items
67
        """
68
        return len(self.storage)
69
70
    def columnCount(self, parent = QtCore.QModelIndex()):
0 ignored issues
show
Unused Code introduced by
The argument parent seems to be unused.
Loading history...
71
        """ Gives the number of data fields.
72
73
          @return int: number of data fields
74
        """
75
        return len(self.headers)
76
77
    def flags(self, index):
0 ignored issues
show
Unused Code introduced by
The argument index seems to be unused.
Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
78
        """ Determines what can be done with entry cells in the table view.
79
80
          @param QModelIndex index: cell fo which the flags are requested
81
82
          @return Qt.ItemFlags: actins allowed fotr this cell
83
        """
84
        return QtCore.Qt.ItemIsEnabled |  QtCore.Qt.ItemIsSelectable
85
86
    def data(self, index, role):
87
        """ Get data from model for a given cell. Data can have a role that affects display.
88
89
          @param QModelIndex index: cell for which data is requested
90
          @param ItemDataRole role: role for which data is requested
91
92
          @return QVariant: data for given cell and role
93
        """
94
        if not index.isValid():
95
            return None
96
        elif role == QtCore.Qt.DisplayRole:
97
            key = self.getKeyByNumber(index.row())
98
            if index.column() == 0:
99
                return self.storage[key].name
100
            elif index.column() == 1:
101
                return self.storage[key].default
102
            else:
103
                return None
104
        else:
105
            return None
106
107
    def headerData(self, section, orientation, role = QtCore.Qt.DisplayRole):
108
        """ Data for the table view headers.
109
110
          @param int section: number of the column to get header data for
111
          @param Qt.Orientation: orientation of header (horizontal or vertical)
112
          @param ItemDataRole: role for which to get data
113
114
          @return QVariant: header data for given column and role
115
        """
116
        if not(0 <= section < len(self.headers)):
117
            return None
118
        elif role != QtCore.Qt.DisplayRole:
119
            return None
120
        elif orientation != QtCore.Qt.Horizontal:
121
            return None
122
        else:
123
            return self.headers[section]
124
125