Theme Editor: Made auto-complete functional and enabled it by default. Added a small...
[kugel-rb.git] / utils / themeeditor / gui / syntaxcompleter.cpp
blob8baace46b1d456d9c8b05908bb6ef400e8c4ba52
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <QFile>
23 #include <QTreeWidgetItem>
25 #include "syntaxcompleter.h"
26 #include "codeeditor.h"
28 SyntaxCompleter::SyntaxCompleter(CodeEditor *parent) :
29 QTreeWidget(parent)
31 setHeaderHidden(true);
32 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
34 setWordWrap(true);
35 setColumnCount(2);
37 QObject::connect(this, SIGNAL(activated(QModelIndex)),
38 parent, SLOT(insertTag()));
40 QFile fin(":/resources/tagdb");
41 fin.open(QFile::ReadOnly | QFile::Text);
43 while(!fin.atEnd())
45 QString line(fin.readLine());
46 if(line.trimmed().length() == 0 || line.trimmed()[0] == '#')
47 continue;
49 QStringList split = line.split(":");
50 QStringList tag;
51 tag.append(split[0].trimmed());
52 tag.append(split[1].trimmed());
53 tags.insertMulti(split[0].trimmed().toLower(), tag);
56 filter("");
60 void SyntaxCompleter::filter(QString text)
62 clear();
64 for(QMap<QString, QStringList>::iterator i = tags.begin()
65 ; i != tags.end(); i++)
67 if(text.length() == 1)
69 if(text[0].toLower() != i.key()[0].toLower())
70 continue;
72 else if(text.length() == 2)
74 if(text[0].toLower() != i.key()[0].toLower() || i.key().length() < 2
75 || text[1].toLower() != i.key()[1].toLower())
76 continue;
78 else if(text.length() > 2)
80 hide();
83 addTopLevelItem(new QTreeWidgetItem(i.value()));
86 if(topLevelItemCount() > 0)
87 setCurrentIndex(indexFromItem(topLevelItem(0)));
89 resizeColumnToContents(0);
90 setColumnWidth(0, columnWidth(0) + 10); // Auto-resize is too small
91 resizeColumnToContents(1);