/*------------------------------------------------------------------------------ -- This file is a part of the QLop Software -- Copyright (C) 2015, Plasma Physics Laboratory - CNRS -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------------*/ /*-- Author : Alexis Jeandet -- Mail : alexis.jeandet@member.fsf.org ----------------------------------------------------------------------------*/ #include "filebrowser.h" #include "ui_filebrowser.h" #include #include FileBrowser::FileBrowser(QWidget *parent) : QDockWidget(parent), ui(new Ui::FileBrowser) { this->doubleClickEater = new DoubleClickEater(); ui->setupUi(this); this->model = new FileSystemModel(this); this->model->setRootPath(QDir::currentPath()); this->view = new QTreeView(); this->ui->gridLayout->addWidget(this->view,1,0,1,-1); this->view->setModel(this->model); this->cd(model->rootPath()); connect(this->view,SIGNAL(clicked(QModelIndex)),this,SLOT(clicked(QModelIndex))); connect(this->view,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClicked(QModelIndex))); connect(this->view,SIGNAL(activated(QModelIndex)),this,SLOT(activated(QModelIndex))); connect(this->ui->ListViewQpb,SIGNAL(clicked()),this,SLOT(changeToListView())); connect(this->ui->TreeViewQpb,SIGNAL(clicked()),this,SLOT(changeToTreeView())); connect(this->ui->parentDirQpb,SIGNAL(clicked()),this,SLOT(parentDir())); view->setSelectionMode(QAbstractItemView::ExtendedSelection); view->setDragEnabled(true); view->setAcceptDrops(true); view->setDropIndicatorShown(true); view->setDragDropMode(QAbstractItemView::DragDrop); this->pathCompleter = new QCompleter(this->model,this); this->ui->pathLineEdit->setCompleter(pathCompleter); this->pathLineEditEnterEditMode(false); this->setFocusPolicy(Qt::StrongFocus); this->ui->pathLineEdit->installEventFilter(this->doubleClickEater); connect(this->doubleClickEater,SIGNAL(doubleClicked()),this,SLOT(pathLineEditDblClicked())); } FileBrowser::~FileBrowser() { delete ui; } void FileBrowser::setNameFilters(const QStringList &filters,bool disables) { this->model->setNameFilters(filters); this->model->setNameFilterDisables(disables); } void FileBrowser::changeToTreeView() { this->ui->gridLayout->removeWidget(this->view); delete this->view; this->view = new QTreeView(); this->ui->gridLayout->addWidget(this->view,1,0,1,-1); this->view->setModel(this->model); connect(this->view,SIGNAL(clicked(QModelIndex)),this,SLOT(clicked(QModelIndex))); connect(this->view,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClicked(QModelIndex))); this->cd(model->rootPath()); view->setSelectionMode(QAbstractItemView::ExtendedSelection); view->setDragEnabled(true); view->setAcceptDrops(true); view->setDropIndicatorShown(true); } void FileBrowser::changeToListView() { this->ui->gridLayout->removeWidget(this->view); delete this->view; this->view = new QListView(); this->ui->gridLayout->addWidget(this->view,1,0,1,-1); this->view->setModel(this->model); connect(this->view,SIGNAL(clicked(QModelIndex)),this,SLOT(clicked(QModelIndex))); connect(this->view,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClicked(QModelIndex))); this->cd(model->rootPath()); view->setSelectionMode(QAbstractItemView::ExtendedSelection); view->setDragEnabled(true); view->setAcceptDrops(true); view->setDropIndicatorShown(true); } void FileBrowser::clicked(QModelIndex index) { QString file=model->filePath(index); if(QFile::exists(file)) emit fileClicked(file); } void FileBrowser::doubleClicked(QModelIndex index) { if(model->isDir(index)) { this->cd(model->filePath(index)); } else { QString file=model->filePath(index); if(QFile::exists(file)) emit fileDoubleClicked(file); } } void FileBrowser::activated(const QModelIndex &index) { doubleClicked(index); } void FileBrowser::parentDir() { this->cd(model->rootPath()+"/.."); } void FileBrowser::cd(const QString &newPath) { model->setRootPath(newPath); this->view->setRootIndex(model->index(model->rootPath())); this->ui->pathLineEdit->setText(model->rootPath()); } void FileBrowser::pathLineEditDblClicked() { if(this->ui->pathLineEdit->isReadOnly()) pathLineEditEnterEditMode(true); } void FileBrowser::changeEvent(QEvent *e) { QDockWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void FileBrowser::keyPressEvent(QKeyEvent *e) { switch (e->key()) { case Qt::Key_L: if(e->modifiers()==Qt::ControlModifier) { pathLineEditEnterEditMode(true); e->accept(); } break; case Qt::Key_Return: if(!this->view->hasFocus()) { if(QFile::exists(this->ui->pathLineEdit->text())) { this->cd(this->ui->pathLineEdit->text()); e->accept(); } pathLineEditEnterEditMode(false); } break; case Qt::Key_Backspace: if(this->view->hasFocus()) { parentDir(); } break; default: break; } if(!e->isAccepted()) QDockWidget::keyPressEvent(e); } void FileBrowser::pathLineEditEnterEditMode(bool enter) { this->ui->pathLineEdit->setReadOnly(!enter); this->ui->pathLineEdit->setDisabled(!enter); if(enter) this->ui->pathLineEdit->setFocus(); } bool DoubleClickEater::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::MouseButtonDblClick) { emit doubleClicked(); return true; } else { // standard event processing return QObject::eventFilter(obj, event); } }