Ejemplo n.º 1
0
 def __setRuleData(self, column, content, value):
     """change rule data in the model"""
     # pylint:  disable=R0912
     # allow more than 12 branches
     dirty, message = False, None
     if column == 0:
         name = unicode(value.toString())
         if content.name != english(name):
             dirty = True
             content.name = english(name)
     elif column == 1 and content.parType:
         oldParameter = content.parameter
         if content.parType is int:
             if content.parameter != value.toInt()[0]:
                 dirty = True
                 content.parameter = value.toInt()[0]
         elif content.parType is bool:
             return False
         elif content.parType is unicode:
             if content.parameter != unicode(value.toString()):
                 dirty = True
                 content.parameter = unicode(value.toString())
         else:
             if content.parameter != unicode(value.toString()):
                 dirty = True
                 content.parameter = unicode(value.toString())
         message = content.validateParameter()
         if message:
             content.parameter = oldParameter
             dirty = False
     else:
         unitName = str(self.rootItem.content(column).toString())
         dirty, message = content.score.change(unitName, value)
     return dirty, message
Ejemplo n.º 2
0
 def __setRuleData(self, column, content, value):
     """change rule data in the model"""
     # pylint:  disable=R0912
     # allow more than 12 branches
     dirty, message = False, None
     if column == 0:
         name = unicode(value.toString())
         if content.name != english(name):
             dirty = True
             content.name = english(name)
     elif column == 1 and content.parType:
         oldParameter = content.parameter
         if content.parType is int:
             if content.parameter != value.toInt()[0]:
                 dirty = True
                 content.parameter = value.toInt()[0]
         elif content.parType is bool:
             return False
         elif content.parType is unicode:
             if content.parameter != unicode(value.toString()):
                 dirty = True
                 content.parameter = unicode(value.toString())
         else:
             if content.parameter != unicode(value.toString()):
                 dirty = True
                 content.parameter = unicode(value.toString())
         message = content.validateParameter()
         if message:
             content.parameter = oldParameter
             dirty = False
     else:
         unitName = str(self.rootItem.content(column).toString())
         dirty, message = content.score.change(unitName, value)
     return dirty, message
Ejemplo n.º 3
0
 def __generateName(widget):
     """generate a name for this widget to be used in the config file"""
     name = english(widget.objectName())
     if not name:
         while widget.parentWidget():
             name = widget.__class__.__name__ + name
             widget = widget.parentWidget()
             widgetName = english(widget.parentWidget().objectName())
             if widgetName:
                 name = widgetName + name
                 break
     return name
Ejemplo n.º 4
0
 def __generateName(widget):
     """generate a name for this widget to be used in the config file"""
     name = english(widget.objectName())
     if not name:
         while widget.parentWidget():
             name = widget.__class__.__name__ + name
             widget = widget.parentWidget()
             widgetName = english(widget.parentWidget().objectName())
             if widgetName:
                 name = widgetName + name
                 break
     return name
Ejemplo n.º 5
0
 def updateRule(self, rule):
     """update rule in database"""
     self.__hash = None  # invalidate, will be recomputed when needed
     with Transaction():
         Query("DELETE FROM rule WHERE ruleset=? and name=?",
               list([self.rulesetId, english(rule.name)]))
         self.saveRule(rule)
         Query("UPDATE ruleset SET hash=? WHERE id=?",
               list([self.hash, self.rulesetId]))
Ejemplo n.º 6
0
 def save(self, copy=False, minus=False):
     """save the ruleset to the database.
     copy=True gives it a new id. If the name already exists in the database, also give it a new name"""
     with Transaction():
         if copy:
             self.rulesetId, self.name = self._newKey(minus)
         Query('INSERT INTO ruleset(id,name,hash,description) VALUES(?,?,?,?)',
             list([self.rulesetId, english(self.name), self.hash, self.description]))
     # do not put this into the transaction, keep it as short as possible. sqlite3/Qt
     # has problems if two processes are trying to do the same here (kajonggtest)
     for rule in self.allRules:
         self.saveRule(rule)
Ejemplo n.º 7
0
 def save(self, copy=False, minus=False):
     """save the ruleset to the database.
     copy=True gives it a new id. If the name already exists in the database, also give it a new name"""
     with Transaction():
         if copy:
             self.rulesetId, self.name = self._newKey(minus)
         Query(
             'INSERT INTO ruleset(id,name,hash,description) VALUES(?,?,?,?)',
             list([
                 self.rulesetId,
                 english(self.name), self.hash, self.description
             ]))
     # do not put this into the transaction, keep it as short as possible. sqlite3/Qt
     # has problems if two processes are trying to do the same here (kajonggtest)
     for rule in self.allRules:
         self.saveRule(rule)
Ejemplo n.º 8
0
 def ruleRecord(self, rule):
     """returns the rule as tuple, prepared for use by sql"""
     score = rule.score
     definition = rule.definition
     if rule.parType:
         parTypeName = rule.parType.__name__
         if parTypeName == 'unicode':
             parTypeName = 'str'
         definition = parTypeName + definition
     ruleList = None
     for ruleList in self.ruleLists:
         if rule in ruleList:
             ruleIdx = ruleList.index(rule)
             break
     assert rule in ruleList
     return (self.rulesetId, english(rule.name), ruleList.listId, ruleIdx,
         definition, score.points, score.doubles, score.limits, str(rule.parameter))
