Commit 8614644b authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Allow creating new file/folder in project


Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent 0d707308
......@@ -142,6 +142,16 @@ public:
return m_file2Item ? m_file2Item->value(file) : nullptr;
}
/**
* add a new file to the project
*/
void addFile(const QString &file, KateProjectItem *item)
{
if (m_file2Item && item) {
(*m_file2Item)[file] = item;
}
}
/**
* Access to project index.
* May be null.
......
......@@ -22,10 +22,62 @@
#include <QClipboard>
#include <QFileInfo>
#include <QIcon>
#include <QLineEdit>
#include <QMenu>
#include <QMimeDatabase>
#include <QMimeType>
#include <QPushButton>
#include <QStandardPaths>
#include <QVBoxLayout>
class AskNameDialog : public QDialog
{
public:
struct Name {
Name(const QString &n, bool s)
: name{n}
, success{s}
{
}
QString name;
bool success = false;
};
AskNameDialog(QWidget *parent = nullptr)
: QDialog(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
layout->addWidget(&m_lineEdit);
QHBoxLayout *hl = new QHBoxLayout;
hl->addWidget(&m_addBtn);
hl->addWidget(&m_cancelBtn);
m_addBtn.setText(i18n("Add"));
m_cancelBtn.setText(i18n("Cancel"));
connect(&m_addBtn, &QPushButton::clicked, this, &QDialog::accept);
connect(&m_cancelBtn, &QPushButton::clicked, this, &QDialog::reject);
layout->addLayout(hl);
}
Name askName()
{
int res = exec();
bool suc = res == QDialog::Accepted;
if (!suc || m_lineEdit.text().isEmpty()) {
return {{}, false};
}
return {m_lineEdit.text(), true};
}
private:
QLineEdit m_lineEdit;
QPushButton m_addBtn;
QPushButton m_cancelBtn;
};
void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelIndex &index, const QPoint &pos, KateProjectViewTree *parent)
{
......@@ -34,6 +86,13 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
*/
QMenu menu;
QAction *addFile = nullptr;
QAction *addFolder = nullptr;
if (index.data(KateProjectItem::TypeRole).toInt() == KateProjectItem::Directory) {
addFile = menu.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("&Add File"));
addFolder = menu.addAction(QIcon::fromTheme(QStringLiteral("folder-new")), i18n("&Add Folder"));
}
/**
* Copy Path
*/
......@@ -120,6 +179,18 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
parent->edit(index);
} else if (action == fileHistory) {
showFileHistory(index.data(Qt::UserRole).toString());
} else if (action == addFile) {
AskNameDialog d;
auto name = d.askName();
if (name.success) {
parent->addFile(index, name.name);
}
} else if (action == addFolder) {
AskNameDialog d;
auto name = d.askName();
if (name.success) {
parent->addDirectory(index, name.name);
}
} else {
// One of the git actions was triggered
}
......
......@@ -20,13 +20,6 @@ class KateProjectTreeViewContextMenu : public QObject
{
Q_OBJECT
public:
/**
* construct project view for given project
* @param pluginView our plugin view
* @param project project this view is for
*/
KateProjectTreeViewContextMenu() = default;
/**
* our project.
* @return project
......
......@@ -15,6 +15,8 @@
#include <QContextMenuEvent>
#include <KLocalizedString>
KateProjectViewTree::KateProjectViewTree(KateProjectPluginView *pluginView, KateProject *project)
: m_pluginView(pluginView)
, m_project(project)
......@@ -108,6 +110,60 @@ void KateProjectViewTree::openSelectedDocument()
}
}
void KateProjectViewTree::addFile(const QModelIndex &idx, const QString &fileName)
{
auto proxyModel = static_cast<QSortFilterProxyModel *>(model());
auto index = proxyModel->mapToSource(idx);
auto item = m_project->model()->itemFromIndex(index);
const QString fullFileName = index.data(Qt::UserRole).toString() + QLatin1Char('/') + fileName;
/**
* Create an actual file on disk
*/
QFile f(fullFileName);
bool created = f.open(QIODevice::WriteOnly);
if (!created) {
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), QStringLiteral("Error"));
genericMessage.insert(QStringLiteral("category"), i18n("Project"));
genericMessage.insert(QStringLiteral("categoryIcon"), QIcon::fromTheme(QStringLiteral("document-new")));
genericMessage.insert(QStringLiteral("text"), i18n("Failed to create file: %1, Error: %2", fileName, f.errorString()));
Q_EMIT m_pluginView->message(genericMessage);
return;
}
KateProjectItem *i = new KateProjectItem(KateProjectItem::File, fileName);
i->setData(fullFileName, Qt::UserRole);
item->appendRow(i);
m_project->addFile(fileName, i);
item->sortChildren(0);
}
void KateProjectViewTree::addDirectory(const QModelIndex &idx, const QString &name)
{
auto proxyModel = static_cast<QSortFilterProxyModel *>(model());
auto index = proxyModel->mapToSource(idx);
auto item = m_project->model()->itemFromIndex(index);
const QString fullDirName = index.data(Qt::UserRole).toString() + QLatin1Char('/') + name;
QDir dir(index.data(Qt::UserRole).toString());
if (!dir.mkdir(name)) {
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), QStringLiteral("Error"));
genericMessage.insert(QStringLiteral("category"), i18n("Project"));
genericMessage.insert(QStringLiteral("categoryIcon"), QIcon::fromTheme(QStringLiteral("folder-new")));
genericMessage.insert(QStringLiteral("text"), i18n("Failed to create dir: %1", name));
Q_EMIT m_pluginView->message(genericMessage);
return;
}
KateProjectItem *i = new KateProjectItem(KateProjectItem::Directory, name);
i->setData(fullDirName, Qt::UserRole);
item->appendRow(i);
item->sortChildren(0);
}
void KateProjectViewTree::slotClicked(const QModelIndex &index)
{
/**
......
......@@ -54,6 +54,16 @@ public:
*/
void openSelectedDocument();
/**
* Add a new file
*/
void addFile(const QModelIndex &idx, const QString &fileName);
/**
* Add a new directory
*/
void addDirectory(const QModelIndex &idx, const QString &name);
private Q_SLOTS:
/**
* item got clicked, do stuff, like open document
......
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