File: preset_widget.py

package info (click to toggle)
openstructure 2.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 205,228 kB
  • sloc: cpp: 188,129; python: 35,361; ansic: 34,298; fortran: 3,275; sh: 286; xml: 146; makefile: 29
file content (162 lines) | stat: -rw-r--r-- 6,219 bytes parent folder | download | duplicates (4)
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
#------------------------------------------------------------------------------
# This file is part of the OpenStructure project <www.openstructure.org>
#
# Copyright (C) 2008-2020 by the OpenStructure authors
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
# This library 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#------------------------------------------------------------------------------

from ost import gui
from ost import gfx
import ost
import os
from datetime import datetime
from PyQt5 import QtCore, QtWidgets, QtGui
from .scene_selection_helper import SelHelper
from .preset_list_model import PresetListModel
from .preset_editor_widget import PresetEditor
from .preset import Preset

class PresetWidget(QtWidgets.QWidget):
  PRESET_XML_FILE = os.path.join(ost.GetSharedDataPath(), "scene", "presets.xml")
  ICONS_DIR = os.path.join(ost.GetSharedDataPath(), "gui", "icons/")
  def __init__(self, parent=None):   
    QtWidgets.QWidget.__init__(self, parent)
    
    self.text_ = "Presets"
        
    #Create Ui elements
    self.list_view_ = QtWidgets.QListView()
        
    #Create Model
    self.list_model_ = PresetListModel(self)
    self.list_view_.setModel(self.list_model_)
    self.list_view_.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
    
    self.add_action = QtWidgets.QAction("+",self)
    self.add_action.setIcon(QtGui.QIcon(PresetWidget.ICONS_DIR+"add_icon.png"))
    
    self.add_action.triggered.connect(self.Add)
    
    self.add_button_ = QtWidgets.QToolButton(self)
    self.add_button_.setIconSize(QtCore.QSize(20,20))
    self.add_button_.setDefaultAction(self.add_action)
    
    self.preset_editor_ = PresetEditor(self)
    
    grid = QtWidgets.QGridLayout()
    grid.setContentsMargins(0,5,0,0)
    grid.addWidget(self.list_view_,0,0,3,3)
    grid.addWidget(self.add_button_,3,0,1,1)
    self.setLayout(grid)
    
    self.list_view_.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    self.list_view_.customContextMenuRequested.connect(self.contextMenuEvent)
    self.CreateImmutableContextMenu()
    self.CreateContextMenu()
  
    self.list_view_.doubleClicked.connect(self.Load)
    
    self.setMinimumSize(250,200)
    
  def CreateImmutableContextMenu(self):  
    self.immucontext_menu_ = QtWidgets.QMenu("Context menu", self)
    self.load_ = QtWidgets.QAction("Load", self.list_view_)  
    self.immucontext_menu_.addAction(self.load_)
    #Connect Signal with Slot  
    self.load_.triggered.connect(self.LoadCurrentIndex)

  def CreateContextMenu(self):
    self.context_menu_ = QtWidgets.QMenu("Context menu", self)
    self.remove_ = QtWidgets.QAction("Remove", self.list_view_)
    self.rename_ = QtWidgets.QAction("Rename", self.list_view_)
    self.edit_ = QtWidgets.QAction("Edit", self.list_view_)
    self.context_menu_.addAction(self.load_)
    self.context_menu_.addAction(self.remove_)
    self.context_menu_.addAction(self.rename_)
    self.context_menu_.addAction(self.edit_)
    #Connect Signals with Slots  
    self.remove_.triggered.connect(self.Remove)
    self.rename_.triggered.connect(self.Rename)
    self.edit_.triggered.connect(self.Edit)
  
  def contextMenuEvent(self, pos):
    #ContextMenu
    index = self.list_view_.indexAt(pos)
    if index.isValid(): 
      if self.list_model_.IsEditable(index.row()):
        self.context_menu_.popup(QtWidgets.QCursor.pos())
      else:
        self.immucontext_menu_.popup(QtWidgets.QCursor.pos())
  
  def Add(self):
    row = self.list_model_.GetLastRow()
    preset = Preset(datetime.now().isoformat(' '))
    self.preset_editor_.SetPreset(preset)
    if(self.preset_editor_.exec_()):
      if self.list_model_.AddItem(preset, row, True, True):
        index = self.list_model_.index(row)
        self.list_view_.setCurrentIndex(index)
        self.Rename()
      
  def Remove(self):
    if(self.list_view_.currentIndex().isValid()):
      ret = QtWidgets.QMessageBox.warning(self, "Delete Preset",
                   "Delete Preset?",
                   QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
      if ret == QtWidgets.QMessageBox.Yes:
        self.list_model_.RemoveItem(self.list_view_.currentIndex().row())

  def Edit(self):
    if(self.list_view_.currentIndex().isValid()):
        preset = self.list_model_.GetPreset(self.list_view_.currentIndex())
        self.preset_editor_.SetPreset(preset)
        if(self.preset_editor_.exec_()):
            row = self.list_view_.currentIndex().row()
            self.list_model_.RemoveItem(row)
            self.list_model_.AddItem(preset, row, True, True)
        
  def LoadCurrentIndex(self):
    if(self.list_view_.currentIndex().isValid()):
      self.Load(self.list_view_.currentIndex())

  def Load(self, index):
    if(index.isValid()):
      scene_selection = gui.SceneSelection.Instance()
      preset=self.list_model_.GetPreset(index)
      for i in range(0,scene_selection.GetActiveNodeCount()):
        node = scene_selection.GetActiveNode(i)
        if isinstance(node, gfx.Entity):
          node.CleanColorOps()
          preset.ApplyOn(node)
          
  def ChangeColor(self,node):
    self.LoadCurrentIndex()
  
  def Update(self):
    self.setEnabled(True)
    if SelHelper().CheckAllFlags(SelHelper.NO_SELECTION):
      self.setEnabled(False)
      return
    
    if SelHelper().CheckNotFlags(SelHelper.HAS_ENTITY | SelHelper.IS_ONE_TYPE):
      self.setEnabled(False)
      return

  def Rename(self):
    if(self.list_view_.currentIndex().isValid()):
      self.list_view_.edit(self.list_view_.currentIndex())
    
  def GetText(self):
    return self.text_