NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstgraphicslinkdialog.cpp
1 #include "qnstgraphicslinkdialog.h"
2 
3 #include <QDesktopWidget>
4 #include <QApplication>
5 #include <QStringList>
6 
7 #include <QKeyEvent>
8 #include <QtGui/QListView>
9 #include <QtGui/QStringListModel>
10 #include <QDebug>
11 
12 CompleteLineEdit::CompleteLineEdit(QStringList words, QWidget *parent)
13  : QLineEdit(parent), words(words)
14 {
15  listView = new QListView(this);
16  model = new QStringListModel(this);
17 
18  listView->setWindowFlags(Qt::ToolTip);
19  listView->setUniformItemSizes(true);
20 
21  installEventFilter(this);
22 
23  connect(this, SIGNAL(textChanged(const QString &)),
24  this, SLOT(setCompleter(const QString &)));
25 
26  connect(listView, SIGNAL(clicked(const QModelIndex &)),
27  this, SLOT(completeText(const QModelIndex &)));
28 }
29 
30 void CompleteLineEdit::setStringList(const QStringList &words)
31 {
32  this->words = words;
33 }
34 
35 bool CompleteLineEdit::eventFilter(QObject *object, QEvent *event)
36 {
37  if (object == this && event->type() == QEvent::KeyPress)
38  {
39  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
40  if (keyEvent->key() == Qt::Key_Tab)
41  {
42  if(!listView->isHidden())
43  {
44  QModelIndex currentIndex = listView->currentIndex();
45  if (currentIndex.isValid())
46  {
47  QString text = listView->currentIndex().data().toString();
48  setText(text);
49  }
50  listView->hide();
51  }
52  }
53  }
54  return false;
55 }
56 
57 void CompleteLineEdit::hideEvent ( QHideEvent * event )
58 {
59  listView->hide();
60 }
61 
62 // void CompleteLineEdit::focusOutEvent(QFocusEvent *e)
63 // {
64 // QLineEdit::focusOutEvent(e);
65 // listView->hide();
66 // }
67 
68 void CompleteLineEdit::focusInEvent(QFocusEvent *e)
69 {
70  setCompleter("");
71  QLineEdit::focusInEvent(e);
72 }
73 
74 void CompleteLineEdit::keyPressEvent(QKeyEvent *e)
75 {
76  if (!listView->isHidden())
77  {
78  int key = e->key();
79  int count = listView->model()->rowCount();
80  QModelIndex currentIndex = listView->currentIndex();
81 
82  if (Qt::Key_Down == key)
83  {
84  int row = currentIndex.row() + 1;
85  if (row >= count)
86  row = 0;
87 
88  QModelIndex index = listView->model()->index(row, 0);
89  listView->setCurrentIndex(index);
90  }
91  else if (Qt::Key_Up == key)
92  {
93  int row = currentIndex.row() - 1;
94  if (row < 0)
95  {
96  row = count - 1;
97  }
98 
99  QModelIndex index = listView->model()->index(row, 0);
100  listView->setCurrentIndex(index);
101  }
102  else if (Qt::Key_Escape == key)
103  {
104  listView->hide();
105  }
106  else if (Qt::Key_Enter == key ||
107  Qt::Key_Return == key )
108  {
109  if (currentIndex.isValid())
110  {
111  QString text = listView->currentIndex().data().toString();
112  setText(text);
113  }
114  listView->hide();
115  }
116  else
117  {
118  // listView->hide();
119  QLineEdit::keyPressEvent(e);
120  }
121  }
122  else
123  {
124  QLineEdit::keyPressEvent(e);
125  }
126 }
127 
128 void CompleteLineEdit::setCompleter(const QString &text)
129 {
130  /*if (text.isEmpty())
131  {
132  listView->hide();
133  return;
134  }
135 
136  if ((text.length() > 1) && (!listView->isHidden()))
137  return;
138  */
139 
140  QStringList sl;
141  foreach(QString word, words)
142  {
143  if (word.toLower().contains(text.toLower()))
144  {
145  sl << word;
146  }
147  }
148 
149  model->setStringList(sl);
150  listView->setModel(model);
151 
152  if (model->rowCount() == 0)
153  return;
154 
155  // Position the text edit
156  listView->setMinimumWidth(width());
157  listView->setMaximumWidth(width());
158 
159  QPoint p(0, height());
160  int x = mapToGlobal(p).x();
161  int y = mapToGlobal(p).y() + 1;
162 
163  listView->move(x, y);
164  listView->show();
165 }
166 
167 void CompleteLineEdit::completeText(const QModelIndex &index)
168 {
169  QString text = index.data().toString();
170  setText(text);
171  listView->hide();
172 }
173 
174 QnstGraphicsLinkDialog::QnstGraphicsLinkDialog(QWidget* parent)
175  : QDialog(parent), firstTime(true)
176 {
177  form.setupUi(this);
178 
179  connLineEdit = new CompleteLineEdit(QStringList());
180  this->form.gridLayout_2->addWidget(connLineEdit, 0, 1);
181 
182  connect(connLineEdit, SIGNAL(textChanged(QString)),
183  SLOT(adjustBinds(QString)));
184 
185  changeModel = true;
186 }
187 
188 QnstGraphicsLinkDialog::~QnstGraphicsLinkDialog()
189 {
190 
191 }
192 
193 void QnstGraphicsLinkDialog::init(QMap<QString, QnstConnector*> connectors)
194 {
195  this->connectors = connectors;
196 
197  form.cbCondition->setEnabled(false);
198  form.cbCondition->clear();
199 
200  form.cbAction->setEnabled(false);
201  form.cbAction->clear();
202 
203  QStringList strConn;
204  foreach(QnstConnector* conn, connectors.values())
205  {
206  strConn << conn->getName();
207  }
208 
209  if (strConn.count() > 0){
210  strConn << "----------";
211  }
212 
213  strConn << "New...";
214 
215  connLineEdit->setStringList(strConn);
216 
217  // Center the Dialog if this is the first time that we are openning
218  // QnstGraphicsLinkDialog
219  if(firstTime)
220  {
221  setMinimumWidth(350);
222  updateGeometry();
223  QWidget *screen = NULL;
224  QRect screenGeometry;
225 
226  if(this->parentWidget())
227  {
228  screen = QApplication::desktop()->screen(
229  QApplication::desktop()->screenNumber(this->parentWidget())
230  );
231 
232  screenGeometry = QApplication::desktop()->screenGeometry(this->parentWidget());
233  }
234  else
235  {
236  screenGeometry = QApplication::desktop()->screenGeometry();
237  }
238 
239  int screenWidth = screenGeometry.width();
240  int screenHeight = screenGeometry.height();
241 
242  int x = screenGeometry.x() + (screenWidth - this->width()) / 2;
243  int y = screenGeometry.y() + (screenHeight - this->height()) / 2;
244 
245  this->move(x, y);
246  firstTime = false;
247  }
248 }
249 
250 void QnstGraphicsLinkDialog::adjustBinds(QString conn)
251 {
252  if (conn == "" || conn == "----------")
253  {
254  form.cbCondition->setEnabled(false);
255  form.cbCondition->clear();
256 
257  form.cbAction->setEnabled(false);
258  form.cbAction->clear();
259 
260  }
261  else if (conn == "New...")
262  {
263  form.cbCondition->setEnabled(true);
264  form.cbCondition->clear();
265 
266  form.cbCondition->addItem("onBegin");
267  form.cbCondition->addItem("onEnd");
268  form.cbCondition->addItem("onSelection");
269  form.cbCondition->addItem("onResume");
270  form.cbCondition->addItem("onPause");
271 
272  form.cbAction->setEnabled(true);
273  form.cbAction->clear();
274 
275  form.cbAction->addItem("start");
276  form.cbAction->addItem("stop");
277  form.cbAction->addItem("resume");
278  form.cbAction->addItem("pause");
279  form.cbAction->addItem("set");
280  }
281  else
282  {
283  QnstConnector* oconn = NULL;
284 
285  if(connectors.contains(conn))
286  {
287  oconn = connectors[conn];
288  }
289  else
290  {
291  form.cbAction->clear();
292  form.cbCondition->clear();
293  return;
294  }
295 
296  form.cbCondition->setEnabled(true);
297  form.cbCondition->clear();
298 
299  if (oconn->getName() == conn)
300  {
301  foreach(QString cond, oconn->getConditions().values())
302  {
303  form.cbCondition->addItem(cond);
304  }
305  }
306 
307  form.cbAction->setEnabled(true);
308  form.cbAction->clear();
309 
310  if (oconn->getName() == conn)
311  {
312  foreach(QString act, oconn->getActions().values()){
313  form.cbAction->addItem(act);
314  }
315  }
316  }
317 }
318 
319 void QnstGraphicsLinkDialog::showEvent(QShowEvent *evt)
320 {
321  this->connLineEdit->setText("");
322  this->connLineEdit->setFocus();
323 }
324 
325 QString QnstGraphicsLinkDialog::getCurrentConnector()
326 {
327  return this->connLineEdit->text();
328 }