Commit e1ee8d1f authored by Nikita Sirgienko's avatar Nikita Sirgienko

[GSoC 2020] Add File Browser panel and two major bugfixes in Cantor Shell: bug...

[GSoC 2020] Add File Browser panel and two major bugfixes in Cantor Shell: bug with saving panel state and wrong logic of closing tabs
parent e3881ab8
Pipeline #26003 passed with stage
in 31 minutes and 7 seconds
......@@ -343,8 +343,15 @@ void CantorShell::addWorksheet(const QString& backendName)
{
connect(part, SIGNAL(setCaption(QString,QIcon)), this, SLOT(setTabCaption(QString,QIcon)));
connect(part, SIGNAL(worksheetSave(QUrl)), this, SLOT(onWorksheetSave(QUrl)));
connect(part, SIGNAL(requestOpenWorksheet(QUrl)), this, SLOT(load(QUrl)));
m_parts.append(part);
m_parts2Backends[part] = backend->id();
if (backend) // If backend empty (loading worksheet from file), then we connect to signal and wait
m_parts2Backends[part] = backend->id();
else
{
m_parts2Backends[part] = QString();
connect(part, SIGNAL(setBackendName(QString)), this, SLOT(updateBackendForPart(setBackendName)));
}
int tab = m_tabWidget->addTab(part->widget(), i18n("Session %1", sessionCount++));
m_tabWidget->setCurrentIndex(tab);
// Setting focus on worksheet view, because Qt clear focus of added widget inside addTab
......@@ -425,9 +432,20 @@ void CantorShell::updateWindowTitle(const QString& fileName)
void CantorShell::closeTab(int index)
{
if (!reallyClose(false))
if (index != -1)
{
return;
QWidget* widget = m_tabWidget->widget(index);
if (widget)
{
KParts::ReadWritePart* part= findPart(widget);
if (part && !reallyCloseThisPart(part))
return;
}
}
else
{
if (!reallyClose(false))
return;
}
QWidget* widget = nullptr;
......@@ -492,17 +510,23 @@ bool CantorShell::reallyClose(bool checkAllParts) {
return false;
}
}
if (m_part && m_part->isModified() ) {
return reallyCloseThisPart(m_part);
}
bool CantorShell::reallyCloseThisPart(KParts::ReadWritePart* part)
{
if (part && part->isModified() ) {
int want_save = KMessageBox::warningYesNoCancel( this,
i18n("The current project has been modified. Do you want to save it?"),
i18n("Save Project"));
switch (want_save) {
case KMessageBox::Yes:
m_part->save();
if(m_part->waitSaveComplete()) {
part->save();
if(part->waitSaveComplete()) {
return true;
} else {
m_part->setModified(true);
part->setModified(true);
return false;
}
case KMessageBox::Cancel:
......@@ -514,6 +538,7 @@ bool CantorShell::reallyClose(bool checkAllParts) {
return true;
}
void CantorShell::closeEvent(QCloseEvent* event) {
if(!reallyClose()) {
event->ignore();
......@@ -767,3 +792,10 @@ void CantorShell::saveDockPanelsState(KParts::ReadWritePart* part)
panelStatusGroup.writeEntry(m_parts2Backends[part], visiblePanelNames.join(QLatin1Char('\n')));
}
}
void CantorShell::updateBackendForPart(const QString& backend)
{
KParts::ReadWritePart* part=dynamic_cast<KParts::ReadWritePart*>(sender());
if (part && m_parts2Backends.contains(part) && m_parts2Backends[part].isEmpty())
m_parts2Backends[part] = backend;
}
......@@ -89,6 +89,7 @@ private Q_SLOTS:
void activateWorksheet(int index);
void setTabCaption(const QString& tab, const QIcon& icon);
void updateBackendForPart(const QString& backend);
void closeTab(int index = -1);
void showSettings();
......@@ -105,6 +106,7 @@ private:
void setupActions();
void closeEvent(QCloseEvent*) override;
bool reallyClose(bool checkAllParts = true);
bool reallyCloseThisPart(KParts::ReadWritePart* part);
void updateWindowTitle(const QString& fileName);
void saveDockPanelsState(KParts::ReadWritePart* part);
KParts::ReadWritePart* findPart(QWidget* widget);
......
......@@ -476,6 +476,8 @@ bool CantorPart::openFile()
if (rc) {
qDebug()<< "Worksheet successfully loaded in " << (float)timer.elapsed()/1000 << " seconds";
updateCaption();
if (m_worksheet && m_worksheet->session() && m_worksheet->session()->backend())
setBackendName(m_worksheet->session()->backend()->id());
// We modified, but it we load file now, so no need in save option
setModified(false);
}
......
......@@ -84,6 +84,8 @@ Q_SIGNALS:
void setCaption(const QString& caption, const QIcon& icon);
void showHelp(const QString& help);
void worksheetSave(const QUrl& url);
void requestOpenWorksheet(const QUrl& url);
void setBackendName(const QString& name);
public Q_SLOTS:
void updateCaption();
......
......@@ -12,3 +12,4 @@ endfunction()
add_subdirectory(helppanel)
add_subdirectory(variablemgr)
add_subdirectory(filebrowserpanel)
set(FileBrowserPanelPlugin_SRCS
filebrowserpanelplugin.cpp
)
add_panel(filebrowserpanelplugin ${FileBrowserPanelPlugin_SRCS})
target_link_libraries(cantor_filebrowserpanelplugin KF5::Parts)
/*
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
---
Copyright (C) 2020 Sirgienko Nikita <warquark@gmail.com>
*/
#include "filebrowserpanelplugin.h"
#include <QFileSystemModel>
#include <QTreeView>
#include <QHeaderView>
#include <QModelIndex>
#include <QStandardItem>
#include <QDesktopServices>
#include <QUrl>
#include <QDebug>
#include <QFileInfo>
#include <KParts/ReadOnlyPart>
QFileSystemModel* FileBrowserPanelPlugin::model = nullptr;
FileBrowserPanelPlugin::FileBrowserPanelPlugin(QObject* parent, const QList<QVariant>& args): Cantor::PanelPlugin(parent),
m_view(nullptr)
{
Q_UNUSED(args);
KParts::ReadOnlyPart* part = dynamic_cast<KParts::ReadOnlyPart*>(parent->parent());
if (part)
m_dirRoot = QFileInfo(part->url().toLocalFile()).absoluteDir().absolutePath();
else
m_dirRoot = QDir::currentPath();
}
FileBrowserPanelPlugin::~FileBrowserPanelPlugin()
{
if (m_view)
m_view->deleteLater();;
}
QWidget* FileBrowserPanelPlugin::widget()
{
if (!m_view)
{
if (!model)
{
model = new QFileSystemModel();
model->setRootPath(QDir::currentPath());
}
m_view = new QTreeView();
m_view->setModel(model);
m_view->setRootIndex(model->index(m_dirRoot));
// First column is name with the dir tree
for (int i = 1; i < model->columnCount(); i++)
m_view->setColumnHidden(i, true);
m_view->header()->hide();
connect(m_view, &QTreeView::doubleClicked, this, &FileBrowserPanelPlugin::handleDoubleClicked);
connect(this, SIGNAL(requestOpenWorksheet(QUrl)), parent()->parent(), SIGNAL(requestOpenWorksheet(QUrl)));
}
return m_view;
}
bool FileBrowserPanelPlugin::showOnStartup()
{
return false;
}
void FileBrowserPanelPlugin::handleDoubleClicked(const QModelIndex& index)
{
if (model->isDir(index))
return;
QVariant data = model->data(index, QFileSystemModel::FilePathRole);
if (data.isValid() && data.type() == QVariant::String)
{
const QString& filename = data.value<QString>();
const QUrl& url = QUrl::fromLocalFile(filename);
if (filename.endsWith(QLatin1String(".cws")) || filename.endsWith(QLatin1String(".ipynb")))
emit requestOpenWorksheet(url);
else
QDesktopServices::openUrl(url);
}
}
K_PLUGIN_FACTORY_WITH_JSON(filebrowserpanelplugin, "filebrowserpanelplugin.json", registerPlugin<FileBrowserPanelPlugin>();)
#include "filebrowserpanelplugin.moc"
/*
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
---
Copyright (C) 2020 Sirgienko Nikita <warquark@gmail.com>
*/
#ifndef _FILEBROWSERPANELPLUGIN_H
#define _FILEBROWSERPANELPLUGIN_H
#include "panelplugin.h"
class QTreeView;
class QModelIndex;
class QFileSystemModel;
class FileBrowserPanelPlugin : public Cantor::PanelPlugin
{
Q_OBJECT
public:
FileBrowserPanelPlugin (QObject* parent, const QList<QVariant>& args);
~FileBrowserPanelPlugin() override;
QWidget* widget() override;
bool showOnStartup() override;
Q_SIGNALS:
void requestOpenWorksheet(const QUrl&);
private Q_SLOTS:
void handleDoubleClicked(const QModelIndex&);
private:
QPointer<QTreeView> m_view;
QString m_dirRoot;
static QFileSystemModel* model;
};
#endif /* _FILEBROWSERPANELPLUGIN_H */
{
"KPlugin": {
"Dependencies": [],
"Description": "A panel for working with files inside Cantor",
"Description[ru]": "Панель для работы с файлами внутри Cantor",
"Icon": "",
"Id": "FileBrowserPanel",
"License": "GPL",
"Name": "File Browser",
"ServiceTypes": [
"Cantor/PanelPlugin"
]
},
"RequiredExtensions": ""
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment