Commit 875874c1 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

merge project closing feature

parents 9d83ff8f d5f592f2
......@@ -12,6 +12,7 @@
#include <ktexteditor/application.h>
#include <ktexteditor/editor.h>
#include <ktexteditor/view.h>
#include <KConfigGroup>
#include <KLocalizedString>
......@@ -20,6 +21,8 @@
#include <QCoreApplication>
#include <QFileInfo>
#include <QTime>
#include <QMessageBox>
#include <QString>
#include <vector>
......@@ -203,6 +206,38 @@ KateProject *KateProjectPlugin::projectForDir(QDir dir, bool userSpecified)
return nullptr;
}
bool KateProjectPlugin::closeProject(KateProject *project)
{
QList< KTextEditor::Document* > documents = KTextEditor::Editor::instance()->application()->documents();
QVector< KTextEditor::Document* > projectDocuments;
QWidget* window = KTextEditor::Editor::instance()->application()->activeMainWindow()->window();
for(int i = 0; i<documents.size(); i++)
if(QUrl(project->baseDir()).isParentOf(documents[i]->url().adjusted(QUrl::RemoveScheme)))
projectDocuments.push_back(documents[i]);
QString title = i18n("Confirm project closing: ") + project->name();
QString text = i18n("Do you want to close ") + QString::number(projectDocuments.size()) + i18n(" documents and ") + project->name() + i18n(" project?");
QMessageBox confirmationBox;
if(QMessageBox::Yes == confirmationBox.question(window, title, text, QMessageBox::No | QMessageBox::Yes, QMessageBox::No))
{
for(int i = 0; i<projectDocuments.size(); i++)
KTextEditor::Editor::instance()->application()->closeDocument(projectDocuments[i]);
Q_EMIT pluginViewProjectClosing(project);
if(m_projects.removeOne(project))
{
m_fileWatcher.removePath(QFileInfo(project->fileName()).canonicalPath());
delete project;
return true;
}
}
return false;
}
KateProject *KateProjectPlugin::projectForUrl(const QUrl &url)
{
if (url.isEmpty() || !url.isLocalFile()) {
......
......@@ -59,6 +59,13 @@ public:
* @return project or null if not openable
*/
KateProject *projectForDir(QDir dir, bool userSpecified = false);
/**
* Search and close project for given project, if possible.
* @param project to search matching project for closing
* @return true if successful
*/
bool closeProject(KateProject *project);
/**
* Search and open project that contains given url, if possible.
......@@ -120,6 +127,13 @@ public:
ClickAction doubleClickAcion();
Q_SIGNALS:
/**
* Signal that for view to clean up
* @param project to close
*/
void pluginViewProjectClosing(KateProject *project);
/**
* Signal that a new project got created.
* @param project new created project
......
......@@ -34,6 +34,8 @@
#include <QTimer>
#include <QVBoxLayout>
#define PROJECTCLOSEICON "window-close"
K_PLUGIN_FACTORY_WITH_JSON(KateProjectPluginFactory, "kateprojectplugin.json", registerPlugin<KateProjectPlugin>();)
KateProjectPluginView::KateProjectPluginView(KateProjectPlugin *plugin, KTextEditor::MainWindow *mainWin)
......@@ -77,10 +79,14 @@ KateProjectPluginView::KateProjectPluginView(KateProjectPlugin *plugin, KTextEdi
m_reloadButton = new QToolButton(m_toolView);
m_reloadButton->setAutoRaise(true);
m_reloadButton->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
m_closeProjectButton = new QToolButton(m_toolView);
m_closeProjectButton->setAutoRaise(true);
m_closeProjectButton->setIcon(QIcon::fromTheme(QStringLiteral(PROJECTCLOSEICON)));
QHBoxLayout *layout = new QHBoxLayout();
layout->setSpacing(0);
layout->addWidget(m_projectsCombo);
layout->addWidget(m_reloadButton);
layout->addWidget(m_closeProjectButton);
m_toolView->layout()->addItem(layout);
m_toolView->layout()->setSpacing(0);
......@@ -109,6 +115,10 @@ KateProjectPluginView::KateProjectPluginView(KateProjectPlugin *plugin, KTextEdi
});
connect(m_projectsCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &KateProjectPluginView::slotCurrentChanged);
connect(m_reloadButton, &QToolButton::clicked, this, &KateProjectPluginView::slotProjectReload);
connect(m_closeProjectButton, &QToolButton::clicked, this, &KateProjectPluginView::slotProjectAboutToClose);
connect(m_plugin, &KateProjectPlugin::pluginViewProjectClosing, this, &KateProjectPluginView::slotProjectClose);
connect(m_gitStatusRefreshButton, &QToolButton::clicked, this, [this] {
if (auto widget = m_stackedgitViews->currentWidget()) {
qobject_cast<GitWidget *>(widget)->getStatus();
......@@ -175,6 +185,9 @@ KateProjectPluginView::KateProjectPluginView(KateProjectPlugin *plugin, KTextEdi
a = actionCollection()->addAction(QStringLiteral("projects_goto_index"), this, SLOT(slotProjectIndex()));
a->setText(i18n("Lookup"));
actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::ALT | Qt::Key_1));
a = actionCollection()->addAction(QStringLiteral("projects_close"), this, SLOT(slotProjectAboutToClose()));
a->setText(i18n("Close Project"));
a->setIcon(QIcon::fromTheme(QStringLiteral(PROJECTCLOSEICON)));
m_gotoSymbolActionAppMenu = a = actionCollection()->addAction(KStandardAction::Goto, QStringLiteral("projects_goto_symbol"), this, SLOT(slotGotoSymbol()));
// popup menu
......@@ -566,6 +579,36 @@ void KateProjectPluginView::slotProjectReload()
}
}
void KateProjectPluginView::slotProjectAboutToClose()
{
if (QWidget* current = m_stackedProjectViews->currentWidget())
{
m_plugin->closeProject(static_cast<KateProjectView *>(current)->project());
}
}
void KateProjectPluginView::slotProjectClose(KateProject *project)
{
const int index = m_plugin->projects().indexOf(project);
m_project2View.erase(m_project2View.find(project));
QWidget* stackedProjectViewsWidget = m_stackedProjectViews->widget(index);
m_stackedProjectViews->removeWidget(stackedProjectViewsWidget);
delete stackedProjectViewsWidget;
QWidget* stackedProjectInfoViewsWidget = m_stackedProjectInfoViews->widget(index);
m_stackedProjectInfoViews->removeWidget(stackedProjectInfoViewsWidget);
delete stackedProjectInfoViewsWidget;
QWidget* stackedgitViewsWidget = m_stackedgitViews->widget(index);
m_stackedgitViews->removeWidget(stackedgitViewsWidget);
delete stackedgitViewsWidget;
m_projectsCombo->removeItem(index);
m_projectsComboGit->removeItem(index);
}
QString KateProjectPluginView::currentWord() const
{
KTextEditor::View *kv = m_activeTextEditorView;
......
......@@ -183,6 +183,17 @@ private Q_SLOTS:
* This will trigger a reload with force.
*/
void slotProjectReload();
/**
* Getting project for others windows
* and closing project documents.
*/
void slotProjectAboutToClose();
/**
* Close current project.
*/
void slotProjectClose(KateProject *project);
/**
* Lookup current word
......@@ -195,6 +206,12 @@ private Q_SLOTS:
void slotGotoSymbol();
Q_SIGNALS:
/**
* Emitted if project is about to close.
*/
void pluginProjectClose(KateProject *project);
/**
* Emitted if projectFileName changed.
*/
......@@ -325,6 +342,11 @@ private:
* Reload button
*/
QToolButton *m_reloadButton;
/**
* Closeing button
*/
QToolButton *m_closeProjectButton;
/**
* Git status refresh button
......
......@@ -12,6 +12,7 @@
<Action name="projects_next_project"/>
<Action name="projects_goto_index" />
<Action name="projects_goto_symbol" />
<Action name="projects_close" />
</Menu>
</MenuBar>
<Menu name="ktexteditor_popup" noMerge="1">
......
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