[go: up one dir, main page]

Menu

[d66369]: / src / item_dialog.py  Maximize  Restore  History

Download this file

277 lines (204 with data), 10.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding: utf-8 -*-
'''
Copyright 2010 Vitaly Volkov
This file is part of Reggata.
Reggata 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.
Reggata 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.
You should have received a copy of the GNU General Public License
along with Reggata. If not, see <http://www.gnu.org/licenses/>.
Created on 15.10.2010
@author: vlkv
'''
import os
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
import ui_itemdialog
import consts
from db_schema import Item, DataRef, Tag, Item_Tag, Field, Item_Field
import helpers
from helpers import show_exc_info, is_none_or_empty, is_internal
from parsers import definition_parser
from parsers.util import quote
from exceptions import MsgException
from common_widgets import TextEdit
class ItemDialog(QtGui.QDialog):
'''
This dialog is for create/edit/view single repository Item.
'''
CREATE_MODE = "CREATE_MODE"
EDIT_MODE = "EDIT_MODE"
VIEW_MODE = "VIEW_MODE"
def __init__(self, parent, item, mode, completer=None):
super(ItemDialog, self).__init__(parent)
self.ui = ui_itemdialog.Ui_ItemDialog()
self.ui.setupUi(self)
self.completer = completer
#Adding my custom widgets to dialog
self.ui.plainTextEdit_fields = TextEdit(self, self.completer, completer_end_str=": ")
self.ui.verticalLayout_text_edit_fields.addWidget(self.ui.plainTextEdit_fields)
self.ui.plainTextEdit_tags = TextEdit(self, self.completer)
self.ui.verticalLayout_text_edit_tags.addWidget(self.ui.plainTextEdit_tags)
if type(item) != Item:
raise TypeError(self.tr("Argument item should be an instance of Item class."))
#parent should be a MainWindow instance
#because, we will need to access to parent.active_repo
if parent.__class__.__name__ != "MainWindow":
raise TypeError(self.tr("Parent must be an instance of MainWindow class."))
self.parent = parent
self.mode = mode
self.item = item
self.connect(self.ui.buttonBox, QtCore.SIGNAL("accepted()"), self.button_ok)
self.connect(self.ui.buttonBox, QtCore.SIGNAL("rejected()"), self.button_cancel)
self.connect(self.ui.pushButtonAddDataRef, QtCore.SIGNAL("clicked()"), self.buttonAddDataRef)
self.connect(self.ui.pushButtonRemoveDataRef, QtCore.SIGNAL("clicked()"), self.buttonRemoveDataRef)
self.connect(self.ui.pushButtonMoveFile, QtCore.SIGNAL("clicked()"), self.buttonMoveFile)
self.read()
def read(self):
'''
Function updates all dialog GUI elements according to self.item object data.
'''
self.ui.lineEdit_id.setText(str(self.item.id))
self.ui.lineEdit_user_login.setText(self.item.user_login)
self.ui.lineEdit_title.setText(self.item.title)
if self.item.data_ref:
#Make an absolute path to the DataRef file
fileAbsPath = self.item.data_ref.url
if not os.path.isabs(fileAbsPath):
fileAbsPath = os.path.join(self.parent.active_repo.base_path, fileAbsPath)
if not os.path.exists(fileAbsPath):
self.ui.fileAbsPath.setText("FILE NOT FOUND: " + fileAbsPath)
else:
self.ui.fileAbsPath.setText(fileAbsPath)
locationDirRelPath = ""
if self.mode == ItemDialog.EDIT_MODE or self.mode == ItemDialog.VIEW_MODE:
#Make a relative path to the directory where DataRef file is located
locationDirRelPath = os.path.dirname(self.item.data_ref.url)
locationDirRelPath = "." if locationDirRelPath.strip() == "" else locationDirRelPath
self.ui.fileLocationDirRelPath.setText(locationDirRelPath)
#Displaying item's list of field-values
s = ""
for itf in self.item.item_fields:
#Processing reserved fields
if itf.field.name in consts.RESERVED_FIELDS:
if itf.field.name == consts.NOTES_FIELD:
self.ui.plainTextEdit_notes.setPlainText(itf.field_value)
elif itf.field.name == consts.RATING_FIELD:
try:
rating = int(itf.field_value)
except:
rating = 0
self.ui.spinBox_rating.setValue(rating)
else:
raise MsgException(
self.tr("Unknown reserved field name '{}'").format(itf.field.name))
#Processing all other fields
else:
name = quote(itf.field.name) if definition_parser.needs_quote(itf.field.name) \
else itf.field.name
value = quote(itf.field_value) if definition_parser.needs_quote(itf.field_value) \
else itf.field_value
s = s + name + ": " + value + os.linesep
self.ui.plainTextEdit_fields.setPlainText(s)
#Displaying item's list of tags
s = ""
for itg in self.item.item_tags:
tag_name = itg.tag.name
s = s + (quote(tag_name) if definition_parser.needs_quote(tag_name) else tag_name) + " "
self.ui.plainTextEdit_tags.setPlainText(s)
def write(self):
'''Writes all data from dialog GUI elements to the self.item object.'''
self.item.title = self.ui.lineEdit_title.text()
#Processing Tags
del self.item.item_tags[:]
text = self.ui.plainTextEdit_tags.toPlainText()
tags, tmp = definition_parser.parse(text)
for t in tags:
tag = Tag(name=t)
item_tag = Item_Tag(tag)
item_tag.user_login = self.item.user_login
self.item.item_tags.append(item_tag)
#Processing Fields
del self.item.item_fields[:]
text = self.ui.plainTextEdit_fields.toPlainText()
tmp, fields = definition_parser.parse(text)
for (f, v) in fields:
if f in consts.RESERVED_FIELDS:
raise MsgException(self.tr("Field name '{}' is reserved.").format(f))
field = Field(name=f)
item_field = Item_Field(field, v)
item_field.user_login = self.item.user_login
self.item.item_fields.append(item_field)
#Processing reserved field NOTES_FIELD
notes = self.ui.plainTextEdit_notes.toPlainText()
if not is_none_or_empty(notes):
field = Field(name=consts.NOTES_FIELD)
item_field = Item_Field(field, notes)
item_field.user_login = self.item.user_login
self.item.item_fields.append(item_field)
#Processing reserved field RATING_FIELD
rating = self.ui.spinBox_rating.value()
if rating > 0:
field = Field(name=consts.RATING_FIELD)
item_field = Item_Field(field, rating)
item_field.user_login = self.item.user_login
self.item.item_fields.append(item_field)
#Processing DataRef object
if self.item.data_ref is not None:
self.item.data_ref.srcAbsPath = self.ui.fileAbsPath.text()
filename = os.path.basename(self.item.data_ref.srcAbsPath)
self.item.data_ref.dstRelPath = os.path.join(self.ui.fileLocationDirRelPath.text(), filename)
#TODO: check that abs and rel paths are valid
def button_ok(self):
try:
if self.mode == ItemDialog.VIEW_MODE:
self.accept()
elif self.mode == ItemDialog.CREATE_MODE or self.mode == ItemDialog.EDIT_MODE:
self.write()
self.item.check_valid()
self.accept()
else:
raise ValueError(self.tr("self.mode has bad value."))
except Exception as ex:
show_exc_info(self, ex)
def button_cancel(self):
self.reject()
def buttonAddDataRef(self):
file = QtGui.QFileDialog.getOpenFileName(self, self.tr("Select a file to link with the Item."))
if is_none_or_empty(file):
return
#Suggest a title for the Item, if it has not any title yet
title = self.ui.lineEdit_title.text()
if is_none_or_empty(title):
self.ui.lineEdit_title.setText(os.path.basename(file))
data_ref = DataRef(type=DataRef.FILE, url=file)
self.item.data_ref = data_ref
assert(os.path.isabs(data_ref.url))
self.ui.fileAbsPath.setText(data_ref.url)
def buttonRemoveDataRef(self):
self.item.data_ref = None
self.item.data_ref_id = None
self.ui.fileAbsPath.setText(None)
self.ui.fileLocationDirRelPath.setText(None)
def buttonMoveFile(self):
try:
if self.item.data_ref is None:
raise Exception(self.tr("You must add a Data Reference first."))
directory = QtGui.QFileDialog.getExistingDirectory(
self,
self.tr("Select destination path within repository"),
self.parent.active_repo.base_path)
if is_none_or_empty(directory):
return
if not is_internal(directory, self.parent.active_repo.base_path):
raise MsgException(self.tr("Chosen directory is out of active repository."))
else:
new_dst_path = os.path.relpath(directory, self.parent.active_repo.base_path)
self.ui.fileLocationDirRelPath.setText(new_dst_path)
except Exception as ex:
show_exc_info(self, ex)