Crossfire Server, Trunk
AssetTextEdit.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "AssetTextEdit.h"
14 #include <QtWidgets>
15 #include "AssetFieldCompleter.h"
16 
17 AssetTextEdit::AssetTextEdit(QWidget *parent) : QTextEdit(parent) {
18  setAcceptRichText(false);
19  myCompleter = new AssetFieldCompleter(this);
20  myCompleter->setCompletionMode(QCompleter::PopupCompletion);
21  myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
22  myCompleter->setWidget(this);
23  connect(myCompleter, SIGNAL(activated(const QString &)), this, SLOT(insertCompletion(const QString &)));
24 }
25 
26 void AssetTextEdit::keyPressEvent(QKeyEvent *e) {
27  if (myCompleter && myCompleter->popup()->isVisible()) {
28  switch (e->key()) {
29  case Qt::Key_Enter:
30  case Qt::Key_Return:
31  case Qt::Key_Escape:
32  case Qt::Key_Tab:
33  case Qt::Key_Backtab:
34  e->ignore();
35  return;
36  default:
37  break;
38  }
39  }
40  QTextEdit::keyPressEvent(e);
41 
42  const QString completionPrefix = textUnderCursor();
43  if (completionPrefix != myCompleter->completionPrefix()) {
44  myCompleter->setCompletionPrefix(completionPrefix);
45  if (!completionPrefix.isEmpty()) {
46  myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
47  QRect cr = cursorRect();
48  cr.setWidth(myCompleter->popup()->sizeHintForColumn(0) + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
49  myCompleter->complete(cr);
50  }
51  }
52 }
53 
54 void AssetTextEdit::insertCompletion(const QString &completion) {
55  QTextCursor tc = textCursor();
56  int extra = completion.length() - myCompleter->completionPrefix().length();
57  tc.movePosition(QTextCursor::Left);
58  tc.movePosition(QTextCursor::EndOfWord);
59  tc.insertText(completion.right(extra));
60  setTextCursor(tc);
61 }
62 
64  QString item;
65  int end = textCursor().position(), start = end - 1;
66  while (start >= 0) {
67  QChar c = document()->characterAt(start);
68  if (c == '.' || c.isLetter()) {
69  --start;
70  item.prepend(c);
71  continue;
72  }
73  break;
74  }
75  while (end < document()->characterCount()) {
76  QChar c = document()->characterAt(end);
77  if (c.isLetter()) {
78  ++end;
79  item.append(c);
80  continue;
81  }
82  break;
83  }
84  return item;
85 }
c
static event_registration c
Definition: citylife.cpp:425
AssetTextEdit::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e) override
Definition: AssetTextEdit.cpp:26
AssetFieldCompleter
Definition: AssetFieldCompleter.h:25
AssetFieldCompleter.h
AssetTextEdit.h
AssetTextEdit::AssetTextEdit
AssetTextEdit(QWidget *parent)
Definition: AssetTextEdit.cpp:17
item
Definition: item.py:1
AssetTextEdit::myCompleter
AssetFieldCompleter * myCompleter
Definition: AssetTextEdit.h:39
AssetTextEdit::insertCompletion
void insertCompletion(const QString &completion)
Definition: AssetTextEdit.cpp:54
say.item
dictionary item
Definition: say.py:149
connect
Definition: connect.py:1
AssetTextEdit::textUnderCursor
QString textUnderCursor() const
Definition: AssetTextEdit.cpp:63