2011-10-13 11:24:01 +07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Relational
|
|
|
|
# Copyright (C) 2008 Salvo "LtWorf" Tomaselli
|
2013-12-26 17:31:43 +07:00
|
|
|
#
|
2011-10-13 11:24:01 +07:00
|
|
|
# Relational is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2013-12-26 17:31:43 +07:00
|
|
|
#
|
2011-10-13 11:24:01 +07:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2013-12-26 17:31:43 +07:00
|
|
|
#
|
2011-10-13 11:24:01 +07:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-12-26 17:31:43 +07:00
|
|
|
#
|
2011-10-13 11:24:01 +07:00
|
|
|
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
|
|
|
|
|
|
|
|
try:
|
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
except:
|
|
|
|
from PySide import QtCore, QtGui
|
2011-10-13 13:44:20 +07:00
|
|
|
|
|
|
|
import compatibility
|
2011-10-13 11:24:01 +07:00
|
|
|
from relational import relation
|
|
|
|
import rel_edit
|
|
|
|
|
|
|
|
|
|
|
|
class creatorForm(QtGui.QDialog):
|
2013-12-26 17:31:43 +07:00
|
|
|
|
|
|
|
def __init__(self, rel=None):
|
2011-10-13 11:24:01 +07:00
|
|
|
QtGui.QDialog.__init__(self)
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 11:24:01 +07:00
|
|
|
self.setSizeGripEnabled(True)
|
2013-12-26 17:31:43 +07:00
|
|
|
self.result_relation = None
|
|
|
|
self.rel = rel
|
|
|
|
|
|
|
|
def setUi(self, ui):
|
|
|
|
self.ui = ui
|
|
|
|
self.table = self.ui.table
|
|
|
|
|
|
|
|
if self.rel == None:
|
2011-10-13 12:06:24 +07:00
|
|
|
self.setup_empty()
|
|
|
|
else:
|
2011-10-13 13:10:01 +07:00
|
|
|
self.setup_relation(self.rel)
|
2013-12-26 17:31:43 +07:00
|
|
|
|
|
|
|
def setup_relation(self, rel):
|
|
|
|
|
2011-10-13 13:10:01 +07:00
|
|
|
self.table.insertRow(0)
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 13:10:01 +07:00
|
|
|
for i in rel.header.attributes:
|
2013-12-26 17:31:43 +07:00
|
|
|
item = QtGui.QTableWidgetItem()
|
2011-10-13 13:10:01 +07:00
|
|
|
item.setText(i)
|
|
|
|
self.table.insertColumn(self.table.columnCount())
|
2013-12-26 17:31:43 +07:00
|
|
|
self.table.setItem(0, self.table.columnCount() - 1, item)
|
|
|
|
|
2011-10-13 13:10:01 +07:00
|
|
|
for i in rel.content:
|
|
|
|
self.table.insertRow(self.table.rowCount())
|
|
|
|
for j in range(len(i)):
|
2013-12-26 17:31:43 +07:00
|
|
|
item = QtGui.QTableWidgetItem()
|
2011-10-13 13:10:01 +07:00
|
|
|
item.setText(i[j])
|
2013-12-26 17:31:43 +07:00
|
|
|
self.table.setItem(self.table.rowCount() - 1, j, item)
|
|
|
|
|
2011-10-13 12:06:24 +07:00
|
|
|
pass
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 12:06:24 +07:00
|
|
|
def setup_empty(self):
|
|
|
|
self.table.insertColumn(0)
|
|
|
|
self.table.insertColumn(0)
|
|
|
|
self.table.insertRow(0)
|
|
|
|
self.table.insertRow(0)
|
2013-12-26 17:31:43 +07:00
|
|
|
|
|
|
|
i00 = QtGui.QTableWidgetItem()
|
|
|
|
i01 = QtGui.QTableWidgetItem()
|
|
|
|
i10 = QtGui.QTableWidgetItem()
|
|
|
|
i11 = QtGui.QTableWidgetItem()
|
2011-10-13 12:06:24 +07:00
|
|
|
i00.setText('Field name 1')
|
|
|
|
i01.setText('Field name 2')
|
|
|
|
i10.setText('Value 1')
|
|
|
|
i11.setText('Value 2')
|
2013-12-26 17:31:43 +07:00
|
|
|
|
|
|
|
self.table.setItem(0, 0, i00)
|
|
|
|
self.table.setItem(0, 1, i01)
|
|
|
|
self.table.setItem(1, 0, i10)
|
|
|
|
self.table.setItem(1, 1, i11)
|
|
|
|
|
2011-10-13 13:44:20 +07:00
|
|
|
def create_relation(self):
|
2013-12-26 17:31:43 +07:00
|
|
|
hlist = []
|
|
|
|
|
2011-10-13 13:44:20 +07:00
|
|
|
for i in range(self.table.columnCount()):
|
2013-12-26 17:31:43 +07:00
|
|
|
hlist.append(
|
|
|
|
compatibility.get_py_str(self.table.item(0, i).text()))
|
2011-10-13 13:44:20 +07:00
|
|
|
try:
|
2013-12-26 17:31:43 +07:00
|
|
|
header = relation.header(hlist)
|
2011-10-13 13:44:20 +07:00
|
|
|
except Exception, e:
|
2013-12-26 17:31:43 +07:00
|
|
|
QtGui.QMessageBox.information(None, QtGui.QApplication.translate("Form", "Error"), "%s\n%s" % (
|
|
|
|
QtGui.QApplication.translate("Form", "Header error!"), e.__str__()))
|
2011-10-13 15:30:06 +07:00
|
|
|
return None
|
2013-12-26 17:31:43 +07:00
|
|
|
r = relation.relation()
|
|
|
|
r.header = header
|
|
|
|
|
|
|
|
for i in range(1, self.table.rowCount()):
|
|
|
|
hlist = []
|
2011-10-13 13:44:20 +07:00
|
|
|
for j in range(self.table.columnCount()):
|
2011-10-13 15:39:48 +07:00
|
|
|
try:
|
2013-12-26 17:31:43 +07:00
|
|
|
hlist.append(
|
|
|
|
compatibility.get_py_str(self.table.item(i, j).text()))
|
2011-10-13 15:39:48 +07:00
|
|
|
except:
|
2013-12-26 17:31:43 +07:00
|
|
|
QtGui.QMessageBox.information(None, QtGui.QApplication.translate(
|
|
|
|
"Form", "Error"), QtGui.QApplication.translate("Form", "Unset value in %d,%d!" % (i + 1, j + 1)))
|
2011-10-13 15:39:48 +07:00
|
|
|
return None
|
2011-10-13 13:44:20 +07:00
|
|
|
r.content.add(tuple(hlist))
|
|
|
|
return r
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 11:24:01 +07:00
|
|
|
def accept(self):
|
2013-12-26 17:31:43 +07:00
|
|
|
|
|
|
|
self.result_relation = self.create_relation()
|
|
|
|
|
|
|
|
# Doesn't close the window in case of errors
|
|
|
|
if self.result_relation != None:
|
2011-10-13 15:30:06 +07:00
|
|
|
QtGui.QDialog.accept(self)
|
2011-10-13 11:24:01 +07:00
|
|
|
pass
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 11:24:01 +07:00
|
|
|
def reject(self):
|
2013-12-26 17:31:43 +07:00
|
|
|
self.result_relation = None
|
2011-10-13 12:06:24 +07:00
|
|
|
QtGui.QDialog.reject(self)
|
|
|
|
pass
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 12:06:24 +07:00
|
|
|
def addColumn(self):
|
|
|
|
self.table.insertColumn(self.table.columnCount())
|
|
|
|
pass
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 12:06:24 +07:00
|
|
|
def addRow(self):
|
|
|
|
self.table.insertRow(1)
|
|
|
|
pass
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 12:06:24 +07:00
|
|
|
def deleteColumn(self):
|
2013-12-26 17:31:43 +07:00
|
|
|
if self.table.columnCount() > 1:
|
2011-10-13 12:06:24 +07:00
|
|
|
self.table.removeColumn(self.table.currentColumn())
|
|
|
|
pass
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 12:06:24 +07:00
|
|
|
def deleteRow(self):
|
2013-12-26 17:31:43 +07:00
|
|
|
if self.table.rowCount() > 2:
|
2011-10-13 12:06:24 +07:00
|
|
|
self.table.removeRow(self.table.currentRow())
|
2011-10-13 11:24:01 +07:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-10-13 15:15:38 +07:00
|
|
|
def edit_relation(rel=None):
|
|
|
|
'''Opens the editor for the given relation and returns a _new_ relation
|
|
|
|
containing the new relation.
|
|
|
|
If the user cancels, it returns None'''
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 11:24:01 +07:00
|
|
|
ui = rel_edit.Ui_Dialog()
|
2011-10-13 12:06:24 +07:00
|
|
|
Form = creatorForm(rel)
|
2011-10-13 11:24:01 +07:00
|
|
|
|
|
|
|
ui.setupUi(Form)
|
2011-10-13 12:06:24 +07:00
|
|
|
Form.setUi(ui)
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 11:24:01 +07:00
|
|
|
Form.exec_()
|
|
|
|
return Form.result_relation
|
2013-12-26 17:31:43 +07:00
|
|
|
|
2011-10-13 11:24:01 +07:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
app = QtGui.QApplication(sys.argv)
|
2013-12-26 17:31:43 +07:00
|
|
|
r = relation.relation(
|
|
|
|
"/home/salvo/dev/relational/trunk/samples/people.csv")
|
2011-10-13 15:15:38 +07:00
|
|
|
print edit_relation(r)
|