Newer
Older
#include "MantidQtAPI/ScriptRepositoryView.h"
#include "MantidQtAPI/RepoModel.h"
#include <QSortFilterProxyModel>
#include <QDebug>
#include "MantidAPI/ScriptRepository.h"
#include "MantidAPI/ScriptRepositoryFactory.h"
#include "MantidKernel/ConfigService.h"
#include <QtConcurrentRun>
#include <QMessageBox>
#include <QTime>
#include <QCoreApplication>
#include <QLabel>
#include <QVBoxLayout>
#include <QPushButton>
#include <QFileDialog>
namespace MantidQt
{
namespace API
{
/** Allow the application to be alive while giving some time to this Widget to ensure that
the installation process is going on well.*/
void delay()
{
QTime dieTime= QTime::currentTime().addSecs(3);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
/**
TODO: it is necessary to make a better approach when a installation is required.
This is a temporary solution in order to allow the installation of the script repository at first run.
*/
int install_repository(){
using Mantid::API::ScriptRepositoryFactory;
Mantid::API::ScriptRepository_sptr repo_ptr = ScriptRepositoryFactory::Instance().create("GitScriptRepository");
try{
repo_ptr->update();
// QMessageBox::information(NULL, "Install Script Repository", "Script Repository Installed!\n");
}catch(Mantid::API::ScriptRepoException & ex){
qWarning() << "Update exception: " << ex.what() << endl;
return -1;
}
return 0;
}
/* Allow to update the script repositoy in background operation when the user tries to open the
ScriptRepository.
*/
int update_repository(){
using Mantid::API::ScriptRepositoryFactory;
Mantid::API::ScriptRepository_sptr repo_ptr = ScriptRepositoryFactory::Instance().create("GitScriptRepository");
try{
repo_ptr->update();
}catch(Mantid::API::ScriptRepoException & ex){
qWarning() << "Update of Script Repository failure: " << ex.what() << endl;
return -1;
}
return 0;
}
//----------------------------------------------------------------------------------------------
/** Constructor
*/
ScriptRepositoryView::ScriptRepositoryView(QWidget * parent):
QDialog(parent),
ui(new Ui::ScriptRepositoryView)
{
using Mantid::API::ScriptRepositoryFactory;
using Mantid::Kernel::ConfigServiceImpl;
using Mantid::Kernel::ConfigService;
Mantid::API::ScriptRepository_sptr repo_ptr = ScriptRepositoryFactory::Instance().create("GitScriptRepository");
if (!repo_ptr->isValid()){
// no repository cloned
if (QMessageBox::Ok != QMessageBox::question(this,"Install Script Repository?",
"The Script Repository allow you to share your scripts and to get mantid scripts from the developers and the community.\n"
"The installation may require a couple of minutes.\nWould you like to install it now?"
"\n\nMore Information: http://www.mantidproject.org/ScriptRepository",
QMessageBox::Ok|QMessageBox::Cancel)){
// user does not whant to install
close();
deleteLater();
return;
}
ConfigServiceImpl & config = ConfigService::Instance();
QString loc = QString::fromStdString(config.getString("ScriptLocalRepository"));
QString dir = QFileDialog::getExistingDirectory(this, tr("Where do you want to install Script Repository?"),
loc,
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
//configuring
if (dir.isEmpty())
{
QMessageBox::warning(this, "Installation Failed",
"Invalid Folder to install Script Repository!\n");
close();
deleteLater();
return;
}
QString local_path = dir + "/scriptRepository";
qDebug() << "setting scriptlocalrepository to " << local_path << "\n";
config.setString("ScriptLocalRepository", local_path.toStdString());
config.saveConfig(config.getUserFilename());
// installing in a new thread
QFuture<int> install = QtConcurrent::run(install_repository);
delay();
if (install.isResultReadyAt(0)){
if (install.resultAt(0) < 0){
QMessageBox::warning(this, "Installatin Failed",
"The installation of Script Repository Failed\n"
"It may be internet connection or firewall, or the proxy definition.\n"
"Look at the result log to get some hints\n");
close();
deleteLater();
}
}else{
// give some time to the thread to start
QLabel * inf = new QLabel("Running Script Repository Installation in background!\n"
"Please, check the Result Log to see the progress of the installation\n\n"
"After completing installation, please, reopen the Script Repository Interface\n",
this);
QPushButton * close = new QPushButton("Close");
connect(close, SIGNAL(clicked()), this, SLOT(close()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(inf);
layout->addWidget(close);
this->setLayout(layout);
return;
}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
ui->setupUi(this);
model = new RepoModel();
ui->repo_treeView->setModel(model);
ui->repo_treeView->setItemDelegateForColumn(1, new RepoDelegate(this));
ui->repo_treeView->setItemDelegateForColumn(2, new RepoDelegate(this));
ui->repo_treeView->setItemDelegateForColumn(3, new RepoDelegate(this));
/*
QSortFilterProxyModel * proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(model);
proxyModel->setFilterKeyColumn (4);
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
ui->repo_treeView->setModel(proxyModel);
*/
ui->repo_treeView->setColumnWidth(0,290);
ui->repo_treeView->setColumnHidden(3,true);
ui->repo_treeView->setColumnHidden(4,true);
connect(ui->repo_treeView, SIGNAL(activated(const QModelIndex &)),
this, SLOT(cell_activated(const QModelIndex&)));
connect(ui->repo_treeView, SIGNAL(clicked(const QModelIndex &)),
this, SLOT(cell_clicked(const QModelIndex&)));
connect(model, SIGNAL(fileDescription(const QString)),
ui->desc_textBrowser, SLOT(setText(const QString &)));
connect(model,SIGNAL(loadScript(const QString)),
this, SIGNAL(loadScript(const QString)));
// connect(ui->filter_lineEdit, SIGNAL(textChanged(QString)),
// this, SLOT(filterValues(QString)));
QFuture<int> update = QtConcurrent::run(update_repository);
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
ScriptRepositoryView::~ScriptRepositoryView()
{
delete ui;
}
void ScriptRepositoryView::filterValues(QString input){
ui->repo_treeView->expandAll();
QSortFilterProxyModel * model = qobject_cast<QSortFilterProxyModel*>( ui->repo_treeView->model());
if (model)
model->setFilterFixedString(input);
}
void ScriptRepositoryView::cell_activated(const QModelIndex & in){
QSortFilterProxyModel * proxyModel = qobject_cast<QSortFilterProxyModel*>( ui->repo_treeView->model());
if (proxyModel){
model->fileSelected(proxyModel->mapToSource(in));
return;
}
RepoModel * _model = qobject_cast<RepoModel*>(ui->repo_treeView->model());
if (_model){
_model->fileSelected(in);
return;
}
}
void ScriptRepositoryView::cell_clicked(const QModelIndex & in){
qDebug() << "Cell activated\n";
QSortFilterProxyModel * proxyModel = qobject_cast<QSortFilterProxyModel*>( ui->repo_treeView->model());
if (proxyModel){
model->entrySelected(proxyModel->mapToSource(in));
return;
}
RepoModel * _model = qobject_cast<RepoModel*>(ui->repo_treeView->model());
if (_model){
_model->entrySelected(in);
return;
}
//
}
} // namespace API
} // namespace Mantid