Skip to content

Commit

Permalink
[39] Add enums constants to script filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
sidsukana committed Sep 4, 2013
1 parent ec8b217 commit 979400d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
6 changes: 6 additions & 0 deletions QSW/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "SWMainForm.h"
#include <QtGui/QApplication>
#include <QtGui/QDesktopWidget>

int main(int argc, char *argv[])
{
Expand All @@ -11,6 +12,11 @@ int main(int argc, char *argv[])
.arg(CLIENT_BUILD)
.arg(QSW_BUILD));

// Set position to center about desktop widget
QRect frameRect = form.frameGeometry();
frameRect.moveCenter(QDesktopWidget().availableGeometry().center());
form.move(frameRect.topLeft());

form.show();

return app.exec();
Expand Down
2 changes: 1 addition & 1 deletion QSW/SWDefines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
quint8 Locale = 0;

QString CLIENT_VERSION("3.3.5a");
QString QSW_BUILD("38");
QString QSW_BUILD("39");
QString CLIENT_BUILD("12340");

QString ProcFlagDesc[] =
Expand Down
2 changes: 2 additions & 0 deletions QSW/SWEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SWEnums
SWEnums();
~SWEnums();

EnumHash getEnums() const { return m_enums; }

Enumerator getSpellFamilies() const { return m_enums["SpellFamily"]; }
Enumerator getMechanics() const { return m_enums["Mechanic"]; }
Enumerator getSchools() const { return m_enums["School"]; }
Expand Down
59 changes: 36 additions & 23 deletions QSW/SWMainForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,8 @@ void TextEdit::insertCompletion(const QString& completion)
{
if (m_completer->widget() != this)
return;

QTextCursor tc = textCursor();
int extra = completion.length() - m_completer->completionPrefix().length();
tc.select(QTextCursor::WordUnderCursor);
tc.insertText(completion);
setTextCursor(tc);
Expand Down Expand Up @@ -634,57 +634,70 @@ void TextEdit::keyPressEvent(QKeyEvent *e)
}
}

bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space); // CTRL+E
if (!m_completer || !isShortcut) // do not process the shortcut when we have a completer
QTextEdit::keyPressEvent(e);

const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!m_completer || (ctrlOrShift && e->text().isEmpty()))
return;

static QString eow(" ~!@#$%^&*()_+{}|:\"<>?,/;'[]\\-="); // end of word
static QString eow(" ~!@#$%^&*_+{}|:\"<>?,/;'[]()\\-="); // end of word
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();

int pos = textCursor().positionInBlock();
bool complete = false;
for (qint32 i = completionPrefix.size() - 1; i != -1; --i)

// Check autocomplete
if (completionPrefix.size() >= 6 && pos >= 6 && completionPrefix.contains("spell."))
{
qint32 endIndx = completionPrefix.size() - i;
if (completionPrefix.at(i) == ' ')
// Remove all after first right whitespace about cursor position
for (qint32 i = pos; i < completionPrefix.size(); ++i)
{
m_completer->popup()->hide();
return;
if (completionPrefix.at(i) == ' ')
{
completionPrefix.remove(i, completionPrefix.size() - i);
break;
}
}
else if (completionPrefix.at(i) == '.')

// Remove all before first dot about cursor position
// and contains 'spell.'
for (qint32 i = pos - 1; i != -1; --i)
{
if (endIndx + 5 <= completionPrefix.size())
if (completionPrefix.at(i) == '.')
{
completionPrefix = completionPrefix.right(endIndx + 5);

if (completionPrefix.startsWith("spell."))
if (completionPrefix.mid(i - 5, 6).startsWith("spell."))
{
completionPrefix.remove(0, 6);
completionPrefix.remove(0, i + 1);
complete = true;
break;
}
}
break;
}
}

if (!isShortcut && e->key() != Qt::Key_Period && (hasModifier || e->text().isEmpty() || (/*completionPrefix.length() < 3 && */!complete)
|| eow.contains(e->text().right(1)))) {
m_completer->popup()->hide();
return;
if (!isShortcut && e->key() != Qt::Key_Period && (hasModifier || e->text().isEmpty() || !complete || eow.contains(e->text().right(1))))
{
m_completer->popup()->hide();
return;
}

if (completionPrefix != m_completer->completionPrefix()) {
if (completionPrefix != m_completer->completionPrefix())
{
m_completer->setCompletionPrefix(completionPrefix);
m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
}

if (completionPrefix == m_completer->currentCompletion())
{
m_completer->popup()->hide();
return;
}
QRect cr = cursorRect();
cr.setWidth(m_completer->popup()->sizeHintForColumn(0)
+ m_completer->popup()->verticalScrollBar()->sizeHint().width());
m_completer->complete(cr); // popup it up!
cr.setWidth(m_completer->popup()->sizeHintForColumn(0) + m_completer->popup()->verticalScrollBar()->sizeHint().width());
m_completer->complete(cr);
}

QAbstractItemModel* TextEdit::setupModel()
Expand Down
13 changes: 8 additions & 5 deletions QSW/SWSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ SWSearch::SWSearch(SWObject* sw, QObject* parent)
{
m_scriptEngine = new QScriptEngine(this);

QString text = m_form->getFilterText();
//text.replace(QRegExp("([A-Za-z]+)"), "spell.\\1");
m_script = m_scriptEngine->evaluate("(function() { return (" + text + "); })");
EnumHash enums = m_form->getEnums()->getEnums();

for (EnumHash::const_iterator itr = enums.begin(); itr != enums.end(); ++itr)
for (Enumerator::const_iterator itr2 = itr->begin(); itr2 != itr->end(); ++itr2)
m_scriptEngine->globalObject().setProperty(itr2.value(), qsreal(itr2.key()));

m_script = m_scriptEngine->evaluate("(function() { return (" + m_form->getFilterText() + "); })");
}

SWSearch::~SWSearch()
Expand All @@ -28,8 +32,7 @@ bool SWSearch::hasValue(SpellEntry const* spellInfo)
MetaSpell metaSpell;
metaSpell.setSpell(spellInfo);

QScriptValue objectValue = m_scriptEngine->newQObject(&metaSpell);
m_scriptEngine->globalObject().setProperty("spell", objectValue);
m_scriptEngine->globalObject().setProperty("spell", m_scriptEngine->newQObject(&metaSpell));

return m_script.call(QScriptValue()).toBool();
}
Expand Down

0 comments on commit 979400d

Please sign in to comment.