OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
loader_manager_widget.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2011 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 # -*- coding: utf-8 -*-
20 
21 import os
22 import ost
23 from ost import gui
24 from ost import gfx
25 from datetime import datetime
26 from PyQt4 import QtCore, QtGui
27 
28 from loader_list_model import LoaderListModel
29 from file_loader import GenericLoader
30 
31 #Gradient Preset Widget
32 class LoaderManagerWidget(QtGui.QDialog):
33  ICONS_DIR = os.path.join(ost.GetSharedDataPath(), "gui", "icons/")
34  def __init__(self, parent=None):
35  QtGui.QDialog.__init__(self, parent)
36 
37  #Title
38  self.text_ = "Loader Manager"
39 
40  #Create Ui elements
41  self.list_view_ = QtGui.QListView()
42 
43  #Create Model
44  self.list_model_ = LoaderListModel(self)
45  self.list_view_.setModel(self.list_model_)
46  self.list_view_.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
47 
48  loader_label = QtGui.QLabel(self.text_)
49  font = loader_label.font()
50  font.setBold(True)
51 
52  self.add_action = QtGui.QAction("+",self)
53  self.add_action.setIcon(QtGui.QIcon(LoaderManagerWidget.ICONS_DIR+"add_icon.png"))
54 
55  QtCore.QObject.connect(self.add_action, QtCore.SIGNAL("triggered()"), self.Add)
56 
57  self.add_button_ = QtGui.QToolButton(self)
58  self.add_button_.setIconSize(QtCore.QSize(20,20))
59  self.add_button_.setDefaultAction(self.add_action)
60 
61  grid = QtGui.QGridLayout()
62  grid.setContentsMargins(0,5,0,0)
63  grid.addWidget(loader_label, 0, 0, 1, 1)
64  qhbox = QtGui.QHBoxLayout()
65  grid.addWidget(self.list_view_,1,0,3,3)
66  grid.addWidget(self.add_button_,4,0,1,1)
67  self.setLayout(grid)
68 
69  self.list_view_.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
70  QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("customContextMenuRequested(const QPoint)"), self.contextMenuEvent)
71  self.CreateContextMenu()
72 
73  QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("doubleClicked(const QModelIndex)"), self.Edit)
74 
75  def CreateContextMenu(self):
76  self.contextMenu_ = QtGui.QMenu("Context menu", self)
77  self.remove_ = QtGui.QAction("Remove", self.list_view_)
78  self.rename_ = QtGui.QAction("Rename", self.list_view_)
79  self.edit_ = QtGui.QAction("Edit", self.list_view_)
80  self.contextMenu_.addAction(self.remove_)
81  self.contextMenu_.addAction(self.rename_)
82  self.contextMenu_.addAction(self.edit_)
83  #Connect Signals with Slots
84  QtCore.QObject.connect(self.remove_, QtCore.SIGNAL("triggered()"), self.Remove)
85  QtCore.QObject.connect(self.rename_, QtCore.SIGNAL("triggered()"), self.Rename)
86  QtCore.QObject.connect(self.edit_, QtCore.SIGNAL("triggered()"), self.Edit)
87 
88  def contextMenuEvent(self, pos):
89  #ContextMenu
90  index = self.list_view_.indexAt(pos)
91  if index.isValid():
92  if self.list_model_.IsEditable(index.row()):
93  self.contextMenu_.popup(QtGui.QCursor.pos())
94 
95  def Add(self):
96  row = self.list_model_.GetLastRow()
97  edit_widget = LoaderEditWidget()
98 
99  if edit_widget.exec_() and self.list_model_.AddItem(datetime.now().isoformat(' '), edit_widget.GetLoader(), row, True, True):
100  index = self.list_model_.index(row)
101  self.list_view_.setCurrentIndex(index)
102  self.Rename()
103 
104  def Edit(self):
105  current_index = self.list_view_.currentIndex()
106  loader = self.list_model_.GetLoader(current_index)
107  edit_widget = LoaderEditWidget()
108  edit_widget.SetLoader(loader)
109  if edit_widget.exec_():
110  self.list_model_.SetItem(current_index, edit_widget.GetLoader())
111 
112 
113  def Remove(self):
114  if(self.list_view_.currentIndex().isValid()):
115  ret = QtGui.QMessageBox.warning(self, "Delete File Loader",
116  "Delete File Loader?",
117  QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
118  if ret == QtGui.QMessageBox.Yes:
119  self.list_model_.RemoveItem(self.list_view_.currentIndex().row())
120 
121  def Rename(self):
122  if(self.list_view_.currentIndex().isValid()):
123  self.list_view_.edit(self.list_view_.currentIndex())
124 
125  def GetText(self):
126  return self.text_
127 
128 
129 
130 
131 class LoaderEditWidget(QtGui.QDialog):
132  def __init__(self, parent=None):
133  QtGui.QDialog.__init__(self, parent)
134  name_ext_label = QtGui.QLabel("Name Extension")
135  self.name_ext_edit_ = QtGui.QLineEdit()
136 
137  url_label = QtGui.QLabel("Url")
138  self.url_edit_ = QtGui.QLineEdit()
139  self.add_id_button_ = QtGui.QToolButton(self)
140  self.add_id_button_.setDefaultAction(QtGui.QAction("ID",self))
141 
142  file_ext_label = QtGui.QLabel("File Type")
143  self.file_ext_auto_ = QtGui.QCheckBox("Auto")
144  self.file_ext_edit_ = QtGui.QLineEdit()
145 
146  self.hbox_ = QtGui.QHBoxLayout()
147  self.ok_button_ = QtGui.QPushButton("OK")
148  self.cancel_button_ = QtGui.QPushButton("Cancel")
149  self.hbox_.addWidget(self.ok_button_)
150  self.hbox_.addStretch()
151  self.hbox_.addWidget(self.cancel_button_)
152 
153  grid = QtGui.QGridLayout()
154  grid.setContentsMargins(0,5,0,0)
155  grid.addWidget(name_ext_label, 0, 0, 1, 1)
156  grid.addWidget(self.name_ext_edit_, 0, 1, 1, 4)
157  grid.addWidget(url_label, 1, 0, 1, 1)
158  grid.addWidget(self.url_edit_, 1, 1, 1, 3)
159  grid.addWidget(self.add_id_button_, 1, 4, 1, 1)
160  grid.addWidget(file_ext_label, 2, 0, 1, 1)
161  grid.addWidget(self.file_ext_auto_, 2, 1, 1, 1)
162  grid.addWidget(self.file_ext_edit_, 2, 2, 1, 3)
163  grid.addLayout(self.hbox_,3,0,1,2)
164  grid.setRowStretch(2, 1)
165  self.setLayout(grid)
166 
167  QtCore.QObject.connect(self.add_id_button_, QtCore.SIGNAL("clicked()"), self.AppendId)
168  QtCore.QObject.connect(self.file_ext_auto_, QtCore.SIGNAL("stateChanged(int)"), self.CbStateChanged)
169  QtCore.QObject.connect(self.ok_button_, QtCore.SIGNAL("clicked()"), self.Ok)
170  QtCore.QObject.connect(self.cancel_button_, QtCore.SIGNAL("clicked()"), self.Cancel)
171 
172  def GetLoader(self):
173  name_ext = str(self.name_ext_edit_.text())
174  url = str(self.url_edit_.text())
175  file_ext = None
176  if not self.file_ext_auto_.isChecked():
177  file_ext = str(self.file_ext_edit_.text())
178 
179  return GenericLoader(name_ext, url, False, file_ext)
180 
181  def SetLoader(self, loader):
182  self.name_ext_edit_.setText(loader.name_)
183  self.url_edit_.setText(loader.url_)
184  if loader.file_type_ is not None:
185  self.file_ext_edit_.setText(loader.file_type_)
186  self.file_ext_auto_.setChecked(False)
187  else:
188  self.file_ext_auto_.setChecked(True)
189  def AppendId(self):
190  self.url_edit_.setText(self.url_edit_.text()+"${ID}")
191 
192  def CbStateChanged(self, state):
193  self.file_ext_edit_.setEnabled(not self.file_ext_auto_.isChecked())
194 
195  def Ok(self):
196  self.accept()
197 
198  def Cancel(self):
199  self.reject()