23 #include <QTreeWidget>
24 #include <QTreeWidgetItem>
25 #include <QVBoxLayout>
27 #include <QHeaderView>
29 #include <QTextBrowser>
30 #include <QPushButton>
31 #include <QSpacerItem>
32 #include <QSizePolicy>
33 #include <QSortFilterProxyModel>
34 #include <QFileDialog>
35 #include <QMessageBox>
41 #define P3D_CONFIG_ENVVAR "P3D_CONFIG_PATH"
53 if (this->path ==
path)
57 for (
unsigned int i = 0; i <
children.size(); i++) {
66 for (
unsigned int i = 0; i <
children.size(); i++) {
77 QFileInfoList infos = QDir(
path).entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
80 for (QList<QFileInfo>::ConstIterator
it = infos.constBegin();
it != infos.constEnd(); ++
it) {
81 const QFileInfo* fi = &*
it;
82 if (fi->isDir() || fi->fileName().endsWith(
".se")) {
96 if (row < 0 || row > (
int)
children.size()) {
108 std::vector<QString> labels, paths;
109 for (
unsigned int i = 0; i <
children.size(); i++) {
116 for (
unsigned int i = 0; i < labels.size(); i++)
addChild(
new ExprTreeItem(
this, labels[i], paths[i]));
152 if (!
index.isValid())
return QModelIndex();
155 if (parentItem ==
root)
156 return QModelIndex();
158 return createIndex(parentItem->
row, 0, parentItem);
161 QModelIndex
index(
int row,
int column,
const QModelIndex&
parent = QModelIndex())
const {
162 if (!hasIndex(row, column,
parent))
163 return QModelIndex();
164 else if (!
parent.isValid())
168 return createIndex(row, column, item->
getChild(row));
189 QVariant
data(
const QModelIndex&
index,
int role = Qt::DisplayRole)
const {
190 if (!
index.isValid())
return QVariant();
191 if (role != Qt::DisplayRole)
return QVariant();
196 return QVariant(item->
label);
199 QModelIndex
find(QString path) {
208 std::cerr <<
"found it " << std::endl;
209 return createIndex(item->
row, 0, item);
212 return QModelIndex();
227 if (sourceParent.isValid() && sourceModel()->data(sourceParent).toString().contains(filterRegExp()))
229 QString data = sourceModel()->data(sourceModel()->
index(sourceRow, 0, sourceParent)).toString();
230 bool keep = data.contains(filterRegExp());
232 QModelIndex subIndex = sourceModel()->index(sourceRow, 0, sourceParent);
233 if (subIndex.isValid()) {
234 for (
int i = 0; i < sourceModel()->rowCount(subIndex); ++i) keep = keep ||
filterAcceptsRow(i, subIndex);
243 : QWidget(parent), editor(editor), _context(
""), _searchPath(
""), _applyOnSelect(true) {
244 QVBoxLayout* rootLayout =
new QVBoxLayout;
245 rootLayout->setMargin(0);
246 this->setLayout(rootLayout);
248 QHBoxLayout* searchAndClearLayout =
new QHBoxLayout();
251 searchAndClearLayout->addWidget(
exprFilter, 2);
252 QPushButton* clearFilterButton =
new QPushButton(
"X");
253 clearFilterButton->setFixedWidth(24);
254 searchAndClearLayout->addWidget(clearFilterButton, 1);
255 rootLayout->addLayout(searchAndClearLayout);
256 connect(clearFilterButton, SIGNAL(clicked()), SLOT(
clearFilter()));
265 treeNew->setHeaderHidden(
true);
266 rootLayout->addWidget(
treeNew);
268 treeNew->setSelectionMode(QAbstractItemView::SingleSelection);
269 connect(
treeNew->selectionModel(),
270 SIGNAL(currentChanged(
const QModelIndex&,
const QModelIndex&)),
275 labels.append(QString::fromStdString(name));
276 paths.append(QString::fromStdString(path));
286 QModelIndex sel =
treeNew->currentIndex();
288 QModelIndex realCurrent =
proxyModel->mapToSource(sel);
290 return item->
path.toStdString();
292 return std::string(
"");
307 if (current.isValid()) {
308 QModelIndex realCurrent =
proxyModel->mapToSource(current);
310 QString path = item->
path;
311 if (path.endsWith(
".se")) {
312 std::ifstream
file(path.toStdString().c_str());
313 std::string fileContents((std::istreambuf_iterator<char>(
file)), std::istreambuf_iterator<char>());
342 QString path = QFileDialog::getSaveFileName(
this,
"Save Expression", QString::fromStdString(
_userExprDir),
"*.se");
344 if (path.length() > 0) {
345 std::ofstream
file(path.toStdString().c_str());
347 QString msg = QString(
"Could not open file %1 for writing").arg(path);
348 QMessageBox::warning(
this,
"Error", QString(
"<font face=fixed>%1</font>").arg(msg));
360 QString path = QFileDialog::getSaveFileName(
this,
"Save Expression", QString::fromStdString(
_localExprDir),
"*.se");
362 if (path.length() > 0) {
363 std::ofstream
file(path.toStdString().c_str());
365 QString msg = QString(
"Could not open file %1 for writing").arg(path);
366 QMessageBox::warning(
this,
"Error", QString(
"<font face=fixed>%1</font>").arg(msg));
379 if (path.length() == 0) {
383 std::ofstream
file(path.c_str());
386 QString(
"Could not open file %1 for writing. Is it read-only?").arg(QString::fromStdString(path));
387 QMessageBox::warning(
this,
"Error", QString(
"<font face=fixed>%1</font>").arg(msg));
400 char* homepath = getenv(
"HOME");
402 std::string path = std::string(homepath) +
"/" +
context +
"/expressions/";
403 if (QDir(QString(path.c_str())).exists()) {
405 addPath(
"My Expressions", path);
417 bool enableLocal =
false;
425 if (!env)
return enableLocal;
436 std::string configFile = std::string(env) +
"/config.txt";
437 std::ifstream
file(configFile.c_str());
446 file.getline(buffer, 1024);
448 if (key ==
"ExpressionDir") {
449 std::string label, path;
452 if (QDir(QString(path.c_str())).exists())
addPath(label, path);
453 }
else if (key ==
"ExpressionSubDir") {
457 if (QDir(QString(path.c_str())).exists()) {
464 }
else if (key ==
"GlobalRepo") {
467 path +=
"/expressions/";
468 if (QDir(QString(path.c_str())).exists())
addPath(
"Global", path);
469 }
else if (key ==
"LocalRepo") {
472 path +=
"/expressions/";
474 if (QDir(QString(path.c_str())).exists()) {
508 file.getline(buffer, 1024);