OpenStructure
query_editor.py
Go to the documentation of this file.
1 from ost import mol
2 from PyQt5 import QtCore, QtWidgets, QtGui
3 
4 class QueryEditorWidget(QtWidgets.QWidget):
5  def __init__(self, parent=None):
6  QtWidgets.QWidget.__init__(self, parent)
7  self.default_font_default_font_=QtGui.QTextCharFormat()
8  self.default_font_default_font_.setForeground(QtGui.QBrush(QtGui.QColor(0,0,0)))
9  self.error_font_error_font_=QtGui.QTextCharFormat()
10  self.error_font_error_font_.setForeground(QtGui.QBrush(QtGui.QColor(255,0,0)))
11  self.selection_edit_selection_edit_=QtWidgets.QTextEdit(self)
12  self.selection_edit_selection_edit_.setFixedHeight(40)
13  self.selection_edit_selection_edit_.updateGeometry()
14  self.status_status_=QtWidgets.QLabel(" ",self);
15  self.status_status_.setWordWrap(True)
16  self.status_status_.setMargin(0)
17  self.status_status_.setAlignment(QtCore.Qt.AlignRight)
18  self.status_status_.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum,
19  QtWidgets.QSizePolicy.Expanding))
20  vl=QtWidgets.QVBoxLayout()
21  vl.addWidget(self.selection_edit_selection_edit_)
22  self.no_bonds_no_bonds_=QtWidgets.QRadioButton('none')
23  self.ex_bonds_ex_bonds_=QtWidgets.QRadioButton('exclusive')
24  self.in_bonds_in_bonds_=QtWidgets.QRadioButton('inclusive')
25  self.in_bonds_in_bonds_.setChecked(True)
26  self.match_res_match_res_=QtWidgets.QCheckBox('match residues')
27 
28  vl.setContentsMargins(0,0,0,0)
29  vl.setSpacing(0)
30  self.setLayout(vl)
31  vl.addWidget(self.status_status_)
32  hl=QtWidgets.QHBoxLayout()
33  l=QtWidgets.QLabel("bonds:")
34 
35  hl.addWidget(l)
36  hl.addSpacing(5)
37  hl.addWidget(self.no_bonds_no_bonds_)
38  hl.addWidget(self.ex_bonds_ex_bonds_)
39  hl.addWidget(self.in_bonds_in_bonds_)
40  hl.addStretch(1)
41  vl.addLayout(hl)
42  vl.addWidget(self.match_res_match_res_)
43 
44  self.changing_text_changing_text_=False;
45  self.selection_edit_selection_edit_.textChanged.connect(self._StartTimer_StartTimer)
46  self.timer_timer_=QtCore.QTimer()
47  self.timer_timer_.timeout.connect(self._UpdateMessage_UpdateMessage)
48 
49  def GetQueryFlags(self):
50  flags=0
51  if self.no_bonds_no_bonds_.isChecked():
52  flags|=mol.NO_BONDS
53  if self.ex_bonds_ex_bonds_.isChecked():
54  flags|=mol.EXCLUSIVE_BONDS
55  if self.match_res_match_res_.isChecked():
56  flags|=mol.MATCH_RESIDUES
57  return flags
58 
59  def GetQuery(self):
60  return mol.Query(str(self.selection_edit_selection_edit_.toPlainText()))
61 
62  def GetQueryText(self):
63  return str(self.selection_edit_selection_edit_.toPlainText())
64 
65  def SetQueryFlags(self,flags):
66  self.no_bonds_no_bonds_.setChecked(flags & mol.NO_BONDS)
67  self.ex_bonds_ex_bonds_.setChecked(flags & mol.EXCLUSIVE_BONDS)
68  self.in_bonds_in_bonds_.setChecked(not (flags & mol.NO_BONDS|mol.EXCLUSIVE_BONDS))
69  self.match_res_match_res_.setChecked(flags & mol.MATCH_RESIDUES)
70 
71  def SetQuery(self,query):
72  self.selection_edit_selection_edit_.setText(query)
73 
74  def _StartTimer(self):
75  self.timer_timer_.stop()
76  self.timer_timer_.start(500)
77 
78  def _UpdateMessage(self):
79  if self.changing_text_changing_text_:
80  return
81  self.changing_text_changing_text_ = True
82  query=self.GetQueryGetQuery()
83 
84  cursor=self.selection_edit_selection_edit_.textCursor()
85  cursor.select(QtGui.QTextCursor.Document)
86  cursor.setCharFormat(self.default_font_default_font_)
87 
88  if query.IsValid():
89  self.status_status_.setText("")
90  else:
91  d=query.GetErrorDescription()
92  self.status_status_.setText("<font color='red'>%s</font>"%d.msg.strip())
93  #self.status_.setFixedSize(self.width(),self.status_.height())
94  cursor.movePosition(QtGui.QTextCursor.Start)
95  if d.range.Loc<len(query.string):
96 
97  cursor.movePosition(QtGui.QTextCursor.NextCharacter,
98  QtGui.QTextCursor.MoveAnchor, d.range.Loc)
99  cursor.movePosition(QtGui.QTextCursor.NextCharacter,
100  QtGui.QTextCursor.KeepAnchor, d.range.Length)
101  cursor.setCharFormat(self.error_font_error_font_)
102  self.changing_text_changing_text_=False
103 
104 class QueryDialog(QtWidgets.QDialog):
105  def __init__(self, title, parent=None):
106  QtWidgets.QDialog.__init__(self, parent)
107  l=QtWidgets.QVBoxLayout(self)
108  self.setWindowTitle(title)
109  self.editoreditor=QueryEditorWidget(self)
110  l.addWidget(self.editoreditor)
111  l.addSpacing(10)
112  l3=QtWidgets.QHBoxLayout()
113  ab=QtWidgets.QPushButton('OK')
114  ab.setDefault(True)
115  cb=QtWidgets.QPushButton('Cancel')
116  l3.addStretch(1)
117  l3.addWidget(cb, 0)
118  l3.addWidget(ab, 0)
119  l.addLayout(l3)
120  cb.clicked.connect(self.reject)
121  ab.clicked.connect(self.accept)
122  self.editoreditor.selection_edit_.textChanged.connect(self._CheckNewline_CheckNewline)
123 
124  @property
125  def query_flags(self):
126  return self.editoreditor.GetQueryFlags()
127  @property
128  def query(self):
129  return self.editoreditor.GetQueryText()
130 
131  def event(self, e):
132  if e.type() == QtCore.QEvent.KeyPress and (e.key () == QtCore.Qt.Key_Return or e.key () == QtCore.Qt.Key_Enter):
133  self.accept()
134  return True
135  else:
136  return QtWidgets.QDialog.event(self, e)
137 
138  def _CheckNewline(self):
139  if self.editoreditor.GetQueryText().endswith("\n"):
140  self.accept()
def __init__(self, title, parent=None)
Selection Query.
Definition: query.hh:74
def stop()
Definition: init.py:140