mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-27 14:30:00 -06:00
Add auto completion in SQL editor again
This commit is contained in:
@@ -176,20 +176,19 @@ void MainWindow::fileNew()
|
||||
void MainWindow::populateStructure()
|
||||
{
|
||||
ui->dbTreeWidget->model()->removeRows(0, ui->dbTreeWidget->model()->rowCount());
|
||||
completerModelTables.clear();
|
||||
completerModelsFields.clear();
|
||||
if(!db.isOpen())
|
||||
return;
|
||||
|
||||
db.updateSchema();
|
||||
QStringList tblnames = db.getBrowsableObjectNames();
|
||||
for(int i=0;i<ui->tabSqlAreas->count();i++)
|
||||
qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->widget(i))->setTableNames(tblnames);
|
||||
sqliteHighlighterLogUser->setTableNames(tblnames);
|
||||
sqliteHighlighterLogApp->setTableNames(tblnames);
|
||||
|
||||
// setup models for sqltextedit autocomplete
|
||||
QStandardItemModel* completerModel = new QStandardItemModel();
|
||||
completerModel->setRowCount(tblnames.count());
|
||||
completerModel->setColumnCount(1);
|
||||
completerModelTables.setRowCount(tblnames.count());
|
||||
completerModelTables.setColumnCount(1);
|
||||
|
||||
objectMap tab = db.getBrowsableObjects();
|
||||
int row = 0;
|
||||
@@ -198,7 +197,7 @@ void MainWindow::populateStructure()
|
||||
QString sName = it.value().getname();
|
||||
QStandardItem* item = new QStandardItem(sName);
|
||||
item->setIcon(QIcon(QString(":icons/%1").arg(it.value().gettype())));
|
||||
completerModel->setItem(row, 0, item);
|
||||
completerModelTables.setItem(row, 0, item);
|
||||
|
||||
// If it is a table add the field Nodes
|
||||
if((*it).gettype() == "table" || (*it).gettype() == "view")
|
||||
@@ -215,14 +214,17 @@ void MainWindow::populateStructure()
|
||||
fldItem->setIcon(QIcon(":/icons/field"));
|
||||
tablefieldmodel->setItem(fldrow, 0, fldItem);
|
||||
}
|
||||
// TODO:
|
||||
//ui->sqlTextEdit->addFieldCompleterModel(sName.toLower(), tablefieldmodel);
|
||||
completerModelsFields.insert(sName.toLower(), tablefieldmodel);
|
||||
}
|
||||
|
||||
}
|
||||
// TODO:
|
||||
//ui->sqlTextEdit->setDefaultCompleterModel(completerModel);
|
||||
// end setup models for sqltextedit autocomplete
|
||||
for(int i=0;i<ui->tabSqlAreas->count();i++)
|
||||
{
|
||||
SqlExecutionArea* sqlarea = qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->widget(i));
|
||||
sqlarea->setTableNames(tblnames);
|
||||
sqlarea->getEditor()->setDefaultCompleterModel(&completerModelTables);
|
||||
sqlarea->getEditor()->insertFieldCompleterModels(completerModelsFields);
|
||||
}
|
||||
|
||||
// fill the structure tab
|
||||
QMap<QString, QTreeWidgetItem*> typeToParentItem;
|
||||
@@ -1114,7 +1116,10 @@ void MainWindow::openSqlTab(bool resetCounter)
|
||||
tabNumber = 0;
|
||||
|
||||
// Create new tab, add it to the tab widget and select it
|
||||
QWidget* w = new SqlExecutionArea(this, &db);
|
||||
SqlExecutionArea* w = new SqlExecutionArea(this, &db);
|
||||
w->setTableNames(db.getBrowsableObjectNames());
|
||||
w->getEditor()->setDefaultCompleterModel(&completerModelTables);
|
||||
w->getEditor()->insertFieldCompleterModels(completerModelsFields);
|
||||
int index = ui->tabSqlAreas->addTab(w, QString("SQL %1").arg(++tabNumber));
|
||||
ui->tabSqlAreas->setCurrentIndex(index);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#define MAINFORM_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QStandardItemModel>
|
||||
#include "sqltextedit.h"
|
||||
#include "sqlitedb.h"
|
||||
|
||||
class QDragEnterEvent;
|
||||
@@ -70,6 +72,9 @@ private:
|
||||
|
||||
DBBrowserDB db;
|
||||
|
||||
QStandardItemModel completerModelTables;
|
||||
SqlTextEdit::FieldCompleterModelMap completerModelsFields;
|
||||
|
||||
void init();
|
||||
|
||||
void updateRecentFileActions();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define __SQLEXECUTIONAREA_H__
|
||||
|
||||
#include <QFrame>
|
||||
#include "sqltextedit.h"
|
||||
class QSplitter;
|
||||
class SqlTextEdit;
|
||||
class ExtendedTableWidget;
|
||||
@@ -10,6 +11,7 @@ class QVBoxLayout;
|
||||
class SQLiteSyntaxHighlighter;
|
||||
class SqliteTableModel;
|
||||
class DBBrowserDB;
|
||||
class QStandardItemModel;
|
||||
|
||||
class SqlExecutionArea : public QFrame
|
||||
{
|
||||
|
||||
@@ -22,8 +22,6 @@ SqlTextEdit::SqlTextEdit(QWidget* parent) :
|
||||
|
||||
SqlTextEdit::~SqlTextEdit()
|
||||
{
|
||||
clearFieldCompleterModelMap();
|
||||
delete m_defaultCompleterModel;
|
||||
}
|
||||
|
||||
void SqlTextEdit::setCompleter(QCompleter *completer)
|
||||
@@ -50,27 +48,26 @@ QCompleter* SqlTextEdit::completer() const
|
||||
|
||||
void SqlTextEdit::setDefaultCompleterModel(QAbstractItemModel *model)
|
||||
{
|
||||
delete m_defaultCompleterModel;
|
||||
m_defaultCompleterModel = model;
|
||||
m_Completer->setModel(m_defaultCompleterModel);
|
||||
}
|
||||
|
||||
void SqlTextEdit::clearFieldCompleterModelMap()
|
||||
{
|
||||
QAbstractItemModel* model;
|
||||
foreach (model, m_fieldCompleterMap)
|
||||
{
|
||||
delete model;
|
||||
}
|
||||
m_fieldCompleterMap.clear();
|
||||
}
|
||||
|
||||
QAbstractItemModel* SqlTextEdit::addFieldCompleterModel(const QString &tablename, QAbstractItemModel* model)
|
||||
{
|
||||
m_fieldCompleterMap[tablename] = model;
|
||||
return model;
|
||||
}
|
||||
|
||||
void SqlTextEdit::insertFieldCompleterModels(const FieldCompleterModelMap& fieldmap)
|
||||
{
|
||||
QMapIterator<QString, QAbstractItemModel*> i(fieldmap);
|
||||
while(i.hasNext())
|
||||
{
|
||||
i.next();
|
||||
addFieldCompleterModel(i.key(), i.value());
|
||||
}
|
||||
}
|
||||
|
||||
void SqlTextEdit::insertCompletion(const QString& completion)
|
||||
{
|
||||
if (m_Completer->widget() != this)
|
||||
|
||||
@@ -25,8 +25,8 @@ public:
|
||||
// map that associates table -> field model
|
||||
typedef QMap<QString,QAbstractItemModel*> FieldCompleterModelMap;
|
||||
|
||||
void clearFieldCompleterModelMap();
|
||||
QAbstractItemModel* addFieldCompleterModel(const QString& tablename, QAbstractItemModel *model);
|
||||
void insertFieldCompleterModels(const FieldCompleterModelMap& fieldmap);
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *e);
|
||||
|
||||
Reference in New Issue
Block a user