#include <QtGui>
#include "treemodelcompleter.h"
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) {
createMenu();
completer = new TreeModelCompleter(this);
completer->setModel(modelFromFile(":/resources/treemodel.txt"));
QObject::connect(completer,
SIGNAL(highlighted
(const QModelIndex
&)),
this, SLOT(highlight(const QModelIndex&)));
modelLabel->setText(tr("Tree Model<br>(Double click items to edit)"));
separatorLabel->setText(tr("Tree Separator"));
separatorLineEdit->setText(completer->separator());
connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
completer, SLOT(setSeparator(const QString&)));
wrapCheckBox->setText(tr("Wrap around completions"));
wrapCheckBox->setChecked(completer->wrapAround());
connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(updateContentsLabel(const QString&)));
treeView->setModel(completer->model());
treeView->header()->hide();
treeView->expandAll();
lineEdit->setCompleter(completer);
layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1);
layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1);
layout->addWidget(wrapCheckBox, 4, 0);
layout->addWidget(contentsLabel, 5, 0, 1, 2);
layout->addWidget(lineEdit, 6, 0, 1, 2);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
setWindowTitle(tr("Tree Model Completer"));
lineEdit->setFocus();
}
void MainWindow::createMenu()
{
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
QMenu* fileMenu
= menuBar
()->addMenu
(tr
("File"));
fileMenu ->addAction(openAct);
fileMenu->addAction(exitAction);
}
void MainWindow::open()
{
QFileDialog::getOpenFileName(this, tr
("Open Bookmark File"),
}
{
if (!file.
open(QFile::ReadOnly))
QVector<QStandardItem *> parents(10);
parents[0] = model->invisibleRootItem();
while (!file.atEnd())
{
QString trimmedLine
= line.
trimmed();
if (line.isEmpty() || trimmedLine.isEmpty())
continue;
int nonws = re.indexIn(line);
int level = 0;
if (nonws == -1)
{
level = 0;
}
else {
if (line.startsWith("\t"))
{
level = re.cap(0).length();
}
else
{
level = re.cap(0).length()/4;
}
}
if (level+1 >= parents.size())
parents.resize(parents.size()*2);
item->setText(trimmedLine);
parents[level]->appendRow(item);
parents[level+1] = item;
}
return model;
}
void MainWindow::updateContentsLabel(const QString& sep)
{
contentsLabel
->setText
(QString(tr
("Type path from model above with items at each level separated by a '%1'")).
arg(sep
));
}
#include <QtGui>
#include "treemodelcompleter.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), completer(0), lineEdit(0)
{
createMenu();
completer = new TreeModelCompleter(this);
completer->setModel(modelFromFile(":/resources/treemodel.txt"));
completer->setSeparator(QLatin1String("1"));
QObject::connect(completer, SIGNAL(highlighted(const QModelIndex&)),
this, SLOT(highlight(const QModelIndex&)));
QWidget *centralWidget = new QWidget;
QLabel *modelLabel = new QLabel;
modelLabel->setText(tr("Tree Model<br>(Double click items to edit)"));
QLabel *separatorLabel = new QLabel;
separatorLabel->setText(tr("Tree Separator"));
QLineEdit *separatorLineEdit = new QLineEdit;
separatorLineEdit->setText(completer->separator());
connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
completer, SLOT(setSeparator(const QString&)));
QCheckBox *wrapCheckBox = new QCheckBox;
wrapCheckBox->setText(tr("Wrap around completions"));
wrapCheckBox->setChecked(completer->wrapAround());
connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
contentsLabel = new QLabel;
contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(separatorLineEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(updateContentsLabel(const QString&)));
treeView = new QTreeView;
treeView->setModel(completer->model());
treeView->header()->hide();
treeView->expandAll();
lineEdit = new QLineEdit;
lineEdit->setCompleter(completer);
QGridLayout *layout = new QGridLayout;
layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1);
layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1);
layout->addWidget(wrapCheckBox, 4, 0);
layout->addWidget(contentsLabel, 5, 0, 1, 2);
layout->addWidget(lineEdit, 6, 0, 1, 2);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
setWindowTitle(tr("Tree Model Completer"));
lineEdit->setFocus();
}
void MainWindow::createMenu()
{
QAction *exitAction = new QAction(tr("Exit"), this);
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QAction *openAct = new QAction(tr("Open"),this);
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
QMenu* fileMenu = menuBar()->addMenu(tr("File"));
fileMenu ->addAction(openAct);
fileMenu->addAction(exitAction);
}
void MainWindow::open()
{
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
QDir::currentPath() );
}
QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
return new QStringListModel(completer);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QStringList words;
QStandardItemModel *model = new QStandardItemModel(completer);
QVector<QStandardItem *> parents(10);
parents[0] = model->invisibleRootItem();
while (!file.atEnd())
{
QString line = file.readLine();
QString trimmedLine = line.trimmed();
if (line.isEmpty() || trimmedLine.isEmpty())
continue;
QRegExp re("^\\s+");
int nonws = re.indexIn(line);
int level = 0;
if (nonws == -1)
{
level = 0;
}
else {
if (line.startsWith("\t"))
{
level = re.cap(0).length();
}
else
{
level = re.cap(0).length()/4;
}
}
if (level+1 >= parents.size())
parents.resize(parents.size()*2);
QStandardItem *item = new QStandardItem;
item->setText(trimmedLine);
parents[level]->appendRow(item);
parents[level+1] = item;
}
QApplication::restoreOverrideCursor();
return model;
}
void MainWindow::updateContentsLabel(const QString& sep)
{
contentsLabel->setText(QString(tr("Type path from model above with items at each level separated by a '%1'")).arg(sep));
}
To copy to clipboard, switch view to plain text mode
Bookmarks