Ejemplo n.º 9
0
 def ruleRecord(self, rule):
     """returns the rule as tuple, prepared for use by sql"""
     score = rule.score
     definition = rule.definition
     if rule.parType:
         parTypeName = rule.parType.__name__
         if parTypeName == 'unicode':
             parTypeName = 'str'
         definition = parTypeName + definition
     ruleList = None
     for ruleList in self.ruleLists:
         if rule in ruleList:
             ruleIdx = ruleList.index(rule)
             break
     assert rule in ruleList
     return (self.rulesetId, english(rule.name), ruleList.listId, ruleIdx,
             definition, score.points, score.doubles, score.limits,
             str(rule.parameter))
Ejemplo n.º 10
0
 def updateServerInfoInDatabase(self):
     """we are online. Update table server."""
     lasttime = datetime.datetime.now().replace(microsecond=0).isoformat()
     url = english(self.url) # use unique name for Local Game
     with Transaction():
         serverKnown = Query('update server set lastname=?,lasttime=? where url=?',
             list([self.username, lasttime, url])).rowcount() == 1
         if not serverKnown:
             Query('insert into server(url,lastname,lasttime) values(?,?,?)',
                 list([url, self.username, lasttime]))
     # needed if the server knows our name but our local data base does not:
     Players.createIfUnknown(self.username)
     playerId = Players.allIds[self.username]
     with Transaction():
         if Query('update passwords set password=? where url=? and player=?',
             list([self.password, url, playerId])).rowcount() == 0:
             Query('insert into passwords(url,player,password) values(?,?,?)',
                 list([url, playerId, self.password]))
Ejemplo n.º 11
0
 def setData(self, index, value, role=Qt.EditRole):
     """change data in the model"""
     # pylint:  disable=R0912
     # allow more than 12 branches
     if not index.isValid():
         return False
     try:
         dirty = False
         column = index.column()
         item = index.internalPointer()
         ruleset = item.ruleset()
         content = item.rawContent
         if role == Qt.EditRole:
             if isinstance(content, Ruleset) and column == 0:
                 name = unicode(value.toString())
                 oldName = content.name
                 content.rename(english(name))
                 dirty = oldName != content.name
             elif isinstance(content, Rule):
                 dirty, message = self.__setRuleData(column, content, value)
                 if message:
                     Sorry(message)
                     return False
             else:
                 return False
         elif role == Qt.CheckStateRole:
             if isinstance(content, Rule) and column ==1:
                 if not isinstance(ruleset, PredefinedRuleset):
                     if content.parType is bool:
                         newValue = value == Qt.Checked
                         if content.parameter != newValue:
                             dirty = True
                             content.parameter = newValue
             else:
                 return False
         if dirty:
             if isinstance(content, Rule):
                 ruleset.updateRule(content)
             self.dataChanged.emit(index, index)
         return True
     except BaseException:
         return False
Ejemplo n.º 12
0
 def setData(self, index, value, role=Qt.EditRole):
     """change data in the model"""
     # pylint:  disable=R0912
     # allow more than 12 branches
     if not index.isValid():
         return False
     try:
         dirty = False
         column = index.column()
         item = index.internalPointer()
         ruleset = item.ruleset()
         content = item.rawContent
         if role == Qt.EditRole:
             if isinstance(content, Ruleset) and column == 0:
                 name = unicode(value.toString())
                 oldName = content.name
                 content.rename(english(name))
                 dirty = oldName != content.name
             elif isinstance(content, Rule):
                 dirty, message = self.__setRuleData(column, content, value)
                 if message:
                     Sorry(message)
                     return False
             else:
                 return False
         elif role == Qt.CheckStateRole:
             if isinstance(content, Rule) and column == 1:
                 if not isinstance(ruleset, PredefinedRuleset):
                     if content.parType is bool:
                         newValue = value == Qt.Checked
                         if content.parameter != newValue:
                             dirty = True
                             content.parameter = newValue
             else:
                 return False
         if dirty:
             if isinstance(content, Rule):
                 ruleset.updateRule(content)
             self.dataChanged.emit(index, index)
         return True
     except BaseException:
         return False
Ejemplo n.º 13
0
 def updateServerInfoInDatabase(self):
     """we are online. Update table server."""
     lasttime = datetime.datetime.now().replace(microsecond=0).isoformat()
     url = english(self.url)  # use unique name for Local Game
     with Transaction():
         serverKnown = Query(
             'update server set lastname=?,lasttime=? where url=?',
             list([self.username, lasttime, url])).rowcount() == 1
         if not serverKnown:
             Query(
                 'insert into server(url,lastname,lasttime) values(?,?,?)',
                 list([url, self.username, lasttime]))
     # needed if the server knows our name but our local data base does not:
     Players.createIfUnknown(self.username)
     playerId = Players.allIds[self.username]
     with Transaction():
         if Query(
                 'update passwords set password=? where url=? and player=?',
                 list([self.password, url, playerId])).rowcount() == 0:
             Query(
                 'insert into passwords(url,player,password) values(?,?,?)',
                 list([url, playerId, self.password]))
Ejemplo n.º 14
0
 def updateRule(self, rule):
     """update rule in database"""
     self.__hash = None  # invalidate, will be recomputed when needed
     with Transaction():
         Query("DELETE FROM rule WHERE ruleset=? and name=?", list([self.rulesetId, english(rule.name)]))
         self.saveRule(rule)
         Query("UPDATE ruleset SET hash=? WHERE id=?", list([self.hash, self.rulesetId]))
Ejemplo n.º 15
0
 def url(self):
     """abstracts the url of the dialog"""
     return english(unicode(self.cbServer.currentText()))
Ejemplo n.º 16
0
 def url(self):
     """abstracts the url of the dialog"""
     return english(unicode(self.cbServer.currentText()))