OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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_=QtGui.QTextCharFormat()
8  self.default_font_.setForeground(QtGui.QBrush(QtGui.QColor(0,0,0)))
9  self.error_font_=QtGui.QTextCharFormat()
10  self.error_font_.setForeground(QtGui.QBrush(QtGui.QColor(255,0,0)))
11  self.selection_edit_=QtWidgets.QTextEdit(self)
12  self.selection_edit_.setFixedHeight(40)
13  self.selection_edit_.updateGeometry()
14  self.status_=QtWidgets.QLabel(" ",self);
15  self.status_.setWordWrap(True)
16  self.status_.setMargin(0)
17  self.status_.setAlignment(QtCore.Qt.AlignRight)
18  self.status_.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum,
19  QtWidgets.QSizePolicy.Expanding))
20  vl=QtWidgets.QVBoxLayout()
21  vl.addWidget(self.selection_edit_)
22  self.no_bonds_=QtWidgets.QRadioButton('none')
23  self.ex_bonds_=QtWidgets.QRadioButton('exclusive')
24  self.in_bonds_=QtWidgets.QRadioButton('inclusive')
25  self.in_bonds_.setChecked(True)
26  self.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_)
32  hl=QtWidgets.QHBoxLayout()
33  l=QtWidgets.QLabel("bonds:")
34 
35  hl.addWidget(l)
36  hl.addSpacing(5)
37  hl.addWidget(self.no_bonds_)
38  hl.addWidget(self.ex_bonds_)
39  hl.addWidget(self.in_bonds_)
40  hl.addStretch(1)
41  vl.addLayout(hl)
42  vl.addWidget(self.match_res_)
43 
44  self.changing_text_=False;
45  self.selection_edit_.textChanged.connect(self._StartTimer)
46  self.timer_=QtCore.QTimer()
47  self.timer_.timeout.connect(self._UpdateMessage)
48 
49  def GetQueryFlags(self):
50  flags=0
51  if self.no_bonds_.isChecked():
52  flags|=mol.NO_BONDS
53  if self.ex_bonds_.isChecked():
54  flags|=mol.EXCLUSIVE_BONDS
55  if self.match_res_.isChecked():
56  flags|=mol.MATCH_RESIDUES
57  return flags
58 
59  def GetQuery(self):
60  return mol.Query(str(self.selection_edit_.toPlainText()))
61 
62  def GetQueryText(self):
63  return str(self.selection_edit_.toPlainText())
64 
65  def SetQueryFlags(self,flags):
66  self.no_bonds_.setChecked(flags & mol.NO_BONDS)
67  self.ex_bonds_.setChecked(flags & mol.EXCLUSIVE_BONDS)
68  self.in_bonds_.setChecked(not (flags & mol.NO_BONDS|mol.EXCLUSIVE_BONDS))
69  self.match_res_.setChecked(flags & mol.MATCH_RESIDUES)
70 
71  def SetQuery(self,query):
72  self.selection_edit_.setText(query)
73 
74  def _StartTimer(self):
75  self.timer_.stop()
76  self.timer_.start(500)
77 
78  def _UpdateMessage(self):
79  if self.changing_text_:
80  return
81  self.changing_text_ = True
82  query=self.GetQuery()
83 
84  cursor=self.selection_edit_.textCursor()
85  cursor.select(QtGui.QTextCursor.Document)
86  cursor.setCharFormat(self.default_font_)
87 
88  if query.IsValid():
89  self.status_.setText("")
90  else:
91  d=query.GetErrorDescription()
92  self.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_)
102  self.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)
110  l.addWidget(self.editor)
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.editor.selection_edit_.textChanged.connect(self._CheckNewline)
123 
124  @property
125  def query_flags(self):
126  return self.editor.GetQueryFlags()
127  @property
128  def query(self):
129  return self.editor.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.editor.GetQueryText().endswith("\n"):
140  self.accept()
Selection Query.
Definition: query.hh:74